Skip to content

spec_v2: migrate bloom #19966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 58 additions & 50 deletions crates/bevy_core_pipeline/src/bloom/downsampling_pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::FullscreenShader;

use super::{Bloom, BLOOM_TEXTURE_FORMAT};
use bevy_asset::{load_embedded_asset, Handle};
use bevy_asset::load_embedded_asset;
use bevy_ecs::{
error::BevyError,
prelude::{Component, Entity},
resource::Resource,
system::{Commands, Query, Res, ResMut},
Expand All @@ -29,14 +30,13 @@ pub struct BloomDownsamplingPipeline {
/// Layout with a texture, a sampler, and uniforms
pub bind_group_layout: BindGroupLayout,
pub sampler: Sampler,
/// The asset handle for the fullscreen vertex shader.
pub fullscreen_shader: FullscreenShader,
/// The fragment shader asset handle.
pub fragment_shader: Handle<Shader>,
pub specialized_cache: SpecializedCache<RenderPipeline, BloomDownsamplingSpecializer>,
}

#[derive(PartialEq, Eq, Hash, Clone)]
pub struct BloomDownsamplingPipelineKeys {
pub struct BloomDownsamplingSpecializer;

#[derive(PartialEq, Eq, Hash, Clone, SpecializerKey)]
pub struct BloomDownsamplingKey {
prefilter: bool,
first_downsample: bool,
uniform_scale: bool,
Expand Down Expand Up @@ -82,28 +82,60 @@ impl FromWorld for BloomDownsamplingPipeline {
..Default::default()
});

let fullscreen_shader = world.resource::<FullscreenShader>().clone();
let fragment_shader = load_embedded_asset!(world, "bloom.wgsl");
let base_descriptor = RenderPipelineDescriptor {
layout: vec![bind_group_layout.clone()],
vertex: fullscreen_shader.to_vertex_state(),
fragment: Some(FragmentState {
shader: fragment_shader.clone(),
targets: vec![Some(ColorTargetState {
format: BLOOM_TEXTURE_FORMAT,
blend: None,
write_mask: ColorWrites::ALL,
})],
..default()
}),
..default()
};

let specialized_cache =
SpecializedCache::new(BloomDownsamplingSpecializer, None, base_descriptor);

BloomDownsamplingPipeline {
bind_group_layout,
sampler,
fullscreen_shader: world.resource::<FullscreenShader>().clone(),
fragment_shader: load_embedded_asset!(world, "bloom.wgsl"),
specialized_cache,
}
}
}

impl SpecializedRenderPipeline for BloomDownsamplingPipeline {
type Key = BloomDownsamplingPipelineKeys;
impl Specializer<RenderPipeline> for BloomDownsamplingSpecializer {
type Key = BloomDownsamplingKey;

fn specialize(
&self,
key: Self::Key,
descriptor: &mut RenderPipelineDescriptor,
) -> Result<Canonical<Self::Key>, BevyError> {
descriptor.label = Some(if key.first_downsample {
"bloom_downsampling_pipeline_first".into()
} else {
"bloom_downsampling_pipeline".into()
});

fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
let layout = vec![self.bind_group_layout.clone()];
// TODO: should this error?
let Some(fragment) = &mut descriptor.fragment else {
return Ok(key);
};

let entry_point = if key.first_downsample {
fragment.entry_point = Some(if key.first_downsample {
"downsample_first".into()
} else {
"downsample".into()
};
});

let mut shader_defs = vec![];
let shader_defs = &mut fragment.shader_defs;

if key.first_downsample {
shader_defs.push("FIRST_DOWNSAMPLE".into());
Expand All @@ -117,61 +149,36 @@ impl SpecializedRenderPipeline for BloomDownsamplingPipeline {
shader_defs.push("UNIFORM_SCALE".into());
}

RenderPipelineDescriptor {
label: Some(
if key.first_downsample {
"bloom_downsampling_pipeline_first"
} else {
"bloom_downsampling_pipeline"
}
.into(),
),
layout,
vertex: self.fullscreen_shader.to_vertex_state(),
fragment: Some(FragmentState {
shader: self.fragment_shader.clone(),
shader_defs,
entry_point: Some(entry_point),
targets: vec![Some(ColorTargetState {
format: BLOOM_TEXTURE_FORMAT,
blend: None,
write_mask: ColorWrites::ALL,
})],
}),
..default()
}
Ok(key)
}
}

pub fn prepare_downsampling_pipeline(
mut commands: Commands,
pipeline_cache: Res<PipelineCache>,
mut pipelines: ResMut<SpecializedRenderPipelines<BloomDownsamplingPipeline>>,
pipeline: Res<BloomDownsamplingPipeline>,
mut pipeline: ResMut<BloomDownsamplingPipeline>,
views: Query<(Entity, &Bloom)>,
) {
) -> Result<(), BevyError> {
for (entity, bloom) in &views {
let prefilter = bloom.prefilter.threshold > 0.0;

let pipeline_id = pipelines.specialize(
let pipeline_id = pipeline.specialized_cache.specialize(
&pipeline_cache,
&pipeline,
BloomDownsamplingPipelineKeys {
BloomDownsamplingKey {
prefilter,
first_downsample: false,
uniform_scale: bloom.scale == Vec2::ONE,
},
);
)?;

let pipeline_first_id = pipelines.specialize(
let pipeline_first_id = pipeline.specialized_cache.specialize(
&pipeline_cache,
&pipeline,
BloomDownsamplingPipelineKeys {
BloomDownsamplingKey {
prefilter,
first_downsample: true,
uniform_scale: bloom.scale == Vec2::ONE,
},
);
)?;

commands
.entity(entity)
Expand All @@ -180,4 +187,5 @@ pub fn prepare_downsampling_pipeline(
main: pipeline_id,
});
}
Ok(())
}
2 changes: 0 additions & 2 deletions crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ impl Plugin for BloomPlugin {
return;
};
render_app
.init_resource::<SpecializedRenderPipelines<BloomDownsamplingPipeline>>()
.init_resource::<SpecializedRenderPipelines<BloomUpsamplingPipeline>>()
.add_systems(
Render,
(
Expand Down
131 changes: 82 additions & 49 deletions crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::FullscreenShader;
use super::{
downsampling_pipeline::BloomUniforms, Bloom, BloomCompositeMode, BLOOM_TEXTURE_FORMAT,
};
use bevy_asset::{load_embedded_asset, Handle};
use bevy_asset::load_embedded_asset;
use bevy_ecs::{
error::BevyError,
prelude::{Component, Entity},
resource::Resource,
system::{Commands, Query, Res, ResMut},
Expand All @@ -29,16 +30,7 @@ pub struct UpsamplingPipelineIds {
#[derive(Resource)]
pub struct BloomUpsamplingPipeline {
pub bind_group_layout: BindGroupLayout,
/// The asset handle for the fullscreen vertex shader.
pub fullscreen_shader: FullscreenShader,
/// The fragment shader asset handle.
pub fragment_shader: Handle<Shader>,
}

#[derive(PartialEq, Eq, Hash, Clone)]
pub struct BloomUpsamplingPipelineKeys {
composite_mode: BloomCompositeMode,
final_pipeline: bool,
pub specialized_cache: SpecializedCache<RenderPipeline, BloomUpsamplingSpecializer>,
}

impl FromWorld for BloomUpsamplingPipeline {
Expand All @@ -60,18 +52,67 @@ impl FromWorld for BloomUpsamplingPipeline {
),
);

let fullscreen_shader = world.resource::<FullscreenShader>().clone();
let fragment_shader = load_embedded_asset!(world, "bloom.wgsl");
let base_descriptor = RenderPipelineDescriptor {
label: Some("bloom_upsampling_pipeline".into()),
layout: vec![bind_group_layout.clone()],
vertex: fullscreen_shader.to_vertex_state(),
fragment: Some(FragmentState {
shader: fragment_shader.clone(),
entry_point: Some("upsample".into()),
targets: vec![Some(ColorTargetState {
format: TextureFormat::Rgba8Unorm, // placeholder
blend: Some(BlendState {
// placeholder
color: BlendComponent {
src_factor: BlendFactor::Zero,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
alpha: BlendComponent {
src_factor: BlendFactor::Zero,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
}),
write_mask: ColorWrites::ALL,
})],
..default()
}),
..default()
};

let specialized_cache =
SpecializedCache::new(BloomUpsamplingSpecializer, None, base_descriptor);

BloomUpsamplingPipeline {
bind_group_layout,
fullscreen_shader: world.resource::<FullscreenShader>().clone(),
fragment_shader: load_embedded_asset!(world, "bloom.wgsl"),
specialized_cache,
}
}
}

impl SpecializedRenderPipeline for BloomUpsamplingPipeline {
type Key = BloomUpsamplingPipelineKeys;
pub struct BloomUpsamplingSpecializer;

#[derive(PartialEq, Eq, Hash, Clone, SpecializerKey)]
pub struct BloomUpsamplingKey {
composite_mode: BloomCompositeMode,
final_pipeline: bool,
}

impl Specializer<RenderPipeline> for BloomUpsamplingSpecializer {
type Key = BloomUpsamplingKey;

fn specialize(
&self,
key: Self::Key,
descriptor: &mut RenderPipelineDescriptor,
) -> Result<Canonical<Self::Key>, BevyError> {
let Some(fragment) = &mut descriptor.fragment else {
return Ok(key);
};

fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
let texture_format = if key.final_pipeline {
ViewTarget::TEXTURE_FORMAT_HDR
} else {
Expand Down Expand Up @@ -110,61 +151,53 @@ impl SpecializedRenderPipeline for BloomUpsamplingPipeline {
},
};

RenderPipelineDescriptor {
label: Some("bloom_upsampling_pipeline".into()),
layout: vec![self.bind_group_layout.clone()],
vertex: self.fullscreen_shader.to_vertex_state(),
fragment: Some(FragmentState {
shader: self.fragment_shader.clone(),
entry_point: Some("upsample".into()),
targets: vec![Some(ColorTargetState {
format: texture_format,
blend: Some(BlendState {
color: color_blend,
alpha: BlendComponent {
src_factor: BlendFactor::Zero,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
}),
write_mask: ColorWrites::ALL,
})],
..default()
let target = ColorTargetState {
format: texture_format,
blend: Some(BlendState {
// placeholder
color: color_blend,
alpha: BlendComponent {
src_factor: BlendFactor::Zero,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
}),
..default()
}
write_mask: ColorWrites::ALL,
};

fragment.set_target(0, target);

Ok(key)
}
}

pub fn prepare_upsampling_pipeline(
mut commands: Commands,
pipeline_cache: Res<PipelineCache>,
mut pipelines: ResMut<SpecializedRenderPipelines<BloomUpsamplingPipeline>>,
pipeline: Res<BloomUpsamplingPipeline>,
mut pipeline: ResMut<BloomUpsamplingPipeline>,
views: Query<(Entity, &Bloom)>,
) {
) -> Result<(), BevyError> {
for (entity, bloom) in &views {
let pipeline_id = pipelines.specialize(
let pipeline_id = pipeline.specialized_cache.specialize(
&pipeline_cache,
&pipeline,
BloomUpsamplingPipelineKeys {
BloomUpsamplingKey {
composite_mode: bloom.composite_mode,
final_pipeline: false,
},
);
)?;

let pipeline_final_id = pipelines.specialize(
let pipeline_final_id = pipeline.specialized_cache.specialize(
&pipeline_cache,
&pipeline,
BloomUpsamplingPipelineKeys {
BloomUpsamplingKey {
composite_mode: bloom.composite_mode,
final_pipeline: true,
},
);
)?;

commands.entity(entity).insert(UpsamplingPipelineIds {
id_main: pipeline_id,
id_final: pipeline_final_id,
});
}
Ok(())
}
Loading