From 1af8ae30a1a596ecdbb454bddcaf70f4aa0b0300 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Sun, 20 Mar 2022 13:16:21 +0100 Subject: [PATCH] add UpscalingPipelineKey --- crates/bevy_core_pipeline/Cargo.toml | 4 +- crates/bevy_core_pipeline/src/lib.rs | 2 +- .../bevy_core_pipeline/src/upscaling/mod.rs | 40 ++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/bevy_core_pipeline/Cargo.toml b/crates/bevy_core_pipeline/Cargo.toml index 671645e40889c..db325f7e3a19e 100644 --- a/crates/bevy_core_pipeline/Cargo.toml +++ b/crates/bevy_core_pipeline/Cargo.toml @@ -23,4 +23,6 @@ bevy_core = { path = "../bevy_core", version = "0.7.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.7.0-dev" } bevy_render = { path = "../bevy_render", version = "0.7.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.7.0-dev" } -bevy_reflect = { path = "../bevy_reflect", version = "0.7.0-dev" } \ No newline at end of file +bevy_reflect = { path = "../bevy_reflect", version = "0.7.0-dev" } + +bitflags = "1.2" \ No newline at end of file diff --git a/crates/bevy_core_pipeline/src/lib.rs b/crates/bevy_core_pipeline/src/lib.rs index 1abd923c1df01..b6efaaa2d7221 100644 --- a/crates/bevy_core_pipeline/src/lib.rs +++ b/crates/bevy_core_pipeline/src/lib.rs @@ -18,6 +18,7 @@ pub use clear_pass_driver::*; pub use main_pass_2d::*; pub use main_pass_3d::*; pub use main_pass_driver::*; +pub use upscaling::*; use std::ops::Range; @@ -41,7 +42,6 @@ use bevy_render::{ use tonemapping::TonemappingNode; use tonemapping::TonemappingPlugin; use upscaling::UpscalingNode; -use upscaling::UpscalingPlugin; /// When used as a resource, sets the color that is used to clear the screen between frames. /// diff --git a/crates/bevy_core_pipeline/src/upscaling/mod.rs b/crates/bevy_core_pipeline/src/upscaling/mod.rs index 157a8d1636cd5..8ca7b085e3ed8 100644 --- a/crates/bevy_core_pipeline/src/upscaling/mod.rs +++ b/crates/bevy_core_pipeline/src/upscaling/mod.rs @@ -70,8 +70,43 @@ impl FromWorld for UpscalingPipeline { } } +#[repr(u8)] +pub enum UpscalingMode { + Filtering = 0, + Nearest = 1, +} + +bitflags::bitflags! { + #[repr(transparent)] + pub struct UpscalingPipelineKey: u32 { + const NONE = 0; + const UPSCALING_MODE_RESERVED_BITS = UpscalingPipelineKey::UPSCALING_MODE_MASK_BITS << UpscalingPipelineKey::UPSCALING_MODE_SHIFT_BITS; + } +} + +impl UpscalingPipelineKey { + const UPSCALING_MODE_MASK_BITS: u32 = 0b1111; // enough for 16 different modes + const UPSCALING_MODE_SHIFT_BITS: u32 = 32 - 4; + + pub fn from_upscaling_mode(upscaling_mode: UpscalingMode) -> Self { + let upscaling_mode_bits = ((upscaling_mode as u32) & Self::UPSCALING_MODE_MASK_BITS) + << Self::UPSCALING_MODE_SHIFT_BITS; + UpscalingPipelineKey::from_bits(upscaling_mode_bits).unwrap() + } + + pub fn upscaling_mode(&self) -> UpscalingMode { + let upscaling_mode_bits = + (self.bits >> Self::UPSCALING_MODE_SHIFT_BITS) & Self::UPSCALING_MODE_MASK_BITS; + match upscaling_mode_bits { + 0 => UpscalingMode::Filtering, + 1 => UpscalingMode::Nearest, + other => panic!("invalid upscaling mode bits in UpscalingPipelineKey: {other}"), + } + } +} + impl SpecializedPipeline for UpscalingPipeline { - type Key = (); + type Key = UpscalingPipelineKey; fn specialize(&self, _: Self::Key) -> RenderPipelineDescriptor { RenderPipelineDescriptor { @@ -113,7 +148,8 @@ fn queue_upscaling_bind_groups( view_targets: Query>, ) { for entity in view_targets.iter() { - let pipeline = pipelines.specialize(&mut render_pipeline_cache, &upscaling_pipeline, ()); + let key = UpscalingPipelineKey::from_upscaling_mode(UpscalingMode::Filtering); + let pipeline = pipelines.specialize(&mut render_pipeline_cache, &upscaling_pipeline, key); commands.entity(entity).insert(UpscalingTarget { pipeline }); }