From 28fb0fdfc8f922cc3b3ee190feaaf07020ee873d Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 17 Mar 2020 13:09:51 -0700 Subject: [PATCH] Reflect Vertex Buffer Attributes Must follow VertexBufferDescriptorName_AttributeName format I_VertexBufferDescriptorName_AttributeName indicates that an attribute is instanced Currently all attributes must be defined in shaders or offsets will be incorrect. --- examples/custom_shader.rs | 6 +- examples/instancing.rs | 239 +++++++----------- examples/instancing_old.rs | 172 +++++++++++++ src/render/pipeline/pipeline.rs | 11 +- .../pipeline/pipelines/forward/forward.vert | 30 ++- src/render/pipeline/pipelines/forward/mod.rs | 2 - .../pipeline/pipelines/forward_flat/mod.rs | 2 - src/render/pipeline/pipelines/ui/mod.rs | 34 +-- src/render/pipeline/pipelines/ui/ui.vert | 22 +- .../pipeline/vertex_buffer_descriptor.rs | 43 +++- src/render/renderable.rs | 16 +- .../renderers/wgpu_renderer/wgpu_renderer.rs | 19 +- src/render/shader/shader_reflect.rs | 220 +++++++++++++++- src/render/shader/uniforms/local_to_world.rs | 2 +- src/render/vertex.rs | 28 -- 15 files changed, 597 insertions(+), 249 deletions(-) create mode 100644 examples/instancing_old.rs diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index 0f32c9008b..806dcffbaf 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -25,7 +25,9 @@ fn main() { ShaderStage::Vertex, r#" #version 450 - layout(location = 0) in vec4 a_Pos; + layout(location = 0) in vec4 Vertex_Position; + layout(location = 1) in vec4 Vertex_Normal; + layout(location = 2) in vec2 Vertex_Uv; layout(location = 0) out vec4 v_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; @@ -34,7 +36,7 @@ fn main() { mat4 Model; }; void main() { - v_Position = Model * a_Pos; + v_Position = Model * Vertex_Position; gl_Position = ViewProj * v_Position; } "#, diff --git a/examples/instancing.rs b/examples/instancing.rs index 156e499e0e..769a633aad 100644 --- a/examples/instancing.rs +++ b/examples/instancing.rs @@ -1,172 +1,107 @@ use bevy::prelude::*; -use rand::{random, rngs::StdRng, Rng, SeedableRng}; - -struct Person; - -struct Velocity { - pub value: math::Vec3, -} - -struct NavigationPoint { - pub target: math::Vec3, -} - -struct Wander { - pub duration_bounds: math::Vec2, - pub distance_bounds: math::Vec2, - pub duration: f32, - pub elapsed: f32, -} +use rand::{rngs::StdRng, Rng, SeedableRng}; fn main() { AppBuilder::new() - .setup_world(setup) - .add_system(build_wander_system()) - .add_system(build_navigate_system()) + .add_defaults() .add_system(build_move_system()) .add_system(bevy::diagnostics::build_fps_printer_system()) + .setup_world(setup) .run(); } -fn setup(world: &mut World, resources: &mut Resources) { - let mut mesh_storage = resources.get_mut::>().unwrap(); - let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube)); - - world.insert( - (), - vec![ - // lights - ( - Light::default(), - LocalToWorld::identity(), - Translation::new(4.0, -4.0, 5.0), - Rotation::from_euler_angles(0.0, 0.0, 0.0), - ), - ], - ); - - world.insert( - (), - vec![ - // camera - ( - Camera::new(CameraType::Projection { - fov: std::f32::consts::PI / 4.0, - near: 1.0, - far: 1000.0, - aspect_ratio: 1.0, - }), - ActiveCamera, - LocalToWorld(Mat4::look_at_rh( - Vec3::new(6.0, -40.0, 20.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 0.0, 1.0), - )), - ), - ], - ); - - let mut rng = StdRng::from_entropy(); - for _ in 0..70000 { - create_person( - world, - cube_handle, - Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)), - ); - } -} - -fn build_wander_system() -> Box { - let mut rng = StdRng::from_entropy(); - - SystemBuilder::new("Wander") - .read_resource::