[每日Shader]ShaderX.2.Introductions and Tutorials with DirectX 9 (2)A simple sample-Vertex Shader
Posted on 2008-11-01 18:16 活着就是幸福 阅读(322) 评论(0) 收藏 举报A Simple Example
一个简单的例程
Before presenting an exhaustive description of the HLSL, let's first have a look at one HLSL vertex shader and one HLSL pixel shader taken from an application which renders simple procedural wood. The first HLSL shader shown below is a simple vertex shader:
在展示一个对HLSL全面的描述之前,让我们先从一个渲染简单的代码生成的木头的应用程序里取得的顶点着色器代码(vertex shader)和像素着色器代码(pixel shader)。首先展示的是一个简单的顶点着色器:
float4x4 view_proj_matrix;
float4x4 texture_matrix0;
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Pshade : TEXCOORD0;
};
VS_OUTPUT main ( float4 vPosition : POSITION)
{
VS_OUTPUT Out = (VS_OUTPUT) 0;
Out.Pos = mul (view_proj_matrix, vPosition);
// Transform Pshade
Out.Pshade = mul (texture_matrix0, vPosition);
return Out;
}
The first two lines of this shader declare a pair of 4×4 matrices called view_proj_matrix and texture_matrix0. Following these global-scope matrices, a structure is declared. This VS_OUTPUT structure has two members: a float4 called Pos and a float3 called Pshade.
这段着色器代码的开始两行声明(declare)了两个4×4的矩阵,分别是view_proj_matrix 和 texture_matrix0。在这些全局域(global-scope)矩阵之后,声明了一个结构体。这个VS_OUTPUT结构体有两个成员,分别是float4类型的Pos和float3类型的Pshade。
The main function for this shader takes a single float4 input parameter and returns a VS_OUTPUT structure. The float4 input vPosition is the sole input to the shader while the returned VS_OUTPUT struct defines this vertex shader's output. For now, don't worry about the POSITION and TEXCOORD0 keywords following these parameters and structure members. These are called semantics and their meaning will be discussed later in this chapter.
这段着色器代码的主函数使用了一个float4输入参数,返回了一个VS_OUTPUT结构体。这个float4类型的输入参数vPosition是着色器的唯一输入,而返回的VS_OUTPUT结构体定义了着色器的输出。现在,不要担心这些参数和结构体成员后面的关键字POSITION和TEXCOORD0。它们被叫做语义(semantics),其代表的意义会在本章节后面进行讨论。
Looking at the actual code body of the main function, you'll see that an intrinsic function called mul is used to multiply the input vPosition vector by the view_proj_matrix matrix. This intrinsic is very commonly used in vertex shaders to perform vector-matrix multiplication. In this case, vPosition is treated as a column vector since it is the second parameter to mul. If the vPosition vector were the first parameter to mul, it would be treated as a row vector. The mul intrinsic and other intrinsics will be discussed in more detail later in the chapter. Following the transformation of the input position vPosition to clip space, vPosition is multiplied by another matrix called texture_matrix0 to generate a 3D texture coordinate. The results of both of these transformations have been written to members of a VS_OUTPUT structure, which is returned. A vertex shader must always output a clip-space position at a minimum. Any additional values output from the vertex shader are interpolated across the rasterized polygon and are available as inputs to the pixel shader. In this case, the 3D Pshade is passed from the vertex to the pixel shader via an interpolator.
现在看看主函数体,您会发现一个叫做mul的内置函数(intrinsic function)被用来将输入的vPosition向量和view_proj_matrix矩阵进行相乘。这个内置函数经常在顶点着色器代码里被用来进行向量-矩阵乘法。在这个例程中,vPosition被当作一个列向量,因为它是乘法的第二个参数。如果vPosition向量被当作乘法的第一个参数,它就会被当作一个行向量。这个内置的乘法函数和其它内置函数的更多细节会在本章的晚些时候被讨论。在输入的位置vPosition被变换(transformation)到裁剪空间(clip space)以后,vPosition被乘以了另外一个叫做texture_matrix0的矩阵以产生一个3维纹理坐标。这两次变换的结果被写到了一个VS_OUTPUT结构体的成员中,而后这个结构体被返回。顶点着色器始终至少输出一个位于裁剪空间内的位置。任何被其他从顶点着色器附加的输出都会通过光栅化多边形进行内插值计算,而后作为像素着色器的输入。在这个例程中,3D Pshade通过内插值从顶点着色器被传递到像素着色器。
-----------------------------------------------------------
每天进步一点
浙公网安备 33010602011771号