DX9 HLSL Semantics (HLSL的'语义'相关说明)

来源:互联网 发布:淘宝网图片显示不正常 编辑:程序博客网 时间:2024/04/29 04:16

本文版权归我所有,仅供个人学习使用,请勿转载,勿用于任何商业用途。
由于本人水平有限,难免出错,欢迎大家和我交流。
作者:clayman
Blog:http://blog.csdn.net/soilwork
clayman_joe@yahoo.com.cn

 

         介绍HLSL或者DirectX / XNA的书中都很少会详细讲解Semantics,DirectX SDK对此也是草草带过,加上这个单词本身对汉语来说含义比较难懂,因此Semantics常常成了HLSL中鲜为人知的一部分。然而,对于构建复杂Effect系统来说,Semantics作为DirectX / XNA和shader之间传输数据的纽带,有相当重要的作用。

 


       Semantics究竟是什么呢?简单来说,它是描述shader中变量的原数据,用来表示某个变量的语义。它与DirectX中D3DDECLUSAGE或者XNA中VertexElementUsage枚举的作用相当类似。

 


       Semantics有什么用呢?Shader程序使用Semantics作为标记,实现数据从CPU到GPU的绑定。

 


       为什么使用Semantics呢? 在Semantics出现之前,shader程序员常常直接使用寄存器名来绑定数据。这样做有两个缺点,首先,编译器无法对代码进行优化,其次,维护移植起来也相当不方便。Semantics隐藏了寄存器的复杂性,所有积存器的分配都由编译器来为你处理,同时,对外提供了一个统一的接口。

 


       如何使用Semantics呢?先来看Semantics的语法:

 


       [ Modifiers] ParameterType ParameterName [ : Semantic] = [ Initializers]

 


       比如:

 


       float4x4 WorldMatrix : WORLD;

 


       冒号前面的部分,声明了一个变量myWorldMatrix,你也许希望用它来表示世界坐标变换。但是,编译器对此一无所知,只会把它当作一个普通的float4x4变量来对待。然而,添加了冒号以及标准Semantics WORLD之后,无论变量叫什么名字,编译器都会知道这个变量将被当作坐标变换矩阵来使用。注意,Semantics本身并不区分大小写,只是习惯上用大写表示。

 


       从类型上来看,Semantics可以分为vertex/pixel semantics和普通变量semantics两种。

 


       对于标记了vertex/pixel semantics值的变量来说,顶点会自动把vertex shader输入顶点或输出数据中相应的值绑定到变量上。

 


       比如:

 

       vs_main( float4 pos : Position, our float3 oColor : COLOR0){…}

 

       Shader会自动把输入顶点中的位置信息绑定到pos变量上,此外

 

       ps_main( float3 diffColor : COLOR0){…}

 

       则会把vertex shader的输出颜色COLOR0绑定到diffColor变量。

      

       对于普通变量semantics来说,则主要用来与外部数据进行绑定。在开发复杂Effect系统时,除非在开发先就有非常详细的变量命名规则,否则,程序将淹没在各种不同的Effect变量名间。每次调用Effect.SetValue()方法时,你都不得不查看HLSL代码中所用的具体变量名,这无疑是开发者的噩梦。然而,只要使用了semantics,这个问题就变的简单了。通过DirectX中的ID3DXBaseEffect::GetParameterBySemantic()或者XNA中的EffectParameterCollection.GetParameterBySemantic()方法,能轻易得到对应某个semantics的变量名,从而为它赋值,大大节约了开发时间。这也是ms推荐使用标准semantics的原因。

 


       需要说明的是在DirectX中,似乎只列出了标准的vertex/pixel semantics,此外,还有相当数量的标准普通变量semantics,他们几乎涵盖了shader中所有可能用到的变量用法。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/soilwork/archive/2007/04/18/1568463.aspx

 

DX SDK的“Semantics (DirectX HLSL)”章节里面有说明的

The input structure identifies the data from the vertex buffer that will provide the shader inputs. This shader maps the data from the "POSITION", "NORMAL", and "BLENDWEIGHT" elements of the vertex buffer into vertex shader registers. The input data type for HLSL does not have to exactly match the vertex declaration data type. If it doesn't exactly match, the vertex data will automatically be converted into the HLSL's data type when it is written into the shader registers. For instance, if the "NORMAL" data were defined to be a UINT by the application, it would be converted into a float3 when read by the shader.

If the data in the vertex stream contains fewer components than the corresponding shader data type, the missing components will be initialized to 0 (except for w, which is initialized to 1).

Input semantics are similar to the values in the "D3DDECLUSAGE" enumeration in the fixed function pipeline.

The output structure identifies the vertex shader output parameters: position and color. These outputs will be used by the pipeline for triangle rasterization (in primitive processing). The output marked "POSITION" denotes the position of a vertex in screen space. In screen space, -1 and 1 are the minimum and maximum x and y values of the boundaries of the viewport, while z is used for z-buffer testing. As a minimum, a vertex shader must output "POSITION" data.

Output semantics are also similar to the values in the "D3DDECLUSAGE" enumeration in the fixed function pipeline. In general, an output structure for a vertex shader can also be used as the input structure for a pixel shader, provided the pixel shader does not read from any variable marked with the semantics "POSITION", "PSIZE", or "FOG". These semantics are associated with per-vertex scalar values that are not used by a pixel shader. If these values are needed for the pixel shader, they can be copied into another output variable that uses a pixel shader semantic.

 

点着色器可以接受的输入数据:

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
 
顶点着色器可以接受的输出数据:
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  

像素着色器可以接受的输入数据:
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  

像素着色器可以接受的输出数据:
COLOR[n] Output color float4  
DEPTH[n] Output depth float  

原创粉丝点击