title: UAV counter indices used across multiple shaders?
body: <p>I've been trying to implement a Compute Shader based particle system.</p>
  
  <p>I have a compute shader which builds a structured buffer of particles, using a UAV with the D3D11_BUFFER_UAV_FLAG_COUNTER flag.</p>
  
  <p>When I add to this buffer, I check if this particle has any complex behaviours, which I want to filter out and perform in a separate compute shader. As an example, if the particle wants to perform collision detection, I add its index to another structured buffer, also with the D3D11_BUFFER_UAV_FLAG_COUNTER flag.</p>
  
  <p>I then run a second compute shader, which processes all the indices, and applies collision detection to those particles.</p>
  
  <p>However, in the second compute shader, I'd estimate that about 5% of the indices are wrong - they belong to other particles, which don't support collision detection.</p>
  
  <p>Here's the compute shader code that perfroms the list building:</p>
  
  <pre><code>// append to destination buffer
  uint dstIndex = g_dstParticles.IncrementCounter();
  g_dstParticles[ dstIndex ] = particle;
  
  // add to behaviour lists
  if ( params.flags &amp; EMITTER_FLAG_COLLISION )
  {
      uint behaviourIndex = g_behaviourCollisionIndices.IncrementCounter();
      g_behaviourCollisionIndices[ behaviourIndex ] = dstIndex;
  }
  </code></pre>
  
  <p>If I split out the "add to behaviour lists" bit into a separate compute shader, and run it after the particle lists are built, everything works perfectly. However I think I shouldn't need to do this - it's a waste of bandwidth going through all the particles again.</p>
  
  <p>I suspect that <code>IncrementCounter</code> is actually not guaranteed to return a unique index into the UAV, and that there is some clever optimisation going on that means the index is only valid inside the compute shader it is used in. And thus my attempt to pass it to the second compute shader is not valid.</p>
  
  <p>Can anyone give any concrete answers to what's going on here? And if there's a way for me to keep the filtering inside the same compute shader as my core update?</p>
  
  <p>Thanks!</p>