HLSL advanced coloring language basics

Source: Internet
Author: User
Tags pixel coloring

Language basics

Keywords

(* Indicates case sensitivity)

Asm * bool compile const decl * do double else extern false float

Half if in inline inout int matrix * out pass * return sampler pixelshader *

Shared static string * struct texture * true typedef uniform vector * void while technique *

Volatile texture * vertexshader *

The following keywords are reserved words but not used:

Auto break case catch char class compile const const_cast continue default delete

Dynamic_cast enum explicit friend goto long mutable namespace new operator private

Protected public register reinterpret_cast short signed sizeof static_cast switch template

This throw try typename union unsigned using virtual

(How do I feel like C ++? GPU programming is on the same path as CPU, assembly-> Class C ++ !?)

Data Type

Scalar type:

Bool -- true or false.

Int -- 32-bit signed integer.

Half -- 16-bit floating point value.

Float -- 32-bit floating point value.

Double -- 64-bit floating point value.

Vector type:

Vector -- a four-dimensional vector. Each element is a float value.

Vector <scalar type, dimension> -- a vector with a specific dimension. Each element is a scalar type.

Example:

Float4 fVector0;

Float fVector1 [4];

Vector fVector2;

Vector <float, 4> fVector3;

Define the three tuples of the bool type:

Bool3 bVector0;

Bool bVector1 [3];

Vector <bool, 3> bVector2;

Access vector: (each element can be used for access, or swizzle (refer to the basic part of shaderX1 Vertex coloring tool ))

Float4 pos = {3.0f, 5.0f, 2.0f, 1.0f };

Float value0 = pos [0]; // value0 = 3.0f;

Float value1 = pos. x;

Float value2 = pos. g; // value2 = 5.0f;

Float2 vec0 = pos. xy;

Float2 vec1 = pos. ry; // invalid. Either xyzw or rgba is used.

Matrix Type:

Float4x4 view_proj_matrix;

Float4x4 texture_matrix0;

Float3x4 mat0;

Matrix <float, 3, 4> mat1;

For matrix-type access, you can use subscripts such:

Float fValue = view_proj_matrix [0] [0];

You can also access the service in the following mode:

_ M00, _ m01, _ m02, _ m03

_ M10, _ m11, _ m12, _ m13

_ M20, _ m21, _ m22, _ m23

_ M30, _ m31, _ m32, _ m33

Or

_ 11, _ 12, _ 13, _ 14

_ 21, _ 22, _ 23, _ 24

_ 31, _ 32, _ 33, _ 34

_ 41, _ 42, _ 43, _ 44

You can also use array access for Matrix access:

Float2x2 fMat = {3.0f, 5.0f, // Row 1

2.0f, 1.0f}; // Line 2

Float value0 = fMat [0]; // value0 = 3.0f a truncation occurs here

Float value1 = fMat. _ m00; // value1 = 3.0f

Float value2 = fMat. _ 12; // value2 = 1.0f

Float value3 = fMat [1] [1]; // value3 = 1.0f

Float2 vec0 = fMat.21 _ 22; // vec0 = {2.0f, 1.0f}

Float2 vec1 = fMat [1]; // The value of value0 corresponding to vec1 = {2.0f, 1.0f}

Modifier


The variable that cannot be changed by the shadow code is of the const type. The row_major and col_major types are used to specify the matrix layout. Row_major indicates that each row of the matrix is stored in a constant register (cx, check shaderx1). If it is stored by column, col_major is used, which is the default value.

Storage Type Modifier

Like C, variables can be declared as "static" or "exter" n, which are mutually exclusive. In the global domain, the "static" identifier indicates that the variable can only be accessed by the shadow but not by the application through API. any non-static variables in the global domain can be modified by the application through APIS. using "static" in the local domain indicates that the data contained in the variable remains unchanged in the function call. "exter" n Identifier if it is used in the whole local area, it indicates that the variable can be modified by the API outside the shadow, and the default type of the variable in the global domain is "extern ". the "shared" modifier indicates that the specified global variables can be shared in different effects. if you use the "uniform" modifier for a variable, its value is considered to be Set outside the HLSL coloring Program (for example, using the Set * ShaderConstant * () function ). the global variable is declared as "uniform" by default, but does not have the const attribute.

For example:

Extern float trans‑cycoeff;

Const float gloss_bias;

Static float gloss_scale;

Float diffuse;

The trans‑cycoeff and diffuse variables are set through the API. gloss_bias can be set through the API, but the coloring program cannot be changed. Finally, gloss_scale cannot be modified by the API, but can be modified by the coloring tool.

Type conversion

To write a simple and efficient coloring program, you should have some knowledge about HLSL type conversion. The following example is to extend a scalar to a 4D vector:

Float4 vResult = 0.0f; // vResult = {0.0f, 0.0f, 0.0f, 0.0f };

If the data is converted from a high dimension to a low dimension, extra data will be ignored.

Shader Input

Vertex and pixel coloring devices have two types of input data: varying and uniform. the input of the varying type indicates that the data is unique to each executable shader program. for Vertex coloring devices, varying data (such as location and normal vector .) from vertex stream. uniform data (for example, material color, world coordinate system conversion, etc.) remains unchanged in the execution of multiple coloring devices. if you are familiar with the Assembly mode, what is the uniform data in the constant register, and what is the varying data in the Vx or Tx register.

Uniform Input

There are two methods in HLSL to specify the Uniform data. generally, a global variable is declared. any global variable used in the colorant will cause the variable to be added to the uniform variable table required by the colorant. the second method is to display the uniform modifier at the declaration of the input parameter, which will also add the variable to the uniform variable table used by the coloring tool. the following is an example:

// Declare a global uniform variable

// Register as 'uniformglobal 'in the constant table'

Float4 UniformGlobal;

// Declare an uniform Parameter

// Register as '$ uniformparam' in the constant table'

Float4 main (uniform float4 UniformParam): POSITION

{

Return UniformGlobal * UniformParam;

}

The uniform variables used by the shader are associated with the application through the constant table (constant table. A constant table is a symbol table that defines how the uniform variable is loaded into the constant register before the coloring er executes. the uniform data in the parameter is preceded by "$" to prevent conflicts with the name of the uniform variable in the same region. the constant table contains the constant Register address used for the uniform data used in all the colored devices. this table also contains type information and default values. the constant table is generated by the compiler and stored in the compressed binary format. the API that explains the table during running will be described in the following sections.

Varying Input

Varying data is used to mark input parameters, indicating that they have input Association semantics. All high-level coloring machine input must have the attribute of varying or uniform. Otherwise, compilation will fail.

Input join indicates the name of the input used to connect the given shader to the output of the rendering pipeline of the previous stage. for example, enter POSITION0 In the vertex coloring machine to specify the location where the position data in the vertex buffer should be linked.

Pixels and Vertex coloring devices have different input Association semantics, depending on different coloring units in the graphic pipeline. Vertex coloring er input semantics describes how to convert a single vertex information loaded in the vertex buffer into a form that can be used by the vertex coloring tool (for example, location, normal vector, coordinate, color, tangent, binary vectors, etc .). These input semantics are directly mapped to the D3DDECLUSAGE enumeration combination. UsageIndex is used to describe the vertex elements in the vertex buffer.

The pixel shader inputs are associated to describe the information of each pixel provided by the raster unit. The Correlated data is the result of interpolation between the output values of each vertex of the current element through the vertex coloring tool. The basic pixel coloring input Association links the color and texture coordinates to the input parameters.

There are two ways to assign an input join to the coloring er input. The first method is to add ":" semicolon and association name after the input parameter declaration. the second method is to define a struct. the following is an example:

// Declare an input structure using association binding

Struct InStruct

{

Float4 Pos1: POSITION1

};

// Declare the Pos variable as the data containing the location

Float4 main (float4 Pos: POSITION0, InStruct In): POSITION

{

Return Pos * In. Pos1;

}

// Declare the Col variable as a parameter containing color (COLOR0) Interpolation

Float4 mainPS (float4 Col: COLOR0): COLOR

{

Return Col;

}

Below is the input Association of the vertex shader

Description

POSITIONn location

BLENDWEIGHTn hybrid width

BLENDINDICESn hybrid Index

NORMALn standard Vector

PSIZEn point size

COLORn color

TEXCOORDn texture coordinates

TANGENTn tangent

BINORMALn subnormal

TESSFACTORn subdivision factor

Below is the input Association of the pixel shader

Description

COLORn color

TEXCOORDn texture coordinates

(N is an optional integer variable .)


Coloring er output

The data provided by the vertex and pixel shader is used by the subsequent graphic pipeline. the output Association is used to specify how the data generated by the shader is linked to the input in the next stage. for example, the output Association of the vertex coloring er links the result obtained by the vertex coloring program to the input Association of the pixel coloring er.

Vertex coloring er output Association is used to connect the coloring machine to the pixel coloring machine and the grating phase. POSITION output is the data that each vertex must output to the grating without being exposed to the pixel coloring machine,

TEXCOORDn and COLORn indicate that the output is used by the pixel coloring tool for interpolation.

The pixel coloring er output Association binds the output color to the correct rendering target. the color output is connected to the alpha mixing stage. DEPTH output Association is used to change the target DEPTH value of the current raster location. the output Association semantics is the same as the input association declaration.

Below is the output Association of the vertex shader

Description

POSITION

PSIZE point size

FOG vertex FOG

COLORn color

TEXCOORDn texture coordinates

The following is the output Association of the pixel shader.

Description

COLORn: Specifies the color of the rendering target n.

DEPTH Value

Example:

// Declare an output structure with associated binding

Struct OutStruct

{

Float2 Tex2: TEXCOORD2

};

// Declare Tex as an output parameter, which contains coordinate data

Float4 main (out float2 Tex0: TEXCOORD0, out OutStruct Out): POSITON

{

Tex0 = float2 (1.0, 0.0 );

Out. Tex2 = float2 (0.1, 0.2 );

Return float4 (0.5, 0.5, 0.5, 1 );

}

// Declare the Col variable containing color Interpolation

Float4 mainPS (out float4 Col1: COLOR1): COLOR

{

Col1 = float4 (0.0, 0.0, 0.0, 0.0 );

Return float4 (1.0, 0.9722, 0.3333334, 0 );

}

Struct PS_OUT

{

Float4 Color: COLOR;

Float4 Depth: DEPTH;

};

// Three methods output from the pixel shader

PSOUT PSFunc1 (){...}

Void PSFunc2 (out float4 Color: COLOR, out float Depth: DEPTH ){...}

Void PDFunc3 (out PS_OUT)

{...}

Example of a shader

Float4x4 view_proj_matrix;

Float4 view_position;

Float4 light0;

Float4 light1;

Float4 light2;

Struct VS_OUTPUT

{

Float4 Pos: POSITION;

Float3 view: TEXCOORD0;

Float3 Normal: TEXCOORD1;

Float3 Light1: TEXCOORD2;

Float3 Light2: TEXCOORD3;

Float3 Light3: TEXCOORD4;

};

VS_OUTPUT main (float4 inPos: POSITION, float3 inNorm: NORMAL)

{

VS_OUTPUT out = (VS_OUTPUT) 0;

Out. Pos = mul (view_proj_matrix, inPos );

Out. Normal = inNorm;

Out. View = normalize (view_position-inPos );

Out. Light1 = normalize (light0-inPos );

Out. Light2 = normalize (light1-inPos );

Out. Light3 = normalize (light2-inPos );

Return out;

}

The vertex shader declares several fully-local variables at the beginning, both of which are implicitly declared as uniform, so they can be accessed by the API and the shader. next we will see a VS_OUTPUT struct. We can see from the struct definition that the vertex shader will output 5 3D texture coordinates in addition to a 4D position. now let's look at the main function. A 4D vector is used as the position input, and a 3D vector is used as the normal vector input. inPos is operated through the mul function, but inNorm is not used. finally, the data after the dry function operation will be interpolated in the polygon.

Next I will compile this file. First, we will copy the above code into a text file and rename it as NPRMetallic. vhl. Then input the following command on the command line:

"Fxc-nologo-T vs_1_1-Fc-Vd NPRMetallic. vhl ".

Because this Vertex coloring tool does not require flow control, we use vs_1_1 to compile the target.

The output is as follows:

//

// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111

//

// Fxc-nologo-T vs_1_1-Fc-Vd j: \ NPRMetallic. vhl

//

//

// Parameters:

//

// Float4 light0;

// Float4 light1;

// Float4 light2;

// Float4 view_position;

// Float4x4 view_proj_matrix;

//

//

// Registers:

//

// Name Reg Size

//-------------------------

// View_proj_matrix c0 4

// View_position c4 1

// Light0 c5 1

// Light1 c6 1

// Light2 c7 1

//


Vs_1_1

Dcl_position v0

Dcl_normal v1

Mul r0, v0.y, c1

Mad r0, c0, v0.x, r0

Mad r0, c2, v0.z, r0

Mad oPos, c3, v0.w, r0

Add r0,-v0, c4

Dp4 r0.w, r0, r0

Rsq r0.w, r0.w

Mul oT0.xyz, r0.w, r0

Add r0,-v0, c5

Dp4 r0.w, r0, r0

Rsq r0.w, r0.w

Mul oT2.xyz, r0.w, r0

Add r0,-v0, c6

Dp4 r0.w, r0, r0

Rsq r0.w, r0.w

Mul oT3.xyz, r0.w, r0

Add r0,-v0, c7

Dp4 r0.w, r0, r0

Rsq r0.w, r0.w

Mul oT4.xyz, r0.w, r0

Mov oT1.xyz, v1


// Approximately 21 instruction slots used

The top part of the code is the input parameters of the vertex coloring machine. These parameters need to be set through the API to make the coloring machine work normally. the following part is the allocation of hardware registers. there are 21 assembly commands after rent. now it is not necessary to read from the beginning to the end, but pay attention to the two statements dcl_position and dcl_normal, which represent the two parameters of the main function. in addition, place the output result in oPos, oT0 ~ 4.

Now we need to use the vertex coloring tool to transform the ry to the clipping space and define the values that will be used in polygon interpolation. Next we will perform pixel coloring:

Float4 Material;

Sampler Outline;

Float4 main (float3 View: TEXCOORD0, float3 Normal: TEXCOORD1, float3 Light1: TEXCOORD2, float3 Light2: TEXCOORD3, float3 Light3: TEXCOORD4): COLOR

{

// Standardized input vector

Float3 norm = normalize (Normal );

Float4 outline = tex1D (Outline, 1-dot (norm, normalize (View )));

Float lighting = (dot (normalize (Light1), norm) * 0.5 + 0.5) +

(Dot (normalize (Light2), norm) * 0.5 + 0.5) +

(Dot (normalize (Light3), norm) * 0.5 + 0.5 );

Return outline * Material * lighting;

}

Compile with "fxc-nologo-T ps_2_0-Fc-Vd NPRMetallic. phl"

The generated code is as follows:

 

//

// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111

//

// Fxc-nologo-T ps_2_0-Fc-Vd J: \ shader \ NPRMetallic. phl

//

//

// Parameters:

//

// Float4 Material;

// Sampler2D Outline;

//

//

// Registers:

//

// Name Reg Size

//---------------------

// Material c0 1

// Outline s0 1

//

Ps_2_0

Def c1, 1, 0.5, 0, 0

Dcl t0.xyz

Dcl t1.xyz

Dcl t2.xyz

Dcl t3.xyz

Dcl t4.xyz

Dcl_2d s0

Nrm r0.xyz, t2

Nrm r1.xyz, t1

Dp3 r1.w, r0, r1

Mad r1.w, r1.w, c1.y, c1.y

Nrm r0.xyz, t3

Dp3 r0.x, r0, r1

Mad r1.w, r0.x, c1.y, r1.w

Add r1.w, r1.w, c1.y

Nrm r0.xyz, t4

Dp3 r0.x, r0, r1

Mad r1.w, r0.x, c1.y, r1.w

Add r1.w, r1.w, c1.y

Nrm r0.xyz, t0

Dp3 r0.x, r1, r0

Add r0.xy,-r0.x, c1.x

Texld r0, r0, s0

Mul r0, r0, c0

Mul r0, r1.w, r0

Mov oC0, r0

// Approximately 29 instruction slots used (1 texture, 28 arithmetic)

Same as the vertex coloring machine, you need to use APIs to set the variables starting with the full local area. after the ps_2_0 command, there is a def command. this command is a free command in the assembly instruction stream. It defines the constants used by ALU operations. this constant is generally the literal value in the HLSL coloring machine, such as the following code segment :... 1-dot (norm, normalize (view )... dot (nomalze (Light1), norm) * 0.5 + 0. 5... after a constant is defined, there are five 3d texture coordinate declarations, such as dcl tn. xyz. these are the input associations of main function parameters. The following is a sampler Declaration: dcl_2d s0, indicating that the sampling machine No. 0 is bound to a 2D texture. texld indicates that only one of the texture coordinates needs to be processed.

 

Reference: ShaderX2.-. Introductions. and. Tutorials. with. DirectX9, Wolfgang Engel. (2004)

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.