Blender V5.3
symmetric_blur_weights.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 <cstdint>
6#include <memory>
7
8#include "BLI_hash.hh"
9#include "BLI_index_range.hh"
10#include "BLI_math_vector.hh"
12
13#include "COM_context.hh"
14#include "COM_result.hh"
16
17namespace blender::compositor {
18
19/* --------------------------------------------------------------------
20 * Symmetric Blur Weights Key.
21 */
22
27
32
34{
35 return a.type == b.type && a.radius == b.radius;
36}
37
38/* --------------------------------------------------------------------
39 * Symmetric Blur Weights.
40 */
41
44 float2 radius)
45 : weights(context.create_result(ResultType::Float))
46{
47 Result weights_cpu = context.create_result(ResultType::Float);
48
49 /* The full size of filter is double the radius plus 1, but since the filter is symmetric, we
50 * only compute a single quadrant of it and so no doubling happens. We add 1 to make sure the
51 * filter size is always odd and there is a center weight. */
52 const float2 scale = math::safe_divide(float2(1.0f), radius);
53 const int2 size = int2(math::ceil(radius)) + int2(1);
54 weights_cpu.allocate_texture(size, false, ResultStorageType::CPUImage);
55
56 float sum = 0.0f;
57
58 /* First, compute the center weight. */
59 const float center_weight = math::filter_kernel_value(type, 0.0f);
60 weights_cpu.store_pixel(int2(0, 0), center_weight);
61 sum += center_weight;
62
63 /* Then, compute the weights along the positive x axis, making sure to add double the weight to
64 * the sum of weights because the filter is symmetric and we only loop over the positive half
65 * of the x axis. Skip the center weight already computed by dropping the front index. */
66 for (const int x : IndexRange(weights_cpu.domain().data_size.x).drop_front(1)) {
67 const float weight = math::filter_kernel_value(type, x * scale.x);
68 weights_cpu.store_pixel(int2(x, 0), weight);
69 sum += weight * 2.0f;
70 }
71
72 /* Then, compute the weights along the positive y axis, making sure to add double the weight to
73 * the sum of weights because the filter is symmetric and we only loop over the positive half
74 * of the y axis. Skip the center weight already computed by dropping the front index. */
75 for (const int y : IndexRange(weights_cpu.domain().data_size.y).drop_front(1)) {
76 const float weight = math::filter_kernel_value(type, y * scale.y);
77 weights_cpu.store_pixel(int2(0, y), weight);
78 sum += weight * 2.0f;
79 }
80
81 /* Then, compute the other weights in the upper right quadrant, making sure to add quadruple
82 * the weight to the sum of weights because the filter is symmetric and we only loop over one
83 * quadrant of it. Skip the weights along the y and x axis already computed by dropping the
84 * front index. */
85 for (const int y : IndexRange(weights_cpu.domain().data_size.y).drop_front(1)) {
86 for (const int x : IndexRange(weights_cpu.domain().data_size.x).drop_front(1)) {
87 const float weight = math::filter_kernel_value(type, math::length(float2(x, y) * scale));
88 weights_cpu.store_pixel(int2(x, y), weight);
89 sum += weight * 4.0f;
90 }
91 }
92
93 /* Finally, normalize the weights. */
94 for (const int y : IndexRange(weights_cpu.domain().data_size.y)) {
95 for (const int x : IndexRange(weights_cpu.domain().data_size.x)) {
96 const int2 texel = int2(x, y);
97 weights_cpu.store_pixel(texel, weights_cpu.load_pixel<float>(texel) / sum);
98 }
99 }
100
101 if (context.use_gpu()) {
102 Result weights_gpu = weights_cpu.upload_to_gpu(false);
103 this->weights.share_data(weights_gpu);
104 weights_gpu.release();
105 }
106 else {
107 this->weights.share_data(weights_cpu);
108 }
109
110 weights_cpu.release();
111}
112
117
118/* --------------------------------------------------------------------
119 * Symmetric Blur Weights Container.
120 */
121
123{
124 /* First, delete all resources that are no longer needed. */
125 map_.remove_if([](auto item) { return !item.value->needed; });
126
127 /* Second, reset the needed status of the remaining resources to false to ready them to track
128 * their needed status for the next evaluation. */
129 for (auto &value : map_.values()) {
130 value->needed = false;
131 }
132}
133
136 float2 radius)
137{
138 const SymmetricBlurWeightsKey key(type, radius);
139
140 auto &weights = *map_.lookup_or_add_cb(
141 key, [&]() { return std::make_unique<SymmetricBlurWeights>(context, type, radius); });
142
143 weights.needed = true;
144 return weights.weights;
145}
146
147} // namespace blender::compositor
constexpr IndexRange()=default
void share_data(const Result &source)
Definition result.cc:996
Result upload_to_gpu(const bool from_pool) const
Definition result.cc:671
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
T load_pixel(const int2 &texel) const
Result & get(Context &context, math::FilterKernel type, float2 radius)
SymmetricBlurWeightsKey(math::FilterKernel type, float2 radius)
SymmetricBlurWeights(Context &context, math::FilterKernel type, float2 radius)
float3x3 scale(float3x3 mat, float2 scale)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
T safe_divide(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T ceil(const T &a)
float filter_kernel_value(FilterKernel kernel, float x)
unsigned __int64 uint64_t
Definition BKE_text.h:91
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252