Queue<AsyncGPUReadbackRequest> _requests = new Queue<AsyncGPUReadbackRequest>();
private byte[] writable_data;
var vectorCount = resolution.x * resolution.y * resolution.z;
const int bytesPerVector = 4 * 3;
const int extraBytes = 4 * 9;
writable_data = new byte[ vectorCount * bytesPerVector + extraBytes];
var x = BitConverter.GetBytes(resolution.x);
var y = BitConverter.GetBytes(resolution.y);
var z = BitConverter.GetBytes(resolution.z);
var mx = BitConverter.GetBytes(bounds.bounds.min.x);
var my = BitConverter.GetBytes(bounds.bounds.min.y);
var mz = BitConverter.GetBytes(bounds.bounds.min.z);
var Mx = BitConverter.GetBytes(bounds.bounds.max.x);
var My = BitConverter.GetBytes(bounds.bounds.max.y);
var Mz = BitConverter.GetBytes(bounds.bounds.max.z);
Array.Copy(x, 0, writable_data, 0, 4);
Array.Copy(y, 0, writable_data, 4, 4);
Array.Copy(z, 0, writable_data, 8, 4);
Array.Copy(mx, 0, writable_data, 12, 4);
Array.Copy(my, 0, writable_data, 16, 4);
Array.Copy(mz, 0, writable_data, 20, 4);
Array.Copy(Mx, 0, writable_data, 24, 4);
Array.Copy(My, 0, writable_data, 28, 4);
Array.Copy(Mz, 0, writable_data, 32, 4);
while (_requests.Count > 0)
var req = _requests.Peek();
Debug.Log("GPU readback error detected.");
var b = req.GetData<byte>();
fixed (byte* dest = writable_data)
Buffer.MemoryCopy(b.GetUnsafeReadOnlyPtr(),
using (var fs = File.OpenWrite(Application.streamingAssetsPath + "/FGA/" + fileName + $"_{cnt:0000}.fgabin"))
fs.Write(writable_data, 0, writable_data.Length);
public void WriteToFGA(ComputeBuffer cb)
if (Time.frameCount < startFrame)
if (Time.frameCount >= endFrame)
if (_requests.Count < 64)
_requests.Enqueue(AsyncGPUReadback.Request(cb));
Debug.Log("Too many requests.");