0
点赞
收藏
分享

微信扫一扫

openGL/openGLES glGetActiveUniform函数详解


openGL官网文档

​​openGLES官网​​

Name

glGetActiveUniform — Returns information about an active uniform variable for the specified program object

C Specification

​void glGetActiveUniform(​

GLuint program,

GLuint index,

GLsizei bufSize,

GLsizei *length,

GLint *size,

GLenum *type,

GLchar *name​​)​​;

Parameters

program

Specifies the program object to be queried.

index

Specifies the index of the uniform variable to be queried.

bufSize

Specifies the maximum number of characters OpenGL is allowed to write in the character buffer indicated by ​name​.

length

Returns the number of characters actually written by OpenGL in the string indicated by ​name​​ (excluding the null terminator) if a value other than ​​NULL​​ is passed.

size

Returns the size of the uniform variable.

type

Returns the data type of the uniform variable.

name

Returns a null terminated string containing the name of the uniform variable.

Description

​glGetActiveUniform​​​ returns information about an active uniform variable in the program object specified by ​program​​. The number of active uniform variables can be obtained by calling ​​glGetProgramiv​​​ with the value ​​GL_ACTIVE_UNIFORMS​​​. A value of zero for ​index​​ selects the first active uniform variable. Permissible values for ​index

Shaders may use either built-in uniform variables, user-defined uniform variables, or both. Built-in uniform variables have a prefix of "gl_" and reference existing OpenGL state or values derived from such state (e.g., ​gl_DepthRange​​, see the OpenGL Shading Language specification for a complete list.) User-defined uniform variables have arbitrary names and obtain their values from the application through calls to ​​glUniform​​​. A uniform variable (either built-in or user-defined) is considered active if it is determined during the link operation that it may be accessed during program execution. Therefore, ​program​​ should have previously been the target of a call to ​​glLinkProgram​​, but it is not necessary for it to have been linked successfully.

The size of the character buffer required to store the longest uniform variable name in ​program​​ can be obtained by calling ​​glGetProgramiv​​​ with the value ​​GL_ACTIVE_UNIFORM_MAX_LENGTH​​​. This value should be used to allocate a buffer of sufficient size to store the returned uniform variable name. The size of this character buffer is passed in ​bufSize​​, and a pointer to this character buffer is passed in ​name.

​glGetActiveUniform​​​ returns the name of the uniform variable indicated by ​index​​, storing it in the character buffer specified by ​name​​. The string returned will be null terminated. The actual number of characters written into this buffer is returned in ​length​​, and this count does not include the null termination character. If the length of the returned string is not required, a value of ​​NULL​​​ can be passed in the ​length

The ​type

Returned Symbolic Contant

Shader Uniform Type

​GL_FLOAT​

​float​

​GL_FLOAT_VEC2​

​vec2​

​GL_FLOAT_VEC3​

​vec3​

​GL_FLOAT_VEC4​

​vec4​

​GL_INT​

​int​

​GL_INT_VEC2​

​ivec2​

​GL_INT_VEC3​

​ivec3​

​GL_INT_VEC4​

​ivec4​

​GL_UNSIGNED_INT​

​unsigned int​

​GL_UNSIGNED_INT_VEC2​

​uvec2​

​GL_UNSIGNED_INT_VEC3​

​uvec3​

​GL_UNSIGNED_INT_VEC4​

​uvec4​

​GL_BOOL​

​bool​

​GL_BOOL_VEC2​

​bvec2​

​GL_BOOL_VEC3​

​bvec3​

​GL_BOOL_VEC4​

​bvec4​

​GL_FLOAT_MAT2​

​mat2​

​GL_FLOAT_MAT3​

​mat3​

​GL_FLOAT_MAT4​

​mat4​

​GL_FLOAT_MAT2x3​

​mat2x3​

​GL_FLOAT_MAT2x4​

​mat2x4​

​GL_FLOAT_MAT3x2​

​mat3x2​

​GL_FLOAT_MAT3x4​

​mat3x4​

​GL_FLOAT_MAT4x2​

​mat4x2​

​GL_FLOAT_MAT4x3​

​mat4x3​

​GL_SAMPLER_2D​

​sampler2D​

​GL_SAMPLER_3D​

​sampler3D​

​GL_SAMPLER_CUBE​

​samplerCube​

​GL_SAMPLER_2D_SHADOW​

​sampler2DShadow​

​GL_SAMPLER_2D_ARRAY​

​sampler2DArray​

​GL_SAMPLER_2D_ARRAY_SHADOW​

​sampler2DArrayShadow​

​GL_SAMPLER_2D_MULTISAMPLE​

​sampler2DMS​

​GL_SAMPLER_2D_MULTISAMPLE_ARRAY​

​sampler2DMSArray​

​GL_SAMPLER_CUBE_SHADOW​

​samplerCubeShadow​

​GL_SAMPLER_CUBE_MAP_ARRAY​

​samplerCubeArray​

​GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW​

​samplerCubeArrayShadow​

​GL_SAMPLER_BUFFER​

​samplerBuffer​

​GL_INT_SAMPLER_2D​

​isampler2D​

​GL_INT_SAMPLER_3D​

​isampler3D​

​GL_INT_SAMPLER_CUBE​

​isamplerCube​

​GL_INT_SAMPLER_2D_ARRAY​

​isampler2DArray​

​GL_INT_SAMPLER_2D_MULTISAMPLE​

​isampler2DMS​

​GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY​

​isampler2DMSArray​

​GL_INT_SAMPLER_CUBE_MAP_ARRAY​

​isamplerCubeArray​

​GL_INT_SAMPLER_BUFFER​

​isamplerBuffer​

​GL_UNSIGNED_INT_SAMPLER_2D​

​usampler2D​

​GL_UNSIGNED_INT_SAMPLER_3D​

​usampler3D​

​GL_UNSIGNED_INT_SAMPLER_CUBE​

​usamplerCube​

​GL_UNSIGNED_INT_SAMPLER_2D_ARRAY​

​usampler2DArray​

​GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE​

​usampler2DMS​

​GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY​

​usampler2DMSArray​

​GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY​

​usamplerCubeArray​

​GL_UNSIGNED_INT_SAMPLER_BUFFER​

​usamplerBuffer​

​GL_IMAGE_2D​

​image2D​

​GL_IMAGE_3D​

​image3D​

​GL_IMAGE_CUBE​

​imageCube​

​GL_IMAGE_2D_ARRAY​

​image2DArray​

​GL_IMAGE_CUBE_MAP_ARRAY​

​imageCubeArray​

​GL_IMAGE_BUFFER​

​imageBuffer​

​GL_INT_IMAGE_2D​

​iimage2D​

​GL_INT_IMAGE_3D​

​iimage3D​

​GL_INT_IMAGE_CUBE​

​iimageCube​

​GL_INT_IMAGE_2D_ARRAY​

​iimage2DArray​

​GL_INT_IMAGE_CUBE_MAP_ARRAY​

​iimageCubeArray​

​GL_INT_IMAGE_BUFFER​

​iimageBuffer​

​GL_UNSIGNED_INT_IMAGE_2D​

​uimage2D​

​GL_UNSIGNED_INT_IMAGE_3D​

​uimage3D​

​GL_UNSIGNED_INT_IMAGE_CUBE​

​uimageCube​

​GL_UNSIGNED_INT_IMAGE_2D_ARRAY​

​uimage2DArray​

​GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY​

​uimageCubeArray​

​GL_UNSIGNED_INT_IMAGE_BUFFER​

​uimageBuffer​

​GL_UNSIGNED_INT_ATOMIC_COUNTER​

​atomic_uint​

If one or more elements of an array are active, the name of the array is returned in ​name​​, the type is returned in ​type​​, and the ​size​​ parameter returns the highest array element index used, plus one, as determined by the compiler and/or linker. Only one active uniform variable will be reported for a uniform array. If the active uniform is an array, the uniform name returned in ​name

Uniform variables that are declared as structures or arrays of structures will not be returned directly by this function. Instead, each of these uniform variables will be reduced to its fundamental components containing the "." and "[]" operators such that each of the names is valid as an argument to ​​glGetUniformLocation​​. Each of these reduced uniform variables is counted as one active uniform variable and is assigned an index. A valid name cannot be a structure, an array of structures, or a subcomponent of a vector or matrix.

The size of the uniform variable will be returned in ​size​. Uniform variables other than arrays will have a size of 1. Structures and arrays of structures will be reduced as described earlier, such that each of the names returned will be a data type in the earlier list. If this reduction results in an array, the size returned will be as described for uniform arrays; otherwise, the size returned will be 1.

The list of active uniform variables may include both built-in uniform variables (which begin with the prefix "gl_") as well as user-defined uniform variable names.

This function will return as much information as it can about the specified active uniform variable. If no information is available, ​length​​ will be 0, and ​name​​ will be an empty string. This situation could occur if this function is called after a link operation that failed. If an error occurs, the return values ​length​​, ​size​​, ​type​​, and ​name

Errors

​GL_INVALID_VALUE​​​ is generated if ​program

​GL_INVALID_OPERATION​​​ is generated if ​program

​GL_INVALID_VALUE​​​ is generated if ​index​​ is greater than or equal to the number of active uniform variables in ​program​.

​GL_INVALID_VALUE​​​ is generated if ​bufSize

Associated Gets

​​glGet​​​ with argument ​​GL_MAX_VERTEX_UNIFORM_COMPONENTS​​​, ​​GL_MAX_FRAGMENT_UNIFORM_COMPONENTS​​​, ​​GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS​​​, or ​​GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS​​.

​​glGetProgramiv​​​ with argument ​​GL_ACTIVE_UNIFORMS​​​ or ​​GL_ACTIVE_UNIFORM_MAX_LENGTH​​.

​​glIsProgram​​

API Version Support

OpenGL ES API Version

Function Name

2.0

3.0

3.1

3.2

glGetActiveUniform





See Also

​​glGetUniform​​​, ​​glGetUniformLocation​​​, ​​glLinkProgram​​​, ​​glUniform​​​, ​​glUseProgram​​

Copyright

Copyright © 2003-2005 3Dlabs Inc. Ltd. Copyright © 2010-2015 Khronos Group This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. ​​http://opencontent.org/openpub/​​.

翻译

名称

glGetActiveUniform - 返回有关活动统一变量的信息

C规范

void glGetActiveUniform(GLuint program,

                                                 GLuint index,

                                                 GLsizei bufSize,

                                                 GLsizei *length,

                                                 GLint *size,

                                                 GLenum *type,

                                                 GLchar *name);

参数

program

指定要查询的程序对象。

index

指定要查询的统一变量的索引。

bufSize

指定允许OpenGL在由name指示的字符缓冲区中写入的最大字符数。

length

如果传递NULL以外的值,则返回由name指示的字符串中的OpenGL实际写入的字符数(不包括空终止符)。

size

返回统一变量的大小。

type

返回统一变量的数据类型。

name

返回包含统一变量名称的以null结尾的字符串。

描述

glGetActiveUniform返回有关程序指定的程序对象中的活动统一变量的信息。可以通过使用值GL_ACTIVE_UNIFORMS调用​​glGetProgramiv​​来获得活动的统一变量的数量。索引的值为0的是选择第一个活动的统一变量。索引的允许值范围从0到活动统一变量的数量减1。

着色器可以使用内置的统一变量,用户定义的统一变量或两者。内置的统一变量具有前缀“gl_”并且引用现有的OpenGL状态或从这种状态导出的值(例如,gl_DepthRange)。用户定义的统一变量具有任意名称,并通过调用​​glUniform​​​从应用程序获取它们的值。如果在链接操作期间确定可以在程序执行期间访问它,则统一变量(内置或用户定义)被认为是活动的。因此,程序之前应该是调用​​glLinkProgram​​的目标,但它没有必要成功链接。

在程序中存储最长的统一变量名所需的字符缓冲区的大小可以通过调用值为GL_ACTIVE_UNIFORM_MAX_LENGTH的​​glGetProgramiv​​来获得。此值应用于分配足够大小的缓冲区来存储返回的统一变量名称。该字符缓冲区的大小在bufSize中传递,并且在该名称中传递指向该字符缓冲区的指针。

glGetActiveUniform返回由index指示的统一变量的名称,将其存储在name指定的字符缓冲区中。返回的字符串将以null结尾。写入此缓冲区的实际字符数以长度形式返回,并且此计数不包括空终止字符。如果不需要返回字符串的长度,则可以在length参数中传递NULL值。

type参数将返回指向统一变量数据类型的指针。可以返回符号常数GL_FLOATGL_FLOAT_VEC2GL_FLOAT_VEC3GL_FLOAT_VEC4GL_INT_GLEC_VEC2GL_INT_VEC3GL_INT_VEC4GL_BOOLGL_BOOL_VEC2GL_BOOL_VEC3GL_BOOL_VEC4GL_FLOAT_MAT2GL_FLOAT_MAT3GL_FLOAT_MAT4GL_SAMPLER_2DGL_SAMPLER_CUBE

如果数组的一个或多个元素处于活动状态,则在name中返回数组的名称,类型以type返回,并且size参数返回使用的最高数组元素索引加上1,具体由编译器确定和/或链接器。对于统一阵列,仅报告一个活动的统一变量。

声明为结构或结构数组的统一变量不会由此函数直接返回。相反,这些统一变量中的每一个都将被简化为包含“.”和“[]”运算符的基本组成部分,使得每个名称作为​​glGetUniformLocation​​的参数有效。

统一变量的大小将以size返回。除数组之外的统一变量将具有1的维度大小。结构和结构数组将如前所述减少,使得返回的每个名称将是先前列表中的数据类型。

活动统一变量列表可以包括内置的统一变量(以前缀“gl_”开头)以及用户定义的统一变量名称。

此函数将返回尽可能多的有关指定的活动统一变量的信息。如果没有可用信息,则length为0,name为空字符串(如果在失败的链接操作后调用此函数,则可能发生这种情况)。如果发生错误,则返回值lengthsizetypename将不会被修改。

错误

GL_INVALID_VALUEprogram不是OpenGL生成的值。

GL_INVALID_OPERATIONprogram不是程序对象。

GL_INVALID_VALUEindex>=程序中活动统一变量的数量。

GL_INVALID_VALUEbufSize<0

相关Gets

​​glGet​​ 参数GL_MAX_VERTEX_UNIFORM_VECTORSGL_MAX_FRAGMENT_UNIFORM_VECTORS

​​glGetProgramiv​​ 参数GL_ACTIVE_UNIFORMSGL_ACTIVE_UNIFORM_MAX_LENGTH

​​glIsProgram​​

另见

​​glGetActiveAttrib​​​,​​glGetUniform​​​,​​glGetUniformLocation​​​,​​glLinkProgram​​​,​​glUniform​​​,​​glUseProgram​​

版权

​​https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glGetActiveUniform.xml​​

Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见​​http://oss.sgi.com/projects/FreeB/​​.

代码

运行效果

openGL/openGLES glGetActiveUniform函数详解_2d

 

// The MIT License (MIT)
//
// Copyright (c) 2013 Dan Ginsburg, Budirijanto Purnomo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//
// Book: OpenGL(R) ES 3.0 Programming Guide, 2nd Edition
// Authors: Dan Ginsburg, Budirijanto Purnomo, Dave Shreiner, Aaftab Munshi
// ISBN-10: 0-321-93388-5
// ISBN-13: 978-0-321-93388-1
// Publisher: Addison-Wesley Professional
// URLs: http://www.opengles-book.com
// http://my.safaribooksonline.com/book/animation-and-3d/9780133440133
//
// Hello_Triangle.c
//
// This is a simple example that draws a single triangle with
// a minimal vertex/fragment shader. The purpose of this
// example is to demonstrate the basic concepts of
// OpenGL ES 3.0 rendering.
//#include "esUtil.h"
//#include "Common/Include/esUtil.h"
#include "esUtil.h"

typedef struct
{
// Handle to a program object
GLuint programObject;

} UserData;

///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader;
GLint compiled;

// Create the shader object
shader = glCreateShader ( type );

if ( shader == 0 )
{
return 0;
}

// Load the shader source
glShaderSource ( shader, 1, &shaderSrc, NULL );

// Compile the shader
glCompileShader ( shader );

// Check the compile status
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );

if ( !compiled )
{
GLint infoLen = 0;

glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );

if ( infoLen > 1 )
{
char *infoLog = malloc ( sizeof ( char ) * infoLen );

glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
esLogMessage ( "Error compiling shader:\n%s\n", infoLog );

free ( infoLog );
}

glDeleteShader ( shader );
return 0;
}

return shader;

}

///
// Initialize the shader and program object
//"precision mediump float;表示着色器中浮点变量的默认精度
//"out vec4 fragColor; 表示这个变量的值将被输出到颜色缓冲区
int Init ( ESContext *esContext )
{
UserData *userData = esContext->userData;
char vShaderStr[] =
"#version 300 es \n"
"layout(location = 0) in vec4 vPosition; \n"
"uniform mat4 projection; \n"
"uniform mat4 view; \n"
"uniform mat4 model; \n"
"void main() \n"
"{ \n"
" gl_Position = projection * view * model * vPosition; \n"
" gl_Position = vPosition; \n"
"} \n";

char fShaderStr[] =
"#version 300 es \n"
"precision mediump float; \n"
"out vec4 fragColor; \n"
"void main() \n"
"{ \n"
" fragColor = vec4 ( 0.0, 1.0, 0.0, 1.0 ); \n"
"} \n";

GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
GLint uniformActiveNum;
GLint uniformMaxNum;
GLsizei bufSize = 0;
GLsizei uniformLeng;

// Load the vertex/fragment shaders
vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );

// Create the program object
programObject = glCreateProgram ( );

if ( programObject == 0 )
{
return 0;
}

glAttachShader ( programObject, vertexShader );
glAttachShader ( programObject, fragmentShader );

// Link the program
glLinkProgram ( programObject );

// Check the link status
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );

//获取uniform的数量
//uniformNum:为什么不是3个uniform,注意光声明uniform变量,不使用这个uniform不做算,只有使用了才算数
//注意:是要激活的,没激活不算数
glGetProgramiv(programObject, GL_ACTIVE_UNIFORMS, &uniformActiveNum);

glGetProgramiv(programObject, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNum);//0xb只有11个?为什么?

GLint errorLogLen = 0;
GLchar errorLog[512];
glGetActiveUniform(programObject, 1, 512, NULL, 1, GL_FLOAT_MAT4, "projection");


glGetProgramInfoLog(programObject, errorLogLen, NULL, errorLog);
esLogMessage("glGetActiveUniform:%s\n", errorLog);

if ( !linked )
{
GLint infoLen = 0;

glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );

if ( infoLen > 1 )
{
char *infoLog = malloc ( sizeof ( char ) * infoLen );

glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
esLogMessage ( "Error linking program:\n%s\n", infoLog );

free ( infoLog );
}

glDeleteProgram ( programObject );
return FALSE;
}

// Store the program object
userData->programObject = programObject;

glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );
return TRUE;
}

///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};

// Set the viewport
glViewport ( 0, 0, esContext->width, esContext->height );

// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT );

// Use the program object
glUseProgram ( userData->programObject );

// Load the vertex data
glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); //vVertices 对应顶点着色器位置0 layout(location = 0)
glEnableVertexAttribArray ( 0 );

glDrawArrays ( GL_TRIANGLES, 0, 3 );
}

void Shutdown ( ESContext *esContext )
{
UserData *userData = esContext->userData;

glDeleteProgram ( userData->programObject );
}

int esMain ( ESContext *esContext )
{
esContext->userData = malloc ( sizeof ( UserData ) );

esCreateWindow ( esContext, "Hello Triangle", 320, 240, ES_WINDOW_RGB );

if ( !Init ( esContext ) )
{
return GL_FALSE;
}

esRegisterShutdownFunc ( esContext, Shutdown );
esRegisterDrawFunc ( esContext, Draw );

return GL_TRUE;
}

​​源码下载地址​​

举报

相关推荐

0 条评论