Transform Feedback: Difference between revisions
→Feedback objects: Clarification on the computation of the number of components. |
|||
Line 142: | Line 142: | ||
Any offsets for global variables or interface blocks that do not specify a buffer explicitly will use the current buffer. The current buffer is set as follows: | Any offsets for global variables or interface blocks that do not specify a buffer explicitly will use the current buffer. The current buffer is set as follows: | ||
<source lang="glsl"> | |||
layout(xfb_buffer = #) out; | layout(xfb_buffer = #) out; | ||
</source> | |||
All following offsets for globals that do not explicitly specify a buffer will use # as their buffer. The initial current buffer for a shader is 0. | All following offsets for globals that do not explicitly specify a buffer will use # as their buffer. The initial current buffer for a shader is 0. | ||
Line 165: | Line 167: | ||
</source> | </source> | ||
Each buffer has the concept of a stride. This represents the byte count from the beginning of one captured vertex to the beginning of the next. It is computed by taking the output with the highest {{code|xfb_offset}} value, adding its size to that offset, and then aligning the computed value to the base alignment of the buffer. The buffer's alignment is 4, unless it captures any double-precision values in which case it is 8. This means you do not need to manually pad structures for alignment, as you did with [[#Doubles and alignment|outside shader setting]]. | |||
The stride for a buffer can also be explicitly set using the '''{{code|xfb_stride}}''' layout qualifier. This allows you to add extra space at the end, perhaps to skip data that will not change. A compilation error will result if the stride you specify is: | |||
* Too small, given the offsets and computed sizes of the captured data for that buffer. | |||
* Not properly aligned. It must be at least 4 byte aligned, and it must be 8 byte aligned if the buffer captures any double-precision values. | |||
The stride for a buffer is set as follows: | |||
<source lang="glsl"> | |||
layout(xfb_buffer = 1, xfb_stride = 32) out; //Sets stride of buffer 1 to 32. Also, sets buffer 1 to be current. | |||
</source> | |||
Linking errors will result if any captured outputs within a buffer overlap in space or violate padding. For example: | |||
<source lang="glsl"> | |||
layout(xfb_buffer = 0) out Data | |||
{ | |||
layout(xfb_offset = 0) float val1; | |||
layout(xfb_offset = 4) vec4 val2; | |||
layout(xfb_offset = 16) float val3; //Compiler error. val2 covers bytes on the range [4, 20). | |||
}; | |||
</source> | |||
Compiler/linker errors will result if you are using [[Geometry_Shader#Output_streams|Geometry Shader output streams]] and two outputs from different streams are routed to the same buffer. | |||
{{note|When using | {{note|When using {{extref|enhanced_layout}} as an extension (on older hardware), if {{extref|transform_feedback3}} is not also available, you may only output to a single buffer. You can still use offsets to put space between vertex attribute data, but you cannot set {{code|xbf_buffer}} to any value other than 0.}} | ||
== Buffer binding == | == Buffer binding == |
Revision as of 14:03, 20 August 2013
|
||||||||||||||||||
OpenGL Rendering Pipeline
|
Transform Feedback is the process of altering the rendering pipeline so that primitives generated by Vertex Processing will be written to buffer objects. This allows one to preserve the post-transform rendering state of an object and resubmit this data multiple times.
This article is a stub. You can help the OpenGL Wiki by expanding it. |
Shader setup
In order to capture primitives, the program containing the final Vertex Processing shader stage must be linked with a number of parameters. These parameters must be set before linking the program, not after. So if you want to use glCreateShaderProgram, you will have to use the in-shader specification API, which is only available with OpenGL 4.4 or ARB_enhanced_layouts.
The only program object that matters for transform feedback is the one that provides the last Vertex Processing shader stage. These settings can be on any other program in a separate program, but only the settings on the last active Vertex Processing shader stage will be used.
Transform feedback can operate in one of two capturing modes. In interleaved mode, all captured outputs go into the same buffer, interleaved with one another. In separate mode, each captured output goes into a separate buffer. This must be selected on a program object at link time.
To define the capture settings for a program, as well as which output variables are captured, use the following function:
The bufferMode is the capturing mode. It must be GL_INTERLEAVED_ATTRIBS or GL_SEPARATE_ATTRIBS
The count is the number of strings in the varyings array. This array specifies the name of the output variables of the appropriate shader stage to capture. The order that the variables are provided in defines the order in which they are captured. The names of these variables conform to the standard rules for naming GLSL variables.
There are limitations on the number of outputs that can be captured, as well as how many total components can be captured. Within these limits, any output variables can be captured, including outputs of struct types, arrays, members of interface blocks.
Captured data format
Transform feedback explicitly captures Primitives. While it does capture data for each vertex, it does so after spliting each primitive up into separate primitives. So if you're rendering using GL_TRIANGLE_STRIP, and you render with 6 vertices, that yields 4 triangles. Transform feedback will capture 4 triangles worth of data. Since each triangle has 3 vertices, TF will capture 12 vertices, not the 6 you might expect from the rendering command.
Each primitive is written in the order it is given. Within a primitive, the order of the vertex data written is the the vertex order after primitive assembly. This means that, when drawing with triangle strips for example, the captured primitive will switch the order of two vertices every other triangle. This ensures that Face Culling will still respect the ordering provided in the initial array of vertex data.
Within each vertex, the data is written in the order specified by the varyings array (when doing interleaved rendering). If an output variable is an aggregate (struct/array), then each member of the aggregate is written in order. Each component of the basic type of the output is written in order. Ultimately, all of the data is written tightly packed together (though capturing double-precision floats may cause padding).
A component will always be a float/double, signed integer, or unsigned integer, using the sizes for GLfloat/GLdouble, GLint, and GLuint. No packing or normalization is performed. The transform feedback system does not have an automated analog to the much more flexible vertex format specification system.
This of course does not prevent you from manually packing bits into unsigned integers in your shader.
Advanced interleaving
Core in version | 4.6 | |
---|---|---|
Core since version | 4.0 | |
Core ARB extension | ARB_transform_feedback3 |
The above settings only provide two modes: either all captured outputs go into separate buffers or they all go into the same buffer. In many cases, it is useful to be able to be able to write several components to one buffer, while writing other components to others.
Also, captured interleaved data is tightly packed, with each variable's components coming immediately after the previous components. It is often useful to be able to skip writing over certain data, if some data changes and other data does not.
These can be achieved by the use of special "varying" names in the varyings array. These special names do not name actual output variables; they only cause some particular effect on subsequent writes.
These names and their effects are:
- gl_NextBuffer
- This causes all subsequent outputs to be routed to the next buffer index. The buffers start at 0 and increment by one each time this is encountered in the varyings list.
- There must not be more of these than the number of buffers that can be bound for use in transform feedback. Therefore, the number of these must be strictly less than GL_MAX_TRANSFORM_FEEDBACK_BUFFERS.
- gl_SkipComponents#
- This causes the system to skip writing # number of components, where # may be from 1 to 4. The memory covered by the skipped components will not be modified. Each component in this case is the size of a float.
- Note that components skipped in this way still count against the limitation on the number of components being output.
Output variables in the Geometry Shader can be declared to go to a particular stream. This is controlled via an in-shader specification, but there are certain limitations that affect advanced component interleaving.
No two outputs that go to different streams can be captured by the same buffer. Attempting to do so will result in a linker error. So using multiple streams with interleaved writing requires using advanced interleaving to route attributes to different buffers.
Note that this ability effectively makes separate capture mode superfluous. Interleaving with these facilities is a functional superset of what separate mode can do, since it can capture one output to each buffer individually.
Doubles and alignment
Core in version | 4.6 | |
---|---|---|
Core since version | 4.0 | |
Core ARB extension | ARB_gpu_shader_fp64 |
The alignment of single-precision floats and integers is 4 bytes. However, the alignment of double-precision values is 8 bytes. This causes a problem when it comes to capturing transform feedback data.
The alignment of components must be ensured. This is trivially ensured with floats and integers, but doubles require special care. It is up to the user to ensure 8-byte alignment of all double precision data. Specifically, you must ensure two things:
- Every double-precision component begins on an 8-byte boundary. You may need to insert padding where needed, using the skipping functionality above.
- All of the vertex data going to a particular buffer that includes a double-precision component must have a total vertex data size aligned to 8 bytes. This ensures that the second vertex will start on an 8 byte boundary. You may therefore need to add padding to the end of vertex data.
For example, if you want to capture the following, in the order defined here:
out DataBlock
{
float var1;
dvec2 someDoubles;
float var3;
};
This is the sequence of strings that you will need in your varyings data if you want to capture it in the order of definition:
const char *varyings[] =
{
"DataBlock.var1",
"gl_SkipComponents1", //Padding the next component to 8-byte alignment.
"DataBlock.someDoubles",
"DataBlock.var3",
"gl_SkipComponents1", //Padding out the entire vertex structure to 8-byte alignment.
};
If you do not do this, you get undefined behavior. You could avoid the padding just by changing the order you capture them. You don't have to change the order you define them in the shader.
In-shader specification
Core in version | 4.6 | |
---|---|---|
Core since version | 4.4 | |
Core ARB extension | ARB_enhanced_layouts |
Shaders can define which outputs are captured by transform feedback and exactly how they are captured. When a shader defines them, querying the program for the mode of transform feedback will return interleaved mode (since the advanced interleaving makes separate mode a complete subset of interleaved mode).
Layout qualifiers can be used to define which output variables are captured in Transform Feedback operations. When these qualifiers are set in a shader, they completely override any attempt to set the transform feedback outputs from OpenGL via glTransformFeedbackVaryings.
Any output variable or output interface block declared with the xfb_offset layout qualifier will be part of the transform feedback output. This qualifier must be specified with an integer byte offset. The offset is the number of bytes from the beginning of a vertex to be written to the current buffer to this particular output variable.
The offsets of contained values (whether in arrays, structs, or members of an interface block if the whole block has an offset) are computed, based on the sizes of prior components to pack them in the order specified. Any explicitly provided offsets are not allowed to violate alignment restrictions. So if a definition contains a double (either directly or indirectly), the offset must be 8-byte aligned.
Members of interface blocks can have their offsets specified directly on them, which overrides any computed offsets. Also, all members of an interface block are not required to be written to outputs (though that will happens if you set the xfb_offset on the block itself). Stream assignments for a geometry shader are required to be the same for all members of a block, but offsets are not.
Different variables being captured are assigned to buffer binding indices. Offset assignments are separate for the separate buffers. It is a linker error for two variables captured by the same buffer to have overlapping byte offsets, whether automatically computed or explicitly assigned.
An explicit buffer assignment is made by using the xfb_buffer qualifier on the same declaration as the offset qualifier. This takes an integer which defines the buffer binding index that the captured output(s) is/are associated with. The integer must be less than GL_MAX_TRANSFORM_FEEDBACK_BUFFERS.
Any offsets for global variables or interface blocks that do not specify a buffer explicitly will use the current buffer. The current buffer is set as follows:
layout(xfb_buffer = #) out;
All following offsets for globals that do not explicitly specify a buffer will use # as their buffer. The initial current buffer for a shader is 0.
Variables can have xfb_buffer assigned to them without xfb_offset. This does nothing and will be ignored.
Interface blocks have a special association with buffers. Each interface block is associated with a buffer, regardless of whether any of its members are captured. The buffer is either the current buffer as defined above or a buffer explicitly specified by xfb_buffer.
As previously stated, all members of a block do not have to be captured. However, if any members of a block are captured, they must all be captured to the same buffer. Specifically, the buffer associated with that block. It is an error to use xfb_buffer on a member if the buffer index you provide is different from the index used by the block.
As an example:
layout(xfb_buffer = 2) out; //Default buffer of 2.
out OutputBlock1 //Block buffer index is implicitly 2.
{
float val1;
layout(xfb_buffer = 2, xfb_offset = 0) first; //The provided index is the same as the block's index.
layout(xfb_buffer = 1, xfb_offset = 0) other; //Compile error, due to changing the buffer index for a block member.
};
Each buffer has the concept of a stride. This represents the byte count from the beginning of one captured vertex to the beginning of the next. It is computed by taking the output with the highest xfb_offset value, adding its size to that offset, and then aligning the computed value to the base alignment of the buffer. The buffer's alignment is 4, unless it captures any double-precision values in which case it is 8. This means you do not need to manually pad structures for alignment, as you did with outside shader setting.
The stride for a buffer can also be explicitly set using the xfb_stride layout qualifier. This allows you to add extra space at the end, perhaps to skip data that will not change. A compilation error will result if the stride you specify is:
- Too small, given the offsets and computed sizes of the captured data for that buffer.
- Not properly aligned. It must be at least 4 byte aligned, and it must be 8 byte aligned if the buffer captures any double-precision values.
The stride for a buffer is set as follows:
layout(xfb_buffer = 1, xfb_stride = 32) out; //Sets stride of buffer 1 to 32. Also, sets buffer 1 to be current.
Linking errors will result if any captured outputs within a buffer overlap in space or violate padding. For example:
layout(xfb_buffer = 0) out Data
{
layout(xfb_offset = 0) float val1;
layout(xfb_offset = 4) vec4 val2;
layout(xfb_offset = 16) float val3; //Compiler error. val2 covers bytes on the range [4, 20).
};
Compiler/linker errors will result if you are using Geometry Shader output streams and two outputs from different streams are routed to the same buffer.
Buffer binding
Feedback process
Feedback objects
Core in version | 4.6 | |
---|---|---|
Core since version | 4.0 | |
Core ARB extension | ARB_transform_feedback2 |
Feedback rendering
Feedback pausing and resuming
Limitations
When using separate capture, there is a limitation on the total number of variables that can be captured. This is GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, which will be at least 4. Also, there is a limit to the number of components that any particular variable can contain. This is GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS, which will be at least 4. If these limits are exceeded, a program linking error will result.
When using interleaved capture, the limit is the total number of components that can be captured. This is GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS, which must be at least 64. Double-precision components count as 2. The number of "components" can be computed for in-shader specification by adding all of the buffer's strides and dividing by 4. That must be less than this queried value.
When using advanced interleaving to route different variables to different buffers, the limit on the number of available buffers is GL_MAX_TRANSFORM_FEEDBACK_BUFFERS.
Before OpenGL 4.0 or ARB_transform_feedback3, the limit on the binding index to glBindBufferRange for GL_TRANSFORM_FEEDBACK was GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, since more than one buffer can only be used with separate outputs. With OpenGL 4.0 or ARB_transform_feedback3, it is GL_MAX_TRANSFORM_FEEDBACK_BUFFERS.
Reference
- Category:Core API Ref Transform Feedback: Function documentation for all transform feedback commands, except for those that actually render with the results of a feedback operation.