@ -1,12 +1,15 @@
use crate ::{ EntityUniforms , Pass } ;
use bytemuck ::{ Pod , Zeroable } ;
use bytemuck ::__core ::mem ;
use wgpu ::util ::DeviceExt ;
use std ::rc ::Rc ;
use std ::{ iter , num ::NonZeroU32 , ops ::Range , rc ::Rc } ;
use crate ::OPENGL_TO_WGPU_MATRIX ;
use crate ::light ::LightRaw ;
use crate ::geometry ::{ Vertex , import_mesh , create_plane } ;
#[ repr(C) ]
#[ derive(Clone, Copy) ]
struct ForwardUniforms {
pub struct ForwardUniforms {
proj : [ [ f32 ; 4 ] ; 4 ] ,
num_lights : [ u32 ; 4 ] ,
}
@ -17,7 +20,7 @@ unsafe impl Zeroable for ForwardUniforms {}
#[ repr(C) ]
#[ derive(Clone, Copy) ]
struct EntityUniforms {
pub struct EntityUniforms {
model : [ [ f32 ; 4 ] ; 4 ] ,
color : [ f32 ; 4 ] ,
}
@ -27,17 +30,16 @@ unsafe impl Pod for EntityUniforms {}
unsafe impl Zeroable for EntityUniforms { }
#[ repr(C) ]
struct ShadowUniforms {
pub struct ShadowUniforms {
proj : [ [ f32 ; 4 ] ; 4 ] ,
}
struct Pass {
pub struct Pass {
pipeline : wgpu ::RenderPipeline ,
bind_group : wgpu ::BindGroup ,
uniform_buf : wgpu ::Buffer ,
}
pub struct Renderer {
lights_are_dirty : bool ,
shadow_pass : Pass ,
@ -63,7 +65,7 @@ impl Renderer {
cgmath ::Point3 ::new ( 0 f32 , 0.0 , 0.0 ) ,
cgmath ::Vector3 ::unit_z ( ) ,
) ;
let mx_correction = framework:: OPENGL_TO_WGPU_MATRIX;
let mx_correction = OPENGL_TO_WGPU_MATRIX;
mx_correction * mx_projection * mx_view
}
}
@ -115,7 +117,7 @@ impl Renderer {
} ) ;
}
pub fn init ( & mut self ) -> Renderer {
pub fn init ( device : & wgpu ::Device ) -> Renderer {
// Pre init the light uniform, with slots enough for MAX_LIGHTS
let light_uniform_size =
@ -133,6 +135,7 @@ impl Renderer {
// This seems way way way way easier than what I was doing in tracer
// Though the attr thing is still a macro. Which would cause issues if
// I wanted to get tricky with the 0,1 types
let vertex_size = mem ::size_of ::< Vertex > ( ) ;
let vertex_attr = wgpu ::vertex_attr_array ! [ 0 = > Float4 , 1 = > Float4 ] ;
let vb_desc = wgpu ::VertexBufferDescriptor {
stride : vertex_size as wgpu ::BufferAddress ,
@ -140,6 +143,24 @@ impl Renderer {
attributes : & vertex_attr ,
} ;
// This is also in the runtime which really shouldn't have this
let local_bind_group_layout =
device . create_bind_group_layout ( & wgpu ::BindGroupLayoutDescriptor {
label : None ,
entries : & [ wgpu ::BindGroupLayoutEntry {
binding : 0 ,
visibility : wgpu ::ShaderStage ::VERTEX | wgpu ::ShaderStage ::FRAGMENT ,
count : None ,
ty : wgpu ::BindingType ::UniformBuffer {
dynamic : false ,
min_binding_size : wgpu ::BufferSize ::new (
mem ::size_of ::< EntityUniforms > ( ) as _
) ,
} ,
} ] ,
} ) ;
/*
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
@ -147,7 +168,8 @@ impl Renderer {
let shadow_pass = {
let uniform_size = mem ::size_of ::< ShadowUniforms > ( ) as wgpu ::BufferAddress ;
// Create pipeline layout
// I believe this is just making a_Pos or u_ViewProj available in the vert shader
let bind_group_layout =
device . create_bind_group_layout ( & wgpu ::BindGroupLayoutDescriptor {
label : None ,
@ -161,12 +183,15 @@ impl Renderer {
count : None ,
} ] ,
} ) ;
// Pipeline is similar between passes, but with a different label
let pipeline_layout = device . create_pipeline_layout ( & wgpu ::PipelineLayoutDescriptor {
label : Some ( "shadow" ) ,
bind_group_layouts : & [ & bind_group_layout , & local_bind_group_layout ] ,
push_constant_ranges : & [ ] ,
} ) ;
// Holds the shadow uniforms, which is just a 4 vec of quaternians
let uniform_buf = device . create_buffer ( & wgpu ::BufferDescriptor {
label : None ,
size : uniform_size ,
@ -276,6 +301,7 @@ impl Renderer {
] ,
label : None ,
} ) ;
let pipeline_layout = device . create_pipeline_layout ( & wgpu ::PipelineLayoutDescriptor {
label : Some ( "main" ) ,
bind_group_layouts : & [ & bind_group_layout , & local_bind_group_layout ] ,