From c5f781b4e575f65ec9baa329167a6ad5a7621f36 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sun, 8 Mar 2020 20:27:07 -0700 Subject: [PATCH] fix ui draw target --- src/render/render_graph/draw_target.rs | 10 +- .../assigned_meshes_draw_target.rs | 4 +- .../draw_targets/meshes_draw_target.rs | 4 +- .../draw_targets/ui_draw_target.rs | 132 +++++++++--------- src/render/render_graph/render_graph.rs | 6 +- src/render/render_graph/renderer.rs | 2 +- .../wgpu_renderer/wgpu_render_pass.rs | 2 +- 7 files changed, 76 insertions(+), 84 deletions(-) diff --git a/src/render/render_graph/draw_target.rs b/src/render/render_graph/draw_target.rs index 1525740f85..5fd00170cc 100644 --- a/src/render/render_graph/draw_target.rs +++ b/src/render/render_graph/draw_target.rs @@ -6,15 +6,7 @@ use crate::{ 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, - pipeline_handle: Handle, -); - -pub trait NewDrawTarget { +pub trait DrawTarget { fn draw( &self, world: &World, diff --git a/src/render/render_graph/draw_targets/assigned_meshes_draw_target.rs b/src/render/render_graph/draw_targets/assigned_meshes_draw_target.rs index 16cd927526..040441ad11 100644 --- a/src/render/render_graph/draw_targets/assigned_meshes_draw_target.rs +++ b/src/render/render_graph/draw_targets/assigned_meshes_draw_target.rs @@ -2,7 +2,7 @@ use crate::{ asset::{AssetStorage, Handle, Mesh}, legion::prelude::*, render::render_graph::{ - resource_name, NewDrawTarget, PipelineDescriptor, RenderPass, Renderable, ResourceInfo, + resource_name, DrawTarget, PipelineDescriptor, RenderPass, Renderable, ResourceInfo, ShaderPipelineAssignments, }, }; @@ -10,7 +10,7 @@ use crate::{ #[derive(Default)] pub struct AssignedMeshesDrawTarget; -impl NewDrawTarget for AssignedMeshesDrawTarget { +impl DrawTarget for AssignedMeshesDrawTarget { fn draw( &self, world: &World, diff --git a/src/render/render_graph/draw_targets/meshes_draw_target.rs b/src/render/render_graph/draw_targets/meshes_draw_target.rs index 0e081324ea..accc8e1de3 100644 --- a/src/render/render_graph/draw_targets/meshes_draw_target.rs +++ b/src/render/render_graph/draw_targets/meshes_draw_target.rs @@ -3,7 +3,7 @@ use crate::{ legion::prelude::*, render::{ render_graph::{ - resource_name, NewDrawTarget, PipelineDescriptor, RenderPass, Renderable, ResourceInfo, + resource_name, DrawTarget, PipelineDescriptor, RenderPass, Renderable, ResourceInfo, }, Instanced, }, @@ -12,7 +12,7 @@ use crate::{ #[derive(Default)] pub struct MeshesDrawTarget; -impl NewDrawTarget for MeshesDrawTarget { +impl DrawTarget for MeshesDrawTarget { fn draw( &self, world: &World, diff --git a/src/render/render_graph/draw_targets/ui_draw_target.rs b/src/render/render_graph/draw_targets/ui_draw_target.rs index fe39d31ab6..9a4794d0e0 100644 --- a/src/render/render_graph/draw_targets/ui_draw_target.rs +++ b/src/render/render_graph/draw_targets/ui_draw_target.rs @@ -2,92 +2,92 @@ use crate::{ asset::{AssetStorage, Handle, Mesh}, legion::prelude::*, render::render_graph::{ - resource_name, NewDrawTarget, PipelineDescriptor, RenderPass, ResourceInfo, + resource_name, DrawTarget, PipelineDescriptor, RenderPass, RenderResource, ResourceInfo, }, }; use zerocopy::AsBytes; #[derive(Default)] -pub struct UiDrawTarget; +pub struct UiDrawTarget { + pub mesh_vertex_buffer: Option, + pub mesh_index_buffer: Option, + pub mesh_index_length: usize, +} -impl NewDrawTarget for UiDrawTarget { +impl DrawTarget for UiDrawTarget { fn draw( &self, _world: &World, - _render_pass: &mut dyn RenderPass, + render_pass: &mut dyn RenderPass, _pipeline_handle: Handle, ) { - // TODO: re-add support for this - // let mesh_storage = world.resources.get_mut::>().unwrap(); - // let mut current_mesh_vertex_buffer = None; - // let mut current_mesh_index_buffer = None; - // let ui_instances_buffer = { - // let renderer = render_pass.get_renderer(); - // match renderer.get_render_resources().get_named_resource(resource_name::buffer::UI_INSTANCES) { - // Some(buffer) => buffer, - // None => return, - // } - // }; - // // NOTE: this is ugly and borrowing is stupid - // let result = { - // let renderer = render_pass.get_renderer(); - // let result = if let Some(ResourceInfo::InstanceBuffer { count, mesh_id, .. }) = - // renderer.get_resource_info(ui_instances_buffer) - // { - // Some((*count, *mesh_id)) - // } else { - // None - // }; + let ui_instances_buffer = { + let renderer = render_pass.get_renderer(); + match renderer + .get_render_resources() + .get_named_resource(resource_name::buffer::UI_INSTANCES) + { + Some(buffer) => buffer, + None => return, + } + }; - // if let Some((instance_count, mesh_id)) = result { - // if let Some(mesh_asset) = mesh_storage.get_id(mesh_id) { - // if let Some(buffer) = current_mesh_vertex_buffer { - // renderer.remove_buffer(buffer); - // } + let index_count = { + let renderer = render_pass.get_renderer(); + if let Some(ResourceInfo::InstanceBuffer { count, .. }) = + renderer.get_resource_info(ui_instances_buffer) + { + Some(*count) + } else { + None + } + }; - // if let Some(buffer) = current_mesh_index_buffer { - // renderer.remove_buffer(buffer); - // } - // current_mesh_vertex_buffer = Some(renderer.create_buffer_with_data( - // mesh_asset.vertices.as_bytes(), - // wgpu::BufferUsage::VERTEX, - // )); - // current_mesh_index_buffer = Some(renderer.create_buffer_with_data( - // mesh_asset.indices.as_bytes(), - // wgpu::BufferUsage::INDEX, - // )); - // Some((instance_count, mesh_asset.indices.len())) - // } else { - // None - // } - // } else { - // None - // } - // }; - // if let Some((instance_count, indices_length)) = result { - // render_pass.setup_bind_groups(None); - // render_pass.set_index_buffer(current_mesh_index_buffer.unwrap(), 0); - // render_pass.set_vertex_buffer(0, current_mesh_vertex_buffer.unwrap(), 0); - // render_pass.set_vertex_buffer(1, ui_instances_buffer, 0); - // render_pass.draw_indexed(0..indices_length as u32, 0, 0..(instance_count as u32)); - // } + render_pass.set_bind_groups(None); + render_pass.set_index_buffer(self.mesh_index_buffer.unwrap(), 0); + render_pass.set_vertex_buffer(0, self.mesh_vertex_buffer.unwrap(), 0); + render_pass.set_vertex_buffer(1, ui_instances_buffer, 0); + render_pass.draw_indexed(0..self.mesh_index_length as u32, 0, 0..(index_count.unwrap() as u32)); - // let renderer = render_pass.get_renderer(); - // if let Some(buffer) = current_mesh_vertex_buffer { - // renderer.remove_buffer(buffer); - // } - - // if let Some(buffer) = current_mesh_index_buffer { - // renderer.remove_buffer(buffer); - // } } fn setup( &mut self, - _world: &World, - _renderer: &mut dyn crate::render::render_graph::Renderer, + world: &World, + renderer: &mut dyn crate::render::render_graph::Renderer, _pipeline_handle: Handle, ) { + // don't create meshes if they have already been created + if let Some(_) = self.mesh_vertex_buffer { + return; + } + + let ui_instances_buffer = { + match renderer + .get_render_resources() + .get_named_resource(resource_name::buffer::UI_INSTANCES) + { + Some(buffer) => buffer, + None => return, + } + }; + + if let ResourceInfo::InstanceBuffer { mesh_id, .. } = + renderer.get_resource_info(ui_instances_buffer).unwrap() + { + let mesh_storage = world.resources.get_mut::>().unwrap(); + if let Some(mesh_asset) = mesh_storage.get_id(*mesh_id) { + self.mesh_vertex_buffer = Some(renderer.create_buffer_with_data( + mesh_asset.vertices.as_bytes(), + wgpu::BufferUsage::VERTEX, + )); + self.mesh_index_buffer = Some(renderer.create_buffer_with_data( + mesh_asset.indices.as_bytes(), + wgpu::BufferUsage::INDEX, + )); + self.mesh_index_length = mesh_asset.indices.len(); + }; + } } fn get_name(&self) -> String { resource_name::draw_target::UI.to_string() diff --git a/src/render/render_graph/render_graph.rs b/src/render/render_graph/render_graph.rs index 84cbc8920f..0371817d33 100644 --- a/src/render/render_graph/render_graph.rs +++ b/src/render/render_graph/render_graph.rs @@ -1,5 +1,5 @@ use super::{ - NewDrawTarget, PassDescriptor, PipelineDescriptor, ResourceProvider, TextureDescriptor, + DrawTarget, PassDescriptor, PipelineDescriptor, ResourceProvider, TextureDescriptor, }; use crate::asset::{AssetStorage, Handle}; use std::collections::{HashMap, HashSet}; @@ -11,7 +11,7 @@ pub struct RenderGraph { pub pass_pipelines: HashMap>>, pub resource_providers: Vec>, pub queued_textures: Vec<(String, TextureDescriptor)>, - pub draw_targets: HashMap>, + pub draw_targets: HashMap>, } impl Default for RenderGraph { @@ -102,7 +102,7 @@ impl RenderGraphBuilder { pub fn add_draw_target(mut self, draw_target: T) -> Self where - T: NewDrawTarget + 'static, + T: DrawTarget + 'static, { self.render_graph .draw_targets diff --git a/src/render/render_graph/renderer.rs b/src/render/render_graph/renderer.rs index ca592f2327..4c06d5bbe9 100644 --- a/src/render/render_graph/renderer.rs +++ b/src/render/render_graph/renderer.rs @@ -100,7 +100,7 @@ pub trait Renderer { pub trait RenderPass { // TODO: consider using static dispatch for the renderer: Renderer. compare compile times - fn get_renderer(&mut self) -> &dyn Renderer; + fn get_renderer(&self) -> &dyn Renderer; fn get_pipeline_descriptor(&self) -> &PipelineDescriptor; fn set_index_buffer(&mut self, resource: RenderResource, offset: u64); fn set_vertex_buffer(&mut self, start_slot: u32, resource: RenderResource, offset: u64); diff --git a/src/render/render_graph/renderers/wgpu_renderer/wgpu_render_pass.rs b/src/render/render_graph/renderers/wgpu_renderer/wgpu_render_pass.rs index bc4f305ae8..b3639746bd 100644 --- a/src/render/render_graph/renderers/wgpu_renderer/wgpu_render_pass.rs +++ b/src/render/render_graph/renderers/wgpu_renderer/wgpu_render_pass.rs @@ -12,7 +12,7 @@ pub struct WgpuRenderPass<'a, 'b, 'c, 'd> { } impl<'a, 'b, 'c, 'd> RenderPass for WgpuRenderPass<'a, 'b, 'c, 'd> { - fn get_renderer(&mut self) -> &dyn Renderer { + fn get_renderer(&self) -> &dyn Renderer { self.renderer }