OpenGL, How to use dynamic struct array in glsl with Shader Storage Buffer Object

48 Views Asked by At

This is fragment shader which is using SSBO

struct Light
{
    float range;
    vec4 position;  
    vec4 color;
};

layout (std430) buffer LocalLights
{
    uint activeLightCount;
    Light light[];
} localLights;

void main()
{
    for(int i = 0; i < localLights.activeLightCount; ++i)
    { //~~~~ doing lighting calc ~~~~/}
}

And this is the C++ data code for SSBO

struct Light
{
    float range;
    glm::vec4 position;
    glm::vec4 color;
};

struct LocalLightData
{
    unsigned int count;
    std::vector<Light> lights;
};

And this is Initialize C++ code (when Initialize, count is zero and lights vector is empty) +) data variable is the type of LocalLightData

unsigned int index = glGetProgramResourceIndex(shader->programId, GL_SHADER_STORAGE_BLOCK, "LocalLights");

glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(unsigned int) + sizeof(Light) * data.lights.size(), &data, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

And this is Update function (for updating dynamic size and data of vector)

glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(unsigned int) + sizeof(Light) * data.lights.size(), &data, GL_DYNAMIC_COPY);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(unsigned int), &data.count);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(unsigned int), sizeof(Light) * data.lights.size(), data.lights.data());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

I tired to use this code for updating dynamic size of lights, but it is not works.

For detail, the activeLightCount variable is working on shader, but Light light[] is not working.

What is the problem of my code? How can I fix this problem?

Use SSBO for making dynamic size of lights.

I want to fix the problem of my code.

0

There are 0 best solutions below