void SharedProjectionAndViewing::determineBlockSizeSetBindingPoint( GLuint shaderProgram ) { // Get the index of the "projectionViewBlock" projViewBlockIndex = glGetUniformBlockIndex(shaderProgram, transformBlockName.c_str()); cout << transformBlockName.c_str() << " index is " << projViewBlockIndex << endl; if (checkLocationFound(transformBlockName.c_str(), projViewBlockIndex)) { // Determine the size in bytes of the uniform block. glGetActiveUniformBlockiv(shaderProgram, projViewBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &projViewBlockSize); cout << transformBlockName.c_str() << " size is " << projViewBlockSize << endl; // Assign the block to a binding point. In this case 2. glUniformBlockBinding(shaderProgram, projViewBlockIndex, projectionViewBlockBindingPoint); } // Get the index of the "EyeBlock" worldEyeBlockIndex = glGetUniformBlockIndex(shaderProgram, eyeBlockName.c_str() ); cout << eyeBlockName.c_str() << " index is " << worldEyeBlockIndex << endl; if ( checkLocationFound( "worldEyeBlock", worldEyeBlockIndex ) ) { // Determine the size in bytes of the uniform block. glGetActiveUniformBlockiv(shaderProgram, worldEyeBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &worldEyeBlockSize); cout << eyeBlockName.c_str() << " size is " << worldEyeBlockSize << endl; // Assign the block to a binding point. In this case 3. glUniformBlockBinding(shaderProgram, worldEyeBlockIndex, worldEyeBlockBindingPoint); } checkOpenGLErrors("findBlockSizeSetBindingPoint"); } // end determineBlockSizeSetBindingPoint
// used for debugging to print the offsets of uniform buffer members void printUniformOffsets(GLuint program, GLuint uniformBlock) { GLchar name[256]; GLint uniformCount; glGetActiveUniformBlockiv( program, uniformBlock, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &uniformCount ); GLint *indices = new GLint[uniformCount]; glGetActiveUniformBlockiv( program, uniformBlock, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, indices ); for ( GLint i = 0; i < uniformCount; ++i ) { const GLuint index = (GLuint)indices[i]; GLint type, offset; glGetActiveUniformName(program, index, 256, 0, name); glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_TYPE, &type); glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_OFFSET, &offset); std::cout << " " << name << " (offset) : " << offset << std::endl; } std::cout << std::endl; delete [] indices; }
void UniformBuffer::init(Shader *referent, std::string const &blockName, std::string const vars[]) { GLint blockIdx, varNbr; GLint *varsInfos; glGenBuffers(1, &_bufferId); glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _bufferId); blockIdx = glGetUniformBlockIndex(referent->getId(), blockName.c_str()); // find the total size to allocate the buffer glGetActiveUniformBlockiv(referent->getId(), blockIdx, GL_UNIFORM_BLOCK_DATA_SIZE, (GLint*)&_dataSize); _buffer = new char[_dataSize]; // get the number of uniforms glGetActiveUniformBlockiv(referent->getId(), blockIdx, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &varNbr); assert(varNbr > 0 && "glGetActiveUniformBlockid Error"); // we store the uniforms informations in this table with this layout: // Indices - Types - Offset varsInfos = new GLint[varNbr * 3]; glGetActiveUniformBlockiv(referent->getId(), blockIdx, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, varsInfos); glGetActiveUniformsiv(referent->getId(), varNbr, (GLuint*)varsInfos, GL_UNIFORM_TYPE, varsInfos + varNbr); glGetActiveUniformsiv(referent->getId(), varNbr, (GLuint*)varsInfos, GL_UNIFORM_OFFSET, varsInfos + 2 * varNbr); // sort the vars by offset to make them correspond with the order of the names std::list<std::pair<GLint, GLint> > sorted; std::list<std::pair<GLint, GLint> >::iterator it; GLint curOffset; GLint i; // insertion sort for (i = 0; i < varNbr; ++i) { curOffset = varsInfos[varNbr * 2 + i]; it = sorted.begin(); while (it != sorted.end() && it->second < curOffset) ++it; sorted.insert(it, std::pair<GLint, GLint>(i, curOffset)); } // bind the name with the offset and type it = sorted.begin(); for (i = 0; it != sorted.end() && i < varNbr; ++i) { SUniformVars added; added.offset = varsInfos[varNbr * 2 + it->first]; added.type = varsInfos[varNbr + it->first]; _vars[vars[i]] = added; ++it; } delete[] varsInfos; }
void UniformBuffer::Create( const GLSLShaderProgram* shaderProgram, const GLchar* const uniformBufferName, const GLchar** const uniformSubNames, const GLshort numUniforms) { m_numUniforms = numUniforms; glGetActiveUniformBlockiv( shaderProgram->GetID(), shaderProgram->GetUniformBlockIndex(uniformBufferName), GL_UNIFORM_BLOCK_DATA_SIZE, &m_sizeInBytes); if(m_sizeInBytes == 0) BREAKPOINT(UniformBuffer Create UniformBuffer has no size); m_buffer = new GLubyte[m_sizeInBytes]; // get offsets per uniform GLuint* indices = new GLuint[m_numUniforms]; glGetUniformIndices( shaderProgram->GetID(), m_numUniforms, uniformSubNames, indices); // validate the indices for( GLuint i = 0; i < m_numUniforms; ++i) { if(indices[i] == GLuint(0 - 1)) BREAKPOINT(UniformBuffer Create not all the uniforms are used in the shader and thus excist); } m_offsets = new GLint[m_numUniforms]; glGetActiveUniformsiv( shaderProgram->GetID(), m_numUniforms, indices, GL_UNIFORM_OFFSET, m_offsets); delete indices; glGenBuffers( 1, &m_handle); }
void OGL4ShaderProgram::CollectUniformBlocks() { GLint count = 0; glGetProgramiv(m_program, GL_ACTIVE_UNIFORM_BLOCKS, &count); for(int i = 0; i < count; ++i) { char szName[1024]; glGetActiveUniformBlockName(m_program, i, 1024, 0, szName); GLint bytes = 0; glGetActiveUniformBlockiv(m_program, i, GL_UNIFORM_BLOCK_DATA_SIZE, &bytes); OGL4BufferPtr pBuffer = std::make_shared<OGL4Buffer>(); if(pBuffer->Create(BT_CONSTANT_BUFFER, bytes, nullptr, true) == false) { pBuffer->Release(); pBuffer.reset(); continue; } UniformBlock block; block.index = i; block.name = szName; block.pBuffer = pBuffer; m_uniformBlocks.push_back(block); } }
void UBOShaderInterface::Create(const std::string &uniformBlockName, Shader* shader, const char** uniformNames, GLsizei numUniformNames) { m_pShader = shader; m_blockIndex = glGetUniformBlockIndex(m_pShader->GetProgID(), uniformBlockName.c_str()); assert(m_blockIndex != GL_INVALID_INDEX); glGetActiveUniformBlockiv(m_pShader->GetProgID(), m_blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &m_blockSize); GLuint* indices = new GLuint[numUniformNames]; glGetUniformIndices(m_pShader->GetProgID(), numUniformNames, uniformNames, indices); // Query offsets and associate names to the offsets int* offsets = new int[numUniformNames]; glGetActiveUniformsiv(m_pShader->GetProgID(), numUniformNames, indices, GL_UNIFORM_OFFSET, offsets); delete[] indices; for(size_t i = 0; i < numUniformNames; i++) m_uniformNameToOffset[uniformNames[i]] = offsets[i]; delete[] offsets; GL_ERROR_CHECK(); }
void GLCamera::SetMatrices( kzsGLSL shader ) { /* NEED TO DEFINE THESE VARIABLES IN SHADER PROGRAM FILE: ------------------------------------------------------ uniform cameraMat { mat4 ModelViewMatrix; mat4 ModelViewProjectionMatrix; mat3 NormalMatrix; }; ------------------------------------------------------ */ GLuint progHandle = shader.getHandle(); blockIndex = glGetUniformBlockIndex( progHandle, "cameraMat" ); //GLint blockSize; glGetActiveUniformBlockiv( progHandle, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize ); blockBuffer = ( GLubyte* ) malloc( blockSize ); // 各ブロック変数のオフセットを問い合わせる const GLchar* names[] = { "ModelViewMatrix", "ModelViewProjectionMatrix", "NormalMatrix" }; GLuint indices[ 3 ]; glGetUniformIndices( progHandle, 3, names, indices ); //GLint offset[ 3 ]; glGetActiveUniformsiv( progHandle, 3, indices, GL_UNIFORM_OFFSET, offset ); // ModelViewMatrix Data modelview = glm::mat4( 1.0f ); // LoadIdentity modelview = glm::translate( modelview, vec3( 0.0, 0.0, dist ) ); modelview *= glm::lookAt( vec3( cameraPos.x(), cameraPos.y(), cameraPos.z() ), vec3( targetPos.x(), targetPos.y(), targetPos.z() ), vec3( viewUpVec.x(), viewUpVec.y(), viewUpVec.z() ) ); // viewUpVecの値次第で変化させる必要あり。(下式はY軸上方向固定で成り立つ) modelview = glm::rotate( modelview, rX, vec3( 1, 0, 0 ) ); modelview = glm::rotate( modelview, rY, vec3( 0, 1, 0 ) ); // ModelViewProjectionMatrix Data glm::mat4 MVP = projection * modelview; // NormalMatrix Data glm::mat3 normal = glm::mat3( vec3( modelview[0] ), vec3( modelview[1] ), vec3( modelview[2] ) ); memcpy( blockBuffer + offset[ 0 ], &modelview, sizeof( glm::mat4 ) ); memcpy( blockBuffer + offset[ 1 ], &MVP , sizeof( glm::mat4 ) ); memcpy( blockBuffer + offset[ 2 ], &normal , sizeof( glm::mat3 ) ); //GLuint uboHandle; glGenBuffers( 1, &uboHandle ); glBindBuffer( GL_UNIFORM_BUFFER, uboHandle ); glBufferData( GL_UNIFORM_BUFFER, blockSize, blockBuffer, GL_DYNAMIC_DRAW ); glBindBufferBase( GL_UNIFORM_BUFFER, blockIndex, uboHandle ); }
static bool try_140_test() { bool pass = true; GLint idx; GLint binding; prog140 = piglit_build_simple_program(vert140_source, frag140_source); idx = glGetUniformBlockIndex(prog140, "U"); if (idx == -1) { printf("Failed to get index for \"U\"\n"); pass = false; } glGetActiveUniformBlockiv(prog140, idx, GL_UNIFORM_BLOCK_BINDING, &binding); if (binding != 2) { printf("Expected block binding = 2, got %d\n", binding); pass = false; } pass = piglit_check_gl_error(GL_NO_ERROR) && pass; return pass; }
bool initBuffer() { // Generate buffer objects glGenBuffers(buffer::MAX, &BufferName[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[buffer::ELEMENT]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, ElementSize, ElementData, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]); glBufferData(GL_ARRAY_BUFFER, PositionSize, PositionData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); GLint UniformBlockSize = 0; { glGetActiveUniformBlockiv( ProgramName, UniformTransform, GL_UNIFORM_BLOCK_DATA_SIZE, &UniformBlockSize); glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glBufferData(GL_UNIFORM_BUFFER, UniformBlockSize, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } return this->checkError("initBuffer"); }
static void CopyShaderState_UniformBlocks(GLuint newProgID, GLuint oldProgID) { if (!GLEW_ARB_uniform_buffer_object) return; GLsizei numUniformBlocks, maxNameLength; glGetProgramiv(oldProgID, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks); glGetProgramiv(oldProgID, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &maxNameLength); if (maxNameLength <= 0) return; std::string name(maxNameLength, 0); for (int i = 0; i < numUniformBlocks; ++i) { GLsizei nameLength = 0; glGetActiveUniformBlockName(oldProgID, i, maxNameLength, &nameLength, &name[0]); name[maxNameLength - 1] = 0; if (nameLength == 0) continue; GLuint oldLoc = glGetUniformBlockIndex(oldProgID, &name[0]); GLuint newLoc = glGetUniformBlockIndex(newProgID, &name[0]); if (oldLoc == GL_INVALID_INDEX || newLoc == GL_INVALID_INDEX) continue; GLint value; glGetActiveUniformBlockiv(oldProgID, oldLoc, GL_UNIFORM_BLOCK_BINDING, &value); glUniformBlockBinding(newProgID, newLoc, value); } }
uint32_t cShaderObject::GetUniformBlockSize(uint32_t index) { int ret; glGetActiveUniformBlockiv(mShaderProgram,index, GL_UNIFORM_BLOCK_DATA_SIZE, &ret); return ret; }
void CDecalsDrawerGL4::SunChanged() { // Ground Lighting UBO GLuint uniformBlockIndex = glGetUniformBlockIndex(decalShader->GetObjID(), "SGroundLighting"); assert(uniformBlockIndex != GL_INVALID_INDEX); GLsizei uniformBlockSize = 0; glGetActiveUniformBlockiv(decalShader->GetObjID(), uniformBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uniformBlockSize); //FIXME if (uniformBlockSize != sizeof(SGLSLGroundLighting)) LOG("uniformBlockSize sizeof(SGLSLGroundLighting) %u " _STPF_, uniformBlockSize, sizeof(SGLSLGroundLighting)); assert(uniformBlockSize == sizeof(SGLSLGroundLighting)); uboGroundLighting.Bind(GL_UNIFORM_BUFFER); uboGroundLighting.New(uniformBlockSize, GL_STATIC_DRAW); SGLSLGroundLighting* uboGroundLightingData = (SGLSLGroundLighting*)uboGroundLighting.MapBuffer(0, sizeof(SGLSLGroundLighting)); uboGroundLightingData->ambientColor = sunLighting->groundAmbientColor * CGlobalRendering::SMF_INTENSITY_MULT; uboGroundLightingData->diffuseColor = sunLighting->groundDiffuseColor * CGlobalRendering::SMF_INTENSITY_MULT; uboGroundLightingData->specularColor = sunLighting->groundSpecularColor * CGlobalRendering::SMF_INTENSITY_MULT; uboGroundLightingData->dir = sky->GetLight()->GetLightDir(); uboGroundLightingData->fogColor = sky->fogColor; uboGroundLightingData->fogEnd = globalRendering->viewRange * sky->fogEnd; uboGroundLightingData->fogScale = 1.0f / (globalRendering->viewRange * (sky->fogEnd - sky->fogStart)); uboGroundLighting.UnmapBuffer(); glUniformBlockBinding(decalShader->GetObjID(), uniformBlockIndex, 5); glBindBufferBase(GL_UNIFORM_BUFFER, 5, uboGroundLighting.GetId()); uboGroundLighting.Unbind(); }
shaderparameter_t* shader_get_param(shaderprogram_t* program, const char* name, ShaderParamType type) { GLint paramIndex = 0; GLint paramSize = 0; GLenum paramType; switch(type) { default: case ShaderParamType_Uniform: { GLsizei nameLength; paramIndex = glGetUniformLocation(program->program, name); glGetActiveUniform(program->program, paramIndex, 0, &nameLength, ¶mSize, ¶mType, nullptr); FOUNDATION_ASSERT(paramSize > 0); } case ShaderParamType_UniformBlock: { paramIndex = glGetUniformBlockIndex(program->program, name); glGetActiveUniformBlockiv(program->program, paramIndex, GL_UNIFORM_BLOCK_DATA_SIZE, ¶mSize); FOUNDATION_ASSERT(paramSize > 0); } } shaderparameter_t* param = (shaderparameter_t*) memory_allocate(sizeof(shaderparameter_t), 4, MEMORY_PERSISTENT); param->index = paramIndex; param->size = paramSize; param->gltype = paramType; param->name = string_clone(name); param->type = type; return param; }
GLint program::get_uniform_blocksize(char const* varname) const { GLint blocksize; glGetActiveUniformBlockiv(id_, glGetUniformBlockIndex(id_, varname), GL_UNIFORM_BLOCK_DATA_SIZE, &blocksize); return blocksize; }
void p_light_t::setup(GLuint program) { const int block_sz = 7; const char *block_name = "pLight"; static const char *p_light_names[block_sz] = { "pLight_num", "pLight_position", "pLight_ambient", "pLight_diffuse", "pLight_specular", "pLight_attenuation", "pLight_mat" }; static GLuint index[block_sz]; static int offset[block_sz]; static GLuint blockIndex; static int blockSize; static bool init = false; static GLuint buffer; if (!init) { init = true; blockIndex = glGetUniformBlockIndex(program, block_name); glGetActiveUniformBlockiv(program, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize); glGetUniformIndices(program, block_sz, p_light_names, index); glGetActiveUniformsiv(program, block_sz, index, GL_UNIFORM_OFFSET, offset); char *ch = new char[blockSize]; memset(ch, 0, blockSize); glGenBuffersARB(1, &buffer); glBindBuffer(GL_UNIFORM_BUFFER, buffer); glBufferData(GL_UNIFORM_BUFFER, blockSize, ch, GL_STREAM_DRAW); } glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer); glUniformBlockBinding(program, blockIndex, 0); char *ptr = (char*)glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY); memcpy(ptr + offset[0], &cnt, sizeof(int)); if (!cnt) { glUnmapBuffer(GL_UNIFORM_BUFFER); return; } memcpy(ptr + offset[1], position[0].v, cnt * 4 * sizeof(GLfloat)); memcpy(ptr + offset[2], ambient[0].v, cnt * 4 * sizeof(GLfloat)); memcpy(ptr + offset[3], diffuse[0].v, cnt * 4 * sizeof(GLfloat)); memcpy(ptr + offset[4], specular[0].v, cnt * 4 * sizeof(GLfloat)); memcpy(ptr + offset[5], attenuation[0].v, cnt * 4 * sizeof(GLfloat)); memcpy(ptr + offset[6], mat[0].m, cnt * 4 * 4 * sizeof(GLfloat)); glUnmapBuffer(GL_UNIFORM_BUFFER); }
static bool try_150_test() { bool pass = true; GLint idx; GLint binding; unsigned i; prog150 = piglit_build_simple_program(vert150_source, frag150_source); for (i = 0; i < 2; i++) { char name[5] = "U[0]"; name[2] = '0' + i; idx = glGetUniformBlockIndex(prog150, name); if (idx == -1) { printf("Failed to get index for \"%s\"\n", name); pass = false; } glGetActiveUniformBlockiv(prog150, idx, GL_UNIFORM_BLOCK_BINDING, &binding); if (binding != (3 + i)) { printf("Expected block binding = %d, got %d\n", 3 + i, binding); pass = false; } } pass = piglit_check_gl_error(GL_NO_ERROR) && pass; return pass; }
static bool test_format(const struct uniform_type *type, bool row_major) { /* Using 140 to get unsigned ints. */ const char *fs_template = "#version 140\n" "layout(std140) uniform ubo {\n" " float align_test;\n" " %s%s u;\n" "};\n" "\n" "void main() {\n" " gl_FragColor = vec4(align_test);\n" "}\n"; char *fs_source; GLuint prog; GLint data_size; int expected; const struct uniform_type *transposed_type; if (row_major) transposed_type = get_transposed_type(type); else transposed_type = type; (void)!asprintf(&fs_source, fs_template, row_major ? "layout(row_major) " : "", type->type); prog = piglit_build_simple_program(NULL, fs_source); free(fs_source); /* There's only one block, so it's uniform block 0. */ glGetActiveUniformBlockiv(prog, 0, GL_UNIFORM_BLOCK_DATA_SIZE, &data_size); glDeleteProgram(prog); /* "align_test" at the start of the UBO is a float, so our * test uniform would start at byte 4 if not for alignment. */ expected = 4; /* The actual space for our object. */ expected = align(expected, transposed_type->alignment); expected += transposed_type->size; /* Finally, align to a vec4 like std140 says. */ expected = align(expected, 16); printf("%-20s %10s %10d %10d%s\n", type->type, row_major ? "y" : "n", data_size, expected, data_size == expected ? "" : " FAIL"); return data_size == expected; }
GLint ShaderProgram::GetUniformBlockSize(const std::string& name) const { GLint blockSize; glGetActiveUniformBlockiv(_programHandle, GetUniformBlockIndex(name), GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize); return blockSize; }
void CDecalsDrawerGL4::CreateStructureVBOs() { { // Decal Groups UBO GLsizei uniformBlockSize = 0; GLuint uniformBlockIndex = glGetUniformBlockIndex(decalShader->GetObjID(), "decalGroupsUBO"); glGetActiveUniformBlockiv(decalShader->GetObjID(), uniformBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uniformBlockSize); uboDecalGroups.Bind(GL_UNIFORM_BUFFER); uboDecalGroups.New(uniformBlockSize, GL_DYNAMIC_DRAW); uboDecalGroups.Unbind(); glUniformBlockBinding(decalShader->GetObjID(), uniformBlockIndex, 4); glBindBufferBase(GL_UNIFORM_BUFFER, 4, uboDecalGroups.GetId()); } { // Decals UBO GLint uniformBlockSize = 0; GLuint uniformBlockIndex = 0; if (useSSBO) { uniformBlockIndex = glGetProgramResourceIndex(decalShader->GetObjID(), GL_SHADER_STORAGE_BLOCK, "decalsUBO"); constexpr GLenum props[] = {GL_BUFFER_DATA_SIZE}; glGetProgramResourceiv(decalShader->GetObjID(), GL_SHADER_STORAGE_BLOCK, uniformBlockIndex, 1, props, 1, nullptr, &uniformBlockSize); } else { uniformBlockIndex = glGetUniformBlockIndex(decalShader->GetObjID(), "decalsUBO"); glGetActiveUniformBlockiv(decalShader->GetObjID(), uniformBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uniformBlockSize); } uboDecalsStructures.Bind(GL_UNIFORM_BUFFER); uboDecalsStructures.New(uniformBlockSize, GL_DYNAMIC_DRAW); uboDecalsStructures.Unbind(); if (useSSBO) { glShaderStorageBlockBinding(decalShader->GetObjID(), uniformBlockIndex, 3); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, uboDecalsStructures.GetId()); } else { glUniformBlockBinding(decalShader->GetObjID(), uniformBlockIndex, 3); glBindBufferBase(GL_UNIFORM_BUFFER, 3, uboDecalsStructures.GetId()); } } }
bool initBuffer() { bool Validated(true); glGenBuffers(buffer::MAX, BufferName); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[buffer::ELEMENT]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, ElementSize, ElementData, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]); glBufferData(GL_ARRAY_BUFFER, PositionSize, PositionData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); GLint UniformBlockSize(0); { glGetActiveUniformBlockiv( ProgramName, glf::semantic::uniform::TRANSFORM0, GL_UNIFORM_BLOCK_DATA_SIZE, &UniformBlockSize); UniformBlockSize = glm::max(UniformBufferOffset, UniformBlockSize) * Instances; glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glBufferData(GL_UNIFORM_BUFFER, UniformBlockSize * Instances, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } { glm::vec4 Diffuse(1.0f, 0.5f, 0.0f, 1.0f); glGetActiveUniformBlockiv( ProgramName, glf::semantic::uniform::MATERIAL, GL_UNIFORM_BLOCK_DATA_SIZE, &UniformBlockSize); glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::MATERIAL]); glBufferData(GL_UNIFORM_BUFFER, UniformBlockSize, &Diffuse[0], GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } return Validated; }
PIGLIT_GL_TEST_CONFIG_END void piglit_init(int argc, char **argv) { bool pass = true; const char *fs_source = "#extension GL_ARB_uniform_buffer_object : require\n" "uniform ubo {\n" " float u;\n" "};\n" "\n" "void main() {\n" " gl_FragColor = vec4(u);\n" "}\n"; GLuint prog; GLint junk = 0xd0d0, unwritten_junk = junk; piglit_require_extension("GL_ARB_uniform_buffer_object"); prog = piglit_build_simple_program(NULL, fs_source); /* Test a bad pname (it's one for glActiveUniformsiv). */ glGetActiveUniformBlockiv(prog, 0, GL_UNIFORM_TYPE, &junk); if (!piglit_check_gl_error(GL_INVALID_ENUM) || junk != unwritten_junk) pass = false; /* Bad active uniform block index. "The indices of the active * uniform blocks of a program are assigned in consecutive * order, beginning with zero." */ glGetActiveUniformBlockiv(prog, 1, GL_UNIFORM_BLOCK_BINDING, &junk); if (!piglit_check_gl_error(GL_INVALID_VALUE) || junk != unwritten_junk) pass = false; /* Test bad program name by deleting ours */ glDeleteProgram(prog); glGetActiveUniformBlockiv(prog, 0, GL_UNIFORM_BLOCK_BINDING, &junk); if (!piglit_check_gl_error(GL_INVALID_VALUE) || junk != unwritten_junk) pass = false; piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL); }
bool ShaderUniformBuffer::createUniforms(UNIFORM_BUFFER_DESC* pdescs, int nr) { glGetActiveUniformBlockiv(mProgram, mBlock, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &mElementNr); GLuint* pindices = new GLuint[mElementNr]; glGetActiveUniformBlockiv(mProgram, mBlock, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, (GLint*) pindices); GLint maxlength; glGetProgramiv(mProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxlength); GLchar* pname = new GLchar[maxlength]; mpElements = new BLOCK_UNIFORM[mElementNr]; for ( int index = 0; index < mElementNr; ++index ) { BLOCK_UNIFORM& element = mpElements[index]; int length; glGetActiveUniformName(mProgram, pindices[index], maxlength, &length, pname); element.name = String::fromUtf8(pname); glGetActiveUniformsiv(mProgram, 1, &pindices[index], GL_UNIFORM_OFFSET, &element.offset); int offset = 0; for ( int src = 0; src < nr; ++src ) { UNIFORM_BUFFER_DESC& desc = pdescs[src]; if ( desc.name == element.name ) { element.source_offset = offset; element.size = desc.size; break; } else { offset += desc.size; } } } // determine total buffer size that needs to be allocated glGetActiveUniformBlockiv(mProgram, mBlock, GL_UNIFORM_BLOCK_DATA_SIZE, &mBufferSize); return true; }
void RendererBasePimpl::InitDomainUniformBufferDecl(ShaderProgram * resource, unsigned int domain, GLuint program) { if (glGetActiveUniformBlockiv) { char uniformblock_name[1000]; GLint name_length; GLint block_binding; GLint block_data_size; GLint block_active_uniforms; GLint uniformblock_count; GLint referenced_by_vertex_shader; GLint referenced_by_geometry_shader; GLint referenced_by_fragment_shader; glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &uniformblock_count); for (int i = 0; i < uniformblock_count; ++i) { glGetActiveUniformBlockName(program, i, 1000, &name_length, uniformblock_name); GLint params; glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_BINDING, &block_binding); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_DATA_SIZE, &block_data_size); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_NAME_LENGTH, ¶ms); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &block_active_uniforms); std::vector<GLuint> uniform_indices(block_active_uniforms, 0); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, reinterpret_cast<GLint*>(&uniform_indices[0])); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER, &referenced_by_vertex_shader); glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER, &referenced_by_fragment_shader); std::vector<GLuint> uniform_offsets(block_active_uniforms, 0); glGetActiveUniformsiv(program, block_active_uniforms, &uniform_indices[0], GL_UNIFORM_OFFSET, reinterpret_cast<GLint*>(&uniform_offsets[0])); std::vector<GLuint> uniform_arraystrides(block_active_uniforms, 0); glGetActiveUniformsiv(program, block_active_uniforms, &uniform_indices[0], GL_UNIFORM_ARRAY_STRIDE, reinterpret_cast<GLint*>(&uniform_arraystrides[0])); std::vector<GLuint> uniform_matrixstrides(block_active_uniforms, 0); glGetActiveUniformsiv(program, block_active_uniforms, &uniform_indices[0], GL_UNIFORM_MATRIX_STRIDE, reinterpret_cast<GLint*>(&uniform_matrixstrides[0])); std::vector<GLuint> uniform_rowmajor(block_active_uniforms, 0); glGetActiveUniformsiv(program, block_active_uniforms, &uniform_indices[0], GL_UNIFORM_IS_ROW_MAJOR, reinterpret_cast<GLint*>(&uniform_rowmajor[0])); std::cout << "UniformBlock: " << uniformblock_name << "[" << i << "]" << std::endl; std::cout << " Size: " << block_data_size << std::endl; std::cout << " UniformCount: " << block_active_uniforms << std::endl; for (unsigned int p = 0; p < uniform_indices.size(); ++p) { std::cout << " Param: " << uniform_indices[p] << std::endl; std::cout << " Offset: " << uniform_offsets[p] << std::endl; std::cout << " ArrayStrides: " << uniform_arraystrides[p] << std::endl; std::cout << " MatrixStrides: " << uniform_matrixstrides[p] << std::endl; std::cout << " RowMajor: " << uniform_rowmajor[p] << std::endl; } //std::cout << " Location: " << glGetUniformLocation(pimpl->combined, uniformblock_name) << std::endl; } } }
/* * * Core in: * * OpenGL : 2.0 * OpenGLES : 3.0 */ void rglGetActiveUniformBlockiv(GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint *params) { #if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) && defined(HAVE_OPENGLES3) glGetActiveUniformBlockiv(program, uniformBlockIndex, pname, params); #else printf("WARNING! Not implemented.\n"); #endif }
static void setup_ubos(void) { static const char *names[NUM_UBOS] = { "ub_pos_size", "ub_color", "ub_rot" }; int i; glGenBuffers(NUM_UBOS, buffers); for (i = 0; i < NUM_UBOS; i++) { GLint index, size; /* query UBO index */ index = glGetUniformBlockIndex(prog, names[i]); /* query UBO size */ glGetActiveUniformBlockiv(prog, index, GL_UNIFORM_BLOCK_DATA_SIZE, &size); printf("UBO %s: index = %d, size = %d\n", names[i], index, size); /* Allocate UBO */ glBindBuffer(GL_UNIFORM_BUFFER, buffers[i]); glBufferStorage(GL_UNIFORM_BUFFER, size, NULL, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_DYNAMIC_STORAGE_BIT); piglit_check_gl_error(GL_NO_ERROR); ubos[i] = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); piglit_check_gl_error(GL_NO_ERROR); if (!ubos[i]) piglit_report_result(PIGLIT_FAIL); /* Attach UBO */ glBindBufferBase(GL_UNIFORM_BUFFER, i, buffers[i]); glUniformBlockBinding(prog, index, i); if (!piglit_check_gl_error(GL_NO_ERROR)) piglit_report_result(PIGLIT_FAIL); } }
int GL3ProgramObjectProvider::get_uniform_buffer_size(int block_index) const { throw_if_disposed(); OpenGL::set_active(); if (!glGetActiveUniformBlockiv) throw Exception("incorrect OpenGL version"); GLsizei uniformBlockSize = 0; glGetActiveUniformBlockiv(handle, block_index, GL_UNIFORM_BLOCK_DATA_SIZE, &uniformBlockSize); return uniformBlockSize; }
/* void glGetActiveUniformBlockName ( GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName ) */ static jstring android_glGetActiveUniformBlockName_II (JNIEnv *_env, jobject _this, jint program, jint uniformBlockIndex) { GLint len = 0; glGetActiveUniformBlockiv((GLuint)program, (GLuint)uniformBlockIndex, GL_UNIFORM_BLOCK_NAME_LENGTH, &len); GLchar* name = (GLchar*)malloc(len); glGetActiveUniformBlockName((GLuint)program, (GLuint)uniformBlockIndex, len, NULL, name); jstring result = _env->NewStringUTF(name); free(name); return result; }
static void setup_ubos(void) { static const char *names[NUM_UBOS] = { "ub_pos_size", "ub_color", "ub_rot" }; int i; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment); printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment); if (test_buffer_offset) { printf("Testing buffer offset %d\n", alignment); } else { /* we use alignment as the offset */ alignment = 0; } glCreateBuffers(NUM_UBOS, buffers); for (i = 0; i < NUM_UBOS; i++) { GLint index, size; /* query UBO index */ index = glGetUniformBlockIndex(prog, names[i]); /* query UBO size */ glGetActiveUniformBlockiv(prog, index, GL_UNIFORM_BLOCK_DATA_SIZE, &size); printf("UBO %s: index = %d, size = %d\n", names[i], index, size); /* Allocate UBO */ glNamedBufferData(buffers[i], size + alignment, NULL, GL_DYNAMIC_DRAW); /* Attach UBO */ glBindBufferRange(GL_UNIFORM_BUFFER, i, buffers[i], alignment, /* offset */ size); glUniformBlockBinding(prog, index, i); if (!piglit_check_gl_error(GL_NO_ERROR)) piglit_report_result(PIGLIT_FAIL); } }
void GLshader::print_block_names() { GLint numBlocks; GLint nameLen; GLint block_binding; GLint block_size; GLint ref_vertex; GLint ref_fragment; std::vector <std::string> name_list; glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &numBlocks); name_list.reserve(numBlocks); std::cout << "Found " << numBlocks << " block(s) in shader" << std::endl; for (int blockIx = 0; blockIx < numBlocks; blockIx++) { glGetActiveUniformBlockiv(program, blockIx, GL_UNIFORM_BLOCK_NAME_LENGTH, &nameLen); std::vector <GLchar> name; name.resize(nameLen); glGetActiveUniformBlockName(program, blockIx, nameLen, NULL, &name[0]); name_list.push_back(std::string()); name_list.back().assign(name.begin(), name.end() - 1); //Remove the null terminator. glGetActiveUniformBlockiv(program, blockIx, GL_UNIFORM_BLOCK_BINDING, &block_binding); glGetActiveUniformBlockiv(program, blockIx, GL_UNIFORM_BLOCK_DATA_SIZE, &block_size); glGetActiveUniformBlockiv(program, blockIx, GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER, &ref_vertex); glGetActiveUniformBlockiv(program, blockIx, GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER, &ref_fragment); std::cout << "Name: " << name.data() << " block binding: " << block_binding << " block size: " << block_size << std::endl; std::cout << "ref vertex: " << ref_vertex << ", ref fragment: " << ref_fragment << std::endl; } for (unsigned int il = 0; il < name_list.size(); il++) { std::cout << "Block name: " << name_list[il] << ", index: " << get_block_index(name_list[il]) << std::endl; } }
void TransformBlock::initialize() { transformBlockIndex = glGetUniformBlockIndex(shaderProgram, "TransformBlock"); glGetActiveUniformBlockiv(shaderProgram, transformBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &transformBlockSize); glUniformBlockBinding(shaderProgram, transformBlockIndex, transformBlockBindingPoint); GLuint uniformIndeces[3] = { 0 }; GLint uniformOffsets[3] = { 0 }; const GLchar * charStringNames[] = { "TransformBlock.modelMatrix", "TransformBlock.viewingMatrix", "TransformBlock.projectionMatrix", }; // Get the indeces of the uniforms. glGetUniformIndices(shaderProgram, 3, (const GLchar **)charStringNames, uniformIndeces); // Get the offsets of the uniforms glGetActiveUniformsiv(shaderProgram, 3, uniformIndeces, GL_UNIFORM_OFFSET, uniformOffsets); cout << "uniform transformblock" << endl; for (int i = 0; i < 3; i++) { cout << uniformOffsets[i] << endl; } modelMatrixLoc = uniformOffsets[0]; viewingMatrixLoc = uniformOffsets[1]; projectionMatrixLoc = uniformOffsets[2]; if (!bufferMade) { bufferMade = true; glGenBuffers(1, &transformBlockBuffer); cout << "TransformBlock Buffer ID " << transformBlockBuffer << endl; glBindBuffer(GL_UNIFORM_BUFFER, transformBlockBuffer); glBufferData(GL_UNIFORM_BUFFER, transformBlockSize, NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, transformBlockBindingPoint, transformBlockBuffer); } }