Skip to content
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

Use dynamic uniform buffer in post processing example #13540

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all 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
15 changes: 11 additions & 4 deletions examples/shader/post_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use bevy::{
prelude::*,
render::{
extract_component::{
ComponentUniforms, ExtractComponent, ExtractComponentPlugin, UniformComponentPlugin,
ComponentUniforms, DynamicUniformIndex, ExtractComponent, ExtractComponentPlugin,
UniformComponentPlugin,
},
render_graph::{
NodeRunError, RenderGraphApp, RenderGraphContext, RenderLabel, ViewNode, ViewNodeRunner,
Expand Down Expand Up @@ -124,6 +125,9 @@ impl ViewNode for PostProcessNode {
&'static ViewTarget,
// This makes sure the node only runs on cameras with the PostProcessSettings component
&'static PostProcessSettings,
// As there could be multiple post processing components sent to the GPU (one per camera),
// we need to get the index of the one that is associated with the current view.
&'static DynamicUniformIndex<PostProcessSettings>,
);

// Runs the node logic
Expand All @@ -137,7 +141,7 @@ impl ViewNode for PostProcessNode {
&self,
_graph: &mut RenderGraphContext,
render_context: &mut RenderContext,
(view_target, _post_process_settings): QueryItem<Self::ViewQuery>,
(view_target, _post_process_settings, settings_index): QueryItem<Self::ViewQuery>,
world: &World,
) -> Result<(), NodeRunError> {
// Get the pipeline resource that contains the global data we need
Expand Down Expand Up @@ -209,7 +213,10 @@ impl ViewNode for PostProcessNode {
// This is mostly just wgpu boilerplate for drawing a fullscreen triangle,
// using the pipeline/bind_group created above
render_pass.set_render_pipeline(pipeline);
render_pass.set_bind_group(0, &bind_group, &[]);
// By passing in the index of the post process settings on this view, we ensure
// that in the event that multiple settings were sent to the GPU (as would be the
// case with multiple cameras), we use the correct one.
render_pass.set_bind_group(0, &bind_group, &[settings_index.index()]);
render_pass.draw(0..3, 0..1);

Ok(())
Expand Down Expand Up @@ -240,7 +247,7 @@ impl FromWorld for PostProcessPipeline {
// The sampler that will be used to sample the screen texture
sampler(SamplerBindingType::Filtering),
// The settings uniform that will control the effect
uniform_buffer::<PostProcessSettings>(false),
uniform_buffer::<PostProcessSettings>(true),
),
),
);
Expand Down