Blender V5.3
eevee_surfel_list.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
7#include "draw_view.bsl.hh"
8#include "eevee_surfel.bsl.hh"
9#include "gpu_shader_index_range_lib.glsl"
11
12namespace eevee::surfel {
13
19int list_index_get(const ViewMatrices view, int2 ray_grid_size, float3 P, float &r_ray_distance)
20{
21 float3 ssP = view.point_world_to_screen(P);
22 r_ray_distance = -ssP.z;
23 int2 ray_coord_on_grid = int2(ssP.xy * float2(ray_grid_size));
24 ray_coord_on_grid = clamp(ray_coord_on_grid, int2(0), ray_grid_size - 1);
25
26 int list_index = ray_coord_on_grid.y * ray_grid_size.x + ray_coord_on_grid.x;
27 return list_index;
28}
29
34int3 cluster_index_get(int3 cluster_grid_size, float4x4 irradiance_grid_world_to_local, float3 P)
35{
36 float3 lP = transform_point(irradiance_grid_world_to_local, P) * 0.5f + 0.5f;
37 int3 cluster_index = int3(lP * float3(cluster_grid_size));
38 cluster_index = clamp(cluster_index, int3(0), cluster_grid_size - 1);
39 return cluster_index;
40}
41
42namespace list::prepare {
43
44struct Resources {
45 [[storage(0, read_write)]] int (&list_counter_buf)[];
46 [[storage(6, read_write)]] SurfelListInfoData &list_info_buf;
47};
48
63 [[resource_table]] const draw::View &views,
64 [[resource_table]] SurfelData &surfels,
65 [[global_invocation_id]] const uint3 global_id)
66{
67 const int surfel_id = int(global_id.x);
68 if (surfel_id >= int(surfels.capture_info_buf.surfel_len)) {
69 return;
70 }
71 const ViewMatrices view = views.get(0);
72
73 float ray_distance;
74 int list_id = list_index_get(
75 view, srt.list_info_buf.ray_grid_size, surfels.surfel_buf[surfel_id].position, ray_distance);
76
77 atomicAdd(srt.list_counter_buf[list_id], 1);
78 /* Do separate assignment to avoid reference to buffer in arguments which is tricky to cross
79 * compile. */
80 surfels.surfel_buf[surfel_id].ray_distance = ray_distance;
81 surfels.surfel_buf[surfel_id].list_id = list_id;
82
83 /* Clear for next step. */
84 if (global_id.x == 0u) {
85 srt.list_info_buf.list_prefix_sum = 0;
86 }
87}
88
89} // namespace list::prepare
90
92
93struct Resources {
94 [[storage(0, read)]] const int (&list_counter_buf)[];
95 [[storage(2, write)]] int (&list_range_buf)[];
96 [[storage(6, read_write)]] SurfelListInfoData &list_info_buf;
97};
98
107 [[global_invocation_id]] const uint3 global_id)
108{
109 const int list_id = int(global_id.x);
110 if (list_id >= srt.list_info_buf.list_max) {
111 return;
112 }
113
114 int list_item_count = srt.list_counter_buf[list_id];
115 int list_item_start = atomicAdd(srt.list_info_buf.list_prefix_sum, list_item_count);
116
117 srt.list_range_buf[list_id * 2 + 0] = list_item_start;
118 srt.list_range_buf[list_id * 2 + 1] = list_item_count;
119}
120
121} // namespace list::prefix_sum
122
123namespace list::flatten {
124
125struct Resources {
126 [[storage(0, read_write)]] int (&list_counter_buf)[];
127 [[storage(1, read)]] const int (&list_range_buf)[];
129 [[storage(3, write)]] int (&list_item_surfel_id_buf)[];
130 [[storage(6, read)]] const SurfelListInfoData &list_info_buf;
131};
132
141 [[resource_table]] SurfelData &surfels,
142 [[global_invocation_id]] const uint3 global_id)
143{
144 const int surfel_id = int(global_id.x);
145 if (surfel_id >= int(surfels.capture_info_buf.surfel_len)) {
146 return;
147 }
148
149 int list_id = surfels.surfel_buf[surfel_id].list_id;
150 int item_id = atomicAdd(srt.list_counter_buf[list_id], -1) - 1;
151 item_id += srt.list_range_buf[list_id * 2 + 0];
152
153 srt.list_item_distance_buf[item_id] = surfels.surfel_buf[surfel_id].ray_distance;
154 srt.list_item_surfel_id_buf[item_id] = surfel_id;
155}
156
157} // namespace list::flatten
158
159namespace list::sort {
160
161struct Resources {
162 [[storage(0, read)]] const int (&list_range_buf)[];
163 [[storage(1, read)]] const int (&list_item_surfel_id_buf)[];
164 [[storage(2, read)]] const float (&list_item_distance_buf)[];
165 [[storage(3, write)]] int (&sorted_surfel_id_buf)[];
166 [[storage(6, read)]] const SurfelListInfoData &list_info_buf;
167};
168
181 [[resource_table]] SurfelData &surfels,
182 [[global_invocation_id]] const uint3 global_id)
183{
184 const int item_id = int(global_id.x);
185 if (item_id >= int(surfels.capture_info_buf.surfel_len)) {
186 return;
187 }
188
189 int surfel_id = srt.list_item_surfel_id_buf[item_id];
190 int list_id = surfels.surfel_buf[surfel_id].list_id;
191 float ray_distance = srt.list_item_distance_buf[item_id];
192
193 IndexRange list_range{srt.list_range_buf[list_id * 2 + 0], srt.list_range_buf[list_id * 2 + 1]};
194 int prefix = 0;
195 /* Prefix sum inside the list range. */
196 for (int i = list_range.start(); i <= list_range.last(); i++) {
197 if (srt.list_item_distance_buf[i] > ray_distance) {
198 prefix++;
199 }
200 else if (srt.list_item_distance_buf[i] == ray_distance) {
201 /* Resolve the case where 2 items have the same value. */
202 if (i > item_id) {
203 prefix++;
204 }
205 }
206 }
207
208 int sorted_id = list_range.start() + prefix;
209 srt.sorted_surfel_id_buf[sorted_id] = surfel_id;
210 surfels.surfel_buf[surfel_id].index_in_sorted_list = sorted_id;
211}
212
213} // namespace list::sort
214
215namespace list::build {
216
217struct Resources {
218 [[storage(0, write)]] int (&list_start_buf)[];
219 [[storage(1, read)]] const int (&list_range_buf)[];
220 [[storage(3, read)]] const int (&sorted_surfel_id_buf)[];
221 [[storage(6, read_write)]] SurfelListInfoData &list_info_buf;
222};
223
233 [[resource_table]] SurfelData &surfels,
234 [[global_invocation_id]] const uint3 global_id)
235{
236 const int list_id = int(global_id.x);
237 if (list_id >= srt.list_info_buf.list_max) {
238 return;
239 }
240
241 const IndexRange list_range{srt.list_range_buf[list_id * 2 + 0],
242 srt.list_range_buf[list_id * 2 + 1]};
243 if (list_range.size() == 0) {
244 srt.list_start_buf[list_id] = -1;
245 return;
246 }
247
248 const int first_item = list_range.start();
249 const int last_item = list_range.last();
250
251 const int sorted_list_first = srt.sorted_surfel_id_buf[first_item];
252 {
253 /* Update surfels linked list. */
254 int prev = -1;
255 int curr = srt.sorted_surfel_id_buf[first_item];
256 for (int i = first_item; i <= last_item; i++) {
257 int next = (i == last_item) ? -1 : srt.sorted_surfel_id_buf[i + 1];
258 surfels.surfel_buf[curr].next = next;
259 surfels.surfel_buf[curr].prev = prev;
260 prev = curr;
261 curr = next;
262 }
263 }
264 /* Update list start for irradiance sample capture. */
265 srt.list_start_buf[list_id] = sorted_list_first;
266
267 /* Now that we have a sorted list, try to avoid connection from coplanar surfels.
268 * For that we disconnect them and link them to the first non-coplanar surfel.
269 * Note that this changes the list to a tree, which doesn't affect the rest of the algorithm.
270 *
271 * This is a really important step since it allows to clump more surfels into one ray list and
272 * avoid light leaking through surfaces. If we don't disconnect coplanar surfels, we loose many
273 * good rays by evaluating null radiance transfer between the coplanar surfels for rays that
274 * are not directly perpendicular to the surface. */
275
276 /* Limiting the total number of search steps avoids TDRs, but may cause overshadowing if the
277 * limit is reached. */
278 const int max_search = 2000;
279 uint search_count = 0;
280
281 /* Mutable `foreach`. */
282 for (int i = sorted_list_first, next = -1; i > -1; i = next) {
283 next = surfels.surfel_buf[i].next;
284
285 int valid_next = surfels.surfel_buf[i].next;
286 int valid_prev = surfels.surfel_buf[i].prev;
287
288 /* Search the list for the first valid next and previous surfel. */
289 while (search_count < max_search) {
290 if (valid_next == -1) {
291 break;
292 }
293 if (surfels.is_valid_surfel_link(i, valid_next)) {
294 break;
295 }
296 valid_next = surfels.surfel_buf[valid_next].next;
297 search_count++;
298 }
299 while (search_count < max_search) {
300 if (valid_prev == -1) {
301 break;
302 }
303 if (surfels.is_valid_surfel_link(i, valid_prev)) {
304 break;
305 }
306 valid_prev = surfels.surfel_buf[valid_prev].prev;
307 search_count++;
308 }
309
310 surfels.surfel_buf[i].next = valid_next;
311 surfels.surfel_buf[i].prev = valid_prev;
312 }
313
314#if 0 /* For debugging the sorted list. */
315 for (int i = sorted_list_first, next = -1; i > -1; i = next) {
316 next = surfel_buf[i].next;
317 if (next != -1) {
318 drw_debug_line(surfel_buf[next].position,
319 surfel_buf[i].position,
320 float4(1, 0, 0, 1),
321 drw_debug_persistent_lifetime);
322 }
323 }
324#endif
325}
326
327} // namespace list::build
328
329} // namespace eevee::surfel
330
void drw_debug_line(uint &vertid, float3 v1, float3 v2, uint v_color, uint lifetime)
#define SURFEL_GROUP_SIZE
PipelineCompute eevee_surfel_list_prepare(eevee::surfel::list::prepare::prepare_comp)
PipelineCompute eevee_surfel_list_sort(eevee::surfel::list::sort::sort_comp)
PipelineCompute eevee_surfel_list_build(eevee::surfel::list::build::build_comp)
PipelineCompute eevee_surfel_list_prefix(eevee::surfel::list::prefix_sum::prefix_sum_comp)
PipelineCompute eevee_surfel_list_flatten(eevee::surfel::list::flatten::flatten_comp)
#define texture_atomic
#define storage(slot, qualifiers)
#define position
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
int atomicAdd(int &, int) RET
constexpr T clamp(T, U, U) RET
float3 transform_point(float3x3 mat, float3 point)
MatBase< float, 4, 4 > float4x4
void build_comp(Resources &srt, SurfelData &surfels, const uint3 global_id)
void flatten_comp(Resources &srt, SurfelData &surfels, const uint3 global_id)
void prefix_sum_comp(Resources &srt, const uint3 global_id)
void prepare_comp(Resources &srt, const draw::View &views, SurfelData &surfels, const uint3 global_id)
void sort_comp(Resources &srt, SurfelData &surfels, const uint3 global_id)
int3 cluster_index_get(int3 cluster_grid_size, float4x4 irradiance_grid_world_to_local, float3 P)
int list_index_get(const ViewMatrices view, int2 ray_grid_size, float3 P, float &r_ray_distance)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float3 position() const
float3 point_world_to_screen(float3 vP) const
float z
Definition sky_math.h:136