OpenGL ES on iOS --- Unified variable (Uniform) and unified block of variables (UBO)

Brief introduction

Uniform is a way of transmitting data from the CPU to the GPU application shaders and vertex uniform properties but somewhat different.

First, uniform is global in (Global). Global variables must mean that uniform is unique in each shader program objects, and it can be arbitrary shader shader programs access at any stage.
Second, whatever you put into what the value is set uniform, uniform will always save their data until they are reset or updated.

Single unified variable

statement

uniform vec3 color

Assignment

//获取指定统一变量的location
int vertexColorLocation = glGetUniformLocation(_program, "test");

//为vec3 的统一变量 赋值
glUniform3f(vertexColorLocation, 0.0f, 1.0f, 0.0f);

Uniform assignment function according to different types of variables adjusted for which the function names contain numbers (1,2,3,4) for the acceptance of this figure a uniform variable changes value, i denotes 32-bit integer, f denotes 32 -bit floating point, ub an 8-bit unsigned byte, ui represents a 32-bit unsigned integer, v acceptance of a pointer corresponding to the type (or is passed in an array).

glUniform1f(GLint locaation,GLFloat x)It represents a float type
glUniform2f(GLint locaation,GLFloat x,GLFloat y)represented by two component vectors of type float
glUniform1fv(GLint locaation,GLSize count,const GLfloat* value)float type pointer
glUniformMatrix4x3(Glint location,GLsizei count,GLboolean transpose,const GLfloat* value)represented as a 4x3 matrix parameters:. Transpose indicates whether to use row major order (GL_TRUE)

UBO

UBO (Uniform Buffer Object) is used to store the language Uniform coloring type variable buffer object , allowing the use of uniform variable UBO implemented in different colored common language program, may be provided to achieve uniform coloration in the type of a variable language program and updates.

UBO mentioned it is necessary to mention Uniform Blocks of GLSL shading language, it will be many types of variables Uniform together unified management, for programs that require a large number Uniform variable's type can significantly improve performance. (A bit like a global version of the VBO)

principle

Schematic .png

Objects created in the memory cache (Buffer), unified storage variable data in the buffer, the Buffer specified point binding, binding buffer index and the point will be unified variable. By this point the connection variables and cache.

Set UBO

//统一变量块
layout (std140) uniform colorBlock{
    vec4 cc;
};
        GLuint blockid,bufferid;
        GLint blocksize;
        GLint point = 1;
        
        // 统一变量数据
        GLfloat blockData[] = {
            1.0f,1.0f,1.0f,1.0f
        };

        //获取统一变量块索引
        blockid = glGetUniformBlockIndex(_program, "colorBlock");
        
        //获取统一变量块大小
        glGetActiveUniformBlockiv(_program, blockid, GL_UNIFORM_BLOCK_DATA_SIZE, &blocksize);
        
        //将变量索引 和 point 绑定
        glUniformBlockBinding(_program, blockid, point);

        //创建与绑定缓冲区
        glGenBuffers(1, &bufferid);
        glBindBuffer(GL_UNIFORM_BUFFER, bufferid);

        //向缓冲区中赋值
        glBufferData(GL_UNIFORM_BUFFER, blocksize, blockData, GL_DYNAMIC_DRAW);
        
        //将UBO 和 point 绑定
        glBindBufferBase(GL_UNIFORM_BUFFER, point, bufferid);

Modify UBO

        GLfloat uploadData[] = {
            0.0f,0.0f,1.0f,1.0f
        };

        // 绑定当然统一变量块的 buffer
        glBindBuffer(GL_UNIFORM_BUFFER, bufferid);
        
        //获取统一变量块 中 指定变量的 偏移量
        const GLchar *names[] = {"cc"};
        GLuint indices[1];
        glGetUniformIndices(_program, 1, names, indices);
        GLint offset[1];
        glGetActiveUniformsiv(_program, 1, indices, GL_UNIFORM_OFFSET, offset);
        glBufferSubData(GL_UNIFORM_BUFFER, offset[0], blocksize, uploadData);

Function complements

1, glGetUniformIndices (GLuint program, GLsizei uniformCount, const GLchar* const *uniformNames, GLuint* uniformIndices)
the function is used to obtain a unified block of variables in the variable's index parameters:

program program object
number of elements in the array variable name uniformCount
uniformNames variable name of the array
uniformIndices for receiving variable indexed arrays

2, glGetActiveUniformsiv (GLuint program, GLsizei uniformCount, const GLuint* uniformIndices, GLenum pname, GLint* params)
the function information for the variable parameters by the index query variables:

program object program
number uniformCount variable
uniformIndices indexed array variable
pname indicates that property to query GL_UNIFORM_OFFSET variable (variable offset) GL_UNIFORM_SIZE (variable size) GL_UNIFORM_NAME_LENGTH (variable name length) GL_UNIFORM_TYPE (variable type) ....
the params receiving query results array

note

Shading Language compiler optimization

If you declare a uniform but not used in GLSL code, the compiler will silently remove this variable, leading to compile the final version and does not contain it.

UBO qualifier

In UBO, the qualifiers for different circumstances there are different byte padded, so acquiring an offset, size is best carried out by a query, to avoid their own calculation error.

This is a memory allocation std140 (unified standard variable block layout) qualifier:

std140 layout .png

Guess you like

Origin www.cnblogs.com/dongguolei/p/11495301.html