From bd2e2557b85776925266d727e5259a02bf450ab8 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Tue, 9 Mar 2021 23:07:07 -0800 Subject: [PATCH] compiling state --- shaders/g_buffer_input.vert | 4 -- src/render/state.rs | 106 ++++++++++++++++++++++++++++-------- src/render/system.rs | 26 +++++---- 3 files changed, 99 insertions(+), 37 deletions(-) diff --git a/shaders/g_buffer_input.vert b/shaders/g_buffer_input.vert index a94d57d..c4c4145 100644 --- a/shaders/g_buffer_input.vert +++ b/shaders/g_buffer_input.vert @@ -6,10 +6,6 @@ layout(set = 0, binding = 0) uniform Globals { mat4 u_ViewProj; }; -layout(set = 0, binding = 1) uniform Globals { - mat4 u_ViewProj; -}; - layout(set = 1, binding = 0) uniform Entity { mat4 u_World; vec4 u_Color; diff --git a/src/render/state.rs b/src/render/state.rs index ed81e3d..b88e45a 100644 --- a/src/render/state.rs +++ b/src/render/state.rs @@ -65,8 +65,13 @@ 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: wgpu::TextureView, + pub(in crate::render) gbuffer_target_views: Vec>, + // this is for the set=1 entity uniforms entity_bind_group_layout: BindGroupLayout, pub(in crate::render) light_uniform_buf: wgpu::Buffer, @@ -310,8 +315,17 @@ impl RenderState { // 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 = { + let uniform_size = mem::size_of::() 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, + }); + // Pretty sure this is the camera projction let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -328,24 +342,8 @@ impl RenderState { }], }); - // Pipeline is similar between passes, but with a different label - 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: &[], - }); - - // Holds the shadow uniforms, which is just a 4 vec of quaternians - let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("shadow pass shadow uniform buffer"), - size: uniform_size, - usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, - mapped_at_creation: false, - }); - - // Create bind group let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Shadow uniform bind group"), + label: Some("g-buffers uniform bind group"), layout: &bind_group_layout, entries: &[ wgpu::BindGroupEntry { @@ -360,12 +358,19 @@ impl RenderState { }); - // Create the render pipeline 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("shadow"), layout: Some(&pipeline_layout), @@ -514,6 +519,19 @@ impl RenderState { } }; + // Pre init the gbuffers camera projection uniform + let gbuffer_cam_uniform_size = + mem::size_of::() 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::()) as wgpu::BufferAddress; @@ -559,6 +577,16 @@ 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 bind_group_layout = @@ -722,7 +750,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, @@ -733,9 +761,38 @@ 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: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: Self::DEPTH_FORMAT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, + label: Some("g-buffer depth texture"), }); + let mut g_buffer_depth_texture_views = (0..2) + .map(|i| { + Arc::new(shadow_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::>(); + // Imgui renderer let renderer_config = ImguiRendererConfig { texture_format: sc_desc.format, @@ -745,6 +802,12 @@ 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: g_buffer_depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), + gbuffer_target_views: g_buffer_depth_texture_views, + swapchain: swap_chain, queue: queue, size, @@ -752,8 +815,7 @@ impl RenderState { lights_are_dirty: true, shadow_pass, forward_pass, - forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), - gbuffer_pass: g_buffer_pass, + 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, diff --git a/src/render/system.rs b/src/render/system.rs index 4a1fd4a..555517c 100644 --- a/src/render/system.rs +++ b/src/render/system.rs @@ -249,15 +249,16 @@ pub fn render_test( // Render the g buffer push_debug_group_checked("g-buffer stuff", &mut encoder); { + // insert_debug_marker_checked("render entities", &mut encoder); - - insert_debug_marker_checked("render entities", &mut encoder); - + // The render pass for the g buffer.. Does it need any attachments + // the shadow pass uses a target view as the input, which are just views on + // top of the shadow texture. So the attachment is a RW buffer which clears on load let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("render pass"), color_attachments: &[], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { - attachment: &light.target_view, + attachment: &renderer.gbuffer_target_views.get(0).unwrap(), depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), store: true, @@ -276,13 +277,11 @@ pub fn render_test( pass.draw_indexed(0..mesh.index_count as u32, 0, 0..1); } } - pop_debug_group_checked(&mut encoder); + // Render the shadow textures push_debug_group_checked("shadow passes", &mut encoder); - let mut query = <(&mut DirectionalLight, &mut Position)>::query(); - for (i, (light, pos)) in query.iter_mut(world).enumerate() { insert_debug_marker_checked( &format!("shadow pass {} (light at position {:?})", i, pos), @@ -301,6 +300,7 @@ pub fn render_test( insert_debug_marker_checked("render entities", &mut encoder); + // let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("render pass"), color_attachments: &[], @@ -324,11 +324,10 @@ pub fn render_test( pass.draw_indexed(0..mesh.index_count as u32, 0, 0..1); } } - pop_debug_group_checked(&mut encoder); - // forward pass - push_debug_group_checked("forward rendering pass", &mut encoder); + // And then render the viewport + push_debug_group_checked("forward rendering pass", &mut encoder); { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("forward render pass"), @@ -370,9 +369,10 @@ pub fn render_test( } pop_debug_group_checked(&mut encoder); - pop_debug_group_checked(&mut encoder); + // Run the Imgui render + push_debug_group_checked("imgui rendering pass", &mut encoder); { let mut imgui_context = &mut imgui_context.lock().unwrap().context; let mut imgui_platform = &mut imgui_platform.lock().unwrap().platform; @@ -407,6 +407,10 @@ pub fn render_test( .render(draw_data, &renderer.queue, &renderer.device, &mut rpass) .expect("Rendering failed"); } + pop_debug_group_checked(&mut encoder); + + // the startfunction debug group + pop_debug_group_checked(&mut encoder); renderer.queue.submit(iter::once(encoder.finish())); }