From 958e4c1760851a104102d90045e38529c797ae68 Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Sat, 16 Dec 2023 20:44:17 +0100 Subject: [PATCH] Replace Option with 0=default --- comfy-core/src/render_queues.rs | 2 +- comfy-core/src/shaders.rs | 24 +++++++++++++----------- comfy-wgpu/src/batching.rs | 8 ++++---- comfy-wgpu/src/pipelines.rs | 6 +++--- comfy-wgpu/src/render_pass.rs | 2 +- comfy/src/update_stages.rs | 4 ++-- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/comfy-core/src/render_queues.rs b/comfy-core/src/render_queues.rs index 0cd3776..307b624 100644 --- a/comfy-core/src/render_queues.rs +++ b/comfy-core/src/render_queues.rs @@ -117,7 +117,7 @@ pub struct MeshGroupKey { pub blend_mode: BlendMode, pub texture_id: TextureHandle, pub shader: Option, - pub render_target: Option, + pub render_target: RenderTargetId, } pub fn consume_render_queues() -> BTreeMap { diff --git a/comfy-core/src/shaders.rs b/comfy-core/src/shaders.rs index 12c887f..2b17b08 100644 --- a/comfy-core/src/shaders.rs +++ b/comfy-core/src/shaders.rs @@ -1,3 +1,5 @@ +use std::sync::atomic::{AtomicU32, Ordering}; + use crate::*; #[derive(Debug)] @@ -73,19 +75,22 @@ pub enum Uniform { Custom(Vec), } -static CURRENT_RENDER_TARGET: Lazy>> = - Lazy::new(|| AtomicRefCell::new(None)); +// static CURRENT_RENDER_TARGET: Lazy>> = +// Lazy::new(|| AtomicRefCell::new(None)); + +static CURRENT_RENDER_TARGET: AtomicU32 = AtomicU32::new(0); pub fn use_render_target(id: RenderTargetId) { - *CURRENT_RENDER_TARGET.borrow_mut() = Some(id); + CURRENT_RENDER_TARGET.store(id.0, Ordering::SeqCst); + // *CURRENT_RENDER_TARGET.borrow_mut() = Some(id); } pub fn use_default_render_target() { - *CURRENT_RENDER_TARGET.borrow_mut() = None; + CURRENT_RENDER_TARGET.store(0, Ordering::SeqCst); } -pub fn get_current_render_target() -> Option { - *CURRENT_RENDER_TARGET.borrow() +pub fn get_current_render_target() -> RenderTargetId { + RenderTargetId(CURRENT_RENDER_TARGET.load(Ordering::SeqCst)) } /// Sets a `f32` uniform value by name. The uniform must exist in the shader. @@ -176,8 +181,5 @@ pub fn build_shader_source( format!("{}\n{}", uniforms_src, fragment_source) } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum RenderTargetId { - Named(&'static str), - Generated(u64), -} +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct RenderTargetId(pub u32); diff --git a/comfy-wgpu/src/batching.rs b/comfy-wgpu/src/batching.rs index 1b6436e..a52b837 100644 --- a/comfy-wgpu/src/batching.rs +++ b/comfy-wgpu/src/batching.rs @@ -17,7 +17,7 @@ pub fn run_batched_render_passes( blend_mode: BlendMode::Alpha, texture_id: TextureHandle::from_path("1px"), shader: None, - render_target: None, + render_target: RenderTargetId::default(), }, RenderPassData { z_index: 0, @@ -151,7 +151,7 @@ pub fn run_batched_render_passes( blend_mode: BlendMode::Alpha, texture: TextureHandle::from_path("1px"), shader: None, - render_target: None, + render_target: RenderTargetId::default(), data: SmallVec::new(), }, surface_view, @@ -225,9 +225,9 @@ pub fn render_meshes( { let clear_color = if is_first { Some(clear_color) } else { None }; - let target_view = if let Some(render_target) = pass_data.render_target { + let target_view = if pass_data.render_target.0 > 0 { &render_targets - .get(&render_target) + .get(&pass_data.render_target) .expect("user render target must exist when used") .view } else if c.post_processing_effects.borrow().iter().any(|x| x.enabled) { diff --git a/comfy-wgpu/src/pipelines.rs b/comfy-wgpu/src/pipelines.rs index 33614bd..1862ac1 100644 --- a/comfy-wgpu/src/pipelines.rs +++ b/comfy-wgpu/src/pipelines.rs @@ -1,8 +1,8 @@ -use std::sync::atomic::AtomicU64; +use std::sync::atomic::AtomicU32; use crate::*; -static GENERATED_RENDER_TARGET_IDS: AtomicU64 = AtomicU64::new(0); +static GENERATED_RENDER_TARGET_IDS: AtomicU32 = AtomicU32::new(1); #[derive(Clone, Debug)] pub struct RenderTargetParams { @@ -100,7 +100,7 @@ fn gen_render_target() -> RenderTargetId { let id = GENERATED_RENDER_TARGET_IDS .fetch_add(1, std::sync::atomic::Ordering::SeqCst); - RenderTargetId::Generated(id) + RenderTargetId(id) } pub fn ensure_pipeline_exists( diff --git a/comfy-wgpu/src/render_pass.rs b/comfy-wgpu/src/render_pass.rs index c921607..5010561 100644 --- a/comfy-wgpu/src/render_pass.rs +++ b/comfy-wgpu/src/render_pass.rs @@ -4,7 +4,7 @@ pub struct MeshDrawData { pub blend_mode: BlendMode, pub texture: TextureHandle, pub shader: Option, - pub render_target: Option, + pub render_target: RenderTargetId, pub data: smallvec::SmallVec<[Mesh; 1]>, } diff --git a/comfy/src/update_stages.rs b/comfy/src/update_stages.rs index ca1bf84..b6dc2b9 100644 --- a/comfy/src/update_stages.rs +++ b/comfy/src/update_stages.rs @@ -1033,7 +1033,7 @@ fn renderer_update(c: &mut EngineContext) { blend_mode: p.blend_mode, texture_id: p.texture, shader: None, - render_target: None, + render_target: RenderTargetId::default(), }; let err_texture = texture_id("error"); @@ -1066,7 +1066,7 @@ fn renderer_update(c: &mut EngineContext) { blend_mode: p.blend_mode, texture_id: p.texture, shader: None, - render_target: None, + render_target: RenderTargetId::default(), }) .or_default() .push(p.to_draw());