This commit is contained in:
Carter Anderson 2020-01-27 19:36:06 -08:00
parent 4675996e4f
commit 6ba659049d
2 changed files with 26 additions and 84 deletions

View file

@ -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::<AssetStorage<Texture>>().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 <Read<ShaderMaterials>>::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"),
// }
// }
// }
// }
}

View file

@ -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::<AssetStorage<Mesh>>().unwrap();
let mut last_mesh_id = None;
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 (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);
}
}