Background
So I’m creating a procedural terrain generation in which all the information is continuously fed through compute shaders and stored between compute buffers.
I’ve managed to process everything so far without ever reading back to the CPU until the mesh is generated at the very end. I read-back the mesh asynchronously at the very end for multiple reasons(collision mesh, memory, etc).
However, I want to keep some data on the GPU, such as geometry shader data, density maps so shaders can use them, etc. Because I’m doing everything through compute shaders, I cannot know the exact size of the mesh or constituent parts necessary for its generation (structure data, helper maps, etc.), so I’m always wasting memory because I have to create compute buffers at their maximum possible size.
Resultantly, I’ve created ~2-3 compute buffers of a collective 3.5 GB in size, to which I’m copying all my data to. These buffers are managed similar to the Heap in memory and are quite complicated in nature, but they are persisted throughout the scene.
So for every chunk, I’m progressively creating and releasing compute buffers, copying some of their data to these persisted buffers, and then releasing these temporary buffers. These temporary buffers can take up to ~0.75-1 GB at a time before they are released.
Problem
That leads into my problem. I’m experiencing some sort of memory corruption inside these persisted buffers. It is not always apparent, and has only appeared in certain cases. One of these examples is when I create and release medium size buffers rapidly through frames, have some sort of memory leak, or a shader compiles with errors.
I believe the problem is Memory Fragmentation on the GPU. I assume that what is happening is that, for example, when creating rapid medium sized buffers and releasing them, I fragment the memory, and when a new temporary buffer cannot find an empty space, it overwrites the memory in the persistent buffers.
In another case, I’m using a persisted compute buffer to bake the SunOpticalDepth in calculating inScattering, but there are frames where it produces gibberish while the terrain generates. The compute buffer is never released or re-created so this phenomenon is really mysterious. I believe one reason of this could be because the GPU is trying to rearrange the memory because it can’t find space for new data, but I’m not sure.
Could something like this happen? How are compute buffers on the GPU physically being sorted? Should I ask an OpenGL thread?