master
mitchellhansen 4 years ago
parent f44f8201b9
commit 3585c053ae

@ -212,18 +212,20 @@ async fn main() {
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc); let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
log::info!("Done doing the loading part..."); 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? // Not sure why this is guarded, maybe we don't handle the event loop timing?
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let mut last_update_inst = Instant::now(); let mut last_update_inst = Instant::now();
log::info!("Entering render loop..."); log::info!("Entering render loop...");
// Load up the renderer (and the resources)
let mut renderer = render::Renderer::init(&device, &sc_desc); 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 // This is just an winit event loop
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {

@ -42,6 +42,7 @@ pub struct Pass {
} }
pub struct Renderer { pub struct Renderer {
device: Device,
lights_are_dirty: bool, lights_are_dirty: bool,
shadow_pass: Pass, shadow_pass: Pass,
forward_pass: Pass, forward_pass: Pass,
@ -77,8 +78,7 @@ impl Renderer {
impl Renderer { impl Renderer {
pub fn create_buffer(&mut self, pub fn create_buffer(device: &wgpu::Device,
device: &wgpu::Device,
indices: Vec<u32>, indices: Vec<u32>,
vertices: Vec<Vertex>) -> (Rc<Buffer>, Rc<Buffer>) { vertices: Vec<Vertex>) -> (Rc<Buffer>, Rc<Buffer>) {
@ -117,18 +117,27 @@ impl Renderer {
// }); // });
// Creates the uniform for entities, which does the rotation and projection // 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<Buffer>, Rc<Buffer>) {
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::<EntityUniforms>() as wgpu::BufferAddress; let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
self.plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: None, label: None,
size: entity_uniform_size, size: entity_uniform_size,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
mapped_at_creation: false, 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 // Pre init the light uniform, with slots enough for MAX_LIGHTS
let light_uniform_size = let light_uniform_size =
@ -460,11 +469,15 @@ impl Renderer {
}); });
Renderer { Renderer {
device,
lights_are_dirty: false, lights_are_dirty: false,
shadow_pass, shadow_pass,
forward_pass, forward_pass,
forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()),
light_uniform_buf, light_uniform_buf,
plane_uniform_buf,
plane_vertex_buf: (),
plane_index_buf: ()
} }
} }

@ -1,8 +1,11 @@
use std::rc::Rc; use std::rc::Rc;
use bytemuck::__core::mem; use bytemuck::__core::mem;
use bytemuck::__core::num::NonZeroU32;
use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3, SquareMatrix};
use crate::light::Light; use crate::light::Light;
use crate::render::EntityUniforms; use crate::render::EntityUniforms;
use bytemuck::__core::num::NonZeroU32;
/* /*
@ -17,10 +20,12 @@ struct Entity {
mx_world: cgmath::Matrix4<f32>, mx_world: cgmath::Matrix4<f32>,
rotation_speed: f32, rotation_speed: f32,
color: wgpu::Color, color: wgpu::Color,
vertex_buf: Rc<wgpu::Buffer>, // Could probably tie this along with index & count to some resource handle in the renderer vertex_buf: Rc<wgpu::Buffer>,
// Could probably tie this along with index & count to some resource handle in the renderer
index_buf: Rc<wgpu::Buffer>, index_buf: Rc<wgpu::Buffer>,
index_count: usize, 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, uniform_buf: wgpu::Buffer,
} }
@ -81,29 +86,26 @@ impl Runtime {
}); });
let mut entities = vec![{ let mut entities = Vec::default();
use cgmath::SquareMatrix;
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, layout: &local_bind_group_layout,
entries: &[wgpu::BindGroupEntry { entries: &[wgpu::BindGroupEntry {
binding: 0, binding: 0,
resource: wgpu::BindingResource::Buffer(plane_uniform_buf.slice(..)), resource: wgpu::BindingResource::Buffer(plane_uniform_buf.slice(..)),
}], }],
label: None, 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 { struct CubeDesc {
offset: cgmath::Vector3<f32>, offset: cgmath::Vector3<f32>,
@ -123,8 +125,6 @@ impl Runtime {
for cube in &cube_descs { for cube in &cube_descs {
use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3};
let transform = Decomposed { let transform = Decomposed {
disp: cube.offset.clone(), disp: cube.offset.clone(),
rot: Quaternion::from_axis_angle(cube.offset.normalize(), Deg(cube.angle)), 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![ let lights = vec![
Light { Light {
pos: cgmath::Point3::new(7.0, -5.0, 10.0), pos: cgmath::Point3::new(7.0, -5.0, 10.0),

Loading…
Cancel
Save