|
|
|
@ -5,6 +5,7 @@ use std::{iter, num::NonZeroU32, ops::Range, rc::Rc};
|
|
|
|
|
use crate::OPENGL_TO_WGPU_MATRIX;
|
|
|
|
|
use crate::light::LightRaw;
|
|
|
|
|
use crate::geometry::{Vertex, import_mesh, create_plane};
|
|
|
|
|
use wgpu::Buffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
@ -45,7 +46,11 @@ pub struct Renderer {
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Renderer {
|
|
|
|
@ -72,52 +77,58 @@ impl Renderer {
|
|
|
|
|
|
|
|
|
|
impl Renderer {
|
|
|
|
|
|
|
|
|
|
pub fn create_buffer(&mut self, device: &wgpu::Device) {
|
|
|
|
|
pub fn create_buffer(&mut self,
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
indices: Vec<u32>,
|
|
|
|
|
vertices: Vec<Vertex>) -> (Rc<Buffer>, Rc<Buffer>) {
|
|
|
|
|
|
|
|
|
|
// Creates the vertex and index buffers for the cube
|
|
|
|
|
let vertex_size = mem::size_of::<Vertex>();
|
|
|
|
|
let (cube_vertex_data, cube_index_data) = import_mesh("/home/mrh/source/3d-min-viable-eng/resources/my_tree.obj");
|
|
|
|
|
let cube_vertex_buf = Rc::new(device.create_buffer_init(
|
|
|
|
|
//import_mesh("/home/mrh/source/3d-min-viable-eng/resources/my_tree.obj");
|
|
|
|
|
|
|
|
|
|
let vertex_buf = Rc::new(device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Cubes Vertex Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&cube_vertex_data),
|
|
|
|
|
label: Some("vertex-buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&vertices),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
let cube_index_buf = Rc::new(device.create_buffer_init(
|
|
|
|
|
let index_buf = Rc::new(device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Cubes Index Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&cube_index_data),
|
|
|
|
|
label: Some("index-buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&indices),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Creates the vertex and index buffers for the plane
|
|
|
|
|
let (plane_vertex_data, plane_index_data) = create_plane(7.0);
|
|
|
|
|
let plane_vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Plane Vertex Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&plane_vertex_data),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let plane_index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Plane Index Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&plane_index_data),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
});
|
|
|
|
|
// // Creates the vertex and index buffers for the plane
|
|
|
|
|
// let (plane_vertex_data, plane_index_data) = create_plane(7.0);
|
|
|
|
|
// self.plane_vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
// label: Some("Plane Vertex Buffer"),
|
|
|
|
|
// contents: bytemuck::cast_slice(&plane_vertex_data),
|
|
|
|
|
// usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// self.plane_index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
// label: Some("Plane Index Buffer"),
|
|
|
|
|
// contents: bytemuck::cast_slice(&plane_index_data),
|
|
|
|
|
// usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// Creates the uniform for entities, which does the rotation and projection
|
|
|
|
|
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
|
|
|
|
|
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
|
self.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) -> Renderer {
|
|
|
|
|
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 =
|
|
|
|
@ -160,7 +171,6 @@ impl Renderer {
|
|
|
|
|
}],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
There appear to be two passes required for shadows, the shadow pass, and the forward pass
|
|
|
|
|
Need to open this up in renderdoc and see what it's actually doing
|
|
|
|
@ -309,16 +319,64 @@ impl Renderer {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
|
|
|
|
|
|
|
|
|
|
// I need to know the number of lights...
|
|
|
|
|
let forward_uniforms = ForwardUniforms {
|
|
|
|
|
proj: *mx_total.as_ref(),
|
|
|
|
|
num_lights: [lights.len() as u32, 0, 0, 0],
|
|
|
|
|
//num_lights: [lights.len() as u32, 0, 0, 0],
|
|
|
|
|
num_lights: [1 as u32, 0, 0, 0],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Uniform Buffer"),
|
|
|
|
|
contents: bytemuck::bytes_of(&forward_uniforms),
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let shadow_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
size: Self::SHADOW_SIZE,
|
|
|
|
|
mip_level_count: 1,
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
dimension: wgpu::TextureDimension::D2,
|
|
|
|
|
format: Self::SHADOW_FORMAT,
|
|
|
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
|
|
|
|
|
label: None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mut shadow_target_views = (0..2)
|
|
|
|
|
.map(|i| {
|
|
|
|
|
Some(shadow_texture.create_view(&wgpu::TextureViewDescriptor {
|
|
|
|
|
label: Some("shadow"),
|
|
|
|
|
format: None,
|
|
|
|
|
dimension: Some(wgpu::TextureViewDimension::D2),
|
|
|
|
|
aspect: wgpu::TextureAspect::All,
|
|
|
|
|
base_mip_level: 0,
|
|
|
|
|
level_count: None,
|
|
|
|
|
base_array_layer: i as u32,
|
|
|
|
|
array_layer_count: NonZeroU32::new(1),
|
|
|
|
|
}))
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
// shadow_target_views[0].take().unwrap(),
|
|
|
|
|
// pub(crate) target_view: wgpu::TextureView,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let shadow_view = shadow_texture.create_view(&wgpu::TextureViewDescriptor::default());
|
|
|
|
|
|
|
|
|
|
let shadow_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
|
|
|
label: Some("shadow"),
|
|
|
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
|
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
|
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
|
|
|
mag_filter: wgpu::FilterMode::Linear,
|
|
|
|
|
min_filter: wgpu::FilterMode::Linear,
|
|
|
|
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
|
|
|
compare: Some(wgpu::CompareFunction::LessEqual),
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create bind group
|
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout: &bind_group_layout,
|
|
|
|
|