Skip to content

Commit

Permalink
Specialize on deband dither key
Browse files Browse the repository at this point in the history
  • Loading branch information
aevyrie committed Nov 10, 2022
1 parent d8c45e1 commit de8f1ab
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Expand Up @@ -75,7 +75,7 @@ impl Camera2dBundle {
global_transform: Default::default(),
camera: Camera::default(),
camera_2d: Camera2d::default(),
tonemapping: Tonemapping { is_enabled: false },
tonemapping: Tonemapping::Disabled,
}
}
}
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Expand Up @@ -74,7 +74,9 @@ impl Default for Camera3dBundle {
fn default() -> Self {
Self {
camera_render_graph: CameraRenderGraph::new(crate::core_3d::graph::NAME),
tonemapping: Tonemapping { is_enabled: true },
tonemapping: Tonemapping::Enabled {
is_deband_dither_enabled: true,
},
camera: Default::default(),
projection: Default::default(),
visible_entities: Default::default(),
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_core_pipeline/src/tonemapping/mod.rs
Expand Up @@ -108,8 +108,16 @@ impl FromWorld for TonemappingPipeline {

#[derive(Component, Clone, Reflect, Default)]
#[reflect(Component)]
pub struct Tonemapping {
pub is_enabled: bool,
pub enum Tonemapping {
#[default]
Disabled,
Enabled { is_deband_dither_enabled: bool },
}

impl Tonemapping {
pub fn is_enabled(&self) -> bool {
matches!(self, Tonemapping::Enabled{..})
}
}

impl ExtractComponent for Tonemapping {
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_pbr/src/material.rs
Expand Up @@ -363,9 +363,16 @@ pub fn queue_material_meshes<M: Material>(
let mut view_key =
MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr);

if let Some(tonemapping) = tonemapping {
if tonemapping.is_enabled && !view.hdr {
if let Some(Tonemapping::Enabled {
is_deband_dither_enabled,
}) = tonemapping
{
if !view.hdr {
view_key |= MeshPipelineKey::TONEMAP_IN_SHADER;

if *is_deband_dither_enabled {
view_key |= MeshPipelineKey::DEBAND_DITHER;
}
}
}
let rangefinder = view.rangefinder3d();
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_pbr/src/render/mesh.rs
Expand Up @@ -518,6 +518,7 @@ bitflags::bitflags! {
const TRANSPARENT_MAIN_PASS = (1 << 0);
const HDR = (1 << 1);
const TONEMAP_IN_SHADER = (1 << 2);
const DEBAND_DITHER = (1 << 3);
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
const PRIMITIVE_TOPOLOGY_RESERVED_BITS = Self::PRIMITIVE_TOPOLOGY_MASK_BITS << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
}
Expand Down Expand Up @@ -636,6 +637,11 @@ impl SpecializedMeshPipeline for MeshPipeline {

if key.contains(MeshPipelineKey::TONEMAP_IN_SHADER) {
shader_defs.push("TONEMAP_IN_SHADER".to_string());

// Debanding is tied to tonemapping in the shader, cannot run without it.
if key.contains(MeshPipelineKey::DEBAND_DITHER) {
shader_defs.push("DEBAND_DITHER".to_string());
}
}

let format = match key.contains(MeshPipelineKey::HDR) {
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/render/pbr.wgsl
Expand Up @@ -97,6 +97,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {

#ifdef TONEMAP_IN_SHADER
output_color = tone_mapping(output_color);
#endif
#ifdef DEBAND_DITHER
output_color = dither(output_color, in.frag_coord.xy);
#endif
return output_color;
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/render/pbr_functions.wgsl
Expand Up @@ -260,7 +260,9 @@ fn tone_mapping(in: vec4<f32>) -> vec4<f32> {
// Not needed with sRGB buffer
// output_color.rgb = pow(output_color.rgb, vec3(1.0 / 2.2));
}
#endif

#ifdef DEBAND_DITHER
fn dither(color: vec4<f32>, pos: vec2<f32>) -> vec4<f32> {
return vec4<f32>(color.rgb + screen_space_dither(pos.xy), color.a);
}
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_sprite/src/mesh2d/material.rs
Expand Up @@ -328,9 +328,13 @@ pub fn queue_material2d_meshes<M: Material2d>(
let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
| Mesh2dPipelineKey::from_hdr(view.hdr);

if let Some(tonemapping) = tonemapping {
if tonemapping.is_enabled && !view.hdr {
if let Some(Tonemapping::Enabled{is_deband_dither_enabled}) = tonemapping {
if !view.hdr {
view_key |= Mesh2dPipelineKey::TONEMAP_IN_SHADER;

if *is_deband_dither_enabled {
view_key |= Mesh2dPipelineKey::DEBAND_DITHER;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Expand Up @@ -288,6 +288,7 @@ bitflags::bitflags! {
const NONE = 0;
const HDR = (1 << 0);
const TONEMAP_IN_SHADER = (1 << 1);
const DEBAND_DITHER = (1 << 2);
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
const PRIMITIVE_TOPOLOGY_RESERVED_BITS = Self::PRIMITIVE_TOPOLOGY_MASK_BITS << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
}
Expand Down Expand Up @@ -376,6 +377,11 @@ impl SpecializedMeshPipeline for Mesh2dPipeline {

if key.contains(Mesh2dPipelineKey::TONEMAP_IN_SHADER) {
shader_defs.push("TONEMAP_IN_SHADER".to_string());

// Debanding is tied to tonemapping in the shader, cannot run without it.
if key.contains(Mesh2dPipelineKey::DEBAND_DITHER) {
shader_defs.push("DEBAND_DITHER".to_string());
}
}

let vertex_buffer_layout = layout.get_layout(&vertex_attributes)?;
Expand Down
14 changes: 12 additions & 2 deletions crates/bevy_sprite/src/render/mod.rs
Expand Up @@ -151,6 +151,7 @@ bitflags::bitflags! {
const COLORED = (1 << 0);
const HDR = (1 << 1);
const TONEMAP_IN_SHADER = (1 << 2);
const DEBAND_DITHER = (1 << 3);
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
}
}
Expand Down Expand Up @@ -212,6 +213,11 @@ impl SpecializedRenderPipeline for SpritePipeline {

if key.contains(SpritePipelineKey::TONEMAP_IN_SHADER) {
shader_defs.push("TONEMAP_IN_SHADER".to_string());

// Debanding is tied to tonemapping in the shader, cannot run without it.
if key.contains(SpritePipelineKey::DEBAND_DITHER) {
shader_defs.push("DEBAND_DITHER".to_string());
}
}

let format = match key.contains(SpritePipelineKey::HDR) {
Expand Down Expand Up @@ -508,9 +514,13 @@ pub fn queue_sprites(

for (mut transparent_phase, visible_entities, view, tonemapping) in &mut views {
let mut view_key = SpritePipelineKey::from_hdr(view.hdr) | msaa_key;
if let Some(tonemapping) = tonemapping {
if tonemapping.is_enabled && !view.hdr {
if let Some(Tonemapping::Enabled{is_deband_dither_enabled}) = tonemapping {
if !view.hdr {
view_key |= SpritePipelineKey::TONEMAP_IN_SHADER;

if *is_deband_dither_enabled {
view_key |= SpritePipelineKey::DEBAND_DITHER;
}
}
}
let pipeline = pipelines.specialize(
Expand Down

0 comments on commit de8f1ab

Please sign in to comment.