mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
move mesh draw target to draw target folder
This commit is contained in:
parent
ebcb925d6f
commit
428bb985c4
5 changed files with 69 additions and 66 deletions
|
@ -1,64 +1,7 @@
|
||||||
use crate::{
|
use crate::render::render_graph_2::RenderPass;
|
||||||
asset::{AssetStorage, Handle, Mesh},
|
use legion::prelude::World;
|
||||||
legion::prelude::*,
|
|
||||||
render::{
|
|
||||||
render_graph_2::{resource_name, RenderPass, ShaderUniforms},
|
|
||||||
Instanced,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zerocopy::AsBytes;
|
|
||||||
|
|
||||||
// A set of draw calls. ex: get + draw meshes, get + draw instanced meshes, draw ui meshes, etc
|
// 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
|
// 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 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);
|
|
||||||
}
|
|
||||||
|
|
59
src/render/render_graph_2/draw_targets/mesh_draw_target.rs
Normal file
59
src/render/render_graph_2/draw_targets/mesh_draw_target.rs
Normal 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);
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
mod ui_draw_target;
|
mod ui_draw_target;
|
||||||
|
mod mesh_draw_target;
|
||||||
|
|
||||||
pub use ui_draw_target::*;
|
pub use ui_draw_target::*;
|
||||||
|
pub use mesh_draw_target::*;
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use crate::render::{
|
use crate::render::{
|
||||||
Vertex,
|
render_graph_2::{
|
||||||
{
|
draw_targets::mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder,
|
||||||
render_graph_2::{
|
|
||||||
mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder,
|
|
||||||
},
|
|
||||||
shader::{Shader, ShaderStage},
|
|
||||||
},
|
},
|
||||||
|
shader::{Shader, ShaderStage},
|
||||||
|
Vertex,
|
||||||
};
|
};
|
||||||
pub trait ForwardPipelineBuilder {
|
pub trait ForwardPipelineBuilder {
|
||||||
fn add_forward_pipeline(self) -> Self;
|
fn add_forward_pipeline(self) -> Self;
|
||||||
|
|
|
@ -2,7 +2,8 @@ use crate::render::{
|
||||||
Vertex,
|
Vertex,
|
||||||
{
|
{
|
||||||
render_graph_2::{
|
render_graph_2::{
|
||||||
mesh_draw_target, pipeline_layout::*, PipelineDescriptor, RenderGraphBuilder,
|
draw_targets::mesh_draw_target, pipeline_layout::*, PipelineDescriptor,
|
||||||
|
RenderGraphBuilder,
|
||||||
},
|
},
|
||||||
shader::{Shader, ShaderStage},
|
shader::{Shader, ShaderStage},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue