move forward uniform setup to forward pass

This commit is contained in:
Carter Anderson 2019-12-05 09:29:26 -08:00
parent 6bdf0a5e14
commit 42fd25cfd9
3 changed files with 14 additions and 14 deletions

View file

@ -250,7 +250,6 @@ fn main() {
Application::run(universe, world, scheduler); Application::run(universe, world, scheduler);
} }
fn spawn_person(command_buffer: &mut CommandBuffer, mesh_handle: Handle<Mesh>) { fn spawn_person(command_buffer: &mut CommandBuffer, mesh_handle: Handle<Mesh>) {
command_buffer.insert((), vec![ command_buffer.insert((), vec![
( (

View file

@ -14,7 +14,7 @@ use std::mem;
use wgpu::{Surface, Device, Queue, SwapChain, SwapChainDescriptor}; 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 pub struct Application
{ {
@ -56,14 +56,7 @@ impl Application {
size: light_uniform_size, size: light_uniform_size,
}); });
let light_count = <Read<Light>>::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::<Vertex>(); let vertex_size = mem::size_of::<Vertex>();
let vb_desc = wgpu::VertexBufferDescriptor { let vb_desc = wgpu::VertexBufferDescriptor {
stride: vertex_size as wgpu::BufferAddress, stride: vertex_size as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex, 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 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(shadow_pass));
self.render_passes.push(Box::new(forward_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.width = width;
self.swap_chain_descriptor.height = height; self.swap_chain_descriptor.height = height;
self.swap_chain = self.device.create_swap_chain(&self.surface, &self.swap_chain_descriptor); self.swap_chain = self.device.create_swap_chain(&self.surface, &self.swap_chain_descriptor);
let mut encoder = let mut encoder =
self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
for (mut camera, local_to_world) in <(Write<Camera>, Read<LocalToWorld>)>::query().iter(&mut self.world) { for (mut camera, local_to_world) in <(Write<Camera>, Read<LocalToWorld>)>::query().iter(&mut self.world) {
camera.update(self.swap_chain_descriptor.width, self.swap_chain_descriptor.height); 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 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); self.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC);
for pass in self.render_passes.iter() { for pass in self.render_passes.iter() {
if let Some(buffer) = pass.get_camera_uniform_buffer() { 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);
} }
} }
} }

View file

@ -1,4 +1,4 @@
use crate::{render::*, asset::*, render::mesh::*}; use crate::{render::*, asset::*, render::mesh::*, math};
use legion::prelude::*; use legion::prelude::*;
use std::{mem, sync::Arc}; use std::{mem, sync::Arc};
use zerocopy::{AsBytes, FromBytes}; use zerocopy::{AsBytes, FromBytes};
@ -73,7 +73,7 @@ impl Pass for ForwardPass {
impl ForwardPass { impl ForwardPass {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn new(device: &Device, forward_uniforms: ForwardUniforms, light_uniform_buffer: Arc::<UniformBuffer>, 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::<UniformBuffer>, shadow_pass: &shadow::ShadowPass, vertex_buffer_descriptor: VertexBufferDescriptor, local_bind_group_layout: &BindGroupLayout, swap_chain_descriptor: &SwapChainDescriptor) -> ForwardPass {
let vs_bytes = shader::load_glsl( let vs_bytes = shader::load_glsl(
include_str!("forward.vert"), include_str!("forward.vert"),
shader::ShaderStage::Vertex, shader::ShaderStage::Vertex,
@ -112,6 +112,12 @@ impl ForwardPass {
], ],
}); });
let light_count = <Read<Light>>::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::<ForwardUniforms>() as wgpu::BufferAddress; let uniform_size = mem::size_of::<ForwardUniforms>() as wgpu::BufferAddress;
let forward_uniform_buffer = device.create_buffer_with_data( let forward_uniform_buffer = device.create_buffer_with_data(
forward_uniforms.as_bytes(), forward_uniforms.as_bytes(),