From 15477b84a96dbf4ccf58ce08f2c58c0448e4ac37 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Tue, 22 Apr 2025 21:24:36 +0200 Subject: [PATCH] [wgpu-hal] make `begin_render_pass` fallible --- wgpu-core/src/command/clear.rs | 30 +++++++++++++++++------------- wgpu-core/src/command/render.rs | 11 ++++++++--- wgpu-hal/examples/halmark/main.rs | 2 +- wgpu-hal/examples/raw-gles.rs | 2 +- wgpu-hal/src/dx12/command.rs | 4 +++- wgpu-hal/src/dynamic/command.rs | 6 +++--- wgpu-hal/src/gles/command.rs | 3 ++- wgpu-hal/src/lib.rs | 2 +- wgpu-hal/src/metal/command.rs | 4 +++- wgpu-hal/src/noop/command.rs | 6 +++++- wgpu-hal/src/vulkan/command.rs | 4 +++- 11 files changed, 47 insertions(+), 27 deletions(-) diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index edf279955..663036d2b 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -324,10 +324,10 @@ pub(crate) fn clear_texture( dst_raw, ), TextureClearMode::Surface { .. } => { - clear_texture_via_render_passes(dst_texture, range, true, encoder) + clear_texture_via_render_passes(dst_texture, range, true, encoder)? } TextureClearMode::RenderPass { is_color, .. } => { - clear_texture_via_render_passes(dst_texture, range, is_color, encoder) + clear_texture_via_render_passes(dst_texture, range, is_color, encoder)? } TextureClearMode::None => { return Err(ClearError::NoValidTextureClearMode( @@ -437,7 +437,7 @@ fn clear_texture_via_render_passes( range: TextureInitRange, is_color: bool, encoder: &mut dyn hal::DynCommandEncoder, -) { +) -> Result<(), ClearError> { assert_eq!(dst_texture.desc.dimension, wgt::TextureDimension::D2); let extent_base = wgt::Extent3d { @@ -487,18 +487,22 @@ fn clear_texture_via_render_passes( ) }; unsafe { - encoder.begin_render_pass(&hal::RenderPassDescriptor { - label: Some("(wgpu internal) clear_texture clear pass"), - extent, - sample_count: dst_texture.desc.sample_count, - color_attachments, - depth_stencil_attachment, - multiview: None, - timestamp_writes: None, - occlusion_query_set: None, - }); + encoder + .begin_render_pass(&hal::RenderPassDescriptor { + label: Some("(wgpu internal) clear_texture clear pass"), + extent, + sample_count: dst_texture.desc.sample_count, + color_attachments, + depth_stencil_attachment, + multiview: None, + timestamp_writes: None, + occlusion_query_set: None, + }) + .map_err(|e| dst_texture.device.handle_hal_error(e))?; encoder.end_render_pass(); } } } + + Ok(()) } diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index edb4f8b3a..1f502d86d 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1277,7 +1277,10 @@ impl<'d> RenderPassInfo<'d> { occlusion_query_set: occlusion_query_set_hal, }; unsafe { - encoder.raw.begin_render_pass(&hal_desc); + encoder + .raw + .begin_render_pass(&hal_desc) + .map_err(|e| device.handle_hal_error(e))?; }; drop(color_attachments_hal); // Drop, so we can consume `color_attachments` for the tracker. @@ -1313,6 +1316,7 @@ impl<'d> RenderPassInfo<'d> { fn finish( mut self, + device: &Device, raw: &mut dyn hal::DynCommandEncoder, snatch_guard: &SnatchGuard, ) -> Result<(UsageScope<'d>, SurfacesInDiscardState), RenderPassErrorInner> { @@ -1375,7 +1379,8 @@ impl<'d> RenderPassInfo<'d> { occlusion_query_set: None, }; unsafe { - raw.begin_render_pass(&desc); + raw.begin_render_pass(&desc) + .map_err(|e| device.handle_hal_error(e))?; raw.end_render_pass(); } } @@ -1974,7 +1979,7 @@ impl Global { let (trackers, pending_discard_init_fixups) = state .info - .finish(state.raw_encoder, state.snatch_guard) + .finish(device, state.raw_encoder, state.snatch_guard) .map_pass_err(pass_scope)?; encoder.close().map_pass_err(pass_scope)?; diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 7302fa068..75f3bc2fb 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -724,7 +724,7 @@ impl Example { occlusion_query_set: None, }; unsafe { - ctx.encoder.begin_render_pass(&pass_desc); + ctx.encoder.begin_render_pass(&pass_desc).unwrap(); ctx.encoder.set_render_pipeline(&self.pipeline); ctx.encoder .set_bind_group(&self.pipeline_layout, 0, &self.global_group, &[]); diff --git a/wgpu-hal/examples/raw-gles.rs b/wgpu-hal/examples/raw-gles.rs index 3d76cf22e..b7b14e361 100644 --- a/wgpu-hal/examples/raw-gles.rs +++ b/wgpu-hal/examples/raw-gles.rs @@ -335,7 +335,7 @@ fn fill_screen(exposed: &hal::ExposedAdapter, width: u32, height }; unsafe { encoder.begin_encoding(None).unwrap(); - encoder.begin_render_pass(&rp_desc); + encoder.begin_render_pass(&rp_desc).unwrap(); encoder.end_render_pass(); let cmd_buf = encoder.end_encoding().unwrap(); od.queue.submit(&[&cmd_buf], &[], (&mut fence, 0)).unwrap(); diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index db49d22c1..e7a27e069 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -745,7 +745,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn begin_render_pass( &mut self, desc: &crate::RenderPassDescriptor, - ) { + ) -> Result<(), crate::DeviceError> { unsafe { self.begin_pass(super::PassKind::Render, desc.label) }; // Start timestamp if any (before all other commands but after debug marker) @@ -867,6 +867,8 @@ impl crate::CommandEncoder for super::CommandEncoder { }; unsafe { list.RSSetViewports(core::slice::from_ref(&raw_vp)) }; unsafe { list.RSSetScissorRects(core::slice::from_ref(&raw_rect)) }; + + Ok(()) } unsafe fn end_render_pass(&mut self) { diff --git a/wgpu-hal/src/dynamic/command.rs b/wgpu-hal/src/dynamic/command.rs index 819cc0694..43205c01d 100644 --- a/wgpu-hal/src/dynamic/command.rs +++ b/wgpu-hal/src/dynamic/command.rs @@ -94,7 +94,7 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug { unsafe fn begin_render_pass( &mut self, desc: &RenderPassDescriptor, - ); + ) -> Result<(), DeviceError>; unsafe fn end_render_pass(&mut self); unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline); @@ -394,7 +394,7 @@ impl DynCommandEncoder for C { unsafe fn begin_render_pass( &mut self, desc: &RenderPassDescriptor, - ) { + ) -> Result<(), DeviceError> { let color_attachments = desc .color_attachments .iter() @@ -424,7 +424,7 @@ impl DynCommandEncoder for C { .occlusion_query_set .map(|set| set.expect_downcast_ref()), }; - unsafe { C::begin_render_pass(self, &desc) }; + unsafe { C::begin_render_pass(self, &desc) } } unsafe fn end_render_pass(&mut self) { diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index 6cdf0fbf8..c0ed14ded 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -496,7 +496,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn begin_render_pass( &mut self, desc: &crate::RenderPassDescriptor, - ) { + ) -> Result<(), crate::DeviceError> { debug_assert!(self.state.end_of_pass_timestamp.is_none()); if let Some(ref t) = desc.timestamp_writes { if let Some(index) = t.beginning_of_pass_write_index { @@ -665,6 +665,7 @@ impl crate::CommandEncoder for super::CommandEncoder { .push(C::ClearStencil(dsat.clear_value.1)); } } + Ok(()) } unsafe fn end_render_pass(&mut self) { for (attachment, dst) in self.state.resolve_attachments.drain(..) { diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index d4faea178..b469aa4f4 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -1411,7 +1411,7 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug { unsafe fn begin_render_pass( &mut self, desc: &RenderPassDescriptor<::QuerySet, ::TextureView>, - ); + ) -> Result<(), DeviceError>; /// End the current render pass. /// diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index 6e9835702..4f534c41b 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -517,7 +517,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn begin_render_pass( &mut self, desc: &crate::RenderPassDescriptor, - ) { + ) -> Result<(), crate::DeviceError> { self.begin_pass(); self.state.index = None; @@ -650,6 +650,8 @@ impl crate::CommandEncoder for super::CommandEncoder { } self.state.render = Some(encoder.to_owned()); }); + + Ok(()) } unsafe fn end_render_pass(&mut self) { diff --git a/wgpu-hal/src/noop/command.rs b/wgpu-hal/src/noop/command.rs index 36b5c9216..f188566d9 100644 --- a/wgpu-hal/src/noop/command.rs +++ b/wgpu-hal/src/noop/command.rs @@ -146,7 +146,11 @@ impl crate::CommandEncoder for CommandBuffer { // render - unsafe fn begin_render_pass(&mut self, desc: &crate::RenderPassDescriptor) { + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor, + ) -> DeviceResult<()> { + Ok(()) } unsafe fn end_render_pass(&mut self) {} diff --git a/wgpu-hal/src/vulkan/command.rs b/wgpu-hal/src/vulkan/command.rs index b345f4a34..bd6eadb4e 100644 --- a/wgpu-hal/src/vulkan/command.rs +++ b/wgpu-hal/src/vulkan/command.rs @@ -711,7 +711,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn begin_render_pass( &mut self, desc: &crate::RenderPassDescriptor, - ) { + ) -> Result<(), crate::DeviceError> { let mut vk_clear_values = ArrayVec::::new(); let mut vk_image_views = ArrayVec::::new(); @@ -851,6 +851,8 @@ impl crate::CommandEncoder for super::CommandEncoder { }; self.bind_point = vk::PipelineBindPoint::GRAPHICS; + + Ok(()) } unsafe fn end_render_pass(&mut self) { unsafe {