<p>The complexity of this makes it seem like you don't really want to use transform feedback; you want to use a <em>compute shader</em>. Then, through <a href="https://www.khronos.org/opengl/wiki/Indirect_Rendering" rel="nofollow noreferrer">indirect rendering</a> and <a href="https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object" rel="nofollow noreferrer">SSBOs</a>, you can write whatever vertex commands data you want.</p>

<p>The idea is that you have some number of indirect rendering commands (one command per set of instances). And for each instance you create, the CS invocation needs to bump the instance count of each of those commands (atomically, of course).</p>

<p>Now, because there are state changes between the commands, you wouldn't be able to use a multi-draw indirect command. But by using indirect commands and a CS, you can at least keep all of the data on the GPU, without having to do things like buffer copies to multiple locations.</p>

<p>Transform feedback is merely an ad-hoc mechanism for something that modern OpenGL allows you to just do directly. If you need to broadcast the "primitive" count to multiple locations (or really, if you're doing anything besides rendering the feedbacked data), then you should be using a compute shader, not TF.</p>

CC BY-SA 3.0
2018-01-25T18:18:00.657Z
Thanks, and I guess it's reasonable advice. It just seemed too elaborate and I don't like to have entirely different approaches for 3 and 4 hardware when it's just a seemingly minor hurdle.
Christian Rau
CC BY-SA 3.0
2018-01-25T15:07:37.197Z
2018-01-25T18:13:29.840Z
2018-01-25T18:13:29.840Z
Nicol Bolas
<p>When using <a href="https://www.khronos.org/opengl/wiki/Transform_Feedback" rel="nofollow noreferrer">Transform Feedback</a> to capture the vertices generated by the primtive generation stage, we can use <code>glDrawTransformFeedback</code> to directly draw the generated data without the need for querying the number of generated primitives and thus <strong>without CPU-GPU synchronization</strong>, which is especially useful with geometry shaders generating a possibly unknown number of primitives.</p>

<p>However, in my case I'm not using the generated primitive data per-vertex, but <strong>per-instance</strong> (it's basically a classic GPU-based instance culling scenario). So I need to <strong>supply the number of generated primitives into the instance count parameter of <code>glDraw...Instanced</code></strong>. But the various <a href="https://www.khronos.org/opengl/wiki/Vertex_Rendering#Transform_feedback_rendering" rel="nofollow noreferrer">Transform Feedback rendering functions</a> only ever use the primitive count as vertex count and never as instance count. Now this <em>does</em> require me to wait for the transform feedback to finish and get the result of the corresponding query object, thus instroducing a synchronization point.</p>

<p>I considered that I could also use the quite powerful <a href="https://www.khronos.org/opengl/wiki/Vertex_Rendering#Indirect_rendering" rel="nofollow noreferrer">Indirect Rendering</a> functionality and together with a <a href="https://www.khronos.org/opengl/wiki/Query_Object#Query_buffer_object" rel="nofollow noreferrer">Query Buffer Object</a> route the result of the transform feedback query directly into the instance count field of the draw indirect buffer. But this has the problem that I'm <strong>not only issuing a single <code>glDraw...Instanced</code> call, but multiple different ones</strong> (each with their own state, per-vertex attributes and draw parameters, but all with the same instance count and per-instance attributes). So I would actually have to scatter the query result into multiple drawing parameter sets (though, possibly in the same buffer), probably by use of a compute shader. But this just seems like overkill to me.</p>

<p>So my question is, <strong>is there any other less involved way to just use the number of primitives/vertices generated from transform feedback directly as the instance count in (preferably multiple different) instanced rendering commands without the need to explicitly query that count onto the CPU?</strong> In light of GPU-based scene culling techniques I can't imagine this to be a rare use-case (but maybe in that case you just have to go the way over indirect drawing commands anyway). I'm not afraid of using extensions if available, so maybe there's some obscure little extension that does exactly that? Or I'm just missing something else obvious here.</p>

<opengl><synchronization><transform-feedback>
Directly use Transform Feedback primitive count as instance count for drawing
Christian Rau
# Directly use Transform Feedback primitive count as instance count for drawing

(Asked by: username_0 on 2018-01-25)

When using [Transform Feedback](https://www.khronos.org/opengl/wiki/Transform_Feedback) to capture the vertices generated by the primtive generation stage, we can use `glDrawTransformFeedback` to directly draw the generated data without the need for querying the number of generated primitives and thus **without CPU-GPU synchronization**, which is especially useful with geometry shaders generating a possibly unknown number of primitives.

However, in my case I'm not using the generated primitive data per-vertex, but **per-instance** (it's basically a classic GPU-based instance culling scenario). So I need to **supply the number of generated primitives into the instance count parameter of `glDraw...Instanced`**. But the various [Transform Feedback rendering functions](https://www.khronos.org/opengl/wiki/Vertex_Rendering#Transform_feedback_rendering) only ever use the primitive count as vertex count and never as instance count. Now this *does* require me to wait for the transform feedback to finish and get the result of the corresponding query object, thus instroducing a synchronization point.

I considered that I could also use the quite powerful [Indirect Rendering](https://www.khronos.org/opengl/wiki/Vertex_Rendering#Indirect_rendering) functionality and together with a [Query Buffer Object](https://www.khronos.org/opengl/wiki/Query_Object#Query_buffer_object) route the result of the transform feedback query directly into the instance count field of the draw indirect buffer. But this has the problem that I'm **not only issuing a single `glDraw...Instanced` call, but multiple different ones** (each with their own state, per-vertex attributes and draw parameters, but all with the same instance count and per-instance attributes). So I would actually have to scatter the query result into multiple drawing parameter sets (though, possibly in the same buffer), probably by use of a compute shader. But this just seems like overkill to me.

So my question is, **is there any other less involved way to just use the number of primitives/vertices generated from transform feedback directly as the instance count in (preferably multiple different) instanced rendering commands without the need to explicitly query that count onto the CPU?** In light of GPU-based scene culling techniques I can't imagine this to be a rare use-case (but maybe in that case you just have to go the way over indirect drawing commands anyway). I'm not afraid of using extensions if available, so maybe there's some obscure little extension that does exactly that? Or I'm just missing something else obvious here.

**Tags:** opengl, synchronization, transform-feedback


---

## 1 answers

### Answer by username_1 on 2018-01-25. Score: 2 | Accepted answer

The complexity of this makes it seem like you don't really want to use transform feedback; you want to use a *compute shader*. Then, through [indirect rendering](https://www.khronos.org/opengl/wiki/Indirect_Rendering) and [SSBOs](https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object), you can write whatever vertex commands data you want.

The idea is that you have some number of indirect rendering commands (one command per set of instances). And for each instance you create, the CS invocation needs to bump the instance count of each of those commands (atomically, of course).

Now, because there are state changes between the commands, you wouldn't be able to use a multi-draw indirect command. But by using indirect commands and a CS, you can at least keep all of the data on the GPU, without having to do things like buffer copies to multiple locations.

Transform feedback is merely an ad-hoc mechanism for something that modern OpenGL allows you to just do directly. If you need to broadcast the "primitive" count to multiple locations (or really, if you're doing anything besides rendering the feedbacked data), then you should be using a compute shader, not TF.

> **Comments:**
> **username_0**: Thanks, and I guess it's reasonable advice. It just seemed too elaborate and I don't like to have entirely different approaches for 3 and 4 hardware when it's just a seemingly minor hurdle.  

---

