update to latest solari

This commit is contained in:
2026-10-09 17:37:53 +01:00
parent 051b3a6c77
commit 867e9c146b
28 changed files with 1776 additions and 597 deletions
@@ -22,7 +22,8 @@ use bytemuck::{Pod, Zeroable};
use tracing::{info_span, warn};
use super::{
MeshBlasManager, RaytracingMesh3d, RaytracingSceneBindings, Tape3d, TapeBlasManager,
MeshBlasKey, MeshBlasManager, MeshBlasOpacity, RaytracingMesh3d, RaytracingSceneBindings,
Tape3d, TapeBlasKey, TapeBlasManager, TapeBlasOpacity,
allocator::{IndexAllocator, RetainedBindingArray},
assets::AssetState,
lights::{GpuLightSource, LightSourceId, LightState},
@@ -90,15 +91,149 @@ pub enum Object {
Tape(AssetId<Tape>),
}
/// Everything tracked per raytracing mesh instance.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlasKey {
Mesh(MeshBlasKey),
Tape(TapeBlasKey),
}
/// Everything tracked per raytracing instance.
#[derive(Clone, Copy)]
struct Instance {
struct MeshInstance {
slot: u32,
object: Object,
mesh: AssetId<Mesh>,
material: AssetId<StandardMaterial>,
opacity: MeshBlasOpacity,
buffers: Option<(BufferId, BufferId)>,
}
/// Everything tracked per raytracing instance.
#[derive(Clone, Copy)]
struct TapeInstance {
slot: u32,
tape: AssetId<Tape>,
material: AssetId<StandardMaterial>,
opacity: TapeBlasOpacity,
buffers: Option<(BufferId, BufferId)>,
}
impl MeshInstance {
fn blas_key(&self) -> MeshBlasKey {
MeshBlasKey {
mesh: self.mesh,
opacity: self.opacity,
}
}
}
impl TapeInstance {
fn blas_key(&self) -> TapeBlasKey {
TapeBlasKey {
tape: self.tape,
opacity: self.opacity,
}
}
}
#[derive(Clone, Copy)]
enum Instance {
Mesh(MeshInstance),
Tape(TapeInstance),
}
impl Instance {
fn get_key(&self) -> BlasKey {
match self {
Instance::Mesh(mesh_instance) => BlasKey::Mesh(mesh_instance.blas_key()),
Instance::Tape(tape_instance) => BlasKey::Tape(tape_instance.blas_key()),
}
}
fn get_slot(&self) -> u32 {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.slot,
Instance::Tape(tape_instance) => tape_instance.slot,
}
}
fn get_material(&self) -> AssetId<StandardMaterial> {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.material,
Instance::Tape(tape_instance) => tape_instance.material,
}
}
fn get_buffers(&self) -> Option<(BufferId, BufferId)> {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.buffers,
Instance::Tape(tape_instance) => tape_instance.buffers,
}
}
fn get_object(&self) -> Object {
match self {
Instance::Mesh(mesh_instance) => Object::Mesh(mesh_instance.mesh),
Instance::Tape(tape_instance) => Object::Tape(tape_instance.tape),
}
}
fn new(
slot: u32,
object: Object,
material: AssetId<StandardMaterial>,
buffers: Option<(BufferId, BufferId)>,
) -> Self {
match object {
Object::Mesh(mesh) => Instance::Mesh(MeshInstance {
slot,
mesh,
material,
opacity: MeshBlasOpacity::Opaque,
buffers,
}),
Object::Tape(tape) => Instance::Tape(TapeInstance {
slot,
tape,
material,
opacity: TapeBlasOpacity::Opaque,
buffers,
}),
}
}
fn take_buffers(&mut self) -> Option<(BufferId, BufferId)> {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.buffers.take(),
Instance::Tape(tape_instance) => tape_instance.buffers.take(),
}
}
fn set_slot(&mut self, slot: u32) {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.slot = slot,
Instance::Tape(tape_instance) => tape_instance.slot = slot,
}
}
fn set_material(&mut self, material: AssetId<StandardMaterial>) {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.material = material,
Instance::Tape(tape_instance) => tape_instance.material = material,
}
}
fn set_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
match self {
Instance::Mesh(mesh_instance) => mesh_instance.buffers = buffers,
Instance::Tape(tape_instance) => tape_instance.buffers = buffers,
}
}
fn get_shared(&self) -> (u32, AssetId<StandardMaterial>, Option<(BufferId, BufferId)>) {
(self.get_slot(), self.get_material(), self.get_buffers())
}
}
/// Stable slots, reverse dependency indices and GPU data owned by raytracing
/// instances.
pub struct InstanceState {
@@ -113,6 +248,10 @@ pub struct InstanceState {
pub material_ids: AtomicSparseBufferVec<u32>,
pub blas_refs: AtomicSparseBufferVec<GpuBlasRef>,
pub slots: IndexAllocator,
/// Slab buffer references dropped this frame, released next frame since the
/// previous frame's TLAS can still reach them through `geometry_ids`.
retired_mesh_buffers: Vec<(BufferId, BufferId)>,
retired_tape_buffers: Vec<(BufferId, BufferId)>,
records: EntityHashMap<Instance>,
pub live_count: u32,
pub pending_refresh: EntityHashSet,
@@ -134,6 +273,8 @@ impl InstanceState {
material_ids: storage_buffer("solari_material_ids"),
blas_refs: storage_buffer("solari_blas_refs"),
slots: IndexAllocator::new(),
retired_mesh_buffers: Vec::new(),
retired_tape_buffers: Vec::new(),
records: EntityHashMap::default(),
live_count: 0,
pending_refresh: EntityHashSet::default(),
@@ -148,11 +289,11 @@ impl InstanceState {
/// descriptors that the raw path sets up on the GPU. Slots with a null
/// acceleration structure reference are not currently drawable, and are
/// left out.
pub fn drawable(&self) -> impl Iterator<Item = (u32, Object, [f32; 12])> + '_ {
pub fn drawable(&self) -> impl Iterator<Item = (u32, BlasKey, [f32; 12])> + '_ {
self.records.values().filter_map(|instance| {
let slot = instance.slot;
let slot = instance.get_slot();
(self.blas_refs.get(slot) != GpuBlasRef::NONE)
.then(|| (slot, instance.object, self.transforms.get(slot).rows()))
.then(|| (slot, instance.get_key(), self.transforms.get(slot).rows()))
})
}
@@ -231,6 +372,20 @@ fn relink<K: Copy + Eq + Hash>(
}
impl InstanceState {
/// Frees slots and slab buffers dropped last frame, now that the previous
/// frame's TLAS no longer references them.
pub fn begin_frame(&mut self) {
self.slots.recycle_retired();
for (vertex_key, index_key) in core::mem::take(&mut self.retired_mesh_buffers) {
self.vertex_buffers.release(&vertex_key);
self.index_buffers.release(&index_key);
}
for (aabb_key, metadata_key) in core::mem::take(&mut self.retired_tape_buffers) {
self.aabb_buffers.release(&aabb_key);
self.tape_metadata_buffers.release(&metadata_key);
}
}
pub fn remove_instances(
&mut self,
lights: &mut LightState,
@@ -331,21 +486,15 @@ impl InstanceState {
let material_id = material.id();
relink(
&mut self.object_instances,
entity,
previous.map(|instance| instance.object),
id,
);
relink(
&mut self.material_instances,
entity,
previous.map(|instance| instance.material),
previous.map(|instance| instance.get_material()),
material_id,
);
let slot = match previous {
Some(previous) => previous.slot,
Some(previous) => previous.get_slot(),
None => self.slots.allocate(),
};
self.reserve_slot(slot);
@@ -356,12 +505,12 @@ impl InstanceState {
self.write_transforms(slot, transform, previous_frame_transform);
}
let mut instance = Instance {
let mut instance = Instance::new(
slot,
object: id,
material: material_id,
buffers: previous.and_then(|instance| instance.buffers),
};
id,
material_id,
previous.and_then(|instance| instance.get_buffers()),
);
let resolved = self.resolve_instance(inputs, lights, entity, &mut instance);
self.records.insert(entity, instance);
@@ -377,12 +526,12 @@ impl InstanceState {
entity: Entity,
instance: &mut Instance,
) -> bool {
match instance.object {
Object::Mesh(asset_id) => {
self.resolve_mesh_instance(inputs, lights, entity, asset_id, instance)
match instance {
Instance::Mesh(instance) => {
self.resolve_mesh_instance(inputs, lights, entity, instance)
},
Object::Tape(asset_id) => {
self.resolve_tape_instance(inputs, lights, entity, asset_id, instance)
Instance::Tape(instance) => {
self.resolve_tape_instance(inputs, lights, entity, instance)
},
}
}
@@ -392,18 +541,44 @@ impl InstanceState {
inputs: &InstanceInputs,
lights: &mut LightState,
entity: Entity,
mesh_id: AssetId<Mesh>,
instance: &mut Instance,
instance: &mut MeshInstance,
) -> bool {
bevy_log::debug!("Resolving mesh instance {mesh_id}");
bevy_log::debug!("Resolving mesh instance {}", instance.mesh);
let slot = instance.slot;
let material_slot = inputs.assets.material_slots.get(&instance.material);
instance.opacity = if inputs
.assets
.non_opaque_materials
.contains(&instance.material)
{
MeshBlasOpacity::NonOpaque
} else {
MeshBlasOpacity::Opaque
};
let blas_key = instance.blas_key();
let blas_address = inputs.mesh_blas_manager.device_address(&blas_key);
if blas_address.is_none()
&& material_slot.is_some()
&& inputs.mesh_blas_manager.is_undeclared(&blas_key)
{
once!(warn!(
"RaytracingMesh3d entity {entity} uses a material that needs `{flag:?}`, but \
`Mesh::raytracing` of mesh {mesh} lacks it. Entities like it will not be \
raytraced.",
flag = instance.opacity.flag(),
mesh = instance.mesh,
));
}
let (Some(vertex_slice), Some(index_slice), Some(material_slot), Some(blas_address)) = (
inputs.mesh_allocator.mesh_vertex_slice(&mesh_id),
inputs.mesh_allocator.mesh_index_slice(&mesh_id),
inputs.assets.material_slots.get(&instance.material),
inputs.mesh_blas_manager.device_address(&mesh_id),
inputs.mesh_allocator.mesh_vertex_slice(&instance.mesh),
inputs.mesh_allocator.mesh_index_slice(&instance.mesh),
material_slot,
blas_address,
) else {
self.deactivate_instance(lights, entity, instance);
self.deactivate_mesh_instance(lights, entity, instance);
return false;
};
@@ -418,7 +593,7 @@ impl InstanceState {
be rendered.",
MAX_MESH_SLAB_COUNT.get()
));
self.deactivate_instance(lights, entity, instance);
self.deactivate_mesh_instance(lights, entity, instance);
return false;
}
@@ -468,26 +643,56 @@ impl InstanceState {
inputs: &InstanceInputs,
lights: &mut LightState,
entity: Entity,
tape_id: AssetId<Tape>,
instance: &mut Instance,
instance: &mut TapeInstance,
) -> bool {
bevy_log::info!("Resolving tape instance {tape_id}");
bevy_log::debug!("Resolving tape instance {}", instance.tape);
let slot = instance.slot;
let material_slot = inputs.assets.material_slots.get(&instance.material);
instance.opacity = if inputs
.assets
.non_opaque_materials
.contains(&instance.material)
{
TapeBlasOpacity::NonOpaque
} else {
TapeBlasOpacity::Opaque
};
let blas_key = instance.blas_key();
let blas_address = inputs.tape_blas_manager.device_address(&blas_key);
if blas_address.is_none()
&& material_slot.is_some()
&& inputs.tape_blas_manager.is_undeclared(&blas_key)
{
once!(warn!(
"Tape3d entity {entity} uses a material that needs `{flag:?}`, but \
`Tape::raytracing` of tape {tape} lacks it. Entities like it will not be \
raytraced.",
flag = instance.opacity.flag(),
tape = instance.tape,
));
}
let (Some(aabb_slice), Some(metadata_slice), Some(material_slot), Some(blas_address)) = (
inputs.tape_allocator.tape_aabb_slice(&tape_id),
inputs.tape_allocator.tape_metadata_slice(&tape_id),
inputs.assets.material_slots.get(&instance.material),
inputs.tape_blas_manager.device_address(&tape_id),
inputs.tape_allocator.tape_aabb_slice(&instance.tape),
inputs.tape_allocator.tape_metadata_slice(&instance.tape),
material_slot,
blas_address,
) else {
bevy_log::info!("Deactivating tape instance {tape_id}");
self.deactivate_instance(lights, entity, instance);
bevy_log::info!("Deactivating tape instance {}", instance.tape);
self.deactivate_tape_instance(lights, entity, instance);
return false;
};
bevy_log::info!("Tape {tape_id} aabb_slice: {:?}", aabb_slice.range);
bevy_log::info!("Tape {tape_id} metadata_slice: {:?}", metadata_slice.range);
bevy_log::info!("Tape {tape_id} material_slot: {material_slot}");
bevy_log::info!("Tape {tape_id} blas_address: {blas_address}");
bevy_log::info!("Tape {} aabb_slice: {:?}", instance.tape, aabb_slice.range);
bevy_log::info!(
"Tape {} metadata_slice: {:?}",
instance.tape,
metadata_slice.range
);
bevy_log::info!("Tape {} material_slot: {material_slot}", instance.tape);
bevy_log::info!("Tape {} blas_address: {blas_address}", instance.tape);
let aabb_buffer_key = aabb_slice.buffer.id();
let metadata_buffer_key = metadata_slice.buffer.id();
@@ -502,7 +707,7 @@ impl InstanceState {
be rendered.",
MAX_TAPE_SLAB_COUNT.get()
));
self.deactivate_instance(lights, entity, instance);
self.deactivate_tape_instance(lights, entity, instance);
return false;
}
@@ -549,7 +754,7 @@ impl InstanceState {
}
*/
bevy_log::info!("Tape {tape_id} resolved");
bevy_log::info!("Tape {} resolved", instance.tape);
true
}
@@ -587,44 +792,66 @@ impl InstanceState {
}
}
fn deactivate_instance(
fn deactivate_instance_common(
&mut self,
lights: &mut LightState,
entity: Entity,
instance: &mut Instance,
instance: &Instance,
) {
self.set_blas_ref(instance.slot, GpuBlasRef::NONE);
self.set_blas_ref(instance.get_slot(), GpuBlasRef::NONE);
lights.remove_light(LightSourceId::EmissiveMesh(entity));
match instance.object {
Object::Mesh(_) => self.release_mesh_buffers(instance.buffers.take()),
Object::Tape(_) => self.release_tape_buffers(instance.buffers.take()),
}
}
fn deactivate_mesh_instance(
&mut self,
lights: &mut LightState,
entity: Entity,
instance: &mut MeshInstance,
) {
self.deactivate_instance_common(lights, entity, &Instance::Mesh(*instance));
self.release_mesh_buffers(instance.buffers.take());
}
fn deactivate_tape_instance(
&mut self,
lights: &mut LightState,
entity: Entity,
instance: &mut TapeInstance,
) {
self.deactivate_instance_common(lights, entity, &Instance::Tape(*instance));
self.release_tape_buffers(instance.buffers.take());
}
fn release_mesh_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
if let Some((vertex_key, index_key)) = buffers {
self.vertex_buffers.release(&vertex_key);
self.index_buffers.release(&index_key);
}
self.retired_mesh_buffers.extend(buffers);
}
fn release_tape_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
if let Some((aabb_key, metadata_key)) = buffers {
self.aabb_buffers.release(&aabb_key);
self.tape_metadata_buffers.release(&metadata_key);
}
self.retired_tape_buffers.extend(buffers);
}
fn remove_instance(&mut self, lights: &mut LightState, entity: Entity) {
let Some(mut instance) = self.records.remove(&entity) else {
let Some(instance) = self.records.remove(&entity) else {
return;
};
self.deactivate_instance(lights, entity, &mut instance);
self.slots.release(instance.slot);
match instance {
Instance::Mesh(mut instance) => {
self.deactivate_mesh_instance(lights, entity, &mut instance)
},
Instance::Tape(mut instance) => {
self.deactivate_tape_instance(lights, entity, &mut instance)
},
}
self.slots.retire(instance.get_slot());
self.pending_refresh.remove(&entity);
unlink(&mut self.object_instances, &instance.object, entity);
unlink(&mut self.material_instances, &instance.material, entity);
unlink(&mut self.object_instances, &instance.get_object(), entity);
unlink(
&mut self.material_instances,
&instance.get_material(),
entity,
);
}
}
@@ -638,8 +865,11 @@ impl RaytracingSceneBindings {
previous_frame_transform: &PreviousGlobalTransform,
) {
if let Some(instance) = self.instances.records.get(&entity) {
self.instances
.write_transforms(instance.slot, transform, previous_frame_transform);
self.instances.write_transforms(
instance.get_slot(),
transform,
previous_frame_transform,
);
}
}
}