Vertex Shader Built-In Variables
1.Built-In Special Variables
gl_VertexID
gl_InstanceID
gl_Position
gl_PointSize
gl_FrontFacing
2.Built-In Uniform State
gl_DepthRange :
struct gl_DepthRangeParameters
{
highp float near; // near Z
highp float far; // far Z
highp float diff; // far – near
}
uniform gl_DepthRangeParameters gl_DepthRange;
3.Built-In Constants
const mediump int gl_MaxVertexAttribs = 16;
const mediump int gl_MaxVertexUniformVectors = 256;
const mediump int gl_MaxVertexOutputVectors = 16;
const mediump int gl_MaxVertexTextureImageUnits = 16;
const mediump int gl_MaxCombinedTextureImageUnits = 32;
查询:
glGetIntegerv ( GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs );
glGetIntegerv ( GL_MAX_VERTEX_UNIFORM_VECTORS, &maxVertexUniforms );
glGetIntegerv ( GL_MAX_VARYING_VECTORS, &maxVaryings );
glGetIntegerv ( GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxVertexTextureUnits );
glGetIntegerv ( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTextureUnits );
Precision Qualifiers
三种精度:
(1)lowp
(2)mediump
(3)highp
定义默认精度,如:
precision highp float;
precision mediump int;
如果没有定义默认精度,则 int 和 float 的默认精度都是 highp
Number of Uniforms Limitations in a Vertex Shader
gl_MaxVertexUniformVectors
一般来说常数是参与计数的,所以下面两段同样逻辑的vertex shader,后者优于前者 :
《《《《《《《《《《《《《《《《《《《《《《《《
#version 300 es
#define NUM_TEXTURES 2
uniform mat4 tex_matrix[NUM_TEXTURES];
uniform bool enable_tex[NUM_TEXTURES];
uniform bool enable_tex_matrix[NUM_TEXTURES];
in vec4 a_texcoord0;
in vec4 a_texcoord1;
out vec4 v_texcoord[NUM_TEXTURES];
void main {
v_texcoord[0] = vec4(0.0, 0.0, 0.0, 0.0);
if(enable_tex[0]) {
if(enable_tex_matrix[0]) {
v_texcoord[0] = tex_matrix[0] * a_texcoord0;
} else {
v_texcoord[0] = a_texcoord0;
}
}
v_texcoord[1] = vec4(0.0, 0.0, 0.0, 0.0);
if(enable_tex[1]) {
if(enable_tex_matrix[1]) {
v_texcoord[1] = tex_matrix[1] * a_texcoord1;
} else {
v_texcoord[1] = a_texcoord1;
}
}
// set gl_Position
}
《《《《《《《《《《《《《《《《《《《《《《《《
《《《《《《《《《《《《《《《《《《《《《《《《
#version 300 es
#define NUM_TEXTURES 2
const int c_zero = 0;
const int c_one = 1;
uniform mat4 tex_matrix[NUM_TEXTURES];
uniform bool enable_tex[NUM_TEXTURES];
uniform bool enable_tex_matrix[NUM_TEXTURES];
in vec4 a_texcoord0;
in vec4 a_texcoordl;
out vec4 v_texcoord[NUM_TEXTURES];
void main() {
v_texcoord[c_zero] = vec4 (float(c_zero), float(c_zero), float(c_zero), float(c_one));
if(enable_tex[c_zero]) {
if ( enable_tex_matrix[c_zero] ) {
v_texcoord[c_zero] = tex_matrix[c_zero] * a_texcoord0;
} else {
v_texcoord[c_zero] = a_texcoord0;
}
}
v_texcoord[c_one] = vec4(float(c_zero), float(c_zero), float(c_zero), float(c_one));
i