Using system-generated values
System-generated values are generated by the Input Assembler (IA) stage (based on user-supplied input semantics) to allow certain efficiencies in shader operations. By attaching data, such as an instance ID (visible to the Vertex Shader (VS) stage), a vertex ID (visible to VS), or a primitive ID (visible to Geometry Shader (GS) stage/Pixel Shader (PS) stage), a subsequent shader stage may look for these system values to optimize processing in that stage.
For instance, the VS stage may look for the instance ID to grab additional per-vertex data for the shader or to perform other operations; the GS and PS stages may use the primitive ID to grab per-primitive data in the same way.
VertexID
A vertex ID is used by each shader stage to identify each vertex. It is a 32-bit unsigned integer whose default value is 0. It is assigned to a vertex when the primitive is processed by the Input Assembler (IA) stage. Attach the vertex-ID semantic to the shader input declaration to inform the IA stage to generate a per-vertex ID.
The IA will add a vertex ID to each vertex for use by shader stages. For each draw call, the vertex ID is incremented by 1. Across indexed draw calls, the count resets back to the start value. If the vertex ID overflows (exceeds 2³²– 1), it wraps to 0.
For all primitive types, vertices have a vertex ID associated with them (regardless of adjacency).
PrimitiveID
A primitive ID is used by each shader stage to identify each primitive. It is a 32-bit unsigned integer whose default value is 0. It is assigned to a primitive when the primitive is processed by the Input Assembler (IA) stage. To inform the IA stage to generate a primitive ID, attach the primitive-ID semantic to the shader input declaration.
The IA stage will add a primitive ID to each primitive for use by the Geometry Shader (GS) stage or the Vertex Shader (VS) stage (whichever is the first stage active after the IA stage). For each indexed draw call, the primitive ID is incremented by 1, however, the primitive ID resets to 0 whenever a new instance begins. All other draw calls do not change the value of the instance ID. If the instance ID overflows (exceeds 2³²– 1), it wraps to 0.
The Pixel Shader (PS) stage does not have a separate input for a primitive ID; however, any pixel shader input that specifies a primitive ID uses a constant interpolation mode.
There is no support for automatically generating a primitive ID for adjacent primitives. For primitive types with adjacency, such as a triangle strip with adjacency, a primitive ID is only maintained for the interior primitives (the non-adjacent primitives), just like the set of primitives in a triangle strip without adjacency.
InstanceID
An instance ID is used by each shader stage to identify the instance of the geometry that is currently being processed. It is a 32-bit unsigned integer whose default value is 0.
The Input Assembler (IA) stage will add an instance ID to each vertex if the vertex shader input declaration includes the instance ID semantic. For each indexed draw call, instance ID is incremented by 1. All other draw calls do not change the value of instance ID. If instance ID overflows (exceeds 2³²– 1), it wraps to 0.
Example
The following illustration shows how system values are attached to an instanced triangle strip in the Input Assembler (IA) stage.
These tables show the system values generated for both instances of the same triangle strip. The first instance (instance U) is shown in blue, the second instance (instance V) is shown in green. The solid lines connect the vertices in the primitives, the dashed lines connect the adjacent vertices.
The following tables show the system-generated values for the instance U.
Vertex Data | C,U | D,U | E,U | F,U | G,U | H,U | I,U | J,U | K,U | L,U |
---|---|---|---|---|---|---|---|---|---|---|
VertexID | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
InstanceID | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Triangle strip instance U has 3 triangle primitives, with the following system-generated values:
Value 1 | Value 2 | Value 3 | |
---|---|---|---|
PrimitiveID | 0 | 1 | 2 |
InstanceID | 0 | 0 | 0 |
The following tables show the system-generated values for the instance V.
Vertex Data | C,V | D,V | E,V | F,V | G,V | H,V | I,V | J,V | K,V | L,V |
---|---|---|---|---|---|---|---|---|---|---|
VertexID | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
InstanceID | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Triangle strip instance V has 3 triangle primitives, with the following system-generated values:
Value 1 | Value 2 | Value 3 | |
---|---|---|---|
PrimitiveID | 0 | 1 | 2 |
InstanceID | 1 | 1 | 1 |
The Input Assembler (IA) stage generates the IDs (vertex, primitive, and instance); notice also that each instance is given a unique instance ID. The data ends with the strip cut, which separates each instance of the triangle strip.
Related topics