Skip to content

Commit 061baa4

Browse files
authored
Use dynamic uniform buffer in post processing example (#13540)
# Objective While learning about shaders and pipelines, I found this example to be misleading; it wasn't clear to me how the node knew what the correct "instance" of `PostProcessSettings` we should send to the shader (as the combination of `ExtractComponentPlugin` and `UniformComponentPlugin` extracts + sends _all_ of our `PostProcessSetting` components to the GPU). The goal of this PR is to clarify how to target the view specific `PostProcessSettings` in the shader when there are multiple cameras. ## Solution To accomplish this, we can use a dynamic uniform buffer for `PostProcessSettings`, querying for the relevant `DynamicUniformIndex` in the `PostProcessNode` to get the relevant index to use with the bind group. While the example in its current state is _correct_, I believe that fact that it's intended to showcase a per camera post processing effect warrants a dynamic uniform buffer (even though in the context of this example we have only one camera, and therefore no adverse behaviour). ## Testing - Run the `post_processing` example before and after this change, verifying they behave the same. ## Reviewer notes This is my first PR to Bevy, and I'm by no means an expert in the world of rendering (though I'm trying to learn all I can). If there's a better way to do this / a reason not to take this route, I'd love to hear it! Thanks in advance.
1 parent ba19815 commit 061baa4

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

examples/shader/post_processing.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use bevy::{
1515
prelude::*,
1616
render::{
1717
extract_component::{
18-
ComponentUniforms, ExtractComponent, ExtractComponentPlugin, UniformComponentPlugin,
18+
ComponentUniforms, DynamicUniformIndex, ExtractComponent, ExtractComponentPlugin,
19+
UniformComponentPlugin,
1920
},
2021
render_graph::{
2122
NodeRunError, RenderGraphApp, RenderGraphContext, RenderLabel, ViewNode, ViewNodeRunner,
@@ -127,6 +128,9 @@ impl ViewNode for PostProcessNode {
127128
&'static ViewTarget,
128129
// This makes sure the node only runs on cameras with the PostProcessSettings component
129130
&'static PostProcessSettings,
131+
// As there could be multiple post processing components sent to the GPU (one per camera),
132+
// we need to get the index of the one that is associated with the current view.
133+
&'static DynamicUniformIndex<PostProcessSettings>,
130134
);
131135

132136
// Runs the node logic
@@ -140,7 +144,7 @@ impl ViewNode for PostProcessNode {
140144
&self,
141145
_graph: &mut RenderGraphContext,
142146
render_context: &mut RenderContext,
143-
(view_target, _post_process_settings): QueryItem<Self::ViewQuery>,
147+
(view_target, _post_process_settings, settings_index): QueryItem<Self::ViewQuery>,
144148
world: &World,
145149
) -> Result<(), NodeRunError> {
146150
// Get the pipeline resource that contains the global data we need
@@ -212,7 +216,10 @@ impl ViewNode for PostProcessNode {
212216
// This is mostly just wgpu boilerplate for drawing a fullscreen triangle,
213217
// using the pipeline/bind_group created above
214218
render_pass.set_render_pipeline(pipeline);
215-
render_pass.set_bind_group(0, &bind_group, &[]);
219+
// By passing in the index of the post process settings on this view, we ensure
220+
// that in the event that multiple settings were sent to the GPU (as would be the
221+
// case with multiple cameras), we use the correct one.
222+
render_pass.set_bind_group(0, &bind_group, &[settings_index.index()]);
216223
render_pass.draw(0..3, 0..1);
217224

218225
Ok(())
@@ -243,7 +250,7 @@ impl FromWorld for PostProcessPipeline {
243250
// The sampler that will be used to sample the screen texture
244251
sampler(SamplerBindingType::Filtering),
245252
// The settings uniform that will control the effect
246-
uniform_buffer::<PostProcessSettings>(false),
253+
uniform_buffer::<PostProcessSettings>(true),
247254
),
248255
),
249256
);

0 commit comments

Comments
 (0)