HLSL learning I

Source: Internet
Author: User

HLSL

Glsl

CG HLSL

Rendermonkey

HLSL (high level Shader Language) High-level coloring Language

 

In this chapter, we describe the advanced coloring language (HLSL). We cover three chapters with vertices and pixels. In short, vertices and pixels are small custom programs we write, executed on the graphics card GPU (graphics processing unit), replacing part of the fixed function pipeline. Using our custom program to replace the original fixed part, we get a very flexible graphic effect. We are not subject to fixed restrictions.

In order to write the coloring program, we need to write it in a language. In dx8.x, the shader is written in a low-level coloring language. Fortunately, we no longer use low-level coloring languages. In DX9, we provide a high-level coloring language, which allows us to write the coloring tool. Use HLSL to replace the same advanced usage. Similar to C ++, it covers applications written in assembly language:

 

Increased productivity

Improved readability

For compilers, more code is generated, which is more efficient than manual assembler.

 

Using the HLSL compiler, We can compile our code to generate various versions. In low-level coloring languages, we have to write a copy for each version.

HLSL is also very similar to C/C ++, so it is very easy to learn.

Finally, you will need a ref device to run your example, if your graphics card does not speak after the vertex and pixel. Using the ref device will be very slow, but they can still produce the correct results, allowing us to verify the correctness of our code.

Note: You can use the software vertex processing tool-d3dcreate_software_vertexprocessing.

Target

Learn to write and compile a ELE. Me HLSL shader

Learn how to make applications communicate with the shader

Familiar with its syntax, type, and built-in function HLSL

Write a ELE. Me HLSL shader

Global Object

First, we define two global variables:

Matrix viewprojmatrix;

Vector Blue = {0.0f, 0.0f, 1.0f, 1.0f };

The first variable, viewprojmatrix, is a matrix type. It is constructed within HLSL as a 4x4 matrix. This type stores views and projection matrices, which describe two transformations. In this way, we have to replace the two steps by operating a matrix vector. Note that we initialize this variable without any Code related to the shader. This is because although we have application code-it is not a shadow. The communication between the shader and the application is very frequent.

Second variable

Blue, defined by vector, is a 4D vector. We initialize it in blue as a vector of rgba color.

Input and Output Structures

After defining global variables, we define two special structures. We call the input and output structures. Provided to vertices. This write structure defines the vertex data input and output by the shader, respectively.

16.2 compile an HLSL shader

Constant table

Each color has a constant table that uses the variables that store it.

The d3dx Library provides us with a brain program to access the structure of the constant table of the colorant.

Syntax of advanced coloring language:

Variable type

Structure

Keywords

Statement, and conversion

Basic Process Control

Operator

User-Defined Functions

Built-in functions

Scalar type

Boolean Type-True or false value. Note that HLSL provides the true or false keyword.

Int type-32-bit signed integer

Half-16-bit floating point number

Float Type-32bit floating point number

Double-64bit floating point number

Note: Some platforms may not support int, half, and double. In this case, these types are simulated using float.

Vector type

HLSL provides the following built-in vector types:

Vector-a 4D vector each member is a float

Vector <t, n> is an n-dimensional vector, each of which is a scalar T. The dimension must be 1 to 4. Here is an example of 2ddouble vector:

We can use array labels to access the syntax. For example, to set the vector vec of the I component, we write:

VEC [I] = 2.0f;

In addition, we can access each structure using X, Y, Z, W, R, G, B, and.

This R, G, B, And a provide exact same components respectively. When you paint a color, use the rgba symbol to more colors that match the added depicted vector.

Alternatively, we can use these other pre-defined 2D, 3D, and 4d vectors to respectively:

Float2 vec2;

Float3 vec3;

Float4 vec4;

Consider vector u = (UX, Uy, uz, UW) And suppose we want to copy u to V like V = (UX, Uy, Uy, UW ). most of them will solve individual replication U and V as necessary. However, HLSL provides a special unordered syntax called swizzles:

Vector u = {1.0f, 2.0f, 3.0f, 4.0f };

Vector v = {0.0f, 0.0f, 5.0f, 6.0 };

V = U. xyyw;

When copying a vector, we do not have every component. For example, we can copy only group X and group y as code instructions.

Vector u = {1.0f, 2.0f, 3.0f, 4.0f };

Vecotr v = {0.0f, 0.0f, 5.0f, 6.0f };

V. XY = u;

Matrix Type

HLSL has the following built-in types:

Matrix-a 4x4 matrix. Each object is of the float type.

Matrix <t, m, n>-An mxn matrix. Each object is a scalar type. The dimension M and N must be between 1 and 4. Here is an integer of 2x2:

Matrix <int, 2, 2> m2x2;

As a choice, we can define an mxn matrix. m and n are between 1 and 4 and use the following syntax:

Floatmxn matmxn;

Note: This type requires not only float-we can use other types. For example, we can use integers and write as follows:

Int2x2 i2x2;

Double2x2 d2x2;

We can use subscript to access each object. For example, to set the matrix m of the IJ object, we can write it:

M [I] [J] = value;

In addition, we can refer to matrix m as the structure for accessing the entire member. The following entities are defined:

M. _ 11 = M. _ 12 = M. _ 13 = M. _ 14 = 0.0f;

Clear 0

Sometimes we want to refer to special rows in matrix. We can use a single array to write the syntax below. For example, for reference to the I-row vector matrix m, we can write:

Vector ithrow = m [I];

Note: We can initialize variables in HLSL using the following method:

Vector u = {0.6f, 0.3f, 1.0f, 1.0f };

Vector v = {1.0f, 5.0f, 0.2f, 1.0f };

Or use the syntax Constructor

Vector u = vector (0.6f, 0.3f, 1.0f, 1.0f );

Vector v = vector (1.0f, 4.0f, 0.2f, 1.0f );

Some other examples:

Float2x2 f2x2 = float2x2 (1.0f, 2.0f, 3.0f, 4.0f );

Int2x2 M = {1, 2, 4 };

Int n = int (5 );

Int A = {5 };

Float3 x = float3 (0, 0 );

Array

We can define special C ++ Syntax:

Float M [4] [4];

Half P [4];

Vector V [12];

Structure

The structure is frequently used in C ++. However, the HLSL structure cannot contain function members. Here is an example of a HLSL structure:

Struct mystruct

{

Matrix T;

Vector N;

Float F;

Int X;

Bool B;

};

Typedef keyword

Like C ++, The hlsltypedef keyword function can replace the same type of vector <float, 3> using the following syntax:

Typedef vector <float, 3> point;

Point NBT;

Two examples show how to use the constant type and array with the typedef Keyword:

Typedef const float cfloat;

Typedef float point2 [2];

Variable prefix

The following keywords are defined as the prefix of the variable:

Static Uniform extern shared volatile const

Keywords, statements, and conversions

Basic Process Control

HLSL supports many c ++-like statement selection, loop, and general process control. The syntax is completely C ++.

Return (expression );

If and if... else statements

If (condition)

{

}

If (condition)

{

}

Else

{

}

For statement

For (initial; condition; increment)

{

Statement (s );

}

While statement

While (condition)

{

}

Do

{

} While (condition );

For while do-while is exactly the same as C ++.

Conversion

HLSL supports flexible conversion of scheme. The conversion syntax is similar to the C language of HLSL. For example, to convert a float to a matrix, we can write:

Float F = 5.0f;

Matrix M = (matrix) F;

For example, in the book, you can get the conversion to syntax. However, if you want to learn more about the supported conversions, In the DirectX SDK documentation, under the tab directory, see DirectX graphics \ reference \ shader reference \ High shading language \ type.

Operator

HLSL supports many c ++ operators. Except for the following, they are exactly the same as C ++. The following table lists operators with limited HLSL values:

[].> <=> =! ===! & |? : ++ =--= ** = // = % = + + -- = (),

Although the operator behavior is similar to C ++, there are differences. First, the modulus % operator operates on two integers and floating point types. Purpose: perform the demodulo operation. The left and right values must have the same symbol (e. g, both must be positive or negative ).

Second, observe that many HLSL operators work in basic operations for each hunger. Many components exist in the language structure and these class habits. In component-level operations, operations such as vector/matrix appending, vector/matrix subtraction, and tests that are equal to vector/matrix can be completed using the same operator. We use the scalar type. Let's take a look at the example below.

Note: The operator uses scalar (because it is usually in the C ++ mode ).

Vector u = {1.0f, 0.0f,-3.0f, 1.0f };

Vector v = {-4.0f, 2.0f, 1.0f, 0.0f };

Add each component:

Sum ++;

Multi-Vector Product

Vector

The bool vector contains the results of each comparison, for example:

Vector u = {1.0f, 0.0f,-3.0f, 1.0f };

Vector v = {-4.0f, 0.0f, 1.0f, 1.0f };

Vector B = (u = V );

Finally, we speculate that the binary operations for variable upgrading are as follows:

Binary operations, if the left and right dimensions, use small dimensions to promote the use of large dimensions for conversion. For example, if X is a float or shifloat3 expression (x + y) variable X is evaluated as float3. this promotion is completed using definition conversion.

In this case, we use the conversion scalar to the vector; therefore, X is converted to float3 x = (x, x, x) after being promoted as the scalar to the vector conversion imperium. Note: For example, we cannot convert promote float2 to float3 because the village definition is different.

For binary operations, if the coordinates are of different types from those on the right, convert them to the same type and use the high type.

For example, if X is an int OR Shi half, X is evaluated as a half value in the expression variable X.

User-defined functions:

The following attributes are available in HLSL.

Function C ++ syntax

The parameter is always set as a value.

Recursion is not supported.

Functions are always inline.

In addition, HLSL adds some extended keyword functions. For example, consider writing

Bool Foo (INT const bool B, out int R1, inout float R2)

{

If (B)

}

Built-in functions

HLSL has a wide range of built-in functions for 3D graphics. The following is a simplified table. In the two chapters, we will learn how to use this function. Now, get familiar with it.

Note: For more information, see the DirectX document. The following content table ,.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.