Skip to content

Commit 727b0f6

Browse files
jgayfermockersf
authored andcommitted
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 7c0b1b9 commit 727b0f6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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,
@@ -124,6 +125,9 @@ impl ViewNode for PostProcessNode {
124125
&'static ViewTarget,
125126
// This makes sure the node only runs on cameras with the PostProcessSettings component
126127
&'static PostProcessSettings,
128+
// As there could be multiple post processing components sent to the GPU (one per camera),
129+
// we need to get the index of the one that is associated with the current view.
130+
&'static DynamicUniformIndex<PostProcessSettings>,
127131
);
128132

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

215222
Ok(())
@@ -240,7 +247,7 @@ impl FromWorld for PostProcessPipeline {
240247
// The sampler that will be used to sample the screen texture
241248
sampler(SamplerBindingType::Filtering),
242249
// The settings uniform that will control the effect
243-
uniform_buffer::<PostProcessSettings>(false),
250+
uniform_buffer::<PostProcessSettings>(true),
244251
),
245252
),
246253
);

0 commit comments

Comments
 (0)