Blender V5.3
node_composite_glare.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <array>
6#include <cmath>
7#include <complex>
8#include <cstdint>
9#include <limits>
10
11#include "MEM_guardedalloc.h"
12
13#if defined(WITH_FFTW3)
14# include <fftw3.h>
15#endif
16
17#include "BLI_fftw.hh"
18#include "BLI_index_range.hh"
20#include "BLI_math_base.hh"
21#include "BLI_math_color_c.hh"
22#include "BLI_math_vector.hh"
24#include "BLI_noise.hh"
25#include "BLI_string.hh"
26#include "BLI_task.hh"
27
28#include "BKE_node_runtime.hh"
29
30#include "DNA_node_types.h"
31#include "DNA_scene_types.h"
32
33#include "GPU_shader.hh"
34#include "GPU_state.hh"
35#include "GPU_texture.hh"
36
39#include "COM_node_operation.hh"
40#include "COM_utilities.hh"
42
44
45#define MAX_GLARE_ITERATIONS 5
46
48
49static const EnumPropertyItem type_items[] = {
50 {CMP_NODE_GLARE_BLOOM, "BLOOM", 0, N_("Bloom"), ""},
51 {CMP_NODE_GLARE_GHOST, "GHOSTS", 0, N_("Ghosts"), ""},
52 {CMP_NODE_GLARE_STREAKS, "STREAKS", 0, N_("Streaks"), ""},
53 {CMP_NODE_GLARE_FOG_GLOW, "FOG_GLOW", 0, N_("Fog Glow"), ""},
54 {CMP_NODE_GLARE_SIMPLE_STAR, "SIMPLE_STAR", 0, N_("Simple Star"), ""},
55 {CMP_NODE_GLARE_SUN_BEAMS, "SUN_BEAMS", 0, N_("Sun Beams"), ""},
56 {CMP_NODE_GLARE_KERNEL, "KERNEL", 0, N_("Kernel"), ""},
57 {0, nullptr, 0, nullptr, nullptr},
58};
59
61 {CMP_NODE_GLARE_QUALITY_HIGH, "HIGH", 0, N_("High"), ""},
62 {CMP_NODE_GLARE_QUALITY_MEDIUM, "MEDIUM", 0, N_("Medium"), ""},
63 {CMP_NODE_GLARE_QUALITY_LOW, "LOW", 0, N_("Low"), ""},
64 {0, nullptr, 0, nullptr, nullptr},
65};
66
67enum class KernelDataType : uint8_t {
68 Float = 0,
69 Color = 1,
70};
71
74 "FLOAT",
75 0,
76 N_("Float"),
77 N_("The kernel is a float and will be convolved with all input channels")},
79 "COLOR",
80 0,
81 N_("Color"),
82 N_("The kernel is a color and each channel of the kernel will be convolved with each "
83 "respective channel in the input")},
84 {0, nullptr, 0, nullptr, nullptr},
85};
86
88{
89 b.use_custom_socket_order();
90 b.allow_any_socket_order();
91
92 b.add_input<decl::Color>("Image"_ustr)
93 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
94 .hide_value()
95 .structure_type(StructureType::Dynamic);
96 b.add_output<decl::Color>("Image"_ustr)
97 .structure_type(StructureType::Dynamic)
98 .description("The image with the generated glare added")
99 .align_with_previous();
100
101 b.add_output<decl::Color>("Glare"_ustr)
102 .structure_type(StructureType::Dynamic)
103 .description("The generated glare");
104 b.add_output<decl::Color>("Highlights"_ustr)
105 .structure_type(StructureType::Dynamic)
106 .description("The extracted highlights from which the glare was generated");
107
108 b.add_input<decl::Menu>("Type"_ustr)
109 .default_value(CMP_NODE_GLARE_STREAKS)
110 .static_items(type_items)
112 b.add_input<decl::Menu>("Quality"_ustr)
113 .default_value(CMP_NODE_GLARE_QUALITY_MEDIUM)
114 .static_items(quality_items)
116
117 PanelDeclarationBuilder &highlights_panel = b.add_panel("Highlights"_ustr).default_closed(true);
118 highlights_panel.add_input<decl::Float>("Threshold"_ustr, "Highlights Threshold"_ustr)
119 .default_value(1.0f)
120 .min(0.0f)
122 "The brightness level at which pixels are considered part of the highlights that "
123 "produce a glare");
124 highlights_panel.add_input<decl::Float>("Smoothness"_ustr, "Highlights Smoothness"_ustr)
125 .default_value(0.1f)
126 .min(0.0f)
127 .max(1.0f)
129 .description("The smoothness of the extracted highlights");
130
131 PanelDeclarationBuilder &supress_highlights_panel =
132 highlights_panel.add_panel("Clamp"_ustr).default_closed(true);
133 supress_highlights_panel.add_input<decl::Bool>("Clamp"_ustr, "Clamp Highlights"_ustr)
134 .default_value(false)
135 .panel_toggle()
136 .description("Clamp bright highlights");
137 supress_highlights_panel.add_input<decl::Float>("Maximum"_ustr, "Maximum Highlights"_ustr)
138 .default_value(10.0f)
139 .min(0.0f)
141 "Clamp bright highlights such that their brightness are not larger than this value");
142
143 PanelDeclarationBuilder &mix_panel = b.add_panel("Adjust"_ustr);
144 mix_panel.add_input<decl::Float>("Strength"_ustr)
145 .default_value(1.0f)
146 .min(0.0f)
147 .max(1.0f)
149 .description("Adjusts the brightness of the glare");
150 mix_panel.add_input<decl::Float>("Saturation"_ustr)
151 .default_value(1.0f)
152 .min(0.0f)
153 .max(1.0f)
155 .description("Adjusts the saturation of the glare");
156 mix_panel.add_input<decl::Color>("Tint"_ustr)
157 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
158 .description("Tints the glare. Consider desaturating the glare to more accurate tinting");
159
160 PanelDeclarationBuilder &glare_panel = b.add_panel("Glare"_ustr);
161 glare_panel.add_input<decl::Float>("Size"_ustr)
162 .default_value(0.5f)
163 .min(0.0f)
164 .max(1.0f)
166 .usage_by_menu("Type"_ustr,
168 .description(
169 "The size of the glare relative to the image. 1 means the glare covers the entire "
170 "image, 0.5 means the glare covers half the image, and so on");
171 glare_panel.add_input<decl::Int>("Streaks"_ustr)
172 .default_value(4)
173 .min(1)
174 .max(16)
175 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_STREAKS)
176 .description("The number of streaks");
177 glare_panel.add_input<decl::Float>("Streaks Angle"_ustr)
178 .default_value(0.0f)
180 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_STREAKS)
181 .description("The angle that the first streak makes with the horizontal axis");
182 glare_panel.add_input<decl::Int>("Iterations"_ustr)
183 .default_value(3)
184 .min(2)
185 .max(5)
186 .usage_by_menu("Type"_ustr,
188 .description(
189 "The number of ghosts for Ghost glare or the quality and spread of Glare for Streaks "
190 "and Simple Star");
191 glare_panel.add_input<decl::Float>("Fade"_ustr)
192 .default_value(0.9f)
193 .min(0.75f)
194 .max(1.0f)
196 .usage_by_menu("Type"_ustr, {CMP_NODE_GLARE_SIMPLE_STAR, CMP_NODE_GLARE_STREAKS})
197 .description("Streak fade-out factor");
198 glare_panel.add_input<decl::Float>("Color Modulation"_ustr)
199 .default_value(0.25)
200 .min(0.0f)
201 .max(1.0f)
203 .usage_by_menu("Type"_ustr, {CMP_NODE_GLARE_GHOST, CMP_NODE_GLARE_STREAKS})
204 .description("Modulates colors of streaks and ghosts for a spectral dispersion effect");
205 glare_panel.add_input<decl::Bool>("Diagonal"_ustr, "Diagonal Star"_ustr)
206 .default_value(true)
207 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_SIMPLE_STAR)
208 .description("Align the star diagonally");
209 glare_panel.add_input<decl::Vector>("Sun Position"_ustr)
210 .subtype(PROP_FACTOR)
211 .dimensions(2)
212 .default_value({0.5f, 0.5f})
213 .min(0.0f)
214 .max(1.0f)
215 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_SUN_BEAMS)
216 .description(
217 "The position of the source of the rays in normalized coordinates. 0 means lower left "
218 "corner and 1 means upper right corner");
219 glare_panel.add_input<decl::Float>("Jitter"_ustr)
220 .default_value(0.0f)
221 .min(0.0f)
222 .max(1.0)
224 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_SUN_BEAMS)
225 .description(
226 "The amount of jitter to introduce while computing rays, higher jitter can be faster "
227 "but can produce grainy or noisy results");
228 glare_panel.add_input<decl::Menu>("Kernel Data Type"_ustr)
229 .default_value(KernelDataType::Float)
230 .static_items(kernel_data_type_items)
231 .usage_by_menu("Type"_ustr, CMP_NODE_GLARE_KERNEL)
233 glare_panel.add_input<decl::Float>("Kernel"_ustr, "Float Kernel"_ustr)
234 .hide_value()
235 .structure_type(StructureType::Dynamic)
236 .usage_by_menu("Kernel Data Type"_ustr, int(KernelDataType::Float))
237 .compositor_realization_mode(CompositorInputRealizationMode::Transforms);
238 glare_panel.add_input<decl::Color>("Kernel"_ustr, "Color Kernel"_ustr)
239 .hide_value()
240 .structure_type(StructureType::Dynamic)
241 .usage_by_menu("Kernel Data Type"_ustr, int(KernelDataType::Color))
242 .compositor_realization_mode(CompositorInputRealizationMode::Transforms);
243}
244
245static void node_init(bNodeTree * /*ntree*/, bNode *node)
246{
247 /* Unused, but kept for forward compatibility. */
248 NodeGlare *ndg = MEM_new<NodeGlare>(__func__);
249 node->storage = ndg;
250}
251
252static void node_update_glare_label(const bNodeTree *ntree,
253 const bNode *node,
254 char *label,
255 int maxlen)
256{
257 ntree->ensure_topology_cache();
258 const bNodeSocket *type_input = node->input_by_identifier("Type"_ustr);
259
260 if (type_input->is_logically_linked()) {
261 BLI_strncpy(label, IFACE_("Glare"), maxlen);
262 return;
263 }
264
265 const int type_value = type_input->default_value_typed<bNodeSocketValueMenu>()->value;
266
267 for (const EnumPropertyItem *item = type_items; item->identifier != nullptr; item++) {
268 if (item->value == type_value) {
269 BLI_strncpy(label, IFACE_(item->name), maxlen);
270 return;
271 }
272 }
273}
274
276 public:
279 {
280 bNode &node = params.add_node("CompositorNodeGlare"_ustr);
281 bNodeSocket &type_socket = *bke::node_find_socket(node, SOCK_IN, "Type"_ustr);
282 type_socket.default_value_typed<bNodeSocketValueMenu>()->value = this->type;
283 params.update_and_connect_available_socket(node, "Image"_ustr);
284 }
285};
286
288{
289 const eNodeSocketDatatype from_socket_type = params.other_socket().type;
290 if (!params.node_tree().typeinfo->validate_link(from_socket_type, SOCK_RGBA)) {
291 return;
292 }
293
301}
302
303using namespace blender::compositor;
304
306 public:
308
309 void execute() override
310 {
311 const Result &image_input = this->get_input("Image");
312 if (image_input.is_single_value()) {
313 Result &image_output = this->get_result("Image");
314 if (image_output.should_compute()) {
315 image_output.share_data(image_input);
316 }
318 return;
319 }
320
321 Result highlights = this->compute_highlights();
322 Result glare = this->compute_glare(highlights);
323
324 Result &highlights_output = this->get_result("Highlights");
325 if (highlights_output.should_compute()) {
326 if (highlights.domain().data_size != image_input.domain().data_size) {
327 /* The highlights were computed on a fraction of the image size, see the get_quality_factor
328 * method. So we need to upsample them while writing as opposed to just stealing the
329 * existing data. */
330 this->write_highlights_output(highlights);
331 }
332 else {
333 highlights_output.share_data(highlights);
334 }
335 }
336 highlights.release();
337
338 /* Combine the original input and the generated glare. */
339 execute_mix(glare);
340
341 Result &glare_output = this->get_result("Glare");
342 if (glare_output.should_compute()) {
343 this->write_glare_output(glare);
344 }
345 glare.release();
346 }
347
348 /* -----------------
349 * Glare Highlights.
350 * ----------------- */
351
353 {
354 if (this->context().use_gpu()) {
355 return this->execute_highlights_gpu();
356 }
357 return this->execute_highlights_cpu();
358 }
359
361 {
362 gpu::Shader *shader = context().get_shader("compositor_glare_highlights");
364
365 GPU_shader_uniform_1f(shader, "threshold", this->get_threshold());
366 GPU_shader_uniform_1f(shader, "highlights_smoothness", this->get_highlights_smoothness());
367 GPU_shader_uniform_1f(shader, "max_brightness", this->get_maximum_brightness());
368 GPU_shader_uniform_1i(shader, "quality", this->get_quality());
369
370 const Result &input_image = get_input("Image");
371 GPU_texture_filter_mode(input_image, true);
372 input_image.bind_as_texture(shader, "input_tx");
373
374 const Domain highlights_domain = this->get_glare_image_domain();
375 Result highlights_result = context().create_result(ResultType::Color);
376 highlights_result.allocate_texture(highlights_domain);
377 highlights_result.bind_as_image(shader, "output_img");
378
380
382 input_image.unbind_as_texture();
383 highlights_result.unbind_as_image();
384
385 return highlights_result;
386 }
387
389 {
390 const float threshold = this->get_threshold();
391 const float highlights_smoothness = this->get_highlights_smoothness();
392 const float max_brightness = this->get_maximum_brightness();
393
394 const Result &input = get_input("Image");
395
396 const Domain highlights_size = this->get_glare_image_domain();
398 output.allocate_texture(highlights_size);
399
400 const CMPNodeGlareQuality quality = this->get_quality();
401 const int2 input_size = input.domain().data_size;
402
403 parallel_for(highlights_size.data_size, [&](const int2 texel) {
404 float4 color = float4(0.0f);
405
406 switch (quality) {
407 case CMP_NODE_GLARE_QUALITY_HIGH: {
408 color = float4(input.load_pixel<Color>(texel));
409 break;
410 }
411
412 /* Down-sample the image 2 times to match the output size by averaging the 2x2 block of
413 * pixels into a single output pixel. This is done due to the bilinear interpolation at the
414 * center of the 2x2 block of pixels */
415 case CMP_NODE_GLARE_QUALITY_MEDIUM: {
416 float2 normalized_coordinates = (float2(texel) * 2.0f + float2(1.0f)) /
417 float2(input_size);
418 color = float4(input.sample_bilinear_extended<Color>(normalized_coordinates));
419 break;
420 }
421
422 /* Down-sample the image 4 times to match the output size by averaging each 4x4 block of
423 * pixels into a single output pixel. This is done by averaging 4 bilinear taps at the
424 * center of each of the corner 2x2 pixel blocks, which are themselves the average of the
425 * 2x2 block due to the bilinear interpolation at the center. */
426 case CMP_NODE_GLARE_QUALITY_LOW: {
427
428 float2 lower_left_coordinates = (float2(texel) * 4.0f + float2(1.0f)) /
429 float2(input_size);
430 float4 lower_left_color = float4(
431 input.sample_bilinear_extended<Color>(lower_left_coordinates));
432
433 float2 lower_right_coordinates = (float2(texel) * 4.0f + float2(3.0f, 1.0f)) /
434 float2(input_size);
435 float4 lower_right_color = float4(
436 input.sample_bilinear_extended<Color>(lower_right_coordinates));
437
438 float2 upper_left_coordinates = (float2(texel) * 4.0f + float2(1.0f, 3.0f)) /
439 float2(input_size);
440 float4 upper_left_color = float4(
441 input.sample_bilinear_extended<Color>(upper_left_coordinates));
442
443 float2 upper_right_coordinates = (float2(texel) * 4.0f + float2(3.0f)) /
444 float2(input_size);
445 float4 upper_right_color = float4(
446 input.sample_bilinear_extended<Color>(upper_right_coordinates));
447
448 color = (upper_left_color + upper_right_color + lower_left_color + lower_right_color) /
449 4.0f;
450 break;
451 }
452 }
453
454 float4 hsva;
455 rgb_to_hsv_v(color, hsva);
456
457 /* Clamp the brightness of the highlights such that pixels whose brightness are less than the
458 * threshold will be equal to the threshold and will become zero once threshold is subtracted
459 * later. We also clamp by the specified max brightness to suppress very bright highlights.
460 *
461 * We use a smooth clamping function such that highlights do not become very sharp but use
462 * the adaptive variant such that we guarantee that zero highlights remain zero even after
463 * smoothing. Notice that when we mention zero, we mean zero after subtracting the threshold,
464 * so we actually mean the minimum bound, the threshold. See the adaptive_smooth_clamp
465 * function for more information. */
466 const float clamped_brightness = this->adaptive_smooth_clamp(
467 hsva.z, threshold, max_brightness, highlights_smoothness);
468
469 /* The final brightness is relative to the threshold. */
470 hsva.z = clamped_brightness - threshold;
471
472 float4 rgba;
473 hsv_to_rgb_v(hsva, rgba);
474
475 output.store_pixel(texel, Color(float4(rgba.xyz(), 1.0f)));
476 });
477
478 return output;
479 }
480
482 {
483 /* Clamp disabled, return the maximum possible brightness. */
484 if (!this->get_clamp_highlights()) {
485 return std::numeric_limits<float>::max();
486 }
487
488 /* Brightness of the highlights are relative to the threshold, see execute_highlights_cpu, so
489 * we add the threshold such that the maximum brightness corresponds to the actual brightness
490 * of the computed highlights. */
491 return this->get_threshold() + this->get_max_highlights();
492 }
493
494 /* A Quadratic Polynomial smooth minimum function *without* normalization, based on:
495 *
496 * https://iquilezles.org/articles/smin/
497 *
498 * This should not be converted into a common utility function in BLI because the glare code is
499 * specifically designed for it as can be seen in the adaptive_smooth_clamp method, and it is
500 * intentionally not normalized. */
501 float smooth_min(const float a, const float b, const float smoothness)
502 {
503 if (smoothness == 0.0f) {
504 return math::min(a, b);
505 }
506 const float h = math::max(smoothness - math::abs(a - b), 0.0f) / smoothness;
507 return math::min(a, b) - h * h * smoothness * (1.0f / 4.0f);
508 }
509
510 float smooth_max(const float a, const float b, const float smoothness)
511 {
512 return -this->smooth_min(-a, -b, smoothness);
513 }
514
515 /* Clamps the input x within min_value and max_value using a quadratic polynomial smooth minimum
516 * and maximum functions, with individual control over their smoothness. */
517 float smooth_clamp(const float x,
518 const float min_value,
519 const float max_value,
520 const float min_smoothness,
521 const float max_smoothness)
522 {
523 return this->smooth_min(
524 max_value, this->smooth_max(min_value, x, min_smoothness), max_smoothness);
525 }
526
527 /* A variant of smooth_clamp that limits the smoothness such that the function evaluates to the
528 * given min for 0 <= min <= max and x >= 0. The aforementioned guarantee holds for the standard
529 * clamp function by definition, but since the smooth clamp function gradually increases before
530 * the specified min/max, if min/max are sufficiently close together or to zero, they will not
531 * evaluate to min at zero or at min, since zero or min will be at the region of the gradual
532 * increase.
533 *
534 * It can be shown that the width of the gradual increase region is equivalent to the smoothness
535 * parameter, so smoothness can't be larger than the difference between the min/max and zero, or
536 * larger than the difference between min and max themselves. Otherwise, zero or min will lie
537 * inside the gradual increase region of min/max. So we limit the smoothness of min/max by taking
538 * the minimum with the distances to zero and to the distance to the other bound. */
539 float adaptive_smooth_clamp(const float x,
540 const float min_value,
541 const float max_value,
542 const float smoothness)
543 {
544 const float range_distance = math::distance(min_value, max_value);
545 const float distance_from_min_to_zero = math::distance(min_value, 0.0f);
546 const float distance_from_max_to_zero = math::distance(max_value, 0.0f);
547
548 const float max_safe_smoothness_for_min = math::min(distance_from_min_to_zero, range_distance);
549 const float max_safe_smoothness_for_max = math::min(distance_from_max_to_zero, range_distance);
550
551 const float min_smoothness = math::min(smoothness, max_safe_smoothness_for_min);
552 const float max_smoothness = math::min(smoothness, max_safe_smoothness_for_max);
553
554 return this->smooth_clamp(x, min_value, max_value, min_smoothness, max_smoothness);
555 }
556
558 {
559 return math::max(0.0f,
560 this->get_input("Highlights Threshold").get_single_value_default<float>());
561 }
562
564 {
565 return math::max(0.0f,
566 this->get_input("Highlights Smoothness").get_single_value_default<float>());
567 }
568
570 {
571 return this->get_input("Clamp Highlights").get_single_value_default<bool>();
572 }
573
575 {
576 return math::max(0.0f,
577 this->get_input("Maximum Highlights").get_single_value_default<float>());
578 }
579
580 /* Writes the given input highlights by upsampling it using bilinear interpolation to match the
581 * size of the original input, allocating the highlights output and writing the result to it. */
582 void write_highlights_output(const Result &highlights)
583 {
584 if (this->context().use_gpu()) {
585 this->write_highlights_output_gpu(highlights);
586 }
587 else {
588 this->write_highlights_output_cpu(highlights);
589 }
590 }
591
592 void write_highlights_output_gpu(const Result &highlights)
593 {
594 gpu::Shader *shader = this->context().get_shader("compositor_glare_write_highlights_output");
596
597 GPU_texture_filter_mode(highlights, true);
599 highlights.bind_as_texture(shader, "input_tx");
600
601 const Result &image_input = this->get_input("Image");
602 Result &output = this->get_result("Highlights");
603 output.allocate_texture(image_input.domain());
604 output.bind_as_image(shader, "output_img");
605
607
609 output.unbind_as_image();
610 highlights.unbind_as_texture();
611 }
612
613 void write_highlights_output_cpu(const Result &highlights)
614 {
615 const Result &image_input = this->get_input("Image");
616 Result &output = this->get_result("Highlights");
617 output.allocate_texture(image_input.domain());
618
619 const int2 size = output.domain().data_size;
620 parallel_for(size, [&](const int2 texel) {
621 float2 normalized_coordinates = (float2(texel) + float2(0.5f)) / float2(size);
622 output.store_pixel(texel,
623 highlights.sample_bilinear_extended<Color>(normalized_coordinates));
624 });
625 }
626
627 /* ------
628 * Glare.
629 * ------ */
630
631 Result compute_glare(Result &highlights_result)
632 {
633 if (!this->should_compute_glare()) {
634 return this->context().create_result(ResultType::Color);
635 }
636
637 switch (this->get_type()) {
639 return this->execute_simple_star(highlights_result);
641 return this->execute_fog_glow(highlights_result);
643 return this->execute_streaks(highlights_result);
645 return this->execute_ghost(highlights_result);
647 return this->execute_bloom(highlights_result);
649 return this->execute_sun_beams(highlights_result);
651 return this->execute_kernel(highlights_result);
652 }
653
654 return this->execute_simple_star(highlights_result);
655 }
656
657 /* Glare should be computed either because the glare output is needed directly or the image
658 * output is needed. */
660 {
661 return this->get_result("Glare").should_compute() ||
662 this->get_result("Image").should_compute();
663 }
664
665 /* ------------------
666 * Simple Star Glare.
667 * ------------------ */
668
670 {
671 if (this->get_diagonal_star()) {
672 return execute_simple_star_diagonal(highlights);
673 }
674 return execute_simple_star_axis_aligned(highlights);
675 }
676
678 {
679 Result horizontal_pass_result = execute_simple_star_horizontal_pass(highlights);
680 Result vertical_pass_result = this->execute_simple_star_vertical_pass(highlights,
681 horizontal_pass_result);
682 horizontal_pass_result.release();
683 return vertical_pass_result;
684 }
685
687 const Result &horizontal_pass_result)
688 {
689 if (this->context().use_gpu()) {
690 return this->execute_simple_star_vertical_pass_gpu(highlights, horizontal_pass_result);
691 }
692 return this->execute_simple_star_vertical_pass_cpu(highlights, horizontal_pass_result);
693 }
694
696 const Result &horizontal_pass_result)
697 {
698 /* First, copy the highlights result to the output since we will be doing the computation
699 * in-place. */
700 const int2 size = highlights.domain().data_size;
701 Result vertical_pass_result = context().create_result(ResultType::Color);
702 vertical_pass_result.allocate_texture(size);
704 GPU_texture_copy(vertical_pass_result, highlights);
705
706 gpu::Shader *shader = context().get_shader("compositor_glare_simple_star_vertical_pass");
708
710 GPU_shader_uniform_1f(shader, "fade_factor", this->get_fade());
711
712 horizontal_pass_result.bind_as_texture(shader, "horizontal_tx");
713
714 vertical_pass_result.bind_as_image(shader, "vertical_img");
715
716 /* Dispatch a thread for each column in the image. */
717 const int width = size.x;
719
720 horizontal_pass_result.unbind_as_texture();
721 vertical_pass_result.unbind_as_image();
723
724 return vertical_pass_result;
725 }
726
728 const Result &horizontal_pass_result)
729 {
730 /* First, copy the highlights result to the output since we will be doing the computation
731 * in-place. */
732 const int2 size = highlights.domain().data_size;
733 Result output = this->context().create_result(ResultType::Color);
734 output.allocate_texture(size);
735 parallel_for(size, [&](const int2 texel) {
736 output.store_pixel(texel, highlights.load_pixel<Color>(texel));
737 });
738
739 const int iterations = this->get_number_of_iterations();
740 const float fade_factor = this->get_fade();
741
742 /* Dispatch a thread for each column in the image. */
743 const int width = size.x;
744 threading::parallel_for(IndexRange(width), 1, [&](const IndexRange sub_range) {
745 for (const int64_t x : sub_range) {
746 int height = size.y;
747
748 /* For each iteration, apply a causal filter followed by a non causal filters along the
749 * column mapped to the current thread invocation. */
750 for (int i = 0; i < iterations; i++) {
751 /* Causal Pass:
752 * Sequentially apply a causal filter running from bottom to top by mixing the value of
753 * the pixel in the column with the average value of the previous output and next input
754 * in the same column. */
755 for (int y = 0; y < height; y++) {
756 int2 texel = int2(x, y);
757 float4 previous_output = float4(output.load_pixel_zero<Color>(texel - int2(0, i)));
758 float4 current_input = float4(output.load_pixel<Color>(texel));
759 float4 next_input = float4(output.load_pixel_zero<Color>(texel + int2(0, i)));
760
761 float4 neighbor_average = (previous_output + next_input) / 2.0f;
762 float4 causal_output = math::interpolate(current_input, neighbor_average, fade_factor);
763 output.store_pixel(texel, Color(causal_output));
764 }
765
766 /* Non Causal Pass:
767 * Sequentially apply a non causal filter running from top to bottom by mixing the value
768 * of the pixel in the column with the average value of the previous output and next
769 * input in the same column. */
770 for (int y = height - 1; y >= 0; y--) {
771 int2 texel = int2(x, y);
772 float4 previous_output = float4(output.load_pixel_zero<Color>(texel + int2(0, i)));
773 float4 current_input = float4(output.load_pixel<Color>(texel));
774 float4 next_input = float4(output.load_pixel_zero<Color>(texel - int2(0, i)));
775
776 float4 neighbor_average = (previous_output + next_input) / 2.0f;
777 float4 non_causal_output = math::interpolate(
778 current_input, neighbor_average, fade_factor);
779 output.store_pixel(texel, Color(non_causal_output));
780 }
781 }
782
783 /* For each pixel in the column mapped to the current invocation thread, add the result of
784 * the horizontal pass to the vertical pass. */
785 for (int y = 0; y < height; y++) {
786 int2 texel = int2(x, y);
787 float4 horizontal = float4(horizontal_pass_result.load_pixel<Color>(texel));
788 float4 vertical = float4(output.load_pixel<Color>(texel));
789 float4 combined = horizontal + vertical;
790 output.store_pixel(texel, Color(float4(combined.xyz(), 1.0f)));
791 }
792 }
793 });
794
795 return output;
796 }
797
799 {
800 if (this->context().use_gpu()) {
801 return this->execute_simple_star_horizontal_pass_gpu(highlights);
802 }
803 return this->execute_simple_star_horizontal_pass_cpu(highlights);
804 }
805
807 {
808 /* First, copy the highlights result to the output since we will be doing the computation
809 * in-place. */
810 const int2 size = highlights.domain().data_size;
811 Result horizontal_pass_result = context().create_result(ResultType::Color);
812 horizontal_pass_result.allocate_texture(size);
814 GPU_texture_copy(horizontal_pass_result, highlights);
815
816 gpu::Shader *shader = context().get_shader("compositor_glare_simple_star_horizontal_pass");
818
820 GPU_shader_uniform_1f(shader, "fade_factor", this->get_fade());
821
822 horizontal_pass_result.bind_as_image(shader, "horizontal_img");
823
824 /* Dispatch a thread for each row in the image. */
826
827 horizontal_pass_result.unbind_as_image();
829
830 return horizontal_pass_result;
831 }
832
834 {
835 /* First, copy the highlights result to the output since we will be doing the computation
836 * in-place. */
837 const int2 size = highlights.domain().data_size;
838 Result horizontal_pass_result = context().create_result(ResultType::Color);
839 horizontal_pass_result.allocate_texture(size);
840 parallel_for(size, [&](const int2 texel) {
841 horizontal_pass_result.store_pixel(texel, highlights.load_pixel<Color>(texel));
842 });
843
844 const int iterations = this->get_number_of_iterations();
845 const float fade_factor = this->get_fade();
846
847 /* Dispatch a thread for each row in the image. */
848 const int width = size.x;
849 threading::parallel_for(IndexRange(size.y), 1, [&](const IndexRange sub_range) {
850 for (const int64_t y : sub_range) {
851 /* For each iteration, apply a causal filter followed by a non causal filters along the
852 * row mapped to the current thread invocation. */
853 for (int i = 0; i < iterations; i++) {
854 /* Causal Pass:
855 * Sequentially apply a causal filter running from left to right by mixing the value of
856 * the pixel in the row with the average value of the previous output and next input in
857 * the same row. */
858 for (int x = 0; x < width; x++) {
859 int2 texel = int2(x, y);
860 float4 previous_output = float4(
861 horizontal_pass_result.load_pixel_zero<Color>(texel - int2(i, 0)));
862 float4 current_input = float4(horizontal_pass_result.load_pixel<Color>(texel));
863 float4 next_input = float4(
864 horizontal_pass_result.load_pixel_zero<Color>(texel + int2(i, 0)));
865
866 float4 neighbor_average = (previous_output + next_input) / 2.0f;
867 float4 causal_output = math::interpolate(current_input, neighbor_average, fade_factor);
868 horizontal_pass_result.store_pixel(texel, Color(causal_output));
869 }
870
871 /* Non Causal Pass:
872 * Sequentially apply a non causal filter running from right to left by mixing the
873 * value of the pixel in the row with the average value of the previous output and next
874 * input in the same row. */
875 for (int x = width - 1; x >= 0; x--) {
876 int2 texel = int2(x, y);
877 float4 previous_output = float4(
878 horizontal_pass_result.load_pixel_zero<Color>(texel + int2(i, 0)));
879 float4 current_input = float4(horizontal_pass_result.load_pixel<Color>(texel));
880 float4 next_input = float4(
881 horizontal_pass_result.load_pixel_zero<Color>(texel - int2(i, 0)));
882
883 float4 neighbor_average = (previous_output + next_input) / 2.0f;
884 float4 non_causal_output = math::interpolate(
885 current_input, neighbor_average, fade_factor);
886 horizontal_pass_result.store_pixel(texel, Color(non_causal_output));
887 }
888 }
889 }
890 });
891
892 return horizontal_pass_result;
893 }
894
896 {
897 Result diagonal_pass_result = execute_simple_star_diagonal_pass(highlights);
898 Result anti_diagonal_pass_result = this->execute_simple_star_anti_diagonal_pass(
899 highlights, diagonal_pass_result);
900 diagonal_pass_result.release();
901 return anti_diagonal_pass_result;
902 }
903
905 const Result &diagonal_pass_result)
906 {
907 if (this->context().use_gpu()) {
908 return this->execute_simple_star_anti_diagonal_pass_gpu(highlights, diagonal_pass_result);
909 }
910 return this->execute_simple_star_anti_diagonal_pass_cpu(highlights, diagonal_pass_result);
911 }
912
914 const Result &diagonal_pass_result)
915 {
916 /* First, copy the highlights result to the output since we will be doing the computation
917 * in-place. */
918 const int2 size = highlights.domain().data_size;
919 Result anti_diagonal_pass_result = context().create_result(ResultType::Color);
920 anti_diagonal_pass_result.allocate_texture(size);
922 GPU_texture_copy(anti_diagonal_pass_result, highlights);
923
924 gpu::Shader *shader = context().get_shader("compositor_glare_simple_star_anti_diagonal_pass");
926
928 GPU_shader_uniform_1f(shader, "fade_factor", this->get_fade());
929
930 diagonal_pass_result.bind_as_texture(shader, "diagonal_tx");
931
932 anti_diagonal_pass_result.bind_as_image(shader, "anti_diagonal_img");
933
934 /* Dispatch a thread for each diagonal in the image. */
936
937 diagonal_pass_result.unbind_as_texture();
938 anti_diagonal_pass_result.unbind_as_image();
940
941 return anti_diagonal_pass_result;
942 }
943
945 const Result &diagonal_pass_result)
946 {
947 /* First, copy the highlights result to the output since we will be doing the computation
948 * in-place. */
949 const int2 size = highlights.domain().data_size;
950 Result output = this->context().create_result(ResultType::Color);
951 output.allocate_texture(size);
952 parallel_for(size, [&](const int2 texel) {
953 output.store_pixel(texel, highlights.load_pixel<Color>(texel));
954 });
955
956 const int iterations = this->get_number_of_iterations();
957 const float fade_factor = this->get_fade();
958
959 /* Dispatch a thread for each diagonal in the image. */
960 const int diagonals_count = compute_number_of_diagonals(size);
961 threading::parallel_for(IndexRange(diagonals_count), 1, [&](const IndexRange sub_range) {
962 for (const int64_t index : sub_range) {
963 int anti_diagonal_length = compute_anti_diagonal_length(size, index);
965 int2 direction = get_anti_diagonal_direction();
966 int2 end = start + (anti_diagonal_length - 1) * direction;
967
968 /* For each iteration, apply a causal filter followed by a non causal filters along the
969 * anti diagonal mapped to the current thread invocation. */
970 for (int i = 0; i < iterations; i++) {
971 /* Causal Pass:
972 * Sequentially apply a causal filter running from the start of the anti diagonal to
973 * its end by mixing the value of the pixel in the anti diagonal with the average value
974 * of the previous output and next input in the same anti diagonal. */
975 for (int j = 0; j < anti_diagonal_length; j++) {
976 int2 texel = start + j * direction;
977 float4 previous_output = float4(output.load_pixel_zero<Color>(texel - i * direction));
978 float4 current_input = float4(output.load_pixel<Color>(texel));
979 float4 next_input = float4(output.load_pixel_zero<Color>(texel + i * direction));
980
981 float4 neighbor_average = (previous_output + next_input) / 2.0f;
982 float4 causal_output = math::interpolate(current_input, neighbor_average, fade_factor);
983 output.store_pixel(texel, Color(causal_output));
984 }
985
986 /* Non Causal Pass:
987 * Sequentially apply a non causal filter running from the end of the diagonal to its
988 * start by mixing the value of the pixel in the diagonal with the average value of the
989 * previous output and next input in the same diagonal. */
990 for (int j = 0; j < anti_diagonal_length; j++) {
991 int2 texel = end - j * direction;
992 float4 previous_output = float4(output.load_pixel_zero<Color>(texel + i * direction));
993 float4 current_input = float4(output.load_pixel<Color>(texel));
994 float4 next_input = float4(output.load_pixel_zero<Color>(texel - i * direction));
995
996 float4 neighbor_average = (previous_output + next_input) / 2.0f;
997 float4 non_causal_output = math::interpolate(
998 current_input, neighbor_average, fade_factor);
999 output.store_pixel(texel, Color(non_causal_output));
1000 }
1001 }
1002
1003 /* For each pixel in the anti diagonal mapped to the current invocation thread, add the
1004 * result of the diagonal pass to the vertical pass. */
1005 for (int j = 0; j < anti_diagonal_length; j++) {
1006 int2 texel = start + j * direction;
1007 float4 horizontal = float4(diagonal_pass_result.load_pixel<Color>(texel));
1008 float4 vertical = float4(output.load_pixel<Color>(texel));
1009 float4 combined = horizontal + vertical;
1010 output.store_pixel(texel, Color(float4(combined.xyz(), 1.0f)));
1011 }
1012 }
1013 });
1014
1015 return output;
1016 }
1017
1019 {
1020 if (this->context().use_gpu()) {
1021 return this->execute_simple_star_diagonal_pass_gpu(highlights);
1022 }
1023 return this->execute_simple_star_diagonal_pass_cpu(highlights);
1024 }
1025
1027 {
1028 /* First, copy the highlights result to the output since we will be doing the computation
1029 * in-place. */
1030 const int2 size = highlights.domain().data_size;
1031 Result diagonal_pass_result = context().create_result(ResultType::Color);
1032 diagonal_pass_result.allocate_texture(size);
1034 GPU_texture_copy(diagonal_pass_result, highlights);
1035
1036 gpu::Shader *shader = context().get_shader("compositor_glare_simple_star_diagonal_pass");
1038
1040 GPU_shader_uniform_1f(shader, "fade_factor", this->get_fade());
1041
1042 diagonal_pass_result.bind_as_image(shader, "diagonal_img");
1043
1044 /* Dispatch a thread for each diagonal in the image. */
1046
1047 diagonal_pass_result.unbind_as_image();
1049
1050 return diagonal_pass_result;
1051 }
1052
1054 {
1055 /* First, copy the highlights result to the output since we will be doing the computation
1056 * in-place. */
1057 const int2 size = highlights.domain().data_size;
1058 Result diagonal_pass_result = this->context().create_result(ResultType::Color);
1059 diagonal_pass_result.allocate_texture(size);
1060 parallel_for(size, [&](const int2 texel) {
1061 diagonal_pass_result.store_pixel(texel, highlights.load_pixel<Color>(texel));
1062 });
1063
1064 const int iterations = this->get_number_of_iterations();
1065 const float fade_factor = this->get_fade();
1066
1067 /* Dispatch a thread for each diagonal in the image. */
1068 const int diagonals_count = compute_number_of_diagonals(size);
1069 threading::parallel_for(IndexRange(diagonals_count), 1, [&](const IndexRange sub_range) {
1070 for (const int64_t index : sub_range) {
1071 int diagonal_length = compute_diagonal_length(size, index);
1072 int2 start = compute_diagonal_start(size, index);
1073 int2 direction = get_diagonal_direction();
1074 int2 end = start + (diagonal_length - 1) * direction;
1075
1076 /* For each iteration, apply a causal filter followed by a non causal filters along the
1077 * diagonal mapped to the current thread invocation. */
1078 for (int i = 0; i < iterations; i++) {
1079 /* Causal Pass:
1080 * Sequentially apply a causal filter running from the start of the diagonal to its end
1081 * by mixing the value of the pixel in the diagonal with the average value of the
1082 * previous output and next input in the same diagonal. */
1083 for (int j = 0; j < diagonal_length; j++) {
1084 int2 texel = start + j * direction;
1085 float4 previous_output = float4(
1086 diagonal_pass_result.load_pixel_zero<Color>(texel - i * direction));
1087 float4 current_input = float4(diagonal_pass_result.load_pixel<Color>(texel));
1088 float4 next_input = float4(
1089 diagonal_pass_result.load_pixel_zero<Color>(texel + i * direction));
1090
1091 float4 neighbor_average = (previous_output + next_input) / 2.0f;
1092 float4 causal_output = math::interpolate(current_input, neighbor_average, fade_factor);
1093 diagonal_pass_result.store_pixel(texel, Color(causal_output));
1094 }
1095
1096 /* Non Causal Pass:
1097 * Sequentially apply a non causal filter running from the end of the diagonal to its
1098 * start by mixing the value of the pixel in the diagonal with the average value of the
1099 * previous output and next input in the same diagonal. */
1100 for (int j = 0; j < diagonal_length; j++) {
1101 int2 texel = end - j * direction;
1102 float4 previous_output = float4(
1103 diagonal_pass_result.load_pixel_zero<Color>(texel + i * direction));
1104 float4 current_input = float4(diagonal_pass_result.load_pixel<Color>(texel));
1105 float4 next_input = float4(
1106 diagonal_pass_result.load_pixel_zero<Color>(texel - i * direction));
1107
1108 float4 neighbor_average = (previous_output + next_input) / 2.0f;
1109 float4 non_causal_output = math::interpolate(
1110 current_input, neighbor_average, fade_factor);
1111 diagonal_pass_result.store_pixel(texel, Color(non_causal_output));
1112 }
1113 }
1114 }
1115 });
1116
1117 return diagonal_pass_result;
1118 }
1119
1121 {
1122 return this->get_input("Diagonal Star").get_single_value_default<bool>();
1123 }
1124
1125 /* --------------
1126 * Streaks Glare.
1127 * -------------- */
1128
1129 Result execute_streaks(const Result &highlights)
1130 {
1131 /* Create an initially zero image where streaks will be accumulated. */
1132 const int2 size = highlights.domain().data_size;
1133 Result accumulated_streaks_result = context().create_result(ResultType::Color);
1134 accumulated_streaks_result.allocate_texture(size);
1135 if (this->context().use_gpu()) {
1136 const float4 zero_color = float4(0.0f);
1137 GPU_texture_clear(accumulated_streaks_result, GPU_DATA_FLOAT, zero_color);
1138 }
1139 else {
1140 parallel_for(size, [&](const int2 texel) {
1141 accumulated_streaks_result.store_pixel(texel, Color(float4(0.0f)));
1142 });
1143 }
1144
1145 /* For each streak, compute its direction and apply a streak filter in that direction, then
1146 * accumulate the result into the accumulated streaks result. */
1147 for (const int streak_index : IndexRange(get_number_of_streaks())) {
1148 const float2 streak_direction = compute_streak_direction(streak_index);
1149 Result streak_result = apply_streak_filter(highlights, streak_direction);
1150 this->accumulate_streak(streak_result, accumulated_streaks_result);
1151 streak_result.release();
1152 }
1153
1154 return accumulated_streaks_result;
1155 }
1156
1157 Result apply_streak_filter(const Result &highlights, const float2 &streak_direction)
1158 {
1159 if (this->context().use_gpu()) {
1160 return this->apply_streak_filter_gpu(highlights, streak_direction);
1161 }
1162 return this->apply_streak_filter_cpu(highlights, streak_direction);
1163 }
1164
1165 Result apply_streak_filter_gpu(const Result &highlights, const float2 &streak_direction)
1166 {
1167 gpu::Shader *shader = context().get_shader("compositor_glare_streaks_filter");
1169
1170 /* Copy the highlights result into a new result because the output will be copied to the input
1171 * after each iteration. */
1172 const int2 size = highlights.domain().data_size;
1173 Result input_streak_result = context().create_result(ResultType::Color);
1174 input_streak_result.allocate_texture(size);
1176 GPU_texture_copy(input_streak_result, highlights);
1177
1178 Result output_streak_result = context().create_result(ResultType::Color);
1179 output_streak_result.allocate_texture(size);
1180
1181 /* For the given number of iterations, apply the streak filter in the given direction. The
1182 * result of the previous iteration is used as the input of the current iteration. */
1183 const IndexRange iterations_range = IndexRange(get_number_of_iterations());
1184 for (const int iteration : iterations_range) {
1185 const float color_modulator = compute_streak_color_modulator(iteration);
1186 const float iteration_magnitude = compute_streak_iteration_magnitude(iteration);
1187 const float3 fade_factors = compute_streak_fade_factors(iteration_magnitude);
1188 const float2 streak_vector = streak_direction * iteration_magnitude;
1189
1190 GPU_shader_uniform_1f(shader, "color_modulator", color_modulator);
1191 GPU_shader_uniform_3fv(shader, "fade_factors", fade_factors);
1192 GPU_shader_uniform_2fv(shader, "streak_vector", streak_vector);
1193
1194 GPU_texture_filter_mode(input_streak_result, true);
1196 input_streak_result.bind_as_texture(shader, "input_streak_tx");
1197
1198 output_streak_result.bind_as_image(shader, "output_streak_img");
1199
1201
1202 input_streak_result.unbind_as_texture();
1203 output_streak_result.unbind_as_image();
1204
1205 /* The accumulated result serves as the input for the next iteration, so copy the result to
1206 * the input result since it can't be used for reading and writing simultaneously. Skip
1207 * copying for the last iteration since it is not needed. */
1208 if (iteration != iterations_range.last()) {
1210 GPU_texture_copy(input_streak_result, output_streak_result);
1211 }
1212 }
1213
1214 input_streak_result.release();
1216
1217 return output_streak_result;
1218 }
1219
1220 Result apply_streak_filter_cpu(const Result &highlights, const float2 &streak_direction)
1221 {
1222 /* Copy the highlights result into a new result because the output will be copied to the input
1223 * after each iteration. */
1224 const int2 size = highlights.domain().data_size;
1225 Result input = this->context().create_result(ResultType::Color);
1226 input.allocate_texture(size);
1227 parallel_for(size, [&](const int2 texel) {
1228 input.store_pixel(texel, highlights.load_pixel<Color>(texel));
1229 });
1230
1231 Result output = this->context().create_result(ResultType::Color);
1232 output.allocate_texture(size);
1233
1234 /* For the given number of iterations, apply the streak filter in the given direction. The
1235 * result of the previous iteration is used as the input of the current iteration. */
1236 const IndexRange iterations_range = IndexRange(this->get_number_of_iterations());
1237 for (const int iteration : iterations_range) {
1238 const float color_modulator = this->compute_streak_color_modulator(iteration);
1239 const float iteration_magnitude = this->compute_streak_iteration_magnitude(iteration);
1240 const float3 fade_factors = this->compute_streak_fade_factors(iteration_magnitude);
1241 const float2 streak_vector = streak_direction * iteration_magnitude;
1242
1243 parallel_for(size, [&](const int2 texel) {
1244 /* Add 0.5 to evaluate the input sampler at the center of the pixel and divide by the image
1245 * size to get the coordinates into the sampler's expected [0, 1] range. Similarly,
1246 * transform the vector into the sampler's space by dividing by the input size. */
1247 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1248 float2 vector = streak_vector / float2(size);
1249
1250 /* Load three equally spaced neighbors to the current pixel in the direction of the streak
1251 * vector. */
1252 float4 neighbors[3];
1253 neighbors[0] = float4(input.sample_bilinear_zero<Color>(coordinates + vector));
1254 neighbors[1] = float4(input.sample_bilinear_zero<Color>(coordinates + vector * 2.0f));
1255 neighbors[2] = float4(input.sample_bilinear_zero<Color>(coordinates + vector * 3.0f));
1256
1257 /* Attenuate the value of two of the channels for each of the neighbors by multiplying by
1258 * the color modulator. The particular channels for each neighbor were chosen to be
1259 * visually similar to the modulation pattern of chromatic aberration. */
1260 neighbors[0] *= float4(1.0f, color_modulator, color_modulator, 1.0f);
1261 neighbors[1] *= float4(color_modulator, color_modulator, 1.0f, 1.0f);
1262 neighbors[2] *= float4(color_modulator, 1.0f, color_modulator, 1.0f);
1263
1264 /* Compute the weighted sum of all neighbors using the given fade factors as weights. The
1265 * weights are expected to be lower for neighbors that are further away. */
1266 float4 weighted_neighbors_sum = float4(0.0f);
1267 for (int i = 0; i < 3; i++) {
1268 weighted_neighbors_sum += fade_factors[i] * neighbors[i];
1269 }
1270
1271 /* The output is the average between the center color and the weighted sum of the
1272 * neighbors. Which intuitively mean that highlights will spread in the direction of the
1273 * streak, which is the desired result. */
1274 float4 center_color = float4(input.sample_bilinear_zero<Color>(coordinates));
1275 float4 output_color = (center_color + weighted_neighbors_sum) / 2.0f;
1276 output.store_pixel(texel, Color(output_color));
1277 });
1278
1279 /* The accumulated result serves as the input for the next iteration, so copy the result to
1280 * the input result since it can't be used for reading and writing simultaneously. Skip
1281 * copying for the last iteration since it is not needed. */
1282 if (iteration != iterations_range.last()) {
1283 parallel_for(size, [&](const int2 texel) {
1284 input.store_pixel(texel, output.load_pixel<Color>(texel));
1285 });
1286 }
1287 }
1288
1289 input.release();
1290 return output;
1291 }
1292
1293 void accumulate_streak(const Result &streak_result, Result &accumulated_streaks_result)
1294 {
1295 if (this->context().use_gpu()) {
1296 this->accumulate_streak_gpu(streak_result, accumulated_streaks_result);
1297 }
1298 else {
1299 this->accumulate_streak_cpu(streak_result, accumulated_streaks_result);
1300 }
1301 }
1302
1303 void accumulate_streak_gpu(const Result &streak_result, Result &accumulated_streaks_result)
1304 {
1305 gpu::Shader *shader = this->context().get_shader("compositor_glare_streaks_accumulate");
1307
1308 const float attenuation_factor = this->compute_streak_attenuation_factor();
1309 GPU_shader_uniform_1f(shader, "attenuation_factor", attenuation_factor);
1310
1311 streak_result.bind_as_texture(shader, "streak_tx");
1312 accumulated_streaks_result.bind_as_image(shader, "accumulated_streaks_img", true);
1313
1315
1316 streak_result.unbind_as_texture();
1317 accumulated_streaks_result.unbind_as_image();
1319 }
1320
1321 void accumulate_streak_cpu(const Result &streak, Result &accumulated_streaks)
1322 {
1323 const float attenuation_factor = this->compute_streak_attenuation_factor();
1324
1325 const int2 size = streak.domain().data_size;
1326 parallel_for(size, [&](const int2 texel) {
1327 float4 attenuated_streak = float4(streak.load_pixel<Color>(texel)) * attenuation_factor;
1328 float4 current_accumulated_streaks = float4(accumulated_streaks.load_pixel<Color>(texel));
1329 float4 combined_streaks = current_accumulated_streaks + attenuated_streak;
1330 accumulated_streaks.store_pixel(texel, Color(float4(combined_streaks.xyz(), 1.0f)));
1331 });
1332 }
1333
1334 /* As the number of iterations increase, the streaks spread farther and their intensity decrease.
1335 * To maintain similar intensities regardless of the number of iterations, streaks with lower
1336 * number of iteration are linearly attenuated. When the number of iterations is maximum, we need
1337 * not attenuate, so the denominator should be one, and when the number of iterations is one, we
1338 * need the attenuation to be maximum. This can be modeled as a simple decreasing linear equation
1339 * by substituting the two aforementioned cases. */
1341 {
1342 return 1.0f / (MAX_GLARE_ITERATIONS + 1 - get_number_of_iterations());
1343 }
1344
1345 /* Given the index of the streak in the [0, Number Of Streaks - 1] range, compute the unit
1346 * direction vector defining the streak. The streak directions should make angles with the x-axis
1347 * that are equally spaced and covers the whole two pi range, starting with the user supplied
1348 * angle. */
1350 {
1351 const int number_of_streaks = get_number_of_streaks();
1352 const float start_angle = this->get_streaks_angle();
1353 const float angle = start_angle + (float(streak_index) / number_of_streaks) * (M_PI * 2.0f);
1355 }
1356
1357 /* Different color channels of the streaks can be modulated by being multiplied by the color
1358 * modulator computed by this method. The color modulation is expected to be maximum when the
1359 * modulation factor is 1 and non existent when it is zero. But since the color modulator is
1360 * multiplied to the channel and the multiplicative identity is 1, we invert the modulation
1361 * factor. Moreover, color modulation should be less visible on higher iterations because they
1362 * produce the farther more faded away parts of the streaks. To achieve that, the modulation
1363 * factor is raised to the power of the iteration, noting that the modulation value is in the
1364 * [0, 1] range so the higher the iteration the lower the resulting modulation factor. The plus
1365 * one makes sure the power starts at one. */
1367 {
1368 return 1.0f - std::pow(this->get_color_modulation(), iteration + 1);
1369 }
1370
1371 /* Streaks are computed by iteratively applying a filter that samples 3 neighboring pixels in the
1372 * direction of the streak. Those neighboring pixels are then combined using a weighted sum. The
1373 * weights of the neighbors are the fade factors computed by this method. Farther neighbors are
1374 * expected to have lower weights because they contribute less to the combined result. Since the
1375 * iteration magnitude represents how far the neighbors are, as noted in the description of the
1376 * compute_streak_iteration_magnitude method, the fade factor for the closest neighbor is
1377 * computed as the user supplied fade parameter raised to the power of the magnitude, noting that
1378 * the fade value is in the [0, 1] range while the magnitude is larger than or equal one, so the
1379 * higher the power the lower the resulting fade factor. Furthermore, the other two neighbors are
1380 * just squared and cubed versions of the fade factor for the closest neighbor to get even lower
1381 * fade factors for those farther neighbors. */
1382 float3 compute_streak_fade_factors(float iteration_magnitude)
1383 {
1384 const float fade_factor = std::pow(this->get_fade(), iteration_magnitude);
1385 return float3(fade_factor, std::pow(fade_factor, 2.0f), std::pow(fade_factor, 3.0f));
1386 }
1387
1388 /* Streaks are computed by iteratively applying a filter that samples the neighboring pixels in
1389 * the direction of the streak. Each higher iteration samples pixels that are farther away, the
1390 * magnitude computed by this method describes how farther away the neighbors are sampled. The
1391 * magnitude exponentially increase with the iteration. A base of 4, was chosen as compromise
1392 * between better quality and performance, since a lower base corresponds to more tightly spaced
1393 * neighbors but would require more iterations to produce a streak of the same length. */
1395 {
1396 return std::pow(4.0f, iteration);
1397 }
1398
1400 {
1401 return math::clamp(this->get_input("Streaks").get_single_value_default<int>(), 1, 16);
1402 }
1403
1405 {
1406 return this->get_input("Streaks Angle").get_single_value_default<float>();
1407 }
1408
1409 /* ------------
1410 * Ghost Glare.
1411 * ------------ */
1412
1413 Result execute_ghost(const Result &highlights)
1414 {
1415 Result base_ghost_result = compute_base_ghost(highlights);
1416 Result accumulated_ghosts_result = context().create_result(ResultType::Color);
1417 if (this->context().use_gpu()) {
1418 this->accumulate_ghosts_gpu(base_ghost_result, accumulated_ghosts_result);
1419 }
1420 else {
1421 this->accumulate_ghosts_cpu(base_ghost_result, accumulated_ghosts_result);
1422 }
1423
1424 base_ghost_result.release();
1425 return accumulated_ghosts_result;
1426 }
1427
1428 void accumulate_ghosts_gpu(const Result &base_ghost_result, Result &accumulated_ghosts_result)
1429 {
1430 gpu::Shader *shader = context().get_shader("compositor_glare_ghost_accumulate");
1432
1433 /* Color modulators are constant across iterations. */
1434 std::array<float4, 4> color_modulators = compute_ghost_color_modulators();
1436 "color_modulators",
1437 color_modulators.size(),
1438 (const float (*)[4])color_modulators.data());
1439
1440 /* Zero initialize output image where ghosts will be accumulated. */
1441 const float4 zero_color = float4(0.0f);
1442 const int2 size = base_ghost_result.domain().data_size;
1443 accumulated_ghosts_result.allocate_texture(size);
1444 GPU_texture_clear(accumulated_ghosts_result, GPU_DATA_FLOAT, zero_color);
1445
1446 /* Copy the highlights result into a new result because the output will be copied to the input
1447 * after each iteration. */
1448 Result input_ghost_result = context().create_result(ResultType::Color);
1449 input_ghost_result.allocate_texture(size);
1450 GPU_texture_copy(input_ghost_result, base_ghost_result);
1451
1452 /* For the given number of iterations, accumulate four ghosts with different scales and color
1453 * modulators. The result of the previous iteration is used as the input of the current
1454 * iteration. We start from index 1 because we are not interested in the scales produced for
1455 * the first iteration according to visual judgment, see the compute_ghost_scales method. */
1456 const IndexRange iterations_range = IndexRange(get_number_of_iterations()).drop_front(1);
1457 for (const int i : iterations_range) {
1458 std::array<float, 4> scales = compute_ghost_scales(i);
1459 GPU_shader_uniform_4fv(shader, "scales", scales.data());
1460
1461 input_ghost_result.bind_as_texture(shader, "input_ghost_tx");
1462 accumulated_ghosts_result.bind_as_image(shader, "accumulated_ghost_img", true);
1463
1465
1466 input_ghost_result.unbind_as_texture();
1467 accumulated_ghosts_result.unbind_as_image();
1468
1469 /* The accumulated result serves as the input for the next iteration, so copy the result to
1470 * the input result since it can't be used for reading and writing simultaneously. Skip
1471 * copying for the last iteration since it is not needed. */
1472 if (i != iterations_range.last()) {
1474 GPU_texture_copy(input_ghost_result, accumulated_ghosts_result);
1475 }
1476 }
1477
1479 input_ghost_result.release();
1480 }
1481
1482 void accumulate_ghosts_cpu(const Result &base_ghost, Result &accumulated_ghosts_result)
1483 {
1484 /* Color modulators are constant across iterations. */
1485 std::array<float4, 4> color_modulators = this->compute_ghost_color_modulators();
1486
1487 /* Zero initialize output image where ghosts will be accumulated. */
1488 const int2 size = base_ghost.domain().data_size;
1489 accumulated_ghosts_result.allocate_texture(size);
1490 parallel_for(size, [&](const int2 texel) {
1491 accumulated_ghosts_result.store_pixel(texel, Color(float4(0.0f)));
1492 });
1493
1494 /* Copy the highlights result into a new result because the output will be copied to the input
1495 * after each iteration. */
1496 Result input = context().create_result(ResultType::Color);
1497 input.allocate_texture(size);
1498 parallel_for(size, [&](const int2 texel) {
1499 input.store_pixel(texel, base_ghost.load_pixel<Color>(texel));
1500 });
1501
1502 /* For the given number of iterations, accumulate four ghosts with different scales and color
1503 * modulators. The result of the previous iteration is used as the input of the current
1504 * iteration. We start from index 1 because we are not interested in the scales produced for
1505 * the first iteration according to visual judgment, see the compute_ghost_scales method. */
1506 const IndexRange iterations_range = IndexRange(this->get_number_of_iterations()).drop_front(1);
1507 for (const int i : iterations_range) {
1508 std::array<float, 4> scales = compute_ghost_scales(i);
1509
1510 parallel_for(size, [&](const int2 texel) {
1511 /* Add 0.5 to evaluate the input sampler at the center of the pixel and divide by the image
1512 * size to get the coordinates into the sampler's expected [0, 1] range. */
1513 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1514
1515 /* We accumulate four variants of the input ghost texture, each is scaled by some amount
1516 * and possibly multiplied by some color as a form of color modulation. */
1517 float4 accumulated_ghost = float4(0.0f);
1518 for (int i = 0; i < 4; i++) {
1519 float scale = scales[i];
1520 float4 color_modulator = color_modulators[i];
1521
1522 /* Scale the coordinates for the ghost, pre subtract 0.5 and post add 0.5 to use 0.5 as
1523 * the origin of the scaling. */
1524 float2 scaled_coordinates = (coordinates - 0.5f) * scale + 0.5f;
1525
1526 /* The value of the ghost is attenuated by a scalar multiple of the inverse distance to
1527 * the center, such that it is maximum at the center and become zero further from the
1528 * center, making sure to take the scale into account. The scalar multiple of 1 / 4 is
1529 * chosen using visual judgment. */
1530 float distance_to_center = math::distance(coordinates, float2(0.5f)) * 2.0f;
1531 float attenuator = math::max(0.0f, 1.0f - distance_to_center * math::abs(scale)) / 4.0f;
1532
1533 /* Accumulate the scaled ghost after attenuating and color modulating its value. */
1534 float4 multiplier = attenuator * color_modulator;
1535 accumulated_ghost += float4(input.sample_bilinear_zero<Color>(scaled_coordinates)) *
1536 multiplier;
1537 }
1538
1539 float4 current_accumulated_ghost = float4(
1540 accumulated_ghosts_result.load_pixel<Color>(texel));
1541 float4 combined_ghost = current_accumulated_ghost + accumulated_ghost;
1542 accumulated_ghosts_result.store_pixel(texel, Color(float4(combined_ghost.xyz(), 1.0f)));
1543 });
1544
1545 /* The accumulated result serves as the input for the next iteration, so copy the result to
1546 * the input result since it can't be used for reading and writing simultaneously. Skip
1547 * copying for the last iteration since it is not needed. */
1548 if (i != iterations_range.last()) {
1549 parallel_for(size, [&](const int2 texel) {
1550 input.store_pixel(texel, accumulated_ghosts_result.load_pixel<Color>(texel));
1551 });
1552 }
1553 }
1554
1555 input.release();
1556 }
1557
1558 /* Computes two ghosts by blurring the highlights with two different radii, then adds them into a
1559 * single base ghost image after scaling them by some factor and flipping the bigger ghost along
1560 * the center of the image. */
1562 {
1563 Result small_ghost_result = context().create_result(ResultType::Color);
1565 highlights,
1566 small_ghost_result,
1569
1570 Result big_ghost_result = context().create_result(ResultType::Color);
1572 highlights,
1573 big_ghost_result,
1576
1577 Result base_ghost_result = context().create_result(ResultType::Color);
1578 if (this->context().use_gpu()) {
1579 this->compute_base_ghost_gpu(small_ghost_result, big_ghost_result, base_ghost_result);
1580 }
1581 else {
1582 this->compute_base_ghost_cpu(small_ghost_result, big_ghost_result, base_ghost_result);
1583 }
1584
1585 small_ghost_result.release();
1586 big_ghost_result.release();
1587
1588 return base_ghost_result;
1589 }
1590
1591 void compute_base_ghost_gpu(const Result &small_ghost_result,
1592 const Result &big_ghost_result,
1593 Result &base_ghost_result)
1594 {
1595 gpu::Shader *shader = context().get_shader("compositor_glare_ghost_base");
1597
1598 GPU_texture_filter_mode(small_ghost_result, true);
1600 small_ghost_result.bind_as_texture(shader, "small_ghost_tx");
1601
1602 GPU_texture_filter_mode(big_ghost_result, true);
1604 big_ghost_result.bind_as_texture(shader, "big_ghost_tx");
1605
1606 base_ghost_result.allocate_texture(small_ghost_result.domain());
1607 base_ghost_result.bind_as_image(shader, "combined_ghost_img");
1608
1610
1612 small_ghost_result.unbind_as_texture();
1613 big_ghost_result.unbind_as_texture();
1614 base_ghost_result.unbind_as_image();
1615 }
1616
1617 void compute_base_ghost_cpu(const Result &small_ghost_result,
1618 const Result &big_ghost_result,
1619 Result &combined_ghost)
1620 {
1621 const int2 size = small_ghost_result.domain().data_size;
1622 combined_ghost.allocate_texture(size);
1623
1624 parallel_for(size, [&](const int2 texel) {
1625 /* Add 0.5 to evaluate the input sampler at the center of the pixel and divide by the image
1626 * size to get the coordinates into the sampler's expected [0, 1] range. */
1627 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1628
1629 /* The small ghost is scaled down with the origin as the center of the image by a factor
1630 * of 2.13, while the big ghost is flipped and scaled up with the origin as the center of the
1631 * image by a factor of 0.97. Note that 1) The negative scale implements the flipping. 2)
1632 * Factors larger than 1 actually scales down the image since the factor multiplies the
1633 * coordinates and not the images itself. 3) The values are arbitrarily chosen using visual
1634 * judgment. */
1635 float small_ghost_scale = 2.13f;
1636 float big_ghost_scale = -0.97f;
1637
1638 /* Scale the coordinates for the small and big ghosts, pre subtract 0.5 and post add 0.5 to
1639 * use 0.5 as the origin of the scaling. Notice that the big ghost is flipped due to the
1640 * negative scale. */
1641 float2 small_ghost_coordinates = (coordinates - 0.5f) * small_ghost_scale + 0.5f;
1642 float2 big_ghost_coordinates = (coordinates - 0.5f) * big_ghost_scale + 0.5f;
1643
1644 /* The values of the ghosts are attenuated by the inverse distance to the center, such that
1645 * they are maximum at the center and become zero further from the center, making sure to
1646 * take the aforementioned scale into account. */
1647 float distance_to_center = math::distance(coordinates, float2(0.5f)) * 2.0f;
1648 float small_ghost_attenuator = math::max(0.0f,
1649 1.0f - distance_to_center * small_ghost_scale);
1650 float big_ghost_attenuator = math::max(
1651 0.0f, 1.0f - distance_to_center * math::abs(big_ghost_scale));
1652
1653 float4 small_ghost = float4(small_ghost_result.sample_bilinear_zero<Color>(
1654 small_ghost_coordinates)) *
1655 small_ghost_attenuator;
1656 float4 big_ghost = float4(
1657 big_ghost_result.sample_bilinear_zero<Color>(big_ghost_coordinates)) *
1658 big_ghost_attenuator;
1659
1660 combined_ghost.store_pixel(texel, Color(small_ghost + big_ghost));
1661 });
1662 }
1663
1664 /* In each iteration of ghost accumulation, four ghosts are accumulated, each of which might be
1665 * modulated by multiplying by some color modulator, this function generates a color modulator
1666 * for each of the four ghosts. The first ghost is always unmodulated, so is the multiplicative
1667 * identity of 1. The second ghost gets only its green and blue channels modulated, the third
1668 * ghost gets only its red and green channels modulated, and the fourth ghost gets only its red
1669 * and blue channels modulated. */
1670 std::array<float4, 4> compute_ghost_color_modulators()
1671 {
1672 const float color_modulation_factor = get_ghost_color_modulation_factor();
1673
1674 std::array<float4, 4> color_modulators;
1675 color_modulators[0] = float4(1.0f);
1676 color_modulators[1] = float4(1.0f, color_modulation_factor, color_modulation_factor, 1.0f);
1677 color_modulators[2] = float4(color_modulation_factor, color_modulation_factor, 1.0f, 1.0f);
1678 color_modulators[3] = float4(color_modulation_factor, 1.0f, color_modulation_factor, 1.0f);
1679
1680 return color_modulators;
1681 }
1682
1683 /* In each iteration of ghost accumulation, four ghosts with different scales are accumulated.
1684 * Given the index of a certain iteration, this method computes the 4 scales for it. Assuming we
1685 * have n number of iterations, that means the total number of accumulations is 4 * n. To get a
1686 * variety of scales, we generate an arithmetic progression that starts from 2.1 and ends at zero
1687 * exclusive, containing 4 * n elements. The start scale of 2.1 is chosen arbitrarily using
1688 * visual judgment. To get more scale variations, every other scale is inverted with a slight
1689 * change in scale such that it alternates between scaling down and up, additionally every other
1690 * ghost is flipped across the image center by negating its scale. Finally, to get variations
1691 * across the number of iterations, a shift of 0.5 is introduced when the number of iterations is
1692 * odd, that way, the user will get variations when changing the number of iterations as opposed
1693 * to just getting less or more ghosts. */
1694 std::array<float, 4> compute_ghost_scales(int iteration)
1695 {
1696 /* Shift scales by 0.5 for odd number of iterations as discussed in the method description.
1697 */
1698 const float offset = (get_number_of_iterations() % 2 == 1) ? 0.5f : 0.0f;
1699
1700 std::array<float, 4> scales;
1701 for (const int i : IndexRange(scales.size())) {
1702 /* Global index in all accumulations. */
1703 const int global_i = iteration * 4 + i;
1704 /* Arithmetic progression in the range [0, 1) + offset. */
1705 const float progression = (global_i + offset) / (get_number_of_iterations() * 4);
1706 /* Remap range [0, 1) to [1, 0) and multiply to remap to [2.1, 0). */
1707 scales[i] = 2.1f * (1.0f - progression);
1708
1709 /* Invert the scale with a slight variation and flip it across the image center through
1710 * negation for odd scales as discussed in the method description. */
1711 if (i % 2 == 1) {
1712 scales[i] = -0.99f / scales[i];
1713 }
1714 }
1715
1716 return scales;
1717 }
1718
1719 /* The operation computes two base ghosts by blurring the highlights with two different radii,
1720 * this method computes the blur radius for the smaller one. The value is chosen using visual
1721 * judgment. Make sure to take the quality factor into account, see the get_quality_factor method
1722 * for more information. */
1724 {
1725 return 16.0f / get_quality_factor();
1726 }
1727
1728 /* Computes the blur radius of the bigger ghost, which is double the blur radius if the smaller
1729 * one, see the get_small_ghost_radius for more information. */
1731 {
1732 return get_small_ghost_radius() * 2.0f;
1733 }
1734
1735 /* The color channels of the glare can be modulated by being multiplied by this factor. In the
1736 * user interface, 0 means no modulation and 1 means full modulation. But since the factor is
1737 * multiplied, 1 corresponds to no modulation and 0 corresponds to full modulation, so we
1738 * subtract from one. */
1740 {
1741 return 1.0f - this->get_color_modulation();
1742 }
1743
1744 /* ------------
1745 * Bloom Glare.
1746 * ------------ */
1747
1748 /* Bloom is computed by first progressively half-down-sampling the highlights down to a certain
1749 * size, then progressively double-up-sampling the last down-sampled result up to the original
1750 * size of the highlights, adding the down-sampled result of the same size in each up-sampling
1751 * step. This can be illustrated as follows:
1752 *
1753 * Highlights ---+---> Bloom
1754 * | |
1755 * Down-sampled ---+---> Up-sampled
1756 * | |
1757 * Down-sampled ---+---> Up-sampled
1758 * | |
1759 * Down-sampled ---+---> Up-sampled
1760 * | ^
1761 * ... |
1762 * Down-sampled ------------'
1763 *
1764 * The smooth down-sampling followed by smooth up-sampling can be thought of as a cheap way to
1765 * approximate a large radius blur, and adding the corresponding down-sampled result while
1766 * up-sampling is done to counter the attenuation that happens during down-sampling.
1767 *
1768 * Smaller down-sampled results contribute to larger glare size, so controlling the size can be
1769 * done by stopping down-sampling down to a certain size, where the maximum possible size is
1770 * achieved when down-sampling happens down to the smallest size of 2. */
1772 {
1773 const int chain_length = this->compute_bloom_chain_length();
1774
1775 /* If the chain length is less than 2, that means no down-sampling will happen, so we just
1776 * return a copy of the highlights. This is a sanitization of a corner case, so no need to
1777 * worry about optimizing the copy away. */
1778 if (chain_length < 2) {
1779 Result bloom_result = context().create_result(ResultType::Color);
1780 bloom_result.allocate_texture(highlights.domain());
1781 if (this->context().use_gpu()) {
1782 GPU_texture_copy(bloom_result, highlights);
1783 }
1784 else {
1785 parallel_for(bloom_result.domain().data_size, [&](const int2 texel) {
1786 bloom_result.store_pixel(texel, highlights.load_pixel<Color>(texel));
1787 });
1788 }
1789 return bloom_result;
1790 }
1791
1792 Vector<Result> downsample_chain = compute_bloom_downsample_chain(highlights, chain_length);
1793
1794 /* Notice that for a chain length of n, we need (n - 1) up-sampling passes. */
1795 const IndexRange upsample_passes_range(chain_length - 1);
1796
1797 for (const int i : upsample_passes_range) {
1798 Result &input = downsample_chain[upsample_passes_range.last() - i + 1];
1799 Result &output = downsample_chain[upsample_passes_range.last() - i];
1800 if (this->context().use_gpu()) {
1801 this->compute_bloom_upsample_gpu(input, output);
1802 }
1803 else {
1804 this->compute_bloom_upsample_cpu(input, output);
1805 }
1806 input.release();
1807 }
1808
1809 Result bloom_output = this->context().create_result(ResultType::Color);
1810 bloom_output.share_data(downsample_chain[0]);
1811 downsample_chain[0].release();
1812 return bloom_output;
1813 }
1814
1816 {
1817 gpu::Shader *shader = context().get_shader("compositor_glare_bloom_upsample");
1819
1820 GPU_texture_filter_mode(input, true);
1821 input.bind_as_texture(shader, "input_tx");
1822
1823 output.bind_as_image(shader, "output_img", true);
1824
1825 compute_dispatch_threads_at_least(shader, output.domain().data_size);
1826
1827 input.unbind_as_texture();
1828 output.unbind_as_image();
1830 }
1831
1833 {
1834 /* Each invocation corresponds to one output pixel, where the output has twice the size of the
1835 * input. */
1836 const int2 size = output.domain().data_size;
1837 parallel_for(size, [&](const int2 texel) {
1838 /* Add 0.5 to evaluate the sampler at the center of the pixel and divide by the image size to
1839 * get the coordinates into the sampler's expected [0, 1] range. */
1840 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1841
1842 /* All the offsets in the following code section are in the normalized pixel space of the
1843 * output image, so compute its normalized pixel size. */
1844 float2 pixel_size = 1.0f / float2(size);
1845
1846 /* Upsample by applying a 3x3 tent filter on the bi-linearly interpolated values evaluated at
1847 * the center of neighboring output pixels. As more tent filter upsampling passes are
1848 * applied, the result approximates a large sized Gaussian filter. This upsampling strategy
1849 * is described in the talk:
1850 *
1851 * Next Generation Post Processing in Call of Duty: Advanced Warfare
1852 * https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
1853 *
1854 * In particular, the upsampling strategy is described and illustrated in slide 162 titled
1855 * "Upsampling - Our Solution". */
1856 float4 upsampled = float4(0.0f);
1857 upsampled += (4.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(coordinates));
1858 upsampled += (2.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1859 coordinates + pixel_size * float2(-1.0f, 0.0f)));
1860 upsampled += (2.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1861 coordinates + pixel_size * float2(0.0f, 1.0f)));
1862 upsampled += (2.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1863 coordinates + pixel_size * float2(1.0f, 0.0f)));
1864 upsampled += (2.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1865 coordinates + pixel_size * float2(0.0f, -1.0f)));
1866 upsampled += (1.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1867 coordinates + pixel_size * float2(-1.0f, -1.0f)));
1868 upsampled += (1.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1869 coordinates + pixel_size * float2(-1.0f, 1.0f)));
1870 upsampled += (1.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1871 coordinates + pixel_size * float2(1.0f, -1.0f)));
1872 upsampled += (1.0f / 16.0f) * float4(input.sample_bilinear_extended<Color>(
1873 coordinates + pixel_size * float2(1.0f, 1.0f)));
1874
1875 float4 combined = float4(output.load_pixel<Color>(texel)) + upsampled;
1876 output.store_pixel(texel, Color(float4(combined.xyz(), 1.0f)));
1877 });
1878 }
1879
1880 /* Progressively down-sample the given result into a result with half the size for the given
1881 * chain length, returning an array containing the chain of down-sampled results. The first
1882 * result of the chain is the given result itself for easier handling. The chain length is
1883 * expected not to exceed the binary logarithm of the smaller dimension of the given result,
1884 * because that would result in down-sampling passes that produce useless textures with just
1885 * one pixel. */
1886 Vector<Result> compute_bloom_downsample_chain(const Result &highlights, int chain_length)
1887 {
1888 Vector<Result> downsample_chain;
1889 downsample_chain.reserve(chain_length);
1890 for (int i = 0; i < chain_length; i++) {
1891 downsample_chain.append(this->context().create_result(ResultType::Color));
1892 }
1893
1894 /* We copy the original highlights result to the first result of the chain to make the code
1895 * easier. */
1896 Result &base_layer = downsample_chain[0];
1897 base_layer.allocate_texture(highlights.domain());
1898 if (this->context().use_gpu()) {
1899 GPU_texture_copy(base_layer, highlights);
1900 }
1901 else {
1902 parallel_for(base_layer.domain().data_size, [&](const int2 texel) {
1903 base_layer.store_pixel(texel, highlights.load_pixel<Color>(texel));
1904 });
1905 }
1906
1907 /* In turn, the number of passes is one less than the chain length, because the first result
1908 * needn't be computed. */
1909 const IndexRange downsample_passes_range(chain_length - 1);
1910
1911 for (const int i : downsample_passes_range) {
1912 /* For the first down-sample pass, we use a special "Karis" down-sample pass that applies a
1913 * form of local tone mapping to reduce the contributions of fireflies, see the shader for
1914 * more information. Later passes use a simple average down-sampling filter because
1915 * fireflies doesn't service the first pass. */
1916 const bool use_karis_average = i == downsample_passes_range.first();
1917 if (this->context().use_gpu()) {
1919 downsample_chain[i], downsample_chain[i + 1], use_karis_average);
1920 }
1921 else {
1922 if (use_karis_average) {
1923 this->compute_bloom_downsample_cpu<true>(downsample_chain[i], downsample_chain[i + 1]);
1924 }
1925 else {
1926 this->compute_bloom_downsample_cpu<false>(downsample_chain[i], downsample_chain[i + 1]);
1927 }
1928 }
1929 }
1930
1931 return downsample_chain;
1932 }
1933
1935 Result &output,
1936 const bool use_karis_average)
1937 {
1938 gpu::Shader *shader = context().get_shader(
1939 use_karis_average ? "compositor_glare_bloom_downsample_karis_average" :
1940 "compositor_glare_bloom_downsample_simple_average");
1942
1943 GPU_texture_filter_mode(input, true);
1944 input.bind_as_texture(shader, "input_tx");
1945
1946 output.allocate_texture(input.domain().data_size / 2);
1947 output.bind_as_image(shader, "output_img");
1948
1949 compute_dispatch_threads_at_least(shader, output.domain().data_size);
1950
1951 input.unbind_as_texture();
1952 output.unbind_as_image();
1954 }
1955
1956 template<bool UseKarisAverage>
1958 {
1959 const int2 size = input.domain().data_size / 2;
1960 output.allocate_texture(size);
1961
1962 /* Each invocation corresponds to one output pixel, where the output has half the size of the
1963 * input. */
1964 parallel_for(size, [&](const int2 texel) {
1965 /* Add 0.5 to evaluate the sampler at the center of the pixel and divide by the image size to
1966 * get the coordinates into the sampler's expected [0, 1] range. */
1967 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1968
1969 /* All the offsets in the following code section are in the normalized pixel space of the
1970 * input texture, so compute its normalized pixel size. */
1971 float2 pixel_size = 1.0f / float2(input.domain().data_size);
1972
1973 /* Each invocation downsamples a 6x6 area of pixels around the center of the corresponding
1974 * output pixel, but instead of sampling each of the 36 pixels in the area, we only sample 13
1975 * positions using bilinear fetches at the center of a number of overlapping square 4-pixel
1976 * groups. This downsampling strategy is described in the talk:
1977 *
1978 * Next Generation Post Processing in Call of Duty: Advanced Warfare
1979 * https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
1980 *
1981 * In particular, the downsampling strategy is described and illustrated in slide 153 titled
1982 * "Downsampling - Our Solution". This is employed as it significantly improves the stability
1983 * of the glare as can be seen in the videos in the talk. */
1984 float4 center = float4(input.sample_bilinear_extended<Color>(coordinates));
1985 float4 upper_left_near = float4(
1986 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(-1.0f, 1.0f)));
1987 float4 upper_right_near = float4(
1988 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(1.0f, 1.0f)));
1989 float4 lower_left_near = float4(
1990 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(-1.0f, -1.0f)));
1991 float4 lower_right_near = float4(
1992 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(1.0f, -1.0f)));
1993 float4 left_far = float4(
1994 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(-2.0f, 0.0f)));
1995 float4 right_far = float4(
1996 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(2.0f, 0.0f)));
1997 float4 upper_far = float4(
1998 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(0.0f, 2.0f)));
1999 float4 lower_far = float4(
2000 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(0.0f, -2.0f)));
2001 float4 upper_left_far = float4(
2002 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(-2.0f, 2.0f)));
2003 float4 upper_right_far = float4(
2004 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(2.0f, 2.0f)));
2005 float4 lower_left_far = float4(
2006 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(-2.0f, -2.0f)));
2007 float4 lower_right_far = float4(
2008 input.sample_bilinear_extended<Color>(coordinates + pixel_size * float2(2.0f, -2.0f)));
2009
2010 float4 result;
2011 if constexpr (!UseKarisAverage) {
2012 /* The original weights equation mentioned in slide 153 is:
2013 * 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1
2014 * The 0.5 corresponds to the center group of pixels and the 0.125 corresponds to the other
2015 * groups of pixels. The center is sampled 4 times, the far non corner pixels are sampled 2
2016 * times, the near corner pixels are sampled only once; but their weight is quadruple the
2017 * weights of other groups; so they count as sampled 4 times, finally the far corner pixels
2018 * are sampled only once, essentially totaling 32 samples. So the weights are as used in
2019 * the following code section. */
2020 result = (4.0f / 32.0f) * center +
2021 (4.0f / 32.0f) *
2022 (upper_left_near + upper_right_near + lower_left_near + lower_right_near) +
2023 (2.0f / 32.0f) * (left_far + right_far + upper_far + lower_far) +
2024 (1.0f / 32.0f) *
2025 (upper_left_far + upper_right_far + lower_left_far + lower_right_far);
2026 }
2027 else {
2028 /* Reduce the contributions of fireflies on the result by reducing each group of pixels
2029 * using a Karis brightness weighted sum. This is described in slide 168 titled "Fireflies
2030 * - Partial Karis Average".
2031 *
2032 * This needn't be done on all downsampling passes, but only the first one, since fireflies
2033 * will not survive the first pass, later passes can use the weighted average. */
2034 float4 center_weighted_sum = this->karis_brightness_weighted_sum(
2035 upper_left_near, upper_right_near, lower_right_near, lower_left_near);
2036 float4 upper_left_weighted_sum = this->karis_brightness_weighted_sum(
2037 upper_left_far, upper_far, center, left_far);
2038 float4 upper_right_weighted_sum = this->karis_brightness_weighted_sum(
2039 upper_far, upper_right_far, right_far, center);
2040 float4 lower_right_weighted_sum = this->karis_brightness_weighted_sum(
2041 center, right_far, lower_right_far, lower_far);
2042 float4 lower_left_weighted_sum = this->karis_brightness_weighted_sum(
2043 left_far, center, lower_far, lower_left_far);
2044
2045 /* The original weights equation mentioned in slide 153 is:
2046 * 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1
2047 * Multiply both sides by 8 and you get:
2048 * 4 + 1 + 1 + 1 + 1 = 8
2049 * So the weights are as used in the following code section. */
2050 result = (4.0f / 8.0f) * center_weighted_sum +
2051 (1.0f / 8.0f) * (upper_left_weighted_sum + upper_right_weighted_sum +
2052 lower_left_weighted_sum + lower_right_weighted_sum);
2053 }
2054
2055 output.store_pixel(texel, Color(result));
2056 });
2057 }
2058
2059 /* Computes the weighted average of the given four colors, which are assumed to the colors of
2060 * spatially neighboring pixels. The weights are computed so as to reduce the contributions of
2061 * fireflies on the result by applying a form of local tone mapping as described by Brian Karis
2062 * in the article "Graphic Rants: Tone Mapping".
2063 *
2064 * https://graphicrants.blogspot.com/2013/12/tone-mapping.html */
2066 const float4 &color2,
2067 const float4 &color3,
2068 const float4 &color4)
2069 {
2070 float4 brightness = float4(math::reduce_max(color1.xyz()),
2071 math::reduce_max(color2.xyz()),
2072 math::reduce_max(color3.xyz()),
2073 math::reduce_max(color4.xyz()));
2074 float4 weights = 1.0f / (brightness + 1.0f);
2075 return (color1 * weights.x + color2 * weights.y + color3 * weights.z + color4 * weights.w) *
2077 }
2078
2079 /* The maximum possible glare size is achieved when we down-sampled down to the smallest size of
2080 * 2, which would result in a down-sampling chain length of the binary logarithm of the smaller
2081 * dimension of the size of the highlights.
2082 *
2083 * However, as users might want a smaller glare size, we reduce the chain length by the size
2084 * supplied by the user. Also make sure that log2 does not get zero. */
2086 {
2087 const int2 image_size = this->get_glare_image_domain().data_size;
2088 const int smaller_dimension = math::reduce_min(image_size);
2089 const float scaled_dimension = smaller_dimension * this->get_size();
2090 return int(std::log2(math::max(1.0f, scaled_dimension)));
2091 }
2092
2093 /* ---------------
2094 * Fog Glow Glare.
2095 * --------------- */
2096
2097 Result execute_fog_glow(const Result &highlights)
2098 {
2099#if defined(WITH_FFTW3)
2100
2101 const int kernel_size = int(math::reduce_max(highlights.domain().data_size));
2102
2103 /* Since we will be doing a circular convolution, we need to zero pad our input image by
2104 * the kernel size to avoid the kernel affecting the pixels at the other side of image.
2105 * Therefore, zero boundary is assumed. */
2106 const int needed_padding_amount = kernel_size;
2107 const int2 image_size = highlights.domain().data_size;
2108 const int2 needed_spatial_size = image_size + needed_padding_amount - 1;
2109 const int2 spatial_size = fftw::optimal_size_for_real_transform(needed_spatial_size);
2110
2111 /* The FFTW real to complex transforms utilizes the hermitian symmetry of real transforms and
2112 * stores only half the output since the other half is redundant, so we only allocate half of
2113 * the first dimension. See Section 4.3.4 Real-data DFT Array Format in the FFTW manual for
2114 * more information. */
2115 const int2 frequency_size = int2(spatial_size.x / 2 + 1, spatial_size.y);
2116
2117 /* We only process the color channels, the alpha channel is written to the output as is. */
2118 const int channels_count = 3;
2119 const int image_channels_count = 4;
2120 const int64_t spatial_pixels_per_channel = int64_t(spatial_size.x) * spatial_size.y;
2121 const int64_t frequency_pixels_per_channel = int64_t(frequency_size.x) * frequency_size.y;
2122 const int64_t spatial_pixels_count = spatial_pixels_per_channel * channels_count;
2123 const int64_t frequency_pixels_count = frequency_pixels_per_channel * channels_count;
2124
2125 float *image_spatial_domain = fftwf_alloc_real(spatial_pixels_count);
2126 std::complex<float> *image_frequency_domain = reinterpret_cast<std::complex<float> *>(
2127 fftwf_alloc_complex(frequency_pixels_count));
2128
2129 /* Create a real to complex plan to transform the image to the frequency domain. */
2130 fftwf_plan forward_plan = fftwf_plan_dft_r2c_2d(
2131 spatial_size.y,
2132 spatial_size.x,
2133 image_spatial_domain,
2134 reinterpret_cast<fftwf_complex *>(image_frequency_domain),
2135 FFTW_ESTIMATE);
2136
2137 const float *highlights_buffer = nullptr;
2138 if (this->context().use_gpu()) {
2140 highlights_buffer = static_cast<const float *>(
2141 GPU_texture_read(highlights, GPU_DATA_FLOAT, 0));
2142 }
2143 else {
2144 highlights_buffer = static_cast<const float *>(highlights.cpu_data().data());
2145 }
2146
2147 /* Zero pad the image to the required spatial domain size, storing each channel in planar
2148 * format for better cache locality, that is, RRRR...GGGG...BBBB. */
2149 threading::parallel_for(IndexRange(spatial_size.y), 1, [&](const IndexRange sub_y_range) {
2150 for (const int64_t y : sub_y_range) {
2151 for (const int64_t x : IndexRange(spatial_size.x)) {
2152 const bool is_inside_image = x < image_size.x && y < image_size.y;
2153 for (const int64_t channel : IndexRange(channels_count)) {
2154 const int64_t base_index = y * spatial_size.x + x;
2155 const int64_t output_index = base_index + spatial_pixels_per_channel * channel;
2156 if (is_inside_image) {
2157 const int64_t image_index = (y * image_size.x + x) * image_channels_count + channel;
2158 image_spatial_domain[output_index] = highlights_buffer[image_index];
2159 }
2160 else {
2161 image_spatial_domain[output_index] = 0.0f;
2162 }
2163 }
2164 }
2165 }
2166 });
2167
2168 threading::parallel_for(IndexRange(channels_count), 1, [&](const IndexRange sub_range) {
2169 for (const int64_t channel : sub_range) {
2170 fftwf_execute_dft_r2c(forward_plan,
2171 image_spatial_domain + spatial_pixels_per_channel * channel,
2172 reinterpret_cast<fftwf_complex *>(image_frequency_domain) +
2173 frequency_pixels_per_channel * channel);
2174 }
2175 });
2176
2177 const FogGlowKernel &fog_glow_kernel = context().cache_manager().fog_glow_kernels.get(
2178 kernel_size, spatial_size, this->compute_fog_glow_field_of_view());
2179
2180 /* Multiply the kernel and the image in the frequency domain to perform the convolution. The
2181 * FFT is not normalized, meaning the result of the FFT followed by an inverse FFT will result
2182 * in an image that is scaled by a factor of the product of the width and height, so we take
2183 * that into account by dividing by that scale. See Section 4.8.6 Multi-dimensional Transforms
2184 * of the FFTW manual for more information. */
2185 const float normalization_scale = float(spatial_size.x) * spatial_size.y *
2186 fog_glow_kernel.normalization_factor();
2187 threading::parallel_for(IndexRange(frequency_size.y), 1, [&](const IndexRange sub_y_range) {
2188 for (const int64_t channel : IndexRange(channels_count)) {
2189 for (const int64_t y : sub_y_range) {
2190 for (const int64_t x : IndexRange(frequency_size.x)) {
2191 const int64_t base_index = x + y * frequency_size.x;
2192 const int64_t output_index = base_index + frequency_pixels_per_channel * channel;
2193 const std::complex<float> kernel_value = fog_glow_kernel.frequencies()[base_index];
2194 image_frequency_domain[output_index] *= kernel_value / normalization_scale;
2195 }
2196 }
2197 }
2198 });
2199
2200 /* Create a complex to real plan to transform the image to the real domain. */
2201 fftwf_plan backward_plan = fftwf_plan_dft_c2r_2d(
2202 spatial_size.y,
2203 spatial_size.x,
2204 reinterpret_cast<fftwf_complex *>(image_frequency_domain),
2205 image_spatial_domain,
2206 FFTW_ESTIMATE);
2207
2208 threading::parallel_for(IndexRange(channels_count), 1, [&](const IndexRange sub_range) {
2209 for (const int64_t channel : sub_range) {
2210 fftwf_execute_dft_c2r(backward_plan,
2211 reinterpret_cast<fftwf_complex *>(image_frequency_domain) +
2212 frequency_pixels_per_channel * channel,
2213 image_spatial_domain + spatial_pixels_per_channel * channel);
2214 }
2215 });
2216
2217 Result fog_glow_result = context().create_result(ResultType::Color);
2218 fog_glow_result.allocate_texture(highlights.domain());
2219
2220 /* For GPU, write the output to the exist highlights_buffer then upload to the result after,
2221 * while for CPU, write to the result directly. */
2222 float *output = this->context().use_gpu() ?
2223 const_cast<float *>(highlights_buffer) :
2224 static_cast<float *>(fog_glow_result.cpu_data_for_write().data());
2225
2226 /* Copy the result to the output. */
2227 threading::parallel_for(IndexRange(image_size.y), 1, [&](const IndexRange sub_y_range) {
2228 for (const int64_t y : sub_y_range) {
2229 for (const int64_t x : IndexRange(image_size.x)) {
2230 for (const int64_t channel : IndexRange(channels_count)) {
2231 const int64_t output_index = (x + y * image_size.x) * image_channels_count;
2232 const int64_t base_index = x + y * spatial_size.x;
2233 const int64_t input_index = base_index + spatial_pixels_per_channel * channel;
2234 output[output_index + channel] = image_spatial_domain[input_index];
2235 output[output_index + 3] = highlights_buffer[output_index + 3];
2236 }
2237 }
2238 }
2239 });
2240
2241 if (this->context().use_gpu()) {
2242 GPU_texture_update(fog_glow_result, GPU_DATA_FLOAT, output);
2243 /* CPU writes to the output directly, so no need to free it. */
2244 MEM_delete(output);
2245 }
2246
2247 fftwf_destroy_plan(forward_plan);
2248 fftwf_destroy_plan(backward_plan);
2249 fftwf_free(image_spatial_domain);
2250 fftwf_free(image_frequency_domain);
2251#else
2252 Result fog_glow_result = context().create_result(ResultType::Color);
2253 fog_glow_result.allocate_texture(highlights.domain());
2254 if (this->context().use_gpu()) {
2255 GPU_texture_copy(fog_glow_result, highlights);
2256 }
2257 else {
2258 parallel_for(fog_glow_result.domain().data_size, [&](const int2 texel) {
2259 fog_glow_result.store_pixel(texel, highlights.load_pixel<float4>(texel));
2260 });
2261 }
2262#endif
2263
2264 return fog_glow_result;
2265 }
2266
2267 /* Computes the field of view of the glare based on the give size as per:
2268 *
2269 * Spencer, Greg, et al. "Physically-Based Glare Effects for Digital Images."
2270 * Proceedings of the 22nd Annual Conference on Computer Graphics and Interactive Techniques,
2271 * 1995.
2272 *
2273 * We choose a minimum field of view of 10 degrees using visual judgement on typical setups,
2274 * otherwise, a too small field of view would make the evaluation domain of the glare lie almost
2275 * entirely in the central Gaussian of the function, losing the exponential characteristic of the
2276 * function. Additionally, we take the power of the size with 1/3 to adjust the rate of change of
2277 * the size to make the apparent size of the glare more linear with respect to the size input. */
2279 {
2281 math::interpolate(180.0f, 10.0f, math::pow(this->get_size(), 1.0f / 3.0f)));
2282 }
2283
2284 /* ----------
2285 * Sun Beams.
2286 * ---------- */
2287
2289 {
2290 const int2 input_size = highlights.domain().data_size;
2291 const int max_steps = int(this->get_size() * math::length(input_size));
2292 if (max_steps == 0) {
2293 Result sun_beams_result = context().create_result(ResultType::Color);
2294 sun_beams_result.allocate_texture(highlights.domain());
2295 if (this->context().use_gpu()) {
2296 GPU_texture_copy(sun_beams_result, highlights);
2297 }
2298 else {
2299 parallel_for(sun_beams_result.domain().data_size, [&](const int2 texel) {
2300 sun_beams_result.store_pixel(texel, highlights.load_pixel<Color>(texel));
2301 });
2302 }
2303 return sun_beams_result;
2304 }
2305
2306 if (this->context().use_gpu()) {
2307 return this->execute_sun_beams_gpu(highlights, max_steps);
2308 }
2309 else {
2310 return this->execute_sun_beams_cpu(highlights, max_steps);
2311 }
2312 }
2313
2314 Result execute_sun_beams_gpu(Result &highlights, const int max_steps)
2315 {
2318
2320 GPU_shader_uniform_1f(shader, "jitter_factor", this->get_jitter_factor());
2321 GPU_shader_uniform_1i(shader, "max_steps", max_steps);
2322
2323 GPU_texture_filter_mode(highlights, true);
2325 highlights.bind_as_texture(shader, "input_tx");
2326
2327 Result output_image = context().create_result(ResultType::Color);
2328 const Domain domain = highlights.domain();
2329 output_image.allocate_texture(domain);
2330 output_image.bind_as_image(shader, "output_img");
2331
2333
2335 output_image.unbind_as_image();
2336 highlights.unbind_as_texture();
2337 return output_image;
2338 }
2339
2341 {
2342 if (this->get_use_jitter()) {
2343 return "compositor_glare_sun_beams_jitter";
2344 }
2345 return "compositor_glare_sun_beams";
2346 }
2347
2348 Result execute_sun_beams_cpu(Result &highlights, const int max_steps)
2349 {
2350 const float2 source = this->get_sun_position();
2351
2352 Result output = context().create_result(ResultType::Color);
2353 output.allocate_texture(highlights.domain());
2354
2355 const int2 input_size = highlights.domain().data_size;
2356 float jitter_factor = this->get_jitter_factor();
2357 bool use_jitter = this->get_use_jitter();
2358 parallel_for(input_size, [&](const int2 texel) {
2359 /* The number of steps is the distance in pixels from the source to the current texel. With
2360 * at least a single step and at most the user specified maximum ray length, which is
2361 * proportional to the diagonal pixel count. */
2362 float unbounded_steps = math::max(
2363 1.0f, math::distance(float2(texel), source * float2(input_size)));
2364 int steps = math::min(max_steps, int(unbounded_steps));
2365
2366 /* We integrate from the current pixel to the source pixel, so compute the start coordinates
2367 * and step vector in the direction to source. Notice that the step vector is still computed
2368 * from the unbounded steps, such that the total integration length becomes limited by the
2369 * bounded steps, and thus by the maximum ray length. */
2370 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(input_size);
2371 float2 vector_to_source = source - coordinates;
2372 float2 step_vector = vector_to_source / unbounded_steps;
2373
2374 float accumulated_weight = 0.0f;
2375 float4 accumulated_color = float4(0.0f);
2376
2377 int number_of_steps = (1.0f - jitter_factor) * steps;
2378 float random_offset = noise::hash_to_float(texel.x, texel.y);
2379
2380 for (int i = 0; i <= number_of_steps; i++) {
2381 float position_index = this->get_sample_position(
2382 i, use_jitter, jitter_factor, random_offset);
2383 float2 position = coordinates + position_index * step_vector;
2384
2385 /* We are already past the image boundaries, and any future steps are also past the image
2386 * boundaries, so break. */
2387 if (position.x < 0.0f || position.y < 0.0f || position.x > 1.0f || position.y > 1.0f) {
2388 break;
2389 }
2390
2391 float4 sample_color = float4(highlights.sample_bilinear_zero<Color>(position));
2392
2393 /* Attenuate the contributions of pixels that are further away from the source using a
2394 * quadratic falloff. */
2395 float weight = math::square(1.0f - position_index / float(steps));
2396
2397 accumulated_weight += weight;
2398 accumulated_color += sample_color * weight;
2399 }
2400
2401 if (accumulated_weight != 0.0f) {
2402 accumulated_color /= accumulated_weight;
2403 }
2404 else {
2405 accumulated_color = float4(highlights.sample_bilinear_zero<Color>(coordinates));
2406 }
2407 output.store_pixel(texel, Color(accumulated_color));
2408 });
2409 return output;
2410 }
2411
2412 /* Returns an index for a position along the path between the texel and the source.
2413 *
2414 * When jitter is enabled, the position index is computed using the Global Shift
2415 * sampling technique: a hash-based global shift is applied to the indices which is then
2416 * factored to cover the range [0, steps].
2417 * Without jitter, the integer index `i` is returned
2418 * directly.
2419 */
2420
2421 float get_sample_position(const int i,
2422 const bool use_jitter,
2423 float jitter_factor,
2424 const float random_offset)
2425 {
2426 if (use_jitter) {
2427 return math::safe_divide(i + random_offset, 1.0f - jitter_factor);
2428 }
2429 return i;
2430 }
2431
2433 {
2434 return this->get_jitter_factor() != 0.0f;
2435 }
2436
2438 {
2439 return math::clamp(this->get_input("Jitter").get_single_value_default<float>(), 0.0f, 1.0f);
2440 }
2441
2442 /* ----------
2443 * Kernel.
2444 * ---------- */
2445
2446 Result execute_kernel(const Result &highlights)
2447 {
2448 const Result &kernel = this->get_kernel_input();
2449 Result kernel_result = this->context().create_result(ResultType::Color);
2450
2451 if (kernel.is_single_value()) {
2452 kernel_result.allocate_texture(highlights.domain());
2453 if (this->context().use_gpu()) {
2454 GPU_texture_copy(kernel_result, highlights);
2455 }
2456 else {
2457 parallel_for(kernel_result.domain().data_size, [&](const int2 texel) {
2458 kernel_result.store_pixel(texel, highlights.load_pixel<Color>(texel));
2459 });
2460 }
2461 return kernel_result;
2462 }
2463
2465 convolve(this->context(), highlights, kernel, kernel_result, true);
2466 }
2467 else {
2468 Result downsampled_kernel = this->downsample_kernel(kernel);
2469 convolve(this->context(), highlights, downsampled_kernel, kernel_result, true);
2470 downsampled_kernel.release();
2471 }
2472
2473 return kernel_result;
2474 }
2475
2477 {
2478 if (this->context().use_gpu()) {
2479 return this->downsample_kernel_gpu(kernel);
2480 }
2481
2482 return this->downsample_kernel_cpu(kernel);
2483 }
2484
2486 {
2487 Result downsampled_kernel = this->context().create_result(kernel.type());
2488 const int2 size = kernel.domain().data_size / this->get_quality_factor();
2489 downsampled_kernel.allocate_texture(size);
2490
2491 if (kernel.type() == ResultType::Float) {
2492 parallel_for(size, [&](const int2 texel) {
2493 const float2 normalized_coordinates = (float2(texel) + float2(0.5f)) / float2(size);
2494 downsampled_kernel.store_pixel(
2495 texel, kernel.sample_bilinear_extended<float>(normalized_coordinates));
2496 });
2497 }
2498 else {
2499 parallel_for(size, [&](const int2 texel) {
2500 const float2 normalized_coordinates = (float2(texel) + float2(0.5f)) / float2(size);
2501 downsampled_kernel.store_pixel(
2502 texel, kernel.sample_bilinear_extended<Color>(normalized_coordinates));
2503 });
2504 }
2505
2506 return downsampled_kernel;
2507 }
2508
2510 {
2511 Result downsampled_kernel = this->context().create_result(kernel.type());
2512 const int2 size = kernel.domain().data_size / this->get_quality_factor();
2513 downsampled_kernel.allocate_texture(size);
2514
2515 gpu::Shader *shader = context().get_shader(this->get_kernel_downsample_shader_name(kernel));
2517
2518 GPU_texture_filter_mode(kernel, true);
2519 kernel.bind_as_texture(shader, "input_tx");
2520
2521 downsampled_kernel.bind_as_image(shader, "output_img");
2522
2524
2526 kernel.unbind_as_texture();
2527 downsampled_kernel.unbind_as_image();
2528
2529 return downsampled_kernel;
2530 }
2531
2533 {
2534 if (kernel.type() == ResultType::Float) {
2535 return "compositor_glare_kernel_downsample_float";
2536 }
2537 return "compositor_glare_kernel_downsample_color";
2538 }
2539
2541 {
2542 switch (this->get_kernel_data_type()) {
2544 return this->get_input("Float Kernel");
2546 return this->get_input("Color Kernel");
2547 }
2548
2549 return this->get_input("Float Kernel");
2550 }
2551
2553 {
2554 return KernelDataType(
2555 this->get_input("Kernel Data Type").get_single_value_default<MenuValue>().value);
2556 }
2557
2558 /* ----------
2559 * Glare Mix.
2560 * ---------- */
2561
2562 void execute_mix(const Result &glare_result)
2563 {
2564 Result &image_output = this->get_result("Image");
2565 if (!image_output.should_compute()) {
2566 return;
2567 }
2568
2569 if (this->context().use_gpu()) {
2570 this->execute_mix_gpu(glare_result);
2571 }
2572 else {
2573 this->execute_mix_cpu(glare_result);
2574 }
2575 }
2576
2577 void execute_mix_gpu(const Result &glare_result)
2578 {
2579 gpu::Shader *shader = context().get_shader("compositor_glare_mix");
2581
2582 GPU_shader_uniform_1f(shader, "saturation", this->get_saturation());
2584
2585 const Result &input_image = get_input("Image");
2586 input_image.bind_as_texture(shader, "input_tx");
2587
2588 GPU_texture_filter_mode(glare_result, true);
2589 glare_result.bind_as_texture(shader, "glare_tx");
2590
2591 const Domain domain = compute_domain();
2592 Result &output_image = get_result("Image");
2593 output_image.allocate_texture(domain);
2594 output_image.bind_as_image(shader, "output_img");
2595
2597
2599 output_image.unbind_as_image();
2600 input_image.unbind_as_texture();
2601 glare_result.unbind_as_texture();
2602 }
2603
2604 void execute_mix_cpu(const Result &glare_result)
2605 {
2606 const float saturation = this->get_saturation();
2607 const float3 tint = this->get_corrected_tint();
2608
2609 const Result &input = get_input("Image");
2610
2611 const Domain domain = compute_domain();
2612 Result &output = get_result("Image");
2613 output.allocate_texture(domain);
2614
2615 parallel_for(domain.data_size, [&](const int2 texel) {
2616 /* Make sure the input is not negative
2617 * to avoid a subtractive effect when adding the glare. */
2618 float4 input_color = math::max(float4(0.0f), float4(input.load_pixel<Color>(texel)));
2619
2620 float2 normalized_coordinates = (float2(texel) + float2(0.5f)) /
2621 float2(input.domain().data_size);
2622 float4 glare_color = float4(
2623 glare_result.sample_bilinear_extended<Color>(normalized_coordinates));
2624
2625 /* Adjust saturation of glare. */
2626 float4 glare_hsva;
2627 rgb_to_hsv_v(glare_color, glare_hsva);
2628 glare_hsva.y = math::clamp(glare_hsva.y * saturation, 0.0f, 1.0f);
2629 float4 glare_rgba;
2630 hsv_to_rgb_v(glare_hsva, glare_rgba);
2631
2632 float3 combined_color = input_color.xyz() + glare_rgba.xyz() * tint;
2633
2634 output.store_pixel(texel, Color(float4(combined_color, input_color.w)));
2635 });
2636 }
2637
2638 /* Writes the given input glare by adjusting it as needed and upsampling it using bilinear
2639 * interpolation to match the size of the original input, allocating the glare output and writing
2640 * the result to it. */
2641 void write_glare_output(const Result &glare)
2642 {
2643 if (this->context().use_gpu()) {
2644 this->write_glare_output_gpu(glare);
2645 }
2646 else {
2647 this->write_glare_output_cpu(glare);
2648 }
2649 }
2650
2652 {
2653 gpu::Shader *shader = this->context().get_shader("compositor_glare_write_glare_output");
2655
2656 GPU_shader_uniform_1f(shader, "saturation", this->get_saturation());
2658
2659 GPU_texture_filter_mode(glare, true);
2661 glare.bind_as_texture(shader, "input_tx");
2662
2663 const Result &image_input = this->get_input("Image");
2664 Result &output = this->get_result("Glare");
2665 output.allocate_texture(image_input.domain());
2666 output.bind_as_image(shader, "output_img");
2667
2668 compute_dispatch_threads_at_least(shader, output.domain().data_size);
2669
2671 output.unbind_as_image();
2672 glare.unbind_as_texture();
2673 }
2674
2676 {
2677 const float saturation = this->get_saturation();
2678 const float3 tint = this->get_corrected_tint();
2679
2680 const Result &image_input = this->get_input("Image");
2681 Result &output = this->get_result("Glare");
2682 output.allocate_texture(image_input.domain());
2683
2684 const int2 size = output.domain().data_size;
2685 parallel_for(size, [&](const int2 texel) {
2686 float2 normalized_coordinates = (float2(texel) + float2(0.5f)) / float2(size);
2687 float4 glare_color = float4(glare.sample_bilinear_extended<Color>(normalized_coordinates));
2688
2689 /* Adjust saturation of glare. */
2690 float4 glare_hsva;
2691 rgb_to_hsv_v(glare_color, glare_hsva);
2692 glare_hsva.y = math::clamp(glare_hsva.y * saturation, 0.0f, 1.0f);
2693 float4 glare_rgba;
2694 hsv_to_rgb_v(glare_hsva, glare_rgba);
2695
2696 float3 adjusted_glare_value = glare_rgba.xyz() * tint;
2697 output.store_pixel(texel, Color(float4(adjusted_glare_value, 1.0f)));
2698 });
2699 }
2700
2701 /* Combine the tint, strength, and normalization scale into a single factor that can be
2702 * multiplied to the glare. */
2704 {
2705 return this->get_tint() * this->get_strength() / this->get_normalization_scale();
2706 }
2707
2708 /* The computed glare might need to be normalized to be energy conserving or be in a reasonable
2709 * range, instead of doing that in a separate step as part of the glare computation, we delay the
2710 * normalization until the mixing step as an optimization, since we multiply by the tint and
2711 * strength anyway. */
2713 {
2714 switch (this->get_type()) {
2716 /* Bloom adds a number of passes equal to the chain length, if the input is constant, each
2717 * of those passes will hold the same constant, so we need to normalize by the chain
2718 * length, see the bloom code for more information. If the chain length is less than 1,
2719 * then no bloom will be generated, so we can return 1 in this case to avoid zero division
2720 * later on. */
2721 return math::max(1, this->compute_bloom_chain_length());
2728 return 1.0f;
2729 }
2730
2731 return 1.0f;
2732 }
2733
2734 /* -------
2735 * Common.
2736 * ------- */
2737
2739 {
2740 return CMPNodeGlareType(this->get_input("Type").get_single_value_default<MenuValue>().value);
2741 }
2742
2744 {
2745 return math::max(0.0f, this->get_input("Strength").get_single_value_default<float>());
2746 }
2747
2749 {
2750 return math::max(0.0f, this->get_input("Saturation").get_single_value_default<float>());
2751 }
2752
2754 {
2755 return float4(this->get_input("Tint").get_single_value_default<Color>()).xyz();
2756 }
2757
2758 float get_size()
2759 {
2760 return math::clamp(this->get_input("Size").get_single_value_default<float>(), 0.0f, 1.0f);
2761 }
2762
2764 {
2765 return math::clamp(this->get_input("Iterations").get_single_value_default<int>(), 2, 5);
2766 }
2767
2768 float get_fade()
2769 {
2770 return math::clamp(this->get_input("Fade").get_single_value_default<float>(), 0.75f, 1.0f);
2771 }
2772
2774 {
2775 return math::clamp(
2776 this->get_input("Color Modulation").get_single_value_default<float>(), 0.0f, 1.0f);
2777 }
2778
2780 {
2781 return this->get_input("Sun Position").get_single_value_default<float2>();
2782 }
2783
2784 /* As a performance optimization, the operation can compute the glare on a fraction of the input
2785 * image size, so the input is downsampled then upsampled at the end, and this method returns the
2786 * domain after downsampling. */
2788 {
2789 Domain domain = this->compute_domain();
2790 const int2 quality_factor = int2(this->get_quality_factor());
2791 domain.data_size = math::divide_ceil(domain.data_size, quality_factor);
2792 domain.display_size = math::divide_ceil(domain.display_size, quality_factor);
2793 domain.data_offset = domain.data_offset / quality_factor;
2794 return domain;
2795 }
2796
2797 /* The glare node can compute the glare on a fraction of the input image size to improve
2798 * performance. The quality values and their corresponding quality factors are as follows:
2799 *
2800 * - High Quality => Quality Value: 0 => Quality Factor: 1.
2801 * - Medium Quality => Quality Value: 1 => Quality Factor: 2.
2802 * - Low Quality => Quality Value: 2 => Quality Factor: 4.
2803 *
2804 * Dividing the image size by the quality factor gives the size where the glare should be
2805 * computed. The glare algorithm should also take the quality factor into account to compensate
2806 * for the reduced sized, perhaps by dividing blur radii and similar values by the quality
2807 * factor. */
2809 {
2810 return 1 << this->get_quality();
2811 }
2812
2814 {
2815 return CMPNodeGlareQuality(
2816 this->get_input("Quality").get_single_value_default<MenuValue>().value);
2817 }
2818};
2819
2821{
2822 return new GlareOperation(context, node);
2823}
2824
2825static void node_register()
2826{
2827 static bke::bNodeType ntype;
2828
2829 cmp_node_type_base(&ntype, "CompositorNodeGlare"_ustr, CMP_NODE_GLARE);
2830 ntype.ui_name = "Glare";
2831 ntype.ui_description = "Add lens flares, fog and glows around bright parts of the image";
2832 ntype.enum_name_legacy = "GLARE";
2834 ntype.declare = node_declare;
2835 ntype.initfunc = node_init;
2841
2843}
2844NOD_REGISTER_NODE(node_register)
2845
2846} // namespace blender::nodes::node_composite_glare_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_GLARE
#define M_PI
#define IFACE_(msgid)
Read Blender memory allocation and freeing.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
long long int int64_t
#define output
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
Result create_result(ResultType type, ResultPrecision precision)
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void share_data(const Result &source)
Definition result.cc:996
T sample_bilinear_extended(const float2 &coordinates) const
void store_pixel(const int2 &texel, const T &pixel_value)
bool should_compute() const
Definition result.cc:1124
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
T load_pixel_zero(const int2 &texel) const
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T sample_bilinear_zero(const float2 &coordinates) const
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
PanelDeclarationBuilder & add_panel(UString name, int identifier=-1)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
NodeOperation(Context &context, const bNode &node)
Result execute_sun_beams_gpu(Result &highlights, const int max_steps)
void compute_bloom_upsample_gpu(const Result &input, Result &output)
Result execute_sun_beams_cpu(Result &highlights, const int max_steps)
Result apply_streak_filter(const Result &highlights, const float2 &streak_direction)
float adaptive_smooth_clamp(const float x, const float min_value, const float max_value, const float smoothness)
void accumulate_streak_gpu(const Result &streak_result, Result &accumulated_streaks_result)
Result execute_simple_star_anti_diagonal_pass(const Result &highlights, const Result &diagonal_pass_result)
float smooth_min(const float a, const float b, const float smoothness)
void compute_base_ghost_gpu(const Result &small_ghost_result, const Result &big_ghost_result, Result &base_ghost_result)
Result execute_simple_star_horizontal_pass_gpu(const Result &highlights)
Result apply_streak_filter_gpu(const Result &highlights, const float2 &streak_direction)
float smooth_clamp(const float x, const float min_value, const float max_value, const float min_smoothness, const float max_smoothness)
Vector< Result > compute_bloom_downsample_chain(const Result &highlights, int chain_length)
Result execute_simple_star_anti_diagonal_pass_gpu(const Result &highlights, const Result &diagonal_pass_result)
void compute_bloom_downsample_cpu(const Result &input, Result &output)
void accumulate_ghosts_cpu(const Result &base_ghost, Result &accumulated_ghosts_result)
Result execute_simple_star_vertical_pass_cpu(const Result &highlights, const Result &horizontal_pass_result)
Result execute_simple_star_horizontal_pass_cpu(const Result &highlights)
Result execute_simple_star_vertical_pass(const Result &highlights, const Result &horizontal_pass_result)
void compute_bloom_upsample_cpu(const Result &input, Result &output)
float smooth_max(const float a, const float b, const float smoothness)
Result execute_simple_star_vertical_pass_gpu(const Result &highlights, const Result &horizontal_pass_result)
const char * get_kernel_downsample_shader_name(const Result &kernel)
void accumulate_streak(const Result &streak_result, Result &accumulated_streaks_result)
void accumulate_streak_cpu(const Result &streak, Result &accumulated_streaks)
Result apply_streak_filter_cpu(const Result &highlights, const float2 &streak_direction)
void compute_base_ghost_cpu(const Result &small_ghost_result, const Result &big_ghost_result, Result &combined_ghost)
void compute_bloom_downsample_gpu(const Result &input, Result &output, const bool use_karis_average)
Result execute_simple_star_anti_diagonal_pass_cpu(const Result &highlights, const Result &diagonal_pass_result)
float get_sample_position(const int i, const bool use_jitter, float jitter_factor, const float random_offset)
float4 karis_brightness_weighted_sum(const float4 &color1, const float4 &color2, const float4 &color3, const float4 &color4)
void accumulate_ghosts_gpu(const Result &base_ghost_result, Result &accumulated_ghosts_result)
#define position
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
int compute_diagonal_length(const int2 &size, const int diagonal_index)
int2 compute_anti_diagonal_start(const int2 &size, const int index)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
int compute_anti_diagonal_length(const int2 &size, const int diagonal_index)
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const math::FilterKernel filter_type=math::FilterKernel::Gauss, const bool extend_bounds=false)
int compute_number_of_diagonals(const int2 &size)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void convolve(Context &context, const Result &input, const Result &kernel, Result &output, const bool normalize_kernel)
int2 compute_diagonal_start(const int2 &size, const int index)
void parallel_for(const int2 range, const Function &function)
int context(const bContext *C, const char *member, bContextDataResult *result)
int optimal_size_for_real_transform(int size)
Definition fftw.cc:59
AngleRadianBase< float > AngleRadian
T cos(const AngleRadianBase< T > &a)
T pow(const T &x, const T &power)
T clamp(const T &a, const T &min, const T &max)
T safe_rcp(const T &a)
T safe_divide(const T &a, const T &b)
T reduce_max(const VecBase< T, Size > &a)
T distance(const T &a, const T &b)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T length(const VecBase< T, Size > &a)
T reduce_min(const VecBase< T, Size > &a)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T square(const T &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
T abs(const T &a)
T reduce_add(const VecBase< T, Size > &a)
static const EnumPropertyItem type_items[]
static void node_declare(NodeDeclarationBuilder &b)
static const EnumPropertyItem quality_items[]
static void node_update_glare_label(const bNodeTree *ntree, const bNode *node, char *label, int maxlen)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void gather_link_searches(GatherLinkSearchOpParams &params)
static const EnumPropertyItem kernel_data_type_items[]
static void node_init(bNodeTree *, bNode *node)
float hash_to_float(uint32_t kx)
Definition noise.cc:232
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
__int64 int64_t
Definition BKE_text.h:90
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
@ CMP_NODE_GLARE_QUALITY_MEDIUM
@ CMP_NODE_GLARE_QUALITY_HIGH
@ CMP_NODE_GLARE_QUALITY_LOW
void GPU_shader_uniform_4fv_array(gpu::Shader *sh, const char *name, int len, const float(*val)[4])
@ CMP_NODE_GLARE_SUN_BEAMS
@ CMP_NODE_GLARE_SIMPLE_STAR
@ CMP_NODE_GLARE_KERNEL
@ CMP_NODE_GLARE_STREAKS
@ CMP_NODE_GLARE_FOG_GLOW
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
unsigned char uint8_t
Definition BKE_text.h:79
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define MAX_GLARE_ITERATIONS
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
static AngleRadianBase from_degree(const float &degrees)
void reserve(int new_size)
Definition vector.hh:78
#define N_(msgid)