int 21h
int 21h

Reputation: 21

How to check if a resource has finished uploading to the GPU

I'm developing an app with a big 3D world where meshes/textures are uploaded and immediately drawn when you move to a new area. The simplified code for rendering a frame looks like this (I'm using Dawn):

// Start frame
commandEncoder = wgpuDeviceCreateCommandEncoder(device, nullptr);

// Create and upload mesh
vertexBuffer = wgpuDeviceCreateBuffer(device, &desc);
wgpuQueueWriteBuffer(queue, vertexBuffer, 0, vertices, desc.size);

// Start render pass
// ...

// Draw mesh
wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 0, vertexBuffer, 0, desc.size);
wgpuRenderPassEncoderDraw(renderPassEncoder, numElements, 1, firstElement, 0);

// End render pass
// ...

// Submit queue and end frame
auto commandBuffer = wgpuCommandEncoderFinish(commandEncoder, nullptr);
wgpuQueueSubmit(queue, 1, &commandBuffer);
wgpuCommandBufferRelease(commandBuffer);
wgpuCommandEncoderRelease(commandEncoder);
wgpuSwapChainPresent(swapchain);

The problem is that (if I understand correctly how the GPU works) the GPU would stall between the upload and the draw until the mesh has finished uploading.

I checked with RenderDoc how this is translated by Dawn to actual D3D12 commands and there is a ResourceBarrier between the CopyBufferRegion and the DrawInstanced which would leave the pipeline waiting until the resource transitions from D3D12_RESOURCE_STATE_COPY_DST to D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER.

I thought of using onSubmittedWorkDone to detect from the CPU if the upload has finished and wait to send any draw commands until then. However the documentation in that link suggests that it shouldn't be used except in two very particular cases (none of them is the one here), which leads me to think I'm on the wrong path.

Upvotes: 2

Views: 110

Answers (0)

Related Questions