Blender V5.3
parallel_reduction.cc
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#include <cmath>
6#include <limits>
7
8#include "BLI_index_range.hh"
9#include "BLI_math_base.hh"
10#include "BLI_math_vector.hh"
12#include "BLI_task.hh"
13
14#include "MEM_guardedalloc.h"
15
16#include "GPU_compute.hh"
17#include "GPU_shader.hh"
18#include "GPU_state.hh"
19#include "GPU_texture.hh"
20#include "GPU_texture_pool.hh"
21
22#include "COM_context.hh"
23#include "COM_result.hh"
24
26
27namespace blender::compositor {
28
29/* Reduces the given texture into a single value and returns it. The return value should be freed
30 * by a call to MEM_delete. The return value is either a pointer to a float, or a pointer to an
31 * array of floats that represents a vector. This depends on the given format, which should be
32 * compatible with the reduction shader.
33 *
34 * The given reduction shader should be bound when calling the function and the shader is expected
35 * to be derived from the compositor_parallel_reduction.glsl shader, see that file for more
36 * information. Also see the compositor_parallel_reduction_info.hh file for example shader
37 * definitions. */
40 gpu::TextureFormat format)
41{
42 GPU_shader_uniform_1b(shader, "is_initial_reduction", true);
43
44 gpu::Texture *texture_to_reduce = texture;
46
47 /* Dispatch the reduction shader until the texture reduces to a single pixel. */
48 while (size_to_reduce != int2(1)) {
49 const int2 reduced_size = math::divide_ceil(size_to_reduce, int2(16));
51 reduced_size, 1, format, GPU_TEXTURE_USAGE_GENERAL);
52
54 const int texture_image_unit = GPU_shader_get_sampler_binding(shader, "input_tx");
55 GPU_texture_bind(texture_to_reduce, texture_image_unit);
56
57 const int image_unit = GPU_shader_get_sampler_binding(shader, "output_img");
58 GPU_texture_image_bind(reduced_texture, image_unit);
59
60 GPU_compute_dispatch(shader, reduced_size.x, reduced_size.y, 1);
61
62 GPU_texture_image_unbind(reduced_texture);
63 GPU_texture_unbind(texture_to_reduce);
64
65 /* Release the input texture only if it is not the source texture, since the source texture is
66 * not acquired or owned by the function. */
67 if (texture_to_reduce != texture) {
68 gpu::TexturePool::get().release_texture(texture_to_reduce);
69 }
70
71 texture_to_reduce = reduced_texture;
72 size_to_reduce = reduced_size;
73
74 GPU_shader_uniform_1b(shader, "is_initial_reduction", false);
75 }
76
78 float *pixel = static_cast<float *>(GPU_texture_read(texture_to_reduce, GPU_DATA_FLOAT, 0));
79
80 /* Release the final texture only if it is not the source texture, since the source texture is
81 * not acquired or owned by the function. */
82 if (texture_to_reduce != texture) {
83 gpu::TexturePool::get().release_texture(texture_to_reduce);
84 }
85
86 return pixel;
87}
88
89/* Reduces the given function in parallel over the given 2D range, the reduction function should
90 * have the given identity value. The given function gets as arguments the texel coordinates of the
91 * element of the range as well as a reference to the value where the result should be accumulated,
92 * while the reduction function gets a reference to two values and returns their reduction. */
93template<typename Value, typename Function, typename Reduction>
94static Value parallel_reduce(const int2 range,
95 const Value &identity,
96 const Function &function,
97 const Reduction &reduction)
98{
100 IndexRange(range.y),
101 64,
102 identity,
103 [&](const IndexRange sub_y_range, const Value &initial_value) {
104 Value result = initial_value;
105 for (const int64_t y : sub_y_range) {
106 for (const int64_t x : IndexRange(range.x)) {
107 function(int2(x, y), result);
108 }
109 }
110 return result;
111 },
112 reduction);
113}
114
115/* --------------------------------------------------------------------
116 * Sum Reductions.
117 */
118
119static float4 sum_color_gpu(Context &context, const Result &input)
120{
121 gpu::Shader *shader = context.get_shader("compositor_sum_color", ResultPrecision::Full);
123
124 float *reduced_value = parallel_reduction_dispatch(
126 const float4 sum = float4(reduced_value);
127 MEM_delete(reduced_value);
129
130 return sum;
131}
132
133static float4 sum_color_cpu(const Result &input)
134{
135 return float4(parallel_reduce(
136 input.domain().data_size,
137 double4(0.0),
138 [&](const int2 texel, double4 &accumulated_value) {
139 accumulated_value += double4(float4(input.load_pixel<Color>(texel)));
140 },
141 [&](const double4 &a, const double4 &b) { return a + b; }));
142}
143
144float4 sum_color(Context &context, const Result &input)
145{
146 if (context.use_gpu()) {
147 return sum_color_gpu(context, input);
148 }
149
150 return sum_color_cpu(input);
151}
152
153static float sum_log_luminance_gpu(Context &context,
154 const Result &input,
155 const float3 &luminance_coefficients)
156{
157 gpu::Shader *shader = context.get_shader("compositor_sum_log_luminance", ResultPrecision::Full);
159
160 GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
161
162 float *reduced_value = parallel_reduction_dispatch(
164 const float sum = *reduced_value;
165 MEM_delete(reduced_value);
167
168 return sum;
169}
170
171static float sum_log_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
172{
173 return float(parallel_reduce(
174 input.domain().data_size,
175 0.0,
176 [&](const int2 texel, double &accumulated_value) {
177 const float luminance = math::dot(float4(input.load_pixel<Color>(texel)).xyz(),
178 luminance_coefficients);
179 accumulated_value += std::log(math::max(luminance, 1e-5f));
180 },
181 [&](const double &a, const double &b) { return a + b; }));
182}
183
185 const Result &input,
186 const float3 &luminance_coefficients)
187{
188 if (context.use_gpu()) {
189 return sum_log_luminance_gpu(context, input, luminance_coefficients);
190 }
191
192 return sum_log_luminance_cpu(input, luminance_coefficients);
193}
194
195/* --------------------------------------------------------------------
196 * Sum Of Squared Difference Reductions.
197 */
198
200 const Result &input,
201 const float4 subtrahend)
202{
203 gpu::Shader *shader = context.get_shader("compositor_sum_squared_difference_color",
206
207 GPU_shader_uniform_4fv(shader, "subtrahend", subtrahend);
208
209 float *reduced_value = parallel_reduction_dispatch(
211 const float4 sum = float4(reduced_value);
212 MEM_delete(reduced_value);
214
215 return sum;
216}
217
218static float4 sum_squared_difference_color_cpu(const Result &input, const float4 subtrahend)
219{
220 return float4(parallel_reduce(
221 input.domain().data_size,
222 double4(0.0),
223 [&](const int2 texel, double4 &accumulated_value) {
224 accumulated_value += double4(
225 math::square(float4(input.load_pixel<Color>(texel)) - subtrahend));
226 },
227 [&](const double4 &a, const double4 &b) { return a + b; }));
228}
229
230float4 sum_squared_difference_color(Context &context, const Result &input, const float4 subtrahend)
231{
233 if (context.use_gpu()) {
234 return sum_squared_difference_color_gpu(context, input, subtrahend);
235 }
236
237 return sum_squared_difference_color_cpu(input, subtrahend);
238}
239
240/* --------------------------------------------------------------------
241 * Minimum Reductions.
242 */
243
244static float minimum_float_gpu(Context &context, const Result &input)
245{
246 gpu::Shader *shader = context.get_shader("compositor_minimum_float", ResultPrecision::Full);
248
249 float *reduced_value = parallel_reduction_dispatch(
251 const float minimum = *reduced_value;
252 MEM_delete(reduced_value);
254
255 return minimum;
256}
257
258static float minimum_float_cpu(const Result &input)
259{
260 return float(parallel_reduce(
261 input.domain().data_size,
262 std::numeric_limits<float>::max(),
263 [&](const int2 texel, float &accumulated_value) {
264 accumulated_value = math::min(accumulated_value, input.load_pixel<float>(texel));
265 },
266 [&](const float &a, const float &b) { return math::min(a, b); }));
267}
268
269float minimum_float(Context &context, const Result &input)
270{
271 if (context.use_gpu()) {
272 return minimum_float_gpu(context, input);
273 }
274
275 return minimum_float_cpu(input);
276}
277
278static Color minimum_color_cpu(const Result &input)
279{
280 return Color(parallel_reduce(
281 input.domain().data_size,
282 float4(std::numeric_limits<float>::max()),
283 [&](const int2 texel, float4 &accumulated_value) {
284 accumulated_value = math::min(accumulated_value, float4(input.load_pixel<Color>(texel)));
285 },
286 [&](const float4 &a, const float4 &b) { return math::min(a, b); }));
287}
288
289static Color minimum_color_gpu(Context &context, const Result &input)
290{
291 gpu::Shader *shader = context.get_shader("compositor_minimum_float4", ResultPrecision::Full);
293
294 float *reduced_value = parallel_reduction_dispatch(
296 const Color minimum = reduced_value;
297 MEM_delete(reduced_value);
299
300 return minimum;
301}
302
303Color minimum_color(Context &context, const Result &input)
304{
305 if (context.use_gpu()) {
306 return minimum_color_gpu(context, input);
307 }
308
309 return minimum_color_cpu(input);
310}
311
312static float minimum_luminance_gpu(Context &context,
313 const Result &input,
314 const float3 &luminance_coefficients)
315{
316 gpu::Shader *shader = context.get_shader("compositor_minimum_luminance", ResultPrecision::Full);
318
319 GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
320
321 float *reduced_value = parallel_reduction_dispatch(
323 const float minimum = *reduced_value;
324 MEM_delete(reduced_value);
326
327 return minimum;
328}
329
330static float minimum_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
331{
332 return float(parallel_reduce(
333 input.domain().data_size,
334 std::numeric_limits<float>::max(),
335 [&](const int2 texel, float &accumulated_value) {
336 const float luminance = math::dot(float4(input.load_pixel<Color>(texel)).xyz(),
337 luminance_coefficients);
338 accumulated_value = math::min(accumulated_value, luminance);
339 },
340 [&](const float &a, const float &b) { return math::min(a, b); }));
341}
342
344 const Result &input,
345 const float3 &luminance_coefficients)
346{
347 if (context.use_gpu()) {
348 return minimum_luminance_gpu(context, input, luminance_coefficients);
349 }
350
351 return minimum_luminance_cpu(input, luminance_coefficients);
352}
353
355 const Result &input,
356 const float lower_bound,
357 const float upper_bound)
358{
359 gpu::Shader *shader = context.get_shader("compositor_minimum_float_in_range",
362
363 GPU_shader_uniform_1f(shader, "lower_bound", lower_bound);
364 GPU_shader_uniform_1f(shader, "upper_bound", upper_bound);
365
366 float *reduced_value = parallel_reduction_dispatch(
368 const float minimum = *reduced_value;
369 MEM_delete(reduced_value);
371
372 return minimum;
373}
374
375static float minimum_float_in_range_cpu(const Result &input,
376 const float lower_bound,
377 const float upper_bound)
378{
379 return parallel_reduce(
380 input.domain().data_size,
381 upper_bound,
382 [&](const int2 texel, float &accumulated_value) {
383 const float value = input.load_pixel<float>(texel);
384 if ((value <= upper_bound) && (value >= lower_bound)) {
385 accumulated_value = math::min(accumulated_value, value);
386 }
387 },
388 [&](const float &a, const float &b) { return math::min(a, b); });
389}
390
392 const Result &input,
393 const float lower_bound,
394 const float upper_bound)
395{
396 if (context.use_gpu()) {
397 return minimum_float_in_range_gpu(context, input, lower_bound, upper_bound);
398 }
399
400 return minimum_float_in_range_cpu(input, lower_bound, upper_bound);
401}
402
403/* --------------------------------------------------------------------
404 * Maximum Reductions.
405 */
406
407static float maximum_float_gpu(Context &context, const Result &input)
408{
409 gpu::Shader *shader = context.get_shader("compositor_maximum_float", ResultPrecision::Full);
411
412 float *reduced_value = parallel_reduction_dispatch(
414 const float maximum = *reduced_value;
415 MEM_delete(reduced_value);
417
418 return maximum;
419}
420
421static float maximum_float_cpu(const Result &input)
422{
423 return float(parallel_reduce(
424 input.domain().data_size,
425 std::numeric_limits<float>::lowest(),
426 [&](const int2 texel, float &accumulated_value) {
427 accumulated_value = math::max(accumulated_value, input.load_pixel<float>(texel));
428 },
429 [&](const float &a, const float &b) { return math::max(a, b); }));
430}
431
432float maximum_float(Context &context, const Result &input)
433{
434 if (context.use_gpu()) {
435 return maximum_float_gpu(context, input);
436 }
437
438 return maximum_float_cpu(input);
439}
440
441static float2 maximum_float2_gpu(Context &context, const Result &input)
442{
443 gpu::Shader *shader = context.get_shader("compositor_maximum_float2", ResultPrecision::Full);
445
446 float *reduced_value = parallel_reduction_dispatch(
448 const float2 maximum = reduced_value;
449 MEM_delete(reduced_value);
451
452 return maximum;
453}
454
455static float2 maximum_float2_cpu(const Result &input)
456{
457 return parallel_reduce(
458 input.domain().data_size,
459 float2(std::numeric_limits<float>::lowest()),
460 [&](const int2 texel, float2 &accumulated_value) {
461 accumulated_value = math::max(accumulated_value, input.load_pixel<float2>(texel));
462 },
463 [&](const float2 &a, const float2 &b) { return math::max(a, b); });
464}
465
466float2 maximum_float2(Context &context, const Result &input)
467{
468 if (context.use_gpu()) {
469 return maximum_float2_gpu(context, input);
470 }
471
472 return maximum_float2_cpu(input);
473}
474
475static Color maximum_color_gpu(Context &context, const Result &input)
476{
477 gpu::Shader *shader = context.get_shader("compositor_maximum_float4", ResultPrecision::Full);
479
480 float *reduced_value = parallel_reduction_dispatch(
482 const Color maximum = Color(reduced_value);
483 MEM_delete(reduced_value);
485
486 return maximum;
487}
488
489static Color maximum_color_cpu(const Result &input)
490{
491 return Color(parallel_reduce(
492 input.domain().data_size,
493 float4(std::numeric_limits<float>::lowest()),
494 [&](const int2 texel, float4 &accumulated_value) {
495 accumulated_value = math::max(accumulated_value, float4(input.load_pixel<Color>(texel)));
496 },
497 [&](const float4 &a, const float4 &b) { return math::max(a, b); }));
498}
499
500Color maximum_color(Context &context, const Result &input)
501{
502 if (context.use_gpu()) {
503 return maximum_color_gpu(context, input);
504 }
505
506 return maximum_color_cpu(input);
507}
508
509static float maximum_luminance_gpu(Context &context,
510 const Result &input,
511 const float3 &luminance_coefficients)
512{
513 gpu::Shader *shader = context.get_shader("compositor_maximum_luminance", ResultPrecision::Full);
515
516 GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
517
518 float *reduced_value = parallel_reduction_dispatch(
520 const float maximum = *reduced_value;
521 MEM_delete(reduced_value);
523
524 return maximum;
525}
526
527static float maximum_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
528{
529 return float(parallel_reduce(
530 input.domain().data_size,
531 std::numeric_limits<float>::lowest(),
532 [&](const int2 texel, float &accumulated_value) {
533 const float luminance = math::dot(float4(input.load_pixel<Color>(texel)).xyz(),
534 luminance_coefficients);
535 accumulated_value = math::max(accumulated_value, luminance);
536 },
537 [&](const float &a, const float &b) { return math::max(a, b); }));
538}
539
541 const Result &input,
542 const float3 &luminance_coefficients)
543{
544 if (context.use_gpu()) {
545 return maximum_luminance_gpu(context, input, luminance_coefficients);
546 }
547
548 return maximum_luminance_cpu(input, luminance_coefficients);
549}
550
552 const Result &input,
553 const float lower_bound,
554 const float upper_bound)
555{
556 gpu::Shader *shader = context.get_shader("compositor_maximum_float_in_range",
559
560 GPU_shader_uniform_1f(shader, "lower_bound", lower_bound);
561 GPU_shader_uniform_1f(shader, "upper_bound", upper_bound);
562
563 float *reduced_value = parallel_reduction_dispatch(
565 const float maximum = *reduced_value;
566 MEM_delete(reduced_value);
568
569 return maximum;
570}
571
572static float maximum_float_in_range_cpu(const Result &input,
573 const float lower_bound,
574 const float upper_bound)
575{
576 return float(parallel_reduce(
577 input.domain().data_size,
578 lower_bound,
579 [&](const int2 texel, float &accumulated_value) {
580 const float value = input.load_pixel<float>(texel);
581 if ((value <= upper_bound) && (value >= lower_bound)) {
582 accumulated_value = math::max(accumulated_value, value);
583 }
584 },
585 [&](const float &a, const float &b) { return math::max(a, b); }));
586}
587
589 const Result &input,
590 const float lower_bound,
591 const float upper_bound)
592{
593 if (context.use_gpu()) {
594 return maximum_float_in_range_gpu(context, input, lower_bound, upper_bound);
595 }
596
597 return maximum_float_in_range_cpu(input, lower_bound, upper_bound);
598}
599
600} // namespace blender::compositor
Read Blender memory allocation and freeing.
constexpr IndexRange()=default
static gpu::TextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
Definition result.cc:84
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
T load_pixel(const int2 &texel) const
virtual void release_texture(Texture *tex)=0
virtual Texture * acquire_texture_2d(int2 extent, int mip_len, TextureFormat format, eGPUTextureUsage usage, const char *name=nullptr)=0
static TexturePool & get()
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
static float2 maximum_float2_gpu(Context &context, const Result &input)
static float minimum_float_in_range_cpu(const Result &input, const float lower_bound, const float upper_bound)
static float minimum_float_gpu(Context &context, const Result &input)
float minimum_float(Context &context, const Result &input)
static Value parallel_reduce(const int2 range, const Value &identity, const Function &function, const Reduction &reduction)
static Color minimum_color_cpu(const Result &input)
static float maximum_float_in_range_cpu(const Result &input, const float lower_bound, const float upper_bound)
static float * parallel_reduction_dispatch(gpu::Texture *texture, gpu::Shader *shader, gpu::TextureFormat format)
float minimum_float_in_range(Context &context, const Result &input, const float lower_bound, const float upper_bound)
float2 maximum_float2(Context &context, const Result &input)
float4 sum_squared_difference_color(Context &context, const Result &input, const float4 subtrahend)
Color minimum_color(Context &context, const Result &input)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
static float4 sum_squared_difference_color_gpu(Context &context, const Result &input, const float4 subtrahend)
float minimum_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
static float4 sum_color_cpu(const Result &input)
static float minimum_luminance_gpu(Context &context, const Result &input, const float3 &luminance_coefficients)
static Color maximum_color_cpu(const Result &input)
float maximum_float_in_range(Context &context, const Result &input, const float lower_bound, const float upper_bound)
static float maximum_float_cpu(const Result &input)
static float maximum_float_gpu(Context &context, const Result &input)
static float sum_log_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
static float sum_log_luminance_gpu(Context &context, const Result &input, const float3 &luminance_coefficients)
static float minimum_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
float sum_log_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
static float minimum_float_cpu(const Result &input)
static Color maximum_color_gpu(Context &context, const Result &input)
static float4 sum_squared_difference_color_cpu(const Result &input, const float4 subtrahend)
Color maximum_color(Context &context, const Result &input)
static float maximum_float_in_range_gpu(Context &context, const Result &input, const float lower_bound, const float upper_bound)
static float4 sum_color_gpu(Context &context, const Result &input)
static float maximum_luminance_cpu(const Result &input, const float3 &luminance_coefficients)
static float minimum_float_in_range_gpu(Context &context, const Result &input, const float lower_bound, const float upper_bound)
float maximum_float(Context &context, const Result &input)
float maximum_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
static float maximum_luminance_gpu(Context &context, const Result &input, const float3 &luminance_coefficients)
static float2 maximum_float2_cpu(const Result &input)
static Color minimum_color_gpu(Context &context, const Result &input)
float4 sum_color(Context &context, const Result &input)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T min(const T &a, const T &b)
T square(const T &a)
T max(const T &a, const T &b)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
void GPU_texture_image_unbind(gpu::Texture *texture)
void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
@ GPU_TEXTURE_USAGE_GENERAL
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
int GPU_texture_width(const gpu::Texture *texture)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
void GPU_texture_unbind(gpu::Texture *texture)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
VecBase< double, 4 > double4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float