Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Compute shaders not working properly

  1. #1
    Newbie Newbie
    Join Date
    Dec 2015
    Posts
    2

    Question Compute shaders not working properly

    I just started learning about compute shaders and openGL 4.3+ functionality.
    Following some tutorials I've implemented a simple atomic counter

    shader code
    Code :
    #version 430
    layout (local_size_x = 1) in;
    layout (binding = 1, offset = 0) uniform atomic_uint counter;
     
    void main() {
    atomicCounterIncrement(counter);
    }

    GL
    Code :
    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)
    Code :
    #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
    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
    Code :
    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.
    Last edited by Gefest; 12-04-2015 at 09:12 AM.

  2. #2
    Newbie Newbie
    Join Date
    Dec 2015
    Posts
    2
    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..

  3. #3
    Newbie Newbie
    Join Date
    Dec 2015
    Posts
    1
    I'm having bug OpenGL version 4.3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •