UBO Binding Issue

Hello everyone!

So I am having an issue with UBOs, to be more precise with binding them to the correct Uniform Block inside the shader.

First a few things about what I have checked:
The binding seems to be consistent with what I have defined with glUniformBlockBinding (I also tested it with shader binding and it also exposes the correct values) - I tested this via RenderDoc and

glGetActiveUniformBlockiv(shader_handle, program_resource_index, GL_UNIFORM_BLOCK_BINDING, &binding);

if I call

glBindBufferBase(GL_UNIFORM_BUFFER, binding, ubo_handle)

it does not bind to anything unless binding happens to equal an index of an uniform block. So now I obviously could use glBindBufferBase to just bind to every shader inside the system, but I specifically wanted to implement UBOs in order to reduce the Uniform write operations.

To go over the exact procedure: I compile and link the programs, use glUniformBlockBinding as “post-build-event”, then assemble the scene (this includes creating the UBOs and putting data in them) and then I render while swapping UBO when needed.

Lets assume I have a Shader with the following Uniform Blocks:
Light - index: 0 - binding: 1
Camera - index: 1 - binding: 10
Material - index: 2 - binding: 0

so I would expect that if I


GLuint ubo_handle = 123; //lets assume this handle is valid
glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo_handle)

, the light would now take the data from that ubo, instead what happens is that Camera now takes the data from this ubo 123.

I am using glfw 3.1.2 Win32 and glew 2.1.0, this is vendor independant and happens on multiple Windows OS, OpenGL 4.0 and GL_ARB_shading_language_420pack is available.

As essential values inside the shaders stay undefined I see my defined clear color instead of anything.

I usually just write myself wrapper classes for all those low level API calls and never touch them again if they work as expected. So I forget the exact details of every API function quiet fast. BUT:

Light - index: 0 - binding: 1
Camera - index: 1 - binding: 10
Material - index: 2 - binding: 0

glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo_handle)

If you want the light to use the UBO, shouldn’t the passed index be 0? You specified the index of the camera to be 1 and you pass this index.

[QUOTE=ProgrammerX;1291978]I usually just write myself wrapper classes for all those low level API calls and never touch them again if they work as expected. So I forget the exact details of every API function quiet fast. BUT:

If you want the light to use the UBO, shouldn’t the passed index be 0? You specified the index of the camera to be 1 and you pass this index.[/QUOTE]

This is precisely my problem, I do not want to Bind the UBO on the index, but rather the Block Binding so that every shader that has that block binding defined gets the camera and I don’t have to bind my camera per shader anymore.

glBindBufferBase() takes the binding point, not the uniform block index. There wouldn’t be much point to glUniformBlockBinding() (or binding points generally) otherwise.

I started to think I am chasing ghosts after checking with

glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, binding_index, &bound_buffer); 

Seems like I stumbled upon a bug in RenderDoc and then did an error myself when trying to query the bound buffers by using

glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, uniform_block_index, &bound_buffer); 

as I used low binding numbers (0,1,2)

This doesn’t change that inside renderdoc the values for camera are garbage even though the padding on 3 mat4 should be the simplest one… I need to further investigate for now

Yeah, as I said: Did it once. Worked. Forgot about it. :stuck_out_tongue: I just checked the documentation of glUniformBlockBinding and the parameter naming there is index. Only if you actually start reading what index is (what I didn’t) they talk about binding points. That’s where my confusion came from.
So I had to reread some stuff about UBOs and check some of my classes code to understand the problem

[QUOTE=Stefl1504;1291984]
This doesn’t change that inside renderdoc the values for camera are garbage even though the padding on 3 mat4 should be the simplest one… I need to further investigate for now[/QUOTE]

So I checked with a nightly build and identified where my data padding was wrong - indeed the combination of me querying wrong and the data display (not the capture) in the RenderDoc 1.0 build being wrong led me to believe that I was using glBindBufferBase wrong or forget to set something