eliemichel / LearnWebGPU-Code

The accompanying code of the Learn WebGPU C++ programming guide
https://eliemichel.github.io/LearnWebGPU
MIT License
106 stars 28 forks source link

[Question] How to MapRead an Index buffer ? #35

Closed JD31 closed 6 months ago

JD31 commented 6 months ago

I am in a case where I would like to read back an index buffer I pushed to GPU (and that I don't keep in RAM in order to reduce memory consumption). I follow the tutorial https://eliemichel.github.io/LearnWebGPU/basic-3d-rendering/input-geometry/playing-with-buffers.html.

I added the flag MapRead to the index buffer flags, but WebGPU rejects it: Error: Buffer usages (BufferUsage::(MapRead|CopyDst|Index)) is invalid. If a buffer usage contains BufferUsage::MapRead the only other allowed usage is BufferUsage::CopyDst.

What is the clean way to achieve this ? Thanks !

eliemichel commented 6 months ago

A map buffer is a bit different from other ones and as such, as the error message tells you, you cannot use it also for indexing. The only way is to have 2 buffers, 1 that has the Index | CopyDst | CopySrc usage, which is the one you use in the render pipeline, and 1 that has the CopyDst | MapRead usage, which is used for the round trop back to the CPU.

If you are worried about the overall amount of memory this takes, you may make the map buffer smaller and read back the index buffer chunk by chunk, or alternatively destroy the map buffer as soon as you are done with it!

JD31 commented 6 months ago

Thanks for the answer. Finally did a binary serialization to disk for the data so I can retrieve it when wanted. And thank you for this great book !