From 0bdd2d95dbf777cdc006978630d3e5e35b1f9707 Mon Sep 17 00:00:00 2001 From: Molive Date: Tue, 29 Sep 2026 22:05:47 +0100 Subject: [PATCH] workin --- Cargo.lock | 6 +- examples/pica_pica.rs | 197 +++++++++++++++++++++++++++++++++- src/scene/binder/instances.rs | 15 +-- src/scene/mod.rs | 15 +-- src/scene/tapes/allocator.rs | 94 +++++++++++----- src/scene/tapes/blas.rs | 1 + src/scene/tapes/types.rs | 3 +- 7 files changed, 283 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa2ea18..dea1109 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -272,7 +272,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.59.0", + "windows-sys 0.60.2", "wl-clipboard-rs", "x11rb", ] @@ -6986,9 +6986,9 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" [[package]] name = "wl-clipboard-rs" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9651471a32e87d96ef3a127715382b2d11cc7c8bb9822ded8a7cc94072eb0a3" +checksum = "4d7888ccd4896447b2d14d3a9350a85df2aeb6f181e2e7a31349d104ac46cac1" dependencies = [ "libc", "log", diff --git a/examples/pica_pica.rs b/examples/pica_pica.rs index bf93472..7e050c4 100644 --- a/examples/pica_pica.rs +++ b/examples/pica_pica.rs @@ -22,12 +22,15 @@ use bevy::{ render::{diagnostic::RenderDiagnosticsPlugin, render_resource::TextureUsages}, world_serialization::WorldInstanceReady, }; +use bevy_color::palettes::css::RED; use bevy_tape_drive::{ pathtracer::{Pathtracer, PathtracingPlugin}, prelude::{RaytracingMesh3d, SolariLighting, SolariPlugins}, + scene::{Tape, Tape3d}, }; use chacha20::ChaCha8Rng; -use rand::{RngExt, SeedableRng}; +use rand::{RngExt, SeedableRng, rngs::SmallRng}; +use tape_load::ssa::{SSAInput, SSAOpcode, SSAOpcodeSized, SSATape}; /// `bevy_solari` demo. #[derive(FromArgs, Resource, Clone, Copy)] @@ -51,7 +54,12 @@ fn main() { ))); app.add_plugins(( - DefaultPlugins, + DefaultPlugins.set(bevy::log::LogPlugin { + // Uncomment this to override the default log settings: + level: bevy::log::Level::INFO, + // filter: "wgpu=warn,bevy_ecs=info".to_string(), + ..default() + }), SolariPlugins, FreeCameraPlugin, RenderDiagnosticsPlugin, @@ -81,6 +89,170 @@ fn main() { app.run(); } +fn create_csg(seed: u64) -> SSATape { + let mut rng = SmallRng::seed_from_u64(seed); + let mut tape = SSATape::default(); + let pos = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAPosition, + size: 1, + }, + vec![], + ); + let offset = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAAdd, + size: 3, + }, + vec![ + pos[0], + pos[1], + pos[2], + SSAInput::Constant(rng.random_range(0.0..1.0)), + SSAInput::Constant(rng.random_range(0.0..1.0)), + SSAInput::Constant(rng.random_range(0.0..1.0)), + ], + ); + let sin = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSASin, + size: 1, + }, + vec![pos[3]], + ); + let offset = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAAdd, + size: 3, + }, + vec![ + offset[0], + offset[1], + offset[2], + sin[0], + SSAInput::Constant(0.0), + SSAInput::Constant(0.0), + ], + ); + let sphere = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSASDFSphere, + size: 1, + }, + vec![ + offset[0], + offset[1], + offset[2], + SSAInput::Constant(rng.random_range(0.0..1.0)), + ], + ); + let torus = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSASDFTorus, + size: 1, + }, + vec![ + pos[0], + pos[1], + pos[2], + SSAInput::Constant(rng.random_range(0.0..1.0)), + SSAInput::Constant(rng.random_range(0.0..1.0)), + ], + ); + let min = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMin, + size: 1, + }, + vec![sphere[0], torus[0]], + ); + let scaled_time = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMul, + size: 1, + }, + vec![pos[3], SSAInput::Constant(0.2)], + ); + let moving_pos = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAAdd, + size: 3, + }, + vec![ + pos[0], + pos[1], + pos[2], + scaled_time[0], + scaled_time[0], + scaled_time[0], + ], + ); + let pos_times_20 = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMul, + size: 3, + }, + vec![ + moving_pos[0], + moving_pos[1], + moving_pos[2], + SSAInput::Constant(20.0), + SSAInput::Constant(20.0), + SSAInput::Constant(20.0), + ], + ); + let sine_of_the_times = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSASin, + size: 3, + }, + pos_times_20, + ); + let sine_of_the_times = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMul, + size: 3, + }, + vec![ + sine_of_the_times[0], + sine_of_the_times[1], + sine_of_the_times[2], + SSAInput::Constant(0.3), + SSAInput::Constant(0.3), + SSAInput::Constant(0.3), + ], + ); + let multiply_out_1 = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMul, + size: 1, + }, + vec![sine_of_the_times[0], sine_of_the_times[1]], + ); + let multiply_out_2 = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAMul, + size: 1, + }, + vec![multiply_out_1[0], sine_of_the_times[2]], + ); + let displacement = tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAAdd, + size: 1, + }, + vec![min[0], multiply_out_2[0]], + ); + tape.push_instruction( + SSAOpcodeSized { + opcode: SSAOpcode::SSAReturn, + size: 1, + }, + displacement, + ); + tape +} + fn setup_pica_pica( mut commands: Commands, asset_server: Res, @@ -199,6 +371,27 @@ fn setup_pica_pica( }, )], )); + + let csg = create_csg(0); + + let tape = Tape { + parts: unsafe { core::mem::transmute(csg) }, + _seed: 0, + subdivision: 8, + }; + let loaded_tape = asset_server.add(tape); + + let tape_material = StandardMaterial { + base_color: RED.into(), + ..Default::default() + }; + let loaded_material = asset_server.add(tape_material); + + commands.spawn(( + Tape3d(loaded_tape), + MeshMaterial3d(loaded_material), + Transform::default(), + )); } fn setup_many_lights( diff --git a/src/scene/binder/instances.rs b/src/scene/binder/instances.rs index 30fafef..5250167 100644 --- a/src/scene/binder/instances.rs +++ b/src/scene/binder/instances.rs @@ -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, 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, 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; diff --git a/src/scene/mod.rs b/src/scene/mod.rs index 0aa50c4..9b3a84b 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -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::) - .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::) + .after(allocate_and_free_tapes), prepare_raytracing_scene_resources.in_set(RenderSystems::PrepareResources), prepare_raytracing_scene_bind_group.in_set(RenderSystems::PrepareBindGroups), ), diff --git a/src/scene/tapes/allocator.rs b/src/scene/tapes/allocator.rs index f434e65..7de3eba 100644 --- a/src/scene/tapes/allocator.rs +++ b/src/scene/tapes/allocator.rs @@ -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, render_queue: Res, ) { + 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::() 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::() 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, + 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) { @@ -441,20 +480,19 @@ impl ElementLayout { /// Creates the appropriate [`ElementLayout`] for the given tape's metadata /// data. - fn metadata(_tape: &Tape) -> Option { - Some(ElementLayout::new( - ElementClass::Metadata, - size_of::() as u64, - )) + fn metadata(_tape: &Tape) -> ElementLayout { + ElementLayout { + class: ElementClass::Metadata, + size: size_of::() 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 { - Some(ElementLayout::new( - ElementClass::Aabb, - size_of::() as u64, - )) + fn aabb(_tape: &Tape) -> ElementLayout { + ElementLayout::new(ElementClass::Aabb, size_of::() as u64) } } diff --git a/src/scene/tapes/blas.rs b/src/scene/tapes/blas.rs index cfc1236..0413cb4 100644 --- a/src/scene/tapes/blas.rs +++ b/src/scene/tapes/blas.rs @@ -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); diff --git a/src/scene/tapes/types.rs b/src/scene/tapes/types.rs index 55b20bb..7dabc8b 100644 --- a/src/scene/tapes/types.rs +++ b/src/scene/tapes/types.rs @@ -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::() as u64 } } @@ -229,4 +229,3 @@ impl RenderAsset for RenderTape { #[reflect(Component, Default, Clone, PartialEq)] #[require(MeshMaterial3d, Transform, SyncToRenderWorld)] pub struct Tape3d(pub Handle); -