OpenGL - Calling glBindBufferBase with index = 1 breaks rendering (Pitch black)
Asked Answered
D

1

6

There's an array of uniform blocks in my shader which is defined as such:

layout (std140) uniform LightSourceBlock
{
    int shadowMapID;
    int type;
    vec3 position;
    vec4 color;

    float dist;

    vec3 direction;
    float cutoffOuter;
    float cutoffInner;
    float attenuation;
} LightSources[12];

To be able to bind my buffer objects to each LightSource, I've bound each uniform to a uniform block index:

for(unsigned int i=0;i<12;i++)
    glUniformBlockBinding(program,locLightSourceBlock[i],i); // locLightSourceBlock contains the locations of each element in LightSources[]

When rendering, I'm binding my buffers to the respective index using:

glBindBufferBase(GL_UNIFORM_BUFFER,i,buffer);

This works fine, as long as I only bind a single buffer to the binding index 0. As soon as there's more, everything is pitch black (Even things that use entirely different shaders). (glGetError returns no errors)

If I change the block indices range from 0-11 to 2-13 (Skipping index 1), everything works as it should. I figured if I use index 1, I'm overwriting something, but I don't have any other uniform blocks in my shader, and I'm not using glUniformBlockBinding or glBindBufferBase anywhere else in my code, so I'm not sure.

What could be causing such behavior? Is the index 1 reserved for something?

Despondent answered 1/2, 2014 at 13:4 Comment(0)
M
0

1) Dont use multiple blocks. Use one block with array. Something like this:

struct Light{
  ...
}
layout(std430, binding=0) uniform lightBuffer{
 Light lights[42];
}

skip glUniformBlockBinding and only glBindBufferBase to index specified in shader

2) Read up on alignment for std140, std430. In short, buffer variable are aligned so they dont cross 128bit boundaries. So in your case position would start at byte 16 (not 8). This results in mismatch of CPU/GPU side access. (Reorder variables or add padding)

Madonna answered 11/4, 2018 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.