@@ -16,7 +16,9 @@ mod light_hierarchy {
1616 dir: Vec3,
1717 flux: f32,
1818 id: i32,
19- leaf: bool
19+
20+ has_dir: bool,
21+ is_leaf: bool
2022 }
2123
2224 fn @load_entry(id: i32, data: all::DeviceBuffer) -> LightHierarchyEntry {
@@ -27,19 +29,25 @@ mod light_hierarchy {
2729 LightHierarchyEntry {
2830 pos = all::make_vec3(e1.x, e1.y, e1.z),
2931 dir = all::make_vec3(e2.x, e2.y, e2.z),
30- flux = e1.w,
32+ flux = math_builtins::fabs( e1.w) ,
3133 id = all::select(index < 0, -index - 1, index),
32- leaf = index >= 0
34+ has_dir = !math_builtins::signbit(e1.w),
35+ is_leaf = index >= 0
3336 }
3437 }
3538
3639 fn @get_entry_cost(entry: LightHierarchyEntry, pos: Vec3) -> f32 {
3740 // TODO: Normal direction?
3841 let cdir = all::vec3_sub(entry.pos, pos);
3942 let dist2 = all::vec3_len2(cdir);
40- let cos_d = math_builtins::fabs(all::vec3_dot(entry.dir, cdir)); // Has dist applied
4143
42- entry.flux * cos_d / math_builtins::fmax(0.0001:f32, dist2)
44+ let cos_d = if entry.has_dir {
45+ math_builtins::fabs(all::vec3_dot(entry.dir, all::vec3_normalize(cdir)))
46+ } else {
47+ 1:f32
48+ };
49+
50+ entry.flux * cos_d / math_builtins::fmax[f32](0.0001, dist2)
4351 }
4452
4553 fn @get_left_prop(left: LightHierarchyEntry, right: LightHierarchyEntry, pos: Vec3) -> f32 {
@@ -56,7 +64,7 @@ mod light_hierarchy {
5664 fn @sample_light_id(rnd: &mut RndState, pos: Vec3, data: DeviceBuffer) -> (i32, f32) {
5765 let mut pdf = 1:f32;
5866 let mut entry = load_entry(0, data);
59- while !entry.leaf {
67+ while !entry.is_leaf {
6068 let (is_left, prop) = random_select_left(rnd, load_entry(entry.id, data), load_entry(entry.id+1, data), pos);
6169
6270 entry = load_entry(all::select(is_left, entry.id, entry.id+1), data);
@@ -70,7 +78,7 @@ mod light_hierarchy {
7078 let mut code = all::bitcast[u32](codes.load_i32(light.id));
7179 let mut pdf = 1:f32;
7280 let mut entry = load_entry(0, data);
73- while !entry.leaf {
81+ while !entry.is_leaf {
7482 let left = load_entry(entry.id, data);
7583 let right = load_entry(entry.id+1, data);
7684 let prop = get_left_prop(left, right, pos);
0 commit comments