HLSL中的 Semantics(语义)
在定义HLSL不管是函数中输入的参数变量或者返回的变量的时候经常要用到诸如xxx:POSITION的形式,这里是HLSL中变量与众不同的一个地方。:后面的叫做叫做语义,它用来描述变量的一些信息,说到本质上语义是规定着色器变量与硬件之间的关系,例如POSITION指定这个变量是被用在位置相关的寄存器。
顶点着色器的语义
语义都有输入跟输出的部分,关于顶点的输入和输出的见下:
| 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 |
从上可以看出变量后面的语义不是随便乱加的,首先它指明了变量的用处,其次针对输入和输出都有专门限制的语义,我们不能把一个用作输入的语义来用作输出。
n是寄存器的索引值,最大值依赖于硬件的支持。如POSITION1.
像素着色器的语义
| 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. 在屏幕中的像素位置 |
float2 |
| Output |
Description |
Type |
| COLOR[n] |
Output color |
float4 |
| DEPTH[n] |
Output depth |
float |