PDA

View Full Version : Compute shaders not working properly



Gefest
12-04-2015, 06:42 AM
I just started learning about compute shaders and openGL 4.3+ functionality.
Following some tutorials I've implemented a simple atomic counter

shader code

#version 430
layout (local_size_x = 1) in;
layout (binding = 1, offset = 0) uniform atomic_uint counter;

void main() {
atomicCounterIncrement(counter);
}

GL

void init(){
glGenBuffers(1, &counterBuffer);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, counterBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint) * 1, NULL, GL_STATIC_DRAW);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 1, counterBuffer);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);
}


void onEachFrame(){
shaderProgram->Use();
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_ATOMIC_COUNTER_BARRIER_BIT);
//now read the counter
GLuint *Counter;
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, counterBuffer);
Counter = (GLuint*)glMapBufferRange(GL_ATOMIC_COUNTER_BUFFER ,0,sizeof(GLuint) * 1,GL_MAP_READ_BIT);
printf("counter: %i \n", Counter[0]);
glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER);
/* alternative way
GLuint Counter[1];
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, counterBuffer);
glGetBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(GLuint) * 1, Counter);
printf("counter: %i \n", Counter[0]);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);
*/
}

That is working fine and giving expected output;
1
2
3 etc

However, if I add another shader pass, first one stops working. Am I missing something ?
Second compute shader (not connected in any way)

#version 430
layout (local_size_x = 1) in;
layout(std430, binding = 2) readonly buffer SomeData
{
uint data[];
};

void main() {
data; //just trying to access data
}

Buffer initialization code

glGenBuffers(1, &dataSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, dataSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, 1 * sizeof(GLuint), 0, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, dataSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

updated loop

void onEachFrame(){
shaderProgram->Use();
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_ATOMIC_COUNTER_BARRIER_BIT);
//now read the counter
GLuint *Counter;
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, counterBuffer);
Counter = (GLuint*)glMapBufferRange(GL_ATOMIC_COUNTER_BUFFER ,0,sizeof(GLuint) * 1,GL_MAP_READ_BIT);
printf("counter: %i \n", Counter[0]);
glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER);

if(Counter[0] > 5 && secondDispatch){
secondDispatch = false;
secondShaderProgram->Use();
glDispatchCompute(1, 1, 1);
}
}

After the second dispatch(even once), first one just stops incrementing the counter.
Both shaders compile fine.
Output is following
1
2
3
4
5
6
6
6
6
6
6

Can somebody explain WHY??? second shader program is not in any way connected to the first one, they don't have any shared data.
Moreover, if I add "data accessing" part to first shader, everything is again working fine, which is weird.

Gefest
12-04-2015, 03:52 PM
Never mind. I've tested other applications . It's a driver bug. Too bad my GPU cannot run openGL version 4.3 (at least with newest drivers), so no fancy opengGL compute shaders and shader storage buffers for me..

vickyjohns
12-04-2015, 07:22 PM
I'm having bug OpenGL version 4.3