I've run into an issue while attempting to use SSBOs as follows:
GLuint lightSSBO;
glGenBuffers(1, &lightSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, lightSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) + sizeof(LightData) * 10, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(int), &lightCount);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(int), sizeof(LightData) * lights.size(), &lights[0]);
void* ptr = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
std::cout << "lightCount value = " << *(int*)ptr << "\n";
std::cout << "First Light: \n";
ptr = (int*)ptr + 1;
std::cout << "\tType: " << *(unsigned int*)ptr << "\n";
ptr = (int*)ptr + 1;
std::cout << "\tPosition: " << vec4ToString((glm::vec4*)ptr) << "\n";
ptr = (float*)ptr + 4;
std::cout << "\tDirection: " << vec4ToString((glm::vec4*)ptr) << "\n";
ptr = (float*)ptr + 4;
std::cout << "\tColour: " << vec4ToString((glm::vec4*)ptr) << "\n";
ptr = (float*)ptr + 4;
std::cout << "\tSize: " << *(float*)ptr << "\n";
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
In the code excerpt above, ptr outputs the correct values I've assigned to lightCount and to lights[0], meaning the buffer ostensibly has the correct data. However, when I try to access beyond lightCount on the GPU (Lights[0]), I get incorrect values. For example, Lights[0].Colour simply returns (0,0,0,?), despite displaying correctly as (1,0.2,0.2,1) throught ptr above.
struct LightData
{
uint Type;
vec4 Position;
vec4 Direction;
vec4 Colour;
float Size;
};
layout (std430, binding = 0) buffer LightBuffer
{
int LightCount;
LightData[] Lights;
};
void main()
{
FragColor = Lights[0].Colour;
}
And the output of the console for completeness: (The correct values)
lightCount value = 1
First Light:
Type: 1
Position: 0.000000, 0.000000, 0.000000, 1.000000
Direction: -0.707107, 0.000000, -0.707107, 1.000000
Colour: 1.000000, 0.200000, 0.200000, 1.000000
Size: 0
Note: Changing the shader colour to be Position is still not correct and does not change if I change the underlying data, indicating that the Light array is either not making it to the GPU or I'm accessing it incorrectly somehow.
Does anyone know why this is happening? Additionally, I'm confused as to the meaning/options for glBufferData's final GLenum parameter, even https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_storage_buffer_object.txt doesn't seem to mention it.