Getting Started with GPU programming (i) _ Programming

Source: Internet
Author: User

This series of articles Al by I am a dog ~ ~

I remember living history, learning some history is useful, can increase interest at least ...

GPU is a graphics processor, with the development of hardware more and more quickly, GPU processing power is not the same, now the GPU can be very complex data processing, and have some CPU different processing characteristics. The GPU language is a language used to control GPU processing ... There are basically two kinds, one is Nvidia CG (c for Graphics Graphics C language) the other is Microsoft's HLSL (High level shader language advanced coloring language God knows if this is the way to spell ...) In fact, these two are the same language, two companies jointly developed, respectively. I wrote this tutorial using HLSL nothing else, I learned this ... Ha ha...

Simply said that I know the history, the following is the subject. Most of the HLSL programs are composed of two parts (how to say, it is, but you have to write one, of course, is also set up, but for complex programs, one is obviously not enough), one is the vertex program (vertex shader) Another pixel program (Pixel shader), The two programs, by name, are known to replace the vertex processing (vertex processing) and pixel processing (pixel processing) in the CPU rendering pipeline respectively, allowing the CPU to release more cycles to handle the complex AI What, what? CPU clock cycles are valuable. Learn about the history and introduction of the game only so much to say, ...

Vertex program
Global variables
Vector color;

Input structure
struct Vs_input ... {

Vertor position:P osition;}

Output structure
struct Vs_output ... {

Vertor position:position;

Vector Color:color;}

Main function
Vs_output Main (vs_input) ... {

Vs_output out;

Out.position=vs_input.posintion;

Out.color=color;

return out;}

Look at the example above, HLSL grammar and C syntax is very similar, all have the basis of C for learning HLSL is very helpful

I would like to give an example of the first to tell me how to run, or to see half a day of boring to death I would have thrown away the book, so I first tell you how to run a HLSL vertex program.

The vertex program is saved as a. txt file. For example, a. Tet, create a simple DirectX program, and then call the D3dxcompileshaderfromfile function from the beginning. Tet to compile a vertex shader in the following code:

Idirect3dvertexshader9 * Firstshader = 0; Vertex shader pointer

A list of constants that are used to assign values to global variables

id3dxcostanttable * firstconsttable = 0;

D3dxhandle colorhandle = 0; What do you think this is?


ID3DXBuffer * shaderbuffer = 0;//create a buffer, the compiled shader saved here

Another buffer is used to save the error message, if any

ID3DXBuffer * Errorbuffer = 0;

Compiling shader code

D3dxcompileshaderfromfile ("First.tex",//Vertex program file path and filename

0,

0.

"Main",//Entry function name

"Vs_3_1"//Shader version number

D3dxshader_debug,//compile with DEBUG mode

& Shaderbuffer,//pointing to the compiled pointer

& Errorbuffer,

& firstconsttable//constant list);

Next, create an electric-adjustable shader

g_pdevice-> Creatvertexshader ((DWORD *) Shaderbuffer-> gerbufferpointer, & Firstshader);

/**//* The next step is to assign values to global variables in the vertex program, and the shader's global variables are compiled and placed in a structure called a list of constants, that is, the consttable, the last parameter of the compiled function D3dxcompileshaderfromfile, The specific assignment process is to get the handle to the shader through the variable name (handle), through the SET function and handle for the variable assignment value, such as to set the color here to use Setvertor, if the matrix that nature is SetMatrix function slightly, the specific process is as follows: * *

D3dvevtor4 color = (1.0f, 1.0f, 0.0f, 1.0f); First define a Vertor value

Colorhandle = frestconsttable-> getconstantbyname (0,//Father pointer indicates that if it is the highest level there is no parent, that is 0

"COLOR"//variable name);

It's a good habit to set each variable to the default value first.

Firstconsttable-> setdefaults (g_pdevice);

Then set the value

Firstconsttable-> setvertor (g_pdevice,colorhandle, & color);

Well, add this code to your render code. Here, a vertex program is finished.

In fact, this vertex program has nothing to do, the only thing is to set a little color, but for the basis of learning, enough ...

Perhaps you have to say, that has not set the value of position, no, this value does not need to be set, but by the program from the vertex to be processed, which is the vertex program is a necessary parameter, that must have position, the truth is self-evident
Well, it took me one hours ah, write this point, is to experience the pain of writing people ....

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.