Blender V5.3
eevee_occupancy_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9namespace occupancy {
10
11struct Bits {
12 uint bits[8];
13};
14
15int bit_index_from_depth(float depth, int bit_count)
16{
17 /* We want the occupancy at the center of each range a bit covers.
18 * So we round the depth to the nearest bit. */
19 return int(depth * float(bit_count) + 0.5f);
20}
21
32Bits bits_from_depth(float depth, int bit_count)
33{
34 /* We want the occupancy at the center of each range a bit covers.
35 * So we round the depth to the nearest bit. */
36 int depth_bit_index = bit_index_from_depth(depth, bit_count);
37
39 for (int i = 0; i < 8; i++) {
40 int shift = clamp(depth_bit_index - i * 32, 0, 32);
41 /* Cannot bit shift more than 31 positions. */
42 occupancy.bits[i] = (shift == 32) ? 0x0u : (~0x0u << uint(shift));
43 }
44 return occupancy;
45}
46
51{
53 for (int i = 0; i < 8; i++) {
54 occupancy.bits[i] = 0x0u;
55 }
56 return occupancy;
57}
58
69Bits bit_from_depth(float depth, int bit_count)
70{
71 /* We want the occupancy at the center of each range a bit covers.
72 * So we round the depth to the nearest bit. */
73 int depth_bit_index = bit_index_from_depth(depth, bit_count);
74
76 for (int i = 0; i < 8; i++) {
77 int shift = depth_bit_index - i * 32;
78 /* Cannot bit shift more than 31 positions. */
79 occupancy.bits[i] = (shift >= 0 && shift < 32) ? (0x1u << uint(shift)) : 0x0u;
80 }
81 return occupancy;
82}
83
88{
90 for (int i = 0; i < 8; i++) {
91 occupancy.bits[i] = a.bits[i] | b.bits[i];
92 }
93 return occupancy;
94}
95
99Bits set_bits_high(Bits occupancy, int bit_start, int bit_count)
100{
101 for (int i = 0; i < bit_count; i++) {
102 int bit = bit_start + i;
103 occupancy.bits[bit / 32] |= 1u << uint(bit % 32);
104 }
105 return occupancy;
106}
107
112{
113 for (int i = 0; i < 8; i++) {
114 if (occupancy.bits[i] != 0) {
115 return findLSB(occupancy.bits[i]) + i * 32;
116 }
117 }
118 return -1;
119}
120
125{
126 return uint4(occupancy.bits[0], occupancy.bits[1], occupancy.bits[2], occupancy.bits[3]);
127}
128
132bool bit_resolve(Bits entry, Bits exit, uint bit_n, int /*bit_count*/)
133{
134 int first_exit = find_lsb(exit);
135 int first_entry = find_lsb(entry);
136 first_exit = (first_exit == -1) ? 99999 : first_exit;
137 /* Check if the first surface is an exit. If it is, initialize as inside the volume. */
138 bool inside_volume = first_exit < first_entry;
139 for (uint j = 0; j <= bit_n / 32; j++) {
140 uint entry_word = entry.bits[j];
141 uint exit_word = exit.bits[j];
142 /* TODO(fclem): Could use fewer iteration using findLSB and/or other intrinsics. */
143 for (uint i = 0; i < 32; i++) {
144 if (flag_test(exit_word, 1u << i) && flag_test(entry_word, 1u << i)) {
145 /* Do nothing. */
146 }
147 else {
148 if (flag_test(exit_word, 1u << i)) {
149 inside_volume = false;
150 }
151 if (flag_test(entry_word, 1u << i)) {
152 inside_volume = true;
153 }
154 }
155 if (i + j * 32 == bit_n) {
156 return inside_volume;
157 }
158 }
159 }
160 return inside_volume;
161}
162
166Bits resolve(Bits entry, Bits exit, int bit_count)
167{
169 for (int j = 0; j < 8; j++) {
170 for (int i = 0; i < 32; i++) {
171 bool test = false;
172 if (i < bit_count - j * 32) {
173 test = bit_resolve(entry, exit, uint(i + j * 32), bit_count);
174 }
175 set_flag_from_test(occupancy.bits[j], test, 1u << uint(i));
176 }
177 }
178 return occupancy;
179}
180
181} // namespace occupancy
constexpr T clamp(T, U, U) RET
VecBase< int, D > findLSB(VecOp< int, D >) RET
void set_flag_from_test(uint &value, bool test, uint flag)
bool flag_test(uint flag, uint val)
Bits bit_from_depth(float depth, int bit_count)
int find_lsb(Bits occupancy)
Bits set_bits_high(Bits occupancy, int bit_start, int bit_count)
uint4 to_uint4(Bits occupancy)
Bits bitwise_or(Bits a, Bits b)
Bits bits_from_depth(float depth, int bit_count)
Bits resolve(Bits entry, Bits exit, int bit_count)
bool bit_resolve(Bits entry, Bits exit, uint bit_n, int)
int bit_index_from_depth(float depth, int bit_count)