Blender V5.3
symmetric_separable_blur.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <type_traits>
6
7#include "BLI_assert.hh"
9
10#include "GPU_shader.hh"
11
12#include "COM_context.hh"
13#include "COM_result.hh"
14#include "COM_utilities.hh"
15
16#include "COM_algorithm_pad.hh"
18
20
21namespace blender::compositor {
22
23template<typename T>
24static void blur_pass(const Result &input, const Result &weights, Result &output)
25{
26
27 /* Notice that the size is transposed, see the note on the horizontal pass method for more
28 * information on the reasoning behind this. */
29 const int2 size = int2(output.domain().data_size.y, output.domain().data_size.x);
30 parallel_for(size, [&](const int2 texel) {
31 /* Use float4 for Color types since Color does not support arithmetic. */
32 using AccumulateT = std::conditional_t<std::is_same_v<T, Color>, float4, T>;
33 AccumulateT accumulated_value = AccumulateT(0);
34
35 /* First, compute the contribution of the center pixel. */
36 AccumulateT center_value = AccumulateT(input.load_pixel_extended<T>(texel));
37 accumulated_value += center_value * weights.load_pixel<float>(int2(0));
38
39 /* Then, compute the contributions of the pixel to the right and left, noting that the
40 * weights texture only stores the weights for the positive half, but since the filter is
41 * symmetric, the same weight is used for the negative half and we add both of their
42 * contributions. */
43 for (int i = 1; i < weights.domain().data_size.x; i++) {
44 float weight = weights.load_pixel<float>(int2(i, 0));
45 accumulated_value += AccumulateT(input.load_pixel_extended<T>(texel + int2(i, 0))) * weight;
46 accumulated_value += AccumulateT(input.load_pixel_extended<T>(texel + int2(-i, 0))) * weight;
47 }
48
49 /* Write the color using the transposed texel. See the horizontal_pass method for more
50 * information on the rational behind this. */
51 output.store_pixel(int2(texel.y, texel.x), T(accumulated_value));
52 });
53}
54
55static const char *get_blur_shader(const ResultType type)
56{
57 switch (type) {
59 return "compositor_symmetric_separable_blur_float";
62 return "compositor_symmetric_separable_blur_float4";
63 default:
64 break;
65 }
66
68 return nullptr;
69}
70
71static void blur_pass_gpu(Context &context,
72 const Result &input,
74 const float radius,
75 const math::FilterKernel filter_type)
76{
77 gpu::Shader *shader = context.get_shader(get_blur_shader(input.type()));
79
80 input.bind_as_texture(shader, "input_tx");
81
82 const Result &weights = context.cache_manager().symmetric_separable_blur_weights.get(
83 context, filter_type, radius);
84 weights.bind_as_texture(shader, "weights_tx");
85
86 /* We allocate an output image of a transposed size, that is, with a height equivalent to the
87 * width of the input and vice versa. This is done as a performance optimization. The shader
88 * will blur the image horizontally and write it to the intermediate output transposed. Then
89 * the vertical pass will execute the same horizontal blur shader, but since its input is
90 * transposed, it will effectively do a vertical blur and write to the output transposed,
91 * effectively undoing the transposition in the horizontal pass. This is done to improve
92 * spatial cache locality in the shader and to avoid having two separate shaders for each blur
93 * pass. */
94 output.allocate_texture(input.domain().transposed());
95 output.bind_as_image(shader, "output_img");
96
98
100 input.unbind_as_texture();
101 weights.unbind_as_texture();
102 output.unbind_as_image();
103}
104
105static void blur_pass_cpu(Context &context,
106 const Result &input,
107 Result &output,
108 const float radius,
109 const math::FilterKernel filter_type)
110{
111 const Result &weights = context.cache_manager().symmetric_separable_blur_weights.get(
112 context, filter_type, radius);
113
114 /* We allocate an output image of a transposed size, that is, with a height equivalent to the
115 * width of the input and vice versa. This is done as a performance optimization. The code will
116 * blur the image horizontally and write it to the intermediate output transposed. Then the
117 * vertical pass will execute the same horizontal blur shader, but since its input is transposed,
118 * it will effectively do a vertical blur and write to the output transposed, effectively undoing
119 * the transposition in the horizontal pass. This is done to improve spatial cache locality in
120 * the shader and to avoid having two separate shaders for each blur pass. */
121 output.allocate_texture(input.domain().transposed());
122
123 switch (input.type()) {
125 blur_pass<float>(input, weights, output);
126 break;
128 blur_pass<float4>(input, weights, output);
129 break;
131 blur_pass<Color>(input, weights, output);
132 break;
133 default:
135 break;
136 }
137}
138
139static void blur_pass(Context &context,
140 const Result &input,
141 Result &output,
142 const float radius,
143 const math::FilterKernel filter_type)
144{
145 if (context.use_gpu()) {
146 blur_pass_gpu(context, input, output, radius, filter_type);
147 return;
148 }
149 blur_pass_cpu(context, input, output, radius, filter_type);
150}
151
153 const Result &input,
154 Result &output,
155 const float2 &radius,
156 const math::FilterKernel filter_type,
157 const bool extend_bounds)
158{
159 if (extend_bounds) {
160 const int2 padding_size = int2(math::ceil(radius));
161 Result padded_input = context.create_result(input.type());
162 pad(context, input, padded_input, int2(padding_size.x, 0), PaddingMethod::Zero);
163
164 Result horizontal_pass_result = context.create_result(input.type());
165 blur_pass(context, padded_input, horizontal_pass_result, radius.x, filter_type);
166 padded_input.release();
167
168 Result padded_horizontal_pass_result = context.create_result(input.type());
169 pad(context,
170 horizontal_pass_result,
171 padded_horizontal_pass_result,
172 int2(padding_size.y, 0),
174 horizontal_pass_result.release();
175
176 blur_pass(context, padded_horizontal_pass_result, output, radius.y, filter_type);
177 padded_horizontal_pass_result.release();
178 }
179 else {
180 Result horizontal_pass_result = context.create_result(input.type());
181 blur_pass(context, input, horizontal_pass_result, radius.x, filter_type);
182 blur_pass(context, horizontal_pass_result, output, radius.y, filter_type);
183 horizontal_pass_result.release();
184 }
185}
186
187} // namespace blender::compositor
#define BLI_assert_unreachable()
#define output
Domain transposed() const
Definition domain.cc:38
void unbind_as_texture() const
Definition result.cc:964
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 load_pixel_extended(const int2 &texel) const
T load_pixel(const int2 &texel) const
#define T
static void blur_pass(const Result &input, const Result &weights, Result &output)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
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)
static void blur_pass_cpu(Context &context, const Result &input, Result &causal_output, Result &non_causal_output, const float sigma)
void pad(Context &context, const Result &input, Result &output, const int2 size, const PaddingMethod padding_method)
Definition pad.cc:113
static const char * get_blur_shader(const ResultType type)
void parallel_for(const int2 range, const Function &function)
static void blur_pass_gpu(Context &context, const Result &input, Result &causal_result, Result &non_causal_result, const float sigma)
static void blur_pass(Context &context, const Result &input, Result &output, const float sigma)
T ceil(const T &a)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)