split into two crates
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
/**
|
||||
* Vaguely analagous to bevy_render::mesh
|
||||
*/
|
||||
pub mod allocator;
|
||||
|
||||
use allocator::TapeAllocatorPlugin;
|
||||
use bevy_app::{App, Plugin};
|
||||
use bevy_asset::{AssetId, AssetServer, Assets, Handle};
|
||||
use bevy_ecs::{
|
||||
prelude::*,
|
||||
system::{SystemParamItem, lifetimeless::SRes},
|
||||
};
|
||||
use bevy_math::Vec3;
|
||||
use bevy_reflect::Reflect;
|
||||
use bevy_render::{
|
||||
Render, RenderApp, RenderSystems,
|
||||
render_asset::{
|
||||
AssetExtractionError, PrepareAssetError, RenderAsset, RenderAssetPlugin, prepare_assets,
|
||||
},
|
||||
render_resource::{
|
||||
BindGroupLayoutDescriptor, Buffer, CachedComputePipelineId, ComputePipelineDescriptor,
|
||||
PipelineCache, ShaderType,
|
||||
},
|
||||
};
|
||||
use bevy_shader::{Shader, load_shader_library};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use rspirv::dr::Module;
|
||||
|
||||
use crate::{
|
||||
render::allocator::{ElementClass, TapeAllocationKey, TapeAllocator, TapeSlabId},
|
||||
tape::Tape,
|
||||
};
|
||||
|
||||
/// Makes sure that [`Tape`]es are extracted and prepared for the GPU.
|
||||
/// Does *not* add the [`Tape`] as an asset. Use [`TapePlugin`] for that.
|
||||
pub struct TapeRenderAssetPlugin;
|
||||
|
||||
impl Plugin for TapeRenderAssetPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
load_shader_library!(app, "metadata_types.wesl");
|
||||
|
||||
app.add_plugins(RenderAssetPlugin::<RenderTape>::default())
|
||||
.add_plugins(TapeAllocatorPlugin);
|
||||
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
|
||||
render_app.add_systems(
|
||||
Render,
|
||||
prepare_tape_metadata_fallback_buffer
|
||||
.in_set(RenderSystems::PrepareAssets)
|
||||
.after(prepare_assets::<RenderTape>),
|
||||
);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
let mut tape_assets = app.world_mut().resource_mut::<Assets<Tape>>();
|
||||
let handle =
|
||||
tape_assets.add(crate::primitives::sphere::SphereTapeBuilder::build_with_radius(1.0));
|
||||
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
|
||||
render_app.insert_resource(TapeMetadataFallbackTape(handle));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct TapeMetadataFallbackTape(pub Handle<Tape>);
|
||||
|
||||
/// Metadata slab ID and buffer of [`TapeMetadataFallbackTape`],
|
||||
/// used to fill bind group for tape without metadata.
|
||||
#[derive(Resource)]
|
||||
pub struct TapeMetadataFallbackBuffer {
|
||||
pub slab_id: TapeSlabId,
|
||||
pub buffer: Buffer,
|
||||
}
|
||||
|
||||
pub fn prepare_tape_metadata_fallback_buffer(
|
||||
mut commands: Commands,
|
||||
tape_allocator: Res<TapeAllocator>,
|
||||
metadata_fallback_tape: Res<TapeMetadataFallbackTape>,
|
||||
) {
|
||||
let slab_id = tape_allocator
|
||||
.key_to_slab
|
||||
.get(&TapeAllocationKey::new(
|
||||
metadata_fallback_tape.0.id(),
|
||||
ElementClass::Metadata,
|
||||
))
|
||||
.cloned()
|
||||
.unwrap();
|
||||
let buffer = tape_allocator.buffer_for_slab(slab_id).unwrap().clone();
|
||||
commands.insert_resource(TapeMetadataFallbackBuffer { slab_id, buffer });
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Reflect)]
|
||||
#[reflect(Debug, PartialEq, Clone)]
|
||||
pub struct Aabb {
|
||||
/// The minimum point of the box
|
||||
pub min: Vec3,
|
||||
/// The maximum point of the box
|
||||
pub max: Vec3,
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
|
||||
/// The render world representation of a [`Tape`].
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RenderTape {
|
||||
pub seed: u64,
|
||||
pub subdivision: u32,
|
||||
|
||||
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 {
|
||||
type Param = (
|
||||
SRes<AssetServer>,
|
||||
SRes<ReplacementModules>,
|
||||
SRes<PipelineCache>,
|
||||
);
|
||||
type SourceAsset = Tape;
|
||||
|
||||
fn take_gpu_data(
|
||||
source: &mut Self::SourceAsset,
|
||||
_previous_gpu_asset: Option<&Self>,
|
||||
) -> Result<Self::SourceAsset, AssetExtractionError> {
|
||||
Ok(source.clone())
|
||||
}
|
||||
|
||||
fn byte_len(_tape: &Self::SourceAsset) -> Option<usize> {
|
||||
// Some(0) // Is this really correct?
|
||||
None
|
||||
}
|
||||
|
||||
/// Converts the extracted tape into a [`RenderTape`].
|
||||
fn prepare_asset(
|
||||
tape: Self::SourceAsset,
|
||||
_tape_id: AssetId<Self::SourceAsset>,
|
||||
(asset_server, replacement_modules, pipeline_cache): &mut SystemParamItem<Self::Param>,
|
||||
_: Option<&Self>,
|
||||
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
|
||||
let key = tape.get_tape_key();
|
||||
|
||||
let enable_gen = asset_server.add(tape.sdf_specialize_module_with_key(
|
||||
key,
|
||||
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(),
|
||||
"trace",
|
||||
));
|
||||
|
||||
let enable_gen_pipeline =
|
||||
pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
label: Some(format!("bevy_tape_drive::gen_shader::enable_gen::{key}").into()),
|
||||
layout: vec![replacement_modules.enable_gen_layout.clone()],
|
||||
shader: enable_gen.clone(),
|
||||
entry_point: Some("enable_gen_entry".into()),
|
||||
..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(),
|
||||
entry_point: Some("fuzz_entry".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(),
|
||||
entry_point: Some("spatial_texture_entry".into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
Ok(RenderTape {
|
||||
seed: tape.seed,
|
||||
subdivision: tape.subdivision,
|
||||
enable_gen,
|
||||
fuzz,
|
||||
normals,
|
||||
spatial_texture,
|
||||
trace,
|
||||
enable_gen_pipeline,
|
||||
fuzz_pipeline,
|
||||
spatial_texture_pipeline,
|
||||
})
|
||||
}
|
||||
|
||||
fn unload_asset(_tape_id: AssetId<Self::SourceAsset>, (..): &mut SystemParamItem<Self::Param>) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user