Basic syntax for HLSL 1 data types
1.1 Scalar types
1. Bool:true or FALSE. Note that the HLSL provides the true and False keywordslike in C + +.
2. Int:32-bit Signedinteger.
3. Half:16-bit-floatingpoint number.
4. Float:32-bit-floatingpoint number.
5. Double:64-bit-floatingpoint number.
1.2 Vector type
1.2.1 Vector type
1. float2:2d vector, wherethe components is of type float.
2. Float3:3d vector, wherethe components is of type float.
3. float4:4d vector, wherethe components is of type float.
In addition to float, we can also use other basic types to define vectors, such as bool2,int3. In short, the expression type of the vector is Typen, where n belongs to (2,4).
Initialization of the 1.2.2 vector
Float3 v = {1.0f, 2.0f, 3.0f};
Float2 w = float2 (x, y);
Float4 u = float4 (W, 3.0f, 4.0f); U= (w.x, W.y, 3.0f, 4.0f)
Access to 1.2.3 vectors
With array syntax:
such as: vec[i] = 2.0f;
Use letter syntax, Xyzw,rgba.
Such as:
vec.x = VEC.R = 1.0f;
Vec.y = VEC.G = 2.0f;
Vec.z = VEC.B = 3.0f;
VEC.W = VEC.A = 4.0f;
1.2.4 Vector substitution leveling (swizzles)
To avoid the complexity of vector assignment, HLSL takes a swizzles approach, as follows:
Float4 u = {1.0f, 2.0f, 3.0f, 4.0f};
Float4 v = {0.0f, 0.0f, 5.0f, 6.0f};
v = u.wyyx; v = {4.0f, 2.0f, 2.0f,1.0f}
v = u.wzyx; v = {4.0f, 3.0f, 2.0f,1.0f}
V.xy = u; v = {1.0f, 2.0f, 5.0f,6.0f}
1.3 Matrix Types
Definition syntax for 1.3.1 matrices
TYPEMXN xxx, such as:
1. float2x2:2x2 matrix, where theentries is of type float.
2. half3x3:3x3 Matrix, where theentries is of type half.
3. int4x4:4x4 Matrix, where theentries is of type int.
4. bool3x4:3x4 Matrix, where theentries is of type bool.
Access to the 1.3.2 matrix
With array syntax:
M[i] [j] = value;
With member variable syntax:
M._11 = M._12 = m._13 = M._14 = 0.0f;
m._21 = m._22 = m._23 = m._24 = 0.0f;
m._31 = m._32 = m._33 = m._34 = 0.0f;
m._41 = m._42 = m._43 = m._44 = 0.0f;
Or
m._m00 = M._M01 = m._m02 = M._m03 =0.0f;
M._M10 = M._M11 = M._m12 = M._m13 =0.0f;
M._M20 = M._m21 = M._M22 = M._m23 =0.0f;
M._M30 = M._M31 = M._m32 = M._m33 =0.0f;
Entire row of Access:
FLOAT3 N = normalize (PIN.NORMALW);
FLOAT3 T = Normalize (Pin.tangentw-dot (PIN.TANGENTW, N) *n);
FLOAT3 B = Cross (n,t);
float3x3 TBN;
Tbn[0] = T; Sets row 1
TBN[1] = B; Sets Row 2
TBN[2] = N; Sets Row 3
Initialization of the 1.3.3 matrix
float2x2 FXX =float2x2 (1.0f,2.0f,3.0f,4.0f);
int2x2 Ixx ={1,2,3,4};
Alternative definition of 1.3.4 array vectors
Vector u = {1.0f, 2.0f, 3.0f, 4.0f};
Matrix M; 4x4 matrix
1.4 Arrays
float M[4] [4];
Half p[4];
FLOAT3 v[12]; 3D vectors
1.5 Structural bodies
The struct in HLSL is similar to a struct in C, which cannot contain function members, and access members only need to be based on subscripts.
struct SURFACEINFO
{
FLOAT3 POS;
FLOAT3 Normal;
FLOAT4 Diffuse;
FLOAT4 spec;
};
Surfaceinfo v;
Litcolor + = V.diffuse;
Dot (Lightvec, v.normal);
float specpower = max (V.SPEC.A, 1.0f);
1.6 Keywords related to variables
typedef , Static , Uniform , extern , Const
Usage is similar to C + +, when the variable is declared static, it is an internal variable and is not visible outside the shader program. Non-static global variables are extern by default, which means they can be accessed by programs outside of the shader program.
1.7 Forced type conversions
The type conversions in HLSL are very flexible, as follows:
float F = 5.0f;
float4x4 m = (float4x4) F; Copy Finto each entry of M.
FLOAT3 n = float3 (...);
FLOAT3 v = 2.0f*n-1.0f;
The 1.0f here is actually converted into (1.0f,1.0f,1.0f);
2 Syntax and functions
2.1 Syntax
2.1.1 Return
return (expression);
2.1.2 If-else Statements
if (condition)
{}
..........................................
if (condition)
{}
Else
{}
2.1.3 For Loop
for (Initial;condition;increament)
{}
2.14 While
while (condition)
{}
2.1.5do-while
Do
{}while (condition);
2.2 Functions
Features of the 2.2.1 HLSL function
Like C + +, parameters are passed by value (references and pointers are not supported), recursion is not supported, functions are always inline
Examples of 2.2.2 functions
BOOL Foo (in const bool B, out int r1,inout float R2)
{
if (b)//test input value
{
r1= 5; Output a value through R1
}
Else
{
R1 = 1; Output a value through R1
}
Sincer2 is inout we can use it as an input value and also output a value through it
r2= R2 * R2 * R2;
Returntrue;
}
Here's what you need to explain is the keyword In,out,inout
In: Default, can not add. That indicates that a parameter value must be passed.
Out: Indicates that the parameter is a return value of the function and is used only as output, not as input
InOut: Both input and output can be
Semantics (semantics) in 3 HLSL
It is often used to define HLSL either as a parameter variable entered in a function or as a variable that is returned, as in the case of Xxx:position, a place where variables in HLSL are different. ': ' is called semantics, and it is used to describe some information about variables, in essence semantics is to specify the relationship between shader variables and hardware, for example position specifies that the variable is a register that is used in the location-related.
3.1 Semantics of vertex shaders
Semantics have input and output parts, and the input and output of vertices are shown below:
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 |
It can be seen from the above that the semantics of the variable is not random, first it indicates the use of variables, and secondly for the input and output have special restrictions on the semantics, we can not use as input semantics to use as output.
n is the index value of the register, and the maximum value depends on the hardware support. such as POSITION1.
Semantics of 3.2 pixel shader
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. To convert a Direct3D 9 shader (so uses this semantic) to a Direct3D shader, seeDirect3D 9 vPOS and Direct3D S V_position) |
Float2 |
Output |
Description |
Type |
Color[n] |
Output Color |
Float4 |
Depth[n] |
Output depth |
Float |
3.3 Examples
void Skyvs (float3posl:position0,out float4 oposh:position0,out float3 oenvtex:texcoord0) { Oposh = Mul (FLOAT4 (POS L, 1.0f), G_MATWVP). Xyww; Oenvtex = POSL;}
/////////////////////////////////
struct outputvs{ float4 posH : POSITION0; FLOAT2 tex0 : TEXCOORD0; D3D fills in for point sprites. float size : PSIZE; In pixels.}; Outputvssnowvs (float3 posl : POSITION0, float3 vel : TEXCOORD0, float size : TEXCOORD1, float initialt:texcoord2, float lifet:texcoord3) { Outputvs Outvs = (Outputvs ) 0; float t = g_time-initialt; POSL = Posl + vel * t + 0.5f * G_ACC *t * t; Outvs.posh = Mul (Float4 (POSL, 1.0f), G_MATWVP); Outvs.size = 0.0035f * g_viewportheight* size; return Outvs;} FLOAT4 Snowps (float2tex0:texcoord0): color{ return tex2d (Texs, tex0);}
Reference documents
"1" Introduction to 3D Game programming with DIRECTX11
"2" Semantics (DirectX HLSL) in Microsoft DirectX Sampler
Basic syntax for HLSL