it starts at least
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user