Skip to content

Commit

Permalink
add UpscalingPipelineKey
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Mar 20, 2022
1 parent babcbb2 commit 21ba00d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
bevy_reflect = { path = "../bevy_reflect", version = "0.7.0-dev" }

bitflags = "1.2"
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
///
Expand Down
40 changes: 38 additions & 2 deletions crates/bevy_core_pipeline/src/upscaling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -113,7 +148,8 @@ fn queue_upscaling_bind_groups(
view_targets: Query<Entity, With<ExtractedView>>,
) {
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 });
}
Expand Down

0 comments on commit 21ba00d

Please sign in to comment.