Skip to content

Surely it shouldn't take 5-15ms to get a trivial result back from GPU with mapAsync? #4432

Closed

Description

@kjpgit

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.

Here is a live repro: https://karl-pickett.dev/gpu-repro-1/index.html
The performance.now() timings are printed to the JS console. No rendering is being done, a black screen is intentional. I'm simply binning 8000 shapes in the GPU and returning a histogram.

This is the relevant code in main.js:

    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.

Activity

  1. conorpo commented on Dec 31, 2023

    @conorpo

    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"
    87916ea8c4c7bf7483a388560de728ea

  2. kjpgit commented on Dec 31, 2023

    @kjpgit
    Author

    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.

  3. conorpo commented on Dec 31, 2023

    @conorpo

    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?

        fps_monitor;
        average_map_time = {
            total: 0,
            count: 0,
        }
        average_map_time_ring = {
            total: 0,
            count: 0,
        }
        constructor() {
    const misc_buffer_cpu = device.createBuffer({
        label: "misc_buffer_cpu",
        size: constants.MISC_BUFFER_SIZE,
        usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
    });
    
    const misc_buffer_cpu_ring = [];
            const perf_gpu_before = performance.now();
            encoder.copyBufferToBuffer(misc_buffer_gpu, 0, misc_buffer_cpu, 0, 4096);
            const buffer_to_map = misc_buffer_cpu_ring.length ? misc_buffer_cpu_ring.pop() : device.createBuffer({
                label: "misc_buffer_cpu",
                size: constants.MISC_BUFFER_SIZE,
                usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
            });
            encoder.copyBufferToBuffer(misc_buffer_gpu, 0, buffer_to_map, 0, misc_buffer_gpu.size);
    
            device.queue.submit([encoder.finish()]);
            misc_buffer_cpu.mapAsync(GPUMapMode.READ).then(() => {
                const perf_gpu_end = performance.now();
                main.average_map_time.total += perf_gpu_end - perf_gpu_before;
                main.average_map_time.count += 1;
    
                const result = new Uint32Array(misc_buffer_cpu.getMappedRange());
                misc_buffer_cpu.unmap();
            }); // mapAsync callback end
    
            buffer_to_map.mapAsync(GPUMapMode.READ).then(() => {
                const perf_gpu_end = performance.now();
                main.average_map_time_ring.total += perf_gpu_end - perf_gpu_before;
                main.average_map_time_ring.count += 1;
    
                const result = new Uint32Array(buffer_to_map.getMappedRange());
                buffer_to_map.unmap();
                misc_buffer_cpu_ring.push(buffer_to_map);
            });
    
            if(main.num_frames % 500 == 499) {
                console.log(`average_map_time: ${main.average_map_time.total / main.average_map_time.count}`);
                console.log(`average_map_time_ring: ${main.average_map_time_ring.total / main.average_map_time_ring.count}`);
            }
  4. kjpgit commented on Dec 31, 2023

    @kjpgit
    Author

    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.

    image

  5. kjpgit commented on Dec 31, 2023

    @kjpgit
    Author

    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.

  6. kjpgit commented on Dec 31, 2023

    @kjpgit
    Author

    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.

  7. gyagp commented on Jan 1, 2024

    @gyagp

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions