it starts at least
This commit is contained in:
@@ -17,7 +17,9 @@ bevy_core_pipeline = { git = "https://github.com/bevyengine/bevy.git", rev = "0f
|
||||
bevy_derive = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_diagnostic = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_ecs = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_image = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_image = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010", features = [
|
||||
"zstd_rust",
|
||||
] }
|
||||
bevy_light = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_log = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_math = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
@@ -30,7 +32,8 @@ bevy_platform = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358
|
||||
] }
|
||||
bevy_reflect = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_render = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010", features = [
|
||||
"spirv_shader_passthrough",
|
||||
"shader_format_spirv",
|
||||
#"spirv_shader_passthrough",
|
||||
] }
|
||||
bevy_shader = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_shape = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
|
||||
@@ -103,6 +103,7 @@ impl RaytracingSceneBindings {
|
||||
&mut self.instances.vertex_buffers.dirty,
|
||||
&mut self.instances.index_buffers.dirty,
|
||||
&mut self.instances.aabb_buffers.dirty,
|
||||
&mut self.instances.tape_metadata_buffers.dirty,
|
||||
&mut self.assets.textures.dirty,
|
||||
] {
|
||||
invalid |= core::mem::replace(dirty, false);
|
||||
@@ -150,6 +151,7 @@ impl RaytracingSceneBindings {
|
||||
let vertex_buffers = buffer_bindings(&self.instances.vertex_buffers, dummy);
|
||||
let index_buffers = buffer_bindings(&self.instances.index_buffers, dummy);
|
||||
let aabb_buffers = buffer_bindings(&self.instances.aabb_buffers, dummy);
|
||||
let tape_metadata_buffers = buffer_bindings(&self.instances.tape_metadata_buffers, dummy);
|
||||
|
||||
let (mut textures, mut samplers): (Vec<_>, Vec<_>) = self
|
||||
.assets
|
||||
@@ -238,6 +240,7 @@ impl RaytracingSceneBindings {
|
||||
vertex_buffers.as_slice(),
|
||||
index_buffers.as_slice(),
|
||||
aabb_buffers.as_slice(),
|
||||
tape_metadata_buffers.as_slice(),
|
||||
textures.as_slice(),
|
||||
samplers.as_slice(),
|
||||
materials,
|
||||
|
||||
@@ -44,9 +44,11 @@ pub struct GpuInstanceGeometryIds {
|
||||
#[derive(Clone, Copy, Default, PartialEq, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct GpuInstanceTapeIds {
|
||||
aabb_buffer_id: u32,
|
||||
aabb_buffer_offset: u32,
|
||||
aabb_count: u32,
|
||||
aabb_buffer_id: u32,
|
||||
aabb_buffer_offset: u32,
|
||||
aabb_count: u32,
|
||||
metadata_buffer_id: u32,
|
||||
metadata_buffer_offset: u32,
|
||||
}
|
||||
|
||||
/// A world-from-local affine transform, stored transposed as three rows.
|
||||
@@ -103,6 +105,7 @@ pub struct InstanceState {
|
||||
pub vertex_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub index_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub aabb_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub tape_metadata_buffers: RetainedBindingArray<BufferId, Buffer>,
|
||||
pub transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||
pub previous_frame_transforms: AtomicSparseBufferVec<GpuTransform>,
|
||||
pub geometry_ids: AtomicSparseBufferVec<GpuInstanceGeometryIds>,
|
||||
@@ -123,6 +126,7 @@ impl InstanceState {
|
||||
vertex_buffers: RetainedBindingArray::new(),
|
||||
index_buffers: RetainedBindingArray::new(),
|
||||
aabb_buffers: RetainedBindingArray::new(),
|
||||
tape_metadata_buffers: RetainedBindingArray::new(),
|
||||
transforms: storage_buffer("solari_transforms"),
|
||||
previous_frame_transforms: storage_buffer("solari_previous_frame_transforms"),
|
||||
geometry_ids: storage_buffer("solari_geometry_ids"),
|
||||
@@ -467,20 +471,32 @@ impl InstanceState {
|
||||
tape_id: AssetId<Tape>,
|
||||
instance: &mut Instance,
|
||||
) -> bool {
|
||||
bevy_log::debug!("Resolving tape instance {tape_id}");
|
||||
bevy_log::info!("Resolving tape instance {tape_id}");
|
||||
let slot = instance.slot;
|
||||
let (Some(aabb_slice), Some(material_slot), Some(blas_address)) = (
|
||||
let (Some(aabb_slice), Some(metadata_slice), Some(material_slot), Some(blas_address)) = (
|
||||
inputs.tape_allocator.tape_aabb_slice(&tape_id),
|
||||
inputs.tape_allocator.tape_metadata_slice(&tape_id),
|
||||
inputs.assets.material_slots.get(&instance.material),
|
||||
inputs.tape_blas_manager.device_address(&tape_id),
|
||||
) else {
|
||||
bevy_log::info!("Deactivating tape instance {tape_id}");
|
||||
self.deactivate_instance(lights, entity, instance);
|
||||
return false;
|
||||
};
|
||||
|
||||
bevy_log::info!("Tape {tape_id} aabb_slice: {:?}", aabb_slice.range);
|
||||
bevy_log::info!("Tape {tape_id} metadata_slice: {:?}", metadata_slice.range);
|
||||
bevy_log::info!("Tape {tape_id} material_slot: {material_slot}");
|
||||
bevy_log::info!("Tape {tape_id} blas_address: {blas_address}");
|
||||
|
||||
let aabb_buffer_key = aabb_slice.buffer.id();
|
||||
let metadata_buffer_key = metadata_slice.buffer.id();
|
||||
let capacity = MAX_TAPE_SLAB_COUNT.get();
|
||||
if !self.aabb_buffers.has_room(&aabb_buffer_key, capacity) {
|
||||
if !self.aabb_buffers.has_room(&aabb_buffer_key, capacity)
|
||||
|| !self
|
||||
.tape_metadata_buffers
|
||||
.has_room(&metadata_buffer_key, capacity)
|
||||
{
|
||||
once!(warn!(
|
||||
"Solari scene needs more than {} tape slabs. Instances past that limit will not \
|
||||
be rendered.",
|
||||
@@ -495,7 +511,13 @@ impl InstanceState {
|
||||
.aabb_buffers
|
||||
.acquire(aabb_buffer_key, capacity, || aabb_slice.buffer.clone())
|
||||
.expect("aabb slab binding array had room but handed out no slot");
|
||||
instance.buffers = Some((aabb_buffer_key, aabb_buffer_key));
|
||||
let metadata_buffer_id = self
|
||||
.tape_metadata_buffers
|
||||
.acquire(metadata_buffer_key, capacity, || {
|
||||
metadata_slice.buffer.clone()
|
||||
})
|
||||
.expect("tape metadata slab binding array had room but handed out no slot");
|
||||
instance.buffers = Some((aabb_buffer_key, metadata_buffer_key));
|
||||
self.release_tape_buffers(previous_buffers);
|
||||
|
||||
let aabb_count = (aabb_slice.range.len()) as u32;
|
||||
@@ -505,6 +527,8 @@ impl InstanceState {
|
||||
aabb_buffer_id,
|
||||
aabb_buffer_offset: aabb_slice.range.start,
|
||||
aabb_count,
|
||||
metadata_buffer_id,
|
||||
metadata_buffer_offset: metadata_slice.range.start,
|
||||
},
|
||||
);
|
||||
self.material_ids.grow_and_set(slot, material_slot);
|
||||
@@ -525,6 +549,8 @@ impl InstanceState {
|
||||
}
|
||||
*/
|
||||
|
||||
bevy_log::info!("Tape {tape_id} resolved");
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
@@ -583,8 +609,9 @@ impl InstanceState {
|
||||
}
|
||||
|
||||
fn release_tape_buffers(&mut self, buffers: Option<(BufferId, BufferId)>) {
|
||||
if let Some((aabb_key, _)) = buffers {
|
||||
if let Some((aabb_key, metadata_key)) = buffers {
|
||||
self.aabb_buffers.release(&aabb_key);
|
||||
self.tape_metadata_buffers.release(&metadata_key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ use super::{
|
||||
RaytracingMesh3d, Tape3d, extract::StandardMaterialAssets,
|
||||
meshes::blas::BlasManager as MeshBlasManager, tapes::blas::BlasManager as TapeBlasManager,
|
||||
};
|
||||
use crate::scene::binder::instances::MAX_TAPE_SLAB_COUNT;
|
||||
|
||||
/// Insert this resource into the render world to make the raytracing scene
|
||||
/// retain the previous frame's TLAS and the light id translation table that
|
||||
@@ -86,7 +87,8 @@ impl FromWorld for RaytracingSceneBindings {
|
||||
(
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_TAPE_SLAB_COUNT),
|
||||
storage_buffer_read_only_sized(false, None).count(MAX_TAPE_SLAB_COUNT),
|
||||
texture_2d(TextureSampleType::Float { filterable: true })
|
||||
.count(MAX_TEXTURE_COUNT),
|
||||
sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT),
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import bevy_pbr::render::pbr_functions::calculate_tbn_mikktspace;
|
||||
import bevy_render::maths::affine3_to_square;
|
||||
import bevy_tape_load::render::metadata_types::TapeMetadata;
|
||||
import bevy_tape_load::render::aabb::PackedAabb;
|
||||
|
||||
enable wgpu_ray_query;
|
||||
enable wgpu_binding_array;
|
||||
@@ -16,13 +18,25 @@ struct InstanceTapeIds {
|
||||
aabb_buffer_id: u32,
|
||||
aabb_buffer_offset: u32,
|
||||
aabb_count: u32,
|
||||
metadata_buffer_id: u32,
|
||||
metadata_buffer_offset: u32,
|
||||
}
|
||||
|
||||
struct VertexBuffer { vertices: array<PackedVertex> }
|
||||
struct VertexBuffer {
|
||||
vertices: array<PackedVertex>,
|
||||
}
|
||||
|
||||
struct IndexBuffer { indices: array<u32> }
|
||||
struct IndexBuffer {
|
||||
indices: array<u32>,
|
||||
}
|
||||
|
||||
struct AabbBuffer { indices: array<PackedAabb> }
|
||||
struct AabbBuffer {
|
||||
indices: array<PackedAabb>,
|
||||
}
|
||||
|
||||
struct TapeMetadataBuffer {
|
||||
indices: array<TapeMetadata>,
|
||||
}
|
||||
|
||||
struct PackedVertex {
|
||||
a: vec4<f32>,
|
||||
@@ -30,11 +44,6 @@ struct PackedVertex {
|
||||
tangent: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PackedAabb {
|
||||
min: vec3<f32>,
|
||||
max: vec3<f32>,
|
||||
}
|
||||
|
||||
struct Vertex {
|
||||
position: vec3<f32>,
|
||||
normal: vec3<f32>,
|
||||
@@ -91,34 +100,63 @@ struct EnvironmentMapLight {
|
||||
|
||||
const LIGHT_NOT_PRESENT_THIS_FRAME = 0xFFFFFFFFu;
|
||||
|
||||
@group(0) @binding(0) var<storage> vertex_buffers: binding_array<VertexBuffer>;
|
||||
@group(0) @binding(1) var<storage> index_buffers: binding_array<IndexBuffer>;
|
||||
@group(0) @binding(2) var<storage> aabb_buffers: binding_array<AabbBuffer>;
|
||||
@group(0) @binding(3) var textures: binding_array<texture_2d<f32>>;
|
||||
@group(0) @binding(4) var samplers: binding_array<sampler>;
|
||||
@group(0) @binding(5) var<storage> materials: array<Material>;
|
||||
@group(0) @binding(6) var tlas: acceleration_structure;
|
||||
@group(0) @binding(7) var previous_frame_tlas: acceleration_structure;
|
||||
@group(0) @binding(8) var<storage> transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(9) var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(10) var<storage> geometry_ids: array<InstanceGeometryIds>;
|
||||
@group(0) @binding(11) var<storage> tape_ids: array<InstanceTapeIds>;
|
||||
@group(0) @binding(12) var<storage> material_ids: array<u32>;
|
||||
@group(0) @binding(13) var<storage> light_sources: array<LightSource>;
|
||||
@group(0) @binding(14) var<storage> directional_lights: array<DirectionalLight>;
|
||||
@group(0) @binding(15) var<storage> previous_frame_light_id_translations: array<u32>;
|
||||
@group(0) @binding(16) var brdf_dfg_lut: texture_2d<f32>;
|
||||
@group(0) @binding(17) var brdf_dfg_lut_sampler: sampler;
|
||||
@group(0) @binding(18) var environment_map: texture_cube<f32>;
|
||||
@group(0) @binding(19) var environment_map_sampler: sampler;
|
||||
@group(0) @binding(20) var<storage> environment_map_light: EnvironmentMapLight;
|
||||
@group(0) @binding(0)
|
||||
var<storage> vertex_buffers: binding_array<VertexBuffer>;
|
||||
@group(0) @binding(1)
|
||||
var<storage> index_buffers: binding_array<IndexBuffer>;
|
||||
@group(0) @binding(2)
|
||||
var<storage> aabb_buffers: binding_array<AabbBuffer>;
|
||||
@group(0) @binding(3)
|
||||
var<storage> tape_metadata_buffers: binding_array<TapeMetadataBuffer>;
|
||||
@group(0) @binding(4)
|
||||
var textures: binding_array<texture_2d<f32>>;
|
||||
@group(0) @binding(5)
|
||||
var samplers: binding_array<sampler>;
|
||||
@group(0) @binding(6)
|
||||
var<storage> materials: array<Material>;
|
||||
@group(0) @binding(7)
|
||||
var tlas: acceleration_structure;
|
||||
@group(0) @binding(8)
|
||||
var previous_frame_tlas: acceleration_structure;
|
||||
@group(0) @binding(9)
|
||||
var<storage> transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(10)
|
||||
var<storage> previous_frame_transforms: array<mat3x4<f32>>;
|
||||
@group(0) @binding(11)
|
||||
var<storage> geometry_ids: array<InstanceGeometryIds>;
|
||||
@group(0) @binding(12)
|
||||
var<storage> tape_ids: array<InstanceTapeIds>;
|
||||
@group(0) @binding(13)
|
||||
var<storage> material_ids: array<u32>;
|
||||
@group(0) @binding(14)
|
||||
var<storage> light_sources: array<LightSource>;
|
||||
@group(0) @binding(15)
|
||||
var<storage> directional_lights: array<DirectionalLight>;
|
||||
@group(0) @binding(16)
|
||||
var<storage> previous_frame_light_id_translations: array<u32>;
|
||||
@group(0) @binding(17)
|
||||
var brdf_dfg_lut: texture_2d<f32>;
|
||||
@group(0) @binding(18)
|
||||
var brdf_dfg_lut_sampler: sampler;
|
||||
@group(0) @binding(19)
|
||||
var environment_map: texture_cube<f32>;
|
||||
@group(0) @binding(20)
|
||||
var environment_map_sampler: sampler;
|
||||
@group(0) @binding(21)
|
||||
var<storage> environment_map_light: EnvironmentMapLight;
|
||||
|
||||
const RAY_T_MIN = 0.001f;
|
||||
const RAY_T_MAX = 100000.0f;
|
||||
|
||||
const RAY_NO_CULL = 0xFFu;
|
||||
|
||||
fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32) -> RayIntersection {
|
||||
fn trace_ray(
|
||||
ray_origin: vec3<f32>,
|
||||
ray_direction: vec3<f32>,
|
||||
ray_t_min: f32,
|
||||
ray_t_max: f32,
|
||||
ray_flag: u32,
|
||||
) -> RayIntersection {
|
||||
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
||||
var rq: ray_query;
|
||||
rayQueryInitialize(&rq, tlas, ray);
|
||||
@@ -126,7 +164,13 @@ fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ra
|
||||
return rayQueryGetCommittedIntersection(&rq);
|
||||
}
|
||||
|
||||
fn trace_ray_previous_frame(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32) -> RayIntersection {
|
||||
fn trace_ray_previous_frame(
|
||||
ray_origin: vec3<f32>,
|
||||
ray_direction: vec3<f32>,
|
||||
ray_t_min: f32,
|
||||
ray_t_max: f32,
|
||||
ray_flag: u32,
|
||||
) -> RayIntersection {
|
||||
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
||||
var rq: ray_query;
|
||||
rayQueryInitialize(&rq, previous_frame_tlas, ray);
|
||||
@@ -188,33 +232,49 @@ fn resolve_material(material: Material, uv: vec2<f32>) -> ResolvedMaterial {
|
||||
}
|
||||
|
||||
fn resolve_ray_hit_full(ray_hit: RayIntersection) -> ResolvedRayHitFull {
|
||||
let barycentrics = vec3(1.0 - ray_hit.barycentrics.x - ray_hit.barycentrics.y, ray_hit.barycentrics);
|
||||
return resolve_triangle_data_full(ray_hit.instance_custom_data, ray_hit.primitive_index, barycentrics);
|
||||
let barycentrics = vec3(
|
||||
1.0 - ray_hit.barycentrics.x - ray_hit.barycentrics.y,
|
||||
ray_hit.barycentrics,
|
||||
);
|
||||
return resolve_triangle_data_full(
|
||||
ray_hit.instance_custom_data,
|
||||
ray_hit.primitive_index,
|
||||
barycentrics,
|
||||
);
|
||||
}
|
||||
|
||||
fn load_vertices(instance_geometry_ids: InstanceGeometryIds, triangle_id: u32) -> array<Vertex, 3> {
|
||||
let index_buffer = &index_buffers[instance_geometry_ids.index_buffer_id].indices;
|
||||
let vertex_buffer = &vertex_buffers[instance_geometry_ids.vertex_buffer_id].vertices;
|
||||
|
||||
let indices_i = (triangle_id * 3u) + vec3(0u, 1u, 2u) + instance_geometry_ids.index_buffer_offset;
|
||||
let indices = vec3((*index_buffer)[indices_i.x], (*index_buffer)[indices_i.y], (*index_buffer)[indices_i.z]) + instance_geometry_ids.vertex_buffer_offset;
|
||||
let indices_i = (triangle_id * 3u) + vec3(0u, 1u, 2u)
|
||||
+ instance_geometry_ids.index_buffer_offset;
|
||||
let indices = vec3(
|
||||
(*index_buffer)[indices_i.x],
|
||||
(*index_buffer)[indices_i.y],
|
||||
(*index_buffer)[indices_i.z],
|
||||
) + instance_geometry_ids.vertex_buffer_offset;
|
||||
|
||||
return array<Vertex, 3>(
|
||||
unpack_vertex((*vertex_buffer)[indices.x]),
|
||||
unpack_vertex((*vertex_buffer)[indices.y]),
|
||||
unpack_vertex((*vertex_buffer)[indices.z])
|
||||
);
|
||||
unpack_vertex((*vertex_buffer)[indices.x]),
|
||||
unpack_vertex((*vertex_buffer)[indices.y]),
|
||||
unpack_vertex((*vertex_buffer)[indices.z]),
|
||||
);
|
||||
}
|
||||
|
||||
fn transform_positions(transform: mat4x4<f32>, vertices: array<Vertex, 3>) -> array<vec3<f32>, 3> {
|
||||
return array<vec3<f32>, 3>(
|
||||
(transform * vec4(vertices[0].position, 1.0)).xyz,
|
||||
(transform * vec4(vertices[1].position, 1.0)).xyz,
|
||||
(transform * vec4(vertices[2].position, 1.0)).xyz
|
||||
);
|
||||
(transform * vec4(vertices[0].position, 1.0)).xyz,
|
||||
(transform * vec4(vertices[1].position, 1.0)).xyz,
|
||||
(transform * vec4(vertices[2].position, 1.0)).xyz,
|
||||
);
|
||||
}
|
||||
|
||||
fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics: vec3<f32>) -> ResolvedRayHitFull {
|
||||
fn resolve_triangle_data_full(
|
||||
instance_id: u32,
|
||||
triangle_id: u32,
|
||||
barycentrics: vec3<f32>,
|
||||
) -> ResolvedRayHitFull {
|
||||
let material_id = material_ids[instance_id];
|
||||
let material = materials[material_id];
|
||||
|
||||
@@ -225,23 +285,51 @@ fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics:
|
||||
let vertices = load_vertices(instance_geometry_ids, triangle_id);
|
||||
|
||||
let world_vertices = transform_positions(transform, vertices);
|
||||
let world_position = mat3x3(world_vertices[0], world_vertices[1], world_vertices[2]) * barycentrics;
|
||||
let world_position = mat3x3(
|
||||
world_vertices[0],
|
||||
world_vertices[1],
|
||||
world_vertices[2],
|
||||
) * barycentrics;
|
||||
|
||||
let previous_frame_world_vertices = transform_positions(previous_frame_transform, vertices);
|
||||
let previous_frame_world_position = mat3x3(previous_frame_world_vertices[0], previous_frame_world_vertices[1], previous_frame_world_vertices[2]) * barycentrics;
|
||||
let previous_frame_world_position = mat3x3(
|
||||
previous_frame_world_vertices[0],
|
||||
previous_frame_world_vertices[1],
|
||||
previous_frame_world_vertices[2],
|
||||
) * barycentrics;
|
||||
|
||||
let uv = mat3x2(vertices[0].uv, vertices[1].uv, vertices[2].uv) * barycentrics;
|
||||
|
||||
let transform_3x3 = mat3x3(transform[0].xyz, transform[1].xyz, transform[2].xyz);
|
||||
let transform_3x3 = mat3x3(
|
||||
transform[0].xyz,
|
||||
transform[1].xyz,
|
||||
transform[2].xyz,
|
||||
);
|
||||
|
||||
let cofactor = mat3x3(cross(transform_3x3[1], transform_3x3[2]), cross(transform_3x3[2], transform_3x3[0]), cross(transform_3x3[0], transform_3x3[1]));
|
||||
let cofactor = mat3x3(
|
||||
cross(transform_3x3[1], transform_3x3[2]),
|
||||
cross(transform_3x3[2], transform_3x3[0]),
|
||||
cross(transform_3x3[0], transform_3x3[1]),
|
||||
);
|
||||
let determinant = dot(transform_3x3[0], cofactor[0]);
|
||||
let determinant_sign = select(sign(determinant), 1.0, determinant == 0.0);
|
||||
|
||||
let local_tangent = mat3x3(vertices[0].tangent.xyz, vertices[1].tangent.xyz, vertices[2].tangent.xyz) * barycentrics;
|
||||
let world_tangent = vec4(normalize(transform_3x3 * local_tangent), vertices[0].tangent.w * determinant_sign);
|
||||
let local_tangent = mat3x3(
|
||||
vertices[0].tangent.xyz,
|
||||
vertices[1].tangent.xyz,
|
||||
vertices[2].tangent.xyz,
|
||||
) * barycentrics;
|
||||
let world_tangent = vec4(
|
||||
normalize(transform_3x3 * local_tangent),
|
||||
vertices[0].tangent.w * determinant_sign,
|
||||
);
|
||||
|
||||
let local_normal = mat3x3(vertices[0].normal, vertices[1].normal, vertices[2].normal) * barycentrics; // TODO: Use barycentric lerp, ray_hit.object_to_world, cross product geo normal
|
||||
let local_normal = mat3x3(
|
||||
vertices[0].normal,
|
||||
vertices[1].normal,
|
||||
vertices[2].normal,
|
||||
)
|
||||
* barycentrics; // TODO: Use barycentric lerp, ray_hit.object_to_world, cross product geo normal
|
||||
var world_normal = normalize(determinant_sign * (cofactor * local_normal));
|
||||
|
||||
let geometric_world_normal = world_normal;
|
||||
@@ -251,7 +339,9 @@ fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics:
|
||||
let B = TBN[1];
|
||||
let N = TBN[2];
|
||||
var Nt = sample_texture(material.normal_map_texture_id, uv) * 2.0 - 1.0;
|
||||
Nt.z = sqrt(max(1.0 - dot(Nt.xy, Nt.xy), 0.0)); // Reconstruct Z to support two-channel normal maps
|
||||
Nt.z = sqrt(
|
||||
max(1.0 - dot(Nt.xy, Nt.xy), 0.0),
|
||||
); // Reconstruct Z to support two-channel normal maps
|
||||
world_normal = normalize(Nt.x * T + Nt.y * B + Nt.z * N);
|
||||
}
|
||||
|
||||
@@ -262,14 +352,14 @@ fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics:
|
||||
let resolved_material = resolve_material(material, uv);
|
||||
|
||||
return ResolvedRayHitFull(
|
||||
world_position,
|
||||
previous_frame_world_position,
|
||||
world_normal,
|
||||
geometric_world_normal,
|
||||
world_tangent,
|
||||
uv,
|
||||
triangle_area,
|
||||
instance_geometry_ids.triangle_count,
|
||||
resolved_material,
|
||||
);
|
||||
world_position,
|
||||
previous_frame_world_position,
|
||||
world_normal,
|
||||
geometric_world_normal,
|
||||
world_tangent,
|
||||
uv,
|
||||
triangle_area,
|
||||
instance_geometry_ids.triangle_count,
|
||||
resolved_material,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ bevy_core_pipeline = { git = "https://github.com/bevyengine/bevy.git", rev = "0f
|
||||
bevy_derive = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_diagnostic = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_ecs = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_image = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_light = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_log = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_math = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
@@ -30,7 +29,8 @@ bevy_platform = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358
|
||||
] }
|
||||
bevy_reflect = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_render = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010", features = [
|
||||
"spirv_shader_passthrough",
|
||||
"shader_format_spirv",
|
||||
#"spirv_shader_passthrough",
|
||||
] }
|
||||
bevy_shader = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
bevy_shape = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010", features = [
|
||||
@@ -40,7 +40,7 @@ bevy_transform = { git = "https://github.com/bevyengine/bevy.git", rev = "0f3835
|
||||
bevy_utils = { git = "https://github.com/bevyengine/bevy.git", rev = "0f38358f573a7dc6ea961076f6151be662142010" }
|
||||
|
||||
# tape drive
|
||||
tape-load = { git = "https://git.stargaze.group/molive/tape-drive.git", rev = "19b241637be770db5ee6201951138f41b1f9f467", features = [
|
||||
tape-load = { git = "https://git.stargaze.group/molive/tape-drive.git", rev = "b7f83169e28105693203ec97cbe26d3b29b60cec", features = [
|
||||
"bevy",
|
||||
] }
|
||||
rspirv = "0.13"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
use std::{fs::read_dir, process::Command};
|
||||
|
||||
fn main() {
|
||||
//let mut output = std::fs::File::create("output.txt").unwrap();
|
||||
println!("cargo::rerun-if-changed=build.rs");
|
||||
println!("cargo::rerun-if-changed=Cargo.lock");
|
||||
println!("cargo::rerun-if-changed=compile_shaders.py");
|
||||
|
||||
let paths = read_dir("shaders/include")
|
||||
.unwrap()
|
||||
.chain(read_dir("shaders/replacement").unwrap());
|
||||
for path in paths {
|
||||
if let Ok(path) = path
|
||||
&& path
|
||||
.path()
|
||||
.extension()
|
||||
.map(|ext| ext == "glsl")
|
||||
.unwrap_or(false)
|
||||
{
|
||||
println!("cargo::rerun-if-changed={}", path.path().display());
|
||||
//writeln!(output, "cargo::rerun-if-changed={}",
|
||||
// path.path().display()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Command::new("python3")
|
||||
.arg("compile_shaders.py")
|
||||
.status()
|
||||
.expect("Failed to assemble header shaders");
|
||||
}
|
||||
Executable
+111
@@ -0,0 +1,111 @@
|
||||
#!/bin/python3
|
||||
|
||||
import shutil
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
TARGET_ENV = "vulkan1.3"
|
||||
SHADERS_OUT = "shaders_out"
|
||||
SHADERS_BASE = "shaders"
|
||||
SHADERS_INCLUDE = f"{SHADERS_BASE}/include"
|
||||
#SHADERS_IN_NORMAL = f"{SHADERS_BASE}/normal"
|
||||
SHADERS_IN_REPLACEMENT = f"{SHADERS_BASE}/replacement"
|
||||
#SHADERS_OUT_NORMAL = f"{SHADERS_OUT}/normal"
|
||||
SHADERS_OUT_REPLACEMENT = f"{SHADERS_OUT}/replacement"
|
||||
|
||||
DUMP_DISASSEMBLIES = True
|
||||
|
||||
if os.path.isdir(SHADERS_OUT):
|
||||
shutil.rmtree(SHADERS_OUT)
|
||||
os.mkdir(SHADERS_OUT)
|
||||
#os.mkdir(SHADERS_OUT_NORMAL)
|
||||
os.mkdir(SHADERS_OUT_REPLACEMENT)
|
||||
|
||||
# normal_shaders = [f for f in os.listdir(SHADERS_IN_NORMAL) if f.endswith(".glsl")]
|
||||
#
|
||||
# for shader in normal_shaders:
|
||||
# result = subprocess.run(
|
||||
# [
|
||||
# "glslangValidator",
|
||||
# f"-I{SHADERS_INCLUDE}",
|
||||
# "--spirv-val",
|
||||
# "-gVS",
|
||||
# # "-q",
|
||||
# "--target-env",
|
||||
# TARGET_ENV,
|
||||
# "-o",
|
||||
# f"{SHADERS_OUT_NORMAL}/{shader}.spv",
|
||||
# f"{SHADERS_IN_NORMAL}/{shader}",
|
||||
# ]
|
||||
# )
|
||||
# if result.returncode != 0:
|
||||
# sys.exit(result.returncode)
|
||||
|
||||
replacement_shaders = [
|
||||
f for f in os.listdir(SHADERS_IN_REPLACEMENT) if f.endswith(".glsl")
|
||||
]
|
||||
|
||||
for shader in replacement_shaders:
|
||||
print(f"{SHADERS_IN_REPLACEMENT}/{shader}")
|
||||
result = subprocess.run(
|
||||
[
|
||||
"glslangValidator",
|
||||
f"-I{SHADERS_INCLUDE}",
|
||||
"--spirv-val",
|
||||
"--spirv-dis",
|
||||
"-g",
|
||||
"--target-env",
|
||||
TARGET_ENV,
|
||||
"--quiet",
|
||||
f"{SHADERS_IN_REPLACEMENT}/{shader}",
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
print(result.stdout)
|
||||
sys.exit(result.returncode)
|
||||
asm = result.stdout
|
||||
|
||||
if DUMP_DISASSEMBLIES:
|
||||
with open(f"{SHADERS_OUT_REPLACEMENT}/{shader}.stage0.spv-dis", "w") as f:
|
||||
f.write(asm)
|
||||
|
||||
asm = re.sub(r"(?ms)%scene_vf4_ =.*?OpFunctionEnd", "", asm)
|
||||
asm = re.sub(r"%scene_vf4_", "%10000", asm)
|
||||
asm = re.sub(r"(?ms)%interval_scene_vf4_vf4_ =.*?OpFunctionEnd", "", asm)
|
||||
asm = re.sub(r"%interval_scene_vf4_vf4_", "%20000", asm)
|
||||
asm = re.sub(r"(?ms)%gradient_scene_vf4_ =.*?OpFunctionEnd", "", asm)
|
||||
asm = re.sub(r"%gradient_scene_vf4_", "%30000", asm)
|
||||
|
||||
asm = re.sub(r"OpName %scene_input_one \"scene_input_one\"", "", asm)
|
||||
asm = re.sub(r"OpName %interval_scene_input_one \"interval_scene_input_one\"", "", asm)
|
||||
asm = re.sub(r"OpName %interval_scene_input_two \"interval_scene_input_two\"", "", asm)
|
||||
asm = re.sub(r"OpName %gradient_scene_input_one \"gradient_scene_input_one\"", "", asm)
|
||||
|
||||
if DUMP_DISASSEMBLIES:
|
||||
with open(f"{SHADERS_OUT_REPLACEMENT}/{shader}.stage1.spv-dis", "w") as f:
|
||||
f.write(asm)
|
||||
|
||||
result = subprocess.run(
|
||||
[
|
||||
"spirv-as",
|
||||
"--preserve-numeric-ids",
|
||||
"--target-env",
|
||||
TARGET_ENV,
|
||||
"-o",
|
||||
f"{SHADERS_OUT_REPLACEMENT}/{shader}.spv",
|
||||
"-",
|
||||
],
|
||||
input=asm,
|
||||
encoding="ascii",
|
||||
)
|
||||
if result.returncode != 0:
|
||||
sys.exit(result.returncode)
|
||||
|
||||
for f in os.listdir("."):
|
||||
if f.endswith(".spv"):
|
||||
print(f"removing {f}")
|
||||
os.remove(f)
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef implicit_include
|
||||
#define implicit_include
|
||||
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
|
||||
float scene(vec4 scene_input_one) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
vec2 interval_scene(vec4 interval_scene_input_one, vec4 interval_scene_input_two) {
|
||||
return vec2(0.0);
|
||||
}
|
||||
|
||||
vec4 gradient_scene(vec4 gradient_scene_input_one) {
|
||||
return vec4(0.0);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,102 @@
|
||||
#version 460
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#extension GL_EXT_buffer_reference : require
|
||||
#include "implicit_include.glsl"
|
||||
|
||||
struct PackedAabb {
|
||||
float min_x;
|
||||
float min_y;
|
||||
float min_z;
|
||||
float max_x;
|
||||
float max_y;
|
||||
float max_z;
|
||||
};
|
||||
|
||||
layout(std430, buffer_reference, buffer_reference_align = 8) writeonly buffer AabbBuffer {
|
||||
PackedAabb aabbs[];
|
||||
};
|
||||
|
||||
struct TapeMetadata {
|
||||
vec3 lowest_corner;
|
||||
uint pad_a;
|
||||
vec3 dimensions;
|
||||
uint pad_b;
|
||||
uint subdivision_log2;
|
||||
};
|
||||
|
||||
layout(std430, buffer_reference, buffer_reference_align = 8) readonly buffer TapeMetadataBuffer {
|
||||
TapeMetadata metadata[];
|
||||
};
|
||||
|
||||
struct InstanceTapeIds {
|
||||
uint aabb_buffer_id;
|
||||
uint aabb_buffer_offset;
|
||||
uint aabb_count;
|
||||
uint metadata_buffer_id;
|
||||
uint metadata_buffer_offset;
|
||||
};
|
||||
|
||||
layout(set = 0, binding = 0, std430) restrict readonly buffer AabbBufferArray {
|
||||
AabbBuffer buffers[];
|
||||
} aabb_buffers;
|
||||
layout(set = 0, binding = 1, std430) restrict readonly buffer TapeMetadataBufferArray {
|
||||
TapeMetadataBuffer buffers[];
|
||||
} tape_metadata_buffers;
|
||||
layout(set = 0, binding = 2, std430) restrict readonly buffer InstanceTapeIdsArray {
|
||||
InstanceTapeIds ids[];
|
||||
} tape_ids;
|
||||
|
||||
layout(push_constant, std430) uniform PushConstantData {
|
||||
uint object_index;
|
||||
float time;
|
||||
} pc;
|
||||
|
||||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
|
||||
|
||||
const float NAN = 0.0 / 0.0;
|
||||
|
||||
void main() {
|
||||
//if (SPEC_BRUTE_FORCE) {
|
||||
// return;
|
||||
//}
|
||||
|
||||
InstanceTapeIds instance = tape_ids.ids[pc.object_index];
|
||||
TapeMetadata metadata = tape_metadata_buffers.buffers[instance.metadata_buffer_id].metadata[instance.metadata_buffer_offset];
|
||||
AabbBuffer aabb_buffer = aabb_buffers.buffers[instance.aabb_buffer_id];
|
||||
|
||||
uvec3 total_invocations = gl_NumWorkGroups * gl_WorkGroupSize;
|
||||
uint base_index = (gl_GlobalInvocationID.z * total_invocations.x * total_invocations.y) +
|
||||
(gl_GlobalInvocationID.y * total_invocations.x) + gl_GlobalInvocationID.x;
|
||||
|
||||
vec3 internal_pos = vec3(
|
||||
float((base_index >> (metadata.subdivision_log2 * 2)) & ((1 << metadata.subdivision_log2) - 1))
|
||||
/ float(1 << metadata.subdivision_log2),
|
||||
float((base_index >> (metadata.subdivision_log2 * 1)) & ((1 << metadata.subdivision_log2) - 1))
|
||||
/ float(1 << metadata.subdivision_log2),
|
||||
float((base_index >> (metadata.subdivision_log2 * 0)) & ((1 << metadata.subdivision_log2) - 1))
|
||||
/ float(1 << metadata.subdivision_log2)
|
||||
);
|
||||
|
||||
vec3 low_corner = (((vec3(0.0) / float(1 << metadata.subdivision_log2)) + internal_pos)
|
||||
* metadata.dimensions) + metadata.lowest_corner;
|
||||
vec3 high_corner = (((vec3(1.0) / float(1 << metadata.subdivision_log2)) + internal_pos)
|
||||
* metadata.dimensions) + metadata.lowest_corner;
|
||||
|
||||
vec2 interval = interval_scene(
|
||||
vec4(low_corner, pc.time),
|
||||
vec4(high_corner, pc.time)
|
||||
);
|
||||
|
||||
bool enable = (interval.x < 0) && (interval.y > 0);
|
||||
|
||||
if (enable) {
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].min_x = low_corner.x;
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].min_y = low_corner.y;
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].min_z = low_corner.z;
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].max_x = high_corner.x;
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].max_y = high_corner.y;
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].max_z = high_corner.z;
|
||||
} else {
|
||||
aabb_buffer.aabbs[instance.aabb_buffer_offset].min_x = NAN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import bevy_tape_load::render::aabb;
|
||||
import bevy_tape_load::render::metadata_types;
|
||||
import bevy_tape_load::render::implicit;
|
||||
|
||||
enable wgpu_binding_array;
|
||||
|
||||
struct AabbBuffer {
|
||||
aabbs: array<PackedAabb>,
|
||||
}
|
||||
struct TapeMetadataBuffer {
|
||||
metadata: array<TapeMetadata>,
|
||||
}
|
||||
struct PushConstants {
|
||||
object_index: 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>;
|
||||
@group(0) @binding(1)
|
||||
var<storage, read> tape_metadata_buffers: binding_array<TapeMetadataBuffer>;
|
||||
@group(0) @binding(2)
|
||||
var<storage> tape_ids: array<InstanceTapeIds>;
|
||||
|
||||
var<immediate> push_constants: PushConstants;
|
||||
|
||||
struct ComputeInput {
|
||||
@builtin(global_invocation_id) global_invocation_id: vec3u,
|
||||
@builtin(num_workgroups) num_workgroups: vec3u,
|
||||
@builtin(workgroup_id) workgroup_id: vec3u,
|
||||
}
|
||||
|
||||
const NAN: f32 = 0.0 / 0.0;
|
||||
|
||||
@compute @workgroup_size(8, 8, 8)
|
||||
fn enable_gen_entry(compute_input: ComputeInput) {
|
||||
let instance = tape_ids[push_constants.object_index];
|
||||
let metadata = tape_metadata_buffers[instance.metadata_buffer_id].metadata[
|
||||
instance.metadata_buffer_offset
|
||||
];
|
||||
|
||||
let total_invocations = compute_input.num_workgroups * vec3u(8);
|
||||
let base_index =
|
||||
(((compute_input.global_invocation_id.z * total_invocations.y)
|
||||
+ compute_input.global_invocation_id.y) * total_invocations.x)
|
||||
+ compute_input.global_invocation_id.x;
|
||||
|
||||
let internal_pos = vec3(
|
||||
((base_index >> (metadata.subdivision_log2 * 2))
|
||||
& ((1 << metadata.subdivision_log2) - 1)) / (1 << metadata.subdivision_log2),
|
||||
((base_index >> (metadata.subdivision_log2 * 1))
|
||||
& ((1 << metadata.subdivision_log2) - 1)) / (1 << metadata.subdivision_log2),
|
||||
((base_index >> (metadata.subdivision_log2 * 0))
|
||||
& ((1 << metadata.subdivision_log2) - 1)) / (1 << metadata.subdivision_log2),
|
||||
);
|
||||
|
||||
let low_corner =
|
||||
(((vec3(0.0) / (1 << metadata.subdivision_log2)) + internal_pos) * metadata.dimensions)
|
||||
+ metadata.lowest_corner;
|
||||
let high_corner =
|
||||
(((vec3(1.0) / (1 << metadata.subdivision_log2)) + internal_pos) * metadata.dimensions)
|
||||
+ metadata.lowest_corner;
|
||||
|
||||
let interval = interval_scene(
|
||||
vec4(low_corner, push_constants.time),
|
||||
vec4(high_corner, push_constants.time),
|
||||
);
|
||||
|
||||
let aabb = &aabb_buffers[instance.aabb_buffer_id].aabbs[instance.aabb_buffer_offset];
|
||||
|
||||
let en = (interval.x < 0) && (interval.y > 0);
|
||||
|
||||
if en {
|
||||
aabb.min = low_corner;
|
||||
aabb.max = high_corner;
|
||||
} else {
|
||||
aabb.min.x = NAN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
struct PackedAabb {
|
||||
min: vec3<f32>,
|
||||
max: vec3<f32>,
|
||||
}
|
||||
@@ -395,6 +395,7 @@ impl TapeAllocator {
|
||||
TapeMetadata {
|
||||
aabb_center,
|
||||
aabb_half_extents,
|
||||
subdivision_log2: tape.subdivision.ilog2(),
|
||||
..Default::default()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// These functions will be overwritten
|
||||
|
||||
fn scene(p: vec4<f32>) -> f32 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
fn interval_scene(pl: vec4<f32>, ph: vec4<f32>) -> vec2<f32> {
|
||||
return vec2(0.0);
|
||||
}
|
||||
|
||||
fn gradient_scene(p: vec4<f32>) -> vec4<f32> {
|
||||
return vec4(0.0);
|
||||
}
|
||||
@@ -5,4 +5,5 @@ struct TapeMetadata {
|
||||
pad_a: u32,
|
||||
aabb_half_extents: vec3<f32>,
|
||||
pad_b: u32,
|
||||
subdivision_log2: u32,
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@ pub mod allocator;
|
||||
|
||||
use allocator::TapeAllocatorPlugin;
|
||||
use bevy_app::{App, Plugin};
|
||||
use bevy_asset::{AssetId, AssetServer, Assets, Handle};
|
||||
use bevy_asset::{AssetId, AssetServer, Assets, Handle, load_embedded_asset};
|
||||
use bevy_ecs::{
|
||||
prelude::*,
|
||||
system::{SystemParamItem, lifetimeless::SRes},
|
||||
@@ -25,19 +25,31 @@ use bevy_render::{
|
||||
use bevy_shader::{Shader, load_shader_library};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use rspirv::dr::Module;
|
||||
use wgpu::{ShaderModuleDescriptor, ShaderSource, include_spirv};
|
||||
|
||||
use crate::{
|
||||
render::allocator::{ElementClass, TapeAllocationKey, TapeAllocator, TapeSlabId},
|
||||
tape::Tape,
|
||||
};
|
||||
|
||||
fn get_module(shader: ShaderModuleDescriptor) -> Module {
|
||||
let ShaderSource::SpirV(source) = shader.source else {
|
||||
unreachable!()
|
||||
};
|
||||
let mut loader = rspirv::dr::Loader::new();
|
||||
rspirv::binary::parse_words(source, &mut loader).unwrap();
|
||||
loader.module()
|
||||
}
|
||||
|
||||
/// 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, "aabb.wesl");
|
||||
load_shader_library!(app, "metadata_types.wesl");
|
||||
load_shader_library!(app, "implicit.wesl");
|
||||
|
||||
app.add_plugins(RenderAssetPlugin::<RenderTape>::default())
|
||||
.add_plugins(TapeAllocatorPlugin);
|
||||
@@ -46,6 +58,19 @@ impl Plugin for TapeRenderAssetPlugin {
|
||||
return;
|
||||
};
|
||||
|
||||
let enable_gen = include_spirv!("../../shaders_out/replacement/enable_gen.comp.glsl.spv");
|
||||
|
||||
render_app.insert_resource(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", &[]),
|
||||
fuzz_layout: BindGroupLayoutDescriptor::new("Tape Fuzz Layout", &[]),
|
||||
spatial_texture_layout: BindGroupLayoutDescriptor::new("Spatial Texture Layout", &[]),
|
||||
});
|
||||
|
||||
render_app.add_systems(
|
||||
Render,
|
||||
prepare_tape_metadata_fallback_buffer
|
||||
@@ -128,6 +153,7 @@ pub struct TapeMetadata {
|
||||
// AABB for decompressing positions.
|
||||
pub aabb_half_extents: Vec3,
|
||||
pub pad2: u32,
|
||||
pub subdivision_log2: u32,
|
||||
}
|
||||
|
||||
/// The render world representation of a [`Tape`].
|
||||
@@ -207,7 +233,7 @@ 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(),
|
||||
entry_point: Some("enable_gen_entry".into()),
|
||||
entry_point: Some("main".into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
@@ -215,7 +241,7 @@ 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(),
|
||||
entry_point: Some("fuzz_entry".into()),
|
||||
entry_point: Some("main".into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
@@ -224,7 +250,7 @@ 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(),
|
||||
entry_point: Some("spatial_texture_entry".into()),
|
||||
entry_point: Some("main".into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
|
||||
@@ -65,6 +65,9 @@ impl Tape {
|
||||
|
||||
let path = format!("bevy_tape_drive::gen_shader::{name}::{key}");
|
||||
|
||||
std::fs::create_dir_all("shader_debug").unwrap();
|
||||
std::fs::write(format!("shader_debug/{}_{}.spv", name, key), &bytes).unwrap();
|
||||
|
||||
Shader::from_spirv(bytes, path)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user