Blender V5.3
eevee_light_iter.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
12
13namespace eevee::light {
14
15uint bitfield_mask(uint bit_width, uint bit_min)
16{
17 /* Cannot bit shift more than 31 positions. */
18 uint mask = (bit_width > 31u) ? 0x0u : (0xFFFFFFFFu << bit_width);
19 return ~mask << bit_min;
20}
21
22uint zbin_mask(uint word_index, uint zbin_min, uint zbin_max)
23{
24 uint word_start = word_index * 32u;
25 uint word_end = word_start + 31u;
26 uint local_min = max(zbin_min, word_start);
27 uint local_max = min(zbin_max, word_end);
28 uint mask_width = local_max - local_min + 1;
29 return bitfield_mask(mask_width, local_min);
30}
31
32int culling_z_to_zbin(float scale, float bias, float z)
33{
34 return int(z * scale + bias);
35}
36
37/* To be instantiated with a callback class that implement eval_directional and eval_local.
38 * If ResourceT is not needed, just pass LightRenderData again. */
39template<typename CallbackT, typename ResourceT>
40void foreach(const LightRenderData &srt, CallbackT &cb, ResourceT &res)
41{
42 const LightCullingData &culling = srt.light_cull_buf;
43
44 for (uint index = culling.local_lights_len; index < culling.items_count; index++) {
45 LightData light = srt.light_buf[index];
46 cb.eval_directional(res, index, light);
47 }
48
49 for (uint index = 0; index < culling.visible_count; index++) {
50 LightData light = srt.light_buf[index];
51 cb.eval_local(res, index, light);
52 }
53}
54
61
63 private:
64 int index;
65
66 uint tile_word_offset;
67 uint local_min_index;
68 uint local_max_index;
69 uint local_word_idx;
70 uint local_word;
71
73
74 uint light_set;
75
76 void next_local_word(const eevee::LightRenderData &lrd)
77 {
78 const auto &words = lrd.light_tile_buf;
79
80 /* Same as divide by 32 but avoid integer division. */
81 uint word_max = local_max_index >> 5u;
82 if (local_word_idx > word_max) {
83 return;
84 }
85
86 local_word = words[tile_word_offset + local_word_idx];
87 local_word &= zbin_mask(local_word_idx, local_min_index, local_max_index);
88/* Ensure all threads inside a subgroup get the same value to reduce VGPR usage. */
89#ifdef GPU_METAL
90 /* Waiting to implement extensions support. We need:
91 * - GL_KHR_shader_subgroup_ballot
92 * - GL_KHR_shader_subgroup_arithmetic
93 * or
94 * - Vulkan 1.1
95 */
96 local_word = simd_broadcast_first(simd_or(local_word));
97#endif
98 }
99
100 public:
102 float2 pixel,
103 float linear_view_z,
104 uint receiver_light_set)
105 {
106 const LightCullingData &culling = lrd.light_cull_buf;
107 const auto &zbins = lrd.light_zbin_buf;
108
109 uint2 tile_co = uint2(pixel / culling.tile_size);
110 tile_word_offset = (tile_co.x + tile_co.y * culling.tile_x_len) * culling.tile_word_len;
111 int zbin_index = culling_z_to_zbin(culling.zbin_scale, culling.zbin_bias, linear_view_z);
112 zbin_index = clamp(zbin_index, 0, CULLING_ZBIN_COUNT - 1);
113 uint zbin_data = zbins[zbin_index];
114 local_min_index = zbin_data & 0xFFFFu;
115 local_max_index = zbin_data >> 16u;
116 /* Ensure all threads inside a subgroup get the same value to reduce VGPR usage. */
117#ifdef GPU_METAL
118 /* Waiting to implement extensions support. We need:
119 * - GL_KHR_shader_subgroup_ballot
120 * - GL_KHR_shader_subgroup_arithmetic
121 * or
122 * - Vulkan 1.1
123 */
124 local_min_index = simd_broadcast_first(simd_min(local_min_index));
125 local_max_index = simd_broadcast_first(simd_max(local_max_index));
126#endif
127 light_set = receiver_light_set;
128
129 phase = LIGHT_ITER_PHASE_INIT;
130 }
131
133 {
134 const LightCullingData &culling = lrd.light_cull_buf;
135
136 switch (phase) {
138 index = int(culling.local_lights_len) - 1;
140 [[fallthrough]];
141 }
143 index++;
144 if (index < int(culling.items_count)) {
145 return true;
146 }
148 /* Same as divide by 32 but avoid integer division. */
149 uint word_min = local_min_index >> 5u;
150 local_word_idx = word_min;
151 next_local_word(lrd);
152 [[fallthrough]];
153 }
155 /* Same as divide by 32 but avoid integer division. */
156 uint word_max = local_max_index >> 5u;
157 while (local_word_idx <= word_max) {
158 int bit_index = findLSB(local_word);
159 if (bit_index != -1) {
160 local_word &= ~1u << uint(bit_index);
161 index = int(local_word_idx * 32u + uint(bit_index));
162 return true;
163 }
164 local_word_idx++;
165 next_local_word(lrd);
166 }
167 phase = LIGHT_ITER_PHASE_END;
168 [[fallthrough]];
169 }
171 return false;
172 }
173 assert(0);
174 return false;
175 }
176
178 {
179 return phase == 1;
180 }
181
183 {
184 LightData light = lrd.light_buf[index];
185
186 if (!light_linking_affects_receiver(light.light_set_membership, light_set)) {
187 return true;
188 }
189
190 if (is_zero(light.color * light.point_power)) {
191 /* This is an optimization, but also ensures we skip the extracted World Sun if it's radiance
192 * falls bellow the extraction threshold. */
193 return true;
194 }
195 if (is_directional()) {
196 return false;
197 }
198 LightVector lv = LightVector::get(light, false, P);
200 return true;
201 }
202 return false;
203 }
204};
205
206/* To be instantiated with a callback class that implement eval_directional and eval_local.
207 * If ResourceT is not needed, just pass LightRenderData again. */
208template<typename CallbackT, typename ResourceT>
210 const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
211{
212 const LightCullingData &culling = srt.light_cull_buf;
213 const auto &zbins = srt.light_zbin_buf;
214 const auto &words = srt.light_tile_buf;
215
216 for (uint index = culling.local_lights_len; index < culling.items_count; index++) {
217 LightData light = srt.light_buf[index];
218 cb.eval_directional(res, index, light);
219 }
220
221/* WORKAROUND: For Surfels to use the same lighting path. Could be improved. */
222#ifdef SRT_CONSTANT_light_iter_force_no_culling
223 /* Same as light::foreach. */
224 for (uint index = 0; index < culling.visible_count; index++) {
225 LightData light = srt.light_buf[index];
226 cb.eval_local(res, index, light);
227 }
228 return;
229#endif
230
231 {
232 uint2 tile_co = uint2(pixel / culling.tile_size);
233 uint tile_word_offset = (tile_co.x + tile_co.y * culling.tile_x_len) * culling.tile_word_len;
234 int zbin_index = culling_z_to_zbin(culling.zbin_scale, culling.zbin_bias, linear_view_z);
235 zbin_index = clamp(zbin_index, 0, CULLING_ZBIN_COUNT - 1);
236 uint zbin_data = zbins[zbin_index];
237 uint min_index = zbin_data & 0xFFFFu;
238 uint max_index = zbin_data >> 16u;
239 /* Ensure all threads inside a subgroup get the same value to reduce VGPR usage. */
240#ifdef GPU_METAL
241 /* Waiting to implement extensions support. We need:
242 * - GL_KHR_shader_subgroup_ballot
243 * - GL_KHR_shader_subgroup_arithmetic
244 * or
245 * - Vulkan 1.1
246 */
247 min_index = simd_broadcast_first(simd_min(min_index));
248 max_index = simd_broadcast_first(simd_max(max_index));
249#endif
250 /* Same as divide by 32 but avoid integer division. */
251 uint word_min = min_index >> 5u;
252 uint word_max = max_index >> 5u;
253 for (uint word_idx = word_min; word_idx <= word_max; word_idx++) {
254 uint word = words[tile_word_offset + word_idx];
255 word &= zbin_mask(word_idx, min_index, max_index);
256 /* Ensure all threads inside a subgroup get the same value to reduce VGPR usage. */
257#ifdef GPU_METAL
258 /* Waiting to implement extensions support. We need:
259 * - GL_KHR_shader_subgroup_ballot
260 * - GL_KHR_shader_subgroup_arithmetic
261 * or
262 * - Vulkan 1.1
263 */
264 word = simd_broadcast_first(simd_or(word));
265#endif
266 int bit_index;
267 while ((bit_index = findLSB(word)) != -1) {
268 word &= ~1u << uint(bit_index);
269 uint index = word_idx * 32u + uint(bit_index);
270 LightData light = srt.light_buf[index];
271 cb.eval_local(res, index, light);
272 }
273 }
274 }
275}
276
277} // namespace eevee::light
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define LIGHT_ATTENUATION_THRESHOLD
float light_attenuation_surface(LightData light, const bool is_directional, LightVector lv)
#define assert(assertion)
#define index(i)
constexpr T clamp(T, U, U) RET
VecBase< int, D > findLSB(VecOp< int, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
bool is_zero(float3x3 a)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
int culling_z_to_zbin(float scale, float bias, float z)
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
uint bitfield_mask(uint bit_width, uint bit_min)
bool light_linking_affects_receiver(uint2 light_set_membership, uchar receiver_light_set)
uint zbin_mask(uint word_index, uint zbin_min, uint zbin_max)
#define min(a, b)
Definition sort.cc:38
static LightVector get(LightData light, const bool is_directional, float3 P)
const LightData(& light_buf)[]
const LightCullingData & light_cull_buf
bool should_skip(const eevee::LightRenderData &lrd, float3 P)
void init(const eevee::LightRenderData &lrd, float2 pixel, float linear_view_z, uint receiver_light_set)
bool next(const eevee::LightRenderData &lrd)
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13