-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add FullscreenMaterial #20414
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
Merged
alice-i-cecile
merged 28 commits into
bevyengine:main
from
IceSentry:fullscreen_material
Dec 14, 2025
Merged
Add FullscreenMaterial #20414
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f73ccfd
WIP post_processing plugin copy
IceSentry 79f8056
move plugin to bevy_pbr
IceSentry 8789c3e
add some comments
IceSentry 75c494a
customizable edges
IceSentry ec128ca
yeet changes to custom_post_processing
IceSentry 6596a79
move to core_pipeline
IceSentry 70e61bd
add custom sub_graph and more docs
IceSentry 92f0872
fix imports
IceSentry 9e003bd
fix example definition
IceSentry 1e94f5e
add release notes
IceSentry bb1a880
fix docs
IceSentry 8e06994
fix docs
IceSentry ab74519
fix docs
IceSentry 659b77a
compile_fail
IceSentry de97f88
fix docs
IceSentry 43cbadf
Merge branch 'main' into fullscreen_material
IceSentry 44947bb
Merge branch 'main' into fullscreen_material
IceSentry 8d37f58
Rename PostProcessSettings in shader
IceSentry 220a9f8
Merge branch 'main' into fullscreen_material
IceSentry 26456a1
fix after merge
IceSentry ce975ca
add automatic label generation
IceSentry bc096ec
fix ci
IceSentry a773d19
make sub_graph optional
IceSentry a613745
fix doc
IceSentry 0b0d384
Update crates/bevy_core_pipeline/src/fullscreen_material.rs
IceSentry 92a98e3
Update crates/bevy_core_pipeline/src/fullscreen_material.rs
IceSentry ae14418
Merge branch 'main' into fullscreen_material
alice-i-cecile 1d55f78
cargo fmt
alice-i-cecile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // This shader computes the chromatic aberration effect | ||
|
|
||
| // Since post processing is a fullscreen effect, we use the fullscreen vertex shader provided by bevy. | ||
| // This will import a vertex shader that renders a single fullscreen triangle. | ||
| // | ||
| // A fullscreen triangle is a single triangle that covers the entire screen. | ||
| // The box in the top left in that diagram is the screen. The 4 x are the corner of the screen | ||
| // | ||
| // Y axis | ||
| // 1 | x-----x...... | ||
| // 0 | | s | . ´ | ||
| // -1 | x_____x´ | ||
| // -2 | : .´ | ||
| // -3 | :´ | ||
| // +--------------- X axis | ||
| // -1 0 1 2 3 | ||
| // | ||
| // As you can see, the triangle ends up bigger than the screen. | ||
| // | ||
| // You don't need to worry about this too much since bevy will compute the correct UVs for you. | ||
| #import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput | ||
|
|
||
| @group(0) @binding(0) var screen_texture: texture_2d<f32>; | ||
| @group(0) @binding(1) var texture_sampler: sampler; | ||
| struct PostProcessSettings { | ||
| intensity: f32, | ||
| #ifdef SIXTEEN_BYTE_ALIGNMENT | ||
| // WebGL2 structs must be 16 byte aligned. | ||
| _webgl2_padding: vec3<f32> | ||
| #endif | ||
| } | ||
| @group(0) @binding(2) var<uniform> settings: PostProcessSettings; | ||
|
|
||
| @fragment | ||
| fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> { | ||
| // Chromatic aberration strength | ||
| let offset_strength = settings.intensity; | ||
|
|
||
| // Sample each color channel with an arbitrary shift | ||
| return vec4<f32>( | ||
| textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(offset_strength, -offset_strength)).r, | ||
| textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(-offset_strength, 0.0)).g, | ||
| textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(0.0, offset_strength)).b, | ||
| 1.0 | ||
| ); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| //! This is mostly a pluginified version of the `custom_post_processing` example | ||
| //! | ||
| //! The plugin will create a new node that runs a fullscreen triangle. | ||
| //! | ||
| //! Users need to use the [`FullscreenMaterial`] trait to define the parameters like the graph label or the graph ordering. | ||
|
|
||
| use core::marker::PhantomData; | ||
|
|
||
| use crate::FullscreenShader; | ||
| use bevy_app::{App, Plugin}; | ||
| use bevy_asset::AssetServer; | ||
| use bevy_ecs::{ | ||
| component::Component, | ||
| query::QueryItem, | ||
| resource::Resource, | ||
| system::{Commands, Res}, | ||
| world::World, | ||
| }; | ||
| use bevy_image::BevyDefault; | ||
| use bevy_render::{ | ||
| extract_component::{ | ||
| ComponentUniforms, DynamicUniformIndex, ExtractComponent, ExtractComponentPlugin, | ||
| UniformComponentPlugin, | ||
| }, | ||
| render_graph::{ | ||
| InternedRenderLabel, NodeRunError, RenderGraph, RenderGraphContext, RenderGraphError, | ||
| RenderGraphExt, RenderLabel, RenderSubGraph, ViewNode, ViewNodeRunner, | ||
| }, | ||
| render_resource::{ | ||
| binding_types::{sampler, texture_2d, uniform_buffer}, | ||
| encase::internal::WriteInto, | ||
| BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId, | ||
| ColorTargetState, ColorWrites, FragmentState, Operations, PipelineCache, | ||
| RenderPassColorAttachment, RenderPassDescriptor, RenderPipelineDescriptor, Sampler, | ||
| SamplerBindingType, SamplerDescriptor, ShaderRef, ShaderStages, ShaderType, TextureFormat, | ||
| TextureSampleType, | ||
| }, | ||
| renderer::{RenderContext, RenderDevice}, | ||
| view::ViewTarget, | ||
| RenderApp, RenderStartup, | ||
| }; | ||
| use bevy_utils::default; | ||
| use tracing::warn; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct FullscreenMaterialPlugin<T: FullscreenMaterial> { | ||
| _marker: PhantomData<T>, | ||
| } | ||
| impl<T: FullscreenMaterial> Plugin for FullscreenMaterialPlugin<T> { | ||
| fn build(&self, app: &mut App) { | ||
| app.add_plugins(( | ||
| ExtractComponentPlugin::<T>::default(), | ||
| UniformComponentPlugin::<T>::default(), | ||
| )); | ||
|
|
||
| let Some(render_app) = app.get_sub_app_mut(RenderApp) else { | ||
| return; | ||
| }; | ||
| render_app.add_systems(RenderStartup, init_pipeline::<T>); | ||
|
|
||
| render_app.add_render_graph_node::<ViewNodeRunner<FullscreenMaterialNode<T>>>( | ||
| T::sub_graph(), | ||
| T::node_label(), | ||
| ); | ||
| // We can't use add_render_graph_edges because it doesn't accept a Vec<RenderLabel> | ||
| if let Some(mut render_graph) = render_app.world_mut().get_resource_mut::<RenderGraph>() | ||
| && let Some(graph) = render_graph.get_sub_graph_mut(T::sub_graph()) | ||
| { | ||
| for window in T::node_edges().windows(2) { | ||
| let [a, b] = window else { | ||
| break; | ||
| }; | ||
| let Err(err) = graph.try_add_node_edge(*a, *b) else { | ||
| continue; | ||
| }; | ||
| match err { | ||
| // Already existing edges are very easy to produce with this api | ||
| // and shouldn't cause a panic | ||
| RenderGraphError::EdgeAlreadyExists(_) => {} | ||
| _ => panic!("{err:?}"), | ||
| } | ||
| } | ||
| } else { | ||
| warn!("Failed to add edges for FullscreenMaterial"); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// A trait to define a material that will render to the entire screen using a fullscrene triangle | ||
| pub trait FullscreenMaterial: | ||
| Component + ExtractComponent + Clone + Copy + ShaderType + WriteInto + Default | ||
| { | ||
| /// The shader that will run on the entire screen using a fullscreen triangle | ||
| fn fragment_shader() -> ShaderRef; | ||
| /// The [`RenderSubGraph`] the effect will run in | ||
| /// | ||
| /// For 2d this is generally [`crate::core_2d::graph::Core2d`] and for 3d it's [`crate::core_3d::graph::Core3d`] | ||
| fn sub_graph() -> impl RenderSubGraph; | ||
IceSentry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// The label used to represent the render node that will run the pass | ||
| fn node_label() -> impl RenderLabel; | ||
IceSentry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// The list of `node_edges`. In 3d, for a post processing effect, it would look like this: | ||
| /// | ||
| /// ```compile_fail | ||
| /// # use bevy_core_pipeline::core_3d::graph::Node3d; | ||
| /// # use bevy_render::render_graph::RenderLabel; | ||
| /// vec![ | ||
| /// Node3d::Tonemapping.intern(), | ||
| /// // Self::sub_graph().intern(), // <--- your own label here | ||
| /// Node3d::EndMainPassPostProcessing.intern(), | ||
| /// ] | ||
| /// ``` | ||
| /// | ||
| /// This tell the render graph to run your fullscreen effect after the tonemapping pass but | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment is wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's correct? It's just explaining the code snippet just above it. |
||
| /// before the end of post processing. For 2d, it would be the same but using Node2d. You can | ||
| /// specify any edges you want but make sure to include your own label. | ||
| fn node_edges() -> Vec<InternedRenderLabel>; | ||
| } | ||
|
|
||
| #[derive(Resource)] | ||
| struct FullscreenMaterialPipeline { | ||
| layout: BindGroupLayout, | ||
| sampler: Sampler, | ||
| pipeline_id: CachedRenderPipelineId, | ||
| pipeline_id_hdr: CachedRenderPipelineId, | ||
| } | ||
|
|
||
| fn init_pipeline<T: FullscreenMaterial>( | ||
| mut commands: Commands, | ||
| render_device: Res<RenderDevice>, | ||
| asset_server: Res<AssetServer>, | ||
| fullscreen_shader: Res<FullscreenShader>, | ||
| pipeline_cache: Res<PipelineCache>, | ||
| ) { | ||
| let layout = render_device.create_bind_group_layout( | ||
| "post_process_bind_group_layout", | ||
| &BindGroupLayoutEntries::sequential( | ||
| ShaderStages::FRAGMENT, | ||
| ( | ||
| // The screen texture | ||
| texture_2d(TextureSampleType::Float { filterable: true }), | ||
| // The sampler that will be used to sample the screen texture | ||
| sampler(SamplerBindingType::Filtering), | ||
| // We use a uniform buffer so users can pass some data to the effect | ||
| // Eventually we should just use a separate bind group for user data | ||
| uniform_buffer::<T>(true), | ||
| ), | ||
| ), | ||
| ); | ||
| let sampler = render_device.create_sampler(&SamplerDescriptor::default()); | ||
| let shader = match T::fragment_shader() { | ||
| ShaderRef::Default => { | ||
| // TODO not sure what an actual fallback should be. An empty shader or output a solid | ||
| // color to indicate a missing shader? | ||
| unimplemented!("No default fallback for FullscreenMaterial shader") | ||
IceSentry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ShaderRef::Handle(handle) => handle, | ||
| ShaderRef::Path(path) => asset_server.load(path), | ||
| }; | ||
| // Setup a fullscreen triangle for the vertex state. | ||
| let vertex_state = fullscreen_shader.to_vertex_state(); | ||
| let mut desc = RenderPipelineDescriptor { | ||
| label: Some("post_process_pipeline".into()), | ||
| layout: vec![layout.clone()], | ||
| vertex: vertex_state, | ||
| fragment: Some(FragmentState { | ||
| shader, | ||
| targets: vec![Some(ColorTargetState { | ||
| format: TextureFormat::bevy_default(), | ||
| blend: None, | ||
| write_mask: ColorWrites::ALL, | ||
| })], | ||
| ..default() | ||
| }), | ||
| ..default() | ||
| }; | ||
| let pipeline_id = pipeline_cache.queue_render_pipeline(desc.clone()); | ||
| desc.fragment.as_mut().unwrap().targets[0] | ||
| .as_mut() | ||
| .unwrap() | ||
| .format = ViewTarget::TEXTURE_FORMAT_HDR; | ||
| let pipeline_id_hdr = pipeline_cache.queue_render_pipeline(desc); | ||
| commands.insert_resource(FullscreenMaterialPipeline { | ||
| layout, | ||
| sampler, | ||
| pipeline_id, | ||
| pipeline_id_hdr, | ||
| }); | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct FullscreenMaterialNode<T: FullscreenMaterial> { | ||
| _marker: PhantomData<T>, | ||
| } | ||
|
|
||
| impl<T: FullscreenMaterial> ViewNode for FullscreenMaterialNode<T> { | ||
| // TODO we should expose the depth buffer and the gbuffer if using deferred | ||
| type ViewQuery = (&'static ViewTarget, &'static DynamicUniformIndex<T>); | ||
|
|
||
| fn run<'w>( | ||
| &self, | ||
| _graph: &mut RenderGraphContext, | ||
| render_context: &mut RenderContext, | ||
| (view_target, settings_index): QueryItem<Self::ViewQuery>, | ||
| world: &World, | ||
| ) -> Result<(), NodeRunError> { | ||
| let fullscreen_pipeline = world.resource::<FullscreenMaterialPipeline>(); | ||
|
|
||
| let pipeline_cache = world.resource::<PipelineCache>(); | ||
| let pipeline_id = if view_target.is_hdr() { | ||
| fullscreen_pipeline.pipeline_id_hdr | ||
| } else { | ||
| fullscreen_pipeline.pipeline_id | ||
| }; | ||
|
|
||
| let Some(pipeline) = pipeline_cache.get_render_pipeline(pipeline_id) else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let data_uniforms = world.resource::<ComponentUniforms<T>>(); | ||
| let Some(settings_binding) = data_uniforms.uniforms().binding() else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| // We should maybe rename this because this can be used for other reasons that aren't | ||
| // post-processing | ||
| let post_process = view_target.post_process_write(); | ||
|
|
||
| let bind_group = render_context.render_device().create_bind_group( | ||
| "post_process_bind_group", | ||
| &fullscreen_pipeline.layout, | ||
| &BindGroupEntries::sequential(( | ||
| post_process.source, | ||
| &fullscreen_pipeline.sampler, | ||
| settings_binding.clone(), | ||
| )), | ||
| ); | ||
|
|
||
| let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor { | ||
| label: Some("post_process_pass"), | ||
| color_attachments: &[Some(RenderPassColorAttachment { | ||
| view: post_process.destination, | ||
| depth_slice: None, | ||
| resolve_target: None, | ||
| ops: Operations::default(), | ||
| })], | ||
| depth_stencil_attachment: None, | ||
| timestamp_writes: None, | ||
| occlusion_query_set: None, | ||
| }); | ||
|
|
||
| render_pass.set_render_pipeline(pipeline); | ||
| render_pass.set_bind_group(0, &bind_group, &[settings_index.index()]); | ||
| render_pass.draw(0..3, 0..1); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.