From 9f7e313dc4cea5970aeef9d6d4097cb87a5a2091 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sat, 11 Jan 2020 02:11:27 -0800 Subject: [PATCH] rustfmt crate --- src/app/app_builder.rs | 44 ++-- src/app/app_stage.rs | 2 +- src/app/mod.rs | 41 +-- src/asset/gltf.rs | 7 +- src/asset/mod.rs | 50 ++-- src/core/mod.rs | 2 +- src/core/time.rs | 7 +- src/lib.rs | 14 +- src/render/camera.rs | 42 ++- src/render/instancing.rs | 2 +- src/render/light.rs | 10 +- src/render/material.rs | 6 +- src/render/mesh.rs | 32 ++- src/render/mod.rs | 22 +- src/render/passes/forward/forward_pass.rs | 30 ++- src/render/passes/forward/forward_pipeline.rs | 196 +++++++------- src/render/passes/forward/mod.rs | 4 +- src/render/passes/forward_instanced/mod.rs | 241 +++++++++++------- src/render/passes/forward_shadow/mod.rs | 225 ++++++++-------- src/render/passes/mod.rs | 9 +- src/render/passes/shadow/mod.rs | 2 +- src/render/passes/shadow/shadow_pass.rs | 38 ++- src/render/passes/shadow/shadow_pipeline.rs | 165 ++++++------ src/render/passes/ui/mod.rs | 191 ++++++++------ src/render/rect.rs | 2 +- src/render/render_graph/mod.rs | 56 ++-- src/render/render_graph/pass.rs | 10 +- src/render/render_graph/pipeline.rs | 12 +- .../render_graph/render_resource_manager.rs | 18 +- .../global_2d_resource_manager.rs | 41 ++- .../global_resource_manager.rs | 48 +++- .../light_resource_manager.rs | 33 ++- .../material_resource_manager.rs | 83 +++--- src/render/render_resources/mod.rs | 10 +- src/render/shader.rs | 2 +- src/vertex.rs | 40 ++- 36 files changed, 1043 insertions(+), 694 deletions(-) diff --git a/src/app/app_builder.rs b/src/app/app_builder.rs index 57b80e5443..2605e84d9f 100644 --- a/src/app/app_builder.rs +++ b/src/app/app_builder.rs @@ -1,4 +1,9 @@ -use crate::{App, asset::AssetStorage, legion::prelude::{World, SystemScheduler}, render::{*, passes::*}, AppStage, Time}; +use crate::{ + asset::AssetStorage, + legion::prelude::{SystemScheduler, World}, + render::{passes::*, *}, + App, AppStage, Time, +}; pub struct AppBuilder { pub app: App, @@ -26,7 +31,7 @@ impl AppBuilder { self } - pub fn with_scheduler(mut self, scheduler: SystemScheduler::) -> Self { + pub fn with_scheduler(mut self, scheduler: SystemScheduler) -> Self { self.app.scheduler = scheduler; self } @@ -37,29 +42,36 @@ impl AppBuilder { } pub fn add_default_passes(mut self) -> Self { - self.app.render_graph.add_render_resource_manager(Box::new(render_resources::MaterialResourceManager)); - self.app.render_graph.add_render_resource_manager(Box::new(render_resources::LightResourceManager::new(10))); - self.app.render_graph.add_render_resource_manager(Box::new(render_resources::GlobalResourceManager)); - self.app.render_graph.add_render_resource_manager(Box::new(render_resources::Global2dResourceManager)); + let render_graph = &mut self.app.render_graph; + render_graph + .add_render_resource_manager(Box::new(render_resources::MaterialResourceManager)); + render_graph + .add_render_resource_manager(Box::new(render_resources::LightResourceManager::new(10))); + render_graph.add_render_resource_manager(Box::new(render_resources::GlobalResourceManager)); + render_graph + .add_render_resource_manager(Box::new(render_resources::Global2dResourceManager)); let depth_format = wgpu::TextureFormat::Depth32Float; - self.app.render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format))); - self.app.render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new())); - self.app.render_graph.set_pipeline("forward", "forward_instanced", Box::new(ForwardInstancedPipeline::new(depth_format))); - self.app.render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new())); + render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format))); + render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new())); + render_graph.set_pipeline( + "forward", + "forward_instanced", + Box::new(ForwardInstancedPipeline::new(depth_format)), + ); + render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new())); self } pub fn add_default_resources(mut self) -> Self { - self.app.world.resources.insert(Time::new()); - self.app.world.resources.insert(AssetStorage::::new()); + let resources = &mut self.app.world.resources; + resources.insert(Time::new()); + resources.insert(AssetStorage::::new()); self } pub fn add_defaults(self) -> Self { - self - .add_default_resources() - .add_default_passes() + self.add_default_resources().add_default_passes() } -} \ No newline at end of file +} diff --git a/src/app/app_stage.rs b/src/app/app_stage.rs index 5ef10ce467..9eaa78ffa9 100644 --- a/src/app/app_stage.rs +++ b/src/app/app_stage.rs @@ -15,4 +15,4 @@ impl std::fmt::Display for AppStage { AppStage::Render => write!(f, "draw"), } } -} \ No newline at end of file +} diff --git a/src/app/mod.rs b/src/app/mod.rs index 7eeae1ed20..6d9de5329d 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,8 +1,8 @@ -mod app_stage; mod app_builder; +mod app_stage; -pub use app_stage::AppStage; pub use app_builder::AppBuilder; +pub use app_stage::AppStage; use winit::{ event, @@ -15,8 +15,7 @@ use legion::prelude::*; use crate::{render::*, Time}; -pub struct App -{ +pub struct App { pub world: World, pub window: Option, pub render_graph: RenderGraph, @@ -33,7 +32,7 @@ impl App { swap_chain: None, window: None, } - } + } fn update(&mut self) { { @@ -48,25 +47,22 @@ impl App { } } - fn resize(&mut self, width: u32, height: u32) - { + fn resize(&mut self, width: u32, height: u32) { self.swap_chain = Some(self.render_graph.resize(width, height, &mut self.world)); } - fn handle_event(&mut self, _: WindowEvent) - { - } + fn handle_event(&mut self, _: WindowEvent) {} - fn render(&mut self) - { - self.render_graph.render(&mut self.world, self.swap_chain.as_mut().unwrap()); + fn render(&mut self) { + self.render_graph + .render(&mut self.world, self.swap_chain.as_mut().unwrap()); } pub fn run(mut self) { env_logger::init(); let event_loop = EventLoop::new(); log::info!("Initializing the window..."); - + let adapter = wgpu::Adapter::request( &wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::Default, @@ -74,7 +70,7 @@ impl App { wgpu::BackendBit::PRIMARY, ) .unwrap(); - + let (device, queue) = adapter.request_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { anisotropic_filtering: false, @@ -91,7 +87,7 @@ impl App { let surface = wgpu::Surface::create(&window); (window, size, surface) }; - + let swap_chain_descriptor = wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, format: wgpu::TextureFormat::Bgra8UnormSrgb, @@ -102,11 +98,16 @@ impl App { let swap_chain = device.create_swap_chain(&surface, &swap_chain_descriptor); log::info!("Initializing the example..."); - self.render_graph.initialize(&mut self.world, device, swap_chain_descriptor, queue, surface); + self.render_graph.initialize( + &mut self.world, + device, + swap_chain_descriptor, + queue, + surface, + ); self.window = Some(window); self.swap_chain = Some(swap_chain); - log::info!("Entering render loop..."); event_loop.run(move |event, _, control_flow| { *control_flow = if cfg!(feature = "metal-auto-capture") { @@ -148,6 +149,6 @@ impl App { } _ => (), } - }); + }); } -} \ No newline at end of file +} diff --git a/src/asset/gltf.rs b/src/asset/gltf.rs index 6306f79c93..34cfa5c034 100644 --- a/src/asset/gltf.rs +++ b/src/asset/gltf.rs @@ -1,6 +1,6 @@ -use std::{fs, io}; use std::boxed::Box; use std::error::Error; +use std::{fs, io}; // use crate::render::Mesh; @@ -10,9 +10,8 @@ pub fn load_gltf(path: &str) -> Result<(), Box> { let reader = io::BufReader::new(file); let gltf = gltf::Gltf::from_reader(reader)?; for scene in gltf.scenes() { - for _mesh in scene.nodes() { - } + for _mesh in scene.nodes() {} } - + Ok(()) } diff --git a/src/asset/mod.rs b/src/asset/mod.rs index e9eb6e41ce..aaa39fac16 100644 --- a/src/asset/mod.rs +++ b/src/asset/mod.rs @@ -2,21 +2,25 @@ mod gltf; pub use self::gltf::load_gltf; -use std::{sync::{Arc, RwLock}, marker::PhantomData, ops::Drop, collections::HashMap}; +use std::{ + collections::HashMap, + marker::PhantomData, + ops::Drop, + sync::{Arc, RwLock}, +}; -pub struct Handle -{ +pub struct Handle { pub id: Arc>, marker: PhantomData, - free_indices: Arc>> + free_indices: Arc>>, } impl Clone for Handle { - fn clone(&self) -> Self { + fn clone(&self) -> Self { Handle { id: self.id.clone(), free_indices: self.free_indices.clone(), - marker: PhantomData + marker: PhantomData, } } } @@ -36,14 +40,20 @@ pub trait Asset { fn load(descriptor: D) -> Self; } -pub struct AssetStorage where T: Asset { +pub struct AssetStorage +where + T: Asset, +{ assets: Vec>, free_indices: Arc>>, names: HashMap>>, marker: PhantomData, } -impl AssetStorage where T: Asset { +impl AssetStorage +where + T: Asset, +{ pub fn new() -> AssetStorage { AssetStorage { assets: Vec::new(), @@ -55,13 +65,11 @@ impl AssetStorage where T: Asset { pub fn get_named(&self, name: &str) -> Option> { match self.names.get(name) { - Some(id) => { - Some(Handle { - id: id.clone(), - marker: PhantomData, - free_indices: self.free_indices.clone() - }) - }, + Some(id) => Some(Handle { + id: id.clone(), + marker: PhantomData, + free_indices: self.free_indices.clone(), + }), None => None, } } @@ -75,9 +83,9 @@ impl AssetStorage where T: Asset { Handle { id: handle, marker: PhantomData, - free_indices: self.free_indices.clone() + free_indices: self.free_indices.clone(), } - }, + } None => { self.assets.push(Some(asset)); let id = self.assets.len() - 1; @@ -86,7 +94,7 @@ impl AssetStorage where T: Asset { Handle { id: handle, marker: PhantomData, - free_indices: self.free_indices.clone() + free_indices: self.free_indices.clone(), } } } @@ -95,14 +103,12 @@ impl AssetStorage where T: Asset { pub fn get(&mut self, id: usize) -> Option<&mut T> { if id >= self.assets.len() { None - } - else { + } else { if let Some(ref mut asset) = self.assets[id] { Some(asset) } else { None } } - } -} \ No newline at end of file +} diff --git a/src/core/mod.rs b/src/core/mod.rs index 3e40bc8327..1ae628fdb6 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,2 +1,2 @@ pub mod time; -pub use time::Time; \ No newline at end of file +pub use time::Time; diff --git a/src/core/time.rs b/src/core/time.rs index fe5cd86177..2070f58a55 100644 --- a/src/core/time.rs +++ b/src/core/time.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; pub struct Time { pub delta: Duration, pub instant: Instant, - pub delta_seconds: f32 + pub delta_seconds: f32, } impl Time { @@ -21,6 +21,7 @@ impl Time { pub fn stop(&mut self) { self.delta = Instant::now() - self.instant; - self.delta_seconds = self.delta.as_secs() as f32 + (self.delta.subsec_nanos() as f32 / 1.0e9); + self.delta_seconds = + self.delta.as_secs() as f32 + (self.delta.subsec_nanos() as f32 / 1.0e9); } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index c7ea840a42..aadffe9aa9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,17 @@ -pub mod render; -pub mod asset; mod app; -mod vertex; +pub mod asset; mod core; +pub mod render; +mod vertex; -pub use app::{App, AppStage, AppBuilder}; pub use crate::core::*; +pub use app::{App, AppBuilder, AppStage}; -pub use wgpu; +pub use glam as math; pub use legion; -pub use legion_transform; pub use legion::prelude::*; pub use legion::schedule::Schedulable; +pub use legion_transform; pub use legion_transform::prelude::*; pub use legion_transform::transform_system_bundle; -pub use glam as math; \ No newline at end of file +pub use wgpu; diff --git a/src/render/camera.rs b/src/render/camera.rs index 4e39a82621..dc096632a7 100644 --- a/src/render/camera.rs +++ b/src/render/camera.rs @@ -8,7 +8,7 @@ pub enum CameraType { fov: f32, aspect_ratio: f32, near: f32, - far: f32 + far: f32, }, Orthographic { left: f32, @@ -35,14 +35,28 @@ impl Camera { pub fn update(&mut self, width: u32, height: u32) { match &mut self.camera_type { - CameraType::Projection { aspect_ratio, fov, near, far } => { + CameraType::Projection { + aspect_ratio, + fov, + near, + far, + } => { *aspect_ratio = width as f32 / height as f32; - self.view_matrix = get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far) - }, - CameraType::Orthographic { left, right, bottom, top, near, far} => { + self.view_matrix = + get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far) + } + CameraType::Orthographic { + left, + right, + bottom, + top, + near, + far, + } => { *right = width as f32; *top = height as f32; - self.view_matrix = get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far) + self.view_matrix = + get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far) } } } @@ -54,7 +68,14 @@ pub fn get_perspective_projection_matrix(fov: f32, aspect_ratio: f32, near: f32, opengl_to_wgpu_matrix() * projection } -pub fn get_orthographic_projection_matrix(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 { +pub fn get_orthographic_projection_matrix( + left: f32, + right: f32, + bottom: f32, + top: f32, + near: f32, + far: f32, +) -> Mat4 { let projection = Mat4::orthographic_rh_gl(left, right, bottom, top, near, far); opengl_to_wgpu_matrix() * projection @@ -62,9 +83,6 @@ pub fn get_orthographic_projection_matrix(left: f32, right: f32, bottom: f32, to pub fn opengl_to_wgpu_matrix() -> Mat4 { Mat4::from_cols_array(&[ - 1.0, 0.0, 0.0, 0.0, - 0.0, -1.0, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.0, 0.0, 0.5, 1.0, + 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0, ]) -} \ No newline at end of file +} diff --git a/src/render/instancing.rs b/src/render/instancing.rs index 8afa906182..f9222bb496 100644 --- a/src/render/instancing.rs +++ b/src/render/instancing.rs @@ -2,4 +2,4 @@ pub struct InstanceBufferInfo { pub buffer: wgpu::Buffer, pub instance_count: usize, pub mesh_id: usize, -} \ No newline at end of file +} diff --git a/src/render/light.rs b/src/render/light.rs index 5b4b18d684..ddd446cf3a 100644 --- a/src/render/light.rs +++ b/src/render/light.rs @@ -2,7 +2,6 @@ use crate::{math, render::camera, Translation}; use std::ops::Range; use zerocopy::{AsBytes, FromBytes}; - pub struct Light { pub color: wgpu::Color, pub fov: f32, @@ -20,7 +19,12 @@ pub struct LightRaw { impl LightRaw { pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw { - let proj = camera::get_perspective_projection_matrix(light.fov, 1.0, light.depth.start, light.depth.end) * *transform; + let proj = camera::get_perspective_projection_matrix( + light.fov, + 1.0, + light.depth.start, + light.depth.end, + ) * *transform; let (x, y, z) = translation.0.into(); LightRaw { proj: proj.to_cols_array_2d(), @@ -33,4 +37,4 @@ impl LightRaw { ], } } -} \ No newline at end of file +} diff --git a/src/render/material.rs b/src/render/material.rs index 4c976fab13..5d21a7fcb1 100644 --- a/src/render/material.rs +++ b/src/render/material.rs @@ -1,5 +1,5 @@ -use zerocopy::{AsBytes, FromBytes}; use crate::math; +use zerocopy::{AsBytes, FromBytes}; pub struct Material { pub color: math::Vec4, @@ -14,7 +14,7 @@ impl Material { Material { color, bind_group: None, - uniform_buf: None, + uniform_buf: None, } } } @@ -37,4 +37,4 @@ pub struct MaterialUniforms { pub struct SimpleMaterialUniforms { pub position: [f32; 3], pub color: [f32; 4], -} \ No newline at end of file +} diff --git a/src/render/mesh.rs b/src/render/mesh.rs index cbdc207898..adc30bac56 100644 --- a/src/render/mesh.rs +++ b/src/render/mesh.rs @@ -1,4 +1,4 @@ -use crate::{vertex::Vertex, asset::Asset, math::*}; +use crate::{asset::Asset, math::*, vertex::Vertex}; use wgpu::{Buffer, Device}; use zerocopy::AsBytes; @@ -8,14 +8,14 @@ pub struct Mesh2d; pub enum MeshType { Cube, Plane { - size: f32 + size: f32, }, Quad { north_west: Vec2, north_east: Vec2, south_west: Vec2, south_east: Vec2, - } + }, } pub struct Mesh { @@ -28,11 +28,15 @@ pub struct Mesh { impl Mesh { pub fn setup_buffers(&mut self, device: &Device) { if let None = self.vertex_buffer { - self.vertex_buffer = Some(device.create_buffer_with_data(self.vertices.as_bytes(), wgpu::BufferUsage::VERTEX)); + self.vertex_buffer = Some( + device.create_buffer_with_data(self.vertices.as_bytes(), wgpu::BufferUsage::VERTEX), + ); } if let None = self.index_buffer { - self.index_buffer = Some(device.create_buffer_with_data(self.indices.as_bytes(), wgpu::BufferUsage::INDEX)); + self.index_buffer = Some( + device.create_buffer_with_data(self.indices.as_bytes(), wgpu::BufferUsage::INDEX), + ); } } } @@ -42,7 +46,12 @@ impl Asset for Mesh { let (vertices, indices) = match descriptor { MeshType::Cube => create_cube(), MeshType::Plane { size } => create_plane(size), - MeshType::Quad { north_west, north_east, south_west, south_east } => create_quad(north_west, north_east, south_west, south_east), + MeshType::Quad { + north_west, + north_east, + south_west, + south_east, + } => create_quad(north_west, north_east, south_west, south_east), }; Mesh { @@ -54,7 +63,12 @@ impl Asset for Mesh { } } -pub fn create_quad(north_west: Vec2, north_east: Vec2, south_west: Vec2, south_east: Vec2) -> (Vec, Vec) { +pub fn create_quad( + north_west: Vec2, + north_east: Vec2, + south_west: Vec2, + south_east: Vec2, +) -> (Vec, Vec) { let vertex_data = [ Vertex::from(([south_west.x(), south_west.y(), 0.0], [0.0, 0.0, 1.0])), Vertex::from(([north_west.x(), north_west.y(), 0.0], [0.0, 0.0, 1.0])), @@ -62,7 +76,7 @@ pub fn create_quad(north_west: Vec2, north_east: Vec2, south_west: Vec2, south_e Vertex::from(([south_east.x(), south_east.y(), 0.0], [0.0, 0.0, 1.0])), ]; - let index_data: &[u16] = &[ 0, 1, 2, 0, 2, 3 ]; + let index_data: &[u16] = &[0, 1, 2, 0, 2, 3]; return (vertex_data.to_vec(), index_data.to_vec()); } @@ -124,4 +138,4 @@ pub fn create_plane(size: f32) -> (Vec, Vec) { let index_data: &[u16] = &[0, 1, 2, 2, 1, 3]; (vertex_data.to_vec(), index_data.to_vec()) -} \ No newline at end of file +} diff --git a/src/render/mod.rs b/src/render/mod.rs index b52745be9b..cb9ffe95ec 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1,25 +1,25 @@ pub mod camera; -pub mod shader; -pub mod mesh; -pub mod render_resources; -pub mod passes; pub mod instancing; +pub mod mesh; +pub mod passes; +pub mod render_resources; +pub mod shader; mod light; -mod render_graph; mod material; mod rect; +mod render_graph; +pub use camera::*; pub use light::*; -pub use shader::*; -pub use render_graph::*; pub use material::*; pub use mesh::*; -pub use camera::*; pub use rect::*; +pub use render_graph::*; +pub use shader::*; -use std::mem; use crate::vertex::Vertex; +use std::mem; pub struct UniformBuffer { pub buffer: wgpu::Buffer, @@ -30,7 +30,7 @@ impl UniformBuffer { pub fn get_binding_resource<'a>(&'a self) -> wgpu::BindingResource<'a> { wgpu::BindingResource::Buffer { buffer: &self.buffer, - range: 0 .. self.size, + range: 0..self.size, } } } @@ -53,4 +53,4 @@ pub fn get_vertex_buffer_descriptor<'a>() -> wgpu::VertexBufferDescriptor<'a> { }, ], } -} \ No newline at end of file +} diff --git a/src/render/passes/forward/forward_pass.rs b/src/render/passes/forward/forward_pass.rs index a0da1fbb92..efe2924b6c 100644 --- a/src/render/passes/forward/forward_pass.rs +++ b/src/render/passes/forward/forward_pass.rs @@ -1,7 +1,7 @@ use crate::render::*; use legion::prelude::*; -use zerocopy::{AsBytes, FromBytes}; use wgpu::{Device, SwapChainDescriptor}; +use zerocopy::{AsBytes, FromBytes}; #[repr(C)] #[derive(Clone, Copy, AsBytes, FromBytes)] @@ -16,11 +16,13 @@ pub struct ForwardPass { impl ForwardPass { pub fn new(depth_format: wgpu::TextureFormat) -> Self { - ForwardPass { - depth_format, - } + ForwardPass { depth_format } } - fn get_depth_texture(&self, device: &Device, swap_chain_descriptor: &SwapChainDescriptor) -> wgpu::TextureView { + fn get_depth_texture( + &self, + device: &Device, + swap_chain_descriptor: &SwapChainDescriptor, + ) -> wgpu::TextureView { let texture = device.create_texture(&wgpu::TextureDescriptor { size: wgpu::Extent3d { width: swap_chain_descriptor.width, @@ -42,11 +44,18 @@ impl ForwardPass { const DEPTH_TEXTURE_NAME: &str = "forward_depth"; impl Pass for ForwardPass { - fn initialize(&self, render_graph: &mut RenderGraphData) { - let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); + fn initialize(&self, render_graph: &mut RenderGraphData) { + let depth_texture = + self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture); } - fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, _: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option> { + fn begin<'a>( + &mut self, + render_graph: &mut RenderGraphData, + _: &mut World, + encoder: &'a mut wgpu::CommandEncoder, + frame: &'a wgpu::SwapChainOutput, + ) -> Option> { let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME); Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { @@ -74,11 +83,12 @@ impl Pass for ForwardPass { } fn resize(&self, render_graph: &mut RenderGraphData) { - let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); + let depth_texture = + self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture); } fn should_repeat(&self) -> bool { false } -} \ No newline at end of file +} diff --git a/src/render/passes/forward/forward_pipeline.rs b/src/render/passes/forward/forward_pipeline.rs index 0706c431fb..3d903d16b1 100644 --- a/src/render/passes/forward/forward_pipeline.rs +++ b/src/render/passes/forward/forward_pipeline.rs @@ -1,4 +1,4 @@ -use crate::{render::*, asset::*, render::mesh::*}; +use crate::{asset::*, render::mesh::*, render::*}; use legion::prelude::*; use wgpu::SwapChainOutput; @@ -13,120 +13,135 @@ impl ForwardPipeline { ForwardPipeline { pipeline: None, bind_group: None, - depth_format: wgpu::TextureFormat::Depth32Float + depth_format: wgpu::TextureFormat::Depth32Float, } } } impl Pipeline for ForwardPipeline { fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) { - let vs_bytes = shader::load_glsl( - include_str!("forward.vert"), - shader::ShaderStage::Vertex, - ); - let fs_bytes = shader::load_glsl( - include_str!("forward.frag"), - shader::ShaderStage::Fragment, - ); + let vs_bytes = shader::load_glsl(include_str!("forward.vert"), shader::ShaderStage::Vertex); + let fs_bytes = + shader::load_glsl(include_str!("forward.frag"), shader::ShaderStage::Fragment); let bind_group_layout = - render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[ - wgpu::BindGroupLayoutBinding { - binding: 0, // global - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }, - wgpu::BindGroupLayoutBinding { - binding: 1, // lights - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - } - ], - }); + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[ + wgpu::BindGroupLayoutBinding { + binding: 0, // global + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + wgpu::BindGroupLayoutBinding { + binding: 1, // lights + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + ], + }); self.bind_group = Some({ - - let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap(); - let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap(); + let forward_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME) + .unwrap(); + let light_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME) + .unwrap(); // Create bind group - render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, - bindings: &[ - wgpu::Binding { - binding: 0, - resource: forward_uniform_buffer.get_binding_resource(), - }, - wgpu::Binding { - binding: 1, - resource: light_uniform_buffer.get_binding_resource(), - } - ], - }) + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[ + wgpu::Binding { + binding: 0, + resource: forward_uniform_buffer.get_binding_resource(), + }, + wgpu::Binding { + binding: 1, + resource: light_uniform_buffer.get_binding_resource(), + }, + ], + }) }); - let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap(); + let material_bind_group_layout = render_graph + .get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME) + .unwrap(); - let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - bind_group_layouts: &[&bind_group_layout, material_bind_group_layout], - }); + let pipeline_layout = + render_graph + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout, material_bind_group_layout], + }); let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); let vs_module = render_graph.device.create_shader_module(&vs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes); - self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - layout: &pipeline_layout, - vertex_stage: wgpu::ProgrammableStageDescriptor { - module: &vs_module, - entry_point: "main", - }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { - module: &fs_module, - entry_point: "main", - }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, - depth_bias: 0, - depth_bias_slope_scale: 0.0, - depth_bias_clamp: 0.0, - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[ - wgpu::ColorStateDescriptor { + self.pipeline = Some(render_graph.device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + front_face: wgpu::FrontFace::Ccw, + cull_mode: wgpu::CullMode::Back, + depth_bias: 0, + depth_bias_slope_scale: 0.0, + depth_bias_clamp: 0.0, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { format: render_graph.swap_chain_descriptor.format, color_blend: wgpu::BlendDescriptor::REPLACE, alpha_blend: wgpu::BlendDescriptor::REPLACE, write_mask: wgpu::ColorWrite::ALL, - }, - ], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { - format: self.depth_format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_read_mask: 0, - stencil_write_mask: 0, - }), - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[vertex_buffer_descriptor], - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, - })); + }], + depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + format: self.depth_format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_read_mask: 0, + stencil_write_mask: 0, + }), + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[vertex_buffer_descriptor], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + }, + )); } - fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) { + fn render( + &mut self, + render_graph: &RenderGraphData, + pass: &mut wgpu::RenderPass, + _: &SwapChainOutput, + world: &mut World, + ) { pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]); - let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut mesh_storage = world + .resources + .get_mut::>() + .unwrap(); let mut last_mesh_id = None; let mut mesh_query = - <(Read, Read>)>::query() - .filter(!component::()); + <(Read, Read>)>::query().filter(!component::()); for (material, mesh) in mesh_query.iter_immutable(world) { let current_mesh_id = *mesh.id.read().unwrap(); @@ -145,17 +160,16 @@ impl Pipeline for ForwardPipeline { if let Some(ref mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) { pass.set_bind_group(1, material.bind_group.as_ref().unwrap(), &[]); - pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. 1); + pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1); }; - last_mesh_id = Some(current_mesh_id); + last_mesh_id = Some(current_mesh_id); } } - - fn resize(&mut self, _: &RenderGraphData) { - } + + fn resize(&mut self, _: &RenderGraphData) {} fn get_pipeline(&self) -> &wgpu::RenderPipeline { self.pipeline.as_ref().unwrap() } -} \ No newline at end of file +} diff --git a/src/render/passes/forward/mod.rs b/src/render/passes/forward/mod.rs index a5923f8d62..441bdc9e5a 100644 --- a/src/render/passes/forward/mod.rs +++ b/src/render/passes/forward/mod.rs @@ -1,5 +1,5 @@ -mod forward_pipeline; mod forward_pass; +mod forward_pipeline; +pub use forward_pass::*; pub use forward_pipeline::*; -pub use forward_pass::*; \ No newline at end of file diff --git a/src/render/passes/forward_instanced/mod.rs b/src/render/passes/forward_instanced/mod.rs index 6c3f578e3b..c83a563261 100644 --- a/src/render/passes/forward_instanced/mod.rs +++ b/src/render/passes/forward_instanced/mod.rs @@ -1,8 +1,13 @@ -use crate::{render::{*, instancing::InstanceBufferInfo}, asset::*, render::mesh::*, LocalToWorld}; +use crate::{ + asset::*, + render::mesh::*, + render::{instancing::InstanceBufferInfo, *}, + LocalToWorld, +}; use legion::prelude::*; use std::mem; -use zerocopy::AsBytes; use wgpu::{Device, SwapChainOutput}; +use zerocopy::AsBytes; pub struct ForwardInstancedPipeline { pub pipeline: Option, @@ -22,7 +27,12 @@ impl ForwardInstancedPipeline { } fn create_instance_buffer_infos(device: &Device, world: &World) -> Vec { - let mut entities = <(Read, Read, Read>, Read)>::query(); + let mut entities = <( + Read, + Read, + Read>, + Read, + )>::query(); let entities_count = entities.iter_immutable(world).count(); if entities_count == 0 { return Vec::new(); @@ -31,16 +41,18 @@ impl ForwardInstancedPipeline { let size = mem::size_of::(); // TODO: use a staging buffer for more efficient gpu reads - let temp_buf_data = device - .create_buffer_mapped(entities_count * size, wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX); + let temp_buf_data = device.create_buffer_mapped( + entities_count * size, + wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX, + ); // TODO: generate these buffers for multiple meshes - + let mut last_mesh_id = None; - for ((material, transform, mesh, _), slot) in entities.iter_immutable(world) + for ((material, transform, mesh, _), slot) in entities + .iter_immutable(world) .zip(temp_buf_data.data.chunks_exact_mut(size)) { - last_mesh_id = Some(*mesh.id.read().unwrap()); let (_, _, translation) = transform.0.to_scale_rotation_translation(); slot.copy_from_slice( @@ -51,7 +63,7 @@ impl ForwardInstancedPipeline { .as_bytes(), ); } - + let mut instance_buffer_infos = Vec::new(); instance_buffer_infos.push(InstanceBufferInfo { mesh_id: last_mesh_id.unwrap(), @@ -63,15 +75,21 @@ impl ForwardInstancedPipeline { } #[allow(dead_code)] - fn create_instance_buffer_infos_direct(device: &Device, world: &World) -> Vec { - let mut entities = <(Read, Read, Read>, Read)>::query(); + fn create_instance_buffer_infos_direct( + device: &Device, + world: &World, + ) -> Vec { + let mut entities = <( + Read, + Read, + Read>, + Read, + )>::query(); let entities_count = entities.iter_immutable(world).count(); let mut last_mesh_id = None; let mut data = Vec::with_capacity(entities_count); - for (material, transform, mesh, _) in entities.iter_immutable(world) - { - + for (material, transform, mesh, _) in entities.iter_immutable(world) { last_mesh_id = Some(*mesh.id.read().unwrap()); let (_, _, translation) = transform.0.to_scale_rotation_translation(); @@ -81,10 +99,11 @@ impl ForwardInstancedPipeline { }); } - let buffer = device - .create_buffer_with_data(data.as_bytes(), wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX); - - + let buffer = device.create_buffer_with_data( + data.as_bytes(), + wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX, + ); + let mut instance_buffer_infos = Vec::new(); instance_buffer_infos.push(InstanceBufferInfo { mesh_id: last_mesh_id.unwrap(), @@ -108,40 +127,48 @@ impl Pipeline for ForwardInstancedPipeline { ); let bind_group_layout = - render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[ - wgpu::BindGroupLayoutBinding { - binding: 0, // global - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }, - wgpu::BindGroupLayoutBinding { - binding: 1, // lights - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - } - ], - }); - + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[ + wgpu::BindGroupLayoutBinding { + binding: 0, // global + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + wgpu::BindGroupLayoutBinding { + binding: 1, // lights + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + ], + }); + // TODO: this is the same as normal forward pipeline. we can probably reuse self.local_bind_group = Some({ - let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap(); - let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap(); + let forward_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME) + .unwrap(); + let light_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME) + .unwrap(); // Create bind group - render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, - bindings: &[ - wgpu::Binding { - binding: 0, - resource: forward_uniform_buffer.get_binding_resource(), - }, - wgpu::Binding { - binding: 1, - resource: light_uniform_buffer.get_binding_resource(), - } - ], - }) + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[ + wgpu::Binding { + binding: 0, + resource: forward_uniform_buffer.get_binding_resource(), + }, + wgpu::Binding { + binding: 1, + resource: light_uniform_buffer.get_binding_resource(), + }, + ], + }) }); let simple_material_uniforms_size = mem::size_of::(); @@ -164,79 +191,99 @@ impl Pipeline for ForwardInstancedPipeline { let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); - let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - bind_group_layouts: &[&bind_group_layout], - }); + let pipeline_layout = + render_graph + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout], + }); let vs_module = render_graph.device.create_shader_module(&vs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes); - self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - layout: &pipeline_layout, - vertex_stage: wgpu::ProgrammableStageDescriptor { - module: &vs_module, - entry_point: "main", - }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { - module: &fs_module, - entry_point: "main", - }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, - depth_bias: 0, - depth_bias_slope_scale: 0.0, - depth_bias_clamp: 0.0, - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[ - wgpu::ColorStateDescriptor { + self.pipeline = Some(render_graph.device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + front_face: wgpu::FrontFace::Ccw, + cull_mode: wgpu::CullMode::Back, + depth_bias: 0, + depth_bias_slope_scale: 0.0, + depth_bias_clamp: 0.0, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { format: render_graph.swap_chain_descriptor.format, color_blend: wgpu::BlendDescriptor::REPLACE, alpha_blend: wgpu::BlendDescriptor::REPLACE, write_mask: wgpu::ColorWrite::ALL, - }, - ], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { - format: self.depth_format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_read_mask: 0, - stencil_write_mask: 0, - }), - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, - })); + }], + depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + format: self.depth_format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_read_mask: 0, + stencil_write_mask: 0, + }), + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + }, + )); - self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(&render_graph.device, world)); + self.instance_buffer_infos = Some(Self::create_instance_buffer_infos( + &render_graph.device, + world, + )); } - fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) { - self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(&render_graph.device, world)); + fn render( + &mut self, + render_graph: &RenderGraphData, + pass: &mut wgpu::RenderPass, + _: &SwapChainOutput, + world: &mut World, + ) { + self.instance_buffer_infos = Some(Self::create_instance_buffer_infos( + &render_graph.device, + world, + )); pass.set_bind_group(0, self.local_bind_group.as_ref().unwrap(), &[]); - let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut mesh_storage = world + .resources + .get_mut::>() + .unwrap(); for instance_buffer_info in self.instance_buffer_infos.as_ref().unwrap().iter() { if let Some(mesh_asset) = mesh_storage.get(instance_buffer_info.mesh_id) { mesh_asset.setup_buffers(&render_graph.device); pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0); pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]); pass.set_vertex_buffers(1, &[(&instance_buffer_info.buffer, 0)]); - pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. instance_buffer_info.instance_count as u32); + pass.draw_indexed( + 0..mesh_asset.indices.len() as u32, + 0, + 0..instance_buffer_info.instance_count as u32, + ); }; } } - fn resize(&mut self, _: &RenderGraphData) { - - } + fn resize(&mut self, _: &RenderGraphData) {} fn get_pipeline(&self) -> &wgpu::RenderPipeline { self.pipeline.as_ref().unwrap() } -} \ No newline at end of file +} diff --git a/src/render/passes/forward_shadow/mod.rs b/src/render/passes/forward_shadow/mod.rs index 7db700ff44..77f367c88b 100644 --- a/src/render/passes/forward_shadow/mod.rs +++ b/src/render/passes/forward_shadow/mod.rs @@ -1,4 +1,4 @@ -use crate::{render::*, render::passes::shadow, asset::*, render::mesh::*}; +use crate::{asset::*, render::mesh::*, render::passes::shadow, render::*}; use legion::prelude::*; use wgpu::SwapChainOutput; @@ -30,138 +30,163 @@ impl Pipeline for ForwardShadowPassNew { ); let bind_group_layout = - render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[ - wgpu::BindGroupLayoutBinding { - binding: 0, // global - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }, - wgpu::BindGroupLayoutBinding { - binding: 1, // lights - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }, - wgpu::BindGroupLayoutBinding { - binding: 2, - visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::SampledTexture { - multisampled: false, - dimension: wgpu::TextureViewDimension::D2Array, - }, - }, - wgpu::BindGroupLayoutBinding { - binding: 3, - visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::Sampler, - }, - ], - }); + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[ + wgpu::BindGroupLayoutBinding { + binding: 0, // global + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + wgpu::BindGroupLayoutBinding { + binding: 1, // lights + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }, + wgpu::BindGroupLayoutBinding { + binding: 2, + visibility: wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::SampledTexture { + multisampled: false, + dimension: wgpu::TextureViewDimension::D2Array, + }, + }, + wgpu::BindGroupLayoutBinding { + binding: 3, + visibility: wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::Sampler, + }, + ], + }); self.bind_group = Some({ - let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap(); - let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap(); - let shadow_sampler = render_graph.get_sampler(shadow::SHADOW_SAMPLER_NAME).unwrap(); - let shadow_texture = render_graph.get_texture(shadow::SHADOW_TEXTURE_NAME).unwrap(); + let forward_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME) + .unwrap(); + let light_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME) + .unwrap(); + let shadow_sampler = render_graph + .get_sampler(shadow::SHADOW_SAMPLER_NAME) + .unwrap(); + let shadow_texture = render_graph + .get_texture(shadow::SHADOW_TEXTURE_NAME) + .unwrap(); // Create bind group - render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, - bindings: &[ - wgpu::Binding { - binding: 0, - resource: forward_uniform_buffer.get_binding_resource(), - }, - wgpu::Binding { - binding: 1, - resource: light_uniform_buffer.get_binding_resource(), - }, - wgpu::Binding { - binding: 2, - resource: wgpu::BindingResource::TextureView(shadow_texture), - }, - wgpu::Binding { - binding: 3, - resource: wgpu::BindingResource::Sampler(shadow_sampler), - }, - ], - }) + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[ + wgpu::Binding { + binding: 0, + resource: forward_uniform_buffer.get_binding_resource(), + }, + wgpu::Binding { + binding: 1, + resource: light_uniform_buffer.get_binding_resource(), + }, + wgpu::Binding { + binding: 2, + resource: wgpu::BindingResource::TextureView(shadow_texture), + }, + wgpu::Binding { + binding: 3, + resource: wgpu::BindingResource::Sampler(shadow_sampler), + }, + ], + }) }); - let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap(); + let material_bind_group_layout = render_graph + .get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME) + .unwrap(); - let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - bind_group_layouts: &[&bind_group_layout, material_bind_group_layout], - }); + let pipeline_layout = + render_graph + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout, material_bind_group_layout], + }); let vs_module = render_graph.device.create_shader_module(&vs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes); let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); - self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - layout: &pipeline_layout, - vertex_stage: wgpu::ProgrammableStageDescriptor { - module: &vs_module, - entry_point: "main", - }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { - module: &fs_module, - entry_point: "main", - }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, - depth_bias: 0, - depth_bias_slope_scale: 0.0, - depth_bias_clamp: 0.0, - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[ - wgpu::ColorStateDescriptor { + self.pipeline = Some(render_graph.device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + front_face: wgpu::FrontFace::Ccw, + cull_mode: wgpu::CullMode::Back, + depth_bias: 0, + depth_bias_slope_scale: 0.0, + depth_bias_clamp: 0.0, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { format: render_graph.swap_chain_descriptor.format, color_blend: wgpu::BlendDescriptor::REPLACE, alpha_blend: wgpu::BlendDescriptor::REPLACE, write_mask: wgpu::ColorWrite::ALL, - }, - ], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { - format: self.depth_format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_read_mask: 0, - stencil_write_mask: 0, - }), - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[vertex_buffer_descriptor], - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, - })); + }], + depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + format: self.depth_format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_read_mask: 0, + stencil_write_mask: 0, + }), + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[vertex_buffer_descriptor], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + }, + )); } - fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _swap_chain_output: &SwapChainOutput, world: &mut World) { + fn render( + &mut self, + render_graph: &RenderGraphData, + pass: &mut wgpu::RenderPass, + _swap_chain_output: &SwapChainOutput, + world: &mut World, + ) { let mut mesh_query = <(Read, Read>)>::query(); pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]); - let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut mesh_storage = world + .resources + .get_mut::>() + .unwrap(); for (material, mesh) in mesh_query.iter_immutable(world) { if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) { mesh_asset.setup_buffers(&render_graph.device); pass.set_bind_group(1, material.bind_group.as_ref().unwrap(), &[]); pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0); pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]); - pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. 1); + pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1); }; } } - fn resize(&mut self, _render_graph: &RenderGraphData) { - } + fn resize(&mut self, _render_graph: &RenderGraphData) {} fn get_pipeline(&self) -> &wgpu::RenderPipeline { self.pipeline.as_ref().unwrap() } -} \ No newline at end of file +} diff --git a/src/render/passes/mod.rs b/src/render/passes/mod.rs index b935e788a9..f3266478d3 100644 --- a/src/render/passes/mod.rs +++ b/src/render/passes/mod.rs @@ -1,12 +1,11 @@ - mod forward; -mod forward_shadow; mod forward_instanced; +mod forward_shadow; mod shadow; mod ui; -pub use forward::{ForwardUniforms, ForwardPipeline, ForwardPass}; -pub use forward_shadow::{ForwardShadowPassNew}; +pub use forward::{ForwardPass, ForwardPipeline, ForwardUniforms}; pub use forward_instanced::ForwardInstancedPipeline; +pub use forward_shadow::ForwardShadowPassNew; pub use shadow::ShadowPass; -pub use ui::UiPipeline; \ No newline at end of file +pub use ui::UiPipeline; diff --git a/src/render/passes/shadow/mod.rs b/src/render/passes/shadow/mod.rs index 957a7085ad..aeaf81690d 100644 --- a/src/render/passes/shadow/mod.rs +++ b/src/render/passes/shadow/mod.rs @@ -2,4 +2,4 @@ mod shadow_pass; mod shadow_pipeline; pub use shadow_pass::*; -pub use shadow_pipeline::*; \ No newline at end of file +pub use shadow_pipeline::*; diff --git a/src/render/passes/shadow/shadow_pass.rs b/src/render/passes/shadow/shadow_pass.rs index ff0a47d63a..e2bd339d69 100644 --- a/src/render/passes/shadow/shadow_pass.rs +++ b/src/render/passes/shadow/shadow_pass.rs @@ -1,4 +1,4 @@ -use crate::{render::*, render::passes::shadow, LocalToWorld, Translation}; +use crate::{render::passes::shadow, render::*, LocalToWorld, Translation}; use legion::prelude::*; use std::mem; @@ -13,7 +13,11 @@ pub struct ShadowPass { pub const SHADOW_TEXTURE_NAME: &str = "shadow_texture"; impl ShadowPass { - pub fn new(shadow_size: wgpu::Extent3d, shadow_format: wgpu::TextureFormat, max_lights: usize) -> Self { + pub fn new( + shadow_size: wgpu::Extent3d, + shadow_format: wgpu::TextureFormat, + max_lights: usize, + ) -> Self { ShadowPass { light_index: -1, shadow_texture: None, @@ -26,15 +30,17 @@ impl ShadowPass { impl Pass for ShadowPass { fn initialize(&self, render_graph: &mut RenderGraphData) { - let shadow_texture = render_graph.device.create_texture(&wgpu::TextureDescriptor { - size: self.shadow_size, - array_layer_count: self.max_lights as u32, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: self.shadow_format, - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED, - }); + let shadow_texture = render_graph + .device + .create_texture(&wgpu::TextureDescriptor { + size: self.shadow_size, + array_layer_count: self.max_lights as u32, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: self.shadow_format, + usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED, + }); let shadow_view = shadow_texture.create_default_view(); render_graph.set_texture(SHADOW_TEXTURE_NAME, shadow_view); @@ -74,8 +80,12 @@ impl Pass for ShadowPass { // The light uniform buffer already has the projection, // let's just copy it over to the shadow uniform buffer. - let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap(); - let shadow_pipeline_uniform_buffer = render_graph.get_uniform_buffer(shadow::SHADOW_PIPELINE_UNIFORMS).unwrap(); + let light_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME) + .unwrap(); + let shadow_pipeline_uniform_buffer = render_graph + .get_uniform_buffer(shadow::SHADOW_PIPELINE_UNIFORMS) + .unwrap(); encoder.copy_buffer_to_buffer( &light_uniform_buffer.buffer, (i * mem::size_of::()) as wgpu::BufferAddress, @@ -110,4 +120,4 @@ impl Pass for ShadowPass { fn should_repeat(&self) -> bool { return self.light_index != -1; } -} \ No newline at end of file +} diff --git a/src/render/passes/shadow/shadow_pipeline.rs b/src/render/passes/shadow/shadow_pipeline.rs index 90ad28541e..61bc376059 100644 --- a/src/render/passes/shadow/shadow_pipeline.rs +++ b/src/render/passes/shadow/shadow_pipeline.rs @@ -30,22 +30,27 @@ impl ShadowPipeline { impl Pipeline for ShadowPipeline { fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) { - let bind_group_layout = render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[wgpu::BindGroupLayoutBinding { - binding: 0, // global - visibility: wgpu::ShaderStage::VERTEX, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }], - }); + let bind_group_layout = + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[wgpu::BindGroupLayoutBinding { + binding: 0, // global + visibility: wgpu::ShaderStage::VERTEX, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }], + }); - let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap(); + let material_bind_group_layout = render_graph + .get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME) + .unwrap(); - let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - bind_group_layouts: &[ - &bind_group_layout, - material_bind_group_layout, - ], - }); + let pipeline_layout = + render_graph + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout, material_bind_group_layout], + }); let uniform_size = mem::size_of::() as wgpu::BufferAddress; let uniform_buf = render_graph.device.create_buffer(&wgpu::BufferDescriptor { @@ -54,81 +59,91 @@ impl Pipeline for ShadowPipeline { }); // Create bind group - self.bind_group = Some(render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, - bindings: &[wgpu::Binding { - binding: 0, - resource: wgpu::BindingResource::Buffer { - buffer: &uniform_buf, - range: 0..uniform_size, - }, - }], - })); + self.bind_group = Some( + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::Buffer { + buffer: &uniform_buf, + range: 0..uniform_size, + }, + }], + }), + ); - render_graph.set_uniform_buffer(SHADOW_PIPELINE_UNIFORMS, UniformBuffer { - buffer: uniform_buf, - size: uniform_size, - }); + render_graph.set_uniform_buffer( + SHADOW_PIPELINE_UNIFORMS, + UniformBuffer { + buffer: uniform_buf, + size: uniform_size, + }, + ); // Create other resources - let shadow_sampler = render_graph.device.create_sampler(&wgpu::SamplerDescriptor { - 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, - lod_min_clamp: -100.0, - lod_max_clamp: 100.0, - compare_function: wgpu::CompareFunction::LessEqual, - }); + let shadow_sampler = render_graph + .device + .create_sampler(&wgpu::SamplerDescriptor { + 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, + lod_min_clamp: -100.0, + lod_max_clamp: 100.0, + compare_function: wgpu::CompareFunction::LessEqual, + }); render_graph.set_sampler(SHADOW_SAMPLER_NAME, shadow_sampler); let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); // Create the render pipeline - let vs_bytes = - shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex); + let vs_bytes = shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex); let fs_bytes = shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment); let vs_module = render_graph.device.create_shader_module(&vs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes); - self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - layout: &pipeline_layout, - vertex_stage: wgpu::ProgrammableStageDescriptor { - module: &vs_module, - entry_point: "main", + self.pipeline = Some(render_graph.device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + 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, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[], + depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + format: self.shadow_format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::LessEqual, + stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_read_mask: 0, + stencil_write_mask: 0, + }), + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[vertex_buffer_descriptor], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { - module: &fs_module, - entry_point: "main", - }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - 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, - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { - format: self.shadow_format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::LessEqual, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_read_mask: 0, - stencil_write_mask: 0, - }), - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[vertex_buffer_descriptor], - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, - })); + )); } fn render( @@ -163,4 +178,4 @@ impl Pipeline for ShadowPipeline { fn get_pipeline(&self) -> &wgpu::RenderPipeline { self.pipeline.as_ref().unwrap() } -} \ No newline at end of file +} diff --git a/src/render/passes/ui/mod.rs b/src/render/passes/ui/mod.rs index 607b6d838f..4a13d53327 100644 --- a/src/render/passes/ui/mod.rs +++ b/src/render/passes/ui/mod.rs @@ -1,7 +1,12 @@ -use crate::{render::{*, instancing::InstanceBufferInfo}, asset::*, render::mesh::*, math}; +use crate::{ + asset::*, + math, + render::mesh::*, + render::{instancing::InstanceBufferInfo, *}, +}; use legion::prelude::*; -use zerocopy::{AsBytes, FromBytes}; use wgpu::SwapChainOutput; +use zerocopy::{AsBytes, FromBytes}; #[repr(C)] #[derive(Clone, Copy, AsBytes, FromBytes)] @@ -25,11 +30,15 @@ impl UiPipeline { pipeline: None, bind_group: None, quad: None, - depth_format: wgpu::TextureFormat::Depth32Float + depth_format: wgpu::TextureFormat::Depth32Float, } } - pub fn create_rect_buffers(&self, device: &wgpu::Device, world: &World) -> Vec { + pub fn create_rect_buffers( + &self, + device: &wgpu::Device, + world: &World, + ) -> Vec { let mut rect_query = >::query(); let rect_count = rect_query.iter_immutable(world).count(); @@ -40,21 +49,22 @@ impl UiPipeline { let mut data = Vec::with_capacity(rect_count); // TODO: this probably isn't the best way to handle z-ordering let mut z = 0.9999; - for rect in rect_query.iter_immutable(world) - { + for rect in rect_query.iter_immutable(world) { data.push(RectData { position: rect.position.into(), dimensions: rect.dimensions.into(), color: rect.color.into(), z_index: z, }); - + z -= 0.0001; } - let buffer = device - .create_buffer_with_data(data.as_bytes(), wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX); - + let buffer = device.create_buffer_with_data( + data.as_bytes(), + wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX, + ); + let mesh_id = *self.quad.as_ref().unwrap().id.read().unwrap(); let mut instance_buffer_infos = Vec::new(); @@ -70,44 +80,42 @@ impl UiPipeline { impl Pipeline for UiPipeline { fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) { - let vs_bytes = shader::load_glsl( - include_str!("ui.vert"), - shader::ShaderStage::Vertex, - ); - let fs_bytes = shader::load_glsl( - include_str!("ui.frag"), - shader::ShaderStage::Fragment, - ); + let vs_bytes = shader::load_glsl(include_str!("ui.vert"), shader::ShaderStage::Vertex); + let fs_bytes = shader::load_glsl(include_str!("ui.frag"), shader::ShaderStage::Fragment); let bind_group_layout = - render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[ - wgpu::BindGroupLayoutBinding { - binding: 0, // global_2d - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }, - ], - }); + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[wgpu::BindGroupLayoutBinding { + binding: 0, // global_2d + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }], + }); self.bind_group = Some({ - - let global_2d_uniform_buffer = render_graph.get_uniform_buffer(render_resources::GLOBAL_2D_UNIFORM_BUFFER_NAME).unwrap(); + let global_2d_uniform_buffer = render_graph + .get_uniform_buffer(render_resources::GLOBAL_2D_UNIFORM_BUFFER_NAME) + .unwrap(); // Create bind group - render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, - bindings: &[ - wgpu::Binding { + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[wgpu::Binding { binding: 0, resource: global_2d_uniform_buffer.get_binding_resource(), - }, - ], - }) + }], + }) }); { - let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut mesh_storage = world + .resources + .get_mut::>() + .unwrap(); let quad = Mesh::load(MeshType::Quad { north_west: math::vec2(-0.5, 0.5), @@ -118,9 +126,12 @@ impl Pipeline for UiPipeline { self.quad = Some(mesh_storage.add(quad, "ui_quad")); } - let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - bind_group_layouts: &[&bind_group_layout], - }); + let pipeline_layout = + render_graph + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout], + }); let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); let rect_data_size = mem::size_of::(); @@ -154,69 +165,81 @@ impl Pipeline for UiPipeline { let vs_module = render_graph.device.create_shader_module(&vs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes); - self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - layout: &pipeline_layout, - vertex_stage: wgpu::ProgrammableStageDescriptor { - module: &vs_module, - entry_point: "main", - }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { - module: &fs_module, - entry_point: "main", - }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::None, - depth_bias: 0, - depth_bias_slope_scale: 0.0, - depth_bias_clamp: 0.0, - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[ - wgpu::ColorStateDescriptor { + self.pipeline = Some(render_graph.device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + front_face: wgpu::FrontFace::Ccw, + cull_mode: wgpu::CullMode::None, + depth_bias: 0, + depth_bias_slope_scale: 0.0, + depth_bias_clamp: 0.0, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { format: render_graph.swap_chain_descriptor.format, color_blend: wgpu::BlendDescriptor::REPLACE, alpha_blend: wgpu::BlendDescriptor::REPLACE, write_mask: wgpu::ColorWrite::ALL, - }, - ], - depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { - format: self.depth_format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_read_mask: 0, - stencil_write_mask: 0, - }), - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, - })); + }], + depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor { + format: self.depth_format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + stencil_read_mask: 0, + stencil_write_mask: 0, + }), + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + }, + )); } - fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) { + fn render( + &mut self, + render_graph: &RenderGraphData, + pass: &mut wgpu::RenderPass, + _: &SwapChainOutput, + world: &mut World, + ) { let instance_buffer_infos = Some(self.create_rect_buffers(&render_graph.device, world)); pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]); - let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let mut mesh_storage = world + .resources + .get_mut::>() + .unwrap(); for instance_buffer_info in instance_buffer_infos.as_ref().unwrap().iter() { if let Some(mesh_asset) = mesh_storage.get(instance_buffer_info.mesh_id) { mesh_asset.setup_buffers(&render_graph.device); pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0); pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]); pass.set_vertex_buffers(1, &[(&instance_buffer_info.buffer, 0)]); - pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. instance_buffer_info.instance_count as u32); + pass.draw_indexed( + 0..mesh_asset.indices.len() as u32, + 0, + 0..instance_buffer_info.instance_count as u32, + ); }; } } - - fn resize(&mut self, _: &RenderGraphData) { - } + + fn resize(&mut self, _: &RenderGraphData) {} fn get_pipeline(&self) -> &wgpu::RenderPipeline { self.pipeline.as_ref().unwrap() } -} \ No newline at end of file +} diff --git a/src/render/rect.rs b/src/render/rect.rs index 4e06e2e48f..e947a74452 100644 --- a/src/render/rect.rs +++ b/src/render/rect.rs @@ -14,4 +14,4 @@ impl Rect { color, } } -} \ No newline at end of file +} diff --git a/src/render/render_graph/mod.rs b/src/render/render_graph/mod.rs index 7f22bdcf15..a6081f5e00 100644 --- a/src/render/render_graph/mod.rs +++ b/src/render/render_graph/mod.rs @@ -7,8 +7,8 @@ pub use pipeline::Pipeline; pub use render_resource_manager::RenderResourceManager; use crate::render::UniformBuffer; -use std::collections::HashMap; use legion::world::World; +use std::collections::HashMap; pub struct RenderGraph { pub data: Option, @@ -29,7 +29,12 @@ pub struct RenderGraphData { bind_group_layouts: HashMap, } impl RenderGraphData { - pub fn new(device: wgpu::Device, swap_chain_descriptor: wgpu::SwapChainDescriptor, queue: wgpu::Queue, surface: wgpu::Surface) -> Self { + pub fn new( + device: wgpu::Device, + swap_chain_descriptor: wgpu::SwapChainDescriptor, + queue: wgpu::Queue, + surface: wgpu::Surface, + ) -> Self { RenderGraphData { textures: HashMap::new(), samplers: HashMap::new(), @@ -43,7 +48,8 @@ impl RenderGraphData { } pub fn set_uniform_buffer(&mut self, name: &str, uniform_buffer: UniformBuffer) { - self.uniform_buffers.insert(name.to_string(), uniform_buffer); + self.uniform_buffers + .insert(name.to_string(), uniform_buffer); } pub fn get_uniform_buffer(&self, name: &str) -> Option<&UniformBuffer> { @@ -51,7 +57,8 @@ impl RenderGraphData { } pub fn set_bind_group_layout(&mut self, name: &str, bind_group_layout: wgpu::BindGroupLayout) { - self.bind_group_layouts.insert(name.to_string(), bind_group_layout); + self.bind_group_layouts + .insert(name.to_string(), bind_group_layout); } pub fn get_bind_group_layout(&self, name: &str) -> Option<&wgpu::BindGroupLayout> { @@ -86,7 +93,14 @@ impl RenderGraph { } } - pub fn initialize(&mut self, world: &mut World, device: wgpu::Device, swap_chain_descriptor: wgpu::SwapChainDescriptor, queue: wgpu::Queue, surface: wgpu::Surface) { + pub fn initialize( + &mut self, + world: &mut World, + device: wgpu::Device, + swap_chain_descriptor: wgpu::SwapChainDescriptor, + queue: wgpu::Queue, + surface: wgpu::Surface, + ) { let mut data = RenderGraphData::new(device, swap_chain_descriptor, queue, surface); for render_resource_manager in self.render_resource_managers.iter_mut() { render_resource_manager.initialize(&mut data, world); @@ -95,7 +109,7 @@ impl RenderGraph { for pass in self.passes.values_mut() { pass.initialize(&mut data); } - + for pipeline in self.pipelines.values_mut() { pipeline.initialize(&mut data, world); } @@ -109,8 +123,9 @@ impl RenderGraph { .expect("Timeout when acquiring next swap chain texture"); let data = self.data.as_mut().unwrap(); - let mut encoder = - data.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); + let mut encoder = data + .device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); for render_resource_manager in self.render_resource_managers.iter_mut() { render_resource_manager.update(data, &mut encoder, world); @@ -121,7 +136,7 @@ impl RenderGraph { let render_pass = pass.begin(data, world, &mut encoder, &frame); if let Some(mut render_pass) = render_pass { // TODO: assign pipelines to specific passes - if let Some(pipeline_names) = self.pass_pipelines.get(pass_name) { + if let Some(pipeline_names) = self.pass_pipelines.get(pass_name) { for pipeline_name in pipeline_names.iter() { let pipeline = self.pipelines.get_mut(pipeline_name).unwrap(); render_pass.set_pipeline(pipeline.get_pipeline()); @@ -144,9 +159,12 @@ impl RenderGraph { let data = self.data.as_mut().unwrap(); data.swap_chain_descriptor.width = width; data.swap_chain_descriptor.height = height; - let swap_chain = data.device.create_swap_chain(&data.surface, &data.swap_chain_descriptor); - let mut encoder = - data.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); + let swap_chain = data + .device + .create_swap_chain(&data.surface, &data.swap_chain_descriptor); + let mut encoder = data + .device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); for render_resource_manager in self.render_resource_managers.iter_mut() { render_resource_manager.resize(data, &mut encoder, world); @@ -166,11 +184,19 @@ impl RenderGraph { swap_chain } - pub fn add_render_resource_manager(&mut self, render_resource_manager: Box) { + pub fn add_render_resource_manager( + &mut self, + render_resource_manager: Box, + ) { self.render_resource_managers.push(render_resource_manager); } - pub fn set_pipeline(&mut self, pass_name: &str, pipeline_name: &str, pipeline: Box) { + pub fn set_pipeline( + &mut self, + pass_name: &str, + pipeline_name: &str, + pipeline: Box, + ) { self.pipelines.insert(pipeline_name.to_string(), pipeline); if let None = self.pass_pipelines.get_mut(pass_name) { let pipelines = Vec::new(); @@ -185,4 +211,4 @@ impl RenderGraph { pub fn set_pass(&mut self, name: &str, pass: Box) { self.passes.insert(name.to_string(), pass); } -} \ No newline at end of file +} diff --git a/src/render/render_graph/pass.rs b/src/render/render_graph/pass.rs index db01ff5016..bfdaf2a152 100644 --- a/src/render/render_graph/pass.rs +++ b/src/render/render_graph/pass.rs @@ -3,7 +3,13 @@ use legion::world::World; pub trait Pass { fn initialize(&self, render_graph: &mut RenderGraphData); - fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, world: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option>; + fn begin<'a>( + &mut self, + render_graph: &mut RenderGraphData, + world: &mut World, + encoder: &'a mut wgpu::CommandEncoder, + frame: &'a wgpu::SwapChainOutput, + ) -> Option>; fn should_repeat(&self) -> bool; fn resize(&self, render_graph: &mut RenderGraphData); -} \ No newline at end of file +} diff --git a/src/render/render_graph/pipeline.rs b/src/render/render_graph/pipeline.rs index 0262473afe..bd11cda032 100644 --- a/src/render/render_graph/pipeline.rs +++ b/src/render/render_graph/pipeline.rs @@ -1,10 +1,16 @@ +use crate::render::RenderGraphData; use legion::world::World; use wgpu::SwapChainOutput; -use crate::render::RenderGraphData; pub trait Pipeline { fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World); - fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, frame: &SwapChainOutput, world: &mut World); + fn render( + &mut self, + render_graph: &RenderGraphData, + pass: &mut wgpu::RenderPass, + frame: &SwapChainOutput, + world: &mut World, + ); fn resize(&mut self, render_graph: &RenderGraphData); fn get_pipeline(&self) -> &wgpu::RenderPipeline; -} \ No newline at end of file +} diff --git a/src/render/render_graph/render_resource_manager.rs b/src/render/render_graph/render_resource_manager.rs index 847bb1a93d..d7bf9587dd 100644 --- a/src/render/render_graph/render_resource_manager.rs +++ b/src/render/render_graph/render_resource_manager.rs @@ -1,8 +1,18 @@ -use legion::world::World; use crate::render::RenderGraphData; +use legion::world::World; pub trait RenderResourceManager { fn initialize(&self, render_graph: &mut RenderGraphData, world: &mut World); - fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World); - fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World); -} \ No newline at end of file + fn update<'a>( + &mut self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ); + fn resize<'a>( + &self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ); +} diff --git a/src/render/render_resources/global_2d_resource_manager.rs b/src/render/render_resources/global_2d_resource_manager.rs index ca81e11c8c..0ded3a101c 100644 --- a/src/render/render_resources/global_2d_resource_manager.rs +++ b/src/render/render_resources/global_2d_resource_manager.rs @@ -1,4 +1,4 @@ -use crate::{render::*, math}; +use crate::{math, render::*}; use legion::prelude::*; use std::mem; @@ -33,19 +33,40 @@ impl RenderResourceManager for Global2dResourceManager { render_graph.set_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME, uniform_buffer); } - fn update<'a>(&mut self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) { - + fn update<'a>( + &mut self, + _render_graph: &mut RenderGraphData, + _encoder: &'a mut wgpu::CommandEncoder, + _world: &mut World, + ) { } - fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) { + fn resize<'a>( + &self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ) { for (mut camera, _) in <(Write, Read)>::query().iter(world) { - camera.update(render_graph.swap_chain_descriptor.width, render_graph.swap_chain_descriptor.height); + camera.update( + render_graph.swap_chain_descriptor.width, + render_graph.swap_chain_descriptor.height, + ); let camera_matrix: [[f32; 4]; 4] = camera.view_matrix.to_cols_array_2d(); let matrix_size = mem::size_of::<[[f32; 4]; 4]>() as u64; - let temp_camera_buffer = - render_graph.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); - let global_2d_uniform_buffer = render_graph.get_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME).unwrap(); - encoder.copy_buffer_to_buffer(&temp_camera_buffer, 0, &global_2d_uniform_buffer.buffer, 0, matrix_size); + let temp_camera_buffer = render_graph + .device + .create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); + let global_2d_uniform_buffer = render_graph + .get_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME) + .unwrap(); + encoder.copy_buffer_to_buffer( + &temp_camera_buffer, + 0, + &global_2d_uniform_buffer.buffer, + 0, + matrix_size, + ); } } -} \ No newline at end of file +} diff --git a/src/render/render_resources/global_resource_manager.rs b/src/render/render_resources/global_resource_manager.rs index 6229abedb0..9cd2e28768 100644 --- a/src/render/render_resources/global_resource_manager.rs +++ b/src/render/render_resources/global_resource_manager.rs @@ -1,4 +1,4 @@ -use crate::{render::*, render::passes::ForwardUniforms, LocalToWorld, math}; +use crate::{math, render::passes::ForwardUniforms, render::*, LocalToWorld}; use legion::prelude::*; use std::mem; @@ -28,18 +28,42 @@ impl RenderResourceManager for GlobalResourceManager { }; render_graph.set_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME, uniform_buffer); } - fn update<'a>(&mut self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) { - + fn update<'a>( + &mut self, + _render_graph: &mut RenderGraphData, + _encoder: &'a mut wgpu::CommandEncoder, + _world: &mut World, + ) { } - fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) { - for (mut camera, local_to_world, _) in <(Write, Read, Read)>::query().iter(world) { - camera.update(render_graph.swap_chain_descriptor.width, render_graph.swap_chain_descriptor.height); - let camera_matrix: [[f32; 4]; 4] = (camera.view_matrix * local_to_world.0).to_cols_array_2d(); + fn resize<'a>( + &self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ) { + for (mut camera, local_to_world, _) in + <(Write, Read, Read)>::query().iter(world) + { + camera.update( + render_graph.swap_chain_descriptor.width, + render_graph.swap_chain_descriptor.height, + ); + let camera_matrix: [[f32; 4]; 4] = + (camera.view_matrix * local_to_world.0).to_cols_array_2d(); let matrix_size = mem::size_of::<[[f32; 4]; 4]>() as u64; - let temp_camera_buffer = - render_graph.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); - let forward_uniform_buffer = render_graph.get_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME).unwrap(); - encoder.copy_buffer_to_buffer(&temp_camera_buffer, 0, &forward_uniform_buffer.buffer, 0, matrix_size); + let temp_camera_buffer = render_graph + .device + .create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); + let forward_uniform_buffer = render_graph + .get_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME) + .unwrap(); + encoder.copy_buffer_to_buffer( + &temp_camera_buffer, + 0, + &forward_uniform_buffer.buffer, + 0, + matrix_size, + ); } } -} \ No newline at end of file +} diff --git a/src/render/render_resources/light_resource_manager.rs b/src/render/render_resources/light_resource_manager.rs index 8fe53c0d21..0864ee975c 100644 --- a/src/render/render_resources/light_resource_manager.rs +++ b/src/render/render_resources/light_resource_manager.rs @@ -23,7 +23,7 @@ impl LightResourceManager { impl RenderResourceManager for LightResourceManager { fn initialize(&self, render_graph: &mut RenderGraphData, _world: &mut World) { let light_uniform_size = - (self.max_lights * mem::size_of::()) as wgpu::BufferAddress; + (self.max_lights * mem::size_of::()) as wgpu::BufferAddress; let light_uniform_buffer = UniformBuffer { buffer: render_graph.device.create_buffer(&wgpu::BufferDescriptor { @@ -37,7 +37,12 @@ impl RenderResourceManager for LightResourceManager { render_graph.set_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME, light_uniform_buffer); } - fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) { + fn update<'a>( + &mut self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ) { if self.lights_are_dirty { let mut light_query = <(Read, Read, Read)>::query(); let light_count = light_query.iter(world).count(); @@ -49,16 +54,21 @@ impl RenderResourceManager for LightResourceManager { self.lights_are_dirty = false; let size = mem::size_of::(); let total_size = size * light_count; - let temp_buf_data = - render_graph.device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC); + let temp_buf_data = render_graph + .device + .create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC); for ((light, local_to_world, translation), slot) in light_query .iter(world) .zip(temp_buf_data.data.chunks_exact_mut(size)) { - slot.copy_from_slice(LightRaw::from(&light, &local_to_world.0, &translation).as_bytes()); + slot.copy_from_slice( + LightRaw::from(&light, &local_to_world.0, &translation).as_bytes(), + ); } - let light_uniform_buffer = render_graph.get_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME).unwrap(); + let light_uniform_buffer = render_graph + .get_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME) + .unwrap(); encoder.copy_buffer_to_buffer( &temp_buf_data.finish(), 0, @@ -66,8 +76,13 @@ impl RenderResourceManager for LightResourceManager { 0, total_size as wgpu::BufferAddress, ); - } } - fn resize<'a>(&self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) { } -} \ No newline at end of file + fn resize<'a>( + &self, + _render_graph: &mut RenderGraphData, + _encoder: &'a mut wgpu::CommandEncoder, + _world: &mut World, + ) { + } +} diff --git a/src/render/render_resources/material_resource_manager.rs b/src/render/render_resources/material_resource_manager.rs index 2209144f63..5adb50fa4d 100644 --- a/src/render/render_resources/material_resource_manager.rs +++ b/src/render/render_resources/material_resource_manager.rs @@ -11,30 +11,40 @@ pub struct MaterialResourceManager; impl RenderResourceManager for MaterialResourceManager { fn initialize(&self, render_graph: &mut RenderGraphData, _world: &mut World) { let material_bind_group_layout = - render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[wgpu::BindGroupLayoutBinding { - binding: 0, - visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::UniformBuffer { dynamic: false }, - }], - }); + render_graph + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[wgpu::BindGroupLayoutBinding { + binding: 0, + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, + ty: wgpu::BindingType::UniformBuffer { dynamic: false }, + }], + }); - render_graph.set_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME, material_bind_group_layout); + render_graph + .set_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME, material_bind_group_layout); } - fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) { - let mut entities = <(Write, Read)>::query() - .filter(!component::()); + fn update<'a>( + &mut self, + render_graph: &mut RenderGraphData, + encoder: &'a mut wgpu::CommandEncoder, + world: &mut World, + ) { + let mut entities = + <(Write, Read)>::query().filter(!component::()); let entities_count = entities.iter(world).count(); if entities_count == 0 { return; } let size = mem::size_of::(); - let temp_buf_data = render_graph.device + let temp_buf_data = render_graph + .device .create_buffer_mapped(entities_count * size, wgpu::BufferUsage::COPY_SRC); - for ((material, transform), slot) in entities.iter(world) + for ((material, transform), slot) in entities + .iter(world) .zip(temp_buf_data.data.chunks_exact_mut(size)) { slot.copy_from_slice( @@ -45,27 +55,36 @@ impl RenderResourceManager for MaterialResourceManager { .as_bytes(), ); } - - let material_bind_group_layout = render_graph.get_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap(); - for mut material in >::query().filter(!component::()).iter(world) { + let material_bind_group_layout = render_graph + .get_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME) + .unwrap(); + + for mut material in >::query() + .filter(!component::()) + .iter(world) + { if let None = material.bind_group { - let material_uniform_size = mem::size_of::() as wgpu::BufferAddress; + let material_uniform_size = + mem::size_of::() as wgpu::BufferAddress; let uniform_buf = render_graph.device.create_buffer(&wgpu::BufferDescriptor { size: material_uniform_size, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, }); - let bind_group = render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: material_bind_group_layout, - bindings: &[wgpu::Binding { - binding: 0, - resource: wgpu::BindingResource::Buffer { - buffer: &uniform_buf, - range: 0 .. material_uniform_size, - }, - }], - }); + let bind_group = + render_graph + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: material_bind_group_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::Buffer { + buffer: &uniform_buf, + range: 0..material_uniform_size, + }, + }], + }); material.bind_group = Some(bind_group); material.uniform_buf = Some(uniform_buf); @@ -83,7 +102,11 @@ impl RenderResourceManager for MaterialResourceManager { ); } } - fn resize<'a>(&self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) { - + fn resize<'a>( + &self, + _render_graph: &mut RenderGraphData, + _encoder: &'a mut wgpu::CommandEncoder, + _world: &mut World, + ) { } -} \ No newline at end of file +} diff --git a/src/render/render_resources/mod.rs b/src/render/render_resources/mod.rs index 520301b42f..4ffb8784b4 100644 --- a/src/render/render_resources/mod.rs +++ b/src/render/render_resources/mod.rs @@ -1,9 +1,9 @@ -mod light_resource_manager; -mod global_resource_manager; mod global_2d_resource_manager; +mod global_resource_manager; +mod light_resource_manager; mod material_resource_manager; -pub use light_resource_manager::*; -pub use global_resource_manager::*; pub use global_2d_resource_manager::*; -pub use material_resource_manager::*; \ No newline at end of file +pub use global_resource_manager::*; +pub use light_resource_manager::*; +pub use material_resource_manager::*; diff --git a/src/render/shader.rs b/src/render/shader.rs index a347ddeb78..e71410c8fe 100644 --- a/src/render/shader.rs +++ b/src/render/shader.rs @@ -13,4 +13,4 @@ pub fn load_glsl(code: &str, stage: ShaderStage) -> Vec { }; wgpu::read_spirv(glsl_to_spirv::compile(&code, ty).unwrap()).unwrap() -} \ No newline at end of file +} diff --git a/src/vertex.rs b/src/vertex.rs index f5eda43b12..b6e4247469 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -1,5 +1,5 @@ -use zerocopy::{AsBytes, FromBytes}; use std::convert::From; +use zerocopy::{AsBytes, FromBytes}; #[repr(C)] #[derive(Clone, Copy, AsBytes, FromBytes)] @@ -9,7 +9,7 @@ pub struct Vertex { } impl From<([f32; 4], [f32; 4])> for Vertex { - fn from((position, normal): ([f32; 4], [f32; 4])) -> Self { + fn from((position, normal): ([f32; 4], [f32; 4])) -> Self { Vertex { position: position, normal: normal, @@ -18,28 +18,48 @@ impl From<([f32; 4], [f32; 4])> for Vertex { } impl From<([f32; 3], [f32; 3])> for Vertex { - fn from((position, normal): ([f32; 3], [f32; 3])) -> Self { + fn from((position, normal): ([f32; 3], [f32; 3])) -> Self { Vertex { - position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0], + position: [ + position[0] as f32, + position[1] as f32, + position[2] as f32, + 1.0, + ], normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0], } } } impl From<([i8; 4], [i8; 4])> for Vertex { - fn from((position, normal): ([i8; 4], [i8; 4])) -> Self { + fn from((position, normal): ([i8; 4], [i8; 4])) -> Self { Vertex { - position: [position[0] as f32, position[1] as f32, position[2] as f32, position[3] as f32], - normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, normal[3] as f32], + position: [ + position[0] as f32, + position[1] as f32, + position[2] as f32, + position[3] as f32, + ], + normal: [ + normal[0] as f32, + normal[1] as f32, + normal[2] as f32, + normal[3] as f32, + ], } } } impl From<([i8; 3], [i8; 3])> for Vertex { - fn from((position, normal): ([i8; 3], [i8; 3])) -> Self { + fn from((position, normal): ([i8; 3], [i8; 3])) -> Self { Vertex { - position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0], + position: [ + position[0] as f32, + position[1] as f32, + position[2] as f32, + 1.0, + ], normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0], } } -} \ No newline at end of file +}