您的位置:首页 > 其它

HLSL 变量语法定义

2015-06-30 14:51 471 查看
在HLSL中声明变量的语法如下:



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




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

ValueDescription
externMark a global variable as an external input to the shader; this is the default marking for all global variables. Cannot be combined with static.
nointerpolationDo not interpolate the outputs of a vertex shader before passing them to a pixel shader.
precisePrevent 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.

sharedMark a variable for sharing between effects; this is a hint to the compiler.
groupsharedMark 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.
staticMark 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.
uniformMark 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.
volatileMark 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
constMark 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_majorMark a variable that stores four components in a single row so they can be stored in a single constant register.
column_majorMark a variable that stores 4 components in a single column to optimize matrix math.
注意 如果没有指定变量类型修饰符, 编译器使用 column_major 作为默认值(如果你传进来的矩阵是row_major的话,计算的结果就会是错误的。).


Type:类型。

Use This Intrinsic TypeTo Define This Shader Variable
BufferBuffer, which contains one or more scalars
ScalarOne-component scalar
Vector, MatrixMultiple-component vector or matrix
Sampler, Shader, TextureSampler, shader, or texture object
Struct, User DefinedCustom structure or typedef
Name[Index]:ASCII字符串标识的变量名,不可重复。使用Index指出数组的容量,Index需要大于等于1.

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

Vertex Shader Semantics

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

InputDescriptionType
BINORMAL
Binormalfloat4
BLENDINDICES
Blend indicesuint
BLENDWEIGHT
Blend weightsfloat
COLOR
Diffuse and specular colorfloat4
NORMAL
Normal vectorfloat4
POSITION
Vertex position in object space.float4
POSITIONTTransformed vertex position.float4
PSIZE
Point sizefloat
TANGENT
Tangentfloat4
TEXCOORD
Texture coordinatesfloat4
OutputDescriptionType
COLOR
Diffuse or specular colorfloat4
FOGVertex fogfloat
POSITION
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
PSIZEPoint sizefloat
TESSFACTOR
Tessellation factorfloat
TEXCOORD
Texture coordinatesfloat4

Pixel Shader Semantics

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

InputDescriptionType
COLOR
Diffuse or specular color.float4
TEXCOORD
Texture coordinatesfloat4
VFACEFloating-point scalar that indicates a back-facing primitive. A negative value faces backwards, while a positive value faces the camera.float
VPOSThe 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
Output colorfloat4
DEPTH
Output depthfloat
以下Semantic仅支持DX10或更高版本。

System-Value SemanticDescriptionType
SV_ClipDistance
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
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_CoverageOutput coverage maskbool
SV_DepthDepth buffer data. Writeable from the pixel shader.float
SV_DispatchThreadIDDefines the global thread offset within the Dispatch call, per dimension of the group. Available as input to compute shader. (read only)uint3
SV_DomainLocationDefines the location on the hull of the current domain point being evaluated. Available as input to the domain shader. (read only)float2|3
SV_GroupIDDefines the group offset within a Dispatch call, per dimension of the dispatch call. Available as input to the compute shader. (read only)uint3
SV_GroupIndexProvides a flattened index for a given thread within a given group. Available as input to the compute shader. (read only)uint
SV_GroupThreadIDDefines the thread offset within the group, per dimension of the group. Available as input to the compute shader. (read only)uint3
SV_GSInstanceIDDefines the instance of the geometry shader. Available as input to the geometry shader. (read only)uint
SV_InsideTessFactorDefines 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_IsFrontFaceFor 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_OutputControlPointIDDefines 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_PositionWhen 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_RenderTargetArrayIndexRender-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_SampleIndexSample frequency index datauint
SV_Target
,


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_TessFactorDefines 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_ViewportArrayIndexViewport 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_InstanceIDPer-instance identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10))uint
SV_PrimitiveIDPer-primitive identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10))uint
SV_VertexIDPer-vertex identifier automatically generated by the runtime (see Using System-Generated Values (Direct3D 10))uint
Packoffset:可选,设置变量的寄存器对齐。

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

Annotation:可选,元数据表示,它是一个字符串,HLSL会忽略掉该信息。

Value:可选,变量的初始值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: