You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Surely it shouldn't take 5-15ms to get a trivial result back from GPU with mapAsync? #4432
I can file a browser bug against chrome if necessary, but I wanted to make sure I'm not doing something stupid here.
According to PIX, my cpu dispatch is taking 50 microseconds, and the buffer copies to and from GPU are taking about 20 microseconds. Overall, it's on the order of 0.1 milliseconds.
But the time it takes to get the mapAsync() promise result is 5-15 milliseconds later. About a 50x difference in latency. This happens on intel and nvidia.
computePass.end();
encoder.copyBufferToBuffer(misc_buffer_gpu, 0, misc_buffer_cpu, 0, 4096)
device.queue.submit([encoder.finish()]);
main.log_perf(`queue submitted`);
misc_buffer_cpu.mapAsync(GPUMapMode.READ).then(() => {
main.log_perf(`got results back and mapped`);
const result = new Uint32Array(misc_buffer_cpu.getMappedRange());
if (main.debug_show_histogram) {
main.log_perf(`histogram ${main.scene.get_histogram(result)}`);
}
misc_buffer_cpu.unmap()
})
There's enormous latency before that mapAsync callback, and it's not explained by PIX. I'm targeting 60FPS, and having most of my frametime budget used to read a 4KB response back from the GPU is unworkable. I even tried getting this read result back while my first rendering pass is running (i.e. overlapping work), but that doesn't seem to change anything.
Maybe this is a device issue? I'm running your live demo and there is never more than a 3ms difference between "queue submitted" and "got results back and mapped"
Thanks for looking at it. On my fast nvidia gpu I get 1-5ms. But I don't see why it would ever be more than 1 ms, and I don't know why it should even fluctuate on my 3060ti. This is a trivial binning step that is happening, it's not the "real work". I've checked on two machines, and my laptop with intel igpu is in the 10ms (!) range.
Does it start out good than get slower? Maybe since you only have 1 buffer that is being mapped/unmapped asynchronously, its causing a stall. Try this version of main.js, it implements a simple buffer ring. See if there is a performance difference on the slower gpu. I could be wrong but I'm pretty sure copying data to and from the CPU is always gonna be pretty slow, its the main bottleneck for any kind of application like this. Not exactly sure how your project works but if you are saying you need to get data from the GPU, process it on the CPU, then feed some result to the GPU for the next frame, you're gonna have some performance issues. If you simply need to get the data from the GPU every frame, and the next frame is independent of the result, then what's wrong with displaying the histogram a bit late?
I'll give that a try, but in the mean time I updated the repro. It now doesn't kick off any compute shaders at all, it merely uploads 8K of uniform data to the GPU and reads back 4K of storage with mapasync. And it is killing my igpu! The cpu goes to 90% and the GPU is at 30%. This makes no sense at all, because I'm using requestanimationFrame(). And my nvidia is also going to 40%!
To answer your high level question -- the GPU is generating a lot of fine particles. I want to send a histogram of that data (A very small 4K array of counts) to the CPU, in the same frame, so the CPU can be smart about how to dispatch the next set of rasterizer shaders.
For example, I don't know how many additional compute passes I would need. I know I can use dispatchIndirect to have a compute shader start another set of shaders, but not if they need multiple passes (for example, due to running out of memory). I.E., I think only the CPU could do something like
for (i = 0; i < num_raster_passes_needed; i++) { dispatchRasterCompute(10, 10, 10) }
So I don't know if I NEED this capability, but I strongly suspect most AAA games are able to get some data from the GPU inside a drawing pass and still stay in 60 fps, especially if they are overlapping other compute work.
Activity
conorpo commentedon Dec 31, 2023
Maybe this is a device issue? I'm running your live demo and there is never more than a 3ms difference between "queue submitted" and "got results back and mapped"

kjpgit commentedon Dec 31, 2023
Thanks for looking at it. On my fast nvidia gpu I get 1-5ms. But I don't see why it would ever be more than 1 ms, and I don't know why it should even fluctuate on my 3060ti. This is a trivial binning step that is happening, it's not the "real work". I've checked on two machines, and my laptop with intel igpu is in the 10ms (!) range.
conorpo commentedon Dec 31, 2023
Does it start out good than get slower? Maybe since you only have 1 buffer that is being mapped/unmapped asynchronously, its causing a stall. Try this version of main.js, it implements a simple buffer ring. See if there is a performance difference on the slower gpu. I could be wrong but I'm pretty sure copying data to and from the CPU is always gonna be pretty slow, its the main bottleneck for any kind of application like this. Not exactly sure how your project works but if you are saying you need to get data from the GPU, process it on the CPU, then feed some result to the GPU for the next frame, you're gonna have some performance issues. If you simply need to get the data from the GPU every frame, and the next frame is independent of the result, then what's wrong with displaying the histogram a bit late?
kjpgit commentedon Dec 31, 2023
I'll give that a try, but in the mean time I updated the repro. It now doesn't kick off any compute shaders at all, it merely uploads 8K of uniform data to the GPU and reads back 4K of storage with mapasync. And it is killing my igpu! The cpu goes to 90% and the GPU is at 30%. This makes no sense at all, because I'm using requestanimationFrame(). And my nvidia is also going to 40%!
https://karl-pickett.dev/gpu-repro-1/index.html
I also updated the stats so they look nice. Just search 'fps' in the console to see 1-second stats.
kjpgit commentedon Dec 31, 2023
To answer your high level question -- the GPU is generating a lot of fine particles. I want to send a histogram of that data (A very small 4K array of counts) to the CPU, in the same frame, so the CPU can be smart about how to dispatch the next set of rasterizer shaders.
For example, I don't know how many additional compute passes I would need. I know I can use dispatchIndirect to have a compute shader start another set of shaders, but not if they need multiple passes (for example, due to running out of memory). I.E., I think only the CPU could do something like
for (i = 0; i < num_raster_passes_needed; i++) { dispatchRasterCompute(10, 10, 10) }
So I don't know if I NEED this capability, but I strongly suspect most AAA games are able to get some data from the GPU inside a drawing pass and still stay in 60 fps, especially if they are overlapping other compute work.
kjpgit commentedon Dec 31, 2023
Even when I commented out all possible work (mapAsync, copyBuffer, clearBuffer) I am still seeing high latency before onSubmittedWorkDone() fires. (3-10ms on intel, 1-3ms on nvidia). I filed a chrome bug. https://bugs.chromium.org/p/chromium/issues/detail?id=1514901
So the issue isn't even related to reading anything back from the GPU.
gyagp commentedon Jan 1, 2024
This is related to browser implementation. For Chrome, a known issue (against Dawn) is tracked at https://bugs.chromium.org/p/dawn/issues/detail?id=1335.
However, the fix is not easy. Google is actively working on WGPUFuture (https://bugs.chromium.org/p/dawn/issues/detail?id=1987), which might be a good solution to fix this.
README: add gpuweb #4432 cite for the 5-15ms claim, note JS Atomics a…