render: add AssetRenderResourceBindings

This commit is contained in:
Carter Anderson 2020-06-15 12:45:18 -07:00
parent f799d3ac93
commit 8a704a3e28
3 changed files with 29 additions and 19 deletions

View file

@ -38,9 +38,9 @@ use bevy_type_registry::RegisterType;
use draw::{clear_draw_system, draw_system, Draw}; use draw::{clear_draw_system, draw_system, Draw};
use legion::prelude::IntoSystem; use legion::prelude::IntoSystem;
use mesh::mesh_resource_provider_system; 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_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 std::ops::Range;
use texture::{PngTextureLoader, TextureResourceSystemState}; use texture::{PngTextureLoader, TextureResourceSystemState};
@ -92,6 +92,7 @@ impl AppPlugin for RenderPlugin {
.init_resource::<RenderResourceBindings>() .init_resource::<RenderResourceBindings>()
.init_resource::<VertexBufferDescriptors>() .init_resource::<VertexBufferDescriptors>()
.init_resource::<TextureResourceSystemState>() .init_resource::<TextureResourceSystemState>()
.init_resource::<AssetRenderResourceBindings>()
.add_system_to_stage(bevy_app::stage::PRE_UPDATE, clear_draw_system.system()) .add_system_to_stage(bevy_app::stage::PRE_UPDATE, clear_draw_system.system())
.init_system_to_stage( .init_system_to_stage(
bevy_app::stage::POST_UPDATE, bevy_app::stage::POST_UPDATE,
@ -115,10 +116,7 @@ impl AppPlugin for RenderPlugin {
stage::RENDER_GRAPH_SYSTEMS, stage::RENDER_GRAPH_SYSTEMS,
compile_pipelines_system.system(), compile_pipelines_system.system(),
) )
.add_system_to_stage( .add_system_to_stage(stage::RENDER_GRAPH_SYSTEMS, bind_groups_system.system())
stage::RENDER_GRAPH_SYSTEMS,
bind_groups_system.system(),
)
.add_system_to_stage(stage::DRAW, draw_system::<RenderPipelines>.system()); .add_system_to_stage(stage::DRAW, draw_system::<RenderPipelines>.system());
if let Some(ref config) = self.base_render_graph_config { if let Some(ref config) = self.base_render_graph_config {

View file

@ -12,7 +12,7 @@ use crate::{
use bevy_asset::{Assets, Handle}; use bevy_asset::{Assets, Handle};
use legion::prelude::*; use legion::prelude::*;
use render_resource::{BufferId, RenderResourceType}; use render_resource::{AssetRenderResourceBindings, BufferId, RenderResourceType};
use std::{collections::HashMap, marker::PhantomData}; use std::{collections::HashMap, marker::PhantomData};
pub const BIND_BUFFER_ALIGNMENT: usize = 256; pub const BIND_BUFFER_ALIGNMENT: usize = 256;
@ -497,12 +497,11 @@ where
let mut command_queue = self.command_queue.clone(); let mut command_queue = self.command_queue.clone();
let mut uniform_buffer_arrays = UniformBufferArrays::<T>::default(); let mut uniform_buffer_arrays = UniformBufferArrays::<T>::default();
// let mut asset_event_reader = EventReader::<AssetEvent<T>>::default(); // let mut asset_event_reader = EventReader::<AssetEvent<T>>::default();
let mut asset_render_resource_bindings =
HashMap::<Handle<T>, RenderResourceBindings>::default();
let dynamic_uniforms = self.dynamic_uniforms; let dynamic_uniforms = self.dynamic_uniforms;
(move |world: &mut SubWorld, (move |world: &mut SubWorld,
assets: Res<Assets<T>>, assets: Res<Assets<T>>,
// asset_events: Res<Events<AssetEvent<T>>>, // asset_events: Res<Events<AssetEvent<T>>>,
mut asset_render_resource_bindings: ResMut<AssetRenderResourceBindings>,
render_resource_context: Res<Box<dyn RenderResourceContext>>, render_resource_context: Res<Box<dyn RenderResourceContext>>,
query: &mut Query<(Read<Handle<T>>, Read<Draw>, Write<RenderPipelines>)>| { query: &mut Query<(Read<Handle<T>>, Read<Draw>, Write<RenderPipelines>)>| {
let render_resource_context = &**render_resource_context; let render_resource_context = &**render_resource_context;
@ -540,9 +539,8 @@ where
for asset_handle in modified_assets.iter() { for asset_handle in modified_assets.iter() {
let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE);
let mut render_resource_bindings = asset_render_resource_bindings let mut render_resource_bindings =
.entry(*asset_handle) asset_render_resource_bindings.get_or_insert_mut(*asset_handle);
.or_insert_with(|| RenderResourceBindings::default());
setup_uniform_texture_resources::<T>( setup_uniform_texture_resources::<T>(
&asset, &asset,
render_resource_context, render_resource_context,
@ -554,9 +552,8 @@ where
let mut staging_buffer: [u8; 0] = []; let mut staging_buffer: [u8; 0] = [];
for asset_handle in modified_assets.iter() { for asset_handle in modified_assets.iter() {
let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE);
let mut render_resource_bindings = asset_render_resource_bindings let mut render_resource_bindings =
.entry(*asset_handle) asset_render_resource_bindings.get_or_insert_mut(*asset_handle);
.or_insert_with(|| RenderResourceBindings::default());
// TODO: only setup buffer if we haven't seen this handle before // TODO: only setup buffer if we haven't seen this handle before
uniform_buffer_arrays.setup_uniform_buffer_resources( uniform_buffer_arrays.setup_uniform_buffer_resources(
&asset, &asset,
@ -576,9 +573,8 @@ where
&mut |mut staging_buffer, _render_resource_context| { &mut |mut staging_buffer, _render_resource_context| {
for asset_handle in modified_assets.iter() { for asset_handle in modified_assets.iter() {
let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE); let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE);
let mut render_resource_bindings = asset_render_resource_bindings let mut render_resource_bindings =
.entry(*asset_handle) asset_render_resource_bindings.get_or_insert_mut(*asset_handle);
.or_insert_with(|| RenderResourceBindings::default());
// TODO: only setup buffer if we haven't seen this handle before // TODO: only setup buffer if we haven't seen this handle before
uniform_buffer_arrays.setup_uniform_buffer_resources( uniform_buffer_arrays.setup_uniform_buffer_resources(
&asset, &asset,
@ -597,7 +593,7 @@ where
} }
for (asset_handle, _draw, mut render_pipelines) in query.iter_mut(world) { 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); render_pipelines.bindings.extend(asset_bindings);
} }
} }

View file

@ -6,6 +6,7 @@ use std::{
ops::Range, ops::Range,
}; };
use uuid::Uuid; use uuid::Uuid;
use bevy_asset::{HandleUntyped, Handle};
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug)]
pub enum RenderResourceBinding { pub enum RenderResourceBinding {
@ -186,6 +187,21 @@ impl RenderResourceBindings {
} }
} }
#[derive(Default)]
pub struct AssetRenderResourceBindings {
pub bindings: HashMap<HandleUntyped, RenderResourceBindings>,
}
impl AssetRenderResourceBindings {
pub fn get<T>(&self, handle: Handle<T>) -> Option<&RenderResourceBindings> {
self.bindings.get(&HandleUntyped::from(handle))
}
pub fn get_or_insert_mut<T>(&mut self, handle: Handle<T>) -> &mut RenderResourceBindings {
self.bindings.entry(HandleUntyped::from(handle)).or_insert_with(|| RenderResourceBindings::default())
}
}
#[derive(Hash, Eq, PartialEq, Debug, Copy, Clone)] #[derive(Hash, Eq, PartialEq, Debug, Copy, Clone)]
pub struct RenderResourceBindingsId(Uuid); pub struct RenderResourceBindingsId(Uuid);