Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 133 additions & 120 deletions crates/gpu/src/path/path.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -184,56 +184,52 @@ fn brdf(t: Triangle, n: vec3<f32>, v: vec3<f32>, l: vec3<f32>) -> vec3<f32> {
// The bit interface also preserves subnormals for the arithmetic oracle; normal
// shader float consumers may still flush subnormal values under WGSL's rules.
fn fp_shift_jam(value: u32, amount: u32) -> u32 {
if amount == 0u { return value; }
if amount >= 32u { return select(0u,1u,value != 0u); }
let lost = value << (32u-amount);
return (value >> amount) | select(0u,1u,lost != 0u);
let shift=min(amount,31u);
let lost=value << ((32u-shift)&31u);
let shifted=(value >> shift) | select(0u,1u,(lost != 0u) & (shift != 0u));
return select(shifted,select(0u,1u,value != 0u),amount >= 32u);
}
fn fp_add_bits(left: u32, right: u32) -> u32 {
var a=left;
var b=right;
if (a&0x7fffffffu) < (b&0x7fffffffu) { a=right; b=left; }
// This helper is inlined many times by precise traversal and shadow rays.
// Keep its integer arithmetic in one block: nested branches/early returns
// make NVIDIA's Vulkan compiler expand the complete path shader excessively.
// Selects retain IEEE RTE, subnormals, signed zeros and canonical NaNs.
// Boolean &/| below are deliberate: &&/|| introduce short-circuit branches.
let swap=(left&0x7fffffffu)<(right&0x7fffffffu);
let a=select(left,right,swap);
let b=select(right,left,swap);
let sign=a&0x80000000u;
let ea=(a>>23u)&255u;
let eb=(b>>23u)&255u;
let fa=a&0x7fffffu;
let fb=b&0x7fffffu;
if ea == 255u {
if fa != 0u || (eb == 255u && fb != 0u) { return 0x7fc00000u; }
if eb == 255u && ((a^b)&0x80000000u) != 0u { return 0x7fc00000u; }
return a;
}
if (b&0x7fffffffu) == 0u {
if (a&0x7fffffffu) == 0u { return (a&b)&0x80000000u; }
return a;
}
var exponent=max(ea,1u);
let other_exponent=max(eb,1u);
let sa=(fa | select(0u,0x800000u,ea != 0u))<<3u;
let sb=fp_shift_jam((fb | select(0u,0x800000u,eb != 0u))<<3u,exponent-other_exponent);
var significand=0u;
if ((a^b)&0x80000000u) == 0u {
significand=sa+sb;
if significand >= 0x8000000u {
significand=fp_shift_jam(significand,1u);
exponent+=1u;
}
} else {
significand=sa-sb;
if significand == 0u { return 0u; }
// Nonzero significand is below bit 27. Shift to bit 26, or stop at
// the subnormal exponent, exactly as the bounded normalization loop.
let shift=min(countLeadingZeros(significand)-5u,exponent-1u);
significand<<=shift;
exponent-=shift;
}
let sb=fp_shift_jam((fb | select(0u,0x800000u,eb != 0u))<<3u,exponent-max(eb,1u));
let same_sign=((a^b)&0x80000000u)==0u;
let sum=sa+sb;
let carry=same_sign & (sum>=0x8000000u);
var significand=select(sa-sb,select(sum,fp_shift_jam(sum,1u),carry),same_sign);
exponent+=select(0u,1u,carry);
// Normalize subtraction to bit 26, stopping at the subnormal exponent.
// A zero significand is handled by the exact-cancellation select below.
let shift=select(min(countLeadingZeros(significand)-5u,exponent-1u),0u,same_sign);
significand<<=shift;
exponent-=shift;
let remainder=significand&7u;
var rounded=significand>>3u;
if remainder>4u || (remainder == 4u && (rounded&1u) != 0u) { rounded+=1u; }
if rounded >= 0x1000000u { rounded>>=1u; exponent+=1u; }
if exponent >= 255u { return sign|0x7f800000u; }
if rounded < 0x800000u { exponent=0u; }
return sign|(exponent<<23u)|(rounded&0x7fffffu);
var rounded=(significand>>3u)+select(0u,1u,(remainder>4u) | ((remainder==4u) & ((significand&8u)!=0u)));
let round_carry=rounded>=0x1000000u;
rounded>>=select(0u,1u,round_carry);
exponent+=select(0u,1u,round_carry);
let overflow=exponent>=255u;
exponent=select(exponent,0u,rounded<0x800000u);
var result=sign|(exponent<<23u)|(rounded&0x7fffffu);
result=select(result,sign|0x7f800000u,overflow);
result=select(result,0u,!same_sign & (significand==0u));
let zero=select(a,(a&b)&0x80000000u,(a&0x7fffffffu)==0u);
result=select(result,zero,(b&0x7fffffffu)==0u);
let nan=(fa!=0u) | ((eb==255u) & ((fb!=0u) | !same_sign));
return select(result,select(a,0x7fc00000u,nan),ea==255u);
}
fn fp_sub_bits(left: u32, right: u32) -> u32 {
return fp_add_bits(left,right^0x80000000u);
Expand All @@ -245,25 +241,27 @@ fn fp_sub_bits(left: u32, right: u32) -> u32 {
fn fp_sub_barrier_bits(left: u32, right: u32) -> u32 {
let a=left&0x7fffffffu;
let b=right&0x7fffffffu;
if a>=0x7f800000u || b>=0x7f800000u { return fp_sub_bits(left,right); }
if a==0u {
if b==0u { return left&(right^0x80000000u)&0x80000000u; }
return right^0x80000000u;
}
if b==0u { return left; }
if left==right { return 0u; }
let small=min(a,b);
let large=max(a,b);
let small_exponent=small>>23u;
let large_exponent=large>>23u;
let same_sign=((left^right)&0x80000000u)==0u;
let within_two=large_exponent==small_exponent ||
(large_exponent==small_exponent+1u && (large&0x7fffffu)<=(small&0x7fffffu));
// Encoded exponent 24 has spacing 2^-126. Any distinct pair admitted here
// therefore has a normal difference, including the closest adjacent pair.
if same_sign && small_exponent>=24u && within_two {
return bitcast<u32>(bitcast<f32>(left)-bitcast<f32>(right));
if a<0x7f800000u && b<0x7f800000u {
if a==0u {
if b==0u { return left&(right^0x80000000u)&0x80000000u; }
return right^0x80000000u;
}
if b==0u { return left; }
if left==right { return 0u; }
let small=min(a,b);
let large=max(a,b);
let small_exponent=small>>23u;
let large_exponent=large>>23u;
let same_sign=((left^right)&0x80000000u)==0u;
let within_two=large_exponent==small_exponent ||
(large_exponent==small_exponent+1u && (large&0x7fffffu)<=(small&0x7fffffu));
// Encoded exponent 24 has spacing 2^-126. Any distinct pair admitted here
// therefore has a normal difference, including the closest adjacent pair.
if same_sign && small_exponent>=24u && within_two {
return bitcast<u32>(bitcast<f32>(left)-bitcast<f32>(right));
}
}
// One fallback call site, including nonfinite inputs, limits inlining.
return fp_sub_bits(left,right);
}
fn anchored_delta_bits(a: u32, a_low: u32, b: u32, b_low: u32) -> u32 {
Expand Down Expand Up @@ -573,17 +571,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]);
}
Expand All @@ -594,7 +597,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]);
Expand Down Expand Up @@ -659,16 +662,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);
Expand Down Expand Up @@ -722,43 +715,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),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;
}
Expand Down Expand Up @@ -815,7 +832,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.
Expand Down Expand Up @@ -946,16 +962,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);
}
}
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 {
Expand Down Expand Up @@ -1104,14 +1111,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); }
Expand All @@ -1126,10 +1141,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); }
}
Expand Down
Loading