2

I am generating a buffer (accessed as a SSBO) in a fragment shader during draw call #1. Then I'd like to use that buffer (accessed as a VBO) as the input for draw call #2.

The problem is that, using the function void glDrawArrays(GLenum mode, GLint first, GLsizei count), I should know the count value on the CPU. But on the CPU I don't know that value. The value is only stored in the GPU memory as an atomic counter storage.

Is there any way to make a draw call using as count argument a value stored in the GPU memory?

I know that I could retrieve the value from the GPU and then use it in CPU, but that would slow down the program because of synchronization.

BDL
  • 21,052
  • 22
  • 49
  • 55
Tarquiscani
  • 649
  • 7
  • 20

1 Answers1

6

For such scenarios, glDrawArraysIndirect in combination with a GL_INDIRECT_DRAW_BUFFER can be used.

You can use the already existing SSBO as an indirect draw buffer, but you have to make sure to follow the DrawArraysIndirectCommand structure and make sure that count is stored on the first four bytes of the SSBO. All other data can be set once and don't have to be accessed through the SSBO.

typedef  struct {
   GLuint  count;
   GLuint  instanceCount;
   GLuint  first;
   GLuint  baseInstance;
} DrawArraysIndirectCommand;
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
BDL
  • 21,052
  • 22
  • 49
  • 55