Skip to content

Commit a7660e4

Browse files
committed
webgpu: fix command buffer (+encoder) life-cycle (avoids breaks on multiple render passes)
1 parent 22e1f9b commit a7660e4

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

filament/backend/src/webgpu/WebGPUDriver.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,22 @@ void WebGPUDriver::endFrame(uint32_t frameId) {
292292
void WebGPUDriver::flush(int) {
293293
}
294294

295-
void WebGPUDriver::finish(int) {
295+
void WebGPUDriver::finish(int /* dummy */) {
296+
if (mCommandEncoder != nullptr) {
297+
// submit the command buffer thus far...
298+
assert_invariant(mRenderPassEncoder == nullptr);
299+
wgpu::CommandBufferDescriptor commandBufferDescriptor{
300+
.label = "command_buffer",
301+
};
302+
mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
303+
assert_invariant(mCommandBuffer);
304+
mQueue.Submit(1, &mCommandBuffer);
305+
mCommandBuffer = nullptr;
306+
// create a new command buffer encoder to continue recording the next command for frame...
307+
wgpu::CommandEncoderDescriptor commandEncoderDescriptor = { .label = "command_encoder" };
308+
mCommandEncoder = mDevice.CreateCommandEncoder(&commandEncoderDescriptor);
309+
assert_invariant(mCommandEncoder);
310+
}
296311
}
297312

298313
void WebGPUDriver::destroyRenderPrimitive(Handle<HwRenderPrimitive> rph) {
@@ -746,12 +761,7 @@ void WebGPUDriver::compilePrograms(CompilerPriorityQueue priority,
746761
}
747762

748763
void WebGPUDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassParams& params) {
749-
wgpu::CommandEncoderDescriptor commandEncoderDescriptor = {
750-
.label = "command_encoder"
751-
};
752-
mCommandEncoder = mDevice.CreateCommandEncoder(&commandEncoderDescriptor);
753764
assert_invariant(mCommandEncoder);
754-
755765
// TODO: Remove this code once WebGPU pipeline is implemented
756766
static float red = 1.0f;
757767
if (red - 0.01 > 0) {
@@ -781,14 +791,9 @@ void WebGPUDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
781791
params.viewport.width, params.viewport.height, params.depthRange.near, params.depthRange.far);
782792
}
783793

784-
void WebGPUDriver::endRenderPass(int) {
794+
void WebGPUDriver::endRenderPass(int /* dummy */) {
785795
mRenderPassEncoder.End();
786796
mRenderPassEncoder = nullptr;
787-
wgpu::CommandBufferDescriptor commandBufferDescriptor {
788-
.label = "command_buffer",
789-
};
790-
mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
791-
assert_invariant(mCommandBuffer);
792797
}
793798

794799
void WebGPUDriver::nextSubpass(int) {
@@ -803,9 +808,19 @@ void WebGPUDriver::makeCurrent(Handle<HwSwapChain> drawSch, Handle<HwSwapChain>
803808
wgpu::Extent2D surfaceSize = mPlatform.getSurfaceExtent(mNativeWindow);
804809
mTextureView = mSwapChain->getCurrentSurfaceTextureView(surfaceSize);
805810
assert_invariant(mTextureView);
811+
wgpu::CommandEncoderDescriptor commandEncoderDescriptor = {
812+
.label = "command_encoder"
813+
};
814+
mCommandEncoder = mDevice.CreateCommandEncoder(&commandEncoderDescriptor);
815+
assert_invariant(mCommandEncoder);
806816
}
807817

808818
void WebGPUDriver::commit(Handle<HwSwapChain> sch) {
819+
wgpu::CommandBufferDescriptor commandBufferDescriptor{
820+
.label = "command_buffer",
821+
};
822+
mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
823+
assert_invariant(mCommandBuffer);
809824
mCommandEncoder = nullptr;
810825
mQueue.Submit(1, &mCommandBuffer);
811826
mCommandBuffer = nullptr;

0 commit comments

Comments
 (0)