View Full Version : Get Shader Storage Buffer Object to work

09-09-2016, 07:29 AM
Hi at all! I'm trying to make a lighting system in my engine.
Basically, I want to have an arbitrary number of lights to pass to my fragment shader.
So, in my engine I have a Light class which contains the ambient and diffuse color, position and direction vector (just to start).
I'm want to use a SSBO.
This is how my GLSL snippet looks like:

struct Light
vec4 ambient;
vec4 diffuse;
vec3 position;
vec3 direction;

layout(std430, binding = 0) readonly buffer LightBuffer
int numLights;
Light lights[];

I used std430, because it doesn't need to be 16-byte aligned, right?

So, I need to pass the amount of lights and their properties.
This is basically how I'm doing it:

int contSize[1] = { (int) lights.size() };

// Calculate buffer range

// Pass number of lights
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(contSize), &contSize[0]);

// Fill the buffer with all the active lights
unsigned int j = 0;
for (auto i = lights.begin(); i != lights.end(); i++)
i->second->PassToBoundBuffer(sizeof(contSize[0]) + ((sizeof(glm::vec4) * 2 + sizeof(glm::vec3) * 2)) * j);

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);

And this is the PassToBoundBuffer method implementation:

void Light::PassToBoundBuffer(GLintptr offset)
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, sizeof(glm::vec4), &Properties.ambient[0]);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset + sizeof(glm::vec4), sizeof(glm::vec4), &Properties.diffuse[0]);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset + sizeof(glm::vec4) * 2, sizeof(glm::vec3), &Properties.position[0]);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset + sizeof(glm::vec4) * 2 + sizeof(glm::vec3), sizeof(glm::vec3), &Properties.direction[0]);

Offsets are correctly calculated, but in my shader nothing is passed.
numLights in LightBuffer is 0, because if I do something like this:

for (int i = 0; i < numLights; i++)
color.rgb = vec3(0.5);

Color doesn't change.
Am I missing something?
Why it doesn't work?
Thanks in advance for every advice!

09-09-2016, 01:35 PM
simple question:
did you actually allocate memory for your buffer ?
glBufferSubData (...) does not allocate memory, glBufferData (...) does