DirectX11 Advanced Shader language HLSL Getting Started

Source: Internet
Author: User
Tags mul
Advanced Shader language HLSL Getting Started 1. Introduction to Data Types

Unlike the CPU, in the graphics chip, the smallest data huff and puff unit is a 32-bit floating-point number of four-dollar group. It makes sense not to think about all the data that you are involved in the rendering process, the most complex of which is four-dimensional coordinates (X,Y,Z,W) or color (r,g,b,a), so that the GPU can process a four-tuple at a one-time. And the whole thing in the video card is placed in a four-tuple of a component used, and many graphics cards, integers, Boolean values are not directly supported, but instead to use floating point numbers. As for matrices, it usually represents a 4x4 matrix with 4 four tuples (by default, a four-tuple store row, or you can specify a column-by-row storage, which is a detail issue, goto: Details), and so on.
Reflected on the program, a four-dimensional vector is declared as a float4,4-dimensional square matrix that is declared as float4x4, and so on. Of course, you can also use any vector or matrix that is no more than 4 dimensions, such as Int3,float3x3,double1. This double1 is actually a scalar, 1 can be omitted not to write. 2. Texture (texture) & Sampler (Sampler) Introduction

These two things can be considered as special type variables. Texture is the mapping resource used in shader, I think there is nothing to say. To explain the sampler: in fact, each map should use a sampler when used. The sampler is equivalent to a structure that includes sampling information, such as filtering parameters, in addition to the data in the texture itself. Typically, an instruction such as a reading map receives parameters from the type of the sampler rather than directly receiving the texture map. Declaring and using textures or samplers is the same as using normal variables. Here are some ways to initialize the sampler, or wait until later in the example to tell. 3. Data type Encyclopedia

Data types have value types, vectors, matrices, samplers, and structs.
1. Value type
BOOL Boolean variable
Half 16 for cosmetic
int 32-bit shaping
Float single precision floating-point number
Double double-precision floating-point number
Declaration method: Float F;
Assign value way: F = 1;
2. Vector
Declaration mode: FLOAT4 F;
Assignment method: F = {1,2,3,4};
Value method: FLOAT3 ff = F.rgb;
Note: to the specified field in the XYZW or rgba access vector, X or R is the number No. 0 field. Not only can you manipulate a field individually, but you can also manipulate multiple fields, such as 3*f.xyz, by multiplying the XYZ in F by 3.
3. Matrix
Declaration mode: float2x4 F; The column after the advance.
Assignment method: F = {1,1,2,2,3,3,4,4};
Value method: Float ff = f[0][0];
Note: If you want to multiply the matrix, use the MUL function, such as Mul (FF,F).
4. Sampling device
Declaration mode:
Texture texture; Texture variables
Sampler Texturesampler = sampler_state/Texture Sampler
{
Texture =; Texture objects used by the texture sampler
Minfilter = Linear; Reducing graphics using linear filtering
Magfilter = Linear; Using linear filtering to enlarge graphics
Mipfilter = Linear; Mipmap using linear filtering
Addressu = Wrap; The texture addressing mode in the U-Direction adopts wrap method
ADDRESSV = Wrap; The texture addressing mode in v direction adopts wrap method
};
Assignment method: Texture is assigned to the effect in C #. parameters["Texture"]. SetValue (Game.Content.Load ("*"));
Value method: tex2d (Texturesampler, TEXCOORD0);
Description: Minfilter, Magfilter, Mipfilter, Addressu, ADDRESSV are optional, if not write will use the default value, that is, the value given above.
5. Structural body
Declaration mode:
struct Vertexshaderinput
{
FLOAT4 position:position;
FLOAT2 texturecoordinates:texcoord0;
FLOAT3 Normal:normal;
};
Vertexshaderinput input;
Here is a little different from the C # syntax, so write directly, without having to write a new one or something.
Assignment method: Consistent with C # syntax.
Value method: Consistent with C # syntax.4. Control Flow

Control flow is if...else,for,while or something. In the CPU, these control streams result in actually instruction jumps. But in the GPU instruction jump is not widely supported, the past most of the graphics card only know in order a sentence of the execution of instructions. So the HLSL compiler may make a rash of things like unfolding loops, traversing branches, and so on to fit the graphics card. So use caution, and not all control flow statements are supported. Many of the specific rules are still in the details. 5. Function

HLSL provides a number of functions to invoke in the Direct3D document-> DirectX Graphics-> Reference-> HLSL Shader Reference-> HLSL intrinsic Func A detailed list of these functions is available in tions. You can also write your own functions, but in earlier versions of shader, you would eventually have to insert function expansion into the function call at compile time like inline functions. One more thing I think you'll think about is what the main function will be. Vertex shader and pixel shader each need a main function, which is specified by the programmer. Yes, the programmer specifies outside the shader.
The function definitions in HLSL are exactly the same as in other programming languages:

returnvalue functionname (parametername:semantic)
{
//function code goes here
}

The return value of a function can be a type defined in any HLSL, including a combination type and void type. When you define a parameter list for a shader function, you need to fully specify the semantic identifier after the following variable. There are also a few things to note when defining function parameters, because HLSL does not have a specific way to return a reference to the value in the argument list, which requires a small number of keywords to achieve the same result.
Before a parameter declaration, using the keyword out allows the compiler to know that the variable can be used for output. Alternatively, the keyword inout can allow a variable to be both an input and an output:

void GetColor (out float3 color)
{
    color = FLOAT3 (0.0f, 1.0f, 1.0f);
}
6. Vertex shader

When objects are to be drawn through the pipeline, their vertices are sent to the
Processed in your vertex shader. If you do not want to do anything with the incoming vertices, you can directly route them to the pixel shader for rendering. In most cases, you need to use at least one world or projection transformation to function these vertices so that they are rendered in the correct space position.
With vertex shaders, you can do a lot of control over vertices, not just simple transformations. You can translate a vertex to any axis, changing its color, or any other control of its nature.

Ps_input Vs_main (vs_input vertex)
{
    Ps_input vsout = (ps_input) 0;
    Vsout.pos = Vertex.pos;
    vsout.tex0 = vertex.tex0;
    return vsout;
}

This vertex shader looks like a C-language function. HLSL uses a similar C-syntax, to learn HLSL is easier when you understand C + +. We can see the vertex renderer, named Vs, with FLOAT4 as the parameter, and the return value is FLOAT4. In HLSL, FLOAT4 is composed of 4 floating-point numbers. That colon defines the semantics of the parameter, and so does the return value. As mentioned earlier, semantics describes the properties of the data in HLSL. In the above renderer, we select position as the semantics of the input parameter pos, because this parameter contains vertex coordinate information. The semantics returned is sv_position (the SV is the abbreviation for system value). Sv_position is a previously defined semantics, the pixel shader vertex semantics is not POSITION, but sv_position, the difference between the two semantics is that SV indicates that the hardware will be interpolated. Sv_position into PS, as you do in the pixel shader to do perspective division results. See the input structure of the vertex shader and the input structure of the pixel shader:

struct Vs_input
{
    float4 pos:position;
    Float2 tex0:texcoord0;
};
struct Ps_input
{
    float4 pos:sv_position;
    Float2 tex0:texcoord0;
};
7. Pixel Shader

Modern computer monitors usually display fast, and the smallest unit of the screen is pixels. Each pixel has a color, and each pixel is independent of each other. When we want to render a triangle on the screen, we do not draw the whole triangle as an entity. In fact, we draw the pixels of the Triangle area.

The operation of drawing a string of pixels covered by the three vertices of a triangle is called raster. The GPU first has to determine which pixels are covered by triangular regions. The GPU then invokes the activated pixel renderer to render these pixels. The primary goal of a pixel shader is to compute the color of each pixel. The renderer calculates the vertex color based on the input, or if the geometry renderer is not used, as in this tutorial, the input of the pixel renderer comes directly from the output of the vertex renderer.
The pixel shader lets you access any pixel prior to the pipeline output. Before the pixel is drawn to the screen, you have the opportunity to change the color of each pixel. In some cases, you simply return the pixel color passed in by the vertex or geometric shader, but in most cases you need to deal with the effect of the light or texture on the pixel color.

Float4 Ps_main (Ps_input frag): Sv_target
{return
colormap_. Sample (Colorsampler_, frag.tex0);
}

In the above function, Sv_target is the return-worthy semantics, which represents an output semantics that specifies the output of the pixel shader for the render target. 8. Semantics (semantic)

The semantic name (semantic name) is a string that describes the purpose of the element. For example, the element is the position of the vertex, its semantics is "POSITION". We can make the element use semantic "color" for vertex color, using "normal" for normal vectors, and so on. Semantics enable an element to bind to a HLSL shader as its input or output variable.
In addition, we must update the input structure of the vertex shader and the input structure of the pixel shader to allow the use of map coordinates. The vertex shader obtains the mapping coordinates from the vertex cache block and passes them directly to the pixel shader, allowing the pixel shader to access them.

struct Vs_input
{
    float4 pos:position;
    Float2 tex0:texcoord0;
};

Some of the common semantics include:
sv_position--the FLOAT4 value of a specific transformation position
normal0--defines a normal vector
color0--defines a color value
There are other semantics found in the full list of the HLSL chapters of the DirectX SDK documentation. A large number of semantic endings follow a number because it is possible to define multiple such semantic types. 9. Register

Texture2d colormap_: Register (t0);
Samplerstate colorsampler_: Register (S0);

Object COLORMAP_ is a texture2d type because it is used for 2D textures, and colorsampler_ is a type of HLSL advanced coloring language Samplerstate. In order to bind these objects in the shader input in the render function we provide, we must register the keyword register with HLSL. In order to bind the first input map we use T0, where he represents the texture type, and 0 indicates that the first index map is used. For the same reason for using S0 for State objects. Because we use function pssetsamplers and Pssetshaderresource to pass an array element to our shader, we must bind the data index we use to every HLSL variable. Because we only have one map and one sample state, we just need to use T0 and S0.
It is important to note that these registered caches must conform to what we specified in the Render function Render to register the cache correctly with the HLSL object. 10. Detail issues

You will find that the foregoing is too sketchy and there are many questions that are not described, but these are relatively minor details. For example, what are the reserved keywords in HLSL, the scope of the variable, the details of the data type, the rule of use for the quaternion component, and so on, these are in the Direct3D document-> DirectX Graphics-> programming Guide-> the Programmable Pipeline-> programmable HLSL shaders-> HLSL Language to speak more clearly than I do, I also no longer redundant translation.

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.