From 6ba659049dffca30bb2e98dd3952aba588be74ea Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 27 Jan 2020 19:36:06 -0800 Subject: [PATCH] cleanup --- examples/shader_material.rs | 64 ------------------------ src/render/render_graph_2/draw_target.rs | 46 +++++++++-------- 2 files changed, 26 insertions(+), 84 deletions(-) delete mode 100644 examples/shader_material.rs diff --git a/examples/shader_material.rs b/examples/shader_material.rs deleted file mode 100644 index ed8db284ec..0000000000 --- a/examples/shader_material.rs +++ /dev/null @@ -1,64 +0,0 @@ -use bevy::{prelude::*, asset, render::{Albedo, render_graph_2::{StandardMaterial, ShaderUniforms}}}; - -fn main() { - AppBuilder::new().add_defaults_legacy().setup_world(setup).run(); -} - -fn setup(world: &mut World) { - let texture_handle = { - let mut texture_storage = world.resources.get_mut::>().unwrap(); - let texture = Texture::load(TextureType::Data(asset::create_texels(256))); - (texture_storage.add(texture)) - }; - - let mut color_shader_uniforms = ShaderUniforms::new(); - let color_material = StandardMaterial { - // albedo: Albedo::Color(math::vec4(1.0, 0.0, 0.0, 0.0)) - albedo: math::vec4(1.0, 0.0, 0.0, 0.0) - }; - - // color_shader_materials.add(color_material.get_selector()); - - // world.insert( - // (), - // vec![( - // color_shader_materials, - // color_material, - // )], - // ); - - // let mut texture_shader_materials = ShaderMaterials::new(); - // let texture_material = StandardMaterial { - // albedo: Albedo::Texture(texture_handle) - // }; - - // texture_shader_materials.add(texture_material.get_selector()); - - // world.insert( - // (), - // vec![( - // texture_shader_materials, - // texture_material, - // )], - // ); - - // for (entity, materials) in >::query().iter_entities(world) { - // println!("entity {}", entity); - // for selector in materials.materials.iter() { - // let shader_material = selector(entity, world).unwrap(); - // print!(" "); - // for property in shader_material.iter_properties() { - // println!("property: {}", property); - // print!(" "); - // match shader_material.get_property(property) { - // Some(a) => match a { - // ShaderValue::Vec4(color) => println!("color {}", color), - // ShaderValue::Texture(ref handle) => println!("tex {}", handle.id), - // _ => println!("other"), - // }, - // None => println!("none"), - // } - // } - // } - // } -} diff --git a/src/render/render_graph_2/draw_target.rs b/src/render/render_graph_2/draw_target.rs index e0046c30df..fbe11d8fd0 100644 --- a/src/render/render_graph_2/draw_target.rs +++ b/src/render/render_graph_2/draw_target.rs @@ -2,7 +2,7 @@ use crate::{ asset::{AssetStorage, Handle, Mesh}, legion::prelude::*, render::{ - render_graph_2::{ShaderUniforms, RenderPass}, + render_graph_2::{RenderPass, ShaderUniforms}, Instanced, }, }; @@ -10,47 +10,53 @@ use crate::{ use zerocopy::AsBytes; // 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); const MESH_VERTEX_BUFFER_NAME: &str = "TempMeshVertexBuffer"; const MESH_INDEX_BUFFER_NAME: &str = "TempMeshIndexBuffer"; pub fn mesh_draw_target(world: &World, render_pass: &mut dyn RenderPass) { let mut mesh_storage = world.resources.get_mut::>().unwrap(); - let mut last_mesh_id = None; + let mut current_mesh_id = None; + let mut current_mesh_index_length = 0; let mesh_query = <(Read, Read>)>::query().filter(!component::()); for (shader_uniforms, mesh) in mesh_query.iter(world) { - let current_mesh_id = mesh.id; - - let mut should_load_mesh = last_mesh_id == None; - if let Some(last) = last_mesh_id { - should_load_mesh = last != current_mesh_id; + 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(MESH_VERTEX_BUFFER_NAME, mesh_asset.vertices.as_bytes(), wgpu::BufferUsage::VERTEX); - renderer.create_buffer_with_data(MESH_INDEX_BUFFER_NAME, mesh_asset.indices.as_bytes(), wgpu::BufferUsage::INDEX); - + renderer.create_buffer_with_data( + MESH_VERTEX_BUFFER_NAME, + mesh_asset.vertices.as_bytes(), + wgpu::BufferUsage::VERTEX, + ); + renderer.create_buffer_with_data( + MESH_INDEX_BUFFER_NAME, + mesh_asset.indices.as_bytes(), + wgpu::BufferUsage::INDEX, + ); + // TODO: Verify buffer format matches render pass render_pass.set_index_buffer(MESH_INDEX_BUFFER_NAME, 0); render_pass.set_vertex_buffer(0, MESH_VERTEX_BUFFER_NAME, 0); + current_mesh_id = Some(mesh.id); + current_mesh_index_length = mesh_asset.indices.len() as u32; }; } - // TODO: re-getting the mesh isn't necessary. just store the index count - if let Some(mesh_asset) = mesh_storage.get(mesh.id) { - // TODO: validate bind group properties against shader uniform properties at least once - render_pass.setup_bind_groups(&&*shader_uniforms); - render_pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1); - }; - - last_mesh_id = Some(current_mesh_id); + // TODO: validate bind group properties against shader uniform properties at least once + render_pass.setup_bind_groups(&&*shader_uniforms); + render_pass.draw_indexed(0..current_mesh_index_length, 0, 0..1); } - + // cleanup buffers let renderer = render_pass.get_renderer(); renderer.remove_buffer(MESH_VERTEX_BUFFER_NAME); renderer.remove_buffer(MESH_INDEX_BUFFER_NAME); -} \ No newline at end of file +}