OpenGL Compute Shader SSBO

不问归期 提交于 2019-12-18 17:12:21

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/32094598/opengl-compute-shader-ssbo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!