Blender
V4.5
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_array.hh
"
9
#include "
BLI_hash.hh
"
10
#include "
BLI_index_range.hh
"
11
#include "
BLI_math_vector.hh
"
12
#include "
BLI_math_vector_types.hh
"
13
14
#include "
RE_pipeline.h
"
15
16
#include "
GPU_texture.hh
"
17
18
#include "
COM_context.hh
"
19
#include "
COM_result.hh
"
20
#include "
COM_symmetric_blur_weights.hh
"
21
22
namespace
blender::compositor
{
23
24
/* --------------------------------------------------------------------
25
* Symmetric Blur Weights Key.
26
*/
27
28
SymmetricBlurWeightsKey::SymmetricBlurWeightsKey
(
int
type
,
float2
radius
)
29
:
type
(
type
),
radius
(
radius
)
30
{
31
}
32
33
uint64_t
SymmetricBlurWeightsKey::hash
()
const
34
{
35
return
get_default_hash
(
type
,
radius
.x,
radius
.y);
36
}
37
38
bool
operator==
(
const
SymmetricBlurWeightsKey
&a,
const
SymmetricBlurWeightsKey
&
b
)
39
{
40
return
a.
type
==
b
.type && a.
radius
==
b
.radius;
41
}
42
43
/* --------------------------------------------------------------------
44
* Symmetric Blur Weights.
45
*/
46
47
SymmetricBlurWeights::SymmetricBlurWeights
(
Context
&context,
int
type,
float2
radius)
48
:
result
(context.create_result(
ResultType
::
Float
))
49
{
50
/* The full size of filter is double the radius plus 1, but since the filter is symmetric, we
51
* only compute a single quadrant of it and so no doubling happens. We add 1 to make sure the
52
* filter size is always odd and there is a center weight. */
53
const
float2
scale =
math::safe_divide
(
float2
(1.0f), radius);
54
const
int2
size
=
int2
(
math::ceil
(radius)) +
int2
(1);
55
weights_ =
Array<float>
(
size
.x *
size
.y);
56
57
float
sum
= 0.0f;
58
59
/* First, compute the center weight. */
60
const
float
center_weight =
RE_filter_value
(type, 0.0f);
61
weights_[0] = center_weight;
62
sum
+= center_weight;
63
64
/* Then, compute the weights along the positive x axis, making sure to add double the weight to
65
* the sum of weights because the filter is symmetric and we only loop over the positive half
66
* of the x axis. Skip the center weight already computed by dropping the front index. */
67
for
(
const
int
x
:
IndexRange
(
size
.x).
drop_front
(1)) {
68
const
float
weight =
RE_filter_value
(type,
x
* scale.x);
69
weights_[
x
] = weight;
70
sum
+= weight * 2.0f;
71
}
72
73
/* Then, compute the weights along the positive y axis, making sure to add double the weight to
74
* the sum of weights because the filter is symmetric and we only loop over the positive half
75
* of the y axis. Skip the center weight already computed by dropping the front index. */
76
for
(
const
int
y
:
IndexRange
(
size
.y).
drop_front
(1)) {
77
const
float
weight =
RE_filter_value
(type,
y
* scale.y);
78
weights_[
size
.x *
y
] = weight;
79
sum
+= weight * 2.0f;
80
}
81
82
/* Then, compute the other weights in the upper right quadrant, making sure to add quadruple
83
* the weight to the sum of weights because the filter is symmetric and we only loop over one
84
* quadrant of it. Skip the weights along the y and x axis already computed by dropping the
85
* front index. */
86
for
(
const
int
y
:
IndexRange
(
size
.y).
drop_front
(1)) {
87
for
(
const
int
x
:
IndexRange
(
size
.x).
drop_front
(1)) {
88
const
float
weight =
RE_filter_value
(type,
math::length
(
float2
(
x
,
y
) * scale));
89
weights_[
size
.x *
y
+
x
] = weight;
90
sum
+= weight * 4.0f;
91
}
92
}
93
94
/* Finally, normalize the weights. */
95
for
(
const
int
y
:
IndexRange
(
size
.y)) {
96
for
(
const
int
x
:
IndexRange
(
size
.x)) {
97
weights_[
size
.x *
y
+
x
] /=
sum
;
98
}
99
}
100
101
if
(context.use_gpu()) {
102
this->
result
.allocate_texture(
Domain
(
size
),
false
);
103
GPU_texture_update
(this->
result
,
GPU_DATA_FLOAT
, weights_.data());
104
105
/* CPU-side data no longer needed, so free it. */
106
weights_ =
Array<float>
();
107
}
108
else
{
109
this->
result
.wrap_external(weights_.data(),
size
);
110
}
111
}
112
113
SymmetricBlurWeights::~SymmetricBlurWeights
()
114
{
115
this->
result
.release();
116
}
117
118
/* --------------------------------------------------------------------
119
* Symmetric Blur Weights Container.
120
*/
121
122
void
SymmetricBlurWeightsContainer::reset
()
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
134
Result
&
SymmetricBlurWeightsContainer::get
(
Context
&context,
int
type,
float2
radius)
135
{
136
const
SymmetricBlurWeightsKey
key(type, radius);
137
138
auto
&weights = *map_.lookup_or_add_cb(
139
key, [&]() {
return
std::make_unique<SymmetricBlurWeights>(context, type, radius); });
140
141
weights.needed =
true
;
142
return
weights.result;
143
}
144
145
}
// namespace blender::compositor
BLI_array.hh
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
GPU_texture.hh
GPU_DATA_FLOAT
@ GPU_DATA_FLOAT
Definition
GPU_texture.hh:744
GPU_texture_update
void GPU_texture_update(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
Definition
gpu_texture.cc:546
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::Array
Definition
BLI_array.hh:50
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::Domain
Definition
COM_domain.hh:135
blender::compositor::Result
Definition
COM_result.hh:95
blender::compositor::SymmetricBlurWeightsContainer::reset
void reset() override
Definition
symmetric_blur_weights.cc:122
blender::compositor::SymmetricBlurWeightsContainer::get
Result & get(Context &context, int type, float2 radius)
Definition
symmetric_blur_weights.cc:134
blender::compositor::SymmetricBlurWeightsKey
Definition
COM_symmetric_blur_weights.hh:24
blender::compositor::SymmetricBlurWeightsKey::radius
float2 radius
Definition
COM_symmetric_blur_weights.hh:27
blender::compositor::SymmetricBlurWeightsKey::SymmetricBlurWeightsKey
SymmetricBlurWeightsKey(int type, float2 radius)
Definition
symmetric_blur_weights.cc:28
blender::compositor::SymmetricBlurWeightsKey::type
int type
Definition
COM_symmetric_blur_weights.hh:26
blender::compositor::SymmetricBlurWeightsKey::hash
uint64_t hash() const
Definition
symmetric_blur_weights.cc:33
blender::compositor::SymmetricBlurWeights::result
Result result
Definition
COM_symmetric_blur_weights.hh:48
blender::compositor::SymmetricBlurWeights::SymmetricBlurWeights
SymmetricBlurWeights(Context &context, int type, float2 radius)
Definition
symmetric_blur_weights.cc:47
blender::compositor::SymmetricBlurWeights::~SymmetricBlurWeights
~SymmetricBlurWeights()
Definition
symmetric_blur_weights.cc:113
y
y
Definition
compositor_morphological_blur_info.hh:22
b
b
Definition
compositor_morphological_distance_info.hh:24
RE_filter_value
float RE_filter_value(int type, float x)
Definition
initrender.cc:105
blender::compositor
Definition
BKE_node.hh:76
blender::compositor::operator==
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
Definition
bokeh_kernel.cc:44
blender::compositor::ResultType
ResultType
Definition
COM_result.hh:36
blender::compositor::ResultType::Float
@ Float
Definition
COM_result.hh:37
blender::math::safe_divide
T safe_divide(const T &a, const T &b)
Definition
BLI_math_base.hh:97
blender::math::length
T length(const VecBase< T, Size > &a)
Definition
BLI_math_vector.hh:428
blender::math::ceil
T ceil(const T &a)
Definition
BLI_math_base.hh:128
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.15.0