A Shader is a program designed to run on some stage of a graphics processor. Its purpose is to execute one of the programmable stages of the rendering pipeline.
The rendering pipeline defines certain sections to be programmable. Each of these sections, or stages, represents a particular type of programmable processing. Each stage has a set of inputs and outputs, which are passed from prior stages and on to subsequent stages (whether programmable or not).
Shaders are written in the OpenGL Shading Language. The OpenGL rendering pipeline defines the following shader stages:
- Vertex Shaders
- Tessellation Control and Evaluation Shaders (requires GL 4.0 or ARB_tessellation_shader)
- Geometry Shaders
- Fragment Shaders
- Compute Shaders (requires GL 4.3 or ARB_compute_shader)
A program object can combine multiple shader stages (built from shader objects) into a single, linked whole. A program pipeline object can combine programs that contain individual shader stages into a whole pipeline.
While shader stages do use the same language, each stage has a separate set of inputs and outputs, as well as built-in variables. As such, shader objects are built for a specific shader stage. So while program objects can contain multiple stages, shader objects only contain code for a single stage.
Shaders have access to a wide variety of resources. They can access Textures, uniforms, uniform blocks, image variables, atomic counters, shader storage buffers, and potentially other information. There are limits however on exactly how much stuff each shader stage can access. Each resource has a query-able maximum count of accessible resources for each stage.
Note that each stage may also have limitations on stage-specific resources; vertex shaders have a hard limit on the number of Vertex Attributes, for example. This section will discuss resources that are general to all shaders.
The query-able limits, and their associated meanings, are as follows. Note that the "*" is the stage name. It can be VERTEX, TESS_CONTROL, TESS_EVALUATION, COMPUTE, GEOMETRY or FRAGMENT. These values also have an OpenGL-required minimum; OpenGL implementations (of a certain version) will support at least this many of that resource.
- This is the number of active components of uniform variables that can be defined outside of a uniform block. The term "component" is meant as the basic component of a vector/matrix. So a vec3 takes up 3 components. The minimum value here is 1024, enough room for 256 vec4s.
- The maximum number of uniform blocks that this shader stage can access. The OpenGL-required minimum is 12 in GL 4.4, 14 in GL 4.3.
- The maximum number of components that this stage can take as input. The required minimum value differs from shader stage to shader stage. Note that Vertex Shaders do not have this value, as they use a different output mechanic based on draw buffers.
- The maximum number of components that this stage can output. The required minimum value differs from shader stage to shader stage. Note that Fragment Shaders do not have this value, as they use a different output mechanic based on draw buffers.
- The maximum number of texture image units that the sampler in this shader can access. The OpenGL-required minimum value is 16 for each stage.
- Note: For legacy reasons, the enumerator for the fragment shader equivalent is called GL_MAX_TEXTURE_IMAGE_UNITS. No FRAGMENT.
- GL_MAX_*_ATOMIC_COUNTERS (requires GL 4.2/ARB_shader_atomic_counters)
- The maximum number of Atomic Counter variables that this stage can define. The OpenGL-required minimum is 8 for fragment and compute shaders, and 0 the rest. This means implementations can disallow you from using atomic counters outside of those specific stages.
- GL_MAX_*_ATOMIC_COUNTER_BUFFERS (requires GL 4.2/ARB_shader_atomic_counters)
- The maximum number of different buffers that the atomic counter variables can come from. The OpenGL-required minimum is 1 for fragment shaders, 8 for compute shaders (note: possible spec typo), and again 0 for the rest.
- GL_MAX_*_SHADER_STORAGE_BLOCKS (requires GL 4.3/ARB_shader_storage_buffer_object)
- The maximum number of different shader storage blocks that a stage can use. For fragment and compute shaders, the OpenGL-required minimum is 8; for the rest, it is 0.