Blender V5.3
morphological_distance_feather_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 <cmath>
6#include <cstdint>
7#include <memory>
8
9#include "BLI_hash.hh"
10#include "BLI_index_range.hh"
11#include "BLI_math_filter.hh"
12
13#include "COM_context.hh"
15#include "COM_result.hh"
16
17namespace blender::compositor {
18
19/* --------------------------------------------------------------------
20 * Morphological Distance Feather Weights Key.
21 */
22
28
33
36{
37 return a.type == b.type && a.radius == b.radius;
38}
39
40/* --------------------------------------------------------------------
41 * Morphological Distance Feather Weights.
42 */
43
45 int type,
46 int radius)
47 : weights(context.create_result(ResultType::Float)),
48 falloffs(context.create_result(ResultType::Float))
49{
50 Result weights_cpu = this->compute_weights(context, radius);
51 Result falloffs_cpu = this->compute_distance_falloffs(context, type, radius);
52
53 if (context.use_gpu()) {
54 Result weights_gpu = weights_cpu.upload_to_gpu(false);
55 this->weights.share_data(weights_gpu);
56 weights_gpu.release();
57
58 Result falloffs_gpu = falloffs_cpu.upload_to_gpu(false);
59 this->falloffs.share_data(falloffs_gpu);
60 falloffs_gpu.release();
61 }
62 else {
63 this->weights.share_data(weights_cpu);
64 this->falloffs.share_data(falloffs_cpu);
65 }
66
67 weights_cpu.release();
68 falloffs_cpu.release();
69}
70
76
77Result MorphologicalDistanceFeatherWeights::compute_weights(Context &context, int radius)
78{
79 Result weights_cpu = context.create_result(ResultType::Float);
80
81 /* The size of filter is double the radius plus 1, but since the filter is symmetric, we only
82 * compute half of it and no doubling happens. We add 1 to make sure the filter size is always
83 * odd and there is a center weight. */
84 const int size = radius + 1;
85 weights_cpu.allocate_texture(Domain(int2(size, 1)), false, ResultStorageType::CPUImage);
86
87 float sum = 0.0f;
88
89 /* First, compute the center weight. */
90 const float center_weight = math::filter_kernel_value(math::FilterKernel::Gauss, 0.0f);
91 weights_cpu.store_pixel(int2(0, 0), center_weight);
92 sum += center_weight;
93
94 /* Second, compute the other weights in the positive direction, making sure to add double the
95 * weight to the sum of weights because the filter is symmetric and we only loop over half of
96 * it. Skip the center weight already computed by dropping the front index. */
97 const float scale = radius > 0.0f ? 1.0f / radius : 0.0f;
98 for (const int i : IndexRange(size).drop_front(1)) {
100 weights_cpu.store_pixel(int2(i, 0), weight);
101 sum += weight * 2.0f;
102 }
103
104 /* Finally, normalize the weights. */
105 for (const int i : IndexRange(size)) {
106 const int2 texel = int2(i, 0);
107 weights_cpu.store_pixel(texel, weights_cpu.load_pixel<float>(texel) / sum);
108 }
109
110 return weights_cpu;
111}
112
113/* Computes a falloff that is equal to 1 at an input of zero and decrease to zero at an input of 1,
114 * with the rate of decrease depending on the falloff type. */
115static float compute_distance_falloff(int type, float x)
116{
117 x = 1.0f - x;
118
119 switch (type) {
120 case PROP_SMOOTH:
121 return 3.0f * x * x - 2.0f * x * x * x;
122 case PROP_SPHERE:
123 return std::sqrt(2.0f * x - x * x);
124 case PROP_ROOT:
125 return std::sqrt(x);
126 case PROP_SHARP:
127 return x * x;
128 case PROP_INVSQUARE:
129 return x * (2.0f - x);
130 case PROP_LIN:
131 return x;
132 default:
134 return x;
135 }
136}
137
138Result MorphologicalDistanceFeatherWeights::compute_distance_falloffs(Context &context,
139 int type,
140 int radius)
141{
142 Result falloffs_cpu = context.create_result(ResultType::Float);
143
144 /* The size of the distance falloffs is double the radius plus 1, but since the falloffs are
145 * symmetric, we only compute half of them and no doubling happens. We add 1 to make sure the
146 * falloffs size is always odd and there is a center falloff. */
147 const int size = radius + 1;
148 falloffs_cpu.allocate_texture(Domain(int2(size, 1)), false, ResultStorageType::CPUImage);
149
150 /* Compute the distance falloffs in the positive direction only, because the falloffs are
151 * symmetric. */
152 const float scale = radius > 0.0f ? 1.0f / radius : 0.0f;
153 for (const int i : IndexRange(size)) {
154 falloffs_cpu.store_pixel(int2(i, 0), compute_distance_falloff(type, i * scale));
155 }
156
157 return falloffs_cpu;
158}
159
160/* --------------------------------------------------------------------
161 * Morphological Distance Feather Weights Container.
162 */
163
165{
166 /* First, delete all resources that are no longer needed. */
167 map_.remove_if([](auto item) { return !item.value->needed; });
168
169 /* Second, reset the needed status of the remaining resources to false to ready them to track
170 * their needed status for the next evaluation. */
171 for (auto &value : map_.values()) {
172 value->needed = false;
173 }
174}
175
177 Context &context, int type, int radius)
178{
179 const MorphologicalDistanceFeatherWeightsKey key(type, radius);
180
181 auto &weights = *map_.lookup_or_add_cb(key, [&]() {
182 return std::make_unique<MorphologicalDistanceFeatherWeights>(context, type, radius);
183 });
184
185 weights.needed = true;
186 return weights;
187}
188
189} // namespace blender::compositor
#define BLI_assert_unreachable()
constexpr IndexRange()=default
Domain(const int2 &size)
Definition domain.cc:20
MorphologicalDistanceFeatherWeights & get(Context &context, int type, int radius)
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
float3x3 scale(float3x3 mat, float2 scale)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
static float compute_distance_falloff(int type, float x)
float filter_kernel_value(FilterKernel kernel, float x)
unsigned __int64 uint64_t
Definition BKE_text.h:91
VecBase< int32_t, 2 > int2
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252