diff --git a/Cargo.toml b/Cargo.toml index f0371b8..31e9094 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,7 @@ version = "0.1.0" authors = ["mitchellhansen "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -wgpu = "0.6.0" arrayvec = "0.5" futures = "0.3" parking_lot = "0.11" @@ -16,8 +13,6 @@ smallvec = "1" tracing = { version = "0.1", default-features = false, features = ["std"] } typed-arena = "2.0.1" serde = { version = "1", features = ["derive"], optional = true } -gfx-backend-vulkan = { version = "0.6", features = ["x11"] } -cgmath = "0.17" log = "0.4" png = "0.16" winit = { version = "0.22.1", features = ["web-sys"] } @@ -27,7 +22,11 @@ noise = "0.6" ddsfile = "0.4" wgpu-subscriber = "0.1.0" tobj = "2.0.3" -legion = "0.3.1" +gilrs = "0.8.0" +gfx-backend-vulkan = { version = "0.6", features = ["x11"] } + +cgmath = "0.18.0" +rapier3d = { version = "0.5.0", features = ["simd-nightly", "parallel"] } nalgebra = "0.24.1" -rapier3d = { version = "0.5.0", features = [ "simd-nightly", "parallel" ] } -gilrs = "0.8.0" \ No newline at end of file +legion = "0.3.1" +wgpu = "0.7.0" \ No newline at end of file diff --git a/src/components.rs b/src/components.rs index 6bc1358..a344949 100644 --- a/src/components.rs +++ b/src/components.rs @@ -50,6 +50,7 @@ pub struct RangeCopy { pub struct Mesh { pub index_buffer: Arc, pub index_count: usize, + pub index_format: wgpu::IndexFormat, pub vertex_buffer: Arc, pub uniform_buffer: Arc, pub bind_group: Arc, diff --git a/src/geometry.rs b/src/geometry.rs index f9ac72e..573184d 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -19,18 +19,12 @@ pub fn vertex(pos: [f32; 3], nor: [f32; 3]) -> Vertex { } pub fn import_mesh(mesh_path: &str) -> (Vec, Vec) { - //let obj_file = "/home/mrh/source/3d-min-viable-eng/resources/Tree_01.obj"; - //let mtl_file = "/home/mrh/source/3d-min-viable-eng/resources/Tree_01.mtl"; - let (models, materials) = tobj::load_obj(mesh_path, false).expect("Failed to load file"); - //let q = tobj::load_mtl(mtl_file).unwrap(); + let (models, materials) = tobj::load_obj(mesh_path, false).expect("Failed to load file"); println!("# of models: {}", models.len()); println!("# of materials: {}", materials.len()); - //let model = models.get(2).unwrap(); - //let mesh = &model.mesh; - let mut index_data : Vec = Vec::new(); let mut vertex_data = Vec::new(); @@ -44,13 +38,10 @@ pub fn import_mesh(mesh_path: &str) -> (Vec, Vec) { for i in face_indices { index_data.push(*i); } - - //println!(" face[{}] = {:?}", f, face_indices); next_face = end; } // Normals and texture coordinates are also loaded, but not printed in this example - //println!("model[{}].vertices: {}", i, mesh.positions.len() / 3); assert!(mesh.positions.len() % 3 == 0); for v in 0..mesh.positions.len() / 3 { @@ -68,7 +59,6 @@ pub fn import_mesh(mesh_path: &str) -> (Vec, Vec) { )); } } - //println!("{:?}\n\n\n\n\n {:?}", vertex_data, index_data); (vertex_data.to_vec(), index_data.to_vec()) } diff --git a/src/render.rs b/src/render.rs index 24e36dc..9754dc9 100644 --- a/src/render.rs +++ b/src/render.rs @@ -13,8 +13,9 @@ use legion::*; use rapier3d::parry::motion::RigidMotionComposition; use wgpu::util::DeviceExt; use wgpu::{ - BackendBit, BindGroup, BindGroupLayout, Buffer, Device, Instance, Queue, Surface, SwapChain, - SwapChainDescriptor, SwapChainFrame, TextureView, + BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, + Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, + VertexState, }; use winit::dpi::PhysicalSize; use winit::platform::unix::x11::ffi::Time; @@ -160,6 +161,7 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { encoder.insert_debug_marker("render entities"); let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("render pass"), color_attachments: &[], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &light.target_view, @@ -175,7 +177,8 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { for mesh in &mesh_stack { pass.set_bind_group(1, &mesh.bind_group, &[]); - pass.set_index_buffer(mesh.index_buffer.slice(..)); + // TODO, pipe through this index format through the mesh + pass.set_index_buffer(mesh.index_buffer.slice(..), mesh.index_format); pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..)); pass.draw_indexed(0..mesh.index_count as u32, 0, 0..1); } @@ -185,6 +188,7 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { encoder.push_debug_group("forward rendering pass"); { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("forward render pass"), color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.output.view, resolve_target: None, @@ -214,7 +218,8 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { for (pos, mesh, color) in query.iter_mut(world) { pass.set_bind_group(1, &mesh.bind_group, &[]); - pass.set_index_buffer(mesh.index_buffer.slice(..)); + // TODO: Pipe this in through the mesh + pass.set_index_buffer(mesh.index_buffer.slice(..), mesh.index_format); pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..)); pass.draw_indexed(0..mesh.index_count as u32, 0, 0..1); } @@ -355,25 +360,33 @@ impl Renderer { let index_count = indices.len(); let (vertex_buf, index_buf) = Renderer::create_buffer(&self.device, indices, vertices); + let uniform_size = mem::size_of::() as wgpu::BufferAddress; + let uniform_buf = Arc::new(self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("Mesh Uniform Buf"), - size: mem::size_of::() as wgpu::BufferAddress, + size: uniform_size, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, mapped_at_creation: false, })); let bind_group = Arc::new(self.device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Mesh Bind Group"), layout: &self.entity_bind_group_layout, entries: &[wgpu::BindGroupEntry { binding: 0, - resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)), + resource: wgpu::BindingResource::Buffer { + buffer: &uniform_buf, + offset: 0, + size: wgpu::BufferSize::new(uniform_size), + }, }], - label: Some("Mesh Bind Group"), })); Mesh { index_buffer: index_buf, index_count: index_count, + // TODO: This is hardcoded by tobj, maybe think about doing something a little more clever? + index_format: wgpu::IndexFormat::Uint32, vertex_buffer: vertex_buf, uniform_buffer: uniform_buf, bind_group: bind_group, @@ -407,9 +420,9 @@ impl Renderer { // And then get the device we want let device = adapter.request_device( &wgpu::DeviceDescriptor { + label: Some("device descriptor"), features: (optional_features & adapter_features) | required_features, limits: needed_limits, - shader_validation: true, }, trace_dir.ok().as_ref().map(std::path::Path::new), ); @@ -421,7 +434,7 @@ impl Renderer { let mut sc_desc = (wgpu::SwapChainDescriptor { // Allows a texture to be a output attachment of a renderpass. - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, format: if cfg!(target_arch = "wasm32") { wgpu::TextureFormat::Bgra8Unorm } else { @@ -442,8 +455,8 @@ impl Renderer { // I wanted to get tricky with the 0,1 types let vertex_size = mem::size_of::(); let vertex_attr = wgpu::vertex_attr_array![0 => Float4, 1 => Float4]; - let vb_desc = wgpu::VertexBufferDescriptor { - stride: vertex_size as wgpu::BufferAddress, + let vb_desc = wgpu::VertexBufferLayout { + array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::InputStepMode::Vertex, attributes: &vertex_attr, }; @@ -456,11 +469,12 @@ impl Renderer { binding: 0, visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, count: None, - ty: wgpu::BindingType::UniformBuffer { - dynamic: false, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, min_binding_size: wgpu::BufferSize::new( mem::size_of::() as _ ), + has_dynamic_offset: false, }, }], }); @@ -480,9 +494,10 @@ impl Renderer { entries: &[wgpu::BindGroupLayoutEntry { binding: 0, // global visibility: wgpu::ShaderStage::VERTEX, - ty: wgpu::BindingType::UniformBuffer { - dynamic: false, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, min_binding_size: wgpu::BufferSize::new(uniform_size), + has_dynamic_offset: false, }, count: None, }], @@ -508,51 +523,56 @@ impl Renderer { layout: &bind_group_layout, entries: &[wgpu::BindGroupEntry { binding: 0, - resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)), + resource: wgpu::BindingResource::Buffer { + buffer: &uniform_buf, + offset: 0, + size: wgpu::BufferSize::new(uniform_size), + }, }], label: Some("Shadow uniform bind group"), }); // Create the render pipeline let vs_module = - device.create_shader_module(wgpu::include_spirv!("../resources/bake.vert.spv")); + device.create_shader_module(&wgpu::include_spirv!("../resources/bake.vert.spv")); let fs_module = - device.create_shader_module(wgpu::include_spirv!("../resources/bake.frag.spv")); + device.create_shader_module(&wgpu::include_spirv!("../resources/bake.frag.spv")); let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("shadow"), layout: Some(&pipeline_layout), - vertex_stage: wgpu::ProgrammableStageDescriptor { + + vertex: VertexState { module: &vs_module, entry_point: "main", + buffers: &[vb_desc.clone()], }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + fragment: Some(FragmentState { module: &fs_module, entry_point: "main", + targets: &[], }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, front_face: wgpu::FrontFace::Ccw, cull_mode: wgpu::CullMode::Back, - depth_bias: 2, // corresponds to bilinear filtering - depth_bias_slope_scale: 2.0, - depth_bias_clamp: 0.0, - clamp_depth: device.features().contains(wgpu::Features::DEPTH_CLAMPING), - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + ..Default::default() + }, + depth_stencil: Some(wgpu::DepthStencilState { format: Self::SHADOW_FORMAT, depth_write_enabled: true, depth_compare: wgpu::CompareFunction::LessEqual, - stencil: wgpu::StencilStateDescriptor::default(), + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState { + constant: 2, // corresponds to bilinear filtering + slope_scale: 2.0, + clamp: 0.0, + }, + clamp_depth: device.features().contains( + wgpu::Features::DEPTH_CLAMPING + ), }), - vertex_state: wgpu::VertexStateDescriptor { - index_format: wgpu::IndexFormat::Uint32, - vertex_buffers: &[vb_desc.clone()], - }, - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, + multisample: wgpu::MultisampleState::default(), }); Pass { @@ -581,7 +601,7 @@ impl Renderer { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: Self::SHADOW_FORMAT, - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT | wgpu::TextureUsage::SAMPLED, label: Some("Shadow texture"), }); @@ -615,39 +635,44 @@ impl Renderer { wgpu::BindGroupLayoutEntry { binding: 0, // global visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { - dynamic: false, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, min_binding_size: wgpu::BufferSize::new(mem::size_of::< ForwardUniforms, >( ) as _), + has_dynamic_offset: false, }, count: None, }, wgpu::BindGroupLayoutEntry { binding: 1, // lights visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { - dynamic: false, + ty: wgpu::BindingType::Buffer { + ty: BufferBindingType::Uniform, min_binding_size: wgpu::BufferSize::new(light_uniform_size), + has_dynamic_offset: false, }, count: None, }, wgpu::BindGroupLayoutEntry { binding: 2, visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::SampledTexture { + ty: wgpu::BindingType::Texture { multisampled: false, - component_type: wgpu::TextureComponentType::Float, - dimension: wgpu::TextureViewDimension::D2Array, + sample_type: wgpu::TextureSampleType::Depth, + view_dimension: wgpu::TextureViewDimension::D2Array, }, count: None, }, wgpu::BindGroupLayoutEntry { binding: 3, visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::Sampler { comparison: true }, + ty: wgpu::BindingType::Sampler { + filtering: false, + comparison: true, + }, count: None, }, ], @@ -687,17 +712,29 @@ impl Renderer { let shadow_view = shadow_texture.create_view(&wgpu::TextureViewDescriptor::default()); + let forward_uniform_size = + wgpu::BufferSize::new(mem::size_of::() as wgpu::BufferAddress); + let light_uniform_size = wgpu::BufferSize::new(light_uniform_size); + // Create bind group let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &bind_group_layout, entries: &[ wgpu::BindGroupEntry { binding: 0, - resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)), + resource: wgpu::BindingResource::Buffer { + buffer: &uniform_buf, + offset: 0, + size: forward_uniform_size, + }, }, wgpu::BindGroupEntry { binding: 1, - resource: wgpu::BindingResource::Buffer(light_uniform_buf.slice(..)), + resource: wgpu::BindingResource::Buffer { + buffer: &light_uniform_buf, + offset: 0, + size: light_uniform_size, + }, }, wgpu::BindGroupEntry { binding: 2, @@ -713,41 +750,37 @@ impl Renderer { // Create the render pipeline let vs_module = - device.create_shader_module(wgpu::include_spirv!("../resources/forward.vert.spv")); + device.create_shader_module(&wgpu::include_spirv!("../resources/forward.vert.spv")); let fs_module = - device.create_shader_module(wgpu::include_spirv!("../resources/forward.frag.spv")); + device.create_shader_module(&wgpu::include_spirv!("../resources/forward.frag.spv")); let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("main"), layout: Some(&pipeline_layout), - vertex_stage: wgpu::ProgrammableStageDescriptor { + vertex: VertexState { module: &vs_module, entry_point: "main", + buffers: &[vb_desc], }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + fragment: Some(FragmentState { module: &fs_module, entry_point: "main", + targets: &[sc_desc.format.into()], }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { + primitive: wgpu::PrimitiveState { front_face: wgpu::FrontFace::Ccw, cull_mode: wgpu::CullMode::Back, ..Default::default() - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[sc_desc.format.into()], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + }, + depth_stencil: Some(wgpu::DepthStencilState { format: Self::DEPTH_FORMAT, depth_write_enabled: true, depth_compare: wgpu::CompareFunction::Less, - stencil: wgpu::StencilStateDescriptor::default(), + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + clamp_depth: false, }), - vertex_state: wgpu::VertexStateDescriptor { - index_format: wgpu::IndexFormat::Uint32, - vertex_buffers: &[vb_desc], - }, - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, + multisample: Default::default(), }); Pass { @@ -767,7 +800,7 @@ impl Renderer { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: Self::DEPTH_FORMAT, - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, label: Some("Depth Texture"), }); @@ -825,7 +858,7 @@ impl Renderer { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: Self::DEPTH_FORMAT, - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, label: Some("Depth Texture"), }); self.forward_depth = depth_texture.create_view(&wgpu::TextureViewDescriptor::default());