Hey everyone, I'm trying to implement my shader system and I was previously hardcoding uniforms but I want to go for a more dynamic approach. I found out about Uniform Buffers. So as I understand, what you want is to potentially have a uniform buffer for the Camera and its properties, a uniform buffer for a potential material, etc. Here is the issue I'm running into.
I have a shader program class and in the shader program class, I want it to store a list of all uniform buffer attributes inside the actual program. So basically, each uniform buffer attribute would tell you the number of indices, the buffer name, the buffer byte size, the offsets, the names of the indices, etc. The problem here is that the buffer index in the program isn't predetermined (if it can be, it still would cause issues since you would have to make sure index 1 is the same for all shader programs). So I wanted to get an output array after linking that would state all the uniform buffer names in the program. From there, I could query all the details I need for each uniform buffer and put that into a Uniform Buffer Attribute class.
When rendering, the Renderable object would have its list of uniform buffers and would check to see if the properties of each uniform buffer in the renderable match that which is in the shader program that it is using to render itself. If true, it would bind it over and if not, I would get an warning.
Unfortunatly, I couldn't find a way online to query the names of all uniform buffers in a shader program. Instead, everyone puts in constant strings since they know before hand what uniform buffers are in the shaders. This doesn't work well since I want my c++ program to easily switch between shader programs/ uniform buffers for each renderable.
An issue that I see with my approach as well is that the same uniform buffer can have different binding indexes for different programs which could remove the benefit of not having to rebind the uniform buffer after switching shader programs.
I'm not sure if there is some general way around this but I couldn't find a answer online. I was looking a lot at this tutorial (http://www.gamedev.net/page/resources/_/technical/opengl/opengl-40-using-uniform-blocks-and-uniform-buffer-objects-r2860) and other links on different sites but they all use glGetUniformBlockIndex with a constant uniform buffer name. Here's how my code looks for my uniform buffer attribute:
class GLUniformBufferAttributes
{
private:
GLuint m_BufferSize;
std::string m_BufferName;
std::vector <GLuint> m_UniformOffsetArray;
std::vector <std::string> m_UniformNameArray;
public:
GLUniformBufferAttributes ();
~GLUniformBufferAttributes ();
bool SetAttributes (GLuint bufferSize, std::string & bufferName, const std::vector<GLuint> & offsetArray,
const std::vector<std::string> & nameArray);
};
My uniform buffer class is sparse and probably has errors since I haven't tried populating it yet but instead getting the querying of attributes to work properly first. As of now, I'm following this link (http://gamedev.stackexchange.com/questions/48926/opengl-fetching-the-names-of-all-uniform-blocks-in-your-program) which does something similar with the index's. The query buffer name should probably be done before the loop but I couldn't find a function to do it. Here's how I'm trying to query the data in my shader program class after linknig:
class GLShaderProgram
{
private:
GLuint m_ShaderProgId;
std::vector <GLUniformBufferAttributes> m_UniformAttributeArray;
};
bool GLShaderProgram::InitializeUniformBlocks ()
{
// Get all uniform buffer specifications
GLint numUniformBlocks; // Get number of uniform buffers
glGetProgramiv(m_ShaderProgId, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
m_UniformAttributeArray = std::vector <GLUniformBufferAttributes> (numUniformBlocks, GLUniformBufferAttributes());
for ( unsigned int index = 0; index < numUniformBlocks; index++ ) // unsafe unsigned vs signed int mistmatch
{
// Query buffer size
GLint blockSize;
glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
// Query buffer name
GLchar bufferName;
// Query number of uniforms
GLint numUniforms;
glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_BLOCK_NAME_LENGTH, &numUniforms);
// Query the names of uniforms
std::vector <GLchar> nameArray;
nameArray.resize(numUniforms);
glGetActiveUniformBlockName(m_ShaderProgId, index, numUniforms, nullptr, &nameArray[0]);
// Query the offsets of uniforms
std::vector <GLint> offsetArray;
glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_OFFSET, &offsetArray[0]);
//m_UniformAttributeArray[index].SetAttributes(bufferSize, bufferName, offsetArray, nameArray);
}
return true;
}
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TexCoords;
uniform TestBlock
{
vec4 v1;
float r1;
};
uniform mat4 gWorld;
out vec2 TexCoord;
void main()
{
gl_Position = gWorld * vec4(Position, 1.0);
TexCoord = TexCoords;
}
Another note, I have a uniform defined in my vertex shader which itself executes properly, however I'm assuming the index loop is messed up since when it gets the uniform buffer at index 0, it pulls out a completely different uniform buffer which I'm assuming is the default one in shader programs since it has weird variable names and what not. It has 10 variables in it and they're names are single letters + numbers. Thanks in advance.