From ee33ae11d882277236c60ec5f4a70419ca2b383e Mon Sep 17 00:00:00 2001 From: Emerson Coskey Date: Tue, 8 Jul 2025 19:15:41 -0700 Subject: [PATCH] error handling --- .../src/bloom/upsampling_pipeline.rs | 6 +----- .../bevy_render/src/render_resource/pipeline.rs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs b/crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs index 71a3b315fe..8f43761a72 100644 --- a/crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs +++ b/crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs @@ -109,10 +109,6 @@ impl Specializer for BloomUpsamplingSpecializer { key: Self::Key, descriptor: &mut RenderPipelineDescriptor, ) -> Result, BevyError> { - let Some(fragment) = &mut descriptor.fragment else { - return Ok(key); - }; - let texture_format = if key.final_pipeline { ViewTarget::TEXTURE_FORMAT_HDR } else { @@ -165,7 +161,7 @@ impl Specializer for BloomUpsamplingSpecializer { write_mask: ColorWrites::ALL, }; - fragment.set_target(0, target); + descriptor.get_fragment_mut()?.set_target(0, target); Ok(key) } diff --git a/crates/bevy_render/src/render_resource/pipeline.rs b/crates/bevy_render/src/render_resource/pipeline.rs index 858228453f..32e33ab44a 100644 --- a/crates/bevy_render/src/render_resource/pipeline.rs +++ b/crates/bevy_render/src/render_resource/pipeline.rs @@ -8,6 +8,7 @@ use alloc::borrow::Cow; use bevy_asset::Handle; use bevy_utils::WgpuWrapper; use core::{iter, ops::Deref}; +use thiserror::Error; use wgpu::{ ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState, PushConstantRange, }; @@ -112,6 +113,16 @@ pub struct RenderPipelineDescriptor { pub zero_initialize_workgroup_memory: bool, } +#[derive(Copy, Clone, Debug, Error)] +#[error("RenderPipelineDescriptor has no FragmentState configured")] +pub struct NoFragmentStateError; + +impl RenderPipelineDescriptor { + pub fn get_fragment_mut(&mut self) -> Result<&mut FragmentState, NoFragmentStateError> { + self.fragment.as_mut().ok_or(NoFragmentStateError) + } +} + #[derive(Clone, Debug, Eq, PartialEq, Default)] pub struct VertexState { /// The compiled shader module for this stage. @@ -139,7 +150,7 @@ pub struct FragmentState { impl FragmentState { pub fn set_target(&mut self, index: usize, target: ColorTargetState) { - filling_insert_at(&mut self.targets, index, None, Some(target)); + filling_set_at(&mut self.targets, index, None, Some(target)); } } @@ -160,7 +171,9 @@ pub struct ComputePipelineDescriptor { pub zero_initialize_workgroup_memory: bool, } -fn filling_insert_at(vec: &mut Vec, index: usize, filler: T, value: T) { +// utility function to set a value at the specified index, extending with +// a filler value if the index is out of bounds. +fn filling_set_at(vec: &mut Vec, index: usize, filler: T, value: T) { let num_to_fill = index.saturating_sub(vec.len() - 1); vec.extend(iter::repeat_n(filler, num_to_fill)); vec[index] = value;