GPU programming in OpenGL

Source: Internet
Author: User
GPU programming in OpenGL (1 )--

This section describes how to use the arb_vertex_program extension to program the GPU. To use the vp1.0 assembly language, opengl1.4 or a later version is required. Of course, arb_vertex_program extension must be supported.
I am so excited that I have studied it for N days. I hope this article will help beginners.
Opengl1.4 supports vertex Program (called vertex in dx) in the form of ARB extension.
Shader is actually the same thing.) I am too lazy to talk about the concept of a programmable graphic pipeline. If I don't know how to Google it, there are many, and many things (omitted below, still not clear
Send me mail!
To use the arb_vertex_program extension, first check whether the current OpenGL implementation supports the extension.
(Gl_extensions) Check the retrieved string for gl_vertex_program_arb. Cut! Forget to use
The arb_vertex_program extension must first work hard to declare and obtain some extended functions. Because the original OpenGL functions are basically not fully supported
Vertex Program (know which standard OpenGL command can load Vertex
Program? I don't know? I do not know ^), so while introducing the arb_vertex_program extension, I also introduced a number of related extension functions (these functions are also
Can be used for arb_fragment_program ). First download the glext. h file, which declares the constants and function types of the ARB extension, and then declare and bind
Function. I am too tired to explain the concept. Here is an example:

Pfnglprogramstringarbproc glprogramstringarb = NULL;
Glprogramstringarb = (pfnglprogramstringarbproc) wglgetprocaddress ("glprogramstringarb ");

Glext in upper case. h has been defined in it. I believe smart people have already seen that the types of these extended functions have grown into pfngl *** proc, and the middle is the upper case of the specific function name.
The hard work naturally does not need to be repeated. I have written the statements about multi texture and vertex program extensions, and can use them after downloading them to the project. Attachment renderer.rar: http://blog.blogchina.com/upload/2005-03-15/2005031516113243510.rar

The creation and usage of vertex program are very similar to those of creating and using textures:
1. Use the glgenprogramsarb (sziei N, uint * IDs) function to generate a programid, similar to texture name;
2. Use glbindprogramarb (Enum target, uint ID) to switch the current vertex program. Here, the target must be gl_vertex_program_arb, And the ID is the ID generated just now;
3. Use glprogramstringarb (Enum target, Enum format, sizei Len, const ubyte
* Program) convert Vertex
Load and compile program. Target is the same as above. format can use gl_program_format_ascii_arb to indicate that the program array is
ASCII code program. Len is the length of the program string. The program string is a string ending with null, that is, vertex.
The program source program.
The following is a specific example (actually copied)

Gluint progid;
Glgenprogramsarb (1, & progid );
Glbindprogramarb (gl_vertex_program_arb, progid );
Glprogramstringarb (gl_vertex_program_arb, gl_program_format_acsii_arb, strlen (mystring), mysring );

In this way, the program is loaded and compiled. Don't be too happy. What should I do if the program is wrong? OpenGL won't be dumbly executed. We have to determine whether the program has any errors:

If (gl_invalid_operation = glgeterror ())
{
Glint errpos; // location where an error occurs (index in the string)
Glgetintegerv (gl_program_error_position_arb, & errpos );
 
// Obtain the specific program error information, which is the same as that returned by the General Compiler: ** A ** error occurs on the line.
Const glubyte * errstring = glgetstring (gl_program_error_string_arb );
Fprintf (stderr, "error at position: % d/n % s/n", errpos, errstring );
}

The last thing to do is to delete vertex program.
Gldeleteprogramsarb (sizei N, gluint * IDs );

To use vertex program, first use glable (gl_vertex_program_arb
) To activate the programmable vertex rendering pipeline, and then use glbindprogramarb (gl_vertex_program_arb, progid
) Switch the current vertex program.
I have completed the basic framework. Next I will introduce some of the knowledge required to write vertex Program (which is so long that I can't stand it. Below we will use VP instead-except the source code.
The ARB vertex processor has six register groups (all registers are X, Y, Z, and W4 tuples), which are:
1. Vertex
Attributes registers (vertex and result ). Use vertex. * In VP to access all attributes of the current vertex. You can use
Glvertexattrib * farb and other functions are specified. In fact, the usual glvertex * F, glcolor, glnormal and other functions can all change vertex attributes.
Note: Non-floating point data is automatically mapped to [-] or []. The vertex attribute register group is as follows:

Property Register Components Content
Vertex. Position {X, Y, Z, w} Vertex position information
Vertex. Weight {W, w} Vertex Weight Information (skeleton animation)
Vertex. weight [N] {W, w} Vertex N Weight Information
Vertex. Normal {X, Y, Z, 1} Vertex normal
Vertex. Color {R, G, B,} Vertex master color
Vertex. color. Primary {R, G, B,} Vertex master color
Vertex. color. Secondary {R, G, B,} Vertex color
Vertex. fogcoord {F, 0, 0, 1} Vertex fog texture coordinate
Vertex. texcoord {S, T, R, Q} Vertex texture coordinates
Vertex. texcoord [N] {S, T, R, Q} Texture coordinates of the nth texture of a vertex
Vertex. matrixindex {I, I} Vertex matrix index (skeleton animation)
Vertex. matrixindex [N] {I, I} Nth matrix index of vertex
Vertex. attrib [N] {X, Y, Z, w} Vertex N attributes

The result register group used for output also has the same composition and will not be pasted again.
2. Address Register ). The address memory only contains the X element for addressing. It must be declared before use in VP, as follows:

Address Areg;
Address A0;
Address A1, Areg;

3. program environment parameters (program environment
Parameters ). There are at least 96 registers, which are accessed using program. env [N] In VP. This register is the only register group that can be shared by different vertices (unknown
Whether the channel can be Fragment
Program sharing), a total of N groups. You can use glprogramenvparameter4farb (
Gl_vertex_program_arb, index, X, Y, Z, W) and other functions set their values.
4. Program local parameters (Program local
Parameters ). There are at least 96 registers for storing the parameters required by the program. Used in OpenGL
Glprogramlocalparameter4farb (gl_vertex_program_arb, index,
X, Y, Z, W) and other function settings. Use Program. Local [I] Access in VP.
5. Temporary register (temporary variables ). Use the "Temp variable name;" declaration in VP, which is the only read/write register group in VP.

VP syntax.
Identifier and variable: any sequence contains one or more letters (A-Z, A-z), numbers (0-9), underscores, and $. The first character cannot be a number.
Constant: it consists of integers, decimal points, and decimal places. You can also describe it in exponential mode. For example, 1, 4.3, and 4e3.
Param parameter: a read-only constant. It is declared using the param keyword. A value must be specified during declaration. For example:

Param A = {0, 0, 0 };
Param B [2] = {1, 1, 1}, {2, 2 }}

There are also some omitted writing methods, so I am too lazy to say.
Temp variable: it is the only readable and writable variable in the program. It is declared using the temp keyword. The initial value cannot be specified during declaration. For example:

Temp;

Output variable: Write-only output variable type. When using the output keyword declaration, you must specify the name of the variable to be bound. In fact, the output variable does not occupy any registers. It only references the variable address specified during declaration. For example:

Output opos = result. position;

VP also has a very important State Register state, which contains some state parameters specified in OpenGL. Such as material, lighting, matrix and other parameters. If you are interested, you can go to the relevant documents and check the information.
I'm off duty! Close!
(Supplemented at home) the final example of vertex program is very simple. It is just an MVP transformation with no light.

!! Arbvp1.0
Attrib IPOs = vertex. position;
Attrib itexcoord = vertex. texcoord;
Param MVP [4] = {state. matrix. MVP };
Param diffusecol = {0.6, 0.6, 0.6, 1 };
Output opos = result. position;
Output ocolor = result. color;
Output otexcoord = result. texcoord;

MoV ocolor. XYZ, diffusecol;
MoV otexcoord, itexcoord;
Dp4 opos. X, MVP [0], IPOs;
Dp4 opos. Y, MVP [1], IPOs;
Dp4 opos. Z, MVP [2], IPOs;
Dp4 opos. W, MVP [3], IPOs;
End

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.