diff --git a/crates/gpu/src/path/path.wgsl b/crates/gpu/src/path/path.wgsl index 7c6aa05f5..35e3f081d 100644 --- a/crates/gpu/src/path/path.wgsl +++ b/crates/gpu/src/path/path.wgsl @@ -574,17 +574,22 @@ fn dominant_vertex(bary: vec3) -> u32 { if bary.z>bary[anchor] { anchor=2u; } return anchor; } +// The vertex after `a`, as (a+1)%3. Not written with `%`: NVIDIA's Vulkan compiler crashes on +// that index after a BVH walk (#6705). +fn next_vertex(a: u32) -> u32 { + return select(a+1u,0u,a>=2u); +} fn surface_point(t: Triangle, bary: vec3) -> vec3 { // Anchor near the hit, retaining the two other directly computed weights. // A tiny weight on a distant vertex can encode an entire corner clearance. // Do not recover that weight by subtracting the other two from one. let p=array,3>(t.p0.xyz,t.p1.xyz,t.p2.xyz); - let a=dominant_vertex(bary); let b=(a+1u)%3u; let c=(a+2u)%3u; + let a=dominant_vertex(bary); let b=next_vertex(a); let c=next_vertex(b); return p[a]+((p[b]-p[a])*bary[b]+(p[c]-p[a])*bary[c]); } fn surface_point_precise(t: Triangle, bary: vec3) -> PrecisePoint { let p=array,3>(t.p0.xyz,t.p1.xyz,t.p2.xyz); - let a=dominant_vertex(bary); let b=(a+1u)%3u; let c=(a+2u)%3u; + let a=dominant_vertex(bary); let b=next_vertex(a); let c=next_vertex(b); // Keep the world anchor separate from its local displacement. return PrecisePoint(p[a],(p[b]-p[a])*bary[b]+(p[c]-p[a])*bary[c]); } @@ -595,7 +600,7 @@ fn surface_ray_depth_precise(t: Triangle, bary: vec3, origin: vec3, or } fn surface_ray_depth(t: Triangle, bary: vec3, origin: vec3, axis: u32) -> f32 { let p=array,3>(t.p0.xyz,t.p1.xyz,t.p2.xyz); - let a=dominant_vertex(bary); let b=(a+1u)%3u; let c=(a+2u)%3u; + let a=dominant_vertex(bary); let b=next_vertex(a); let c=next_vertex(b); // Original vertex differences avoid subtracting two rounded ray-relative // coordinates; adding the anchor last preserves distant thin-hit ordering. return (p[a][axis]-origin[axis])+((p[b][axis]-p[a][axis])*bary[b]+(p[c][axis]-p[a][axis])*bary[c]); @@ -660,16 +665,6 @@ fn area_pdf(light: AreaLight, delta: vec3) -> f32 { if cosine<=0.0 { return 0.0; } return dot(delta,delta)/(light.center.w*cosine); } -fn visible(o: vec3, d: vec3, distance: f32) -> ShadowResult { - if emitter_hit(o,d).distance0.0 { - let attenuation=volume_transmittance(o,d,distance); - visibility.transmittance*=attenuation; - visibility.branch_pdf*=attenuation; - } - return visibility; -} struct EnvironmentSample { direction: vec3, radiance: vec3, pdf: f32 }; fn environment_to_world(d: vec3) -> vec3 { return vec3(u.environment_map.x*d.x-u.environment_map.y*d.y,u.environment_map.y*d.x+u.environment_map.x*d.y,d.z); @@ -723,43 +718,67 @@ fn sun_sample(rng:ptr)->EnvironmentSample { let direction=from_local(vec3(radius*cos(phi),radius*sin(phi),z),u.sun_direction.xyz); return EnvironmentSample(direction,u.sun_radiance.xyz,u.sun_radiance.w); } -fn direct_studio(t: Triangle, position: vec3, o: vec3, n: vec3, geometric: vec3, v: vec3, remaining: u32, rng: ptr) -> vec3 { +// The three fixed lights of the preview without studio lighting: direction and intensity. +const FIXED_LIGHTS = array,3>(vec4(0.6,0.0,0.8,3.0),vec4(-0.8,0.0,0.6,1.0),vec4(0.0,-0.6,0.8,1.5)); +// Next-event estimation for every light source family through ONE shadow-ray call site, like +// volume_direct. Native compilers inline each call of the BVH walk; one walk per source family +// made `main` too large for NVIDIA's Vulkan compiler (#6705). Studio lighting visits every area +// light, then the environment, then the sun, drawing random numbers in that order; without it +// the three fixed lights are used. +fn direct_light(t: Triangle, position: vec3, o: vec3, n: vec3, geometric: vec3, v: vec3, remaining: u32, rng: ptr) -> vec3 { var result = vec3(0.0); let opaque = 1.0-t.base.w; - if opaque<=0.0 { return result; } - for (var i=0u; i=remaining); - result += visibility.transmittance*brdf(t,n,v,direction)*light.radiance.xyz*(opaque*nl*weight/pdf); - } - if u.environment_map.w>0.0 || any(u.environment.xyz>vec3(0.0)) { - let sample=env_sample(rng); - let nl=dot(n,sample.direction); - if sample.pdf>0.0 && nl>0.0 && dot(geometric,sample.direction)>0.0 { - let visibility=visible(o,sample.direction,FAR); - let competing=opaque*surface_pdf(t,n,v,sample.direction)*visibility.branch_pdf; - let weight=select(power_weight(sample.pdf,competing),1.0,visibility.solid || visibility.crossings>=remaining); - result+=visibility.transmittance*brdf(t,n,v,sample.direction)*sample.radiance*(opaque*nl*weight/sample.pdf); + let studio = u.environment.w > 0.0; + if studio && opaque<=0.0 { return result; } + let area_count = u32(u.light_count.w); + let count = select(3u, area_count+2u, studio); + for (var source=0u; source(0.0,0.0,1.0),vec3(0.0),0.0); + var shadow_direction = vec3(0.0,0.0,1.0); + var shadow_distance = FAR; + if !studio { + let light = FIXED_LIGHTS[source]; + if u.lighting.z <= 0.0 || dot(geometric,light.xyz)<=0.0 || dot(n,light.xyz)<=0.0 { continue; } + sample = EnvironmentSample(light.xyz,vec3(light.w*u.lighting.z*u.studio_weights[source]),1.0); + shadow_direction = light.xyz; + } else { + if sourcevec3(0.0)) { continue; } + sample = env_sample(rng); + shadow_direction = sample.direction; + } else { + if u.sun_radiance.w<=0.0 { continue; } + sample = sun_sample(rng); + shadow_direction = sample.direction; + } + if sample.pdf<=0.0 || dot(n,sample.direction)<=0.0 || dot(geometric,sample.direction)<=0.0 { continue; } } - } - if u.sun_radiance.w>0.0 { - let sample=sun_sample(rng); - let nl=dot(n,sample.direction); - if nl>0.0 && dot(geometric,sample.direction)>0.0 { - let visibility=visible(o,sample.direction,FAR); - let competing=opaque*surface_pdf(t,n,v,sample.direction)*visibility.branch_pdf; - let weight=select(power_weight(sample.pdf,competing),1.0,visibility.solid || visibility.crossings>=remaining); - result+=visibility.transmittance*brdf(t,n,v,sample.direction)*sample.radiance*(opaque*nl*weight/sample.pdf); + // Studio emitters block a connection that passes them; studio volumes attenuate it. + var visibility = blocked_shadow(); + if !studio || emitter_hit(o,shadow_direction).distance>=shadow_distance-u.lighting.x*2.0 { + visibility = shadow_transmittance(o,shadow_direction,shadow_distance); + if studio && u.volume_min.w>0.0 { + let attenuation=volume_transmittance(o,shadow_direction,shadow_distance); + visibility.transmittance*=attenuation; + visibility.branch_pdf*=attenuation; + } } + let nl = dot(n,sample.direction); + if !studio { + result += visibility.transmittance * brdf(t,n,v,sample.direction) * (opaque*nl*sample.radiance); + continue; + } + let competing=opaque*surface_pdf(t,n,v,sample.direction)*visibility.branch_pdf; + let weight=select(power_weight(sample.pdf,competing),1.0,visibility.solid || visibility.crossings>=remaining); + result += visibility.transmittance*brdf(t,n,v,sample.direction)*sample.radiance*(opaque*nl*weight/sample.pdf); } return result; } @@ -816,7 +835,6 @@ fn sample_path_lane(pixel: u32, lane: u32) -> vec4 { var previous_delta = true; var media: array; var medium_count = 0u; - let lights = array,3>(vec4(0.6,0.0,0.8,3.0),vec4(-0.8,0.0,0.6,1.0),vec4(0.0,-0.6,0.8,1.5)); for (var bounce = 0u; bounce < u.sampling.y; bounce += 1u) { // Closed-glass continuations set an excluded producing triangle; every // other launch (camera, opaque or thin-sheet surface) does not. @@ -947,16 +965,7 @@ fn sample_path_lane(pixel: u32, lane: u32) -> vec4 { } previous_position = position; previous_position_low=position_low; o = position + geometric*(u.lighting.x*2.0); o_low=vec3(0.0); - if u.environment.w > 0.0 { - radiance += throughput*direct_studio(t,position,o,n,geometric,v,u.sampling.y-bounce-1u,&rng); - } - for (var i=0u; i<3u && u.environment.w==0.0; i+=1u) { - let light = lights[i]; - if u.lighting.z > 0.0 && dot(geometric,light.xyz)>0.0 && dot(n,light.xyz)>0.0 { - let visibility=shadow_transmittance(o,light.xyz,FAR); - radiance += visibility.transmittance * throughput * brdf(t,n,v,light.xyz) * ((1.0-t.base.w)*light.w*dot(n,light.xyz)*u.lighting.z*u.studio_weights[i]); - } - } + radiance += throughput*direct_light(t,position,o,n,geometric,v,u.sampling.y-bounce-1u,&rng); if t.base.w > 0.0 && random(&rng) < t.base.w { var nt = t.parameters.z; if t.parameters.w < 0.0 && entering && medium_count > 0u && u.lighting.w==0.0 { @@ -1105,14 +1114,22 @@ fn main(@builtin(global_invocation_id) gid: vec3) { let pilots = u.adaptive_budget.x; let strata = max(1u,u.adaptive_budget.z); let lane = u32(u.adaptive_error.w); + // Settle first whether this invocation samples at all, then take the path sample at one call + // site: every call is inlined, and three copies of the integrator were more than NVIDIA's + // Vulkan compiler could build (#6705). + var budget_count = 0u; + if pilots > 0u && u.image.z >= pilots { + budget_count = u32(accumulation[2u*pixels+pixel].x); + if u.image.z-pilots >= budget_count { return; } + } + let value = sample_path(pixel); if pilots == 0u { - let sample = sample_path(pixel)/f32(strata); + let sample = value/f32(strata); if u.image.z == 0u && lane == 0u { accumulation[pixel] = sample; } else { accumulation[pixel] += sample; } return; } if u.image.z < pilots { - let value = sample_path(pixel); var mean = accumulation[pixels+pixel]; var m2 = accumulation[2u*pixels+pixel]; if u.image.z == 0u { mean=vec4(0.0); m2=vec4(0.0); } @@ -1127,10 +1144,8 @@ fn main(@builtin(global_invocation_id) gid: vec3) { } return; } - let budget_count=u32(accumulation[2u*pixels+pixel].x); let ordinal=u.image.z-pilots; - if ordinal>=budget_count { return; } - accumulation[pixel] += sample_path(pixel)/f32(strata); + accumulation[pixel] += value/f32(strata); // Per-pixel production means enter both raw EXR and denoising. Pilots do not. if ordinal+1u == budget_count && lane+1u == strata { accumulation[pixel] /= f32(budget_count); } } diff --git a/crates/gpu/src/path/tests.rs b/crates/gpu/src/path/tests.rs index ed40cae2f..24e838ffc 100644 --- a/crates/gpu/src/path/tests.rs +++ b/crates/gpu/src/path/tests.rs @@ -531,6 +531,48 @@ fn shader_validates_without_a_device() { naga::valid::Validator::new(naga::valid::ValidationFlags::all(), naga::valid::Capabilities::empty()).validate(&module).unwrap(); } +/// #6705: native compilers inline every call. With 33 inlined BVH walks in `main` (three path +/// samples, a shadow walk per light source family) NVIDIA's Vulkan compiler crashed or ran out +/// of memory and never built the pipeline. Count the walks `main` inlines, so another call site +/// is a deliberate choice, checked on such a driver. +#[test] +fn main_inlines_few_bvh_walks() { + fn callees(block: &naga::Block, out: &mut Vec>) { + for statement in block.iter() { + match statement { + naga::Statement::Call { function, .. } => out.push(*function), + naga::Statement::Block(inner) => callees(inner, out), + naga::Statement::If { accept, reject, .. } => { + callees(accept, out); + callees(reject, out); + } + naga::Statement::Switch { cases, .. } => cases.iter().for_each(|case| callees(&case.body, out)), + naga::Statement::Loop { body, continuing, .. } => { + callees(body, out); + callees(continuing, out); + } + _ => {} + } + } + } + fn walks(module: &naga::Module, function: &naga::Function) -> usize { + let mut called = Vec::new(); + callees(&function.body, &mut called); + called + .into_iter() + .map(|handle| { + let callee = &module.functions[handle]; + let walk = matches!(callee.name.as_deref(), Some("trace_precise_within" | "trace_ordered_within")); + usize::from(walk) + walks(module, callee) + }) + .sum() + } + let module = naga::front::wgsl::parse_str(SHADER).unwrap(); + let main = module.entry_points.iter().find(|entry| entry.name == "main").unwrap(); + let inlined = walks(&module, &main.function); + assert!(inlined <= 5, "`main` inlines {inlined} BVH walks"); +} + #[test] fn render_lights_path_visibility_uniform_keeps_the_shader_contract() { shader_validates_without_a_device();