Corbie
Corbie

Reputation: 1066

Modifying a Shader Storage Buffer Object from within a Vertex Shader

In my base program (C++/OpenGL 4.5) I have copied the content of the Vertex Buffer to an Shader Storage Buffer (SSBO):

float* buffer = (float*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo[2]);

glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat)*size,buffer, GL_DYNAMIC_DRAW);

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, 0);
glUnmapBuffer(GL_ARRAY_BUFFER);

In the Vertex Shader this data is bound to an array:

#version 430
#extension GL_ARB_shader_storage_buffer_object : require

layout(shared, binding = 3) buffer storage
{
    float array[];
}

But when I'm trying to overwrite an array entry in the main function like this:

array[index_in_bounds] = 4.2;

nothing happens.

What am I doing wrong? Can I change the buffer from within the Vertex Shader? Is this only possible in a Geometry Shader? Do I have to do this with Transform Feedback (that I have never used before)?

edit: I'm mapping the buffers for test purposes in my main program, just to see if the data changes:

float* buffer = (float*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
float* ssbo = (float*) glMapNamedBuffer(3, GL_READ_ONLY);

for(int i = 0; i < SIZE_OF_BUFFERS; i++)
    printf("% 5f | % 5f\n", ssbo[i], buffer[i]);

glUnmapNamedBuffer(3);
glUnmapBuffer(GL_ARRAY_BUFFER);

Upvotes: 2

Views: 2296

Answers (1)

Corbie
Corbie

Reputation: 1066

Okay, I have found the problem using the red book. I have not bound the buffer correctly and binding the buffer base has to happen after buffering the data:

float* buffer = (float*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, &ssbo); // bind buffer

// switched these two:
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat)*size,buffer, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, &ssbo);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); // unbind buffer
glUnmapBuffer(GL_ARRAY_BUFFER);

Upvotes: 3

Related Questions