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; import bevy_tape_load::render::trace::trace_tape; import bevy_tape_load::render::normal::get_normal; enable wgpu_ray_query; enable wgpu_binding_array; struct InstanceGeometryIds { vertex_buffer_id: u32, vertex_buffer_offset: u32, index_buffer_id: u32, index_buffer_offset: u32, triangle_count: u32, } struct InstanceTapeIds { aabb_buffer_id: u32, aabb_buffer_offset: u32, aabb_count: u32, spatial_texture_id: u32, metadata_buffer_id: u32, metadata_buffer_offset: u32, } struct VertexBuffer { vertices: array, } struct IndexBuffer { indices: array, } struct AabbBuffer { aabbs: array, } struct TapeMetadataBuffer { metadata: array, } struct PackedVertex { a: vec4, b: vec4, tangent: vec4, } struct Vertex { position: vec3, normal: vec3, uv: vec2, tangent: vec4, } fn unpack_vertex(packed: PackedVertex) -> Vertex { var vertex: Vertex; vertex.position = packed.a.xyz; vertex.normal = vec3(packed.a.w, packed.b.xy); vertex.uv = packed.b.zw; vertex.tangent = packed.tangent; return vertex; } struct Material { normal_map_texture_id: u32, base_color_texture_id: u32, emissive_texture_id: u32, metallic_roughness_texture_id: u32, base_color: vec3, perceptual_roughness: f32, emissive: vec3, metallic: f32, reflectance: f32, flags: u32, uv_translation: vec2, uv_transform: mat2x2, alpha: f32, alpha_cutoff: f32, _padding: vec2, } const TEXTURE_MAP_NONE = 0xFFFFFFFFu; const MATERIAL_FLAG_DOUBLE_SIDED = 1u << 0u; const MATERIAL_FLAG_FLIP_NORMAL_MAP_Y = 1u << 1u; const MIRROR_ROUGHNESS_THRESHOLD = 0.001f; struct LightSource { kind: u32, // LIGHT_SOURCE_KIND_BITS bits for kind, rest of the bits for extra data id: u32, } const LIGHT_SOURCE_KIND_BITS = 3u; const LIGHT_SOURCE_KIND_EMISSIVE_MESH = 0u; const LIGHT_SOURCE_KIND_DIRECTIONAL = 1u; const LIGHT_SOURCE_KIND_POINT = 2u; const LIGHT_SOURCE_KIND_RECT = 3u; fn light_source_kind(light_source: LightSource) -> u32 { return light_source.kind & ((1u << LIGHT_SOURCE_KIND_BITS) - 1u); } fn emissive_mesh_triangle_count(light_source: LightSource) -> u32 { return light_source.kind >> LIGHT_SOURCE_KIND_BITS; } struct DirectionalLight { direction_to_light: vec3, cos_theta_max: f32, luminance: vec3, inverse_pdf: f32, } /// A point or spot light. Point lights have no cone, e.g. `cos_outer <= -1`. struct PointLight { position: vec3, radius: f32, intensity: vec3, cos_inner: f32, axis: vec3, cos_outer: f32, } /// A one-sided rectangular light facing `cross(edge_y, edge_x)`. struct RectLight { center: vec3, area: f32, edge_x: vec3, _padding0: f32, edge_y: vec3, _padding1: f32, radiance: vec3, _padding2: f32, } struct EnvironmentMapLight { light_from_world: mat3x3, intensity: f32, } const LIGHT_NOT_PRESENT_THIS_FRAME = 0xFFFFFFFFu; @group(0) @binding(0) var vertex_buffers: binding_array; @group(0) @binding(1) var index_buffers: binding_array; @group(0) @binding(2) var aabb_buffers: binding_array; @group(0) @binding(3) var tape_metadata_buffers: binding_array; @group(0) @binding(6) var textures: binding_array>; @group(0) @binding(7) var samplers: binding_array; @group(0) @binding(8) var materials: array; @group(0) @binding(9) var tlas: acceleration_structure; @group(0) @binding(10) var previous_frame_tlas: acceleration_structure; @group(0) @binding(11) var transforms: array>; @group(0) @binding(12) var previous_frame_transforms: array>; @group(0) @binding(13) var geometry_ids: array; @group(0) @binding(14) var tape_ids: array; @group(0) @binding(15) var material_ids: array; @group(0) @binding(16) var brdf_dfg_lut: texture_2d; @group(0) @binding(17) var brdf_dfg_lut_sampler: sampler; @group(0) @binding(18) var light_sources: array; @group(0) @binding(19) var directional_lights: array; @group(0) @binding(20) var point_lights: array; @group(0) @binding(21) var rect_lights: array; @group(0) @binding(22) var environment_map: texture_cube; @group(0) @binding(23) var environment_map_sampler: sampler; @group(0) @binding(24) var environment_map_light: EnvironmentMapLight; @group(0) @binding(25) var previous_frame_light_id_translations: array; const RAY_T_MIN = 0.001f; const RAY_T_MAX = 100000.0f; const RAY_NO_CULL = 0xFFu; fn trace_ray(ray_origin: vec3, ray_direction: vec3, 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); while rayQueryProceed(&rq) { let candidate = rayQueryGetCandidateIntersection(&rq); if candidate.kind == RAY_QUERY_INTERSECTION_TRIANGLE { if passes_alpha_test(candidate) { rayQueryConfirmIntersection(&rq); } } if candidate.kind == RAY_QUERY_INTERSECTION_AABB { let t = tape_confirm_intersection(candidate.instance_index, candidate.geometry_index, ray_origin, ray_direction); if t > 0.0 { rayQueryGenerateIntersection(&rq, t); } } rayQueryTerminate(&rq); } return rayQueryGetCommittedIntersection(&rq); } fn trace_ray_previous_frame(ray_origin: vec3, ray_direction: vec3, 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); while rayQueryProceed(&rq) { let candidate = rayQueryGetCandidateIntersection(&rq); if candidate.kind == RAY_QUERY_INTERSECTION_TRIANGLE { if passes_alpha_test(candidate) { rayQueryConfirmIntersection(&rq); } } if candidate.kind == RAY_QUERY_INTERSECTION_AABB { let t = tape_confirm_intersection(candidate.instance_index, candidate.geometry_index, ray_origin, ray_direction); if t > 0.0 { rayQueryGenerateIntersection(&rq, t); } } rayQueryTerminate(&rq); } return rayQueryGetCommittedIntersection(&rq); } fn passes_alpha_test(candidate: RayIntersection) -> bool { let barycentrics = vec3(1.0 - candidate.barycentrics.x - candidate.barycentrics.y, candidate.barycentrics); return !resolve_triangle_data_full(candidate.instance_custom_data, candidate.primitive_index, barycentrics).material.alpha_masked_out; } fn sample_texture(id: u32, uv: vec2) -> vec4 { return textureSampleLevel(textures[id], samplers[id], uv, 0.0); // TODO: Mipmap } struct ResolvedMaterial { base_color: vec3, emissive: vec3, reflectance: f32, perceptual_roughness: f32, roughness: f32, metallic: f32, alpha_masked_out: bool, } struct ResolvedRayHitFull { world_position: vec3, previous_frame_world_position: vec3, world_normal: vec3, geometric_world_normal: vec3, world_tangent: vec4, uv: vec2, triangle_area: f32, triangle_count: u32, material: ResolvedMaterial, double_sided: bool, } fn resolve_material(material: Material, uv: vec2) -> ResolvedMaterial { var m: ResolvedMaterial; m.base_color = material.base_color.rgb; var alpha = material.alpha; if material.base_color_texture_id != TEXTURE_MAP_NONE { let base_color = sample_texture(material.base_color_texture_id, uv); m.base_color *= base_color.rgb; alpha *= base_color.a; } m.emissive = material.emissive.rgb; if material.emissive_texture_id != TEXTURE_MAP_NONE { m.emissive *= sample_texture(material.emissive_texture_id, uv).rgb; } m.alpha_masked_out = alpha < material.alpha_cutoff; if m.alpha_masked_out { m.emissive = vec3(0.0); } m.reflectance = material.reflectance; m.perceptual_roughness = material.perceptual_roughness; m.metallic = material.metallic; if material.metallic_roughness_texture_id != TEXTURE_MAP_NONE { let metallic_roughness = sample_texture(material.metallic_roughness_texture_id, uv).rgb; m.perceptual_roughness *= metallic_roughness.g; m.metallic *= metallic_roughness.b; } m.roughness = m.perceptual_roughness * m.perceptual_roughness; return m; } fn resolve_ray_hit_full(ray_hit: RayIntersection, ray_origin: vec3, ray_direction: vec3) -> ResolvedRayHitFull { var resolved: ResolvedRayHitFull; if ray_hit.kind == RAY_QUERY_INTERSECTION_TRIANGLE { let barycentrics = vec3(1.0 - ray_hit.barycentrics.x - ray_hit.barycentrics.y, ray_hit.barycentrics); resolved = resolve_triangle_data_full(ray_hit.instance_custom_data, ray_hit.primitive_index, barycentrics); } else { //RAY_QUERY_INTERSECTION_GENERATED resolved = resolve_tape_data_full(ray_hit.instance_custom_data, ray_hit.geometry_index, fma(vec3(ray_hit.t), ray_direction, ray_origin)); } // Flip normals for backfacing hits of double sided materials if !ray_hit.front_face && resolved.double_sided { resolved.world_normal = -resolved.world_normal; resolved.geometric_world_normal = -resolved.geometric_world_normal; // Emissive surfaces only emit from their front side resolved.material.emissive = vec3(0.0); } return resolved; } fn load_vertices(instance_geometry_ids: InstanceGeometryIds, triangle_id: u32) -> array { 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; return array( unpack_vertex((*vertex_buffer)[indices.x]), unpack_vertex((*vertex_buffer)[indices.y]), unpack_vertex((*vertex_buffer)[indices.z]) ); } fn transform_positions(transform: mat4x4, vertices: array) -> array, 3> { return array, 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 ); } fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics: vec3) -> ResolvedRayHitFull { let material_id = material_ids[instance_id]; let material = materials[material_id]; let transform = affine3_to_square(transforms[instance_id]); let previous_frame_transform = affine3_to_square(previous_frame_transforms[instance_id]); let instance_geometry_ids = geometry_ids[instance_id]; 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 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 mesh_uv = mat3x2(vertices[0].uv, vertices[1].uv, vertices[2].uv) * barycentrics; let uv = material.uv_transform * mesh_uv + material.uv_translation; 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 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_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; if material.normal_map_texture_id != TEXTURE_MAP_NONE { let TBN = calculate_tbn_mikktspace(world_normal, world_tangent); let T = TBN[0]; let B = TBN[1]; let N = TBN[2]; var Nt = sample_texture(material.normal_map_texture_id, uv).rgb * 2.0 - 1.0; if (material.flags & MATERIAL_FLAG_FLIP_NORMAL_MAP_Y) != 0u { Nt.y = -Nt.y; } 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); } let triangle_edge0 = world_vertices[0] - world_vertices[1]; let triangle_edge1 = world_vertices[0] - world_vertices[2]; let triangle_area = length(cross(triangle_edge0, triangle_edge1)) / 2.0; 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, (material.flags & MATERIAL_FLAG_DOUBLE_SIDED) != 0u, ); } fn tape_confirm_intersection(instance_id: u32, geometry_index: u32, ray_position: vec3, ray_direction: vec3) -> f32 { let instance_tape_ids = tape_ids[instance_id]; let metadata = tape_metadata_buffers[instance_tape_ids.metadata_buffer_id].metadata[instance_tape_ids.metadata_buffer_offset]; return trace_tape(metadata, instance_tape_ids.spatial_texture_id, geometry_index, ray_position, ray_direction); } fn resolve_tape_data_full(instance_id: u32, geometry_index: u32, position: vec3) -> ResolvedRayHitFull { let material_id = material_ids[instance_id]; let material = materials[material_id]; let transform = affine3_to_square(transforms[instance_id]); let previous_frame_transform = affine3_to_square(previous_frame_transforms[instance_id]); let instance_tape_ids = tape_ids[instance_id]; let metadata = tape_metadata_buffers[instance_tape_ids.metadata_buffer_id].metadata[instance_tape_ids.metadata_buffer_offset]; let local_position = position; // dont think about it let world_position: vec3 = (transform * vec4(local_position, 1.0)).xyz; let previous_frame_world_position: vec3 = (previous_frame_transform * vec4(local_position, 1.0)).xyz; let mesh_uv: vec2 = vec2(0.0, 0.0); let uv: vec2 = material.uv_transform * mesh_uv + material.uv_translation; 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 determinant = dot(transform_3x3[0], cofactor[0]); let determinant_sign = select(sign(determinant), 1.0, determinant == 0.0); let local_tangent = vec3(1.0, 0.0, 0.0); // Implicit surfaces don't have tangent vectors let world_tangent: vec4 = vec4(normalize(transform_3x3 * local_tangent), 1.0); let local_normal: vec3 = get_normal(metadata, instance_tape_ids.spatial_texture_id, local_position); var world_normal: vec3 = normalize(determinant_sign * (cofactor * local_normal)); let geometric_world_normal: vec3 = world_normal; if material.normal_map_texture_id != TEXTURE_MAP_NONE { let TBN = calculate_tbn_mikktspace(world_normal, world_tangent); let T = TBN[0]; let B = TBN[1]; let N = TBN[2]; var Nt = sample_texture(material.normal_map_texture_id, uv).rgb * 2.0 - 1.0; if (material.flags & MATERIAL_FLAG_FLIP_NORMAL_MAP_Y) != 0u { Nt.y = -Nt.y; } 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); } let triangle_area: f32 = 0.0; // ??? let triangle_count: u32 = 0; // ??? 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, triangle_count, resolved_material, (material.flags & MATERIAL_FLAG_DOUBLE_SIDED) != 0u, ); }