Spatial texture creation

This commit is contained in:
2026-10-20 21:13:28 +01:00
parent dcc59b04dd
commit 2c05cc28a0
10 changed files with 212 additions and 60 deletions
+40 -4
View File
@@ -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();
+24 -1
View File
@@ -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,