diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 0a0b75891d..3a4fe6b8b2 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -38,9 +38,9 @@ use bevy_type_registry::RegisterType; use draw::{clear_draw_system, draw_system, Draw}; use legion::prelude::IntoSystem; use mesh::mesh_resource_provider_system; -use pipeline::{RenderPipelines, compile_pipelines_system}; +use pipeline::{compile_pipelines_system, RenderPipelines}; use render_graph::{system::render_graph_schedule_executor_system, RenderGraph}; -use render_resource::bind_groups_system; +use render_resource::{bind_groups_system, AssetRenderResourceBindings}; use std::ops::Range; use texture::{PngTextureLoader, TextureResourceSystemState}; @@ -92,6 +92,7 @@ impl AppPlugin for RenderPlugin { .init_resource::() .init_resource::() .init_resource::() + .init_resource::() .add_system_to_stage(bevy_app::stage::PRE_UPDATE, clear_draw_system.system()) .init_system_to_stage( bevy_app::stage::POST_UPDATE, @@ -115,10 +116,7 @@ impl AppPlugin for RenderPlugin { stage::RENDER_GRAPH_SYSTEMS, compile_pipelines_system.system(), ) - .add_system_to_stage( - stage::RENDER_GRAPH_SYSTEMS, - bind_groups_system.system(), - ) + .add_system_to_stage(stage::RENDER_GRAPH_SYSTEMS, bind_groups_system.system()) .add_system_to_stage(stage::DRAW, draw_system::.system()); if let Some(ref config) = self.base_render_graph_config { diff --git a/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs b/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs index 72e43949d8..465222ea15 100644 --- a/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs @@ -12,7 +12,7 @@ use crate::{ use bevy_asset::{Assets, Handle}; use legion::prelude::*; -use render_resource::{BufferId, RenderResourceType}; +use render_resource::{AssetRenderResourceBindings, BufferId, RenderResourceType}; use std::{collections::HashMap, marker::PhantomData}; pub const BIND_BUFFER_ALIGNMENT: usize = 256; @@ -497,12 +497,11 @@ where let mut command_queue = self.command_queue.clone(); let mut uniform_buffer_arrays = UniformBufferArrays::::default(); // let mut asset_event_reader = EventReader::>::default(); - let mut asset_render_resource_bindings = - HashMap::, RenderResourceBindings>::default(); let dynamic_uniforms = self.dynamic_uniforms; (move |world: &mut SubWorld, assets: Res>, // asset_events: Res>>, + mut asset_render_resource_bindings: ResMut, render_resource_context: Res>, query: &mut Query<(Read>, Read, Write)>| { let render_resource_context = &**render_resource_context; @@ -540,9 +539,8 @@ where for asset_handle in modified_assets.iter() { let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); - let mut render_resource_bindings = asset_render_resource_bindings - .entry(*asset_handle) - .or_insert_with(|| RenderResourceBindings::default()); + let mut render_resource_bindings = + asset_render_resource_bindings.get_or_insert_mut(*asset_handle); setup_uniform_texture_resources::( &asset, render_resource_context, @@ -554,9 +552,8 @@ where let mut staging_buffer: [u8; 0] = []; for asset_handle in modified_assets.iter() { let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); - let mut render_resource_bindings = asset_render_resource_bindings - .entry(*asset_handle) - .or_insert_with(|| RenderResourceBindings::default()); + let mut render_resource_bindings = + asset_render_resource_bindings.get_or_insert_mut(*asset_handle); // TODO: only setup buffer if we haven't seen this handle before uniform_buffer_arrays.setup_uniform_buffer_resources( &asset, @@ -576,9 +573,8 @@ where &mut |mut staging_buffer, _render_resource_context| { for asset_handle in modified_assets.iter() { let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); - let mut render_resource_bindings = asset_render_resource_bindings - .entry(*asset_handle) - .or_insert_with(|| RenderResourceBindings::default()); + let mut render_resource_bindings = + asset_render_resource_bindings.get_or_insert_mut(*asset_handle); // TODO: only setup buffer if we haven't seen this handle before uniform_buffer_arrays.setup_uniform_buffer_resources( &asset, @@ -597,7 +593,7 @@ where } for (asset_handle, _draw, mut render_pipelines) in query.iter_mut(world) { - if let Some(asset_bindings) = asset_render_resource_bindings.get(&asset_handle) { + if let Some(asset_bindings) = asset_render_resource_bindings.get(*asset_handle) { render_pipelines.bindings.extend(asset_bindings); } } diff --git a/crates/bevy_render/src/render_resource/render_resource_bindings.rs b/crates/bevy_render/src/render_resource/render_resource_bindings.rs index 5bdd59ec11..d4a575ae44 100644 --- a/crates/bevy_render/src/render_resource/render_resource_bindings.rs +++ b/crates/bevy_render/src/render_resource/render_resource_bindings.rs @@ -6,6 +6,7 @@ use std::{ ops::Range, }; use uuid::Uuid; +use bevy_asset::{HandleUntyped, Handle}; #[derive(Clone, Eq, PartialEq, Debug)] pub enum RenderResourceBinding { @@ -186,6 +187,21 @@ impl RenderResourceBindings { } } +#[derive(Default)] +pub struct AssetRenderResourceBindings { + pub bindings: HashMap, +} + +impl AssetRenderResourceBindings { + pub fn get(&self, handle: Handle) -> Option<&RenderResourceBindings> { + self.bindings.get(&HandleUntyped::from(handle)) + } + + pub fn get_or_insert_mut(&mut self, handle: Handle) -> &mut RenderResourceBindings { + self.bindings.entry(HandleUntyped::from(handle)).or_insert_with(|| RenderResourceBindings::default()) + } +} + #[derive(Hash, Eq, PartialEq, Debug, Copy, Clone)] pub struct RenderResourceBindingsId(Uuid);