Spatial texture creation
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user