@@ -47,13 +47,13 @@ mod light_hierarchy {
4747 1:f32
4848 };
4949
50- entry.flux * cos_d / math_builtins::fmax[f32](0.0001 , dist2)
50+ all::safe_div( entry.flux * cos_d, dist2)
5151 }
5252
5353 fn @get_left_prop(left: LightHierarchyEntry, right: LightHierarchyEntry, pos: Vec3) -> f32 {
5454 let cl = get_entry_cost(left, pos);
5555 let cr = get_entry_cost(right, pos);
56- cl / (cl + cr)
56+ 1 / (1 + cr/cl )
5757 }
5858
5959 fn @random_select_left(rnd: &mut RndState, left: LightHierarchyEntry, right: LightHierarchyEntry, pos: Vec3) -> (bool, f32) {
@@ -65,9 +65,11 @@ mod light_hierarchy {
6565 let mut pdf = 1:f32;
6666 let mut entry = load_entry(0, data);
6767 while !entry.is_leaf {
68- let (is_left, prop) = random_select_left(rnd, load_entry(entry.id, data), load_entry(entry.id+1, data), pos);
68+ let left = load_entry(entry.id, data);
69+ let right = load_entry(entry.id+1, data);
70+ let (is_left, prop) = random_select_left(rnd, left, right, pos);
6971
70- entry = load_entry( all::select(is_left, entry.id, entry.id+1), data );
72+ entry = all::select(is_left, left, right );
7173 pdf *= all::select(is_left, prop, 1-prop);
7274 }
7375
@@ -82,19 +84,14 @@ mod light_hierarchy {
8284 let left = load_entry(entry.id, data);
8385 let right = load_entry(entry.id+1, data);
8486 let prop = get_left_prop(left, right, pos);
87+ let is_left = (code & 0x1) == 0;
8588
86- if (code & 0x1) == 0 {
87- entry = left;
88- pdf *= prop;
89- } else {
90- entry = right;
91- pdf *= 1 - prop;
92- }
93-
89+ entry = all::select(is_left, left, right);
90+ pdf *= all::select(is_left, prop, 1-prop);
9491 code >>= 1;
9592 }
9693
97- all::select(entry.id == light.id, pdf, 0:f32)
94+ pdf
9895 }
9996}
10097
0 commit comments