HLSL 变量语法定义

在HLSL中声明变量的语法如下:

[Storage_Class] [Type_Modifier] Type Name[Index]
[: Semantic]
[: Packoffset]
[: Register];
[Annotations]
[= Initial_Value]

Storage_Class:提示编译器该变量的作用域与生命周期,可按任意方式排列。

ValueDescription
extern Mark a global variable as an external input to the shader; this is the default marking for all global variables. Cannot be combined with static.
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 fused mul + add as an optimization. Instead, you must explicitly use the mad intrinsic function. The 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. Without the precise modifier, 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 use the precise keyword, 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 calls. If the declaration does not include an initializer, the value is set to zero. A global variable marked static is 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 considered uniform by 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.

Note  The HLSL compiler currently ignores this storage class modifier.

Type_Modifier:变量类型修饰符。

ValueDescription
const Mark a variable that cannot be changed by a shader, therefore, it must be initialized in the variable declaration. Global variables are considered const by 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.

注意  如果没有指定变量类型修饰符, 编译器使用 column_major 作为默认值(如果你传进来的矩阵是row_major的话,计算的结果就会是错误的。).

Type:类型。

Use This Intrinsic Type To Define This Shader Variable
Buffer Buffer, which contains one or more scalars
Scalar One-component scalar
VectorMatrix Multiple-component vector or matrix
SamplerShaderTexture Sampler, shader, or texture object
StructUser Defined Custom structure or typedef

Name[Index]:ASCII字符串标识的变量名,不可重复。使用Index指出数组的容量,Index需要大于等于1.

Semantic:可选,参数用途信息。编译器用来设置shader的输入输出连接。

Vertex Shader Semantics

 以下Semantic同时支持 Direct3D 9 和 Direct3D 10.

InputDescriptionType
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
OutputDescriptionType
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

以下Semantic同时支持 Direct3D 9 和 Direct3D 10.

InputDescriptionType
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
OutputDescriptionType
COLOR[n] Output color float4
DEPTH[n] Output depth float

 以下Semantic仅支持DX10或更高版本。

System-Value SemanticDescriptionType
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 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 to. 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:可选,设置变量的寄存器对齐。

Register:可选,指定变量占用的寄存器。

Annotation:可选,元数据表示,它是一个字符串,HLSL会忽略掉该信息。
Value:可选,变量的初始值。

原文地址:https://www.cnblogs.com/Baesky/p/HLSL_variableSyntax.html