lots of hacking and gutting, but it compiles

This commit is contained in:
2021-01-31 23:59:23 -08:00
parent 3585c053ae
commit f933fe8312
5 changed files with 231 additions and 113 deletions

View File

@@ -2,11 +2,13 @@ use bytemuck::{Pod, Zeroable};
use bytemuck::__core::mem;
use wgpu::util::DeviceExt;
use std::{iter, num::NonZeroU32, ops::Range, rc::Rc};
use crate::OPENGL_TO_WGPU_MATRIX;
use crate::{OPENGL_TO_WGPU_MATRIX, Velocity};
use crate::light::LightRaw;
use crate::geometry::{Vertex, import_mesh, create_plane};
use wgpu::Buffer;
use wgpu::{Buffer, Device};
use winit::dpi::Position;
use winit::platform::unix::x11::ffi::Time;
use legion::*;
#[repr(C)]
#[derive(Clone, Copy)]
@@ -42,16 +44,16 @@ pub struct Pass {
}
pub struct Renderer {
device: Device,
device: Rc<Device>,
lights_are_dirty: bool,
shadow_pass: Pass,
forward_pass: Pass,
forward_depth: wgpu::TextureView,
light_uniform_buf: wgpu::Buffer,
plane_uniform_buf: wgpu::Buffer,
plane_vertex_buf: wgpu::Buffer,
plane_index_buf: wgpu::Buffer,
// plane_uniform_buf: wgpu::Buffer,
// plane_vertex_buf: wgpu::Buffer,
// plane_index_buf: wgpu::Buffer,
}
impl Renderer {
@@ -122,13 +124,13 @@ impl Renderer {
}
pub fn load_mesh_to_buffer(device: &wgpu::Device, filepath: &str) -> (Rc<Buffer>, Rc<Buffer>) {
pub fn load_mesh_to_buffer(device: Rc<wgpu::Device>, filepath: &str) -> (Rc<Buffer>, Rc<Buffer>) {
let (vertices, indices) = import_mesh(filepath);
Renderer::create_buffer(device, indices, vertices)
Renderer::create_buffer(&device, indices, vertices)
}
pub fn init(device: &wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Renderer {
pub fn init(device: Rc<wgpu::Device>, sc_desc: &wgpu::SwapChainDescriptor) -> Renderer {
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
@@ -469,17 +471,23 @@ impl Renderer {
});
Renderer {
device,
device: 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: ()
// plane_uniform_buf,
// plane_vertex_buf: (),
// plane_index_buf: ()
}
}
//
// #[system(for_each)]
// pub fn render_test(pos: &mut Position, vel: &Velocity) {
// //pos.x += vel.dx * time.elapsed_seconds;
// //pos.y += vel.dy * time.elapsed_seconds;
// }
pub fn render(
&mut self,
@@ -490,39 +498,42 @@ impl Renderer {
)
{
// update uniforms
for entity in self.entities.iter_mut() {
if entity.rotation_speed != 0.0 {
let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
entity.mx_world = entity.mx_world * rotation;
}
let data = EntityUniforms {
model: entity.mx_world.into(),
color: [
entity.color.r as f32,
entity.color.g as f32,
entity.color.b as f32,
entity.color.a as f32,
],
};
queue.write_buffer(&entity.uniform_buf, 0, bytemuck::bytes_of(&data));
}
// for entity in self.entities.iter_mut() {
//
// // Revolve the entity by the rotation speed, only if it is non-zero
// if entity.rotation_speed != 0.0 {
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
// entity.mx_world = entity.mx_world * rotation;
// }
//
// let data = EntityUniforms {
// model: entity.mx_world.into(),
// color: [
// entity.color.r as f32,
// entity.color.g as f32,
// entity.color.b as f32,
// entity.color.a as f32,
// ],
// };
// queue.write_buffer(&entity.uniform_buf, 0, bytemuck::bytes_of(&data));
// }
if self.lights_are_dirty {
self.lights_are_dirty = false;
for (i, light) in self.lights.iter().enumerate() {
queue.write_buffer(
&self.light_uniform_buf,
(i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
bytemuck::bytes_of(&light.to_raw()),
);
}
}
// if self.lights_are_dirty {
// self.lights_are_dirty = false;
// for (i, light) in self.lights.iter().enumerate() {
// queue.write_buffer(
// &self.light_uniform_buf,
// (i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
// bytemuck::bytes_of(&light.to_raw()),
// );
// }
// }
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
encoder.push_debug_group("shadow passes");
for (i, light) in self.lights.iter().enumerate() {
/*for (i, light) in self.lights.iter().enumerate() {
encoder.push_debug_group(&format!(
"shadow pass {} (light at position {:?})",
i, light.pos
@@ -565,7 +576,7 @@ impl Renderer {
}
encoder.pop_debug_group();
}
}*/
encoder.pop_debug_group();
// forward pass
@@ -597,18 +608,22 @@ impl Renderer {
pass.set_pipeline(&self.forward_pass.pipeline);
pass.set_bind_group(0, &self.forward_pass.bind_group, &[]);
for entity in &self.entities {
pass.set_bind_group(1, &entity.bind_group, &[]);
pass.set_index_buffer(entity.index_buf.slice(..));
pass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
pass.draw_indexed(0..entity.index_count as u32, 0, 0..1);
}
// for entity in &self.entities {
// pass.set_bind_group(1, &entity.bind_group, &[]);
// pass.set_index_buffer(entity.index_buf.slice(..));
// pass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
// pass.draw_indexed(0..entity.index_count as u32, 0, 0..1);
// }
}
encoder.pop_debug_group();
queue.submit(iter::once(encoder.finish()));
}
pub(crate) fn required_features() -> wgpu::Features {
wgpu::Features::empty()
}
pub fn optional_features() -> wgpu::Features {
wgpu::Features::DEPTH_CLAMPING
}