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

[Merged by Bors] - Recreate tonemapping bind group if view uniforms buffer has changed #7904

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 18 additions & 10 deletions crates/bevy_core_pipeline/src/tonemapping/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use bevy_render::{
render_asset::RenderAssets,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_resource::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindingResource, LoadOp, Operations,
PipelineCache, RenderPassColorAttachment, RenderPassDescriptor, SamplerDescriptor,
TextureViewId,
BindGroup, BindGroupDescriptor, BindGroupEntry, BindingResource, BufferId, LoadOp,
Operations, PipelineCache, RenderPassColorAttachment, RenderPassDescriptor,
SamplerDescriptor, TextureViewId,
},
renderer::RenderContext,
texture::Image,
Expand All @@ -29,7 +29,7 @@ pub struct TonemappingNode {
),
With<ExtractedView>,
>,
cached_texture_bind_group: Mutex<Option<(TextureViewId, BindGroup)>>,
cached_bind_group: Mutex<Option<(BufferId, TextureViewId, BindGroup)>>,
last_tonemapping: Mutex<Option<Tonemapping>>,
}

Expand All @@ -39,7 +39,7 @@ impl TonemappingNode {
pub fn new(world: &mut World) -> Self {
Self {
query: QueryState::new(world),
cached_texture_bind_group: Mutex::new(None),
cached_bind_group: Mutex::new(None),
last_tonemapping: Mutex::new(None),
}
}
Expand All @@ -65,7 +65,8 @@ impl Node for TonemappingNode {
let tonemapping_pipeline = world.resource::<TonemappingPipeline>();
let gpu_images = world.get_resource::<RenderAssets<Image>>().unwrap();
let view_uniforms_resource = world.resource::<ViewUniforms>();
let view_uniforms = view_uniforms_resource.uniforms.binding().unwrap();
let view_uniforms = &view_uniforms_resource.uniforms;
let view_uniforms_id = view_uniforms.buffer().unwrap().id();

let (view_uniform_offset, target, view_tonemapping_pipeline, tonemapping) =
match self.query.get_manual(world, view_entity) {
Expand Down Expand Up @@ -97,9 +98,15 @@ impl Node for TonemappingNode {
*last_tonemapping = Some(*tonemapping);
}

let mut cached_bind_group = self.cached_texture_bind_group.lock().unwrap();
let mut cached_bind_group = self.cached_bind_group.lock().unwrap();
let bind_group = match &mut *cached_bind_group {
Some((id, bind_group)) if source.id() == *id && !tonemapping_changed => bind_group,
Some((buffer_id, texture_id, bind_group))
if view_uniforms_id == *buffer_id
&& source.id() == *texture_id
&& !tonemapping_changed =>
{
bind_group
}
cached_bind_group => {
let sampler = render_context
.render_device()
Expand All @@ -110,7 +117,7 @@ impl Node for TonemappingNode {
let mut entries = vec![
BindGroupEntry {
binding: 0,
resource: view_uniforms.clone(),
resource: view_uniforms.binding().unwrap(),
},
BindGroupEntry {
binding: 1,
Expand Down Expand Up @@ -138,7 +145,8 @@ impl Node for TonemappingNode {
entries: &entries,
});

let (_, bind_group) = cached_bind_group.insert((source.id(), bind_group));
let (_, _, bind_group) =
cached_bind_group.insert((view_uniforms_id, source.id(), bind_group));
bind_group
}
};
Expand Down