Blender V5.3
partial_update.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <mutex>
10#include <optional>
11
12#include "IMB_imbuf_types.hh"
13#include "IMB_partial_update.hh"
14
15#include "BLI_bit_vector.hh"
16#include "BLI_math_base_c.hh"
17#include "BLI_mutex.hh"
18#include "BLI_rect.hh"
19#include "BLI_vector.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "atomic_ops.h"
24
25namespace blender {
26
27namespace imbuf::partial_update {
28
35constexpr int MAX_HISTORY_LEN = 4;
36
42static int chunk_number_for_pixel(int pixel_offset)
43{
44 int chunk_offset = pixel_offset / CHUNK_SIZE;
45 if (pixel_offset < 0) {
46 chunk_offset -= 1;
47 }
48 return chunk_offset;
49}
50
52struct Changeset {
53 private:
55 BitVector<> chunk_modified_flags_;
57 bool has_modified_chunks_ = false;
58
59 public:
63 int buffer_width = 0;
67 int chunk_x_len = 0;
69 int chunk_y_len = 0;
70
71 void clear()
72 {
74 }
75
82 bool update_resolution(const ImBuf *image_buffer)
83 {
84 if (buffer_width == image_buffer->x && buffer_height == image_buffer->y) {
85 return false;
86 }
87
88 buffer_width = image_buffer->x;
89 buffer_height = image_buffer->y;
90
91 const int new_chunk_x_len = divide_ceil_u(buffer_width, CHUNK_SIZE);
92 const int new_chunk_y_len = divide_ceil_u(buffer_height, CHUNK_SIZE);
93 init_chunks(new_chunk_x_len, new_chunk_y_len);
94 return true;
95 }
96
97 void mark_region(const rcti &updated_region)
98 {
99 int start_x_chunk = chunk_number_for_pixel(updated_region.xmin);
100 int end_x_chunk = chunk_number_for_pixel(updated_region.xmax - 1);
101 int start_y_chunk = chunk_number_for_pixel(updated_region.ymin);
102 int end_y_chunk = chunk_number_for_pixel(updated_region.ymax - 1);
103
104 /* Clamp to chunks inside the buffer. */
105 start_x_chunk = max_ii(0, start_x_chunk);
106 start_y_chunk = max_ii(0, start_y_chunk);
107 end_x_chunk = min_ii(chunk_x_len - 1, end_x_chunk);
108 end_y_chunk = min_ii(chunk_y_len - 1, end_y_chunk);
109
110 /* Early exit when nothing overlaps the buffer. */
111 if (start_x_chunk >= chunk_x_len || start_y_chunk >= chunk_y_len || end_x_chunk < 0 ||
112 end_y_chunk < 0)
113 {
114 return;
115 }
116
117 mark_chunks_modified(start_x_chunk, start_y_chunk, end_x_chunk, end_y_chunk);
118 }
119
120 void mark_chunks_modified(int start_x_chunk, int start_y_chunk, int end_x_chunk, int end_y_chunk)
121 {
122 for (int chunk_y = start_y_chunk; chunk_y <= end_y_chunk; chunk_y++) {
123 for (int chunk_x = start_x_chunk; chunk_x <= end_x_chunk; chunk_x++) {
124 const int chunk_index = chunk_y * chunk_x_len + chunk_x;
125 chunk_modified_flags_[chunk_index].set();
126 }
127 }
128 has_modified_chunks_ = true;
129 }
130
132 {
133 return has_modified_chunks_;
134 }
135
136 void init_chunks(int chunk_x_len_, int chunk_y_len_)
137 {
138 chunk_x_len = chunk_x_len_;
139 chunk_y_len = chunk_y_len_;
140 const int chunk_len = chunk_x_len * chunk_y_len;
141 const int previous_chunk_len = chunk_modified_flags_.size();
142
143 chunk_modified_flags_.resize(chunk_len);
144 /* Fast exit: when the changeset was already empty there is nothing to reset. */
145 if (!has_modified_chunks()) {
146 return;
147 }
148 for (int index = 0; index < min_ii(chunk_len, previous_chunk_len); index++) {
149 chunk_modified_flags_[index].reset();
150 }
151 has_modified_chunks_ = false;
152 }
153
155 void merge(const Changeset &other)
156 {
159 const int chunk_len = chunk_x_len * chunk_y_len;
160
161 for (int chunk_index = 0; chunk_index < chunk_len; chunk_index++) {
162 chunk_modified_flags_[chunk_index].set(chunk_modified_flags_[chunk_index] ||
163 other.chunk_modified_flags_[chunk_index]);
164 }
165 has_modified_chunks_ |= other.has_modified_chunks_;
166 }
167
169 bool is_modified(int chunk_x, int chunk_y) const
170 {
171 const int chunk_index = chunk_y * chunk_x_len + chunk_x;
172 return chunk_modified_flags_[chunk_index];
173 }
174
176 {
177 return std::move(chunk_modified_flags_);
178 }
179};
180
189
191struct Tracker {
200
205
206 Tracker(const ChangesetID change_id)
207 : first_changeset_id(change_id), last_changeset_id(change_id)
208 {
209 }
210
211 void update_resolution(const ImBuf *image_buffer)
212 {
214
215 if (buffer_width == image_buffer->x && buffer_height == image_buffer->y) {
216 return;
217 }
218
219 const bool had_resolution = buffer_width != 0 || buffer_height != 0;
220 buffer_width = image_buffer->x;
221 buffer_height = image_buffer->y;
222
223 if (had_resolution) {
226 }
227 }
228
236
237 void mark_region(const rcti &updated_region)
238 {
239 current_changeset.mark_region(updated_region);
240 }
241
243 {
245 /* No need to create a new changeset when previous changeset does not contain any
246 * modified chunks. */
247 return;
248 }
251 }
252
261
264 {
265 const int num_items_to_remove = max_ii(history.size() - MAX_HISTORY_LEN, 0);
266 if (num_items_to_remove == 0) {
267 return;
268 }
269 /* Changes up to and including the last removed changeset can no longer be reconstructed. */
270 first_changeset_id = history[num_items_to_remove - 1].changeset_id;
271 history.remove(0, num_items_to_remove);
272 }
273
277 std::optional<Changeset> changed_chunks_since(const ChangesetID from_changeset)
278 {
279 std::optional<Changeset> changed_chunks = std::nullopt;
280 for (const Changeset &changeset : history) {
281 if (changeset.changeset_id <= from_changeset || !changeset.has_modified_chunks()) {
282 continue;
283 }
284 if (!changed_chunks.has_value()) {
285 changed_chunks = std::make_optional<Changeset>();
286 changed_chunks->init_chunks(changeset.chunk_x_len, changeset.chunk_y_len);
287 }
288 changed_chunks->merge(changeset);
289 }
290 return changed_chunks;
291 }
292};
293
301
303{
305
306 Vector<rcti> regions;
307 BitVector<> remaining = modified_chunks;
308
309 for (int start_y = 0; start_y < chunk_y_len; start_y++) {
310 for (int start_x = 0; start_x < chunk_x_len;) {
311 if (!remaining[start_y * chunk_x_len + start_x]) {
312 start_x++;
313 continue;
314 }
315
316 /* Extend right. */
317 int end_x = start_x;
318 while (end_x < chunk_x_len && remaining[start_y * chunk_x_len + end_x]) {
319 end_x++;
320 }
321
322 /* Extend up. */
323 int end_y = start_y + 1;
324 while (end_y < chunk_y_len) {
325 bool row_matches = true;
326 for (int x = start_x; x < end_x; x++) {
327 if (!remaining[end_y * chunk_x_len + x]) {
328 row_matches = false;
329 break;
330 }
331 }
332 if (!row_matches) {
333 break;
334 }
335 end_y++;
336 }
337
338 /* Consume all chunks covered by the region. */
339 for (int y = start_y; y < end_y; y++) {
340 for (int x = start_x; x < end_x; x++) {
341 remaining[y * chunk_x_len + x].reset();
342 }
343 }
344
345 rcti region;
346 BLI_rcti_init(&region,
347 start_x * CHUNK_SIZE,
349 start_y * CHUNK_SIZE,
351 regions.append(region);
352
353 start_x = end_x;
354 }
355 }
356 return regions;
357}
358
360{
361 if (ibuf->partial_update == nullptr) {
362 /* Allocate new changeset ID for new tracker, needed to disambiguate two ImBufs that happened
363 * to be allocated at the same memory address.
364 *
365 * The GPU changeset ID is used if there is already a GPU texture, so GPU texture consumers
366 * do not get unnecessarily updated. */
367 const ChangesetID gpu_changeset_id = ibuf->gpu.partial_update_changeset_id;
368 const ChangesetID changeset_id = gpu_changeset_id >= 0 ?
369 gpu_changeset_id :
371 ibuf->partial_update = MEM_new<Tracker>(__func__, changeset_id);
372 }
373 return *ibuf->partial_update;
374}
375
376} // namespace imbuf::partial_update
377
378using namespace imbuf::partial_update;
379
384static bool imb_track_updates(const ImBuf *ibuf)
385{
386 return ibuf->partial_update != nullptr || ibuf->gpu.texture != nullptr;
387}
388
393
398
399void IMB_partial_update_mark_region(ImBuf *ibuf, const rcti &region)
400{
401 std::scoped_lock lock(ibuf->partial_update_mutex);
402 if (!imb_track_updates(ibuf)) {
403 return;
404 }
405 Tracker &tracker = tracker_ensure(ibuf);
406 tracker.update_resolution(ibuf);
407 tracker.mark_region(region);
408}
409
411{
413
414 std::scoped_lock lock(ibuf->partial_update_mutex);
415 if (!imb_track_updates(ibuf)) {
416 return;
417 }
418 Tracker &tracker = tracker_ensure(ibuf);
419 tracker.mark_full_update();
420}
421
423{
424 ibuf->userflags |= IB_BITMAPDIRTY;
425}
426
428{
429 std::scoped_lock lock(ibuf->partial_update_mutex);
430 Tracker &tracker = tracker_ensure(ibuf);
431 tracker.update_resolution(ibuf);
432 tracker.ensure_empty_changeset();
433}
434
436{
437 std::scoped_lock lock(ibuf->partial_update_mutex);
438 Tracker &tracker = tracker_ensure(ibuf);
439
440 Changes changes;
441 changes.buffer_width = ibuf->x;
442 changes.buffer_height = ibuf->y;
443
444 if (last_changeset_id < tracker.first_changeset_id) {
445 const bool resized = tracker.last_resize_changeset_id != 0 &&
446 last_changeset_id < tracker.last_resize_changeset_id;
447 changes.kind = resized ? Changes::Kind::Resized : Changes::Kind::Full;
448 return changes;
449 }
450
451 if (last_changeset_id >= tracker.last_changeset_id) {
452 changes.kind = Changes::Kind::None;
453 return changes;
454 }
455
456 std::optional<Changeset> changed = tracker.changed_chunks_since(last_changeset_id);
457 if (!changed.has_value() || !changed->has_modified_chunks()) {
458 changes.kind = Changes::Kind::None;
459 return changes;
460 }
461
462 changes.kind = Changes::Kind::Partial;
463 changes.chunk_x_len = changed->chunk_x_len;
464 changes.chunk_y_len = changed->chunk_y_len;
465 changes.modified_chunks = changed->extract_modified_flags();
466 return changes;
467}
468
470{
471 if (ibuf->partial_update != nullptr) {
472 MEM_delete(ibuf->partial_update);
473 ibuf->partial_update = nullptr;
474 }
475}
476
477} // namespace blender
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE uint64_t atomic_load_uint64(const uint64_t *v)
ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
volatile int lock
int64_t size() const
void append(const T &value)
void remove(const int64_t index)
void append_as(ForwardValue &&...value)
void resize(const int64_t new_size_in_bits, const bool value=false)
void fill(const bool value)
static int chunk_number_for_pixel(int pixel_offset)
static Tracker & tracker_ensure(ImBuf *ibuf)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
void IMB_mark_dirty(ImBuf *ibuf)
MINLINE int max_ii(int a, int b)
imbuf::ChangesetID IMB_partial_update_changeset_id_current()
void IMB_partial_update_free(ImBuf *ibuf)
static bool imb_track_updates(const ImBuf *ibuf)
void IMB_partial_update_mark_region(ImBuf *ibuf, const rcti &region)
void IMB_partial_update_mark_full(ImBuf *ibuf)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
imbuf::ChangesetID IMB_partial_update_changeset_id_next()
MINLINE uint divide_ceil_u(uint a, uint b)
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
void IMB_partial_update_flush(ImBuf *ibuf)
MINLINE int min_ii(int a, int b)
std::atomic< imbuf::ChangesetID > partial_update_changeset_id
std::atomic< gpu::Texture * > texture
std::atomic< imbuf::ChangesetID > full_update_changeset_id
imbuf::partial_update::Tracker * partial_update
void mark_region(const rcti &updated_region)
void init_chunks(int chunk_x_len_, int chunk_y_len_)
bool is_modified(int chunk_x, int chunk_y) const
bool update_resolution(const ImBuf *image_buffer)
void mark_chunks_modified(int start_x_chunk, int start_y_chunk, int end_x_chunk, int end_y_chunk)
Tracker(const ChangesetID change_id)
void update_resolution(const ImBuf *image_buffer)
void commit_current_changeset()
Move the current changeset to the history and resets the current changeset.
void mark_region(const rcti &updated_region)
std::optional< Changeset > changed_chunks_since(const ChangesetID from_changeset)