From 7bbe87dcfda352cfee5d69e28aabe1420000f4a8 Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Mon, 9 Dec 2024 18:40:44 +0000 Subject: [PATCH 1/2] Fix 2D depth stencil not set properly on render Fix the depth stencil attachment not being set properly on the render pipeline when a 2D camera is used, and both the `2d` and `3d` features are active at the same time. Bug: #406 --- src/render/mod.rs | 50 +++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/render/mod.rs b/src/render/mod.rs index 5f399fa..7485ba8 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1179,30 +1179,7 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline { shader_defs.push("RIBBONS".into()); } - #[cfg(all(feature = "2d", feature = "3d"))] - assert_eq!(CORE_2D_DEPTH_FORMAT, CORE_3D_DEPTH_FORMAT); - #[cfg(all(feature = "2d", feature = "3d"))] - let depth_stencil = match key.pipeline_mode { - // Bevy's Transparent2d render phase doesn't support a depth-stencil buffer. - PipelineMode::Camera2d => None, - PipelineMode::Camera3d => Some(DepthStencilState { - format: CORE_3D_DEPTH_FORMAT, - // Use depth buffer with alpha-masked or opaque particles, not - // with transparent ones - depth_write_enabled: matches!( - key.alpha_mask, - ParticleRenderAlphaMaskPipelineKey::AlphaMask - | ParticleRenderAlphaMaskPipelineKey::Opaque - ), - // Bevy uses reverse-Z, so GreaterEqual really means closer - depth_compare: CompareFunction::GreaterEqual, - stencil: StencilState::default(), - bias: DepthBiasState::default(), - }), - }; - - #[cfg(all(feature = "2d", not(feature = "3d")))] - let depth_stencil = Some(DepthStencilState { + let depth_stencil_2d = DepthStencilState { format: CORE_2D_DEPTH_FORMAT, // Use depth buffer with alpha-masked particles, not with transparent ones depth_write_enabled: false, // TODO - opaque/alphamask 2d @@ -1210,12 +1187,11 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline { depth_compare: CompareFunction::GreaterEqual, stencil: StencilState::default(), bias: DepthBiasState::default(), - }); - - #[cfg(all(feature = "3d", not(feature = "2d")))] - let depth_stencil = Some(DepthStencilState { + }; + let depth_stencil_3d = DepthStencilState { format: CORE_3D_DEPTH_FORMAT, - // Use depth buffer with alpha-masked particles, not with transparent ones + // Use depth buffer with alpha-masked or opaque particles, not + // with transparent ones depth_write_enabled: matches!( key.alpha_mask, ParticleRenderAlphaMaskPipelineKey::AlphaMask @@ -1225,7 +1201,21 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline { depth_compare: CompareFunction::GreaterEqual, stencil: StencilState::default(), bias: DepthBiasState::default(), - }); + }; + + #[cfg(all(feature = "2d", feature = "3d"))] + assert_eq!(CORE_2D_DEPTH_FORMAT, CORE_3D_DEPTH_FORMAT); + #[cfg(all(feature = "2d", feature = "3d"))] + let depth_stencil = match key.pipeline_mode { + PipelineMode::Camera2d => Some(depth_stencil_2d), + PipelineMode::Camera3d => Some(depth_stencil_3d), + }; + + #[cfg(all(feature = "2d", not(feature = "3d")))] + let depth_stencil = Some(depth_stencil_2d); + + #[cfg(all(feature = "3d", not(feature = "2d")))] + let depth_stencil = Some(depth_stencil_3d); let format = if key.hdr { ViewTarget::TEXTURE_FORMAT_HDR From c0b862a48c2f73632234ca4f4b627173eb54f42d Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Mon, 9 Dec 2024 18:48:32 +0000 Subject: [PATCH 2/2] Build fix --- src/render/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/render/mod.rs b/src/render/mod.rs index 7485ba8..d00775b 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1179,6 +1179,7 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline { shader_defs.push("RIBBONS".into()); } + #[cfg(feature = "2d")] let depth_stencil_2d = DepthStencilState { format: CORE_2D_DEPTH_FORMAT, // Use depth buffer with alpha-masked particles, not with transparent ones @@ -1188,6 +1189,8 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline { stencil: StencilState::default(), bias: DepthBiasState::default(), }; + + #[cfg(feature = "3d")] let depth_stencil_3d = DepthStencilState { format: CORE_3D_DEPTH_FORMAT, // Use depth buffer with alpha-masked or opaque particles, not