Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Replace Option<RenderTargetId> with 0=default
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Dec 16, 2023
1 parent bb832de commit 958e4c1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion comfy-core/src/render_queues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct MeshGroupKey {
pub blend_mode: BlendMode,
pub texture_id: TextureHandle,
pub shader: Option<ShaderInstanceId>,
pub render_target: Option<RenderTargetId>,
pub render_target: RenderTargetId,
}

pub fn consume_render_queues() -> BTreeMap<MeshGroupKey, RenderQueue> {
Expand Down
24 changes: 13 additions & 11 deletions comfy-core/src/shaders.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::atomic::{AtomicU32, Ordering};

use crate::*;

#[derive(Debug)]
Expand Down Expand Up @@ -73,19 +75,22 @@ pub enum Uniform {
Custom(Vec<u8>),
}

static CURRENT_RENDER_TARGET: Lazy<AtomicRefCell<Option<RenderTargetId>>> =
Lazy::new(|| AtomicRefCell::new(None));
// static CURRENT_RENDER_TARGET: Lazy<AtomicRefCell<Option<RenderTargetId>>> =
// 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<RenderTargetId> {
*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.
Expand Down Expand Up @@ -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);
8 changes: 4 additions & 4 deletions comfy-wgpu/src/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions comfy-wgpu/src/pipelines.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion comfy-wgpu/src/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub struct MeshDrawData {
pub blend_mode: BlendMode,
pub texture: TextureHandle,
pub shader: Option<ShaderInstanceId>,
pub render_target: Option<RenderTargetId>,
pub render_target: RenderTargetId,
pub data: smallvec::SmallVec<[Mesh; 1]>,
}

Expand Down
4 changes: 2 additions & 2 deletions comfy/src/update_stages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 958e4c1

Please sign in to comment.