enable gen running?

This commit is contained in:
2026-10-12 23:00:24 +01:00
parent f799783ee4
commit cb7fdfcea9
8 changed files with 329 additions and 256 deletions
+1 -1
View File
@@ -150,7 +150,7 @@ fn main() {
}
std::fs::create_dir_all("shaders_out").unwrap();
for (shader, entry, stage) in [("enable_gen", "enable_gen_entry", naga::ShaderStage::Compute)] {
for (shader, entry, stage) in [("enable_gen", "main", naga::ShaderStage::Compute)] {
compile_replacement_shader(shader, entry.into(), stage);
}
}
+6 -2
View File
@@ -1,4 +1,8 @@
struct PackedAabb {
min: vec3<f32>,
max: vec3<f32>,
min_x: f32,
min_y: f32,
min_z: f32,
max_x: f32,
max_y: f32,
max_z: f32,
}
+14 -22
View File
@@ -12,24 +12,15 @@ struct TapeMetadataBuffer {
metadata: array<TapeMetadata>,
}
struct PushConstants {
object_index: u32,
aabb_buffer_offset: u32,
metadata_buffer_offset: u32,
time: f32,
}
struct InstanceTapeIds {
aabb_buffer_id: u32,
aabb_buffer_offset: u32,
aabb_count: u32,
metadata_buffer_id: u32,
metadata_buffer_offset: u32,
}
@group(0) @binding(0)
var<storage, read_write> aabb_buffers: binding_array<AabbBuffer, 500>;
var<storage, read_write> aabb_buffer: AabbBuffer;
@group(0) @binding(1)
var<storage> tape_metadata_buffers: binding_array<TapeMetadataBuffer, 500>;
@group(0) @binding(2)
var<storage> tape_ids: array<InstanceTapeIds>;
var<storage> tape_metadata_buffer: TapeMetadataBuffer;
var<immediate> push_constants: PushConstants;
@@ -40,10 +31,8 @@ struct ComputeInput {
}
@compute @workgroup_size(8, 8, 8)
fn enable_gen_entry(compute_input: ComputeInput) {
let instance = tape_ids[push_constants.object_index];
let metadata_buffer = &tape_metadata_buffers[instance.metadata_buffer_id].metadata;
let metadata = &(*metadata_buffer)[instance.metadata_buffer_offset];
fn main(compute_input: ComputeInput) {
let metadata = &tape_metadata_buffer.metadata[push_constants.metadata_buffer_offset];
let total_invocations = compute_input.num_workgroups * vec3u(8);
let base_index =
@@ -78,15 +67,18 @@ fn enable_gen_entry(compute_input: ComputeInput) {
vec4(high_corner, push_constants.time),
);
let aabb_buffer = &aabb_buffers[instance.aabb_buffer_id].aabbs;
let aabb = &(*aabb_buffer)[instance.aabb_buffer_offset];
let aabb = &aabb_buffer.aabbs[push_constants.aabb_buffer_offset + base_index];
let en = (interval.x < 0) && (interval.y > 0);
if en {
aabb.min = low_corner;
aabb.max = high_corner;
aabb.min_x = low_corner.x;
aabb.min_y = low_corner.y;
aabb.min_z = low_corner.z;
aabb.max_x = high_corner.x;
aabb.max_y = high_corner.y;
aabb.max_z = high_corner.z;
} else {
aabb.min.x = NAN();
aabb.min_x = NAN();
}
}
+12 -13
View File
@@ -21,7 +21,6 @@ use bevy_render::{
SlabItemLayout,
},
};
use bevy_shape::BoundingVolume;
use wgpu::{BufferUsages, COPY_BUFFER_ALIGNMENT};
use super::{Aabb, RenderTape, TapeMetadata};
@@ -64,7 +63,7 @@ impl Default for TapeAllocatorSettings {
fn default() -> TapeAllocatorSettings {
TapeAllocatorSettings {
slab_allocator_settings: SlabAllocatorSettings::default(),
extra_buffer_usages: BufferUsages::empty(),
extra_buffer_usages: BufferUsages::empty(),
}
}
}
@@ -93,7 +92,7 @@ impl SlabItem for TapeSlabItem {
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct TapeSlabs {
/// 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.
pub metadata_slab_id: Option<TapeSlabId>,
}
@@ -109,7 +108,7 @@ pub struct TapeAllocationKey {
/// The ID of the tape asset.
pub tape_id: AssetId<Tape>,
/// The type of data: vertex data, index data
pub class: ElementClass,
pub class: ElementClass,
}
impl TapeAllocationKey {
@@ -275,7 +274,7 @@ impl TapeAllocator {
/// None.
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()?,
aabb_slab_id: self.tape_id_to_aabb_slab(tape_id).cloned()?,
metadata_slab_id: self.tape_id_to_metadata_slab(tape_id).cloned(),
})
}
@@ -390,11 +389,11 @@ impl TapeAllocator {
) {
let metadata = {
let aabb = tape.get_aabb(0.0);
let aabb_center = aabb.center().into();
let aabb_half_extents = aabb.half_size().into();
let lowest_corner = aabb.min.into();
let dimensions = (aabb.max - aabb.min).into();
TapeMetadata {
aabb_center,
aabb_half_extents,
lowest_corner,
dimensions,
subdivision_log2: tape.subdivision.ilog2(),
..Default::default()
}
@@ -482,10 +481,10 @@ impl ElementLayout {
/// data.
fn metadata(_tape: &Tape) -> ElementLayout {
ElementLayout {
class: ElementClass::Metadata,
size: size_of::<TapeMetadata>() as u64,
class: ElementClass::Metadata,
size: size_of::<TapeMetadata>() as u64,
elements_per_slot: 1,
buffer_usages: ElementClass::Metadata.buffer_usages(),
buffer_usages: ElementClass::Metadata.buffer_usages(),
}
}
@@ -518,7 +517,7 @@ impl ElementClass {
ElementClass::Metadata => BufferUsages::STORAGE,
ElementClass::Aabb => {
BufferUsages::union(BufferUsages::STORAGE, BufferUsages::BLAS_INPUT)
},
}
}
}
}
+53 -56
View File
@@ -3,8 +3,6 @@
*/
pub mod allocator;
use std::num::NonZeroU32;
use allocator::TapeAllocatorPlugin;
use bevy_app::{App, Plugin};
use bevy_asset::{AssetId, AssetServer, Assets, Handle};
@@ -22,7 +20,7 @@ use bevy_render::{
render_resource::{
BindGroupLayoutDescriptor, BindGroupLayoutEntries, Buffer, CachedComputePipelineId,
ComputePipelineDescriptor, PipelineCache, ShaderType,
binding_types::{storage_buffer_read_only_sized, storage_buffer_sized},
binding_types::{storage_buffer, storage_buffer_read_only},
},
};
use bevy_shader::{Shader, load_shader_library};
@@ -35,6 +33,25 @@ use crate::{
tape::Tape,
};
#[derive(Default, Pod, Zeroable, Clone, Copy, Debug, ShaderType)]
#[repr(C)]
pub struct PackedAabb {
min: Vec3,
max: Vec3,
}
/// Per-tape metadata, stored in [`super::allocator::TapeAllocator`].
/// Currently this is used to decompress vertex.
#[derive(Default, Pod, Zeroable, Clone, Copy, Debug, ShaderType)]
#[repr(C)]
pub struct TapeMetadata {
lowest_corner: Vec3,
pad_a: u32,
dimensions: Vec3,
pad_b: u32,
subdivision_log2: u32,
}
fn get_module(shader: ShaderModuleDescriptorPassthrough) -> Module {
let Some(source) = shader.spirv else {
unreachable!()
@@ -64,32 +81,28 @@ impl Plugin for TapeRenderAssetPlugin {
let enable_gen = include_spirv_raw!("../../shaders_out/enable_gen.orig.spv");
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: 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",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer_sized(false, None).count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None)
.count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None),
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
),
),
),
fuzz_layout: BindGroupLayoutDescriptor::new(
fuzz_layout: BindGroupLayoutDescriptor::new(
"Tape Fuzz Layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer_sized(false, None).count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None)
.count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None),
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
),
),
),
@@ -98,10 +111,8 @@ impl Plugin for TapeRenderAssetPlugin {
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer_sized(false, None).count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None)
.count(NonZeroU32::new(500).unwrap()),
storage_buffer_read_only_sized(false, None),
storage_buffer::<PackedAabb>(false),
storage_buffer_read_only::<TapeMetadata>(false),
),
),
),
@@ -138,7 +149,7 @@ pub struct TapeMetadataFallbackTape(pub Handle<Tape>);
#[derive(Resource)]
pub struct TapeMetadataFallbackBuffer {
pub slab_id: TapeSlabId,
pub buffer: Buffer,
pub buffer: Buffer,
}
pub fn prepare_tape_metadata_fallback_buffer(
@@ -169,45 +180,31 @@ pub struct Aabb {
#[derive(Resource)]
pub struct ReplacementModules {
pub enable_gen: Module,
pub fuzz: Module,
pub normals: Module,
pub enable_gen: Module,
pub fuzz: Module,
pub normals: Module,
pub spatial_texture: Module,
pub trace: Module,
pub trace: Module,
pub enable_gen_layout: BindGroupLayoutDescriptor,
pub fuzz_layout: BindGroupLayoutDescriptor,
pub enable_gen_layout: BindGroupLayoutDescriptor,
pub fuzz_layout: BindGroupLayoutDescriptor,
pub spatial_texture_layout: BindGroupLayoutDescriptor,
}
/// Per-tape metadata, stored in [`super::allocator::TapeAllocator`].
/// Currently this is used to decompress vertex.
#[derive(Default, Pod, Zeroable, Clone, Copy, Debug, ShaderType)]
#[repr(C)]
pub struct TapeMetadata {
// AABB for decompressing positions.
pub aabb_center: Vec3,
pub pad1: u32,
// AABB for decompressing positions.
pub aabb_half_extents: Vec3,
pub pad2: u32,
pub subdivision_log2: u32,
}
/// The render world representation of a [`Tape`].
#[derive(Debug, Clone)]
pub struct RenderTape {
pub seed: u64,
pub seed: u64,
pub subdivision: u32,
pub enable_gen: Handle<Shader>,
pub fuzz: Handle<Shader>,
pub normals: Handle<Shader>,
pub enable_gen: Handle<Shader>,
pub fuzz: Handle<Shader>,
pub normals: Handle<Shader>,
pub spatial_texture: Handle<Shader>,
pub trace: Handle<Shader>,
pub trace: Handle<Shader>,
pub enable_gen_pipeline: CachedComputePipelineId,
pub fuzz_pipeline: CachedComputePipelineId,
pub enable_gen_pipeline: CachedComputePipelineId,
pub fuzz_pipeline: CachedComputePipelineId,
pub spatial_texture_pipeline: CachedComputePipelineId,
}
@@ -271,8 +268,8 @@ impl RenderAsset for RenderTape {
label: Some(format!("bevy_tape_drive::gen_shader::enable_gen::{key}").into()),
layout: vec![replacement_modules.enable_gen_layout.clone()],
shader: enable_gen.clone(),
immediate_size: 8,
entry_point: Some("enable_gen_entry".into()),
immediate_size: 12,
entry_point: Some("main".into()),
..Default::default()
});
@@ -280,8 +277,8 @@ impl RenderAsset for RenderTape {
label: Some(format!("bevy_tape_drive::gen_shader::fuzz::{key}").into()),
layout: vec![replacement_modules.fuzz_layout.clone()],
shader: fuzz.clone(),
immediate_size: 8,
entry_point: Some("enable_gen_entry".into()),
immediate_size: 12,
entry_point: Some("main".into()),
..Default::default()
});
@@ -290,8 +287,8 @@ impl RenderAsset for RenderTape {
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: 8,
entry_point: Some("enable_gen_entry".into()),
immediate_size: 12,
entry_point: Some("main".into()),
..Default::default()
});