Re: Screen Capture using `CopyTextureToBuffer `. WGPUBufferMapAsyncStatus set to error.

Skip to first unread message

Kai Ninomiya

Feb 2, 2023, 2:17:35 PM2/2/23
to Jean-Colas Prunier, Dawn Graphics

-Kai (he/they)

On Thu, Feb 2, 2023 at 7:54 AM Jean-Colas Prunier <> wrote:
I wanted to test ` CopyTextureToBuffer` functionality but have totally failed so far. I have been using the code below, which I copied for the most part from `CopyCommandValidationTests`. 

When I look at the value of `WGPUBufferMapAsyncStatus` in SaveToFile the callback function called by  ` MapAsync` I constantly get an error message.

- I create a basic texture (empty)
- Create a buffer to get copy the data to
- use  CopyTextureToBuffer for the cmd
- push the command to the queue.
- call  MapAsync on the buffer so that it calls SaveToFile once the command has been fully executed.

I would appreciate your input as to what I need to do better here (if that's obvious) or how I should be doing it. Thank you.

void CaptureScreen()
    uint64_t offset = 0;
    uint32_t bytesPerRow = sizeof(unsigned char) * newSize.width * 3;
    uint32_t rowsPerImage = newSize.height;
    // these 2 are globals
    bufferSize = BufferSizeForTextureCopy(newSize.width, newSize.height, 1);
    destination = CreateBuffer(bufferSize, wgpu::BufferUsage::CopyDst);
In order to map this buffer later, it must be created with the MapRead usage as well (wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead).

MapAsync should have generated a textual error message in the form of a validation error. If you haven't, you should set up a handler to print uncaptured validation errors, like so:;l=291;drc=52cc4f5d1c2840f0ac05cdfed87e9167df6607fb

    wgpu::ImageCopyBuffer imageCopyBuffer = CreateImageCopyBuffer(destination, offset, bytesPerRow, rowsPerImage);

    wgpu::Texture source = Create2DTexture(device, wgpu::TextureDimension::e2D,
        kColorFormat, newSize.width, newSize.width, 1, 1, 1);

    wgpu::ImageCopyTexture imageCopyTexture = CreateImageCopyTexture( source , 1, {0, 0, 0});
    wgpu::Extent3D extent3D = {width, height, 1};

    wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
    encoder.CopyTextureToBuffer(&imageCopyTexture, &imageCopyBuffer, &extent3D);
    wgpu::CommandBuffer commands = encoder.Finish();
    device.GetQueue().Submit(1, &commands);

    destination.MapAsync(wgpu::MapMode::Read, 0, bufferSize, SaveToFile, nullptr);

You received this message because you are subscribed to the Google Groups "Dawn Graphics" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Kai Ninomiya

Feb 2, 2023, 2:18:52 PM2/2/23
to Jean-Colas Prunier, Dawn Graphics
Sorry, I just saw there was already a reply to this. Somehow the `backticks` in the subject line caused the thread to fork.
-Kai (he/they)

Reply all
Reply to author
0 new messages