Analysis of parameter passing in the vertex shader and pixel shader functions in HLSL shader programming

Source: Internet
Author: User

HLSL is basically written in the language of C, but I personally feel that the most difficult way to get started is to pass in the parameters of the two functions of the vertex shader and pixel shader completely in the C language.
The following is an example of your own understanding in the simplest HLSL effect framework.
uniform extern float4x4 GWVP;

struct OUTPUTVS
{
FLOAT4 Posh:position0;
FLOAT4 color:color0;
};

Outputvs Colorvs (float3 posl:position0, Float4 c:color0)
{
Zero out of our output.
Outputvs Outvs = (Outputvs) 0;

Transform to homogeneous clip space.
Outvs.posh = Mul (Float4 (POSL, 1.0f), GWVP);

Just Pass the vertex color into the pixel shader.
Outvs.color = C;
Done–return the output.
return Outvs;
}

Float4 Colorps (float4 c:color0): COLOR
{
return C;
}

Technique Colortech
{
Pass P0
{
Specify the vertex and pixel shader associated with this pass.
VertexShader = Compile vs_2_0 colorvs ();
PixelShader = Compile ps_2_0 colorps ();
}
}
The first is the defined Outputvs, which is the output of the vertex shader, not the tube first. Look at the parameters inside the vertex shader function:
Outputvs Colorvs (float3 posl:position0, Float4 c:color0)
{
}
Compared to the C language function, the most confusing for beginners is the inside two parameters float3 Posl:position0, Float4 C:color0 is where to pass in. In fact, these two parameters are fixed by the DirectX Pipeline pass in. In DirectX We will define our vertex format and register it as follows:
//============================================================
Define our vertex format first in the relevant initialization function of DirectX
struct VERTEXCOL
{
Vertexcol ():p os (0.0f, 0.0f, 0.0f), col (0x00000000) {}
Vertexcol (float x, float y, float z, d3dcolor c):p os (x, Y, z), col (c) {}
Vertexcol (const d3dxvector3& V, d3dcolor c):p os (v), col (c) {}

D3dxvector3 POS;
D3dcolor Col;
Static idirect3dvertexdeclaration9* Decl;
};
D3dvertexelement9 vertexcolelements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3ddeclmethod_default, d3ddeclusage_position, 0},
{0, D3ddecltype_d3dcolor, D3ddeclmethod_default, D3ddeclusage_color, 0},
D3ddecl_end ()
};
HR (Gd3ddevice->createvertexdeclaration (vertexcolelements, &vertexcol::D ecl));
//=============================================================
Then tell DirectX our vertex format before calling IDirect3DDevice9::D raw** ().
HR (Gd3ddevice->setvertexdeclaration (vertexcol::D ecl));
//================================================================
So we define the vertex component in the d3dvertexelement9 {... D3ddeclusage_position, 0} is passed to the FLOAT3 posl:position0 in the vertex shader parameter, and {... D3ddeclusage_color, 0} is passed to the FLOAT4 c:color0 in the vertex shader parameter.
The vertex shader is calculated to get the Outputvs of each pixel and output to the pixel shader.
//=============================
Float4 Colorps (float4 c:color0): COLOR
{
return C;
}
The FLOAT4 c:color0 of the upper pixel shader is derived from Float4 color:color0 in Outputvs;
The pixel shader then outputs the calculated color value of each pixel, which is what we see on the screen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.