3#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance 4#import bevy_pbr::pbr_functions::calculate_F0 5#import bevy_pbr::utils::{rand_f, rand_vec2f} 6#import bevy_render::maths::{PI, orthonormalize} 7#import bevy_render::view::View 8#import bevy_solari::brdf::{evaluate_brdf, evaluate_and_sample_brdf, fresnel} 9#import bevy_solari::sampling::{sample_random_light, random_emissive_light_pdf, ggx_vndf_pdf, power_heuristic} 10#import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, ResolvedRayHitFull, RAY_T_MIN, RAY_T_MAX, MIRROR_ROUGHNESS_THRESHOLD} 12@group(1) @binding(0) var accumulation_texture: texture_storage_2d<rgba32float, read_write>; 13@group(1) @binding(1) var view_output: texture_storage_2d<rgba16float, write>; 14@group(1) @binding(2) var<uniform> view: View; 16@compute @workgroup_size(8, 8, 1) 17fn pathtrace(@builtin(global_invocation_id) global_id: vec3<u32>) { 18 if any(global_id.xy >= vec2u(view.viewport.zw)) { 22 let old_color = textureLoad(accumulation_texture, global_id.xy); 25 let pixel_index = global_id.x + global_id.y * u32(view.viewport.z); 26 let frame_index = u32(old_color.a) * 5782582u; 27 var rng = pixel_index + frame_index; 30 let pixel_center = vec2<f32>(global_id.xy) + 0.5; 31 let jitter = rand_vec2f(&rng) - 0.5; 32 let pixel_uv = (pixel_center + jitter) / view.viewport.zw; 33 let pixel_ndc = (pixel_uv * 2.0) - 1.0; 34 let primary_ray_target = view.world_from_clip * vec4(pixel_ndc.x, -pixel_ndc.y, 1.0, 1.0); 35 var ray_origin = view.world_position; 36 var ray_direction = normalize((primary_ray_target.xyz / primary_ray_target.w) - ray_origin); 40 var radiance = vec3(0.0); 41 var throughput = vec3(1.0); 44 let ray = trace_ray(ray_origin, ray_direction, ray_t_min, RAY_T_MAX, RAY_FLAG_NONE); 45 if ray.kind != RAY_QUERY_INTERSECTION_NONE { 46 let ray_hit = resolve_ray_hit_full(ray); 47 let wo = -ray_direction; 52 let p_light = random_emissive_light_pdf(ray_hit); 53 mis_weight = power_heuristic(p_bounce, p_light); 55 radiance += mis_weight * throughput * ray_hit.material.emissive; 59 let is_perfectly_specular = ray_hit.material.roughness <= MIRROR_ROUGHNESS_THRESHOLD && ray_hit.material.metallic > 0.9999; 60 if !is_perfectly_specular { 61 let direct_lighting = sample_random_light(ray_hit.world_position, ray_hit.world_normal, &rng); 64 if direct_lighting.brdf_rays_can_hit { 65 let pdf_of_bounce = brdf_pdf(wo, direct_lighting.wi, ray_hit); 66 mis_weight = power_heuristic(1.0 / direct_lighting.inverse_pdf, pdf_of_bounce); 69 let direct_lighting_brdf = evaluate_brdf(wo, direct_lighting.wi, ray_hit.world_normal, ray_hit.material); 70 radiance += mis_weight * throughput * direct_lighting.radiance * direct_lighting.inverse_pdf * direct_lighting_brdf; 74 let next_bounce = evaluate_and_sample_brdf(wo, ray_hit.world_normal, ray_hit.material, &rng); 75 if next_bounce.pdf == 0.0 { break; } 76 ray_direction = next_bounce.wi; 77 ray_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN); 78 ray_t_min = RAY_T_MIN; 79 p_bounce = next_bounce.pdf; 80 throughput *= next_bounce.throughput; 83 let p = luminance(throughput); 84 if rand_f(&rng) > p { break; } 90 radiance *= view.exposure; 93 let new_color = mix(old_color.rgb, radiance, 1.0 / (old_color.a + 1.0)); 94 textureStore(accumulation_texture, global_id.xy, vec4(new_color, old_color.a + 1.0)); 95 textureStore(view_output, global_id.xy, vec4(new_color, 1.0)); 98fn brdf_pdf(wo: vec3<f32>, wi: vec3<f32>, ray_hit: ResolvedRayHitFull) -> f32 { 99 let NdotV = max(dot(ray_hit.world_normal, wo), 0.0001); 100 let F0 = calculate_F0(ray_hit.material.base_color, ray_hit.material.metallic, vec3(ray_hit.material.reflectance)); 101 let df = 1.0 - luminance(fresnel(F0, NdotV)); 103 let diffuse_weight = mix(df, 0.0, ray_hit.material.metallic); 104 let specular_weight = 1.0 - diffuse_weight; 106 let TBN = orthonormalize(ray_hit.world_normal); 111 let wo_tangent = vec3(dot(wo, T), dot(wo, B), dot(wo, N)); 112 let wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N)); 114 let diffuse_pdf = wi_tangent.z / PI; 115 let specular_pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, ray_hit.material.roughness); 116 let pdf = (diffuse_weight * diffuse_pdf) + (specular_weight * specular_pdf);