Spatial texture creation
This commit is contained in:
Generated
+1
@@ -1678,6 +1678,7 @@ dependencies = [
|
|||||||
"derive_more",
|
"derive_more",
|
||||||
"tracing",
|
"tracing",
|
||||||
"wgpu",
|
"wgpu",
|
||||||
|
"wgpu-types",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ bytemuck = { version = "1" }
|
|||||||
derive_more = { version = "2", default-features = false, features = ["from"] }
|
derive_more = { version = "2", default-features = false, features = ["from"] }
|
||||||
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
||||||
wgpu = { version = "30", default-features = false }
|
wgpu = { version = "30", default-features = false }
|
||||||
|
wgpu-types = { version = "30", default-features = false }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
dlss = ["bevy_anti_alias/dlss"]
|
dlss = ["bevy_anti_alias/dlss"]
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ impl RaytracingSceneBindings {
|
|||||||
&mut self.instances.index_buffers.dirty,
|
&mut self.instances.index_buffers.dirty,
|
||||||
&mut self.instances.aabb_buffers.dirty,
|
&mut self.instances.aabb_buffers.dirty,
|
||||||
&mut self.instances.tape_metadata_buffers.dirty,
|
&mut self.instances.tape_metadata_buffers.dirty,
|
||||||
|
&mut self.instances.spatial_textures.dirty,
|
||||||
&mut self.assets.textures.dirty,
|
&mut self.assets.textures.dirty,
|
||||||
] {
|
] {
|
||||||
invalid |= core::mem::replace(dirty, false);
|
invalid |= core::mem::replace(dirty, false);
|
||||||
@@ -159,6 +160,19 @@ impl RaytracingSceneBindings {
|
|||||||
return None;
|
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
|
let (mut textures, mut samplers): (Vec<_>, Vec<_>) = self
|
||||||
.assets
|
.assets
|
||||||
.textures
|
.textures
|
||||||
@@ -199,6 +213,7 @@ impl RaytracingSceneBindings {
|
|||||||
index_buffers.as_slice(),
|
index_buffers.as_slice(),
|
||||||
aabb_buffers.as_slice(),
|
aabb_buffers.as_slice(),
|
||||||
tape_metadata_buffers.as_slice(),
|
tape_metadata_buffers.as_slice(),
|
||||||
|
spatial_textures.as_slice(),
|
||||||
textures.as_slice(),
|
textures.as_slice(),
|
||||||
samplers.as_slice(),
|
samplers.as_slice(),
|
||||||
self.assets.materials.binding().unwrap(),
|
self.assets.materials.binding().unwrap(),
|
||||||
|
|||||||
@@ -13,13 +13,16 @@ use bevy_platform::collections::HashMap;
|
|||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
impl_atomic_pod,
|
impl_atomic_pod,
|
||||||
mesh::allocator::MeshAllocator,
|
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_tape_load::{Tape, render::allocator::TapeAllocator};
|
||||||
use bevy_transform::components::GlobalTransform;
|
use bevy_transform::components::GlobalTransform;
|
||||||
use bevy_utils::once;
|
use bevy_utils::once;
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
use tracing::{info_span, warn};
|
use tracing::{info_span, warn};
|
||||||
|
use wgpu_types::TextureViewDescriptor;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
MeshBlasKey, MeshBlasManager, MeshBlasOpacity, RaytracingMesh3d, RaytracingSceneBindings,
|
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_MESH_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap();
|
||||||
pub const MAX_TAPE_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)]
|
#[derive(Clone, Copy, Default, PartialEq, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@@ -48,6 +52,7 @@ pub struct GpuInstanceTapeIds {
|
|||||||
aabb_buffer_id: u32,
|
aabb_buffer_id: u32,
|
||||||
aabb_buffer_offset: u32,
|
aabb_buffer_offset: u32,
|
||||||
aabb_count: u32,
|
aabb_count: u32,
|
||||||
|
spatial_texture_id: u32,
|
||||||
metadata_buffer_id: u32,
|
metadata_buffer_id: u32,
|
||||||
metadata_buffer_offset: u32,
|
metadata_buffer_offset: u32,
|
||||||
}
|
}
|
||||||
@@ -97,6 +102,30 @@ pub enum BlasKey {
|
|||||||
Tape(TapeBlasKey),
|
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.
|
/// Everything tracked per raytracing instance.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
struct MeshInstance {
|
struct MeshInstance {
|
||||||
@@ -104,7 +133,7 @@ struct MeshInstance {
|
|||||||
mesh: AssetId<Mesh>,
|
mesh: AssetId<Mesh>,
|
||||||
material: AssetId<StandardMaterial>,
|
material: AssetId<StandardMaterial>,
|
||||||
opacity: MeshBlasOpacity,
|
opacity: MeshBlasOpacity,
|
||||||
buffers: Option<(BufferId, BufferId)>,
|
buffers: Option<MeshBuffers>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Everything tracked per raytracing instance.
|
/// Everything tracked per raytracing instance.
|
||||||
@@ -114,7 +143,7 @@ struct TapeInstance {
|
|||||||
tape: AssetId<Tape>,
|
tape: AssetId<Tape>,
|
||||||
material: AssetId<StandardMaterial>,
|
material: AssetId<StandardMaterial>,
|
||||||
opacity: TapeBlasOpacity,
|
opacity: TapeBlasOpacity,
|
||||||
buffers: Option<(BufferId, BufferId)>,
|
buffers: Option<TapeBuffers>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MeshInstance {
|
impl MeshInstance {
|
||||||
@@ -163,10 +192,10 @@ impl Instance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_buffers(&self) -> Option<(BufferId, BufferId)> {
|
fn get_buffers(&self) -> Option<Buffers> {
|
||||||
match self {
|
match self {
|
||||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers,
|
Instance::Mesh(mesh_instance) => mesh_instance.buffers.map(|b| Buffers::Mesh(b)),
|
||||||
Instance::Tape(tape_instance) => tape_instance.buffers,
|
Instance::Tape(tape_instance) => tape_instance.buffers.map(|b| Buffers::Tape(b)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +210,7 @@ impl Instance {
|
|||||||
slot: u32,
|
slot: u32,
|
||||||
object: Object,
|
object: Object,
|
||||||
material: AssetId<StandardMaterial>,
|
material: AssetId<StandardMaterial>,
|
||||||
buffers: Option<(BufferId, BufferId)>,
|
buffers: Option<Buffers>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
match object {
|
match object {
|
||||||
Object::Mesh(mesh) => Instance::Mesh(MeshInstance {
|
Object::Mesh(mesh) => Instance::Mesh(MeshInstance {
|
||||||
@@ -189,25 +218,18 @@ impl Instance {
|
|||||||
mesh,
|
mesh,
|
||||||
material,
|
material,
|
||||||
opacity: MeshBlasOpacity::Opaque,
|
opacity: MeshBlasOpacity::Opaque,
|
||||||
buffers,
|
buffers: buffers.map(Buffers::as_mesh),
|
||||||
}),
|
}),
|
||||||
Object::Tape(tape) => Instance::Tape(TapeInstance {
|
Object::Tape(tape) => Instance::Tape(TapeInstance {
|
||||||
slot,
|
slot,
|
||||||
tape,
|
tape,
|
||||||
material,
|
material,
|
||||||
opacity: TapeBlasOpacity::Opaque,
|
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) {
|
fn set_slot(&mut self, slot: u32) {
|
||||||
match self {
|
match self {
|
||||||
Instance::Mesh(mesh_instance) => mesh_instance.slot = slot,
|
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 {
|
match self {
|
||||||
Instance::Mesh(mesh_instance) => mesh_instance.buffers = buffers,
|
Instance::Mesh(mesh_instance) => mesh_instance.buffers = buffers.map(Buffers::as_mesh),
|
||||||
Instance::Tape(tape_instance) => tape_instance.buffers = buffers,
|
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())
|
(self.get_slot(), self.get_material(), self.get_buffers())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,6 +263,7 @@ pub struct InstanceState {
|
|||||||
pub index_buffers: RetainedBindingArray<BufferId, Buffer>,
|
pub index_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||||
pub aabb_buffers: RetainedBindingArray<BufferId, Buffer>,
|
pub aabb_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||||
pub tape_metadata_buffers: RetainedBindingArray<BufferId, Buffer>,
|
pub tape_metadata_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||||
|
pub spatial_textures: RetainedBindingArray<TextureId, TextureView>,
|
||||||
pub transforms: AtomicSparseBufferVec<GpuTransform>,
|
pub transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||||
pub previous_frame_transforms: AtomicSparseBufferVec<GpuTransform>,
|
pub previous_frame_transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||||
pub geometry_ids: AtomicSparseBufferVec<GpuInstanceGeometryIds>,
|
pub geometry_ids: AtomicSparseBufferVec<GpuInstanceGeometryIds>,
|
||||||
@@ -250,8 +273,8 @@ pub struct InstanceState {
|
|||||||
pub slots: IndexAllocator,
|
pub slots: IndexAllocator,
|
||||||
/// Slab buffer references dropped this frame, released next frame since the
|
/// Slab buffer references dropped this frame, released next frame since the
|
||||||
/// previous frame's TLAS can still reach them through `geometry_ids`.
|
/// previous frame's TLAS can still reach them through `geometry_ids`.
|
||||||
retired_mesh_buffers: Vec<(BufferId, BufferId)>,
|
retired_mesh_buffers: Vec<MeshBuffers>,
|
||||||
retired_tape_buffers: Vec<(BufferId, BufferId)>,
|
retired_tape_buffers: Vec<TapeBuffers>,
|
||||||
records: EntityHashMap<Instance>,
|
records: EntityHashMap<Instance>,
|
||||||
pub live_count: u32,
|
pub live_count: u32,
|
||||||
pub pending_refresh: EntityHashSet,
|
pub pending_refresh: EntityHashSet,
|
||||||
@@ -266,6 +289,7 @@ impl InstanceState {
|
|||||||
index_buffers: RetainedBindingArray::new(),
|
index_buffers: RetainedBindingArray::new(),
|
||||||
aabb_buffers: RetainedBindingArray::new(),
|
aabb_buffers: RetainedBindingArray::new(),
|
||||||
tape_metadata_buffers: RetainedBindingArray::new(),
|
tape_metadata_buffers: RetainedBindingArray::new(),
|
||||||
|
spatial_textures: RetainedBindingArray::new(),
|
||||||
transforms: storage_buffer("solari_transforms"),
|
transforms: storage_buffer("solari_transforms"),
|
||||||
previous_frame_transforms: storage_buffer("solari_previous_frame_transforms"),
|
previous_frame_transforms: storage_buffer("solari_previous_frame_transforms"),
|
||||||
geometry_ids: storage_buffer("solari_geometry_ids"),
|
geometry_ids: storage_buffer("solari_geometry_ids"),
|
||||||
@@ -380,9 +404,11 @@ impl InstanceState {
|
|||||||
self.vertex_buffers.release(&vertex_key);
|
self.vertex_buffers.release(&vertex_key);
|
||||||
self.index_buffers.release(&index_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.aabb_buffers.release(&aabb_key);
|
||||||
self.tape_metadata_buffers.release(&metadata_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_aabb_slice(&instance.tape),
|
||||||
inputs.tape_allocator.tape_metadata_slice(&instance.tape),
|
inputs.tape_allocator.tape_metadata_slice(&instance.tape),
|
||||||
|
inputs.tape_allocator.tape_spatial_texture(&instance.tape),
|
||||||
material_slot,
|
material_slot,
|
||||||
blas_address,
|
blas_address,
|
||||||
) else {
|
)
|
||||||
|
else {
|
||||||
bevy_log::info!("Deactivating tape instance {}", instance.tape);
|
bevy_log::info!("Deactivating tape instance {}", instance.tape);
|
||||||
self.deactivate_tape_instance(lights, entity, instance);
|
self.deactivate_tape_instance(lights, entity, instance);
|
||||||
return false;
|
return false;
|
||||||
@@ -711,6 +745,21 @@ impl InstanceState {
|
|||||||
return false;
|
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 previous_buffers = instance.buffers.take();
|
||||||
let aabb_buffer_id = self
|
let aabb_buffer_id = self
|
||||||
.aabb_buffers
|
.aabb_buffers
|
||||||
@@ -722,7 +771,19 @@ impl InstanceState {
|
|||||||
metadata_slice.buffer.clone()
|
metadata_slice.buffer.clone()
|
||||||
})
|
})
|
||||||
.expect("tape metadata slab binding array had room but handed out no slot");
|
.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);
|
self.release_tape_buffers(previous_buffers);
|
||||||
|
|
||||||
let aabb_count = (aabb_slice.range.len()) as u32;
|
let aabb_count = (aabb_slice.range.len()) as u32;
|
||||||
@@ -732,6 +793,7 @@ impl InstanceState {
|
|||||||
aabb_buffer_id,
|
aabb_buffer_id,
|
||||||
aabb_buffer_offset: aabb_slice.range.start,
|
aabb_buffer_offset: aabb_slice.range.start,
|
||||||
aabb_count,
|
aabb_count,
|
||||||
|
spatial_texture_id,
|
||||||
metadata_buffer_id,
|
metadata_buffer_id,
|
||||||
metadata_buffer_offset: metadata_slice.range.start,
|
metadata_buffer_offset: metadata_slice.range.start,
|
||||||
},
|
},
|
||||||
@@ -822,11 +884,11 @@ impl InstanceState {
|
|||||||
self.release_tape_buffers(instance.buffers.take());
|
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);
|
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);
|
self.retired_tape_buffers.extend(buffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ use super::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
use crate::scene::{
|
use crate::scene::{
|
||||||
binder::instances::MAX_TAPE_SLAB_COUNT,
|
binder::instances::{MAX_SPATIAL_TEXTURE_COUNT, MAX_TAPE_SLAB_COUNT},
|
||||||
extract::{
|
extract::{
|
||||||
ExtractedRaytracingDirectionalLight, ExtractedRaytracingPointLight,
|
ExtractedRaytracingDirectionalLight, ExtractedRaytracingPointLight,
|
||||||
ExtractedRaytracingRectLight, ExtractedRaytracingSpotLight,
|
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_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),
|
||||||
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 })
|
texture_2d(TextureSampleType::Float { filterable: true })
|
||||||
.count(MAX_TEXTURE_COUNT),
|
.count(MAX_TEXTURE_COUNT),
|
||||||
sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT),
|
sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT),
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ struct InstanceTapeIds {
|
|||||||
aabb_buffer_id: u32,
|
aabb_buffer_id: u32,
|
||||||
aabb_buffer_offset: u32,
|
aabb_buffer_offset: u32,
|
||||||
aabb_count: u32,
|
aabb_count: u32,
|
||||||
|
spatial_texture_id: u32,
|
||||||
metadata_buffer_id: u32,
|
metadata_buffer_id: u32,
|
||||||
metadata_buffer_offset: 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(0) var<storage> vertex_buffers: binding_array<VertexBuffer>;
|
||||||
@group(0) @binding(1) var<storage> index_buffers: binding_array<IndexBuffer>;
|
@group(0) @binding(1) var<storage> index_buffers: binding_array<IndexBuffer>;
|
||||||
@group(0) @binding(2)
|
@group(0) @binding(2) var<storage> aabb_buffers: binding_array<AabbBuffer>;
|
||||||
var<storage> aabb_buffers: binding_array<AabbBuffer>;
|
@group(0) @binding(3) var<storage> tape_metadata_buffers: binding_array<TapeMetadataBuffer>;
|
||||||
@group(0) @binding(3)
|
@group(0) @binding(4) var spatial_textures: binding_array<texture_3d<f32>>;
|
||||||
var<storage> tape_metadata_buffers: binding_array<TapeMetadataBuffer>;
|
@group(0) @binding(5) var textures: binding_array<texture_2d<f32>>;
|
||||||
@group(0) @binding(4) var textures: binding_array<texture_2d<f32>>;
|
@group(0) @binding(6) var samplers: binding_array<sampler>;
|
||||||
@group(0) @binding(5) var samplers: binding_array<sampler>;
|
@group(0) @binding(7) var<storage> materials: array<Material>;
|
||||||
@group(0) @binding(6) var<storage> materials: array<Material>;
|
@group(0) @binding(8) var tlas: acceleration_structure;
|
||||||
@group(0) @binding(7) var tlas: acceleration_structure;
|
@group(0) @binding(9) var previous_frame_tlas: acceleration_structure;
|
||||||
@group(0) @binding(8) var previous_frame_tlas: acceleration_structure;
|
@group(0) @binding(10) var<storage> transforms: array<mat3x4<f32>>;
|
||||||
@group(0) @binding(9) var<storage> transforms: array<mat3x4<f32>>;
|
@group(0) @binding(11) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
||||||
@group(0) @binding(10) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
@group(0) @binding(12) var<storage> geometry_ids: array<InstanceGeometryIds>;
|
||||||
@group(0) @binding(11) var<storage> geometry_ids: array<InstanceGeometryIds>;
|
@group(0) @binding(13) var<storage> tape_ids: array<InstanceTapeIds>;
|
||||||
@group(0) @binding(12)
|
@group(0) @binding(14) var<storage> material_ids: array<u32>;
|
||||||
var<storage> tape_ids: array<InstanceTapeIds>;
|
@group(0) @binding(15) var brdf_dfg_lut: texture_2d<f32>;
|
||||||
@group(0) @binding(13) var<storage> material_ids: array<u32>;
|
@group(0) @binding(16) var brdf_dfg_lut_sampler: sampler;
|
||||||
@group(0) @binding(14) var brdf_dfg_lut: texture_2d<f32>;
|
@group(0) @binding(17) var<storage> light_sources: array<LightSource>;
|
||||||
@group(0) @binding(15) var brdf_dfg_lut_sampler: sampler;
|
@group(0) @binding(18) var<storage> directional_lights: array<DirectionalLight>;
|
||||||
@group(0) @binding(16) var<storage> light_sources: array<LightSource>;
|
@group(0) @binding(19) var<storage> point_lights: array<PointLight>;
|
||||||
@group(0) @binding(17) var<storage> directional_lights: array<DirectionalLight>;
|
@group(0) @binding(20) var<storage> rect_lights: array<RectLight>;
|
||||||
@group(0) @binding(18) var<storage> point_lights: array<PointLight>;
|
@group(0) @binding(21) var environment_map: texture_cube<f32>;
|
||||||
@group(0) @binding(19) var<storage> rect_lights: array<RectLight>;
|
@group(0) @binding(22) var environment_map_sampler: sampler;
|
||||||
@group(0) @binding(20) var environment_map: texture_cube<f32>;
|
@group(0) @binding(23) var<storage> environment_map_light: EnvironmentMapLight;
|
||||||
@group(0) @binding(21) var environment_map_sampler: sampler;
|
@group(0) @binding(24) var<storage> previous_frame_light_id_translations: array<u32>;
|
||||||
@group(0) @binding(22) var<storage> environment_map_light: EnvironmentMapLight;
|
|
||||||
@group(0) @binding(23) var<storage> previous_frame_light_id_translations: array<u32>;
|
|
||||||
|
|
||||||
const RAY_T_MIN = 0.001f;
|
const RAY_T_MIN = 0.001f;
|
||||||
const RAY_T_MAX = 100000.0f;
|
const RAY_T_MAX = 100000.0f;
|
||||||
|
|||||||
@@ -12,16 +12,21 @@ use bevy_ecs::{
|
|||||||
world::{FromWorld, World},
|
world::{FromWorld, World},
|
||||||
};
|
};
|
||||||
use bevy_log::warn;
|
use bevy_log::warn;
|
||||||
|
use bevy_platform::collections::HashMap;
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
GpuResourceAppExt, Render, RenderApp, RenderSystems,
|
GpuResourceAppExt, Render, RenderApp, RenderSystems,
|
||||||
render_asset::{ExtractedAssets, prepare_assets},
|
render_asset::{ExtractedAssets, prepare_assets},
|
||||||
|
render_resource::Texture,
|
||||||
renderer::{RenderDevice, RenderQueue},
|
renderer::{RenderDevice, RenderQueue},
|
||||||
slab_allocator::{
|
slab_allocator::{
|
||||||
Slab, SlabAllocationBufferSlice, SlabAllocator, SlabAllocatorSettings, SlabId, SlabItem,
|
Slab, SlabAllocationBufferSlice, SlabAllocator, SlabAllocatorSettings, SlabId, SlabItem,
|
||||||
SlabItemLayout,
|
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 super::{PackedAabb, RenderTape, TapeMetadata};
|
||||||
use crate::tape::Tape;
|
use crate::tape::Tape;
|
||||||
@@ -43,6 +48,7 @@ pub struct TapeAllocator {
|
|||||||
/// Holds all buffers and offset allocators.
|
/// Holds all buffers and offset allocators.
|
||||||
#[deref]
|
#[deref]
|
||||||
slab_allocator: SlabAllocator<TapeSlabItem>,
|
slab_allocator: SlabAllocator<TapeSlabItem>,
|
||||||
|
spatial_texture_allocator: HashMap<AssetId<Tape>, Texture>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tunable parameters that customize the behavior of the allocator.
|
/// Tunable parameters that customize the behavior of the allocator.
|
||||||
@@ -94,7 +100,7 @@ pub struct TapeSlabs {
|
|||||||
/// The slab storing the tape's aabb data.
|
/// The slab storing the tape's aabb data.
|
||||||
pub aabb_slab_id: TapeSlabId,
|
pub aabb_slab_id: TapeSlabId,
|
||||||
/// The slab storing the tape's metadata.
|
/// 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.
|
/// 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();
|
let mut slab_allocator = SlabAllocator::new();
|
||||||
slab_allocator.extra_buffer_usages |= tape_allocator_settings.extra_buffer_usages;
|
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
|
/// Returns the IDs of the vertex buffer and index buffer respectively for
|
||||||
/// the tape with the given ID.
|
/// the tape with the given ID.
|
||||||
///
|
///
|
||||||
@@ -275,7 +291,7 @@ impl TapeAllocator {
|
|||||||
pub fn tape_slabs(&self, tape_id: &AssetId<Tape>) -> Option<TapeSlabs> {
|
pub fn tape_slabs(&self, tape_id: &AssetId<Tape>) -> Option<TapeSlabs> {
|
||||||
Some(TapeSlabs {
|
Some(TapeSlabs {
|
||||||
aabb_slab_id: self.tape_id_to_aabb_slab(tape_id).cloned()?,
|
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,
|
aabb_element_layout,
|
||||||
tape_allocator_settings,
|
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.
|
// Perform growth.
|
||||||
@@ -450,6 +485,7 @@ impl TapeAllocator {
|
|||||||
for tape_id in tapes_to_free {
|
for tape_id in tapes_to_free {
|
||||||
deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Metadata));
|
deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Metadata));
|
||||||
deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Aabb));
|
deallocation_stage.free(&TapeAllocationKey::new(*tape_id, ElementClass::Aabb));
|
||||||
|
self.spatial_texture_allocator.remove(tape_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
deallocation_stage.commit();
|
deallocation_stage.commit();
|
||||||
|
|||||||
@@ -187,6 +187,8 @@ pub struct ReplacementModules {
|
|||||||
pub struct RenderTape {
|
pub struct RenderTape {
|
||||||
pub seed: u64,
|
pub seed: u64,
|
||||||
pub subdivision: u32,
|
pub subdivision: u32,
|
||||||
|
pub time: f32,
|
||||||
|
pub key: u64,
|
||||||
|
|
||||||
pub enable_gen: Handle<Shader>,
|
pub enable_gen: Handle<Shader>,
|
||||||
pub fuzz: Handle<Shader>,
|
pub fuzz: Handle<Shader>,
|
||||||
@@ -224,10 +226,29 @@ impl RenderAsset for RenderTape {
|
|||||||
tape: Self::SourceAsset,
|
tape: Self::SourceAsset,
|
||||||
_tape_id: AssetId<Self::SourceAsset>,
|
_tape_id: AssetId<Self::SourceAsset>,
|
||||||
(asset_server, replacement_modules, pipeline_cache): &mut SystemParamItem<Self::Param>,
|
(asset_server, replacement_modules, pipeline_cache): &mut SystemParamItem<Self::Param>,
|
||||||
_: Option<&Self>,
|
previous_asset: Option<&Self>,
|
||||||
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
|
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
|
||||||
let key = tape.get_tape_key();
|
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(
|
let enable_gen = asset_server.add(tape.sdf_specialize_module_with_key(
|
||||||
key,
|
key,
|
||||||
replacement_modules.enable_gen.clone(),
|
replacement_modules.enable_gen.clone(),
|
||||||
@@ -286,6 +307,8 @@ impl RenderAsset for RenderTape {
|
|||||||
Ok(RenderTape {
|
Ok(RenderTape {
|
||||||
seed: tape.seed,
|
seed: tape.seed,
|
||||||
subdivision: tape.subdivision,
|
subdivision: tape.subdivision,
|
||||||
|
time: tape.time,
|
||||||
|
key,
|
||||||
enable_gen,
|
enable_gen,
|
||||||
fuzz,
|
fuzz,
|
||||||
normals,
|
normals,
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ use rspirv::{binary::Assemble as _, dr::Module};
|
|||||||
use crate::{render::PackedAabb, ssa::*};
|
use crate::{render::PackedAabb, ssa::*};
|
||||||
|
|
||||||
/// A tape drive tape.
|
/// A tape drive tape.
|
||||||
#[derive(Asset, Clone, Debug, Reflect, PartialEq, Hash)]
|
#[derive(Asset, Clone, Debug, Reflect, PartialEq)]
|
||||||
#[reflect(Clone)]
|
#[reflect(Clone)]
|
||||||
pub struct Tape {
|
pub struct Tape {
|
||||||
pub raytracing: MeshRaytracingFlags,
|
pub raytracing: MeshRaytracingFlags,
|
||||||
pub parts: SSATape,
|
pub parts: SSATape,
|
||||||
pub seed: u64,
|
pub seed: u64,
|
||||||
pub subdivision: u32,
|
pub subdivision: u32,
|
||||||
|
pub time: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Tape {
|
impl Default for Tape {
|
||||||
@@ -26,10 +27,21 @@ impl Default for Tape {
|
|||||||
parts: Default::default(),
|
parts: Default::default(),
|
||||||
seed: 0,
|
seed: 0,
|
||||||
subdivision: 8,
|
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 {
|
impl Tape {
|
||||||
pub fn get_aabb(&self, time: f32) -> Aabb3d {
|
pub fn get_aabb(&self, time: f32) -> Aabb3d {
|
||||||
let (lowest_corner, highest_corner) = self.parts.get_aabb(time);
|
let (lowest_corner, highest_corner) = self.parts.get_aabb(time);
|
||||||
|
|||||||
@@ -324,6 +324,7 @@ fn setup_pica_pica(
|
|||||||
parts: csg,
|
parts: csg,
|
||||||
seed: 0,
|
seed: 0,
|
||||||
subdivision: 8,
|
subdivision: 8,
|
||||||
|
time: 0.0,
|
||||||
};
|
};
|
||||||
let loaded_tape = asset_server.add(tape);
|
let loaded_tape = asset_server.add(tape);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user