1. What is buffer mapping
Without giving a definition, simply put, after mapping (Mapping) a certain piece of video memory, it can be accessed by the CPU.
After the Buffer (meaning video memory) of the three graphics APIs (D3D12, Vulkan, Metal) is mapped, the CPU will be able to access it, and at this point, note that the GPU can still access this piece of video memory. This leads to a problem: IO conflict, which needs to be considered by the program.
WebGPU disables this behavior and instead passes “ownership” to represent the mapped state, rather like the Rust philosophy. At each moment, the CPU and GPU access the memory unilaterally, thus avoiding competition and conflicts.
GPUBuffer mapping method is an asynchronous method.
However, the unmapping operation is a synchronous operation, and the CPU can be unmapped when it runs out.
Note that the
mapAsync method will directly press an operation into the device’s default queue inside the WebGPU, which acts on the queue timeline of the three main timelines in the WebGPU. And memory is only incremented after a successful mapAsync (actually tested).
The data on the memory is committed to the GPU only after the instruction buffer is committed to the queue (a GPUBuffer is used for one of the rendering channels of this instruction buffer) (guess).
I didn’t see any significant memory reduction after calling the
destroy method because I didn’t have much room to test it, so I hope some of you can test it.
1.1 Create time mapping
You can pass
mappedAtCreation: true when creating the buffer, so you don’t even need to declare its usage with
2. Flow of buffered data
2.1 CPU to GPU
The most common examples of this are passing the VertexBuffer, UniformBuffer, and StorageBuffer needed to compute the channel for each frame, etc.
Writing buffer objects using the
writeBuffer method of the queue object is very efficient, but
writeBuffer has an extra copy operation compared to the mapped GPUBuffer used for writing. Presumably this affects performance, although there are many writeBuffer operations in the officially recommended examples, mostly for UniformBuffer updates.
2.2 GPU to CPU
Such reverse passes are relatively rare, but not unheard of. For example, screenshots (to save color attachments to ArrayBuffer), result statistics of computation channels, etc., need to get data from the GPU’s computation results.
For example, the official example of getting pixel data from a rendered texture.