From 42fd25cfd9b587610eca3889411d80223109e6d1 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 5 Dec 2019 09:29:26 -0800 Subject: [PATCH] move forward uniform setup to forward pass --- examples/simple.rs | 1 - src/application.rs | 17 ++++++----------- src/render/forward/mod.rs | 10 ++++++++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index c86aaf2d7c..8119569f5a 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -250,7 +250,6 @@ fn main() { Application::run(universe, world, scheduler); } - fn spawn_person(command_buffer: &mut CommandBuffer, mesh_handle: Handle) { command_buffer.insert((), vec![ ( diff --git a/src/application.rs b/src/application.rs index dd20808faa..45c73e7c7d 100644 --- a/src/application.rs +++ b/src/application.rs @@ -14,7 +14,7 @@ use std::mem; use wgpu::{Surface, Device, Queue, SwapChain, SwapChainDescriptor}; -use crate::{vertex::*, render::*, math, LocalToWorld, ApplicationStage, Time}; +use crate::{vertex::*, render::*, LocalToWorld, ApplicationStage, Time}; pub struct Application { @@ -56,14 +56,7 @@ impl Application { size: light_uniform_size, }); - let light_count = >::query().iter(&mut self.world).count(); - let forward_uniforms = ForwardUniforms { - proj: math::Mat4::identity().to_cols_array_2d(), - num_lights: [light_count as u32, 0, 0, 0], - }; - let vertex_size = mem::size_of::(); - let vb_desc = wgpu::VertexBufferDescriptor { stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::InputStepMode::Vertex, @@ -82,7 +75,7 @@ impl Application { }; let shadow_pass = ShadowPass::new(&mut self.device, &mut self.world, light_uniform_buffer.clone(), vb_desc.clone(), local_bind_group_layout.clone(), Self::MAX_LIGHTS as u32); - let forward_pass = ForwardPass::new(&mut self.device, forward_uniforms, light_uniform_buffer.clone(), &shadow_pass, vb_desc, &local_bind_group_layout, &self.swap_chain_descriptor); + let forward_pass = ForwardPass::new(&mut self.device, &self.world, light_uniform_buffer.clone(), &shadow_pass, vb_desc, &local_bind_group_layout, &self.swap_chain_descriptor); self.render_passes.push(Box::new(shadow_pass)); self.render_passes.push(Box::new(forward_pass)); } @@ -105,17 +98,19 @@ impl Application { self.swap_chain_descriptor.width = width; self.swap_chain_descriptor.height = height; self.swap_chain = self.device.create_swap_chain(&self.surface, &self.swap_chain_descriptor); + let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); for (mut camera, local_to_world) in <(Write, Read)>::query().iter(&mut self.world) { camera.update(self.swap_chain_descriptor.width, self.swap_chain_descriptor.height); let camera_matrix: [[f32; 4]; 4] = (camera.view_matrix * local_to_world.0).to_cols_array_2d(); - let temp_buf = + let matrix_size = mem::size_of::<[[f32; 4]; 4]>() as u64; + let temp_camera_buffer = self.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); for pass in self.render_passes.iter() { if let Some(buffer) = pass.get_camera_uniform_buffer() { - encoder.copy_buffer_to_buffer(&temp_buf, 0, buffer, 0, 64); + encoder.copy_buffer_to_buffer(&temp_camera_buffer, 0, buffer, 0, matrix_size); } } } diff --git a/src/render/forward/mod.rs b/src/render/forward/mod.rs index edfa342e21..4c0cd9e013 100644 --- a/src/render/forward/mod.rs +++ b/src/render/forward/mod.rs @@ -1,4 +1,4 @@ -use crate::{render::*, asset::*, render::mesh::*}; +use crate::{render::*, asset::*, render::mesh::*, math}; use legion::prelude::*; use std::{mem, sync::Arc}; use zerocopy::{AsBytes, FromBytes}; @@ -73,7 +73,7 @@ impl Pass for ForwardPass { impl ForwardPass { pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; - pub fn new(device: &Device, forward_uniforms: ForwardUniforms, light_uniform_buffer: Arc::, shadow_pass: &shadow::ShadowPass, vertex_buffer_descriptor: VertexBufferDescriptor, local_bind_group_layout: &BindGroupLayout, swap_chain_descriptor: &SwapChainDescriptor) -> ForwardPass { + pub fn new(device: &Device, world: &World, light_uniform_buffer: Arc::, shadow_pass: &shadow::ShadowPass, vertex_buffer_descriptor: VertexBufferDescriptor, local_bind_group_layout: &BindGroupLayout, swap_chain_descriptor: &SwapChainDescriptor) -> ForwardPass { let vs_bytes = shader::load_glsl( include_str!("forward.vert"), shader::ShaderStage::Vertex, @@ -112,6 +112,12 @@ impl ForwardPass { ], }); + let light_count = >::query().iter_immutable(world).count(); + let forward_uniforms = ForwardUniforms { + proj: math::Mat4::identity().to_cols_array_2d(), + num_lights: [light_count as u32, 0, 0, 0], + }; + let uniform_size = mem::size_of::() as wgpu::BufferAddress; let forward_uniform_buffer = device.create_buffer_with_data( forward_uniforms.as_bytes(),