Results 1 to 2 of 2

Thread: how to read atomic counter value using shader storage buffer

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    3

    Question how to read atomic counter value using shader storage buffer

    I want to access atomic counter value in my code. My shader is

    #version 430
    #extension GL_EXT_compute_shader : enable
    layout (local_size_x = 16, local_size_y = 16) in;
    layout (binding = 0) uniform atomic_uint atRed;
    layout(std140,binding = 0) buffer myBuffer
    {
    uint red;
    uint inc_red;
    }g_out;

    void main() {
    g_out.inc_red=0;
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);
    g_out.inc_red+=atomicCounterIncrement(atRed);

    g_out.red=atomicCounterIncrement(atRed);


    }

    In my access i am accessing these values using following code

    GLuint* counter=NULL;
    glGenBuffers(1, &shaderStorageBufferID);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, shaderStorageBufferID);
    glBufferData(GL_SHADER_STORAGE_BUFFER, 2*sizeof(GLuint), NULL ,GL_STATIC_DRAW);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0,shaderStorageBufferID );
    glUseProgram(tdk_ShaderObject.cpsId);
    glDispatchCompute(16,16,1);
    counter = (GLuint*) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0,2*sizeof(GLuint), GL_MAP_READ_BIT );
    printf("counter %d %d",*counter,*(counter+1));


    glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

    It is giving counter values as 0 0.
    Is there anything wrong in above code?

  2. #2
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    3
    This issue is solved now
    Solution is

    GLuint* counters=(GLuint*)tdkMalloc(sizeof(GLuint));
    GLuint* counter;
    GLuint* counter1;
    *counters=1000;
    glGenBuffers(1, &atomicsBuffer);
    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicsBuffer);

    glUseProgram(cpsId);

    glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint) * 1,counters, GL_STATIC_DRAW);
    glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicsBuffer);
    GLuint shaderStorageBufferID;

    glGenBuffers(1, &shaderStorageBufferID);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, shaderStorageBufferID);
    glBufferData(GL_SHADER_STORAGE_BUFFER, 1*sizeof(GLuint), NULL ,GL_STATIC_READ);
    int buffer_index=tdk_glGetProgramResourceIndex(tdk_Sha derObject.cpsId, GL_SHADER_STORAGE_BLOCK, "myBuffer", nResult);
    printf("buffer_index = %d", buffer_index);
    glShaderStorageBlockBinding(tdk_ShaderObject.cpsId , buffer_index, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0,shaderStorageBufferID);
    glDispatchCompute(1,1,1,nResult);
    counter = (GLuint*) glMapBufferRange(GL_ATOMIC_COUNTER_BUFFER, 0,1*sizeof(GLuint), GL_MAP_READ_BIT|GL_MAP_WRITE_BIT );


    printf("counter %d",*counter);


    counter1 = (GLuint*) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0,1*sizeof(GLuint), GL_MAP_READ_BIT|GL_MAP_WRITE_BIT );
    Printf("counter %d",*counter1);
    tdk_glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER,nResult );
    tdk_glUnmapBuffer(GL_SHADER_STORAGE_BUFFER,nResult );
    both counter and counter1 are same

Similar Threads

  1. Replies: 4
    Last Post: 08-03-2017, 01:01 PM
  2. What's the point of Atomic Counters when we have Storage Blocks?
    By Betrayal in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-09-2015, 03:57 PM
  3. shader storage buffer objects within geometry shader
    By minidrive in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-14-2014, 08:43 AM
  4. atomic counter buffer mapping
    By lecrab in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 11-18-2012, 02:21 AM
  5. Atomic access to Shader Storage Buffers fails linking
    By danielkr in forum OpenGL: GLSL
    Replies: 7
    Last Post: 11-02-2012, 07:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean