workin
This commit is contained in:
@@ -29,6 +29,7 @@ use super::{
|
||||
use crate::scene::tapes::{allocator::TapeAllocator, types::Tape};
|
||||
|
||||
pub const MAX_MESH_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap();
|
||||
pub const MAX_TAPE_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap();
|
||||
|
||||
#[derive(Clone, Copy, Default, PartialEq, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
@@ -390,6 +391,7 @@ impl InstanceState {
|
||||
mesh_id: AssetId<Mesh>,
|
||||
instance: &mut Instance,
|
||||
) -> bool {
|
||||
bevy_log::debug!("Resolving mesh instance {mesh_id}");
|
||||
let slot = instance.slot;
|
||||
let (Some(vertex_slice), Some(index_slice), Some(material_slot), Some(blas_address)) = (
|
||||
inputs.mesh_allocator.mesh_vertex_slice(&mesh_id),
|
||||
@@ -465,23 +467,24 @@ impl InstanceState {
|
||||
tape_id: AssetId<Tape>,
|
||||
instance: &mut Instance,
|
||||
) -> bool {
|
||||
bevy_log::debug!("Resolving tape instance {tape_id}");
|
||||
let slot = instance.slot;
|
||||
let (Some(aabb_slice), Some(material_slot), Some(blas_address)) = (
|
||||
inputs.tape_allocator.tape_aabb_slice(&tape_id),
|
||||
inputs.assets.material_slots.get(&instance.material),
|
||||
inputs.tape_blas_manager.device_address(&tape_id),
|
||||
inputs.tape_allocator.tape_aabb_slice(&tape_id),
|
||||
inputs.assets.material_slots.get(&instance.material),
|
||||
inputs.tape_blas_manager.device_address(&tape_id),
|
||||
) else {
|
||||
self.deactivate_instance(lights, entity, instance);
|
||||
return false;
|
||||
};
|
||||
|
||||
let aabb_buffer_key = aabb_slice.buffer.id();
|
||||
let capacity = MAX_MESH_SLAB_COUNT.get();
|
||||
let capacity = MAX_TAPE_SLAB_COUNT.get();
|
||||
if !self.aabb_buffers.has_room(&aabb_buffer_key, capacity) {
|
||||
once!(warn!(
|
||||
"Solari scene needs more than {} mesh slabs. Instances past that limit will not \
|
||||
"Solari scene needs more than {} tape slabs. Instances past that limit will not \
|
||||
be rendered.",
|
||||
MAX_MESH_SLAB_COUNT.get()
|
||||
MAX_TAPE_SLAB_COUNT.get()
|
||||
));
|
||||
self.deactivate_instance(lights, entity, instance);
|
||||
return false;
|
||||
|
||||
+8
-7
@@ -34,19 +34,17 @@ pub use meshes::types::RaytracingMesh3d;
|
||||
pub use tapes::{
|
||||
allocator::{TapeAllocatorPlugin, TapeRenderAssetPlugin},
|
||||
plugin::TapePlugin,
|
||||
types::Tape3d,
|
||||
types::{Tape, Tape3d},
|
||||
};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
SolariPlugins,
|
||||
scene::{
|
||||
SolariPlugins, scene::{
|
||||
extract::{
|
||||
extract_raytracing_scene_meshes_structural,
|
||||
extract_raytracing_scene_tapes_and_materials,
|
||||
extract_raytracing_scene_tapes_structural,
|
||||
},
|
||||
tapes::allocator::allocate_and_free_tapes,
|
||||
}, tapes::{allocator::allocate_and_free_tapes, types::RenderTape},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -105,11 +103,14 @@ impl Plugin for RaytracingScenePlugin {
|
||||
meshes::blas::prepare_raytracing_blas
|
||||
.in_set(RenderSystems::PrepareAssets)
|
||||
.before(prepare_assets::<RenderMesh>)
|
||||
.after(allocate_and_free_meshes)
|
||||
.after(allocate_and_free_tapes),
|
||||
.after(allocate_and_free_meshes),
|
||||
meshes::blas::compact_raytracing_blas
|
||||
.in_set(RenderSystems::PrepareAssets)
|
||||
.after(meshes::blas::prepare_raytracing_blas),
|
||||
tapes::blas::prepare_raytracing_blas
|
||||
.in_set(RenderSystems::PrepareAssets)
|
||||
.before(prepare_assets::<RenderTape>)
|
||||
.after(allocate_and_free_tapes),
|
||||
prepare_raytracing_scene_resources.in_set(RenderSystems::PrepareResources),
|
||||
prepare_raytracing_scene_bind_group.in_set(RenderSystems::PrepareBindGroups),
|
||||
),
|
||||
|
||||
@@ -122,7 +122,7 @@ impl TapeAllocationKey {
|
||||
}
|
||||
|
||||
/// The type of element that a tape slab can store.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ElementClass {
|
||||
/// Per-tape metadata
|
||||
Metadata,
|
||||
@@ -142,7 +142,7 @@ pub enum ElementClass {
|
||||
/// guaranteeing that the size of an allocation quantum is divisible by both the
|
||||
/// size of an element and [`COPY_BUFFER_ALIGNMENT`], so we can relocate it
|
||||
/// freely.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ElementLayout {
|
||||
class: ElementClass,
|
||||
|
||||
@@ -207,6 +207,12 @@ pub fn allocate_and_free_tapes(
|
||||
render_device: Res<RenderDevice>,
|
||||
render_queue: Res<RenderQueue>,
|
||||
) {
|
||||
bevy_log::debug!("Allocating and freeing tapes");
|
||||
bevy_log::debug!(
|
||||
"extracted tapes ({}): {:?}",
|
||||
extracted_tapes.extracted.len(),
|
||||
extracted_tapes.extracted
|
||||
);
|
||||
// Clear the list of tapes displaced by last frame's slab growth.
|
||||
tape_allocator.clear_displaced_keys();
|
||||
|
||||
@@ -322,34 +328,47 @@ impl TapeAllocator {
|
||||
// Loop over each tape that was extracted this frame.
|
||||
for (tape_id, tape) in &extracted_tapes.extracted {
|
||||
let aabb_buffer_size = tape.get_aabb_buffer_size() as u64;
|
||||
bevy_log::debug!("Tape {:?} has buffer size {}", tape_id, aabb_buffer_size);
|
||||
if aabb_buffer_size == 0 {
|
||||
warn!("Tape {:?} contains no aabbs.", tape_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Allocate metadata.
|
||||
if let Some(metadata_element_layout) = ElementLayout::metadata(tape) {
|
||||
allocation_stage.allocate(
|
||||
&TapeAllocationKey::new(*tape_id, ElementClass::Metadata),
|
||||
size_of::<TapeMetadata>() as u64,
|
||||
metadata_element_layout,
|
||||
tape_allocator_settings,
|
||||
);
|
||||
}
|
||||
let metadata_element_layout = ElementLayout::metadata(tape);
|
||||
bevy_log::debug!(
|
||||
"Tape {:?} has metadata element layout {:?}",
|
||||
tape_id,
|
||||
metadata_element_layout
|
||||
);
|
||||
allocation_stage.allocate(
|
||||
&TapeAllocationKey::new(*tape_id, ElementClass::Metadata),
|
||||
size_of::<TapeMetadata>() as u64,
|
||||
metadata_element_layout,
|
||||
tape_allocator_settings,
|
||||
);
|
||||
|
||||
// Allocate aabb data.
|
||||
if let Some(aabb_element_layout) = ElementLayout::aabb(tape) {
|
||||
allocation_stage.allocate(
|
||||
&TapeAllocationKey::new(*tape_id, ElementClass::Aabb),
|
||||
aabb_buffer_size,
|
||||
aabb_element_layout,
|
||||
tape_allocator_settings,
|
||||
);
|
||||
}
|
||||
let aabb_element_layout = ElementLayout::aabb(tape);
|
||||
bevy_log::debug!(
|
||||
"Tape {:?} has aabb element layout {:?}",
|
||||
tape_id,
|
||||
aabb_element_layout
|
||||
);
|
||||
allocation_stage.allocate(
|
||||
&TapeAllocationKey::new(*tape_id, ElementClass::Aabb),
|
||||
aabb_buffer_size,
|
||||
aabb_element_layout,
|
||||
tape_allocator_settings,
|
||||
);
|
||||
}
|
||||
|
||||
// Perform growth.
|
||||
allocation_stage.commit(render_device, render_queue);
|
||||
for (tape_id, _) in &extracted_tapes.extracted {
|
||||
bevy_log::debug!("Tape {}: Aabb slice allocated", tape_id);
|
||||
debug_assert!(self.tape_id_to_aabb_slab(tape_id).is_some())
|
||||
}
|
||||
|
||||
// Copy new tape data in.
|
||||
for (tape_id, tape) in &extracted_tapes.extracted {
|
||||
@@ -357,6 +376,7 @@ impl TapeAllocator {
|
||||
if aabb_buffer_size == 0 {
|
||||
continue;
|
||||
}
|
||||
self.copy_tape_aabb(tape_id, tape, render_device, render_queue);
|
||||
self.copy_tape_metadata(tape_id, tape, render_device, render_queue);
|
||||
}
|
||||
}
|
||||
@@ -389,6 +409,25 @@ impl TapeAllocator {
|
||||
);
|
||||
}
|
||||
|
||||
/// Fills the tape AABB buffer with zeros.
|
||||
fn copy_tape_aabb(
|
||||
&mut self,
|
||||
tape_id: &AssetId<Tape>,
|
||||
tape: &Tape,
|
||||
render_device: &RenderDevice,
|
||||
render_queue: &RenderQueue,
|
||||
) {
|
||||
let length = tape.get_aabb_buffer_size();
|
||||
// Call the generic function.
|
||||
self.copy_element_data(
|
||||
&TapeAllocationKey::new(*tape_id, ElementClass::Aabb),
|
||||
length as _,
|
||||
|mut slice| slice.fill(0),
|
||||
render_device,
|
||||
render_queue,
|
||||
);
|
||||
}
|
||||
|
||||
/// Frees allocations for tapes that were removed, modified, or
|
||||
/// re-extracted this frame.
|
||||
fn free_tapes(&mut self, extracted_tapes: &ExtractedAssets<RenderTape>) {
|
||||
@@ -441,20 +480,19 @@ impl ElementLayout {
|
||||
|
||||
/// Creates the appropriate [`ElementLayout`] for the given tape's metadata
|
||||
/// data.
|
||||
fn metadata(_tape: &Tape) -> Option<ElementLayout> {
|
||||
Some(ElementLayout::new(
|
||||
ElementClass::Metadata,
|
||||
size_of::<TapeMetadata>() as u64,
|
||||
))
|
||||
fn metadata(_tape: &Tape) -> ElementLayout {
|
||||
ElementLayout {
|
||||
class: ElementClass::Metadata,
|
||||
size: size_of::<TapeMetadata>() as u64,
|
||||
elements_per_slot: 1,
|
||||
buffer_usages: ElementClass::Metadata.buffer_usages(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates the appropriate [`ElementLayout`] for the given tape's aabb
|
||||
/// data.
|
||||
fn aabb(_tape: &Tape) -> Option<ElementLayout> {
|
||||
Some(ElementLayout::new(
|
||||
ElementClass::Aabb,
|
||||
size_of::<Aabb>() as u64,
|
||||
))
|
||||
fn aabb(_tape: &Tape) -> ElementLayout {
|
||||
ElementLayout::new(ElementClass::Aabb, size_of::<Aabb>() as u64)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ pub fn prepare_raytracing_blas(
|
||||
.extracted
|
||||
.iter()
|
||||
.map(|(asset_id, _)| {
|
||||
bevy_log::info!("Tape {}: Getting the aabb slice", asset_id);
|
||||
let aabbs_slice = tape_allocator.tape_aabb_slice(asset_id).unwrap();
|
||||
|
||||
let (blas, blas_size) = allocate_blas(&aabbs_slice, asset_id, &render_device);
|
||||
|
||||
@@ -168,7 +168,7 @@ impl Tape {
|
||||
}
|
||||
|
||||
pub fn get_aabb_buffer_size(&self) -> u64 {
|
||||
(self.subdivision * self.subdivision * self.subdivision) as u64
|
||||
((self.subdivision * self.subdivision * self.subdivision) as u64) * size_of::<Aabb>() as u64
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,4 +229,3 @@ impl RenderAsset for RenderTape {
|
||||
#[reflect(Component, Default, Clone, PartialEq)]
|
||||
#[require(MeshMaterial3d<StandardMaterial>, Transform, SyncToRenderWorld)]
|
||||
pub struct Tape3d(pub Handle<Tape>);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user