From 428bb985c4d44fa5668b79c454e65fba8374383a Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sat, 8 Feb 2020 15:42:07 -0800 Subject: [PATCH] move mesh draw target to draw target folder --- src/render/render_graph_2/draw_target.rs | 61 +------------------ .../draw_targets/mesh_draw_target.rs | 59 ++++++++++++++++++ src/render/render_graph_2/draw_targets/mod.rs | 2 + .../render_graph_2/pipelines/forward/mod.rs | 10 ++- .../pipelines/forward_flat/mod.rs | 3 +- 5 files changed, 69 insertions(+), 66 deletions(-) create mode 100644 src/render/render_graph_2/draw_targets/mesh_draw_target.rs diff --git a/src/render/render_graph_2/draw_target.rs b/src/render/render_graph_2/draw_target.rs index 0f374e058c..8716859695 100644 --- a/src/render/render_graph_2/draw_target.rs +++ b/src/render/render_graph_2/draw_target.rs @@ -1,64 +1,7 @@ -use crate::{ - asset::{AssetStorage, Handle, Mesh}, - legion::prelude::*, - render::{ - render_graph_2::{resource_name, RenderPass, ShaderUniforms}, - Instanced, - }, -}; - -use zerocopy::AsBytes; +use crate::render::render_graph_2::RenderPass; +use legion::prelude::World; // A set of draw calls. ex: get + draw meshes, get + draw instanced meshes, draw ui meshes, etc // TODO: consider swapping out dyn RenderPass for explicit WgpuRenderPass type to avoid dynamic dispatch pub type DrawTarget = fn(world: &World, render_pass: &mut dyn RenderPass); - -pub fn mesh_draw_target(world: &World, render_pass: &mut dyn RenderPass) { - let mut mesh_storage = world.resources.get_mut::>().unwrap(); - let mut current_mesh_id = None; - let mut current_mesh_index_length = 0; - let mesh_query = - <(Read, Read>)>::query().filter(!component::()); - for (entity, (_shader_uniforms, mesh)) in mesh_query.iter_entities(world) { - let mut should_load_mesh = current_mesh_id == None; - if let Some(current) = current_mesh_id { - should_load_mesh = current != mesh.id; - } - - if should_load_mesh { - if let Some(mesh_asset) = mesh_storage.get(mesh.id) { - let renderer = render_pass.get_renderer(); - renderer.create_buffer_with_data( - resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME, - mesh_asset.vertices.as_bytes(), - wgpu::BufferUsage::VERTEX, - ); - renderer.create_buffer_with_data( - resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME, - mesh_asset.indices.as_bytes(), - wgpu::BufferUsage::INDEX, - ); - - // TODO: Verify buffer format matches render pass - render_pass.set_index_buffer(resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME, 0); - render_pass.set_vertex_buffer( - 0, - resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME, - 0, - ); - current_mesh_id = Some(mesh.id); - current_mesh_index_length = mesh_asset.indices.len() as u32; - }; - } - - // TODO: validate bind group properties against shader uniform properties at least once - render_pass.setup_bind_groups(Some(&entity)); - render_pass.draw_indexed(0..current_mesh_index_length, 0, 0..1); - } - - // cleanup buffers - let renderer = render_pass.get_renderer(); - renderer.remove_buffer(resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME); - renderer.remove_buffer(resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME); -} diff --git a/src/render/render_graph_2/draw_targets/mesh_draw_target.rs b/src/render/render_graph_2/draw_targets/mesh_draw_target.rs new file mode 100644 index 0000000000..62567bad5d --- /dev/null +++ b/src/render/render_graph_2/draw_targets/mesh_draw_target.rs @@ -0,0 +1,59 @@ +use crate::{ + asset::{AssetStorage, Handle, Mesh}, + legion::prelude::*, + render::{ + render_graph_2::{resource_name, RenderPass, ShaderUniforms}, + Instanced, + }, +}; + +use zerocopy::AsBytes; + +pub fn mesh_draw_target(world: &World, render_pass: &mut dyn RenderPass) { + let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut current_mesh_id = None; + let mut current_mesh_index_length = 0; + let mesh_query = + <(Read, Read>)>::query().filter(!component::()); + for (entity, (_shader_uniforms, mesh)) in mesh_query.iter_entities(world) { + let mut should_load_mesh = current_mesh_id == None; + if let Some(current) = current_mesh_id { + should_load_mesh = current != mesh.id; + } + + if should_load_mesh { + if let Some(mesh_asset) = mesh_storage.get(mesh.id) { + let renderer = render_pass.get_renderer(); + renderer.create_buffer_with_data( + resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME, + mesh_asset.vertices.as_bytes(), + wgpu::BufferUsage::VERTEX, + ); + renderer.create_buffer_with_data( + resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME, + mesh_asset.indices.as_bytes(), + wgpu::BufferUsage::INDEX, + ); + + // TODO: Verify buffer format matches render pass + render_pass.set_index_buffer(resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME, 0); + render_pass.set_vertex_buffer( + 0, + resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME, + 0, + ); + current_mesh_id = Some(mesh.id); + current_mesh_index_length = mesh_asset.indices.len() as u32; + }; + } + + // TODO: validate bind group properties against shader uniform properties at least once + render_pass.setup_bind_groups(Some(&entity)); + render_pass.draw_indexed(0..current_mesh_index_length, 0, 0..1); + } + + // cleanup buffers + let renderer = render_pass.get_renderer(); + renderer.remove_buffer(resource_name::buffer::TEMP_MESH_VERTEX_BUFFER_NAME); + renderer.remove_buffer(resource_name::buffer::TEMP_MESH_INDEX_BUFFER_NAME); +} \ No newline at end of file diff --git a/src/render/render_graph_2/draw_targets/mod.rs b/src/render/render_graph_2/draw_targets/mod.rs index 1bed6d285b..f13cc7152d 100644 --- a/src/render/render_graph_2/draw_targets/mod.rs +++ b/src/render/render_graph_2/draw_targets/mod.rs @@ -1,3 +1,5 @@ mod ui_draw_target; +mod mesh_draw_target; pub use ui_draw_target::*; +pub use mesh_draw_target::*; diff --git a/src/render/render_graph_2/pipelines/forward/mod.rs b/src/render/render_graph_2/pipelines/forward/mod.rs index f0ba67236a..f595ee273f 100644 --- a/src/render/render_graph_2/pipelines/forward/mod.rs +++ b/src/render/render_graph_2/pipelines/forward/mod.rs @@ -1,11 +1,9 @@ use crate::render::{ - Vertex, - { - render_graph_2::{ - mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder, - }, - shader::{Shader, ShaderStage}, + render_graph_2::{ + draw_targets::mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder, }, + shader::{Shader, ShaderStage}, + Vertex, }; pub trait ForwardPipelineBuilder { fn add_forward_pipeline(self) -> Self; diff --git a/src/render/render_graph_2/pipelines/forward_flat/mod.rs b/src/render/render_graph_2/pipelines/forward_flat/mod.rs index fceeb98327..602429cef9 100644 --- a/src/render/render_graph_2/pipelines/forward_flat/mod.rs +++ b/src/render/render_graph_2/pipelines/forward_flat/mod.rs @@ -2,7 +2,8 @@ use crate::render::{ Vertex, { render_graph_2::{ - mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder, + draw_targets::mesh_draw_target, pipeline_layout::*, PipelineDescriptor, + RenderGraphBuilder, }, shader::{Shader, ShaderStage}, },