move mesh draw target to draw target folder

This commit is contained in:
Carter Anderson 2020-02-08 15:42:07 -08:00
parent ebcb925d6f
commit 428bb985c4
5 changed files with 69 additions and 66 deletions

View file

@ -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::<AssetStorage<Mesh>>().unwrap();
let mut current_mesh_id = None;
let mut current_mesh_index_length = 0;
let mesh_query =
<(Read<ShaderUniforms>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
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);
}

View file

@ -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::<AssetStorage<Mesh>>().unwrap();
let mut current_mesh_id = None;
let mut current_mesh_index_length = 0;
let mesh_query =
<(Read<ShaderUniforms>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
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);
}

View file

@ -1,3 +1,5 @@
mod ui_draw_target;
mod mesh_draw_target;
pub use ui_draw_target::*;
pub use mesh_draw_target::*;

View file

@ -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;

View file

@ -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},
},