Blender V5.3
van_vliet_gaussian_blur.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_assert.hh"
6#include "BLI_math_vector.hh"
7
8#include "GPU_shader.hh"
9
10#include "COM_context.hh"
11#include "COM_result.hh"
12#include "COM_utilities.hh"
13
14#include "COM_algorithm_pad.hh"
17
18namespace blender::compositor {
19
20#define FILTER_ORDER 2
21
23 const Result &first_causal_input,
24 const Result &first_non_causal_input,
25 const Result &second_causal_input,
26 const Result &second_non_causal_input,
28{
29 gpu::Shader *shader = context.get_shader("compositor_van_vliet_gaussian_blur_sum");
31
32 first_causal_input.bind_as_texture(shader, "first_causal_input_tx");
33 first_non_causal_input.bind_as_texture(shader, "first_non_causal_input_tx");
34 second_causal_input.bind_as_texture(shader, "second_causal_input_tx");
35 second_non_causal_input.bind_as_texture(shader, "second_non_causal_input_tx");
36
37 const Domain domain = first_causal_input.domain();
38 const Domain transposed_domain = domain.transposed();
39 output.allocate_texture(transposed_domain);
40 output.bind_as_image(shader, "output_img");
41
43
45 first_causal_input.unbind_as_texture();
46 first_non_causal_input.unbind_as_texture();
47 second_causal_input.unbind_as_texture();
48 second_non_causal_input.unbind_as_texture();
49 output.unbind_as_image();
50}
51
52/* See sum_causal_and_non_causal_results. */
53static void sum_causal_and_non_causal_results_cpu(const Result &first_causal_input,
54 const Result &first_non_causal_input,
55 const Result &second_causal_input,
56 const Result &second_non_causal_input,
58{
59 const Domain domain = first_causal_input.domain();
60 const Domain transposed_domain = domain.transposed();
61 output.allocate_texture(transposed_domain);
62 parallel_for(domain.data_size, [&](const int2 texel) {
63 /* The Van Vliet filter is a parallel interconnection filter, meaning its output is the sum of
64 * all of its causal and non causal filters. */
65 float4 filter_output = float4(first_causal_input.load_pixel<Color>(texel)) +
66 float4(first_non_causal_input.load_pixel<Color>(texel)) +
67 float4(second_causal_input.load_pixel<Color>(texel)) +
68 float4(second_non_causal_input.load_pixel<Color>(texel));
69
70 /* Write the color using the transposed texel. See the sum_causal_and_non_causal_results method
71 * for more information on the rational behind this. */
72 output.store_pixel(int2(texel.y, texel.x), Color(filter_output));
73 });
74}
75
76/* Sum all four of the causal and non causal outputs of the first and second filters and write the
77 * sum to the output. This is because the Van Vliet filter is implemented as a bank of 2 parallel
78 * second order filters, meaning its output is the sum of the causal and non causal filters of both
79 * filters. The output is expected not to be allocated as it will be allocated internally.
80 *
81 * The output is allocated and written transposed, that is, with a height equivalent to the width
82 * of the input and vice versa. This is done as a performance optimization. The blur pass will
83 * blur the image horizontally and write it to the intermediate output transposed. Then the
84 * vertical pass will execute the same horizontal blur shader, but since its input is transposed,
85 * it will effectively do a vertical blur and write to the output transposed, effectively undoing
86 * the transposition in the horizontal pass. This is done to improve spatial cache locality in the
87 * shader and to avoid having two separate shaders for each blur pass. */
89 const Result &first_causal_input,
90 const Result &first_non_causal_input,
91 const Result &second_causal_input,
92 const Result &second_non_causal_input,
94{
95 if (context.use_gpu()) {
97 first_causal_input,
98 first_non_causal_input,
99 second_causal_input,
100 second_non_causal_input,
101 output);
102 }
103 else {
104 sum_causal_and_non_causal_results_cpu(first_causal_input,
105 first_non_causal_input,
106 second_causal_input,
107 second_non_causal_input,
108 output);
109 }
110}
111
112static void blur_pass_gpu(Context &context,
113 const Result &input,
114 Result &first_causal_result,
115 Result &first_non_causal_result,
116 Result &second_causal_result,
117 Result &second_non_causal_result,
118 const float sigma)
119{
120 gpu::Shader *shader = context.get_shader("compositor_van_vliet_gaussian_blur");
122
123 const VanVlietGaussianCoefficients &coefficients =
124 context.cache_manager().van_vliet_gaussian_coefficients.get(context, sigma);
125
127 shader, "first_feedback_coefficients", float2(coefficients.first_feedback_coefficients()));
129 "first_causal_feedforward_coefficients",
132 "first_non_causal_feedforward_coefficients",
135 shader, "second_feedback_coefficients", float2(coefficients.second_feedback_coefficients()));
137 "second_causal_feedforward_coefficients",
140 "second_non_causal_feedforward_coefficients",
143 "first_causal_boundary_coefficient",
144 float(coefficients.first_causal_boundary_coefficient()));
146 "first_non_causal_boundary_coefficient",
147 float(coefficients.first_non_causal_boundary_coefficient()));
149 "second_causal_boundary_coefficient",
150 float(coefficients.second_causal_boundary_coefficient()));
152 "second_non_causal_boundary_coefficient",
153 float(coefficients.second_non_causal_boundary_coefficient()));
154
155 input.bind_as_texture(shader, "input_tx");
156
157 const Domain domain = input.domain();
158
159 first_causal_result.allocate_texture(domain);
160 first_causal_result.bind_as_image(shader, "first_causal_output_img");
161
162 first_non_causal_result.allocate_texture(domain);
163 first_non_causal_result.bind_as_image(shader, "first_non_causal_output_img");
164
165 second_causal_result.allocate_texture(domain);
166 second_causal_result.bind_as_image(shader, "second_causal_output_img");
167
168 second_non_causal_result.allocate_texture(domain);
169 second_non_causal_result.bind_as_image(shader, "second_non_causal_output_img");
170
171 /* The second dispatch dimension is 4 dispatches, one for the first causal filter, one for the
172 * first non causal filter, one for the second causal filter, and one for the second non causal
173 * filter. */
175
177 input.unbind_as_texture();
178 first_causal_result.unbind_as_image();
179 first_non_causal_result.unbind_as_image();
180 second_causal_result.unbind_as_image();
181 second_non_causal_result.unbind_as_image();
182}
183
184static void blur_pass_cpu(Context &context,
185 const Result &input,
186 Result &first_causal_output,
187 Result &first_non_causal_output,
188 Result &second_causal_output,
189 Result &second_non_causal_output,
190 const float sigma)
191{
192 const VanVlietGaussianCoefficients &coefficients =
193 context.cache_manager().van_vliet_gaussian_coefficients.get(context, sigma);
194
195 const float2 first_feedback_coefficients = float2(coefficients.first_feedback_coefficients());
196 const float2 first_causal_feedforward_coefficients = float2(
198 const float2 first_non_causal_feedforward_coefficients = float2(
200 const float2 second_feedback_coefficients = float2(coefficients.second_feedback_coefficients());
201 const float2 second_causal_feedforward_coefficients = float2(
203 const float2 second_non_causal_feedforward_coefficients = float2(
205 const float first_causal_boundary_coefficient = float(
206 coefficients.first_causal_boundary_coefficient());
207 const float first_non_causal_boundary_coefficient = float(
209 const float second_causal_boundary_coefficient = float(
211 const float second_non_causal_boundary_coefficient = float(
213
214 const Domain domain = input.domain();
215 first_causal_output.allocate_texture(domain);
216 first_non_causal_output.allocate_texture(domain);
217 second_causal_output.allocate_texture(domain);
218 second_non_causal_output.allocate_texture(domain);
219
220 /* The first dispatch dimension is 4 dispatches, one for the first causal filter, one for the
221 * first non causal filter, one for the second causal filter, and one for the second non causal
222 * filter. */
223 const int2 parallel_for_size = int2(4, domain.data_size.y);
224 /* Blur the input horizontally by applying a fourth order IIR filter approximating a Gaussian
225 * filter using Van Vliet's design method. This is based on the following paper:
226 *
227 * Van Vliet, Lucas J., Ian T. Young, and Piet W. Verbeek. "Recursive Gaussian derivative
228 * filters." Proceedings. Fourteenth International Conference on Pattern Recognition (Cat. No.
229 * 98EX170). Vol. 1. IEEE, 1998.
230 *
231 * We decomposed the filter into two second order filters, so we actually run four filters per
232 * row in parallel, one for the first causal filter, one for the first non causal filter, one for
233 * the second causal filter, and one for the second non causal filter, storing the result of each
234 * separately. See the VanVlietGaussianCoefficients class and the implementation for more
235 * information. */
236 parallel_for(parallel_for_size, [&](const int2 invocation) {
237 /* The shader runs parallel across rows but serially across columns. */
238 int y = invocation.y;
239 int width = input.domain().data_size.x;
240
241 /* The second dispatch dimension is four dispatches:
242 *
243 * 0 -> First causal filter.
244 * 1 -> First non causal filter.
245 * 2 -> Second causal filter.
246 * 3 -> Second non causal filter.
247 *
248 * We detect causality by even numbers. */
249 bool is_causal = invocation.x % 2 == 0;
250 float2 first_feedforward_coefficients = is_causal ? first_causal_feedforward_coefficients :
251 first_non_causal_feedforward_coefficients;
252 float first_boundary_coefficient = is_causal ? first_causal_boundary_coefficient :
253 first_non_causal_boundary_coefficient;
254 float2 second_feedforward_coefficients = is_causal ?
255 second_causal_feedforward_coefficients :
256 second_non_causal_feedforward_coefficients;
257 float second_boundary_coefficient = is_causal ? second_causal_boundary_coefficient :
258 second_non_causal_boundary_coefficient;
259 /* And we detect the filter by order. */
260 bool is_first_filter = invocation.x < 2;
261 float2 feedforward_coefficients = is_first_filter ? first_feedforward_coefficients :
262 second_feedforward_coefficients;
263 float2 feedback_coefficients = is_first_filter ? first_feedback_coefficients :
264 second_feedback_coefficients;
265 float boundary_coefficient = is_first_filter ? first_boundary_coefficient :
266 second_boundary_coefficient;
267
268 /* Create an array that holds the last FILTER_ORDER inputs along with the current input. The
269 * current input is at index 0 and the oldest input is at index FILTER_ORDER. We assume Neumann
270 * boundary condition, so we initialize all inputs by the boundary pixel. */
271 int2 boundary_texel = is_causal ? int2(0, y) : int2(width - 1, y);
272 float4 input_boundary = float4(input.load_pixel<Color>(boundary_texel));
273 float4 inputs[FILTER_ORDER + 1] = {input_boundary, input_boundary, input_boundary};
274
275 /* Create an array that holds the last FILTER_ORDER outputs along with the current output. The
276 * current output is at index 0 and the oldest output is at index FILTER_ORDER. We assume
277 * Neumann boundary condition, so we initialize all outputs by the boundary pixel multiplied by
278 * the boundary coefficient. See the VanVlietGaussianCoefficients class for more information on
279 * the boundary handing. */
280 float4 output_boundary = input_boundary * boundary_coefficient;
281 float4 outputs[FILTER_ORDER + 1] = {output_boundary, output_boundary, output_boundary};
282
283 for (int x = 0; x < width; x++) {
284 /* Run forward across rows for the causal filter and backward for the non causal filter. */
285 int2 texel = is_causal ? int2(x, y) : int2(width - 1 - x, y);
286 inputs[0] = float4(input.load_pixel<Color>(texel));
287
288 /* Compute the filter based on its difference equation, this is not in the Van Vliet paper
289 * because the filter was decomposed, but it is essentially similar to Equation (28) for the
290 * causal filter or Equation (29) for the non causal filter in Deriche's paper, except it is
291 * second order, not fourth order.
292 *
293 * Deriche, Rachid. Recursively implementating the Gaussian and its derivatives. Diss.
294 * INRIA, 1993.
295 *
296 * The only difference is that the non causal filter ignores the current value and starts
297 * from the previous input, as can be seen in the subscript of the first input term in both
298 * equations. So add one while indexing the non causal inputs. */
299 outputs[0] = float4(0.0f);
300 int first_input_index = is_causal ? 0 : 1;
301 for (int i = 0; i < FILTER_ORDER; i++) {
302 outputs[0] += feedforward_coefficients[i] * inputs[first_input_index + i];
303 outputs[0] -= feedback_coefficients[i] * outputs[i + 1];
304 }
305
306 /* Store the causal and non causal outputs of each of the two filters independently, then sum
307 * them in a separate shader dispatch for better parallelism. */
308 if (is_causal) {
309 if (is_first_filter) {
310 first_causal_output.store_pixel(texel, Color(outputs[0]));
311 }
312 else {
313 second_causal_output.store_pixel(texel, Color(outputs[0]));
314 }
315 }
316 else {
317 if (is_first_filter) {
318 first_non_causal_output.store_pixel(texel, Color(outputs[0]));
319 }
320 else {
321 second_non_causal_output.store_pixel(texel, Color(outputs[0]));
322 }
323 }
324
325 /* Shift the inputs temporally by one. The oldest input is discarded, while the current input
326 * will retain its value but will be overwritten with the new current value in the next
327 * iteration. */
328 for (int i = FILTER_ORDER; i >= 1; i--) {
329 inputs[i] = inputs[i - 1];
330 }
331
332 /* Shift the outputs temporally by one. The oldest output is discarded, while the current
333 * output will retain its value but will be overwritten with the new current value in the
334 * next iteration. */
335 for (int i = FILTER_ORDER; i >= 1; i--) {
336 outputs[i] = outputs[i - 1];
337 }
338 }
339 });
340}
341
342static void blur_pass(Context &context, const Result &input, Result &output, const float sigma)
343{
344 Result first_causal_result = context.create_result(ResultType::Color);
345 Result first_non_causal_result = context.create_result(ResultType::Color);
346 Result second_causal_result = context.create_result(ResultType::Color);
347 Result second_non_causal_result = context.create_result(ResultType::Color);
348
349 if (context.use_gpu()) {
350 blur_pass_gpu(context,
351 input,
352 first_causal_result,
353 first_non_causal_result,
354 second_causal_result,
355 second_non_causal_result,
356 sigma);
357 }
358 else {
359 blur_pass_cpu(context,
360 input,
361 first_causal_result,
362 first_non_causal_result,
363 second_causal_result,
364 second_non_causal_result,
365 sigma);
366 }
367
369 first_causal_result,
370 first_non_causal_result,
371 second_causal_result,
372 second_non_causal_result,
373 output);
374 first_causal_result.release();
375 first_non_causal_result.release();
376 second_causal_result.release();
377 second_non_causal_result.release();
378}
379
380/* Computes the inverse of compute_sigma_from_radius in recursive_gaussian_blur.cc, see that
381 * function for more information. */
383{
384 return sigma * 3.0f;
385}
386
388 const Result &input,
389 Result &output,
390 const float2 &sigma,
391 const bool extend_bounds)
392{
393 BLI_assert_msg(math::reduce_max(sigma) >= 32.0f,
394 "Van Vliet filter is less accurate for sigma values less than 32. Use Deriche "
395 "filter instead or direct convolution instead.");
396
397 if (extend_bounds) {
398 const int2 padding_size = int2(math::ceil(compute_radius_from_sigma(sigma)));
399 Result padded_input = context.create_result(input.type());
400 pad(context, input, padded_input, int2(padding_size.x, 0), PaddingMethod::Zero);
401
402 Result horizontal_pass_result = context.create_result(input.type());
403 blur_pass(context, padded_input, horizontal_pass_result, sigma.x);
404 padded_input.release();
405
406 Result padded_horizontal_pass_result = context.create_result(input.type());
407 pad(context,
408 horizontal_pass_result,
409 padded_horizontal_pass_result,
410 int2(padding_size.y, 0),
412 horizontal_pass_result.release();
413
414 blur_pass(context, padded_horizontal_pass_result, output, sigma.y);
415 padded_horizontal_pass_result.release();
416 }
417 else {
418 Result horizontal_pass_result = context.create_result(input.type());
419 blur_pass(context, input, horizontal_pass_result, sigma.x);
420 blur_pass(context, horizontal_pass_result, output, sigma.y);
421 horizontal_pass_result.release();
422 }
423}
424
425} // namespace blender::compositor
#define BLI_assert_msg(a, msg)
#define output
Domain transposed() const
Definition domain.cc:38
void store_pixel(const int2 &texel, const T &pixel_value)
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
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(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
#define FILTER_ORDER
static void sum_causal_and_non_causal_results_cpu(const Result &causal_input, const Result &non_causal_input, Result &output)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
static void sum_causal_and_non_causal_results(Context &context, const Result &causal_input, const Result &non_causal_input, Result &output)
static float2 compute_radius_from_sigma(const float2 sigma)
static void blur_pass_cpu(Context &context, const Result &input, Result &causal_output, Result &non_causal_output, const float sigma)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void pad(Context &context, const Result &input, Result &output, const int2 size, const PaddingMethod padding_method)
Definition pad.cc:113
void van_vliet_gaussian_blur(Context &context, const Result &input, Result &output, const float2 &sigma, const bool extend_bounds)
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 sum_causal_and_non_causal_results_gpu(Context &context, const Result &causal_input, const Result &non_causal_input, Result &output)
static void blur_pass(Context &context, const Result &input, Result &output, const float sigma)
T reduce_max(const VecBase< T, Size > &a)
T ceil(const T &a)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float