简体   繁体   中英

OpenGL Compute Shader SSBO

I want a compute shader that writes 1's in the output buffer. I compile the shader and attach it to the program without problem, then I call glDispatchCompute() function and I wait until compute shader ends. But when I see the array, there are only 0's.

Can anyone tell me where is the mistake?

This is my compute shader code:

#version 430 core

layout  (local_size_x  =  2)  in;

layout(std430, binding=0) writeonly buffer Pos{
    float Position[];
};

void main(){
    Position[gl_GlobalInvocationID.x] = 1.0f;
}

And this is some of my main:

GLuint program_compute = 0, SSBO = 0;

//...(Create, compile and link the program with the shader)...

vector<GLfloat> initPos;
int num_numeros = 12;

for (int i = 0; i < num_numeros; i++){
    initPos.push_back(0.0f);
}

glUseProgram(program_compute);

glGenBuffers(1, &SSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_numeros * sizeof(GLfloat), &initPos, GL_DYNAMIC_DRAW);

glDispatchCompute(num_numeros/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);

for (int i = 0; i < num_numeros; i++){
    cout << "p" << i << ": " << initPos[i] <<  endl;
}
cout << endl;

EDIT: Finally it works. Thank you BDL.

As BDL has said, I've forgotten read the buffer back from GPU memory. Now it works. This is the new code:

GLuint program_compute = 0, SSBO = 0;

// ...The same code as above

glDispatchCompute(num_numeros/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);

GLfloat *ptr;
ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
initPos.clear();

for (int i = 0; i < num_numeros; i++){
    initPos.push_back(ptr[i]);
}

glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

for (int i = 0; i < num_numeros; i++){
    cout << "p" << i << ": " << initPos[i] <<  endl;
}
cout << endl;

EDIT: Thank you Andon M. Coleman.

I read from a write only buffer. Here is the line fixed:

ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);

glBufferData copies the content of initPos into the SSBO. The shader then operates on the buffer, not on the cpu memory array. Unless you read the buffer back from GPU to CPU memory somewhere, initPos will never change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM