Blender V5.3
cached_mask.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 <memory>
6
7#include "BLI_math_color_c.hh"
9
10#include "BKE_lib_id.hh"
11#include "BKE_mask.hh"
12
13#include "DNA_ID.h"
14#include "DNA_mask_types.h"
15
16#include "COM_cached_mask.hh"
17#include "COM_context.hh"
18#include "COM_result.hh"
19#include "COM_utilities.hh"
20
21namespace blender::compositor {
22
23/* --------------------------------------------------------------------
24 * Cached Mask Key.
25 */
26
45
52
53/* --------------------------------------------------------------------
54 * Cached Mask.
55 */
56
58 int2 size,
59 int frame,
60 bool frame_is_current,
61 bool use_feather,
62 int motion_blur_samples,
63 float motion_blur_shutter)
64{
66
67 if (!mask) {
68 return handles;
69 }
70
71 /* If motion blur samples are 1 (no motion blur) and frame is current, we can just use currently
72 * evaluated mask. */
73 if (motion_blur_samples == 1 && frame_is_current) {
75 BKE_maskrasterize_handle_init(handle, mask, size.x, size.y, true, true, use_feather);
76 handles.append(handle);
77 return handles;
78 }
79
80 /* Otherwise, we have a number of motion blur samples or non-current frame, so make a copy of the
81 * Mask ID and evaluate it at the needed frames to get the needed raster handles. */
82 Mask *evaluation_mask = reinterpret_cast<Mask *>(
84
85 /* We evaluate at the frames in the range [current_frame - shutter, current_frame + shutter]. */
86 const float start_frame = frame - motion_blur_shutter;
87 const float frame_step = (motion_blur_shutter * 2.0f) / motion_blur_samples;
88 for (int i = 0; i < motion_blur_samples; i++) {
90 BKE_mask_evaluate(evaluation_mask, start_frame + frame_step * i, true);
92 handle, evaluation_mask, size.x, size.y, true, true, use_feather);
93 handles.append(handle);
94 }
95
96 BKE_id_free(nullptr, &evaluation_mask->id);
97
98 return handles;
99}
100
102 Mask *mask,
103 const Domain &domain,
104 int frame,
105 float aspect_ratio,
106 bool use_feather,
107 int motion_blur_samples,
108 float motion_blur_shutter,
109 bool srgb_to_linear)
110 : result(context.create_result(ResultType::Float))
111{
112 const bool frame_is_current = context.get_frame_number() == frame;
114 domain.display_size,
115 frame,
116 frame_is_current,
117 use_feather,
118 motion_blur_samples,
119 motion_blur_shutter);
120
121 Result result_cpu = context.create_result(ResultType::Float);
122
123 result_cpu.allocate_texture(domain, false, ResultStorageType::CPUImage);
124 parallel_for(domain.data_size, [&](const int2 texel) {
125 /* Compute the coordinates in the [0, 1] range and add 0.5 to evaluate the mask at the
126 * center of pixels. */
127 float2 coordinates = (float2(texel + domain.data_offset) + 0.5f) / float2(domain.display_size);
128 /* Do aspect ratio correction around the center 0.5 point. */
129 coordinates = (coordinates - float2(0.5)) * float2(1.0, aspect_ratio) + float2(0.5);
130
131 float mask_value = 0.0f;
132 for (MaskRasterHandle *handle : handles) {
133 mask_value += BKE_maskrasterize_handle_sample(handle, coordinates);
134 }
135 mask_value /= handles.size();
136 if (srgb_to_linear) {
137 mask_value = srgb_to_linearrgb(mask_value);
138 }
139 result_cpu.store_pixel(texel, mask_value);
140 });
141
142 for (MaskRasterHandle *handle : handles) {
144 }
145
146 if (context.use_gpu()) {
147 Result result_gpu = result_cpu.upload_to_gpu(false);
148 this->result.share_data(result_gpu);
149 result_gpu.release();
150 }
151 else {
152 this->result.share_data(result_cpu);
153 }
154
155 result_cpu.release();
156}
157
159{
160 this->result.release();
161}
162
163/* --------------------------------------------------------------------
164 * Cached Mask Container.
165 */
166
168{
169 /* First, delete all cached masks that are no longer needed. */
170 for (auto &cached_masks_for_id : map_.values()) {
171 cached_masks_for_id.remove_if([](auto item) { return !item.value->needed; });
172 }
173 map_.remove_if([](auto item) { return item.value.is_empty(); });
174 update_counts_.remove_if([&](auto item) { return !map_.contains(item.key); });
175
176 /* Second, reset the needed status of the remaining cached masks to false to ready them to track
177 * their needed status for the next evaluation. */
178 for (auto &cached_masks_for_id : map_.values()) {
179 for (auto &value : cached_masks_for_id.values()) {
180 value->needed = false;
181 }
182 }
183}
184
186 Mask *mask,
187 const Domain &domain,
188 float aspect_ratio,
189 bool use_feather,
190 int frame,
191 int motion_blur_samples,
192 float motion_blur_shutter,
193 bool srgb_to_linear)
194{
195 const CachedMaskKey key(domain,
196 aspect_ratio,
197 use_feather,
198 srgb_to_linear,
199 frame,
200 motion_blur_samples,
201 motion_blur_shutter);
202
203 const std::string library_key = mask->id.lib ? mask->id.lib->id.name : "";
204 const std::string id_key = std::string(mask->id.name) + library_key;
205 auto &cached_masks_for_id = map_.lookup_or_add_default(id_key);
206
207 /* Invalidate the cache for that mask if it was changed since it was cached. */
208 if (!cached_masks_for_id.is_empty() &&
209 mask->runtime.last_update != update_counts_.lookup(id_key))
210 {
211 cached_masks_for_id.clear();
212 }
213
214 auto &cached_mask = *cached_masks_for_id.lookup_or_add_cb(key, [&]() {
215 return std::make_unique<CachedMask>(context,
216 mask,
217 domain,
218 frame,
219 aspect_ratio,
220 use_feather,
221 motion_blur_samples,
222 motion_blur_shutter,
223 srgb_to_linear);
224 });
225
226 /* Store the current update count to later compare to and check if the mask changed. */
227 update_counts_.add_overwrite(id_key, mask->runtime.last_update);
228
229 cached_mask.needed = true;
230 return cached_mask.result;
231}
232
233} // namespace blender::compositor
ID and Library types, which are fundamental for SDNA.
Result & get(Context &context, Mask *mask, const Domain &domain, float aspect_ratio, bool use_feather, int frame, int motion_blur_samples, float motion_blur_shutter, bool srgb_to_linear)
CachedMaskKey(const Domain &domain, float aspect_ratio, bool use_feather, bool srgb_to_linear, int frame, int motion_blur_samples, float motion_blur_shutter)
CachedMask(Context &context, Mask *mask, const Domain &domain, int frame, float aspect_ratio, bool use_feather, int motion_blur_samples, float motion_blur_shutter, bool srgb_to_linear)
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
static Vector< MaskRasterHandle * > get_mask_raster_handles(Mask *mask, int2 size, int frame, bool frame_is_current, bool use_feather, int motion_blur_samples, float motion_blur_shutter)
void parallel_for(const int2 range, const Function &function)
unsigned __int64 uint64_t
Definition BKE_text.h:91
MaskRasterHandle * BKE_maskrasterize_handle_new()
float srgb_to_linearrgb(float c)
void BKE_mask_evaluate(Mask *mask, float ctime, bool do_newframe)
@ LIB_ID_COPY_LOCALIZE
@ LIB_ID_COPY_NO_ANIMDATA
void BKE_maskrasterize_handle_init(MaskRasterHandle *mr_handle, Mask *mask, int width, int height, bool do_aspect_correct, bool do_mask_aa, bool do_feather)
void BKE_maskrasterize_handle_free(MaskRasterHandle *mr_handle)
void BKE_id_free(Main *bmain, void *idv)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
int size() const
Definition vector.hh:130