Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using OpenGL with JOGL and want to use two unfirorm blocks in my vertex and pixel shaders. The following GLSL-Code shows the definitions:-

uniform perObject
{
    Material uMaterial;
    mat4 uWorld;
    mat4 uWorldViewProj;
};

uniform perFrame
{
    vec3 uEyePosW;
    PointLight uPointLight;
};

My Java code looks like this:

perObjectIndex = gl.glGetUniformBlockIndex(shaderProgram, "perObject");
gl.glUniformBlockBinding(shaderProgram, perObjectIndex, 0);

perFrameIndex = gl.glGetUniformBlockIndex(shaderProgram, "perFrame");
gl.glUniformBlockBinding(shaderProgram, perFrameIndex, 1);

gl.glGenBuffers(2, uniformBufferHandles, 0);
perObjectBufferHandle = uniformBufferHandles[0];    
perFrameBufferHandle = uniformBufferHandles[1]; 

gl.glBindBuffer(GL3.GL_UNIFORM_BUFFER, perObjectBufferHandle);
gl.glBufferData(GL3.GL_UNIFORM_BUFFER, perObjectBuffer.capacity(), perObjectBuffer, GL3.GL_DYNAMIC_DRAW);
gl.glBindBufferBase(GL3.GL_UNIFORM_BUFFER, 0, perObjectBufferHandle);

gl.glBindBuffer(GL3.GL_UNIFORM_BUFFER, perFrameBufferHandle);
gl.glBufferData(GL3.GL_UNIFORM_BUFFER, perFrameBuffer.capacity(), perFrameBuffer, GL3.GL_DYNAMIC_DRAW);
gl.glBindBufferBase(GL3.GL_UNIFORM_BUFFER, 1, perFrameBufferHandle);

However, if I remove the code for the perFrameBlock, everything seems to work very fine. In a draw method I update the data in the following way:

gl.glBindBuffer(GL3.GL_UNIFORM_BUFFER, perObjectBufferHandle);
gl.glBufferSubData(GL3.GL_UNIFORM_BUFFER, 0, perObjectBuffer.capacity(), perObjectBuffer);

All the data is properly transferred to the GPU. But if I want to use both uniform blocks, something strange seems to happen. Using glGetActiveUniformsiv with GL_UNIFORM_OFFSET it looks like the two blocks are merged in an alphabetic way depending on the variable names.

I thought I can deal with both blocks independently, provide one ByteBuffer for each block and transfer data to the corresponding block by invoking glBindBuffer and glBufferSubData.

How can I achieve my goal?

share|improve this question
    
Where is the definition for Material and PointLight? If you use the std140 layout for this instead of using the implementation-defined packing rules, then I can tell you right off the bat uPointLight is misaligned. Otherwise, I am going to have to take your word that you queried the correct offset for uPointLight before trying to store anything in your perFrame UBO because you have shown no such code. –  Andon M. Coleman Apr 16 at 23:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.