Blender
V5.0
source
blender
compositor
cached_resources
intern
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
"
11
#include "
BLI_math_vector_types.hh
"
12
13
#include "
RE_pipeline.h
"
14
15
#include "
COM_context.hh
"
16
#include "
COM_result.hh
"
17
#include "
COM_symmetric_blur_weights.hh
"
18
19
namespace
blender::compositor
{
20
21
/* --------------------------------------------------------------------
22
* Symmetric Blur Weights Key.
23
*/
24
25
SymmetricBlurWeightsKey::SymmetricBlurWeightsKey
(
int
type
,
float2
radius
)
26
:
type
(
type
),
radius
(
radius
)
27
{
28
}
29
30
uint64_t
SymmetricBlurWeightsKey::hash
()
const
31
{
32
return
get_default_hash
(
type
,
radius
.x,
radius
.y);
33
}
34
35
bool
operator==
(
const
SymmetricBlurWeightsKey
&a,
const
SymmetricBlurWeightsKey
&
b
)
36
{
37
return
a.
type
==
b
.type && a.
radius
==
b
.radius;
38
}
39
40
/* --------------------------------------------------------------------
41
* Symmetric Blur Weights.
42
*/
43
44
SymmetricBlurWeights::SymmetricBlurWeights
(
Context
&context,
int
type,
float2
radius)
45
:
result
(context.create_result(
ResultType
::
Float
))
46
{
47
/* The full size of filter is double the radius plus 1, but since the filter is symmetric, we
48
* only compute a single quadrant of it and so no doubling happens. We add 1 to make sure the
49
* filter size is always odd and there is a center weight. */
50
const
float2
scale =
math::safe_divide
(
float2
(1.0f), radius);
51
const
int2
size
=
int2
(
math::ceil
(radius)) +
int2
(1);
52
this->
result
.allocate_texture(
size
,
false
,
ResultStorageType::CPU
);
53
54
float
sum
= 0.0f;
55
56
/* First, compute the center weight. */
57
const
float
center_weight =
RE_filter_value
(type, 0.0f);
58
this->
result
.store_pixel(
int2
(0, 0), center_weight);
59
sum
+= center_weight;
60
61
/* Then, compute the weights along the positive x axis, making sure to add double the weight to
62
* the sum of weights because the filter is symmetric and we only loop over the positive half
63
* of the x axis. Skip the center weight already computed by dropping the front index. */
64
for
(
const
int
x
:
IndexRange
(
size
.x).
drop_front
(1)) {
65
const
float
weight =
RE_filter_value
(type,
x
* scale.x);
66
this->
result
.store_pixel(
int2
(
x
, 0), weight);
67
sum
+= weight * 2.0f;
68
}
69
70
/* Then, compute the weights along the positive y axis, making sure to add double the weight to
71
* the sum of weights because the filter is symmetric and we only loop over the positive half
72
* of the y axis. Skip the center weight already computed by dropping the front index. */
73
for
(
const
int
y
:
IndexRange
(
size
.y).
drop_front
(1)) {
74
const
float
weight =
RE_filter_value
(type,
y
* scale.y);
75
this->
result
.store_pixel(
int2
(0,
y
), weight);
76
sum
+= weight * 2.0f;
77
}
78
79
/* Then, compute the other weights in the upper right quadrant, making sure to add quadruple
80
* the weight to the sum of weights because the filter is symmetric and we only loop over one
81
* quadrant of it. Skip the weights along the y and x axis already computed by dropping the
82
* front index. */
83
for
(
const
int
y
:
IndexRange
(
size
.y).
drop_front
(1)) {
84
for
(
const
int
x
:
IndexRange
(
size
.x).
drop_front
(1)) {
85
const
float
weight =
RE_filter_value
(type,
math::length
(
float2
(
x
,
y
) * scale));
86
this->
result
.store_pixel(
int2
(
x
,
y
), weight);
87
sum
+= weight * 4.0f;
88
}
89
}
90
91
/* Finally, normalize the weights. */
92
for
(
const
int
y
:
IndexRange
(
size
.y)) {
93
for
(
const
int
x
:
IndexRange
(
size
.x)) {
94
const
int2
texel =
int2
(
x
,
y
);
95
this->
result
.store_pixel(texel, this->
result
.load_pixel<
float
>(texel) /
sum
);
96
}
97
}
98
99
if
(context.use_gpu()) {
100
const
Result
gpu_result = this->
result
.upload_to_gpu(
false
);
101
this->
result
.release();
102
this->
result
= gpu_result;
103
}
104
}
105
106
SymmetricBlurWeights::~SymmetricBlurWeights
()
107
{
108
this->
result
.release();
109
}
110
111
/* --------------------------------------------------------------------
112
* Symmetric Blur Weights Container.
113
*/
114
115
void
SymmetricBlurWeightsContainer::reset
()
116
{
117
/* First, delete all resources that are no longer needed. */
118
map_.remove_if([](
auto
item) {
return
!item.value->needed; });
119
120
/* Second, reset the needed status of the remaining resources to false to ready them to track
121
* their needed status for the next evaluation. */
122
for
(
auto
&value : map_.values()) {
123
value->needed =
false
;
124
}
125
}
126
127
Result
&
SymmetricBlurWeightsContainer::get
(
Context
&context,
int
type,
float2
radius)
128
{
129
const
SymmetricBlurWeightsKey
key(type, radius);
130
131
auto
&weights = *map_.lookup_or_add_cb(
132
key, [&]() {
return
std::make_unique<SymmetricBlurWeights>(context, type, radius); });
133
134
weights.needed =
true
;
135
return
weights.result;
136
}
137
138
}
// namespace blender::compositor
x
x
Definition
BLI_expr_pylike_eval_test.cc:345
BLI_hash.hh
BLI_index_range.hh
BLI_math_vector.hh
BLI_math_vector_types.hh
COM_context.hh
COM_result.hh
COM_symmetric_blur_weights.hh
RE_pipeline.h
uint64_t
unsigned long long int uint64_t
Definition
btConvexHullComputer.cpp:33
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition
btDbvt.cpp:52
sum
static T sum(const btAlignedObjectArray< T > &items)
Definition
btSoftBodyHelpers.cpp:94
blender::IndexRange
Definition
BLI_index_range.hh:50
blender::IndexRange::drop_front
constexpr IndexRange drop_front(int64_t n) const
Definition
BLI_index_range.hh:302
blender::compositor::Context
Definition
COM_context.hh:44
blender::compositor::Result
Definition
COM_result.hh:100
blender::compositor::SymmetricBlurWeightsContainer::reset
void reset() override
Definition
symmetric_blur_weights.cc:115
blender::compositor::SymmetricBlurWeightsContainer::get
Result & get(Context &context, int type, float2 radius)
Definition
symmetric_blur_weights.cc:127
blender::compositor::SymmetricBlurWeightsKey
Definition
COM_symmetric_blur_weights.hh:23
blender::compositor::SymmetricBlurWeightsKey::radius
float2 radius
Definition
COM_symmetric_blur_weights.hh:26
blender::compositor::SymmetricBlurWeightsKey::SymmetricBlurWeightsKey
SymmetricBlurWeightsKey(int type, float2 radius)
Definition
symmetric_blur_weights.cc:25
blender::compositor::SymmetricBlurWeightsKey::type
int type
Definition
COM_symmetric_blur_weights.hh:25
blender::compositor::SymmetricBlurWeightsKey::hash
uint64_t hash() const
Definition
symmetric_blur_weights.cc:30
blender::compositor::SymmetricBlurWeights::result
Result result
Definition
COM_symmetric_blur_weights.hh:44
blender::compositor::SymmetricBlurWeights::SymmetricBlurWeights
SymmetricBlurWeights(Context &context, int type, float2 radius)
Definition
symmetric_blur_weights.cc:44
blender::compositor::SymmetricBlurWeights::~SymmetricBlurWeights
~SymmetricBlurWeights()
Definition
symmetric_blur_weights.cc:106
y
y
Definition
compositor_morphological_blur_infos.hh:22
b
b
Definition
compositor_morphological_distance_infos.hh:24
RE_filter_value
float RE_filter_value(int type, float x)
Definition
initrender.cc:105
blender::compositor
Definition
BKE_node.hh:77
blender::compositor::ResultStorageType::CPU
@ CPU
Definition
COM_result.hh:63
blender::compositor::operator==
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
Definition
bokeh_kernel.cc:44
blender::compositor::ResultType
ResultType
Definition
COM_result.hh:37
blender::compositor::ResultType::Float
@ Float
Definition
COM_result.hh:38
blender::math::safe_divide
T safe_divide(const T &a, const T &b)
Definition
BLI_math_base.hh:107
blender::math::length
T length(const VecBase< T, Size > &a)
Definition
BLI_math_vector.hh:451
blender::math::ceil
T ceil(const T &a)
Definition
BLI_math_base.hh:138
blender::get_default_hash
uint64_t get_default_hash(const T &v, const Args &...args)
Definition
BLI_hash.hh:233
blender::int2
VecBase< int32_t, 2 > int2
Definition
BLI_math_vector_types.hh:601
blender::float2
VecBase< float, 2 > float2
Definition
BLI_math_vector_types.hh:618
Generated on
for Blender by
doxygen
1.16.1