I have written an erosion simulator that I want to execute from the editor (for generating terrain and such outside of play mode). Unfortunately, when I crank the parameters on my simulation, unity hangs. The UI becomes unresponsive, none of the menus work anymore. However, the process is still running - windows doesn't claim that the program is no longer responding.
The weird thing is that task manager claims that the GPU is sitting at zero utilization while Unity hangs. My (CPU side) erosion code looks something like this (executed via editor script):
HydraulicCompute.SetFloat("_ErosionRadius", ErosionRadius);
HydraulicCompute.SetBuffer(_erosionId, "HeightMap", heightMap);
HydraulicCompute.SetBuffer(_erosionId, "Droplets", _dropletBuffer);
int threadGroupsX = Mathf.CeilToInt(mapSize / 8.0f);
int threadGroupsY = Mathf.CeilToInt(mapSize / 8.0f);
for (int i = 0; i < 100; i++) {
HydraulicCompute.Dispatch(_dropletId, Mathf.CeilToInt(_dropletBuffer.count / 1024.0f), 1, 1);
HydraulicCompute.Dispatch(_erosionId, threadGroupsX, threadGroupsY, 1);
}
When the size of the _dropletBuffer
is bigger than 12,000 (32 bytes per droplet), and the heightMap
buffer larger than 1024x1024
(4 bytes per pixel), then I run into the aforementioned problem.
I would guess that the issue occurs either because there is a problem with the buffer sizes, or that the frame takes too long and Unity times out.
When I comment out the Dispatch
calls Unity is able to allocate and populate the buffers instantaneously (at least it looks really quick). While that doesn't rule out the potential buffer problem, it certainly makes it seem less likely.
So what exactly is the problem here? Is it a memory problem? Or is the frame timing out? Or something else entirely?
Is there a prescribed way to doing this kind of intensive GPU work in the background in editor mode? I've looked at the Job system, but that seems to be CPU side only...
[EDIT]
I have managed to put together the following snippet which always crashes on my GPU (NVIDIA 980ti):
[numthreads(1,1,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
for (int i = 0; i < _NumIterations; i++) {
HeightMap[1024*512 + 512] = 0;
}
}
When executed as 1 thread group for 1 iteration (_NumIterations = 1
), this crashes my GPU driver. I am running on the latest version. I have no idea as to what might be causing the problem, windows event viewer just lists a nvlddmkm
error, which apparently means it could be just about anything. It's frustrating to say the least.