Shader history-Data Transmission

Source: Internet
Author: User
"Continue the previous article, and take the book back ".... Too many games in the western region. ^_^
As part of a graphic application, the shader defines the graphic rendering commands of this program. But to let the system render the image according to our design, we also need to pass the necessary data to the shader. The shader is executed by the GPU. How can we transmit data to the shader as required by the GPU?
First, let's take a rough look at the data types in the shader. Because the processors are different, shader also has its own data types.
Scalar data types include float, Int, and bool;
Vector data types include vec2, vec3, and vec4, I .e., 2, 3, and 4-dimensional vectors. The component can be any of the three scalar data types), and then the matrix, there are also 2, 3, and 4 dimensions;
There are 6 types of sampler1d, sampler2d, sampler3d, samplercube, sampler1dshadow and sampler2dshadow. They are used for texture sampling;
In addition, there are "struct" and "array" similar to C/C ++.
The data in the shader can be divided into three types:
1. attribute type. This type of data is the fastest changing frequency in the shader. It is transmitted by the application to the vertex shader and is specified for each vertex. It generally describes the attributes of the vertex, such as coordinates, colors, and normal. In OpenGL, commands such as glvertex and glnormal send such data to vertex shader.
2. Uniform type. This data changes at a relatively low frequency. Generally, the global rendering attribute belongs to this type of data, such as the parameters of the light source and the position of the viewpoint. They are the same for all vertices.
3. Varying type. This type is used to transmit data from vertex shader to pixel shader. The calculation results of vertex shader flow to pixel shader through them.
In addition, glsl has some built-in uniform and constant type data, which are headers with GL _, such as uniform mat4 gl_modelviewmatrix and const int gl_maxlights = 8. Note the naming conflict issue when defining your own variables.
The above is a rough introduction to the Data Types in glsl. For details, refer to OpenGL shading language, second edition. If you are interested, you can ask for it from me. Next we will go to today's core topic: data transmission.
For the attribute of the 1st data types, OpenGL uses conventional vertex setting commands to transmit relevant data, such as the glvertex and glnormal commands we mentioned earlier. For example, after the coordinates of the vertex are specified, the value of the built-in data variable gl_vertex (attribute vec4 gl_vertex;) is changed to the value specified by the glvertex command, then this value can be used by the code in vertex shader. However, this is only limited to the built-in data of the system. For a programmable graphic assembly line, it is not enough to create exquisite and complex rendering effects, generally, we need to input some data with other meanings to the shader, which will be stored in the space reserved by OpenGL. Let's take a look at the following commands.
Command void glvertexattrib {1 | 2 | 3 | 4} {S | f | D} (gluint index, Type V) can be used to specify custom attribute data. This is the same as other vertex attribute commands in OpenGL. The index specifies the location where the data is stored (we can think so). vertex shader can read data from these locations. In addition, there are some similar commands that provide data transmission for array type and unitization (that is, the absolute value cannot exceed 1.0), as follows:
Void glvertexattrib {1 | 2 | 3} {S | f | D} V (gluint index, const type * V) [array type]
Void glvertexattrib4 {B | S | I | f | d | UB | us | UI} V (gluint index, const type * V) [array type]
Void glvertexattrib4nub (gluint index, Type V) [unitization]
Void glvertexattrib4n {B | S | I | f | d | UB | us | UI} V (gluint index, const type * V) [unitization]
A more common command is
Void glvertexattribpointer (gluint index,
Glint size,
Glenum type,
Glboolean normalized,
Glsizei stride,
Const glvoid * pointer)
This command requires the following commands for use.
Void glenablevertexattribarray (gluint index) [open the vertex attribute array]
Void gldisablevertexattribarray (gluint index) [disable the vertex attribute array]
In fact, they are used in the same way as opening and closing the OpenGL client status.
Okay. Now we have completed half of the work. After starting the drawing command (such as the gldawxxxx series), the application will pass the data to the shader. The remaining half of the work is how the shader accesses the incoming data.
The first solution is that all work is automatically completed by the glsl linker, and then the data to be accessed is queried from OpenGL. [It is rarely used. It is not described in detail in the book. It is skipped here...].
The second solution is binding, that is, the application explicitly indicates which indexes (storage locations) are bound to which variables (names). In this way, directly reference the variable name in the shader, as shown below:
Void glbindattriblocation (gluint program, gluint index, const glchar * name)
This command binds the index (storage location) in a shader Program (Program) to the variable name. In this way, the program stores the data to the location specified by the index. After binding, the shader can access the data by name, which is the same as that of common variables. Correspondingly, there is also a query command used to query the index location of the storage zone corresponding to a variable in the shader:
Glint glgetattriblocation (gluint program, const glchar * name)
The BIND command can bind a location to multiple variables, that is, the alias mechanism, just like creating a reference in C ++. However, a variable cannot correspond to multiple index locations, this is easy to understand. At the same time, not all bindings take effect. If a variable is not used in the shader and the binding command is used, the variable is in the inactive state, the glsl variable will be optimized during processing. Instructions
Void glgetactiveattrib (gluint program,
Gluint index,
Glsizei bufsize,
Glsizei * length,
Glint * size,
Glenum * type,
Glchar * name)
You can query the activation status of a variable. This is an attribute-type data transmission method.
The uniform data is not "Bound", but is set by querying the index location corresponding to the corresponding variable. The usage is similar to that of attribute and corresponds to the following group of commands:
Void gluniform {1 | 2 | 3 | 4} {f | I} (glint location, type V) [specify common variables]
Void gluniform {1 | 2 | 3 | 4} {f | I} V (glint location, gluint count, const Type V) [specify an array variable]
Void gluniformmatrix {2 | 3 | 4} FV (glint location, gluint count, glboolean transpose, const glfloat * V) [Specifying matrix variables]
Glint glgetuniformlocation (gluint program, const glchar * Name) [query]
Void glgetuniformfv (gluint program, glint location, glfloat * Params) [query array]
Void glgetuniformiv (gluint program, glint location, glint * Params) [query an array]
Void glgetactiveuniform (gluint program,
Gluint index,
Glsizei bufsize,
Glsizei * length,
Glint * size,
Glenum * type,
Glchar * Name) [query activated variables]
Note that, unlike the attribute type, uniform can define struct variables, while struct variables cannot be directly queried and can only be queried for its members. For example, if a is a struct, glgetuniformlocation (Program, "A") is invalid, while glgetuniformlocation (Program, ". B ") is valid (B is a member variable of ). The other types are basically the same as those of attribute.
For the sampler type, there are dedicated commands: gluniform1i and gluniform1iv. Actually, the corresponding Texture unit number is passed to the shader.
The above is the main content about data transmission. There are some special cases that are rarely used. We will discuss them with you later. Now, I want to continue learning. I will share my learning achievements with you later. ^_^

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.