WebGPU 1.0: Query Result Timing for Indirect Draw Pagination
0 reputation · 15 Sept 2023, 21:04 UTC
0 reputation · 15 Sept 2023, 21:04 UTC
To bound a large vertex dataset by counting visible primitives with an occlusion query and then use the result to drive an indirect draw call for pagination.
WebGPU 1.0 defines GPUQuerySet for occlusion and timestamp queries, but the spec does not specify when a query result can be read after a render pass finishes. Implementations vary: some browsers require an explicit GPUQueue.submit followed by GPUQueue.onSubmittedWorkDone or waitForIdle before mapReadAsync resolves, while others allow mapping immediately after the pass ends. This ambiguity directly affects whether the query buffer must be resolved before encoding an indirect draw command or can be resolved concurrently, which is critical for efficient pagination.
According to the WebGPU 1.0 specification, a query result buffer can be read only after all GPU work that writes to that buffer has finished. This means the application must submit the command encoder that recorded the render pass and then wait for the queue to become idle before calling mapReadAsync on the query buffer.
GPUQuerySet result becomes available when the corresponding GPUCommandBuffer that contains the query has completed execution.mapReadAsync) is allowed only when the buffer is not in use by the GPU; otherwise the call is rejected or yields undefined data.Some browsers internally flush the command buffer when a render pass ends, making the query result appear immediately mappable. This behavior is not guaranteed by the spec and relying on it can lead to intermittent failures or stale data on other implementations.
GPUQuerySet with one occlusion query and a matching GPUBuffer for the result.GPUQueue.submit.await queue.onSubmittedWorkDone() (or queue.waitForIdle()).queryBuffer.mapReadAsync, await the promise, then copy the mapped value into a readable ArrayBuffer (or use getMappedRange).Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.