Spatial texture writing works

This commit is contained in:
2026-10-20 22:03:16 +01:00
parent 2c05cc28a0
commit 41b27347cb
7 changed files with 154 additions and 180 deletions
+26 -22
View File
@@ -16,7 +16,7 @@ use bevy_platform::collections::HashMap;
use bevy_render::{
GpuResourceAppExt, Render, RenderApp, RenderSystems,
render_asset::{ExtractedAssets, prepare_assets},
render_resource::Texture,
render_resource::{Texture, TextureView},
renderer::{RenderDevice, RenderQueue},
slab_allocator::{
Slab, SlabAllocationBufferSlice, SlabAllocator, SlabAllocatorSettings, SlabId, SlabItem,
@@ -26,7 +26,7 @@ use bevy_render::{
use wgpu::{
BufferUsages, COPY_BUFFER_ALIGNMENT, Extent3d, TextureDimension, TextureFormat, TextureUsages,
};
use wgpu_types::TextureDescriptor;
use wgpu_types::{TextureDescriptor, TextureViewDescriptor};
use super::{PackedAabb, RenderTape, TapeMetadata};
use crate::tape::Tape;
@@ -48,7 +48,7 @@ pub struct TapeAllocator {
/// Holds all buffers and offset allocators.
#[deref]
slab_allocator: SlabAllocator<TapeSlabItem>,
spatial_texture_allocator: HashMap<AssetId<Tape>, Texture>,
spatial_texture_allocator: HashMap<AssetId<Tape>, (Texture, TextureView)>,
}
/// Tunable parameters that customize the behavior of the allocator.
@@ -278,7 +278,7 @@ 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> {
pub fn tape_spatial_texture(&self, tape_id: &AssetId<Tape>) -> Option<&(Texture, TextureView)> {
self.spatial_texture_allocator.get(tape_id)
}
@@ -376,24 +376,28 @@ impl TapeAllocator {
tape_allocator_settings,
);
let texture = 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,
},
});
let texture_view = texture.create_view(&TextureViewDescriptor {
label: Some(&format!("Tape {} spatial texture view", tape_id)),
..Default::default()
});
// 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,
},
}),
);
self.spatial_texture_allocator
.insert(*tape_id, (texture, texture_view));
}
// Perform growth.
@@ -430,7 +434,7 @@ impl TapeAllocator {
lowest_corner,
dimensions,
subdivision_log2: tape.subdivision.ilog2(),
..Default::default()
time: tape.time,
}
};
// Call the generic function.
+13 -70
View File
@@ -19,13 +19,19 @@ use bevy_render::{
render_resource::{
BindGroupLayoutDescriptor, BindGroupLayoutEntries, Buffer, CachedComputePipelineId,
ComputePipelineDescriptor, PipelineCache, ShaderType,
binding_types::{storage_buffer, storage_buffer_read_only},
binding_types::{
storage_buffer, storage_buffer_read_only, texture_3d, texture_storage_2d_array,
texture_storage_3d,
},
},
};
use bevy_shader::{Shader, load_shader_library};
use bytemuck::{Pod, Zeroable};
use rspirv::dr::Module;
use wgpu::{ShaderModuleDescriptorPassthrough, ShaderStages, include_spirv_raw};
use wgpu::{
ShaderModuleDescriptorPassthrough, ShaderStages, StorageTextureAccess, TextureFormat,
TextureSampleType, include_spirv_raw,
};
use crate::{
render::allocator::{ElementClass, TapeAllocationKey, TapeAllocator, TapeSlabId},
@@ -47,7 +53,7 @@ pub struct PackedAabb {
#[repr(C)]
pub struct TapeMetadata {
lowest_corner: Vec3,
pad_a: u32,
time: f32,
dimensions: Vec3,
subdivision_log2: u32,
}
@@ -82,9 +88,7 @@ impl Plugin for TapeRenderAssetPlugin {
let modules = ReplacementModules {
enable_gen: get_module(enable_gen.clone()),
fuzz: get_module(enable_gen.clone()),
normals: get_module(enable_gen.clone()),
spatial_texture: get_module(enable_gen.clone()),
trace: get_module(enable_gen.clone()),
enable_gen_layout: BindGroupLayoutDescriptor::new(
"Enable Gen Layout",
@@ -93,26 +97,10 @@ impl Plugin for TapeRenderAssetPlugin {
(
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
),
),
),
fuzz_layout: BindGroupLayoutDescriptor::new(
"Tape Fuzz Layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
),
),
),
spatial_texture_layout: BindGroupLayoutDescriptor::new(
"Spatial Texture Layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
texture_storage_3d(
TextureFormat::R32Float,
StorageTextureAccess::WriteOnly,
),
),
),
),
@@ -172,14 +160,10 @@ pub fn prepare_tape_metadata_fallback_buffer(
#[derive(Resource)]
pub struct ReplacementModules {
pub enable_gen: Module,
pub fuzz: Module,
pub normals: Module,
pub spatial_texture: Module,
pub trace: Module,
pub enable_gen_layout: BindGroupLayoutDescriptor,
pub fuzz_layout: BindGroupLayoutDescriptor,
pub spatial_texture_layout: BindGroupLayoutDescriptor,
}
/// The render world representation of a [`Tape`].
@@ -191,14 +175,10 @@ pub struct RenderTape {
pub key: u64,
pub enable_gen: Handle<Shader>,
pub fuzz: Handle<Shader>,
pub normals: Handle<Shader>,
pub spatial_texture: Handle<Shader>,
pub trace: Handle<Shader>,
pub enable_gen_pipeline: CachedComputePipelineId,
pub fuzz_pipeline: CachedComputePipelineId,
pub spatial_texture_pipeline: CachedComputePipelineId,
}
impl RenderAsset for RenderTape {
@@ -239,13 +219,9 @@ impl RenderAsset for RenderTape {
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,
});
}
@@ -254,21 +230,11 @@ impl RenderAsset for RenderTape {
replacement_modules.enable_gen.clone(),
"enable_gen",
));
let fuzz = asset_server.add(tape.sdf_specialize_module_with_key(
key,
replacement_modules.fuzz.clone(),
"fuzz",
));
let normals = asset_server.add(tape.sdf_specialize_module_with_key(
key,
replacement_modules.normals.clone(),
"normals",
));
let spatial_texture = asset_server.add(tape.sdf_specialize_module_with_key(
key,
replacement_modules.spatial_texture.clone(),
"spatial_texture",
));
let trace = asset_server.add(tape.sdf_specialize_module_with_key(
key,
replacement_modules.trace.clone(),
@@ -285,38 +251,15 @@ impl RenderAsset for RenderTape {
..Default::default()
});
let fuzz_pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
label: Some(format!("bevy_tape_drive::gen_shader::fuzz::{key}").into()),
layout: vec![replacement_modules.fuzz_layout.clone()],
shader: fuzz.clone(),
immediate_size: 12,
entry_point: Some("main".into()),
..Default::default()
});
let spatial_texture_pipeline =
pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
label: Some(format!("bevy_tape_drive::gen_shader::spatial_texture::{key}").into()),
layout: vec![replacement_modules.spatial_texture_layout.clone()],
shader: spatial_texture.clone(),
immediate_size: 12,
entry_point: Some("main".into()),
..Default::default()
});
Ok(RenderTape {
seed: tape.seed,
subdivision: tape.subdivision,
time: tape.time,
key,
enable_gen,
fuzz,
normals,
spatial_texture,
trace,
enable_gen_pipeline,
fuzz_pipeline,
spatial_texture_pipeline,
})
}