diff --git a/crates/bevy_tape_drive_solari/src/scene/binder/bind_group.rs b/crates/bevy_tape_drive_solari/src/scene/binder/bind_group.rs index 2230dea..bedce53 100644 --- a/crates/bevy_tape_drive_solari/src/scene/binder/bind_group.rs +++ b/crates/bevy_tape_drive_solari/src/scene/binder/bind_group.rs @@ -104,6 +104,7 @@ impl RaytracingSceneBindings { &mut self.instances.aabb_buffers.dirty, &mut self.instances.tape_metadata_buffers.dirty, &mut self.instances.spatial_textures.dirty, + &mut self.instances.tiletrees.dirty, &mut self.assets.textures.dirty, ] { invalid |= core::mem::replace(dirty, false); @@ -173,6 +174,19 @@ impl RaytracingSceneBindings { spatial_textures.push(fallback_texture.d3.texture_view.deref()); } + let mut tiletrees: Vec<_> = self + .instances + .tiletrees + .iter() + .map(|texture| match texture { + Some(view) => view.deref(), + None => fallback_texture.d3.texture_view.deref(), + }) + .collect(); + if tiletrees.is_empty() { + tiletrees.push(fallback_texture.d3.texture_view.deref()); + } + let (mut textures, mut samplers): (Vec<_>, Vec<_>) = self .assets .textures @@ -215,6 +229,8 @@ impl RaytracingSceneBindings { tape_metadata_buffers.as_slice(), spatial_textures.as_slice(), &self.spatial_texture_sampler, + tiletrees.as_slice(), + &self.tiletree_sampler, textures.as_slice(), samplers.as_slice(), self.assets.materials.binding().unwrap(), diff --git a/crates/bevy_tape_drive_solari/src/scene/binder/instances.rs b/crates/bevy_tape_drive_solari/src/scene/binder/instances.rs index 94946cb..e62b62c 100644 --- a/crates/bevy_tape_drive_solari/src/scene/binder/instances.rs +++ b/crates/bevy_tape_drive_solari/src/scene/binder/instances.rs @@ -35,6 +35,7 @@ use super::{ pub const MAX_MESH_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap(); pub const MAX_TAPE_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap(); pub const MAX_SPATIAL_TEXTURE_COUNT: NonZeroU32 = NonZeroU32::new(5000).unwrap(); +pub const MAX_TILETREE_COUNT: NonZeroU32 = NonZeroU32::new(5000).unwrap(); #[derive(Clone, Copy, Default, PartialEq, Pod, Zeroable)] #[repr(C)] @@ -51,8 +52,8 @@ pub struct GpuInstanceGeometryIds { pub struct GpuInstanceTapeIds { aabb_buffer_id: u32, aabb_buffer_offset: u32, - aabb_count: u32, spatial_texture_id: u32, + tiletree_id: u32, metadata_buffer_id: u32, metadata_buffer_offset: u32, } @@ -103,7 +104,7 @@ pub enum BlasKey { } type MeshBuffers = (BufferId, BufferId); -type TapeBuffers = (BufferId, BufferId, TextureId); +type TapeBuffers = (BufferId, BufferId, TextureId, TextureId); pub enum Buffers { Mesh(MeshBuffers), @@ -264,6 +265,7 @@ pub struct InstanceState { pub aabb_buffers: RetainedBindingArray, pub tape_metadata_buffers: RetainedBindingArray, pub spatial_textures: RetainedBindingArray, + pub tiletrees: RetainedBindingArray, pub transforms: AtomicSparseBufferVec, pub previous_frame_transforms: AtomicSparseBufferVec, pub geometry_ids: AtomicSparseBufferVec, @@ -290,6 +292,7 @@ impl InstanceState { aabb_buffers: RetainedBindingArray::new(), tape_metadata_buffers: RetainedBindingArray::new(), spatial_textures: RetainedBindingArray::new(), + tiletrees: RetainedBindingArray::new(), transforms: storage_buffer("solari_transforms"), previous_frame_transforms: storage_buffer("solari_previous_frame_transforms"), geometry_ids: storage_buffer("solari_geometry_ids"), @@ -404,11 +407,13 @@ impl InstanceState { self.vertex_buffers.release(&vertex_key); self.index_buffers.release(&index_key); } - for (aabb_key, metadata_key, spatial_key) in core::mem::take(&mut self.retired_tape_buffers) + for (aabb_key, metadata_key, spatial_key, tiletree_key) in + core::mem::take(&mut self.retired_tape_buffers) { self.aabb_buffers.release(&aabb_key); self.tape_metadata_buffers.release(&metadata_key); self.spatial_textures.release(&spatial_key); + self.tiletrees.release(&tiletree_key); } } @@ -704,12 +709,14 @@ impl InstanceState { Some(aabb_slice), Some(metadata_slice), Some(spatial_texture), + Some(tiletree), Some(material_slot), Some(blas_address), ) = ( inputs.tape_allocator.tape_aabb_slice(&instance.tape), inputs.tape_allocator.tape_metadata_slice(&instance.tape), inputs.tape_allocator.tape_spatial_texture(&instance.tape), + inputs.tape_allocator.tape_tiletree(&instance.tape), material_slot, blas_address, ) @@ -760,6 +767,18 @@ impl InstanceState { return false; } + let tiletree_key = tiletree.0.id(); + let tiletree_capacity = MAX_TILETREE_COUNT.get(); + if !self.tiletrees.has_room(&tiletree_key, spatial_capacity) { + once!(warn!( + "Solari scene needs more than {} tiletrees. Instances past that limit will not \ + be rendered.", + MAX_TILETREE_COUNT.get() + )); + self.deactivate_tape_instance(lights, entity, instance); + return false; + } + let previous_buffers = instance.buffers.take(); let aabb_buffer_id = self .aabb_buffers @@ -776,18 +795,26 @@ impl InstanceState { .acquire(spatial_texture_key, spatial_capacity, || { spatial_texture.1.clone() }) - .expect("spatial vslab binding array had room but handed out no slot"); - instance.buffers = Some((aabb_buffer_key, metadata_buffer_key, spatial_texture_key)); + .expect("spatial texture binding array had room but handed out no slot"); + let tiletree_id = self + .tiletrees + .acquire(tiletree_key, tiletree_capacity, || tiletree.1.clone()) + .expect("tiletree binding array had room but handed out no slot"); + instance.buffers = Some(( + aabb_buffer_key, + metadata_buffer_key, + spatial_texture_key, + tiletree_key, + )); self.release_tape_buffers(previous_buffers); - let aabb_count = (aabb_slice.range.len()) as u32; self.tape_ids.grow_and_set( slot, GpuInstanceTapeIds { aabb_buffer_id, aabb_buffer_offset: aabb_slice.range.start, - aabb_count, spatial_texture_id, + tiletree_id, metadata_buffer_id, metadata_buffer_offset: metadata_slice.range.start, }, diff --git a/crates/bevy_tape_drive_solari/src/scene/binder/mod.rs b/crates/bevy_tape_drive_solari/src/scene/binder/mod.rs index abef367..9ca6012 100644 --- a/crates/bevy_tape_drive_solari/src/scene/binder/mod.rs +++ b/crates/bevy_tape_drive_solari/src/scene/binder/mod.rs @@ -49,7 +49,7 @@ use super::{ }, }; use crate::scene::{ - binder::instances::{MAX_SPATIAL_TEXTURE_COUNT, MAX_TAPE_SLAB_COUNT}, + binder::instances::{MAX_SPATIAL_TEXTURE_COUNT, MAX_TAPE_SLAB_COUNT, MAX_TILETREE_COUNT}, extract::{ ExtractedRaytracingDirectionalLight, ExtractedRaytracingPointLight, ExtractedRaytracingRectLight, ExtractedRaytracingSpotLight, @@ -78,6 +78,7 @@ pub struct RaytracingSceneBindings { tlas: TlasState, bind_groups: BindGroupCacheState, spatial_texture_sampler: Sampler, + tiletree_sampler: Sampler, } impl RaytracingSceneBindings { @@ -105,6 +106,9 @@ impl FromWorld for RaytracingSceneBindings { texture_3d(TextureSampleType::Float { filterable: true }) .count(MAX_SPATIAL_TEXTURE_COUNT), sampler(SamplerBindingType::Filtering), + texture_3d(TextureSampleType::Float { filterable: true }) + .count(MAX_TILETREE_COUNT), + sampler(SamplerBindingType::Filtering), texture_2d(TextureSampleType::Float { filterable: true }) .count(MAX_TEXTURE_COUNT), sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT), @@ -155,6 +159,17 @@ impl FromWorld for RaytracingSceneBindings { ..Default::default() }); + let tiletree_sampler = render_device.create_sampler(&SamplerDescriptor { + label: Some("tape_drive_tiletree_sampler"), + address_mode_u: AddressMode::ClampToEdge, + address_mode_v: AddressMode::ClampToEdge, + address_mode_w: AddressMode::ClampToEdge, + mag_filter: FilterMode::Linear, + min_filter: FilterMode::Linear, + mipmap_filter: MipmapFilterMode::Linear, + ..Default::default() + }); + Self { bind_group: None, bind_group_layout, @@ -166,6 +181,7 @@ impl FromWorld for RaytracingSceneBindings { tlas: TlasState::new(render_device), bind_groups: BindGroupCacheState::new(render_device), spatial_texture_sampler, + tiletree_sampler, } } } diff --git a/crates/bevy_tape_drive_solari/src/scene/bindings.wesl b/crates/bevy_tape_drive_solari/src/scene/bindings.wesl index dafba07..feeb817 100644 --- a/crates/bevy_tape_drive_solari/src/scene/bindings.wesl +++ b/crates/bevy_tape_drive_solari/src/scene/bindings.wesl @@ -19,8 +19,8 @@ struct InstanceGeometryIds { struct InstanceTapeIds { aabb_buffer_id: u32, aabb_buffer_offset: u32, - aabb_count: u32, spatial_texture_id: u32, + tiletree_id: u32, metadata_buffer_id: u32, metadata_buffer_offset: u32, } @@ -149,26 +149,28 @@ const LIGHT_NOT_PRESENT_THIS_FRAME = 0xFFFFFFFFu; @group(0) @binding(2) var aabb_buffers: binding_array; @group(0) @binding(3) var tape_metadata_buffers: binding_array; -@group(0) @binding(6) var textures: binding_array>; -@group(0) @binding(7) var samplers: binding_array; -@group(0) @binding(8) var materials: array; -@group(0) @binding(9) var tlas: acceleration_structure; -@group(0) @binding(10) var previous_frame_tlas: acceleration_structure; -@group(0) @binding(11) var transforms: array>; -@group(0) @binding(12) var previous_frame_transforms: array>; -@group(0) @binding(13) var geometry_ids: array; -@group(0) @binding(14) var tape_ids: array; -@group(0) @binding(15) var material_ids: array; -@group(0) @binding(16) var brdf_dfg_lut: texture_2d; -@group(0) @binding(17) var brdf_dfg_lut_sampler: sampler; -@group(0) @binding(18) var light_sources: array; -@group(0) @binding(19) var directional_lights: array; -@group(0) @binding(20) var point_lights: array; -@group(0) @binding(21) var rect_lights: array; -@group(0) @binding(22) var environment_map: texture_cube; -@group(0) @binding(23) var environment_map_sampler: sampler; -@group(0) @binding(24) var environment_map_light: EnvironmentMapLight; -@group(0) @binding(25) var previous_frame_light_id_translations: array; +@group(0) @binding(6) var tiletrees: binding_array>; +@group(0) @binding(7) var tiletree_sampler: sampler; +@group(0) @binding(8) var textures: binding_array>; +@group(0) @binding(9) var samplers: binding_array; +@group(0) @binding(10) var materials: array; +@group(0) @binding(11) var tlas: acceleration_structure; +@group(0) @binding(12) var previous_frame_tlas: acceleration_structure; +@group(0) @binding(13) var transforms: array>; +@group(0) @binding(14) var previous_frame_transforms: array>; +@group(0) @binding(15) var geometry_ids: array; +@group(0) @binding(16) var tape_ids: array; +@group(0) @binding(17) var material_ids: array; +@group(0) @binding(18) var brdf_dfg_lut: texture_2d; +@group(0) @binding(19) var brdf_dfg_lut_sampler: sampler; +@group(0) @binding(20) var light_sources: array; +@group(0) @binding(21) var directional_lights: array; +@group(0) @binding(22) var point_lights: array; +@group(0) @binding(23) var rect_lights: array; +@group(0) @binding(24) var environment_map: texture_cube; +@group(0) @binding(25) var environment_map_sampler: sampler; +@group(0) @binding(26) var environment_map_light: EnvironmentMapLight; +@group(0) @binding(27) var previous_frame_light_id_translations: array; const RAY_T_MIN = 0.001f; const RAY_T_MAX = 100000.0f; diff --git a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs index d733ce2..2379e40 100644 --- a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs +++ b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs @@ -256,6 +256,7 @@ pub fn compile_raytracing_blas( const FLAG_FILL_ENABLE_GEN: u32 = 1 << 0; const FLAG_FILL_SPATIAL_TEXTURE: u32 = 1 << 1; + const FLAG_FILL_TILETREE: u32 = 1 << 1; let tape_info = tapes .iter() @@ -269,6 +270,7 @@ pub fn compile_raytracing_blas( let aabbs_slice = tape_allocator.tape_aabb_slice(&asset_id).unwrap(); let metadata_slice = tape_allocator.tape_metadata_slice(&asset_id).unwrap(); let spatial_texture = tape_allocator.tape_spatial_texture(&asset_id).unwrap(); + let tiletree = tape_allocator.tape_tiletree(&asset_id).unwrap(); let blas_size = BlasAABBGeometrySizeDescriptor { primitive_count: aabbs_slice.range.len() as u32, @@ -281,6 +283,7 @@ pub fn compile_raytracing_blas( aabbs_slice, metadata_slice, spatial_texture, + tiletree, blas_size, ) }) @@ -291,7 +294,7 @@ pub fn compile_raytracing_blas( let build_entries = tape_info .iter() .filter_map( - |(key, tape, aabbs_slice, metadata_slice, spatial_texture, blas_size)| { + |(key, tape, aabbs_slice, metadata_slice, spatial_texture, tiletree, blas_size)| { let pipeline = pipeline_cache.get_compute_pipeline(tape.enable_gen_pipeline)?; let bind_group = render_device.create_bind_group( @@ -301,6 +304,7 @@ pub fn compile_raytracing_blas( aabbs_slice.buffer.as_entire_buffer_binding(), metadata_slice.buffer.as_entire_buffer_binding(), &spatial_texture.1, + &tiletree.1, )), ); @@ -314,16 +318,16 @@ pub fn compile_raytracing_blas( compute_pass.set_bind_group(0, &bind_group, &[]); compute_pass.set_immediates(0, bytemuck::bytes_of(&pc)); compute_pass.dispatch_workgroups( - tape.subdivision / 8, - tape.subdivision / 8, - tape.subdivision / 8, + tape.subdivision / 4, + tape.subdivision / 4, + tape.subdivision / 4, ); pc.flags = FLAG_FILL_SPATIAL_TEXTURE; compute_pass.set_immediates(0, bytemuck::bytes_of(&pc)); compute_pass.dispatch_workgroups( - tape.subdivision, - tape.subdivision, - tape.subdivision, + tape.subdivision * 2, + tape.subdivision * 2, + tape.subdivision * 2, ); updated_blases.push(key.tape); diff --git a/crates/bevy_tape_load/src/render/allocator.rs b/crates/bevy_tape_load/src/render/allocator.rs index 1074366..779707c 100644 --- a/crates/bevy_tape_load/src/render/allocator.rs +++ b/crates/bevy_tape_load/src/render/allocator.rs @@ -49,6 +49,7 @@ pub struct TapeAllocator { #[deref] slab_allocator: SlabAllocator, spatial_texture_allocator: HashMap, (Texture, TextureView)>, + tiletree_allocator: HashMap, (Texture, TextureView)>, } /// Tunable parameters that customize the behavior of the allocator. @@ -201,6 +202,7 @@ impl FromWorld for TapeAllocator { Self { slab_allocator, spatial_texture_allocator: HashMap::new(), + tiletree_allocator: HashMap::new(), } } } @@ -282,6 +284,13 @@ impl TapeAllocator { self.spatial_texture_allocator.get(tape_id) } + /// Returns the spatial texture for the tape with the given ID. + /// + /// If the tape wasn't allocated, returns None. + pub fn tape_tiletree(&self, tape_id: &AssetId) -> Option<&(Texture, TextureView)> { + self.tiletree_allocator.get(tape_id) + } + /// Returns the IDs of the vertex buffer and index buffer respectively for /// the tape with the given ID. /// @@ -376,14 +385,15 @@ impl TapeAllocator { tape_allocator_settings, ); + // Allocate spatial texture. let texture = render_device.create_texture(&TextureDescriptor { label: Some(&format!("Tape {} spatial texture", tape_id)), - format: TextureFormat::R32Float, + format: TextureFormat::R16Float, dimension: TextureDimension::D3, usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING, mip_level_count: 1, sample_count: 1, - view_formats: &[TextureFormat::R32Float], + view_formats: &[TextureFormat::R16Float], size: Extent3d { width: tape.subdivision * 8, height: tape.subdivision * 8, @@ -394,10 +404,31 @@ impl TapeAllocator { label: Some(&format!("Tape {} spatial texture view", tape_id)), ..Default::default() }); - - // Allocate spatial texture. self.spatial_texture_allocator .insert(*tape_id, (texture, texture_view)); + + // Allocate worst-case tiletree + let texture = render_device.create_texture(&TextureDescriptor { + label: Some(&format!("Tape {} tiletree", tape_id)), + format: TextureFormat::Rgb10a2Unorm, + dimension: TextureDimension::D3, + usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING, + mip_level_count: 1, + sample_count: 1, + view_formats: &[TextureFormat::Rgb10a2Unorm], + size: Extent3d { + width: tape.subdivision * 128, + height: tape.subdivision * 128, + depth_or_array_layers: tape.subdivision * 6, + }, + }); + let texture_view = texture.create_view(&TextureViewDescriptor { + label: Some(&format!("Tape {} tiletree view", tape_id)), + ..Default::default() + }); + + self.tiletree_allocator + .insert(*tape_id, (texture, texture_view)); } // Perform growth. @@ -490,6 +521,7 @@ impl TapeAllocator { deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Metadata)); deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Aabb)); self.spatial_texture_allocator.remove(tape_id); + self.tiletree_allocator.remove(tape_id); } deallocation_stage.commit(); diff --git a/crates/bevy_tape_load/src/render/enable_gen.wesl b/crates/bevy_tape_load/src/render/enable_gen.wesl index 6530494..105a4ff 100644 --- a/crates/bevy_tape_load/src/render/enable_gen.wesl +++ b/crates/bevy_tape_load/src/render/enable_gen.wesl @@ -20,13 +20,16 @@ struct PushConstants { const FLAG_FILL_ENABLE_GEN = 1 << 0; const FLAG_FILL_SPATIAL_TEXTURE = 1 << 1; +const FLAG_FILL_TILETREE = 1 << 2; @group(0) @binding(0) var aabb_buffer: AabbBuffer; @group(0) @binding(1) var tape_metadata_buffer: TapeMetadataBuffer; @group(0) @binding(2) -var spatial_texture: texture_storage_3d; +var spatial_texture: texture_storage_3d; +@group(0) @binding(3) +var tiletree: texture_storage_3d; var push_constants: PushConstants; @@ -34,7 +37,7 @@ struct ComputeInput { @builtin(global_invocation_id) global_invocation_id: vec3u } -@compute @workgroup_size(8, 8, 8) +@compute @workgroup_size(4, 4, 4) fn main(compute_input: ComputeInput) { var metadata = tape_metadata_buffer.metadata[push_constants.metadata_buffer_offset]; @@ -78,4 +81,8 @@ fn main(compute_input: ComputeInput) { textureStore(spatial_texture, compute_input.global_invocation_id, vec4(point, 0., 0., 0.)); } + + if (push_constants.flags & FLAG_FILL_TILETREE) > 0 { + + } } diff --git a/crates/bevy_tape_load/src/render/mod.rs b/crates/bevy_tape_load/src/render/mod.rs index 6cd4a81..97a2295 100644 --- a/crates/bevy_tape_load/src/render/mod.rs +++ b/crates/bevy_tape_load/src/render/mod.rs @@ -102,7 +102,11 @@ impl Plugin for TapeRenderAssetPlugin { storage_buffer::(false), storage_buffer_read_only::(false), texture_storage_3d( - TextureFormat::R32Float, + TextureFormat::R16Float, + StorageTextureAccess::WriteOnly, + ), + texture_storage_3d( + TextureFormat::Rgb10a2Unorm, StorageTextureAccess::WriteOnly, ), ),