Blender
V4.5
source
blender
compositor
cached_resources
intern
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_array.hh
"
9
#include "
BLI_hash.hh
"
10
#include "
BLI_index_range.hh
"
11
#include "
BLI_math_base.hh
"
12
13
#include "
RE_pipeline.h
"
14
15
#include "
GPU_texture.hh
"
16
17
#include "
COM_context.hh
"
18
#include "
COM_result.hh
"
19
#include "
COM_symmetric_separable_blur_weights.hh
"
20
21
namespace
blender::compositor
{
22
23
/* --------------------------------------------------------------------
24
* Symmetric Separable Blur Weights Key.
25
*/
26
27
SymmetricSeparableBlurWeightsKey::SymmetricSeparableBlurWeightsKey
(
int
type
,
float
radius
)
28
:
type
(
type
),
radius
(
radius
)
29
{
30
}
31
32
uint64_t
SymmetricSeparableBlurWeightsKey::hash
()
const
33
{
34
return
get_default_hash
(
type
,
radius
);
35
}
36
37
bool
operator==
(
const
SymmetricSeparableBlurWeightsKey
&a,
38
const
SymmetricSeparableBlurWeightsKey
&
b
)
39
{
40
return
a.
type
==
b
.type && a.
radius
==
b
.radius;
41
}
42
43
/* --------------------------------------------------------------------
44
* Symmetric Separable Blur Weights.
45
*/
46
47
SymmetricSeparableBlurWeights::SymmetricSeparableBlurWeights
(
Context
&context,
48
int
type,
49
float
radius)
50
:
result
(context.create_result(
ResultType
::
Float
))
51
{
52
/* The size of filter is double the radius plus 1, but since the filter is symmetric, we only
53
* compute half of it and no doubling happens. We add 1 to make sure the filter size is always
54
* odd and there is a center weight. */
55
const
int
size
=
math::ceil
(radius) + 1;
56
weights_ =
Array<float>
(
size
);
57
58
float
sum
= 0.0f;
59
60
/* First, compute the center weight. */
61
const
float
center_weight =
RE_filter_value
(type, 0.0f);
62
weights_[0] = center_weight;
63
sum
+= center_weight;
64
65
/* Second, compute the other weights in the positive direction, making sure to add double the
66
* weight to the sum of weights because the filter is symmetric and we only loop over half of
67
* it. Skip the center weight already computed by dropping the front index. */
68
const
float
scale = radius > 0.0f ? 1.0f / radius : 0.0f;
69
for
(
const
int
i
: weights_.index_range().drop_front(1)) {
70
const
float
weight =
RE_filter_value
(type,
i
* scale);
71
weights_[
i
] = weight;
72
sum
+= weight * 2.0f;
73
}
74
75
/* Finally, normalize the weights. */
76
for
(
const
int
i
: weights_.index_range()) {
77
weights_[
i
] /=
sum
;
78
}
79
80
if
(context.use_gpu()) {
81
this->
result
.allocate_texture(
Domain
(
int2
(
size
, 1)),
false
);
82
GPU_texture_update
(this->
result
,
GPU_DATA_FLOAT
, weights_.data());
83
84
/* CPU-side data no longer needed, so free it. */
85
weights_ =
Array<float>
();
86
}
87
else
{
88
this->
result
.wrap_external(weights_.data(),
int2
(
size
, 1));
89
}
90
}
91
92
SymmetricSeparableBlurWeights::~SymmetricSeparableBlurWeights
()
93
{
94
this->
result
.release();
95
}
96
97
/* --------------------------------------------------------------------
98
* Symmetric Separable Blur Weights Container.
99
*/
100
101
void
SymmetricSeparableBlurWeightsContainer::reset
()
102
{
103
/* First, delete all resources that are no longer needed. */
104
map_.remove_if([](
auto
item) {
return
!item.value->needed; });
105
106
/* Second, reset the needed status of the remaining resources to false to ready them to track
107
* their needed status for the next evaluation. */
108
for
(
auto
&value : map_.values()) {
109
value->needed =
false
;
110
}
111
}
112
113
Result
&
SymmetricSeparableBlurWeightsContainer::get
(
Context
&context,
int
type,
float
radius)
114
{
115
const
SymmetricSeparableBlurWeightsKey
key(type, radius);
116
117
auto
&weights = *map_.lookup_or_add_cb(key, [&]() {
118
return
std::make_unique<SymmetricSeparableBlurWeights>(context, type, radius);
119
});
120
121
weights.needed =
true
;
122
return
weights.result;
123
}
124
125
}
// namespace blender::compositor
BLI_array.hh
BLI_hash.hh
BLI_index_range.hh
BLI_math_base.hh
COM_context.hh
COM_result.hh
COM_symmetric_separable_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::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::SymmetricSeparableBlurWeightsContainer::reset
void reset() override
Definition
symmetric_separable_blur_weights.cc:101
blender::compositor::SymmetricSeparableBlurWeightsContainer::get
Result & get(Context &context, int type, float radius)
Definition
symmetric_separable_blur_weights.cc:113
blender::compositor::SymmetricSeparableBlurWeightsKey
Definition
COM_symmetric_separable_blur_weights.hh:24
blender::compositor::SymmetricSeparableBlurWeightsKey::radius
float radius
Definition
COM_symmetric_separable_blur_weights.hh:27
blender::compositor::SymmetricSeparableBlurWeightsKey::SymmetricSeparableBlurWeightsKey
SymmetricSeparableBlurWeightsKey(int type, float radius)
Definition
symmetric_separable_blur_weights.cc:27
blender::compositor::SymmetricSeparableBlurWeightsKey::type
int type
Definition
COM_symmetric_separable_blur_weights.hh:26
blender::compositor::SymmetricSeparableBlurWeightsKey::hash
uint64_t hash() const
Definition
symmetric_separable_blur_weights.cc:32
blender::compositor::SymmetricSeparableBlurWeights::SymmetricSeparableBlurWeights
SymmetricSeparableBlurWeights(Context &context, int type, float radius)
Definition
symmetric_separable_blur_weights.cc:47
blender::compositor::SymmetricSeparableBlurWeights::~SymmetricSeparableBlurWeights
~SymmetricSeparableBlurWeights()
Definition
symmetric_separable_blur_weights.cc:92
blender::compositor::SymmetricSeparableBlurWeights::result
Result result
Definition
COM_symmetric_separable_blur_weights.hh:53
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::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
i
i
Definition
text_draw.cc:230
Generated on
for Blender by
doxygen
1.15.0