diff --git a/native/shared/src/renderer/card_capture_pass.rs b/native/shared/src/renderer/card_capture_pass.rs new file mode 100644 index 00000000..ea80928a --- /dev/null +++ b/native/shared/src/renderer/card_capture_pass.rs @@ -0,0 +1,126 @@ +//! The render pass and atlas copies of one frame's mesh-card captures. + +use super::{Renderer, CARD_SLOTS_PER_ROW, CARD_SLOT_SIZE}; + +/// One axis of one mesh: what to draw, the scratch-grid cell it draws into +/// and the atlas slot that cell is copied to. +pub(super) struct CardCaptureDraw { + pub uniform_offset: u32, + pub texture_bg: wgpu::BindGroup, + pub vertex_buffer: wgpu::Buffer, + pub index_buffer: wgpu::Buffer, + pub index_count: u32, + pub cell_x: u32, + pub cell_y: u32, + pub slot: u32, +} + +impl Renderer { + pub(super) fn record_card_capture_draws( + &self, + encoder: &mut wgpu::CommandEncoder, + uniform_bg: &wgpu::BindGroup, + draws: &[CardCaptureDraw], + ) { + if draws.is_empty() { + return; + } + { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("card_capture_pass"), + color_attachments: &[ + Some(wgpu::RenderPassColorAttachment { + view: &self.card_capture_albedo_scratch_view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + store: wgpu::StoreOp::Store, + }, + }), + Some(wgpu::RenderPassColorAttachment { + view: &self.card_capture_emissive_scratch_view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + store: wgpu::StoreOp::Store, + }, + }), + ], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &self.card_capture_depth_scratch_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Discard, + }), + stencil_ops: None, + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&self.card_capture_pipeline); + for draw in draws { + // The viewport maps the capture's clip space onto its cell; + // the scissor keeps geometry outside the card bounds from + // spilling into a neighbouring cell. + pass.set_viewport( + draw.cell_x as f32, + draw.cell_y as f32, + CARD_SLOT_SIZE as f32, + CARD_SLOT_SIZE as f32, + 0.0, + 1.0, + ); + pass.set_scissor_rect(draw.cell_x, draw.cell_y, CARD_SLOT_SIZE, CARD_SLOT_SIZE); + pass.set_bind_group(0, uniform_bg, &[draw.uniform_offset]); + pass.set_bind_group(1, &draw.texture_bg, &[]); + pass.set_vertex_buffer(0, draw.vertex_buffer.slice(..)); + pass.set_index_buffer(draw.index_buffer.slice(..), wgpu::IndexFormat::Uint32); + pass.draw_indexed(0..draw.index_count, 0, 0..1); + } + } + + let copy_extent = wgpu::Extent3d { + width: CARD_SLOT_SIZE, + height: CARD_SLOT_SIZE, + depth_or_array_layers: 1, + }; + for draw in draws { + let cell_origin = wgpu::Origin3d { + x: draw.cell_x, + y: draw.cell_y, + z: 0, + }; + let atlas_origin = wgpu::Origin3d { + x: (draw.slot % CARD_SLOTS_PER_ROW) * CARD_SLOT_SIZE, + y: (draw.slot / CARD_SLOTS_PER_ROW) * CARD_SLOT_SIZE, + z: 0, + }; + for (scratch, atlas) in [ + (&self.card_capture_albedo_scratch, &self.mesh_card_atlas), + ( + &self.card_capture_emissive_scratch, + &self.mesh_card_emissive_tex, + ), + ] { + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { + texture: scratch, + mip_level: 0, + origin: cell_origin, + aspect: wgpu::TextureAspect::All, + }, + wgpu::TexelCopyTextureInfo { + texture: atlas, + mip_level: 0, + origin: atlas_origin, + aspect: wgpu::TextureAspect::All, + }, + copy_extent, + ); + } + } + } +} diff --git a/native/shared/src/renderer/mod.rs b/native/shared/src/renderer/mod.rs index 2df73928..0d08157b 100644 --- a/native/shared/src/renderer/mod.rs +++ b/native/shared/src/renderer/mod.rs @@ -60,6 +60,8 @@ pub fn build_cooked_color_mip_chain( (bytes, mip_count) } mod capability_api; +mod card_capture_pass; +use card_capture_pass::CardCaptureDraw; mod draw2d; mod env_prefilter; mod final_pass; @@ -6737,8 +6739,8 @@ impl Renderer { let texture = device.create_texture(&wgpu::TextureDescriptor { label: Some(label), size: wgpu::Extent3d { - width: CARD_SLOT_SIZE, - height: CARD_SLOT_SIZE, + width: CARD_SLOT_SIZE * CARD_AXES_PER_MESH, + height: CARD_SLOT_SIZE * CARD_CAPTURE_MAX_PER_FRAME as u32, depth_or_array_layers: 1, }, mip_level_count: 1, @@ -6758,8 +6760,8 @@ impl Renderer { let card_capture_depth_scratch = device.create_texture(&wgpu::TextureDescriptor { label: Some("card_capture_depth_scratch"), size: wgpu::Extent3d { - width: CARD_SLOT_SIZE, - height: CARD_SLOT_SIZE, + width: CARD_SLOT_SIZE * CARD_AXES_PER_MESH, + height: CARD_SLOT_SIZE * CARD_CAPTURE_MAX_PER_FRAME as u32, depth_or_array_layers: 1, }, mip_level_count: 1, @@ -9428,6 +9430,14 @@ impl Renderer { }], }); let mut capture_index = 0_u32; + // Every capture of the frame draws into its own cell of the scratch + // grid (one row per mesh, one column per axis) inside a single render + // pass, and the cells are copied to their atlas slots afterwards. + // One pass and one run of copies per frame replaces a render pass + // plus a blit per axis: 120 of each at the per-frame budget, which + // on Metal held about 400 MB of encoder memory while a scene loaded. + let mut capture_draws: Vec = + Vec::with_capacity(pending.len() * CARD_AXES_PER_MESH as usize); for handle in pending { let ( @@ -9538,91 +9548,16 @@ impl Renderer { bytemuck::bytes_of(¶ms), ); - let slot_x = slot % CARD_SLOTS_PER_ROW; - let slot_y = slot / CARD_SLOTS_PER_ROW; - - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("card_capture_pass"), - color_attachments: &[ - Some(wgpu::RenderPassColorAttachment { - view: &self.card_capture_albedo_scratch_view, - resolve_target: None, - depth_slice: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), - store: wgpu::StoreOp::Store, - }, - }), - Some(wgpu::RenderPassColorAttachment { - view: &self.card_capture_emissive_scratch_view, - resolve_target: None, - depth_slice: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), - store: wgpu::StoreOp::Store, - }, - }), - ], - depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { - view: &self.card_capture_depth_scratch_view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Clear(1.0), - store: wgpu::StoreOp::Discard, - }), - stencil_ops: None, - }), - timestamp_writes: None, - occlusion_query_set: None, - multiview_mask: None, + capture_draws.push(CardCaptureDraw { + uniform_offset: uniform_offset as u32, + texture_bg: texture_bg.clone(), + vertex_buffer: vb_ptr.clone(), + index_buffer: ib_ptr.clone(), + index_count, + cell_x: axis * CARD_SLOT_SIZE, + cell_y: capture_index * CARD_SLOT_SIZE, + slot, }); - pass.set_pipeline(&self.card_capture_pipeline); - pass.set_bind_group(0, &capture_uniform_bg, &[uniform_offset as u32]); - pass.set_bind_group(1, &texture_bg, &[]); - pass.set_vertex_buffer(0, vb_ptr.slice(..)); - pass.set_index_buffer(ib_ptr.slice(..), wgpu::IndexFormat::Uint32); - pass.draw_indexed(0..index_count, 0, 0..1); - drop(pass); - - let atlas_origin = wgpu::Origin3d { - x: slot_x * CARD_SLOT_SIZE, - y: slot_y * CARD_SLOT_SIZE, - z: 0, - }; - let copy_extent = wgpu::Extent3d { - width: CARD_SLOT_SIZE, - height: CARD_SLOT_SIZE, - depth_or_array_layers: 1, - }; - encoder.copy_texture_to_texture( - wgpu::TexelCopyTextureInfo { - texture: &self.card_capture_albedo_scratch, - mip_level: 0, - origin: wgpu::Origin3d::ZERO, - aspect: wgpu::TextureAspect::All, - }, - wgpu::TexelCopyTextureInfo { - texture: &self.mesh_card_atlas, - mip_level: 0, - origin: atlas_origin, - aspect: wgpu::TextureAspect::All, - }, - copy_extent, - ); - encoder.copy_texture_to_texture( - wgpu::TexelCopyTextureInfo { - texture: &self.card_capture_emissive_scratch, - mip_level: 0, - origin: wgpu::Origin3d::ZERO, - aspect: wgpu::TextureAspect::All, - }, - wgpu::TexelCopyTextureInfo { - texture: &self.mesh_card_emissive_tex, - mip_level: 0, - origin: atlas_origin, - aspect: wgpu::TextureAspect::All, - }, - copy_extent, - ); // Stash the world-space normal for the card-lighting // pass. Object-space face normals are axis unit vectors @@ -9657,6 +9592,7 @@ impl Renderer { } capture_index += 1; } + self.record_card_capture_draws(encoder, &capture_uniform_bg, &capture_draws); // Upload the new slot-meta entries in one contiguous batch. // Sorting by slot index lets us coalesce adjacent writes, but