How to attach one more shader to remove primitives?

Hello :slight_smile:,

I would like to adapt some existing code. As I am not experienced in OpenGL so I might overlooked something. I went through the basics with

Short description of what the code does at the moment:
Code is part of a real-time application. This part copies the gpu buffer to an extra gpu buffer. The content of this extra buffer is stored in an array in normal memory.

My Problem:
Several tens of thousands of primitives are emitted. I only need a few primitives with special characteristics.

My train of thought:
Add a vertex shader and geometry shader. The vert shader just passes the primitives. The geometry shader emits only primitives which are interesting.

That’s how the code is at the moment:

// copy buffer to extra buffer which is only used for output
glBindBuffer(GL_COPY_READ_BUFFER, source);
glBindBuffer(GL_COPY_WRITE_BUFFER, storeToArray);

glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, numOfPrimitives * primitivesize);
glGetBufferSubData(GL_COPY_WRITE_BUFFER, 0, numOfPrimitives * primitivesize, array);

My questions:

  1. Do I need to tell OpenGL the Attributes as they are given to source? If yes, where do I that?
  2. Where do I need to call glUseProgram(filterPrimitives)?
  3. How do I get the number of primitives after filterPrimitives to allocate the correct size of the array later? Maybe with glGenQueries() …

I’m assuming that the data was generated using transform feedback. If the data was captured by a single operation (bracketed with glBeginTransformFeedback and glEndTransformFeedback), you can render the captured data using glDrawTransformFeedback; this doesn’t require an explicit query to determine the number of vertices.

A geometry shader can be used to filter the data. This approach essentially replaces the glCopyBufferSubData call with a rendering operation: setting up the attribute state, any uniform state, setting the program, enabling transform feedback (with a different transform feedback object and buffers) and rendering the data, either with glDrawTransformFeedback or with glDrawArrays using a count obtained via a query.

Either way, you need to set up the attribute arrays to use the buffer(s) containing the captured data.

To obtain a count of the vertices generated by transform feedback, create a query object and bracket the rendering operation(s) with glBeginQuery and glEndQuery with a target of GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, then use glGetQueryObject to retrieve the value from the query object. The count stored in the transform feedback object and used by glDrawTransformFeedback is essentially hidden state; there’s no mechanism to obtain its value.

1 Like