The syntax for declaring variables in HLSL is as follows:
[Storage_Class] [Type_Modifier] Type Name[Index]
[: Semantic]
[: Packoffset]
[: Register];
[Annotations]
[= Initial_Value]
Storage_Class: prompts the compiler about the scope and lifecycle of the variable, which can be arranged in any way.
Value |
Description |
Extern |
Mark a global variable as an external input to the shader; this is the default marking for all global variables. Cannot be combinedStatic. |
Nointerpolation |
Do not interpolate the outputs of a vertex shader before passing them to a pixel shader. |
Precise |
Prevent the compiler from making IEEE unsafe optimizations that affect this variable. For example, the compiler cannot use fusedMul+AddAs an optimization. Instead, you must explicitly useMadIntrinsic function.PreciseModifier also ensures that the compiler preserves the order of operations and that it must account for the possibility of NaN (not a number) and INF (infinite) values from constants and stream inputs. WithoutPreciseModifier, the compiler can perform optimizations and mathematical operations that do not account for NaN and INF values. These optimizations and mathematical operations are not IEEE safe. When you usePreciseKeyword, these optimizations are prohibited for all calculations that affect the variable. The ability to control optimizations in this way is useful when you write shaders for tessellation to maintain water-tight patch seams. |
Shared |
Mark a variable for sharing between effects; this is a hint to the compiler. |
Groupshared |
Mark a variable for thread-group-shared memory for compute shaders. in D3D10 the maximum total size of all variables with the groupshared storage class is 16kb, in D3D11 the maximum size is 32kb. see examples. |
Static |
Mark a local variable so that it is initialized one time and persists between function CILS. If the declaration does not include an initializer, the value is set to zero. A global variable markedStaticIs not visible to an application. |
Uniform |
Mark a variable whose data is constant throughout the execution of a shader (such as a material color in a vertex shader); global variables are consideredUniformBy default. |
Volatile |
Mark a variable that changes frequently; this is a hint to the compiler. This storage class modifier only applies to a local variable. NoteThe HLSL compiler currently ignores this storage class modifier. |
Type_Modifier: variable type modifier.
Value |
Description |
Const |
Mark a variable that cannot be changed by a shader, therefore, it must be initialized in the variable declaration. Global variables are consideredConstBy default (suppress this behavior by supplying the/Gec flag to the compiler ). |
Row_major |
Mark a variable that stores four components in a single row so they can be stored in a single constant register. |
Column_major |
Mark a variable that stores 4 components in a single column to optimize matrix math. |
Note:If no variable type modifier is specified, the compiler usesColumn_majorAs the default value (if the input matrix is row_major, the calculation result will be incorrect .).
Type: Type.
Use This Intrinsic Type |
To Define This Shader Variable |
Buffer |
Buffer, which contains one or more scalars |
Scalar |
One-component scalar |
Vector, Matrix |
Multiple-component vector or matrix |
Sampler, Shader, Texture |
Sampler, shader, or texture object |
Struct, User Defined |
Custom structure or typedef |
Name [Index]: variable Name identified by an ASCII string. It cannot be repeated. Use Index to indicate the size of the array. The Index must be greater than or equal to 1.
Semantic: Optional. Parameter usage information. The compiler is used to set the input and output connections of the shader.
Vertex Shader Semantics
The following Semantic supports both Direct3D 9 and Direct3D 10.
Input |
Description |
Type |
BINORMAL [n] |
Binormal |
Float4 |
BLENDINDICES [n] |
Blend indices |
Uint |
BLENDWEIGHT [n] |
Blend weights |
Float |
COLOR [n] |
Diffuse and specular color |
Float4 |
NORMAL [n] |
Normal vector |
Float4 |
POSITION [n] |
Vertex position in object space. |
Float4 |
POSITIONT |
Transformed vertex position. |
Float4 |
PSIZE [n] |
Point size |
Float |
TANGENT [n] |
Tangent |
Float4 |
TEXCOORD [n] |
Texture coordinates |
Float4 |
Output |
Description |
Type |
COLOR [n] |
Diffuse or specular color |
Float4 |
FOG |
Vertex fog |
Float |
POSITION [n] |
Position of a vertex in homogenous space. Compute position in screen-space by dividing (x, y, z) by w. Every vertex shader must write out a parameter with this semantic. |
Float4 |
PSIZE |
Point size |
Float |
TESSFACTOR [n] |
Tessellation factor |
Float |
TEXCOORD [n] |
Texture coordinates |
Float4 |
Pixel Shader Semantics
The following Semantic supports both Direct3D 9 and Direct3D 10.
Input |
Description |
Type |
COLOR [n] |
Diffuse or specular color. |
Float4 |
TEXCOORD [n] |
Texture coordinates |
Float4 |
VFACE |
Floating-point scalar that indicates a back-facing primitive. A negative value faces backwards, while a positive value faces the camera. |
Float |
VPOS |
The pixel location (x, y) in screen space. To convert a Direct3D 9 shader (that uses this semantic) to a Direct3D 10 shader, see Direct3D 9 VPOS and Direct3D 10 SV_Position) |
Float2 |
Output |
Description |
Type |
COLOR [n] |
Output color |
Float4 |
DEPTH [n] |
Output depth |
Float |
The following Semantic only supports DX10 or later versions.
System-Value Semantic |
Description |
Type |
SV_ClipDistance [n] |
Clip distance data. SV_ClipDistance values are each assumed to be a float32 signed distance to a plane. primitive setup only invokes rasterization on pixels for which the interpolated plane distance (s) are> = 0. multiple clip planes can be implemented simultaneously, by declaring multiple component (s) of one or more vertex elements as the SV_ClipDistance. the combined clip and cull distance values are at most D3D # _ CLIP_OR_CULL_DISTANCE_COUNT components in at most D3D # _ CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT registers. Writable in the Vertex and Geometry shaders. |
Float |
SV_CullDistance [n] |
Cull distance data. when component (s) of vertex Element (s) are given this label, these values are each assumed to be a float32 signed distance to a plane. primitives will be completely discarded if the plane distance (s) for all of the vertices in the primitive are <0. multiple cull planes can be used simultaneously, by declaring multiple component (s) of one or more vertex elements as the SV_CullDistance. the combined clip and cull distance values are at most D3D # _ CLIP_OR_CULL_DISTANCE_COUNT components in at most D3D # _ CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT registers. Writeable in the Vertex and Geometry shaders. |
Float |
SV_Coverage |
Output coverage mask |
Bool |
SV_Depth |
Depth buffer data. Writeable from the pixel shader. |
Float |
SV_DispatchThreadID |
Defines the global thread offset within the Dispatch call, per dimension of the group. Available as input to compute shader. (read only) |
Uint3 |
SV_DomainLocation |
Defines the location on the hull of the current domain point being evaluated. Available as input to the domain shader. (read only) |
Float2 | 3 |
SV_GroupID |
Defines the group offset within a Dispatch call, per dimension of the dispatch call. Available as input to the compute shader. (read only) |
Uint3 |
SV_GroupIndex |
Provides a flattened index for a given thread within a given group. Available as input to the compute shader. (read only) |
Uint |
SV_GroupThreadID |
Defines the thread offset within the group, per dimension of the group. Available as input to the compute shader. (read only) |
Uint3 |
SV_GSInstanceID |
Defines the instance of the geometry shader. Available as input to the geometry shader. (read only) |
Uint |
SV_InsideTessFactor |
Defines the tessellation amount within a patch surface. Available in the Hull shader for writing, and available in the Domain shader for reading. |
Float | float [2] |
SV_IsFrontFace |
For lines and points, IsFrontFace has the value true. the exception is lines drawn out of triangles (wireframe mode), which sets IsFrontFace the same way as rasterizing the triangle in solid mode. available as input to the pixel shader. (read only) |
Bool |
SV_OutputControlPointID |
Defines the index of the control point ID being operated on by an invocation of the main entry point of the hull shader. Available for input to the Domain shader. (read only) |
Uint |
SV_Position |
When SV_Position is declared for input to a Pixel Shader, it can have one of two interpolation modes specified: linearNoPerspective or linearNoPerspectiveCentroid, where the latter causes centroid-snapped xyzw values to be provided when multisample antialiasing. when used in a pixel shader, SV_Position describes the pixel location. available for read/write in the vertex shader and geometry shader. the pixel shader can use it as read only to get the pixel center with an a 0.5 offset. |
Float4 |
SV_RenderTargetArrayIndex |
Render-target array index. applied to geometry shader output and indicates the render target array slice that the primitive will be drawn to by the pixel shader. SV_RenderTargetArrayIndex is only valid if the render target is an array resource. this semantic applies only to primitives, if a primitive has more than one vertex the value from the leading vertex will be used. This value also indicates which array slice of a depthstencilview is used for read/write purposes. Can be written from the geometry shader and read by the pixel shader. |
Uint |
SV_SampleIndex |
Sample frequency index data |
Uint |
SV_Target [n], Where 0 <= n <= 7 |
The output value that will be stored in a render target. the index indicates which of the 8 possibly bound render targets to write. the value is available as output from the pixel shader. (write only) |
Float |
SV_TessFactor |
Defines the tessellation amount on each edge of a patch. Available for writing in the Hull shader and reading in the Domain shader. |
Float [2 | 3 | 4] |
SV_ViewportArrayIndex |
Viewport array index. applied to geometry shader output and indicates which viewport to use for the primitive currently being written out. the primitive will be transformed and clipped against the viewport specified by the index before it is passed to the rasterizer. this semantic applies only to primitives, if a primitive has more than one vertex the value from the leading vertex will be used. |
Uint |
SV_InstanceID |
Per-instance identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10 )) |
Uint |
SV_PrimitiveID |
Per-primitive identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10 )) |
Uint |
SV_VertexID |
Per-vertex identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10 )) |
Uint |
Packoffset: (optional) registers alignment of variables.
Register: (optional) registers occupied by variables.
Annotation: (optional) Metadata. It is a string and HLSL ignores this information.
Value: (optional) Initial Value of the variable.