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