Shader

From OpenGL.org
(Redirected from Shader Stage)
Jump to: navigation, search

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.

Stages

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, with their enumerator name:

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.

Resource limitations

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.

MAX_*_UNIFORM_COMPONENTS​
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 vec4​s.
GL_MAX_*_UNIFORM_BLOCKS​
The maximum number of uniform blocks that this shader stage can access. The OpenGL-required minimum is 12 in GL 3.3, and 14 in GL 4.3.
GL_MAX_*_INPUT_COMPONENTS​
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 input mechanic based on Vertex Attributes. Their limit is GL_MAX_VERTEX_ATTRIBUTES​, where each attribute can be at most 4 components.
GL_MAX_*_OUTPUT_COMPONENTS​
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. Their limit is GL_MAX_DRAW_BUFFERS​, where each draw buffer output can be at most 4 components.
GL_MAX_*_TEXTURE_IMAGE_UNITS​
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_*_IMAGE_UNIFORMS​ (requires GL 4.2/ARB_shader_image_load_store)
The maximum number of image variables for this shader stage. The OpenGL-required minimum is 8 for fragment and compute shaders, and 0 for the rest. This means implementations may not allow you to use image variables in non-fragment or compute stages.
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 for the rest.
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.

Aggregate limits

While these define the resources usable to a shader stage, there are some limits beyond this, which cover all shader stages in total.

GL_MAX_UNIFORM_BUFFER_BINDINGS​
The limit on the number of uniform buffer binding points. This is the limit for glBindBufferRange when using GL_UNIFORM_BUFFER​. In GL 3.3, this value is a minimum of 36 (3 shader stages, with a minimum of 12 blocks per stage). In 4.3, this value is a minimum of 72.
GL_MAX_COMBINED_UNIFORM_BLOCKS​
The maximum number of uniform blocks that all of the active programs can use. If two (or more) shader stages use the same block, they count separately towards this limit. In GL 3.3, this was 36; in 4.3, it is 70.
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS​
The total number of texture units that can be used from all active programs. This is the limit on glActiveTexture(GL_TEXTURE0 + i) and glBindSampler. In GL 3.3, this was 48; in 4.3, it is 96.
GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS​
When doing separate mode Transform Feedback, this is the maximum number of varying variables that can be captured. This has a minimum of 4.
GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS​
When doing separate mode Transform Feedback, this is the maximum number of components for a single varying variable (note that varyings can be arrays or structs) that can be captured. This has a minimum of 4.
GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS​
When doing interleaved Transform Feedback, this is the total number of components that can be captured within a single buffer. This has a minimum of 64.
GL_MAX_TRANSFORM_FEEDBACK_BUFFERS​ (requires GL 4.0/ARB_transform_feedback3)
The maximum number of buffers that can be written to in transform feedback operations. This has a minimum of 4.
GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS​ (requires GL 4.2/ARB_shader_atomic_counters)
The total number of atomic counter buffer binding points. This is the limit for glBindBufferRange when using GL_ATOMIC_COUNTER_BUFFER​. This value has a minimum of 1.
GL_MAX_COMBINED_ATOMIC_COUNTER_BUFFERS​ (requires GL 4.2/ARB_shader_atomic_counters)
The maximum number of atomic counter buffers variables across all active programs. This value has a minimum of 1.
GL_MAX_COMBINED_ATOMIC_COUNTERS​ (requires GL 4.2/ARB_shader_atomic_counters)
The maximum number of atomic counter variables across all active programs. This value has a minimum of 8.
GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS​ (requires GL 4.2/ARB_shader_atomic_counters)
The total number of shader storage buffer binding points. This is the limit for glBindBufferRange when using GL_SHADER_STORAGE_BUFFER​. This value has a minimum of 8.
GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS​ (requires GL 4.3/ARB_shader_storage_buffer_object)
The maximum number of shader storage blocks across all active programs. As with UBOs, blocks that are the same between stages are counted for each stage. This value has a minimum of 8.
GL_MAX_IMAGE_UNITS​ (requires GL 4.2/ARB_image_load_store)
The total number of image units that can be used for image variables from all active programs. This is the limit on glBindImageTexture. This value has a minimum of 8.
GL_MAX_COMBINED_SHADER_OUTPUT_RESOURCES​ (requires GL 4.3/ARB_shader_storage_buffer_object)
The total number of shader storage blocks, image variables, and fragment shader outputs across all active programs cannot exceed this number. This is the "amount of stuff" that a sequence of shaders can write to (barring Transform Feedback). This value has a minimum of 8.