From 3585c053ae40177fe34902bcda0b72a8ae799056 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 31 Jan 2021 02:08:53 -0800 Subject: [PATCH] hacking --- src/main.rs | 10 ++++++---- src/render.rs | 29 +++++++++++++++++++++-------- src/runtime.rs | 44 ++++++++++++++++++++++---------------------- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1edbd99..fe80da8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,18 +212,20 @@ async fn main() { let mut swap_chain = device.create_swap_chain(&surface, &sc_desc); log::info!("Done doing the loading part..."); - // Init - let mut runtime = runtime::Runtime::init(&sc_desc, &device, &queue); - // Not sure why this is guarded, maybe we don't handle the event loop timing? #[cfg(not(target_arch = "wasm32"))] let mut last_update_inst = Instant::now(); - log::info!("Entering render loop..."); + // Load up the renderer (and the resources) let mut renderer = render::Renderer::init(&device, &sc_desc); + let (plane_vertex_buffer, plane_index_buffer) = Renderer::load_mesh_to_buffer(device, "plane.obj"); + + // Init, this wants the references to the buffers... + let mut runtime = runtime::Runtime::init(&sc_desc, &device, &queue); + // This is just an winit event loop event_loop.run(move |event, _, control_flow| { diff --git a/src/render.rs b/src/render.rs index 7004e15..2ff4523 100644 --- a/src/render.rs +++ b/src/render.rs @@ -42,6 +42,7 @@ pub struct Pass { } pub struct Renderer { + device: Device, lights_are_dirty: bool, shadow_pass: Pass, forward_pass: Pass, @@ -77,8 +78,7 @@ impl Renderer { impl Renderer { - pub fn create_buffer(&mut self, - device: &wgpu::Device, + pub fn create_buffer(device: &wgpu::Device, indices: Vec, vertices: Vec) -> (Rc, Rc) { @@ -117,18 +117,27 @@ impl Renderer { // }); // Creates the uniform for entities, which does the rotation and projection + + (vertex_buf, index_buf) + } + + + pub fn load_mesh_to_buffer(device: &wgpu::Device, filepath: &str) -> (Rc, Rc) { + let (vertices, indices) = import_mesh(filepath); + Renderer::create_buffer(device, indices, vertices) + } + + + pub fn init(device: &wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Renderer { + + let entity_uniform_size = mem::size_of::() as wgpu::BufferAddress; - self.plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { + let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { label: None, size: entity_uniform_size, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, mapped_at_creation: false, }); - (vertex_buf, index_buf) - - } - - pub fn init(device: &wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Renderer { // Pre init the light uniform, with slots enough for MAX_LIGHTS let light_uniform_size = @@ -460,11 +469,15 @@ impl Renderer { }); Renderer { + device, lights_are_dirty: false, shadow_pass, forward_pass, forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), light_uniform_buf, + plane_uniform_buf, + plane_vertex_buf: (), + plane_index_buf: () } } diff --git a/src/runtime.rs b/src/runtime.rs index eb5d3cc..22b880a 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,8 +1,11 @@ use std::rc::Rc; + use bytemuck::__core::mem; +use bytemuck::__core::num::NonZeroU32; +use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3, SquareMatrix}; + use crate::light::Light; use crate::render::EntityUniforms; -use bytemuck::__core::num::NonZeroU32; /* @@ -17,10 +20,12 @@ struct Entity { mx_world: cgmath::Matrix4, rotation_speed: f32, color: wgpu::Color, - vertex_buf: Rc, // Could probably tie this along with index & count to some resource handle in the renderer + vertex_buf: Rc, + // Could probably tie this along with index & count to some resource handle in the renderer index_buf: Rc, index_count: usize, - bind_group: wgpu::BindGroup, // This is a little weird to have in the entity isn't it? + bind_group: wgpu::BindGroup, + // This is a little weird to have in the entity isn't it? uniform_buf: wgpu::Buffer, } @@ -81,29 +86,26 @@ impl Runtime { }); - let mut entities = vec![{ - use cgmath::SquareMatrix; + let mut entities = Vec::default(); - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + entities.push(Entity { + mx_world: cgmath::Matrix4::identity(), + rotation_speed: 0.0, + color: wgpu::Color::WHITE, + vertex_buf: Rc::new(plane_vertex_buf), + index_buf: Rc::new(plane_index_buf), + index_count: plane_index_data.len(), + bind_group: device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &local_bind_group_layout, entries: &[wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::Buffer(plane_uniform_buf.slice(..)), }], label: None, - }); + }), + uniform_buf: plane_uniform_buf, + }); - Entity { - mx_world: cgmath::Matrix4::identity(), - rotation_speed: 0.0, - color: wgpu::Color::WHITE, - vertex_buf: Rc::new(plane_vertex_buf), - index_buf: Rc::new(plane_index_buf), - index_count: plane_index_data.len(), - bind_group, - uniform_buf: plane_uniform_buf, - } - }]; struct CubeDesc { offset: cgmath::Vector3, @@ -123,8 +125,6 @@ impl Runtime { for cube in &cube_descs { - use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3}; - let transform = Decomposed { disp: cube.offset.clone(), rot: Quaternion::from_axis_angle(cube.offset.normalize(), Deg(cube.angle)), @@ -155,9 +155,9 @@ impl Runtime { }); } - // Create other resources +// Create other resources - // This is just metadata we hold for the lights. We can hold onto this +// This is just metadata we hold for the lights. We can hold onto this let lights = vec![ Light { pos: cgmath::Point3::new(7.0, -5.0, 10.0),