1
$\begingroup$

My first question in this community.

I have following code.The code is shortened for clarity, error checks skipped:

int creat_ssbo_buf(uint32_t *ssbo, size_t size, size_t unit_size){
  glGenBuffers(1, ssbo);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, *ssbo);
  glBufferData(GL_SHADER_STORAGE_BUFFER, 
               size * unit_size, 
               NULL, 
               GL_DYNAMIC_READ);
  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, *ssbo);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
  return 0;
}

int read_ssbo(uint32_t  ssbo,
              void     *pbuf,
              size_t    size,
              intptr_t  off)
{
 glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER,
                    off, 
                    size, 
                    pbuf);
 glBindBuffer( GL_SHADER_STORAGE_BUFFER, 0 );
 return 0;
}

int compute(...){ /* runs the GLSL code */
 ...
 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
 return 0;
}

int main(){
 ...
 size_t buf_size = w * h;     
 uint32_t *pbuf = /* some raster image with the size of above */;
 uint32_t  ssbo;

 creat_ssbo_buf(&ssbo, buf_size, sizeof(uint32_t));
 compute(...);
 read_ssbo(ssbo, pbuf, buf_size, 0);
 
 printf(" 0 : %u\n", pbuf[0]);
 printf(" 1 : %u\n", pbuf[1]);
 printf(" 2 : %u\n", pbuf[2]);
 printf(" 3 : %u\n", pbuf[3]);
 printf(" 4 : %u\n", pbuf[4]);

 return 0;
}

With the GLSL code as below.

layout(std430, binding = 0) writeonly buffer layout0
{
 uint data[];
};

void main(){
 uvec2 pos  = uvec2(gl_GlobalInvocationID.xy);

 if(pos == uvec2(0,0)){
  data[0] = pos.x;
  data[1] = pos.y;
  data[2] = 1;
  data[3] = 1;
  data[4] = 1;
 }
};

Now that outputs 0,0,1,1,1 as expected. What I want is to read from buffer with some offset. Below is a C and GLSL code with changes:

/* GLSL */
layout(std430, binding = 0) writeonly buffer layout0
{
 uint pad[1]; /* add some padding */
 uint data[];
};

/* C */
...
size_t buf_size = w * h + 1;
...
read_ssbo(ssbo, pbuf, buf_size, sizeof(uint32_t)); /* note the offset */
...

And with the offset added, I get an error: invalid value. The error fires up after glGetBufferSubData call. What am I doing wrong here?

P.S Also, if offset is left as zero and the indices of pbuf in the printf's are changed to one index greater, the output is correct.

Ask away what code I should add to make things clearer.

$\endgroup$
1
  • 1
    $\begingroup$ Resolved. As per documentation: GL_INVALID_VALUE is generated if offset or size is negative, or if offset+size is greater than the value of GL_BUFFER_SIZE for the buffer object. The offset + size is greater than the buffer size. $\endgroup$ Jan 8 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.