|
|
|
@ -18,7 +18,11 @@ use legion::world::SubWorld;
|
|
|
|
|
use legion::*;
|
|
|
|
|
use rapier3d::parry::motion::RigidMotionComposition;
|
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
|
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
|
|
|
|
|
use wgpu::{
|
|
|
|
|
BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, CommandEncoder, Device,
|
|
|
|
|
Features, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor,
|
|
|
|
|
SwapChainFrame, TextureView, VertexState,
|
|
|
|
|
};
|
|
|
|
|
use winit_24::dpi::PhysicalSize;
|
|
|
|
|
use winit_24::platform::unix::x11::ffi::Time;
|
|
|
|
|
use winit_24::window::Window;
|
|
|
|
@ -26,24 +30,23 @@ use winit_24::window::Window;
|
|
|
|
|
use crate::camera::{Camera, CameraController};
|
|
|
|
|
use crate::components::{Mesh, Position, RangeCopy};
|
|
|
|
|
use crate::current_ui;
|
|
|
|
|
use crate::geometry::{load_obj, Vertex, RawMesh};
|
|
|
|
|
use crate::geometry::{load_obj, RawMesh, Vertex};
|
|
|
|
|
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
|
|
|
|
use crate::light::{DirectionalLight, LightRaw};
|
|
|
|
|
use crate::render::{EntityUniforms, ShadowUniforms, ForwardUniforms};
|
|
|
|
|
|
|
|
|
|
use crate::render::{CameraProjectionView, EntityUniforms, ForwardUniforms, ShadowUniforms};
|
|
|
|
|
|
|
|
|
|
/// A render pass consists of a pipeline, bindgroup, and uniform buf
|
|
|
|
|
/// The uniform buf is just the ShadowUniforms or ForwardUniforms
|
|
|
|
|
/// They are uniform across all cu's.
|
|
|
|
|
/// And the bindgroup is just the localbindgroup (the EntityUniforms) and the rest
|
|
|
|
|
///
|
|
|
|
|
pub struct Pass {
|
|
|
|
|
pub pipeline: wgpu::RenderPipeline,
|
|
|
|
|
pub bind_group: wgpu::BindGroup,
|
|
|
|
|
pub uniform_buf: wgpu::Buffer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct RenderState {
|
|
|
|
|
|
|
|
|
|
swapchain: SwapChain,
|
|
|
|
|
swapchain_description: SwapChainDescriptor,
|
|
|
|
|
instance: Arc<Instance>,
|
|
|
|
@ -61,6 +64,15 @@ pub struct RenderState {
|
|
|
|
|
pub(in crate::render) forward_pass: Pass,
|
|
|
|
|
pub(in crate::render) forward_depth: wgpu::TextureView,
|
|
|
|
|
|
|
|
|
|
// Gbuffer bs
|
|
|
|
|
pub(in crate::render) gbuffer_pass: Pass,
|
|
|
|
|
pub(in crate::render) gbuffer_cam_projection_buffer: wgpu::Buffer,
|
|
|
|
|
pub(in crate::render) gbuffer_depth_texture: wgpu::TextureView,
|
|
|
|
|
pub(in crate::render) gbuffer_depth_views: Vec<Arc<TextureView>>,
|
|
|
|
|
pub(in crate::render) gbuffer_target_texture: wgpu::TextureView,
|
|
|
|
|
pub(in crate::render) gbuffer_target_views: Vec<Arc<TextureView>>,
|
|
|
|
|
|
|
|
|
|
// this is for the set=1 entity uniforms
|
|
|
|
|
entity_bind_group_layout: BindGroupLayout,
|
|
|
|
|
|
|
|
|
|
pub(in crate::render) light_uniform_buf: wgpu::Buffer,
|
|
|
|
@ -73,12 +85,14 @@ pub struct RenderState {
|
|
|
|
|
impl RenderState {
|
|
|
|
|
const MAX_LIGHTS: usize = 10;
|
|
|
|
|
const SHADOW_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
|
const G_BUFFER_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba32Float;
|
|
|
|
|
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
|
|
|
|
|
|
const SHADOW_SIZE: wgpu::Extent3d = wgpu::Extent3d {
|
|
|
|
|
width: 1024,
|
|
|
|
|
height: 1024,
|
|
|
|
|
depth: Self::MAX_LIGHTS as u32,
|
|
|
|
|
};
|
|
|
|
|
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
|
|
|
|
|
|
/// Generate a projection matrix with no look-at yet
|
|
|
|
|
pub fn generate_matrix(&self) -> cgmath::Matrix4<f32> {
|
|
|
|
@ -109,10 +123,7 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a buffer for a mesh
|
|
|
|
|
fn create_buffer(
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
raw_mesh: &RawMesh,
|
|
|
|
|
) -> (Arc<Buffer>, Arc<Buffer>) {
|
|
|
|
|
fn create_buffer(device: &wgpu::Device, raw_mesh: &RawMesh) -> (Arc<Buffer>, Arc<Buffer>) {
|
|
|
|
|
let vertex_buf = Arc::new(
|
|
|
|
|
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("vertex-buffer"),
|
|
|
|
@ -133,8 +144,11 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Take a meshes raw representation and upload it to a GPU buffer
|
|
|
|
|
pub fn upload_mesh_to_buffer(&mut self, mesh: &RawMesh, color: Option<wgpu::Color>) -> Result<Mesh, String> {
|
|
|
|
|
|
|
|
|
|
pub fn upload_mesh_to_buffer(
|
|
|
|
|
&mut self,
|
|
|
|
|
mesh: &RawMesh,
|
|
|
|
|
color: Option<wgpu::Color>,
|
|
|
|
|
) -> Result<Mesh, String> {
|
|
|
|
|
let index_count = mesh.indices.len() * 3; // TODO bad bad bad bad!
|
|
|
|
|
|
|
|
|
|
let (vertex_buf, index_buf) = RenderState::create_buffer(&self.device, mesh);
|
|
|
|
@ -174,7 +188,11 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// explicitly load from file, and upload to gpu the mesh
|
|
|
|
|
pub fn load_mesh_to_buffer(&mut self, filepath: &str, color: Option<wgpu::Color>) -> Result<Mesh, String> {
|
|
|
|
|
pub fn load_mesh_to_buffer(
|
|
|
|
|
&mut self,
|
|
|
|
|
filepath: &str,
|
|
|
|
|
color: Option<wgpu::Color>,
|
|
|
|
|
) -> Result<Mesh, String> {
|
|
|
|
|
let raw_mesh = load_obj(filepath)?;
|
|
|
|
|
self.upload_mesh_to_buffer(&raw_mesh, color)
|
|
|
|
|
}
|
|
|
|
@ -205,6 +223,105 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a render pass with the ability to add external bindings
|
|
|
|
|
pub fn create_render_pass(
|
|
|
|
|
device: &Device,
|
|
|
|
|
external_binding_layout: &BindGroupLayout,
|
|
|
|
|
vertex_description: &wgpu::VertexBufferLayout,
|
|
|
|
|
uniform_size: u64,
|
|
|
|
|
label: Option<&str>,
|
|
|
|
|
) -> Option<Pass> {
|
|
|
|
|
// The g-buffers camera projection matrix that we stream data to
|
|
|
|
|
let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
|
label: Some(format!("{} uniform-buffer", label.unwrap_or("unnamed")).as_str()),
|
|
|
|
|
size: uniform_size,
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
mapped_at_creation: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Uniform bind group for the view matrix
|
|
|
|
|
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
label: Some(format!("{} bind group layout", label.unwrap_or("unnamed")).as_str()),
|
|
|
|
|
entries: &[wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0, // global
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::Buffer {
|
|
|
|
|
ty: wgpu::BufferBindingType::Uniform,
|
|
|
|
|
min_binding_size: wgpu::BufferSize::new(uniform_size),
|
|
|
|
|
has_dynamic_offset: false,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
}],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
label: Some(format!("{} uniform bind group", label.unwrap_or("unnamed")).as_str()),
|
|
|
|
|
layout: &bind_group_layout,
|
|
|
|
|
entries: &[wgpu::BindGroupEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
resource: wgpu::BindingResource::Buffer {
|
|
|
|
|
buffer: &uniform_buf,
|
|
|
|
|
offset: 0,
|
|
|
|
|
size: wgpu::BufferSize::new(uniform_size),
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let vs_module =
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../shaders/bake.vert.spv"));
|
|
|
|
|
let fs_module =
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../shaders/bake.frag.spv"));
|
|
|
|
|
|
|
|
|
|
// The bind group layouts are accessed by layout(set = X, binding = x)
|
|
|
|
|
// in the shader.
|
|
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: Some(format!("{} pipeline layout", label.unwrap_or("unnamed")).as_str()),
|
|
|
|
|
bind_group_layouts: &[&bind_group_layout, external_binding_layout],
|
|
|
|
|
push_constant_ranges: &[],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some(format!("{} render pipeline", label.unwrap_or("unnamed")).as_str()),
|
|
|
|
|
layout: Some(&pipeline_layout),
|
|
|
|
|
|
|
|
|
|
vertex: VertexState {
|
|
|
|
|
module: &vs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
buffers: &[vertex_description.clone()],
|
|
|
|
|
},
|
|
|
|
|
fragment: Some(FragmentState {
|
|
|
|
|
module: &fs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
targets: &[],
|
|
|
|
|
}),
|
|
|
|
|
primitive: wgpu::PrimitiveState {
|
|
|
|
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
|
|
|
cull_mode: wgpu::CullMode::Back,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
depth_stencil: Some(wgpu::DepthStencilState {
|
|
|
|
|
format: wgpu::TextureFormat::Depth32Float,
|
|
|
|
|
depth_write_enabled: true,
|
|
|
|
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
|
|
|
|
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),
|
|
|
|
|
}),
|
|
|
|
|
multisample: wgpu::MultisampleState::default(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Some(Pass {
|
|
|
|
|
pipeline,
|
|
|
|
|
bind_group,
|
|
|
|
|
uniform_buf,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init(window: &Window, imgui_context: &mut ImguiContext) -> RenderState {
|
|
|
|
|
// Grab the GPU instance, and query its features
|
|
|
|
|
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
|
|
|
@ -292,10 +409,149 @@ impl RenderState {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
There appear to be two passes required for shadows, the shadow pass, and the forward pass
|
|
|
|
|
Need to open this up in renderdoc and see what it's actually doing
|
|
|
|
|
The shadow pass renders from the perspective of each camera and applies that to the shadow
|
|
|
|
|
texture. Due to light FOV and the shadow texture itself, it's rather difficult to have
|
|
|
|
|
hands-off global lighting / point lights
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// This pass is just going to forward the vertex info to the fragments
|
|
|
|
|
// And they are going to render to the gbuffer
|
|
|
|
|
let g_buffer_pass = Self::create_render_pass(
|
|
|
|
|
&device,
|
|
|
|
|
&entity_bind_group_layout,
|
|
|
|
|
&vb_desc,
|
|
|
|
|
mem::size_of::<CameraProjectionView>() as wgpu::BufferAddress,
|
|
|
|
|
Some("g-buffer"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let shadow_pass = Self::create_render_pass(
|
|
|
|
|
&device,
|
|
|
|
|
&entity_bind_group_layout,
|
|
|
|
|
&vb_desc,
|
|
|
|
|
mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress,
|
|
|
|
|
Some("shadow-pass"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let g_buffer_pass = {
|
|
|
|
|
let uniform_size = mem::size_of::<CameraProjectionView>() as wgpu::BufferAddress;
|
|
|
|
|
|
|
|
|
|
// The g-buffers camera projection matrix that we stream data to
|
|
|
|
|
let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
|
label: Some("g-buffers CameraProjectionView uniform buffer"),
|
|
|
|
|
size: uniform_size,
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
mapped_at_creation: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Uniform bind group for the view matrix
|
|
|
|
|
let bind_group_layout =
|
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
label: Some("g-buffer input pass bindgroup layout (cam)"),
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0, // global
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::Buffer {
|
|
|
|
|
ty: wgpu::BufferBindingType::Uniform,
|
|
|
|
|
min_binding_size: wgpu::BufferSize::new(uniform_size),
|
|
|
|
|
has_dynamic_offset: false,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1, // texture2DArray
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Texture {
|
|
|
|
|
multisampled: false,
|
|
|
|
|
sample_type: wgpu::TextureSampleType::Depth,
|
|
|
|
|
view_dimension: wgpu::TextureViewDimension::D2Array,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 2, // samplerShadow
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Sampler {
|
|
|
|
|
filtering: false,
|
|
|
|
|
comparison: true,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//sudo rm /usr/bin/ld
|
|
|
|
|
//sudo ln -s /usr/bin/lld /usr/bin/ld
|
|
|
|
|
//sudo ln -s /usr/bin/x86_64-linux-gnu-ld /usr/bin/ld
|
|
|
|
|
|
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
label: Some("g-buffers uniform bind group"),
|
|
|
|
|
layout: &bind_group_layout,
|
|
|
|
|
entries: &[wgpu::BindGroupEntry {
|
|
|
|
|
binding: 0, // globals
|
|
|
|
|
resource: wgpu::BindingResource::Buffer {
|
|
|
|
|
buffer: &uniform_buf,
|
|
|
|
|
offset: 0,
|
|
|
|
|
size: wgpu::BufferSize::new(uniform_size),
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let vs_module =
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../shaders/bake.vert.spv"));
|
|
|
|
|
let fs_module =
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../shaders/bake.frag.spv"));
|
|
|
|
|
|
|
|
|
|
// The bind group layouts are accessed by layout(set = X, binding = x)
|
|
|
|
|
// in the shader.
|
|
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: Some("g-buffer input pipeline layout"),
|
|
|
|
|
bind_group_layouts: &[&bind_group_layout, &entity_bind_group_layout],
|
|
|
|
|
push_constant_ranges: &[],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some("g-buffer"),
|
|
|
|
|
layout: Some(&pipeline_layout),
|
|
|
|
|
|
|
|
|
|
vertex: VertexState {
|
|
|
|
|
module: &vs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
buffers: &[vb_desc.clone()],
|
|
|
|
|
},
|
|
|
|
|
fragment: Some(FragmentState {
|
|
|
|
|
module: &fs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
targets: &[],
|
|
|
|
|
}),
|
|
|
|
|
primitive: wgpu::PrimitiveState {
|
|
|
|
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
|
|
|
cull_mode: wgpu::CullMode::Back,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
depth_stencil: Some(wgpu::DepthStencilState {
|
|
|
|
|
format: wgpu::TextureFormat::Depth32Float,
|
|
|
|
|
depth_write_enabled: true,
|
|
|
|
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
|
|
|
|
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),
|
|
|
|
|
}),
|
|
|
|
|
multisample: wgpu::MultisampleState::default(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Pass {
|
|
|
|
|
pipeline,
|
|
|
|
|
bind_group,
|
|
|
|
|
uniform_buf,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let shadow_pass = {
|
|
|
|
|
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
|
|
|
|
|
|
|
|
|
@ -346,9 +602,9 @@ impl RenderState {
|
|
|
|
|
|
|
|
|
|
// 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!("../../shaders/bake.vert.spv"));
|
|
|
|
|
let fs_module =
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../resources/bake.frag.spv"));
|
|
|
|
|
device.create_shader_module(&wgpu::include_spirv!("../../shaders/bake.frag.spv"));
|
|
|
|
|
|
|
|
|
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some("shadow"),
|
|
|
|
@ -392,6 +648,19 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Pre init the gbuffers camera projection uniform
|
|
|
|
|
let gbuffer_cam_uniform_size =
|
|
|
|
|
mem::size_of::<CameraProjectionView>() as wgpu::BufferAddress;
|
|
|
|
|
|
|
|
|
|
let gbuffer_cam_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
|
label: Some("g-buffer camera projection uniform buffer"),
|
|
|
|
|
size: gbuffer_cam_uniform_size,
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM
|
|
|
|
|
| wgpu::BufferUsage::COPY_SRC
|
|
|
|
|
| wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
mapped_at_creation: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Pre init the light uniform, with slots enough for MAX_LIGHTS
|
|
|
|
|
let light_uniform_size =
|
|
|
|
|
(Self::MAX_LIGHTS * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
|
|
|
|
@ -437,8 +706,22 @@ impl RenderState {
|
|
|
|
|
50.0,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let g_buffer_camera_projection_matrix = CameraProjectionView {
|
|
|
|
|
proj: *mx_projection.as_ref(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let g_buffer_camera_projection_uniform =
|
|
|
|
|
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("g-buffer camera projection uniform buffer"),
|
|
|
|
|
contents: bytemuck::bytes_of(&g_buffer_camera_projection_matrix),
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let forward_pass = {
|
|
|
|
|
// Create pipeline layout
|
|
|
|
|
let forward_uniform_size =
|
|
|
|
|
mem::size_of::<ForwardUniforms>() as wgpu::BufferAddress;
|
|
|
|
|
|
|
|
|
|
let bind_group_layout =
|
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
entries: &[
|
|
|
|
@ -447,11 +730,7 @@ impl RenderState {
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Buffer {
|
|
|
|
|
ty: wgpu::BufferBindingType::Uniform,
|
|
|
|
|
min_binding_size: wgpu::BufferSize::new(mem::size_of::<
|
|
|
|
|
ForwardUniforms,
|
|
|
|
|
>(
|
|
|
|
|
)
|
|
|
|
|
as _),
|
|
|
|
|
min_binding_size: wgpu::BufferSize::new(forward_uniform_size),
|
|
|
|
|
has_dynamic_offset: false,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
@ -467,7 +746,7 @@ impl RenderState {
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 2,
|
|
|
|
|
binding: 2, // texture2DArray
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Texture {
|
|
|
|
|
multisampled: false,
|
|
|
|
@ -477,7 +756,7 @@ impl RenderState {
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 3,
|
|
|
|
|
binding: 3, // samplerShadow
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Sampler {
|
|
|
|
|
filtering: false,
|
|
|
|
@ -559,10 +838,10 @@ impl RenderState {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create the render pipeline
|
|
|
|
|
let vs_module =
|
|
|
|
|
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"));
|
|
|
|
|
let vs_module = device
|
|
|
|
|
.create_shader_module(&wgpu::include_spirv!("../../shaders/forward.vert.spv"));
|
|
|
|
|
let fs_module = device
|
|
|
|
|
.create_shader_module(&wgpu::include_spirv!("../../shaders/forward.frag.spv"));
|
|
|
|
|
|
|
|
|
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some("main"),
|
|
|
|
@ -600,7 +879,7 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
let forward_depth_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
size: wgpu::Extent3d {
|
|
|
|
|
width: sc_desc.width,
|
|
|
|
|
height: sc_desc.height,
|
|
|
|
@ -611,9 +890,73 @@ impl RenderState {
|
|
|
|
|
dimension: wgpu::TextureDimension::D2,
|
|
|
|
|
format: Self::DEPTH_FORMAT,
|
|
|
|
|
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
|
|
|
|
|
label: Some("Depth Texture"),
|
|
|
|
|
label: Some("Forward Depth Texture"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let g_buffer_depth_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
size: wgpu::Extent3d {
|
|
|
|
|
width: sc_desc.width,
|
|
|
|
|
height: sc_desc.height,
|
|
|
|
|
depth: 5,
|
|
|
|
|
},
|
|
|
|
|
mip_level_count: 1,
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
dimension: wgpu::TextureDimension::D2,
|
|
|
|
|
format: Self::DEPTH_FORMAT,
|
|
|
|
|
usage: wgpu::TextureUsage::RENDER_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
|
|
|
|
|
label: Some("g-buffer depth texture"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// I need one of these for each of the g-buffer elements I'm calculating
|
|
|
|
|
let mut g_buffer_depth_texture_views = (0..4)
|
|
|
|
|
.map(|i| {
|
|
|
|
|
Arc::new(
|
|
|
|
|
g_buffer_depth_texture.create_view(&wgpu::TextureViewDescriptor {
|
|
|
|
|
label: Some("g-buffer depth texture"),
|
|
|
|
|
format: None,
|
|
|
|
|
dimension: Some(wgpu::TextureViewDimension::D2),
|
|
|
|
|
aspect: wgpu::TextureAspect::All,
|
|
|
|
|
base_mip_level: 0,
|
|
|
|
|
level_count: None,
|
|
|
|
|
base_array_layer: i as u32,
|
|
|
|
|
array_layer_count: NonZeroU32::new(1),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
let g_buffer_data_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
size: wgpu::Extent3d {
|
|
|
|
|
width: sc_desc.width,
|
|
|
|
|
height: sc_desc.height,
|
|
|
|
|
depth: 5,
|
|
|
|
|
},
|
|
|
|
|
mip_level_count: 1,
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
dimension: wgpu::TextureDimension::D2,
|
|
|
|
|
format: Self::DEPTH_FORMAT,
|
|
|
|
|
usage: wgpu::TextureUsage::RENDER_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
|
|
|
|
|
label: Some("g-buffer depth texture"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// I need one of these for each of the g-buffer elements I'm calculating
|
|
|
|
|
let mut g_buffer_data_texture_views = (0..4)
|
|
|
|
|
.map(|i| {
|
|
|
|
|
Arc::new(
|
|
|
|
|
g_buffer_depth_texture.create_view(&wgpu::TextureViewDescriptor {
|
|
|
|
|
label: Some("g-buffer depth texture"),
|
|
|
|
|
format: None,
|
|
|
|
|
dimension: Some(wgpu::TextureViewDimension::D2),
|
|
|
|
|
aspect: wgpu::TextureAspect::All,
|
|
|
|
|
base_mip_level: 0,
|
|
|
|
|
level_count: None,
|
|
|
|
|
base_array_layer: i as u32,
|
|
|
|
|
array_layer_count: NonZeroU32::new(1),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
// Imgui renderer
|
|
|
|
|
let renderer_config = ImguiRendererConfig {
|
|
|
|
|
texture_format: sc_desc.format,
|
|
|
|
@ -623,6 +966,15 @@ impl RenderState {
|
|
|
|
|
ImguiRenderer::new(&mut imgui_context.context, &device, &queue, renderer_config);
|
|
|
|
|
|
|
|
|
|
RenderState {
|
|
|
|
|
gbuffer_pass: g_buffer_pass,
|
|
|
|
|
gbuffer_cam_projection_buffer: g_buffer_camera_projection_uniform,
|
|
|
|
|
gbuffer_depth_texture: g_buffer_depth_texture
|
|
|
|
|
.create_view(&wgpu::TextureViewDescriptor::default()),
|
|
|
|
|
gbuffer_depth_views: g_buffer_depth_texture_views,
|
|
|
|
|
gbuffer_target_texture: g_buffer_data_texture
|
|
|
|
|
.create_view(&wgpu::TextureViewDescriptor::default()),
|
|
|
|
|
gbuffer_target_views: g_buffer_data_texture_views,
|
|
|
|
|
|
|
|
|
|
swapchain: swap_chain,
|
|
|
|
|
queue: queue,
|
|
|
|
|
size,
|
|
|
|
@ -630,7 +982,8 @@ impl RenderState {
|
|
|
|
|
lights_are_dirty: true,
|
|
|
|
|
shadow_pass,
|
|
|
|
|
forward_pass,
|
|
|
|
|
forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()),
|
|
|
|
|
forward_depth: forward_depth_texture
|
|
|
|
|
.create_view(&wgpu::TextureViewDescriptor::default()),
|
|
|
|
|
entity_bind_group_layout: entity_bind_group_layout,
|
|
|
|
|
shadow_target_views: shadow_target_views,
|
|
|
|
|
light_uniform_buf,
|
|
|
|
@ -685,7 +1038,6 @@ impl RenderState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|