Blender V5.3
symmetric_separable_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_base.hh"
11
12#include "COM_context.hh"
13#include "COM_result.hh"
15
16namespace blender::compositor {
17
18/* --------------------------------------------------------------------
19 * Symmetric Separable Blur Weights Key.
20 */
21
27
32
35{
36 return a.type == b.type && a.radius == b.radius;
37}
38
39/* --------------------------------------------------------------------
40 * Symmetric Separable Blur Weights.
41 */
42
45 float radius)
46 : weights(context.create_result(ResultType::Float))
47{
48 Result weights_cpu = context.create_result(ResultType::Float);
49
50 /* The size of filter is double the radius plus 1, but since the filter is symmetric, we only
51 * compute half of it and no doubling happens. We add 1 to make sure the filter size is always
52 * odd and there is a center weight. */
53 weights_cpu.allocate_texture(
54 Domain(int2(math::ceil(radius) + 1, 1)), 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 /* Second, compute the other weights in the positive direction, making sure to add double the
64 * weight to the sum of weights because the filter is symmetric and we only loop over half of
65 * it. Skip the center weight already computed by dropping the front index. */
66 const float scale = radius > 0.0f ? 1.0f / radius : 0.0f;
67 for (const int i : IndexRange(weights_cpu.domain().data_size.x).drop_front(1)) {
68 const float weight = math::filter_kernel_value(type, i * scale);
69 weights_cpu.store_pixel(int2(i, 0), weight);
70 sum += weight * 2.0f;
71 }
72
73 /* Finally, normalize the weights. */
74 for (const int i : IndexRange(weights_cpu.domain().data_size.x)) {
75 const int2 texel = int2(i, 0);
76 weights_cpu.store_pixel(texel, weights_cpu.load_pixel<float>(texel) / sum);
77 }
78
79 if (context.use_gpu()) {
80 Result weights_gpu = weights_cpu.upload_to_gpu(false);
81 this->weights.share_data(weights_gpu);
82 weights_gpu.release();
83 }
84 else {
85 this->weights.share_data(weights_cpu);
86 }
87
88 weights_cpu.release();
89}
90
95
96/* --------------------------------------------------------------------
97 * Symmetric Separable Blur Weights Container.
98 */
99
101{
102 /* First, delete all resources that are no longer needed. */
103 map_.remove_if([](auto item) { return !item.value->needed; });
104
105 /* Second, reset the needed status of the remaining resources to false to ready them to track
106 * their needed status for the next evaluation. */
107 for (auto &value : map_.values()) {
108 value->needed = false;
109 }
110}
111
114 float radius)
115{
116 const SymmetricSeparableBlurWeightsKey key(type, radius);
117
118 auto &weights = *map_.lookup_or_add_cb(key, [&]() {
119 return std::make_unique<SymmetricSeparableBlurWeights>(context, type, radius);
120 });
121
122 weights.needed = true;
123 return weights.weights;
124}
125
126} // namespace blender::compositor
constexpr IndexRange()=default
Domain(const int2 &size)
Definition domain.cc:20
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, float radius)
SymmetricSeparableBlurWeights(Context &context, math::FilterKernel type, float radius)
float3x3 scale(float3x3 mat, float2 scale)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
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