tiletrees?

This commit is contained in:
2026-11-02 21:09:25 +00:00
parent 78df66b46c
commit ec7c5623de
8 changed files with 151 additions and 43 deletions
@@ -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(),
@@ -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<BufferId, Buffer>,
pub tape_metadata_buffers: RetainedBindingArray<BufferId, Buffer>,
pub spatial_textures: RetainedBindingArray<TextureId, TextureView>,
pub tiletrees: RetainedBindingArray<TextureId, TextureView>,
pub transforms: AtomicSparseBufferVec<GpuTransform>,
pub previous_frame_transforms: AtomicSparseBufferVec<GpuTransform>,
pub geometry_ids: AtomicSparseBufferVec<GpuInstanceGeometryIds>,
@@ -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,
},
@@ -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,
}
}
}
@@ -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<storage> aabb_buffers: binding_array<AabbBuffer>;
@group(0) @binding(3) var<storage> tape_metadata_buffers: binding_array<TapeMetadataBuffer>;
@group(0) @binding(6) var textures: binding_array<texture_2d<f32>>;
@group(0) @binding(7) var samplers: binding_array<sampler>;
@group(0) @binding(8) var<storage> materials: array<Material>;
@group(0) @binding(9) var tlas: acceleration_structure;
@group(0) @binding(10) var previous_frame_tlas: acceleration_structure;
@group(0) @binding(11) var<storage> transforms: array<mat3x4<f32>>;
@group(0) @binding(12) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
@group(0) @binding(13) var<storage> geometry_ids: array<InstanceGeometryIds>;
@group(0) @binding(14) var<storage> tape_ids: array<InstanceTapeIds>;
@group(0) @binding(15) var<storage> material_ids: array<u32>;
@group(0) @binding(16) var brdf_dfg_lut: texture_2d<f32>;
@group(0) @binding(17) var brdf_dfg_lut_sampler: sampler;
@group(0) @binding(18) var<storage> light_sources: array<LightSource>;
@group(0) @binding(19) var<storage> directional_lights: array<DirectionalLight>;
@group(0) @binding(20) var<storage> point_lights: array<PointLight>;
@group(0) @binding(21) var<storage> rect_lights: array<RectLight>;
@group(0) @binding(22) var environment_map: texture_cube<f32>;
@group(0) @binding(23) var environment_map_sampler: sampler;
@group(0) @binding(24) var<storage> environment_map_light: EnvironmentMapLight;
@group(0) @binding(25) var<storage> previous_frame_light_id_translations: array<u32>;
@group(0) @binding(6) var tiletrees: binding_array<texture_3d<f32>>;
@group(0) @binding(7) var tiletree_sampler: sampler;
@group(0) @binding(8) var textures: binding_array<texture_2d<f32>>;
@group(0) @binding(9) var samplers: binding_array<sampler>;
@group(0) @binding(10) var<storage> materials: array<Material>;
@group(0) @binding(11) var tlas: acceleration_structure;
@group(0) @binding(12) var previous_frame_tlas: acceleration_structure;
@group(0) @binding(13) var<storage> transforms: array<mat3x4<f32>>;
@group(0) @binding(14) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
@group(0) @binding(15) var<storage> geometry_ids: array<InstanceGeometryIds>;
@group(0) @binding(16) var<storage> tape_ids: array<InstanceTapeIds>;
@group(0) @binding(17) var<storage> material_ids: array<u32>;
@group(0) @binding(18) var brdf_dfg_lut: texture_2d<f32>;
@group(0) @binding(19) var brdf_dfg_lut_sampler: sampler;
@group(0) @binding(20) var<storage> light_sources: array<LightSource>;
@group(0) @binding(21) var<storage> directional_lights: array<DirectionalLight>;
@group(0) @binding(22) var<storage> point_lights: array<PointLight>;
@group(0) @binding(23) var<storage> rect_lights: array<RectLight>;
@group(0) @binding(24) var environment_map: texture_cube<f32>;
@group(0) @binding(25) var environment_map_sampler: sampler;
@group(0) @binding(26) var<storage> environment_map_light: EnvironmentMapLight;
@group(0) @binding(27) var<storage> previous_frame_light_id_translations: array<u32>;
const RAY_T_MIN = 0.001f;
const RAY_T_MAX = 100000.0f;
@@ -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);