GPU path tracer: the path shader compiles on NVIDIA's Vulkan driver (#6705) (#6850)

Native compilers inline every call, and `main` inlined 33 BVH walks: three
sample_path calls, and a shadow walk per light source family. NVIDIA's
compiler (driver 610.88) crashed in nvgpucomp64.dll or ran out of memory.

- main settles the adaptive budget first, then samples at one call site.
- direct_light takes over direct_studio, visible and the fixed-light loop:
  one loop over the sources with one shadow-ray call site, drawing random
  numbers in the same order.
- next_vertex (a select) replaces (a+1u)%3u: after a BVH walk that index
  crashed NVIDIA's compiler within seconds.

main now inlines 5 BVH walks; main_inlines_few_bvh_walks counts them with
naga, so the limit holds without an NVIDIA GPU.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Frank
2026-10-30 13:28:57 +00:00
committed by GitHub
parent f331a0740b
commit 44a9f9ee42
2 changed files with 119 additions and 62 deletions
+77 -62
View File
@@ -574,17 +574,22 @@ fn dominant_vertex(bary: vec3<f32>) -> 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<f32>) -> vec3<f32> {
// 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<vec3<f32>,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<f32>) -> PrecisePoint {
let p=array<vec3<f32>,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<f32>, origin: vec3<f32>, or
}
fn surface_ray_depth(t: Triangle, bary: vec3<f32>, origin: vec3<f32>, axis: u32) -> f32 {
let p=array<vec3<f32>,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>) -> f32 {
if cosine<=0.0 { return 0.0; }
return dot(delta,delta)/(light.center.w*cosine);
}
fn visible(o: vec3<f32>, d: vec3<f32>, distance: f32) -> ShadowResult {
if emitter_hit(o,d).distance<distance-u.lighting.x*2.0 { return blocked_shadow(); }
var visibility=shadow_transmittance(o,d,distance);
if u.volume_min.w>0.0 {
let attenuation=volume_transmittance(o,d,distance);
visibility.transmittance*=attenuation;
visibility.branch_pdf*=attenuation;
}
return visibility;
}
struct EnvironmentSample { direction: vec3<f32>, radiance: vec3<f32>, pdf: f32 };
fn environment_to_world(d: vec3<f32>) -> vec3<f32> {
return vec3<f32>(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<function,u32>)->EnvironmentSample {
let direction=from_local(vec3<f32>(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<f32>, o: vec3<f32>, n: vec3<f32>, geometric: vec3<f32>, v: vec3<f32>, remaining: u32, rng: ptr<function,u32>) -> vec3<f32> {
// The three fixed lights of the preview without studio lighting: direction and intensity.
const FIXED_LIGHTS = array<vec4<f32>,3>(vec4<f32>(0.6,0.0,0.8,3.0),vec4<f32>(-0.8,0.0,0.6,1.0),vec4<f32>(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<f32>, o: vec3<f32>, n: vec3<f32>, geometric: vec3<f32>, v: vec3<f32>, remaining: u32, rng: ptr<function,u32>) -> vec3<f32> {
var result = vec3<f32>(0.0);
let opaque = 1.0-t.base.w;
if opaque<=0.0 { return result; }
for (var i=0u; i<u32(u.light_count.w); i+=1u) {
let light = u.area_lights[i];
let point = light.center.xyz + light.u.xyz*(2.0*random(rng)-1.0) + light.v.xyz*(2.0*random(rng)-1.0);
let delta = point-position;
let direction = unit(delta);
let pdf = area_pdf(light,delta);
let nl = dot(n,direction);
if pdf<=0.0 || nl<=0.0 || dot(geometric,direction)<=0.0 { continue; }
let shadow = point-o;
let visibility=visible(o,unit(shadow),length(shadow));
let competing=opaque*surface_pdf(t,n,v,direction)*visibility.branch_pdf;
let weight=select(power_weight(pdf,competing),1.0,visibility.solid || visibility.crossings>=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<f32>(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<count; source+=1u) {
var sample = EnvironmentSample(vec3<f32>(0.0,0.0,1.0),vec3<f32>(0.0),0.0);
var shadow_direction = vec3<f32>(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<f32>(light.w*u.lighting.z*u.studio_weights[source]),1.0);
shadow_direction = light.xyz;
} else {
if source<area_count {
let light = u.area_lights[source];
let point = light.center.xyz + light.u.xyz*(2.0*random(rng)-1.0) + light.v.xyz*(2.0*random(rng)-1.0);
let delta = point-position;
sample = EnvironmentSample(unit(delta),light.radiance.xyz,area_pdf(light,delta));
let shadow = point-o;
shadow_direction = unit(shadow);
shadow_distance = length(shadow);
} else if source==area_count {
if u.environment_map.w<=0.0 && !any(u.environment.xyz>vec3<f32>(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<f32> {
var previous_delta = true;
var media: array<Medium,16>;
var medium_count = 0u;
let lights = array<vec4<f32>,3>(vec4<f32>(0.6,0.0,0.8,3.0),vec4<f32>(-0.8,0.0,0.6,1.0),vec4<f32>(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<f32> {
}
previous_position = position; previous_position_low=position_low;
o = position + geometric*(u.lighting.x*2.0); o_low=vec3<f32>(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<u32>) {
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<f32>(0.0); m2=vec4<f32>(0.0); }
@@ -1127,10 +1144,8 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
}
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); }
}
+42
View File
@@ -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<naga::Handle<naga::Function>>) {
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();