2

I am working on particle system. For calculation of each particle position, time alive and so on I use compute shader. I have problem to get count of dead particles back to the cpu, so I can set how many particles renderer should render. To store data of particles i use shader storage buffer. To render particles i use instancing. I tried to use atomic buffer counter, it works fine, but it is slow to copy data from buffer to the cpu. I wonder if there is some other option.

This is important part of compute shader

if (pData.timeAlive >= u_LifeTime)
    {
        pData.velocity = pData.defaultVelocity;
        pData.timeAlive = 0;
        pData.isAlive = u_Loop;
        atomicCounterIncrement(deadParticles)
        pVertex.position.x = pData.defaultPosition.x;
        pVertex.position.y = pData.defaultPosition.y;
    }

    InVertex[id] = pVertex;
    InData[id] = pData;

To copy data to the cpu i use following code

uint32_t* OpenGLAtomicCounter::GetCounters()
    {
        glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_AC);
        glGetBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(uint32_t) * m_NumberOfCounters, m_Counters);     

        return m_Counters;
    }
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • "slow to copy data from buffer to the cpu" it isn't slow. You must be stalling the pipeline. Read [how to stream data to the GPU](https://www.khronos.org/opengl/wiki/Buffer_Object_Streaming). The same applies for reading data back. – Yakov Galka May 22 '20 at 15:00
  • Thank you for your answer. Unfortunatelly streaming data is quite complex solution for my problem i believe. And streaming just one uint would be overkill. I found different solution "indirect drawing" . If i understood correctly i can skip copying to the cpu and share values related to draw command on the gpu. – Šimon Gido May 23 '20 at 09:25
  • Indirect drawing is indeed the way to go. Unfortunately you didn't say what you needed the counters for in the question. In the future please provide a full description of the problem. – Yakov Galka May 23 '20 at 15:52
  • Actually i mentioned that I need to setup number of particles in renderer, i believe my intentions were obvious. Thank you anyway – Šimon Gido May 23 '20 at 18:59

0 Answers0