openGL/openGLES glGetActiveUniform函数详解

阅读: 评论:0

openGL/openGLES glGetActiveUniform函数详解

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 range from zero to the number of active uniform variables minus one.

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 argument.

The type argument will return a pointer to the uniform variable's data type. The symbolic constants returned for uniform types are shown in the table below.

Returned Symbolic ContantShader Uniform Type
GL_FLOATfloat
GL_FLOAT_VEC2vec2
GL_FLOAT_VEC3vec3
GL_FLOAT_VEC4vec4
GL_INTint
GL_INT_VEC2ivec2
GL_INT_VEC3ivec3
GL_INT_VEC4ivec4
GL_UNSIGNED_INTunsigned int
GL_UNSIGNED_INT_VEC2uvec2
GL_UNSIGNED_INT_VEC3uvec3
GL_UNSIGNED_INT_VEC4uvec4
GL_BOOLbool
GL_BOOL_VEC2bvec2
GL_BOOL_VEC3bvec3
GL_BOOL_VEC4bvec4
GL_FLOAT_MAT2mat2
GL_FLOAT_MAT3mat3
GL_FLOAT_MAT4mat4
GL_FLOAT_MAT2x3mat2x3
GL_FLOAT_MAT2x4mat2x4
GL_FLOAT_MAT3x2mat3x2
GL_FLOAT_MAT3x4mat3x4
GL_FLOAT_MAT4x2mat4x2
GL_FLOAT_MAT4x3mat4x3
GL_SAMPLER_2Dsampler2D
GL_SAMPLER_3Dsampler3D
GL_SAMPLER_CUBEsamplerCube
GL_SAMPLER_2D_SHADOWsampler2DShadow
GL_SAMPLER_2D_ARRAYsampler2DArray
GL_SAMPLER_2D_ARRAY_SHADOWsampler2DArrayShadow
GL_SAMPLER_2D_MULTISAMPLEsampler2DMS
GL_SAMPLER_2D_MULTISAMPLE_ARRAYsampler2DMSArray
GL_SAMPLER_CUBE_SHADOWsamplerCubeShadow
GL_SAMPLER_CUBE_MAP_ARRAYsamplerCubeArray
GL_SAMPLER_CUBE_MAP_ARRAY_SHADOWsamplerCubeArrayShadow
GL_SAMPLER_BUFFERsamplerBuffer
GL_INT_SAMPLER_2Disampler2D
GL_INT_SAMPLER_3Disampler3D
GL_INT_SAMPLER_CUBEisamplerCube
GL_INT_SAMPLER_2D_ARRAYisampler2DArray
GL_INT_SAMPLER_2D_MULTISAMPLEisampler2DMS
GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAYisampler2DMSArray
GL_INT_SAMPLER_CUBE_MAP_ARRAYisamplerCubeArray
GL_INT_SAMPLER_BUFFERisamplerBuffer
GL_UNSIGNED_INT_SAMPLER_2Dusampler2D
GL_UNSIGNED_INT_SAMPLER_3Dusampler3D
GL_UNSIGNED_INT_SAMPLER_CUBEusamplerCube
GL_UNSIGNED_INT_SAMPLER_2D_ARRAYusampler2DArray
GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLEusampler2DMS
GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAYusampler2DMSArray
GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAYusamplerCubeArray
GL_UNSIGNED_INT_SAMPLER_BUFFERusamplerBuffer
GL_IMAGE_2Dimage2D
GL_IMAGE_3Dimage3D
GL_IMAGE_CUBEimageCube
GL_IMAGE_2D_ARRAYimage2DArray
GL_IMAGE_CUBE_MAP_ARRAYimageCubeArray
GL_IMAGE_BUFFERimageBuffer
GL_INT_IMAGE_2Diimage2D
GL_INT_IMAGE_3Diimage3D
GL_INT_IMAGE_CUBEiimageCube
GL_INT_IMAGE_2D_ARRAYiimage2DArray
GL_INT_IMAGE_CUBE_MAP_ARRAYiimageCubeArray
GL_INT_IMAGE_BUFFERiimageBuffer
GL_UNSIGNED_INT_IMAGE_2Duimage2D
GL_UNSIGNED_INT_IMAGE_3Duimage3D
GL_UNSIGNED_INT_IMAGE_CUBEuimageCube
GL_UNSIGNED_INT_IMAGE_2D_ARRAYuimage2DArray
GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAYuimageCubeArray
GL_UNSIGNED_INT_IMAGE_BUFFERuimageBuffer
GL_UNSIGNED_INT_ATOMIC_COUNTERatomic_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 will always be the name of the uniform array appended with "[0]".

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 lengthsizetype, and name will be unmodified.

Errors

GL_INVALID_VALUE is generated if program is not a value generated by OpenGL.

GL_INVALID_OPERATION is generated if program is not a program object.

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 is less than 0.

Associated Gets

glGet with argument GL_MAX_VERTEX_UNIFORM_COMPONENTSGL_MAX_FRAGMENT_UNIFORM_COMPONENTSGL_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 Name2.03.03.13.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. /.

翻译

名称

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

版权

.0/l

Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见/.

代码

运行效果

 

// 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:      
//            
//
// 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 objectGLuint 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 objectshader = glCreateShader ( type );if ( shader == 0 ){return 0;}// Load the shader sourceglShaderSource ( shader, 1, &shaderSrc, NULL );// Compile the shaderglCompileShader ( shader );// Check the compile statusglGetShaderiv ( 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%sn", 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 shadersvertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );// Create the program objectprogramObject = glCreateProgram ( );if ( programObject == 0 ){return 0;}glAttachShader ( programObject, vertexShader );glAttachShader ( programObject, fragmentShader );// Link the programglLinkProgram ( programObject );// Check the link statusglGetProgramiv ( 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:%sn", 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%sn", infoLog );free ( infoLog );}glDeleteProgram ( programObject );return FALSE;}// Store the program objectuserData->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 viewportglViewport ( 0, 0, esContext->width, esContext->height );// Clear the color bufferglClear ( GL_COLOR_BUFFER_BIT );// Use the program objectglUseProgram ( userData->programObject );// Load the vertex dataglVertexAttribPointer ( 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;
}

源码下载地址

本文发布于:2024-02-04 05:11:02,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170699823852357.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:有关OpenGL ES
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23