OpenGL ES's uniform and varying

old

Meng

One

There attitude

Programmers

uniform

variables is GLSL uniform type qualifiers, uniform variables defined value is read-only, can not be changed in the Shader, can only pass through the application to the uniform.

uniform variable global shared variables, can be acquired in all Shader, uniform defined as follows:

uniform float uTexPos;

uniform variables usually the GPU "constant region" is stored, the memory area is limited, therefore there is the number of uniform limits, but for a lot more than attribute, OpenGL predetermined OpenGL ES 2.0 ISO predetermined uniform support at least 128 vertices 16 and fragment (fragment) uniform.

Get support the maximum number of vertex uniform code is as follows:

    fun getMaxVertexUniformNum(): Int {
        var count = IntArray(1)
        GLES20.glGetIntegerv(GLES20.GL_MAX_VERTEX_UNIFORM_VECTORS, count, 0)
        return count[0]
    }

Obtaining uniform maximum number of segments supported code is as follows:

    fun getMaxFragmentUniformNum(): Int {
        var count = IntArray(1)
        GLES20.glGetIntegerv(GLES20.GL_MAX_FRAGMENT_UNIFORM_VECTORS, count, 0)
        return count[0]
    }

Setting uniform variable values

Obtaining uniform property handler code as follows:

GLES20.glGetUniformLocation(programHandle, uniformName)

programHandle on behalf of program handles, uniformName is the name of uniform properties.

The method set values ​​are more uniform in Android, as follows:

GLES20.glUniform1f(location,x)
GLES20.glUniform2f(location,x,y)
GLES20.glUniform3f(location,x,y,z)
GLES20.glUniform4f(location,x,y,z,w)

Set float type data, location represents the handle uniform properties, followed by x, y, z, w is the value, the parameter (x) / (x, y) / (x, y, z) / (x, y, z , w) corresponding vec2 / vec3 / vec4 type uniform.

Provided vec types of variables may be used as follows:

GLES20.glUniform2fv()

Set int type data using the following method:

GLES20.glUniform1i(location,x)

Provided a method of using the type of mat:

GLES20.glUniformMatrix2fv()

varying

GLSL qualifier is varying, varying the variables defined only transferred between Shader, is Vertex Shader (vertex shader) output, Fragment Shader (fragment shader) input, Shader type declarations and consistency.

varying format is defined as follows:

varying float color;

Varying variables defined in the Vertex Shader and set vec4 (1,0,0,1), code is as follows:

attribute vec4 vPosition;
varying vec4 color;
void main() {
    color= vec4(1,0,0,1);
    gl_Position = vPosition;
}

Fragment Shader acquires color values ​​using the following code:

precision mediump float;
varying vec4 color;
void main()
{
  gl_FragColor = color;
}

Like the attribute and Uniform, there are varying quantity, OpenGL ES 2.0 supports at least 8, obtaining the maximum number varying code is as follows:

fun getMaxSupportNum(): Int {
        var count = IntArray(1)
        GLES20.glGetIntegerv(GLES20.GL_MAX_VARYING_VECTORS, count, 0)
        return count[0]
    }

varying only limited number, as well as size limitations, varying variable can pass data float up to 32, or 8 vec4 or 2 mat4 matrix.

Read More:

 

Published 123 original articles · won praise 68 · Views 300,000 +

Guess you like

Origin blog.csdn.net/mengks1987/article/details/104075157