英文:
Using the same buffer as input and output for a compute shader
问题
希望这是一个非常简单的问题!我有一个粒子系统,我正在用计算着色器进行更新(只是更新一个包含位置数据的缓冲区)。目前我正在从一个输入缓冲区读取并写入到不同的输出缓冲区(这将成为下一帧的输入缓冲区)。但我想知道,由于每次更新都在读取/写入相同的槽位,是否可能更有效,甚至可能更新原地而不必来回创建两个缓冲区。
我怀疑答案是“不,这是一个坏主意”,因为我确信在缓存、内存访问顺序等方面存在一系列未定义的行为,但我想知道是否有任何可能的方法可以像这样原地操作缓冲区,因为这样可以节省额外的内存(如果更快的话,那就更好了)。
英文:
Hopefully a very simple question! I have a particle system that I'm updating with a compute shader (just updating a buffer with position data). Right now I'm reading from an input buffer and writing to a different output buffer (that will be the input buffer next frame). But I was wondering, since each update is reading/writing to the same slot, whether it might be more efficient, or even possible, to update the buffer in-situ rather than having to create two and ping-pong back and forth.
I suspect the answer here is "no, this is a bad idea", since I'm sure there's a whole host of undefined behaviors around caches, memory access ordering, etc, but I thought it was worth asking if there's any approach that could work on a buffer in-place like this, since it would be nice to save that extra memory (and if it's faster!).
答案1
得分: 2
如果每个单独的着色器调用仅触及缓冲区中自己的字节,而不是其他人的... 那就没问题。可以随心所欲地读取/修改/写入。该调用甚至可以读取刚刚写入的数据,而且对该调用可见。无需特殊的同步。
不用担心此处相邻读取的缓存问题,这不是你的责任,而是 GPU 的责任。只要每个调用从其他所有调用中读取不同的内存,就没问题。也就是说,你不需要了解缓存行大小。
现在,你确实需要适当的同步,以便进一步使用该数据的任何进程。但无论如何,你都需要那个。
英文:
If each individual shader invocation is only touching its own bytes in the buffer and nobody elses... then you're fine. Do read/modify/writes to your heart's content. That invocation can even read the data it just wrote, and it will be visible to that invocation. No special synchronization required.
And it's not your job to worry about cache issues regarding neighboring reads here. That's the GPU's job. As long as each invocation is reading disjoint memory from all others, there is no problem. That is, you don't need to know the cache line size.
Now, you do need appropriate synchronization for whatever process is going to further use that data. But you were going to need that anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论