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?
Material
andPointLight
? If you use thestd140
layout for this instead of using the implementation-defined packing rules, then I can tell you right off the batuPointLight
is misaligned. Otherwise, I am going to have to take your word that you queried the correct offset foruPointLight
before trying to store anything in yourperFrame
UBO because you have shown no such code. – Andon M. Coleman Apr 16 at 23:04