Spatial texture creation
This commit is contained in:
Generated
+1
@@ -1678,6 +1678,7 @@ dependencies = [
|
||||
"derive_more",
|
||||
"tracing",
|
||||
"wgpu",
|
||||
"wgpu-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -49,6 +49,7 @@ bytemuck = { version = "1" }
|
||||
derive_more = { version = "2", default-features = false, features = ["from"] }
|
||||
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
||||
wgpu = { version = "30", default-features = false }
|
||||
wgpu-types = { version = "30", default-features = false }
|
||||
|
||||
[features]
|
||||
dlss = ["bevy_anti_alias/dlss"]
|
||||
|
||||
@@ -103,6 +103,7 @@ impl RaytracingSceneBindings {
|
||||
&mut self.instances.index_buffers.dirty,
|
||||
&mut self.instances.aabb_buffers.dirty,
|
||||
&mut self.instances.tape_metadata_buffers.dirty,
|
||||
&mut self.instances.spatial_textures.dirty,
|
||||
&mut self.assets.textures.dirty,
|
||||
] {
|
||||
invalid |= core::mem::replace(dirty, false);
|
||||
@@ -159,6 +160,19 @@ impl RaytracingSceneBindings {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut spatial_textures: Vec<_> = self
|
||||
.instances
|
||||
.spatial_textures
|
||||
.iter()
|
||||
.map(|texture| match texture {
|
||||
Some(view) => view.deref(),
|
||||
None => fallback_texture.d3.texture_view.deref(),
|
||||
})
|
||||
.collect();
|
||||
if spatial_textures.is_empty() {
|
||||
spatial_textures.push(fallback_texture.d3.texture_view.deref());
|
||||
}
|
||||
|
||||
let (mut textures, mut samplers): (Vec<_>, Vec<_>) = self
|
||||
.assets
|
||||
.textures
|
||||
@@ -199,6 +213,7 @@ impl RaytracingSceneBindings {
|
||||
index_buffers.as_slice(),
|
||||
aabb_buffers.as_slice(),
|
||||
tape_metadata_buffers.as_slice(),
|
||||
spatial_textures.as_slice(),
|
||||
textures.as_slice(),
|
||||
samplers.as_slice(),
|
||||
self.assets.materials.binding().unwrap(),
|
||||
|
||||
@@ -13,13 +13,16 @@ use bevy_platform::collections::HashMap;
|
||||
use bevy_render::{
|
||||
impl_atomic_pod,
|
||||
mesh::allocator::MeshAllocator,
|
||||
render_resource::{AtomicPod, AtomicSparseBufferVec, Buffer, BufferId, BufferUsages},
|
||||
render_resource::{
|
||||
AtomicPod, AtomicSparseBufferVec, Buffer, BufferId, BufferUsages, TextureId, TextureView,
|
||||
},
|
||||
};
|
||||
use bevy_tape_load::{Tape, render::allocator::TapeAllocator};
|
||||
use bevy_transform::components::GlobalTransform;
|
||||
use bevy_utils::once;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use tracing::{info_span, warn};
|
||||
use wgpu_types::TextureViewDescriptor;
|
||||
|
||||
use super::{
|
||||
MeshBlasKey, MeshBlasManager, MeshBlasOpacity, RaytracingMesh3d, RaytracingSceneBindings,
|
||||
@@ -31,6 +34,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();
|
||||
|
||||
#[derive(Clone, Copy, Default, PartialEq, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
@@ -48,6 +52,7 @@ pub struct GpuInstanceTapeIds {
|
||||
aabb_buffer_id: u32,
|
||||
aabb_buffer_offset: u32,
|
||||
aabb_count: u32,
|
||||
spatial_texture_id: u32,
|
||||
metadata_buffer_id: u32,
|
||||
metadata_buffer_offset: u32,
|
||||
}
|
||||
@@ -97,6 +102,30 @@ pub enum BlasKey {
|
||||
Tape(TapeBlasKey),
|
||||
}
|
||||
|
||||
type MeshBuffers = (BufferId, BufferId);
|
||||
type TapeBuffers = (BufferId, BufferId, TextureId);
|
||||
|
||||
pub enum Buffers {
|
||||
Mesh(MeshBuffers),
|
||||
Tape(TapeBuffers),
|
||||
}
|
||||
|
||||
impl Buffers {
|
||||
fn as_mesh(self) -> MeshBuffers {
|
||||
match self {
|
||||
Buffers::Mesh(b) => b,
|
||||
Buffers::Tape(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_tape(self) -> TapeBuffers {
|
||||
match self {
|
||||
Buffers::Mesh(_) => unreachable!(),
|
||||
Buffers::Tape(b) => b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything tracked per raytracing instance.
|
||||
#[derive(Clone, Copy)]
|
||||
struct MeshInstance {
|
||||
@@ -104,7 +133,7 @@ struct MeshInstance {
|
||||
mesh: AssetId<Mesh>,
|
||||
material: AssetId<StandardMaterial>,
|
||||
opacity: MeshBlasOpacity,
|
||||
buffers: Option<(BufferId, BufferId)>,
|
||||
buffers: Option<MeshBuffers>,
|
||||
}
|
||||
|
||||
/// Everything tracked per raytracing instance.
|
||||
@@ -114,7 +143,7 @@ struct TapeInstance {
|
||||
tape: AssetId<Tape>,
|
||||
material: AssetId<StandardMaterial>,
|
||||
opacity: TapeBlasOpacity,
|
||||
buffers: Option<(BufferId, BufferId)>,
|
||||
buffers: Option<TapeBuffers>,
|
||||
}
|
||||
|
||||
impl MeshInstance {
|
||||
@@ -163,10 +192,10 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_buffers(&self) -> Option<(BufferId, BufferId)> {
|
||||
fn get_buffers(&self) -> Option<Buffers> {
|
||||
match self {
|
||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers,
|
||||
Instance::Tape(tape_instance) => tape_instance.buffers,
|
||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers.map(|b| Buffers::Mesh(b)),
|
||||
Instance::Tape(tape_instance) => tape_instance.buffers.map(|b| Buffers::Tape(b)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +210,7 @@ impl Instance {
|
||||
slot: u32,
|
||||
object: Object,
|
||||
material: AssetId<StandardMaterial>,
|
||||
buffers: Option<(BufferId, BufferId)>,
|
||||
buffers: Option<Buffers>,
|
||||
) -> Self {
|
||||
match object {
|
||||
Object::Mesh(mesh) => Instance::Mesh(MeshInstance {
|
||||
@@ -189,25 +218,18 @@ impl Instance {
|
||||
mesh,
|
||||
material,
|
||||
opacity: MeshBlasOpacity::Opaque,
|
||||
buffers,
|
||||
buffers: buffers.map(Buffers::as_mesh),
|
||||
}),
|
||||
Object::Tape(tape) => Instance::Tape(TapeInstance {
|
||||
slot,
|
||||
tape,
|
||||
material,
|
||||
opacity: TapeBlasOpacity::Opaque,
|
||||
buffers,
|
||||
buffers: buffers.map(Buffers::as_tape),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -222,14 +244,14 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
|
||||
fn set_buffers(&mut self, buffers: Option<Buffers>) {
|
||||
match self {
|
||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers = buffers,
|
||||
Instance::Tape(tape_instance) => tape_instance.buffers = buffers,
|
||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers = buffers.map(Buffers::as_mesh),
|
||||
Instance::Tape(tape_instance) => tape_instance.buffers = buffers.map(Buffers::as_tape),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_shared(&self) -> (u32, AssetId<StandardMaterial>, Option<(BufferId, BufferId)>) {
|
||||
fn get_shared(&self) -> (u32, AssetId<StandardMaterial>, Option<Buffers>) {
|
||||
(self.get_slot(), self.get_material(), self.get_buffers())
|
||||
}
|
||||
}
|
||||
@@ -241,6 +263,7 @@ pub struct InstanceState {
|
||||
pub index_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub aabb_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub tape_metadata_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub spatial_textures: RetainedBindingArray<TextureId, TextureView>,
|
||||
pub transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||
pub previous_frame_transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||
pub geometry_ids: AtomicSparseBufferVec<GpuInstanceGeometryIds>,
|
||||
@@ -250,8 +273,8 @@ pub struct InstanceState {
|
||||
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)>,
|
||||
retired_mesh_buffers: Vec<MeshBuffers>,
|
||||
retired_tape_buffers: Vec<TapeBuffers>,
|
||||
records: EntityHashMap<Instance>,
|
||||
pub live_count: u32,
|
||||
pub pending_refresh: EntityHashSet,
|
||||
@@ -266,6 +289,7 @@ impl InstanceState {
|
||||
index_buffers: RetainedBindingArray::new(),
|
||||
aabb_buffers: RetainedBindingArray::new(),
|
||||
tape_metadata_buffers: RetainedBindingArray::new(),
|
||||
spatial_textures: RetainedBindingArray::new(),
|
||||
transforms: storage_buffer("solari_transforms"),
|
||||
previous_frame_transforms: storage_buffer("solari_previous_frame_transforms"),
|
||||
geometry_ids: storage_buffer("solari_geometry_ids"),
|
||||
@@ -380,9 +404,11 @@ impl InstanceState {
|
||||
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) {
|
||||
for (aabb_key, metadata_key, spatial_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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,12 +700,20 @@ impl InstanceState {
|
||||
));
|
||||
}
|
||||
|
||||
let (Some(aabb_slice), Some(metadata_slice), Some(material_slot), Some(blas_address)) = (
|
||||
let (
|
||||
Some(aabb_slice),
|
||||
Some(metadata_slice),
|
||||
Some(spatial_texture),
|
||||
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),
|
||||
material_slot,
|
||||
blas_address,
|
||||
) else {
|
||||
)
|
||||
else {
|
||||
bevy_log::info!("Deactivating tape instance {}", instance.tape);
|
||||
self.deactivate_tape_instance(lights, entity, instance);
|
||||
return false;
|
||||
@@ -711,6 +745,21 @@ impl InstanceState {
|
||||
return false;
|
||||
}
|
||||
|
||||
let spatial_texture_key = spatial_texture.id();
|
||||
let spatial_capacity = MAX_SPATIAL_TEXTURE_COUNT.get();
|
||||
if !self
|
||||
.spatial_textures
|
||||
.has_room(&spatial_texture_key, spatial_capacity)
|
||||
{
|
||||
once!(warn!(
|
||||
"Solari scene needs more than {} spatial textures. Instances past that limit will not \
|
||||
be rendered.",
|
||||
MAX_SPATIAL_TEXTURE_COUNT.get()
|
||||
));
|
||||
self.deactivate_tape_instance(lights, entity, instance);
|
||||
return false;
|
||||
}
|
||||
|
||||
let previous_buffers = instance.buffers.take();
|
||||
let aabb_buffer_id = self
|
||||
.aabb_buffers
|
||||
@@ -722,7 +771,19 @@ impl InstanceState {
|
||||
metadata_slice.buffer.clone()
|
||||
})
|
||||
.expect("tape metadata slab binding array had room but handed out no slot");
|
||||
instance.buffers = Some((aabb_buffer_key, metadata_buffer_key));
|
||||
let spatial_texture_id = self
|
||||
.spatial_textures
|
||||
.acquire(spatial_texture_key, spatial_capacity, || {
|
||||
spatial_texture.create_view(&TextureViewDescriptor {
|
||||
label: Some(&format!(
|
||||
"Tape {} slot {} spatial texture view",
|
||||
instance.tape, instance.slot
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
.expect("spatial vslab binding array had room but handed out no slot");
|
||||
instance.buffers = Some((aabb_buffer_key, metadata_buffer_key, spatial_texture_key));
|
||||
self.release_tape_buffers(previous_buffers);
|
||||
|
||||
let aabb_count = (aabb_slice.range.len()) as u32;
|
||||
@@ -732,6 +793,7 @@ impl InstanceState {
|
||||
aabb_buffer_id,
|
||||
aabb_buffer_offset: aabb_slice.range.start,
|
||||
aabb_count,
|
||||
spatial_texture_id,
|
||||
metadata_buffer_id,
|
||||
metadata_buffer_offset: metadata_slice.range.start,
|
||||
},
|
||||
@@ -822,11 +884,11 @@ impl InstanceState {
|
||||
self.release_tape_buffers(instance.buffers.take());
|
||||
}
|
||||
|
||||
fn release_mesh_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
|
||||
fn release_mesh_buffers(&mut self, buffers: Option<MeshBuffers>) {
|
||||
self.retired_mesh_buffers.extend(buffers);
|
||||
}
|
||||
|
||||
fn release_tape_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
|
||||
fn release_tape_buffers(&mut self, buffers: Option<TapeBuffers>) {
|
||||
self.retired_tape_buffers.extend(buffers);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ use super::{
|
||||
},
|
||||
};
|
||||
use crate::scene::{
|
||||
binder::instances::MAX_TAPE_SLAB_COUNT,
|
||||
binder::instances::{MAX_SPATIAL_TEXTURE_COUNT, MAX_TAPE_SLAB_COUNT},
|
||||
extract::{
|
||||
ExtractedRaytracingDirectionalLight, ExtractedRaytracingPointLight,
|
||||
ExtractedRaytracingRectLight, ExtractedRaytracingSpotLight,
|
||||
@@ -101,6 +101,8 @@ impl FromWorld for RaytracingSceneBindings {
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_TAPE_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_TAPE_SLAB_COUNT),
|
||||
texture_3d(TextureSampleType::Float { filterable: true })
|
||||
.count(MAX_SPATIAL_TEXTURE_COUNT),
|
||||
texture_2d(TextureSampleType::Float { filterable: true })
|
||||
.count(MAX_TEXTURE_COUNT),
|
||||
sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT),
|
||||
|
||||
@@ -18,6 +18,7 @@ struct InstanceTapeIds {
|
||||
aabb_buffer_id: u32,
|
||||
aabb_buffer_offset: u32,
|
||||
aabb_count: u32,
|
||||
spatial_texture_id: u32,
|
||||
metadata_buffer_id: u32,
|
||||
metadata_buffer_offset: u32,
|
||||
}
|
||||
@@ -143,31 +144,29 @@ const LIGHT_NOT_PRESENT_THIS_FRAME = 0xFFFFFFFFu;
|
||||
|
||||
@group(0) @binding(0) var<storage> vertex_buffers: binding_array<VertexBuffer>;
|
||||
@group(0) @binding(1) var<storage> index_buffers: binding_array<IndexBuffer>;
|
||||
@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(4) var textures: binding_array<texture_2d<f32>>;
|
||||
@group(0) @binding(5) var samplers: binding_array<sampler>;
|
||||
@group(0) @binding(6) var<storage> materials: array<Material>;
|
||||
@group(0) @binding(7) var tlas: acceleration_structure;
|
||||
@group(0) @binding(8) var previous_frame_tlas: acceleration_structure;
|
||||
@group(0) @binding(9) var<storage> transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(10) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(11) var<storage> geometry_ids: array<InstanceGeometryIds>;
|
||||
@group(0) @binding(12)
|
||||
var<storage> tape_ids: array<InstanceTapeIds>;
|
||||
@group(0) @binding(13) var<storage> material_ids: array<u32>;
|
||||
@group(0) @binding(14) var brdf_dfg_lut: texture_2d<f32>;
|
||||
@group(0) @binding(15) var brdf_dfg_lut_sampler: sampler;
|
||||
@group(0) @binding(16) var<storage> light_sources: array<LightSource>;
|
||||
@group(0) @binding(17) var<storage> directional_lights: array<DirectionalLight>;
|
||||
@group(0) @binding(18) var<storage> point_lights: array<PointLight>;
|
||||
@group(0) @binding(19) var<storage> rect_lights: array<RectLight>;
|
||||
@group(0) @binding(20) var environment_map: texture_cube<f32>;
|
||||
@group(0) @binding(21) var environment_map_sampler: sampler;
|
||||
@group(0) @binding(22) var<storage> environment_map_light: EnvironmentMapLight;
|
||||
@group(0) @binding(23) var<storage> previous_frame_light_id_translations: array<u32>;
|
||||
@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(4) var spatial_textures: binding_array<texture_3d<f32>>;
|
||||
@group(0) @binding(5) var textures: binding_array<texture_2d<f32>>;
|
||||
@group(0) @binding(6) var samplers: binding_array<sampler>;
|
||||
@group(0) @binding(7) var<storage> materials: array<Material>;
|
||||
@group(0) @binding(8) var tlas: acceleration_structure;
|
||||
@group(0) @binding(9) var previous_frame_tlas: acceleration_structure;
|
||||
@group(0) @binding(10) var<storage> transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(11) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(12) var<storage> geometry_ids: array<InstanceGeometryIds>;
|
||||
@group(0) @binding(13) var<storage> tape_ids: array<InstanceTapeIds>;
|
||||
@group(0) @binding(14) var<storage> material_ids: array<u32>;
|
||||
@group(0) @binding(15) var brdf_dfg_lut: texture_2d<f32>;
|
||||
@group(0) @binding(16) var brdf_dfg_lut_sampler: sampler;
|
||||
@group(0) @binding(17) var<storage> light_sources: array<LightSource>;
|
||||
@group(0) @binding(18) var<storage> directional_lights: array<DirectionalLight>;
|
||||
@group(0) @binding(19) var<storage> point_lights: array<PointLight>;
|
||||
@group(0) @binding(20) var<storage> rect_lights: array<RectLight>;
|
||||
@group(0) @binding(21) var environment_map: texture_cube<f32>;
|
||||
@group(0) @binding(22) var environment_map_sampler: sampler;
|
||||
@group(0) @binding(23) var<storage> environment_map_light: EnvironmentMapLight;
|
||||
@group(0) @binding(24) var<storage> previous_frame_light_id_translations: array<u32>;
|
||||
|
||||
const RAY_T_MIN = 0.001f;
|
||||
const RAY_T_MAX = 100000.0f;
|
||||
|
||||
@@ -12,16 +12,21 @@ use bevy_ecs::{
|
||||
world::{FromWorld, World},
|
||||
};
|
||||
use bevy_log::warn;
|
||||
use bevy_platform::collections::HashMap;
|
||||
use bevy_render::{
|
||||
GpuResourceAppExt, Render, RenderApp, RenderSystems,
|
||||
render_asset::{ExtractedAssets, prepare_assets},
|
||||
render_resource::Texture,
|
||||
renderer::{RenderDevice, RenderQueue},
|
||||
slab_allocator::{
|
||||
Slab, SlabAllocationBufferSlice, SlabAllocator, SlabAllocatorSettings, SlabId, SlabItem,
|
||||
SlabItemLayout,
|
||||
},
|
||||
};
|
||||
use wgpu::{BufferUsages, COPY_BUFFER_ALIGNMENT};
|
||||
use wgpu::{
|
||||
BufferUsages, COPY_BUFFER_ALIGNMENT, Extent3d, TextureDimension, TextureFormat, TextureUsages,
|
||||
};
|
||||
use wgpu_types::TextureDescriptor;
|
||||
|
||||
use super::{PackedAabb, RenderTape, TapeMetadata};
|
||||
use crate::tape::Tape;
|
||||
@@ -43,6 +48,7 @@ pub struct TapeAllocator {
|
||||
/// Holds all buffers and offset allocators.
|
||||
#[deref]
|
||||
slab_allocator: SlabAllocator<TapeSlabItem>,
|
||||
spatial_texture_allocator: HashMap<AssetId<Tape>, Texture>,
|
||||
}
|
||||
|
||||
/// Tunable parameters that customize the behavior of the allocator.
|
||||
@@ -94,7 +100,7 @@ pub struct TapeSlabs {
|
||||
/// The slab storing the tape's aabb data.
|
||||
pub aabb_slab_id: TapeSlabId,
|
||||
/// The slab storing the tape's metadata.
|
||||
pub metadata_slab_id: Option<TapeSlabId>,
|
||||
pub metadata_slab_id: TapeSlabId,
|
||||
}
|
||||
|
||||
/// Returns the type of buffer that this is: vertex, index, metadata.
|
||||
@@ -192,7 +198,10 @@ impl FromWorld for TapeAllocator {
|
||||
let mut slab_allocator = SlabAllocator::new();
|
||||
slab_allocator.extra_buffer_usages |= tape_allocator_settings.extra_buffer_usages;
|
||||
|
||||
Self { slab_allocator }
|
||||
Self {
|
||||
slab_allocator,
|
||||
spatial_texture_allocator: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,6 +275,13 @@ impl TapeAllocator {
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the spatial texture for the tape with the given ID.
|
||||
///
|
||||
/// If the tape wasn't allocated, returns None.
|
||||
pub fn tape_spatial_texture(&self, tape_id: &AssetId<Tape>) -> Option<&Texture> {
|
||||
self.spatial_texture_allocator.get(tape_id)
|
||||
}
|
||||
|
||||
/// Returns the IDs of the vertex buffer and index buffer respectively for
|
||||
/// the tape with the given ID.
|
||||
///
|
||||
@@ -275,7 +291,7 @@ impl TapeAllocator {
|
||||
pub fn tape_slabs(&self, tape_id: &AssetId<Tape>) -> Option<TapeSlabs> {
|
||||
Some(TapeSlabs {
|
||||
aabb_slab_id: self.tape_id_to_aabb_slab(tape_id).cloned()?,
|
||||
metadata_slab_id: self.tape_id_to_metadata_slab(tape_id).cloned(),
|
||||
metadata_slab_id: self.tape_id_to_metadata_slab(tape_id).cloned()?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -359,6 +375,25 @@ impl TapeAllocator {
|
||||
aabb_element_layout,
|
||||
tape_allocator_settings,
|
||||
);
|
||||
|
||||
// Allocate spatial texture.
|
||||
self.spatial_texture_allocator.insert(
|
||||
*tape_id,
|
||||
render_device.create_texture(&TextureDescriptor {
|
||||
label: Some(&format!("Tape {} spatial texture", tape_id)),
|
||||
format: TextureFormat::R32Float,
|
||||
dimension: TextureDimension::D3,
|
||||
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
view_formats: &[TextureFormat::R32Float],
|
||||
size: Extent3d {
|
||||
width: tape.subdivision,
|
||||
height: tape.subdivision,
|
||||
depth_or_array_layers: tape.subdivision,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Perform growth.
|
||||
@@ -450,6 +485,7 @@ impl TapeAllocator {
|
||||
for tape_id in tapes_to_free {
|
||||
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);
|
||||
}
|
||||
|
||||
deallocation_stage.commit();
|
||||
|
||||
@@ -187,6 +187,8 @@ pub struct ReplacementModules {
|
||||
pub struct RenderTape {
|
||||
pub seed: u64,
|
||||
pub subdivision: u32,
|
||||
pub time: f32,
|
||||
pub key: u64,
|
||||
|
||||
pub enable_gen: Handle<Shader>,
|
||||
pub fuzz: Handle<Shader>,
|
||||
@@ -224,10 +226,29 @@ impl RenderAsset for RenderTape {
|
||||
tape: Self::SourceAsset,
|
||||
_tape_id: AssetId<Self::SourceAsset>,
|
||||
(asset_server, replacement_modules, pipeline_cache): &mut SystemParamItem<Self::Param>,
|
||||
_: Option<&Self>,
|
||||
previous_asset: Option<&Self>,
|
||||
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
|
||||
let key = tape.get_tape_key();
|
||||
|
||||
if let Some(previous) = previous_asset
|
||||
&& previous.key == key
|
||||
{
|
||||
return Ok(RenderTape {
|
||||
seed: tape.seed,
|
||||
subdivision: tape.subdivision,
|
||||
time: tape.time,
|
||||
key,
|
||||
enable_gen: previous.enable_gen.clone(),
|
||||
fuzz: previous.fuzz.clone(),
|
||||
normals: previous.normals.clone(),
|
||||
spatial_texture: previous.spatial_texture.clone(),
|
||||
trace: previous.trace.clone(),
|
||||
enable_gen_pipeline: previous.enable_gen_pipeline,
|
||||
fuzz_pipeline: previous.fuzz_pipeline,
|
||||
spatial_texture_pipeline: previous.spatial_texture_pipeline,
|
||||
});
|
||||
}
|
||||
|
||||
let enable_gen = asset_server.add(tape.sdf_specialize_module_with_key(
|
||||
key,
|
||||
replacement_modules.enable_gen.clone(),
|
||||
@@ -286,6 +307,8 @@ impl RenderAsset for RenderTape {
|
||||
Ok(RenderTape {
|
||||
seed: tape.seed,
|
||||
subdivision: tape.subdivision,
|
||||
time: tape.time,
|
||||
key,
|
||||
enable_gen,
|
||||
fuzz,
|
||||
normals,
|
||||
|
||||
@@ -10,13 +10,14 @@ use rspirv::{binary::Assemble as _, dr::Module};
|
||||
use crate::{render::PackedAabb, ssa::*};
|
||||
|
||||
/// A tape drive tape.
|
||||
#[derive(Asset, Clone, Debug, Reflect, PartialEq, Hash)]
|
||||
#[derive(Asset, Clone, Debug, Reflect, PartialEq)]
|
||||
#[reflect(Clone)]
|
||||
pub struct Tape {
|
||||
pub raytracing: MeshRaytracingFlags,
|
||||
pub parts: SSATape,
|
||||
pub seed: u64,
|
||||
pub subdivision: u32,
|
||||
pub time: f32,
|
||||
}
|
||||
|
||||
impl Default for Tape {
|
||||
@@ -26,10 +27,21 @@ impl Default for Tape {
|
||||
parts: Default::default(),
|
||||
seed: 0,
|
||||
subdivision: 8,
|
||||
time: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Tape {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.raytracing.hash(state);
|
||||
self.parts.hash(state);
|
||||
self.seed.hash(state);
|
||||
self.subdivision.hash(state);
|
||||
self.time.to_bits().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl Tape {
|
||||
pub fn get_aabb(&self, time: f32) -> Aabb3d {
|
||||
let (lowest_corner, highest_corner) = self.parts.get_aabb(time);
|
||||
|
||||
@@ -324,6 +324,7 @@ fn setup_pica_pica(
|
||||
parts: csg,
|
||||
seed: 0,
|
||||
subdivision: 8,
|
||||
time: 0.0,
|
||||
};
|
||||
let loaded_tape = asset_server.add(tape);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user