Blender V5.3
image_gpu.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <mutex>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_boxpack_2d.hh"
14#include "BLI_fileops.hh"
15#include "BLI_listbase.hh"
16#include "BLI_math_base.hh"
17#include "BLI_math_base_c.hh"
19#include "BLI_path_utils.hh"
20#include "BLI_string.hh"
21#include "BLI_time.hh"
22
23#include "DNA_image_types.h"
24
25#include "IMB_cache.hh"
26#include "IMB_imbuf.hh"
27#include "IMB_imbuf_types.hh"
28
29#include "BKE_image.hh"
30#include "BKE_image_gpu.hh"
31#include "BKE_main.hh"
32
33#include "IMB_partial_update.hh"
34
35#include "GPU_capabilities.hh"
36#include "GPU_texture.hh"
37
38#include "CLG_log.h"
39
40#include "image_intern.hh"
41
42namespace blender {
43
44static CLG_LogRef LOG = {"image.gpu"};
45
47
48/* -------------------------------------------------------------------- */
51
53{
54 if (image->runtime->cache == nullptr) {
55 return nullptr;
56 }
57 ImBuf *ibuf = IMB_cache_get(image->runtime->cache, &key, nullptr);
58 if (ibuf != nullptr) {
60 }
61 return ibuf;
62}
63
64static ImBuf *image_udim_gpu_ibuf_get(Image *ima, const int udim_index)
65{
66 return image_udim_gpu_cache_get(ima, ImageCacheKey{.index = udim_index});
67}
68
69static ImBuf *image_udim_gpu_ibuf_ensure(Image *ima, const int udim_index)
70{
71 ImageCacheKey key = {.index = udim_index};
72 ImBuf *ibuf = image_udim_gpu_cache_get(ima, key);
73 if (ibuf == nullptr) {
74 ibuf = IMB_allocImBuf(1, 1, ImBufFlags::Zero);
75 imagecache_put(ima, key, ibuf);
76 }
77 return ibuf;
78}
79
80static void image_udim_gpu_ibuf_remove(Image *ima, const int udim_index)
81{
82 if (ima->runtime->cache == nullptr) {
83 return;
84 }
85 ImageCacheKey key{.index = udim_index};
86 IMB_cache_remove(ima->runtime->cache, &key);
87}
88
90
91/* -------------------------------------------------------------------- */
94
96{
97 if (ima->runtime->cache == nullptr) {
98 return false;
99 }
100
101 std::scoped_lock lock(ima->runtime->cache_mutex);
102 ImBufCacheIter *iter = IMB_cacheIter_new(ima->runtime->cache);
103 bool found = false;
104 while (!IMB_cacheIter_done(iter)) {
105 ImBuf *ibuf = IMB_cacheIter_getImBuf(iter);
106 if (ibuf != nullptr && ibuf->gpu.texture != nullptr) {
107 found = true;
108 break;
109 }
110 IMB_cacheIter_step(iter);
111 }
112 IMB_cacheIter_free(iter);
113 return found;
114}
115
117{
118 if (image) {
119 /* Render result and compositor output are always premultiplied */
121 return true;
122 }
123 /* Generated images use pre multiplied float buffer, but straight alpha for byte buffers. */
124 if (image->type == IMA_TYPE_UV_TEST && ibuf) {
125 return ibuf->float_data() != nullptr;
126 }
127 }
128 if (ibuf) {
129 if (ibuf->float_data()) {
130 return image ? (image->alpha_mode != IMA_ALPHA_STRAIGHT) : false;
131 }
132
133 return image ? (image->alpha_mode == IMA_ALPHA_PREMUL) : true;
134 }
135 return false;
136}
137
139
140/* -------------------------------------------------------------------- */
143
144static bool is_over_resolution_limit(int w, int h)
145{
147}
148
153
155{
156 if (tilearray == nullptr) {
157 return nullptr;
158 }
159
160 float array_w = GPU_texture_width(tilearray);
161 float array_h = GPU_texture_height(tilearray);
162
163 /* Determine maximum tile number. */
165 ImageTile *last_tile = ima->tiles.last();
166 int max_tile = last_tile->tile_number - 1001;
167
168 /* create image */
169 int width = max_tile + 1;
170 float *data = MEM_new_array_zeroed<float>(size_t(width) * 8, __func__);
171 for (int i = 0; i < width; i++) {
172 data[4 * i] = -1.0f;
173 }
174 for (ImageTile &tile : ima->tiles) {
175 int i = tile.tile_number - 1001;
176 ImageTile_Runtime *tile_runtime = &tile.runtime;
177 data[4 * i] = tile_runtime->tilearray_layer;
178
179 float *tile_info = &data[4 * width + 4 * i];
180 tile_info[0] = tile_runtime->tilearray_offset[0] / array_w;
181 tile_info[1] = tile_runtime->tilearray_offset[1] / array_h;
182 tile_info[2] = tile_runtime->tilearray_size[0] / array_w;
183 tile_info[3] = tile_runtime->tilearray_size[1] / array_h;
184 }
185
186 gpu::Texture *tex = GPU_texture_create_1d_array(ima->id.name + 2,
187 width,
188 2,
189 1,
190 gpu::TextureFormat::SFLOAT_32_32_32_32,
192 data);
193 if (tex != nullptr) {
194 GPU_texture_mipmap_mode(tex, false, false);
195 }
196
197 MEM_delete(data);
198
199 return tex;
200}
201
207
208static int compare_packtile(const void *a, const void *b)
209{
210 const PackTile *tile_a = static_cast<const PackTile *>(a);
211 const PackTile *tile_b = static_cast<const PackTile *>(b);
212
213 return tile_a->pack_score < tile_b->pack_score;
214}
215
217{
218 int arraywidth = 0, arrayheight = 0;
219 ListBaseT<FixedSizeBoxPack> boxes = {nullptr};
220
221 bool all_grayscale = true;
222
223 for (ImageTile &tile : ima->tiles) {
224 ImageUser iuser;
225 BKE_imageuser_default(&iuser);
226 iuser.tile = tile.tile_number;
227 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &iuser, nullptr);
228
229 if (ibuf) {
230 PackTile *packtile = MEM_new_zeroed<PackTile>(__func__);
231 packtile->tile = &tile;
232 packtile->boxpack.w = ibuf->x;
233 packtile->boxpack.h = ibuf->y;
234
235 if (is_over_resolution_limit(packtile->boxpack.w, packtile->boxpack.h)) {
236 packtile->boxpack.w = smaller_power_of_2_limit(packtile->boxpack.w);
237 packtile->boxpack.h = smaller_power_of_2_limit(packtile->boxpack.h);
238 }
239 arraywidth = max_ii(arraywidth, packtile->boxpack.w);
240 arrayheight = max_ii(arrayheight, packtile->boxpack.h);
241
242 /* We sort the tiles by decreasing size, with an additional penalty term
243 * for high aspect ratios. This improves packing efficiency. */
244 float w = packtile->boxpack.w, h = packtile->boxpack.h;
245 packtile->pack_score = max_ff(w, h) / min_ff(w, h) * w * h;
246
247 BKE_image_release_ibuf(ima, ibuf, nullptr);
248 BLI_addtail(&boxes, packtile);
249 if (ibuf->color_mode != ImColorMode::BW) {
250 all_grayscale = false;
251 }
252 }
253 }
254
255 BLI_assert(arraywidth > 0 && arrayheight > 0);
256
258 int arraylayers = 0;
259 /* Keep adding layers until all tiles are packed. */
260 while (boxes.first() != nullptr) {
261 ListBaseT<FixedSizeBoxPack> packed = {nullptr};
262 BLI_box_pack_2d_fixedarea(&boxes, arraywidth, arrayheight, &packed);
263 BLI_assert(packed.first() != nullptr);
264
265 for (const FixedSizeBoxPack &fixedpack : packed) {
266 const PackTile *packtile = reinterpret_cast<const PackTile *>(&fixedpack);
267 ImageTile *tile = packtile->tile;
268 ImageTile_Runtime *tile_runtime = &tile->runtime;
269 int *tileoffset = tile_runtime->tilearray_offset;
270 int *tilesize = tile_runtime->tilearray_size;
271
272 tileoffset[0] = packtile->boxpack.x;
273 tileoffset[1] = packtile->boxpack.y;
274 tilesize[0] = packtile->boxpack.w;
275 tilesize[1] = packtile->boxpack.h;
276 tile_runtime->tilearray_layer = arraylayers;
277 }
278
279 packed.free_no_destruct();
280 arraylayers++;
281 }
282
283 const bool use_high_bitdepth = (ima->flag & IMA_HIGH_BITDEPTH);
284 const bool use_grayscale = all_grayscale;
285 /* Create Texture without content. */
286 gpu::Texture *tex = IMB_touch_gpu_texture(ima->id.name + 2,
287 main_ibuf,
288 arraywidth,
289 arrayheight,
290 arraylayers,
291 use_high_bitdepth,
292 use_grayscale);
293
294 if (!tex) {
295 return nullptr;
296 }
297
298 /* Upload each tile one by one. */
299 for (ImageTile &tile : ima->tiles) {
300 const ImageTile_Runtime *tile_runtime = &tile.runtime;
301 const int tilelayer = tile_runtime->tilearray_layer;
302 const int *tileoffset = tile_runtime->tilearray_offset;
303 const int *tilesize = tile_runtime->tilearray_size;
304
305 if (tilesize[0] == 0 || tilesize[1] == 0) {
306 continue;
307 }
308
309 ImageUser iuser;
310 BKE_imageuser_default(&iuser);
311 iuser.tile = tile.tile_number;
312 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &iuser, nullptr);
313
314 if (ibuf) {
315 const bool store_premultiplied = BKE_image_has_gpu_texture_premultiplied_alpha(ima, ibuf);
317 ibuf,
318 UNPACK2(tileoffset),
319 tilelayer,
320 UNPACK2(tilesize),
321 use_grayscale,
322 store_premultiplied);
323 }
324
325 BKE_image_release_ibuf(ima, ibuf, nullptr);
326 }
327
329 GPU_texture_original_size_set(tex, main_ibuf->x, main_ibuf->y);
330
331 return tex;
332}
333
335
336/* -------------------------------------------------------------------- */
339
345static void image_cache_free_inactive_frame_gpu_textures(Image *ima, const ImBuf *current_ibuf)
346{
347 if (!ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) || ima->runtime->cache == nullptr) {
348 return;
349 }
350
351 std::scoped_lock lock(ima->runtime->cache_mutex);
352
353 /* The frame that backs the buffer currently in use. */
354 const int current_frame = current_ibuf->fileframe;
355
356 /* Free GPU textures of other frames. */
357 ImBufCacheIter *iter = IMB_cacheIter_new(ima->runtime->cache);
358 while (!IMB_cacheIter_done(iter)) {
359 ImBuf *ibuf = IMB_cacheIter_getImBuf(iter);
360 if (ibuf != nullptr && ibuf->gpu.texture != nullptr && ibuf->fileframe != current_frame &&
361 ibuf->refcounter == 0)
362 {
364 }
365 IMB_cacheIter_step(iter);
366 }
367 IMB_cacheIter_free(iter);
368}
369
371
372/* -------------------------------------------------------------------- */
375
376static ImBuf *g_error_imbuf = nullptr;
377
379{
380 /* Create on demand so we have a GPU context available when creating. */
381 if (g_error_imbuf == nullptr) {
383 }
384 if (g_error_imbuf->gpu.texture == nullptr) {
386 }
387 return g_error_imbuf;
388}
389
395
397{
398 if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
399 return;
400 }
401 if (ima->runtime->gpu_load_error_logged) {
402 return;
403 }
404 ima->runtime->gpu_load_error_logged = true;
405
406 char filepath[FILE_MAX];
407 BKE_image_user_file_path(iuser, ima, filepath);
408
409 /* Report a missing file separately, matching the Cycles message. */
412 !BLI_exists(filepath))
413 {
415 }
416 }
417 else {
418 STRNCPY(filepath, ima->id.name + 2);
419 }
420
421 switch (error) {
423 CLOG_ERROR(&LOG, "Failed to load image \"%s\"", filepath);
424 break;
426 CLOG_ERROR(&LOG, "Image file \"%s\" does not exist", filepath);
427 break;
429 CLOG_ERROR(&LOG, "Failed to create texture for \"%s\"", filepath);
430 break;
431 }
432}
433
435{
436 ima->runtime->gpu_load_error_logged = false;
437}
438
440{
441 if (g_error_imbuf == nullptr) {
442 return;
443 }
444 if (g_error_imbuf->gpu.texture != nullptr) {
446 g_error_imbuf->gpu.texture = nullptr;
447 }
449 g_error_imbuf = nullptr;
450}
451
453
454/* -------------------------------------------------------------------- */
457
459{
460 if (image->source != IMA_SRC_TILED) {
461 return;
462 }
464 if (atlas_ibuf == nullptr) {
465 return;
466 }
467 gpu::Texture *atlas_tex = atlas_ibuf->gpu.texture;
468
469 /* A resized tile invalidates the atlas packing and forces a rebuild of all tiles. */
470 bool need_full_rebuild = false;
471
472 ImageUser tile_user = {};
473 if (iuser != nullptr) {
474 tile_user = *iuser;
475 }
476 else {
477 tile_user.framenr = image->lastframe;
478 }
479
480 /* Get changeset ID that we will update to, and last changeset ID. */
481 const imbuf::ChangesetID new_changeset_id = BKE_image_partial_update_flush(image, &tile_user);
482 const imbuf::ChangesetID last_changeset_id = atlas_ibuf->gpu.partial_update_changeset_id;
483
484 for (ImageTile &tile : image->tiles) {
485 tile_user.tile = tile.tile_number;
486 void *lock;
487 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &tile_user, &lock);
488 if (ibuf != nullptr) {
489 Changes changes = IMB_partial_update_collect(ibuf, last_changeset_id);
490 switch (changes.kind) {
491 case Changes::Kind::Resized:
492 need_full_rebuild = true;
493 break;
494 case Changes::Kind::Full:
495 changes.set_all_chunks_modified();
496 [[fallthrough]];
497 case Changes::Kind::Partial:
498 if (atlas_tex != nullptr) {
499 const bool store_premultiplied = BKE_image_has_gpu_texture_premultiplied_alpha(image,
500 ibuf);
502 ibuf,
503 store_premultiplied,
504 changes,
505 tile.runtime.tilearray_layer,
506 int2(tile.runtime.tilearray_offset),
507 int2(tile.runtime.tilearray_size));
508 }
509 break;
510 case Changes::Kind::None:
511 break;
512 }
513 }
515
516 if (need_full_rebuild) {
517 break;
518 }
519 }
520
521 atlas_ibuf->gpu.partial_update_changeset_id = new_changeset_id;
522 IMB_freeImBuf(atlas_ibuf);
523
524 if (need_full_rebuild) {
526 }
527}
528
530 ImageUser *iuser,
531 const bool try_only)
532{
534 result.need_tile_mapping = true;
535
536 /* Get or create atlas and tile mapping image buffers. */
537 ImBuf *atlas_ibuf, *mapping_ibuf;
538 {
539 std::scoped_lock lock(ima->runtime->cache_mutex);
542 }
543
544 /* Update time for garbage collection. */
545 const int64_t now = BLI_time_now_seconds_i();
546 atlas_ibuf->gpu.lastused = now;
547 mapping_ibuf->gpu.lastused = now;
548
549 /* Acquire textures if they exist. */
551 ima->id.name + 2, atlas_ibuf, GPUTextureCreateFlags::EnableMipmaps, true);
552 result.tile_mapping = IMB_acquire_gpu_texture(
553 ima->id.name + 2, mapping_ibuf, GPUTextureCreateFlags::EnableMipmaps, true);
554
555 if (try_only || (result.texture != nullptr && result.tile_mapping != nullptr)) {
556 IMB_freeImBuf(atlas_ibuf);
557 IMB_freeImBuf(mapping_ibuf);
558 return result;
559 }
560
561 /* Recreate both textures in case one got freed (unlikely in practice). */
562 if (result.texture) {
563 GPU_texture_free(result.texture);
564 result.texture = nullptr;
565 }
566 if (result.tile_mapping) {
567 GPU_texture_free(result.tile_mapping);
568 result.tile_mapping = nullptr;
569 }
570
571 /* Get changeset ID that we will update to. */
572 const imbuf::ChangesetID new_changeset_id = BKE_image_partial_update_flush(ima, nullptr);
573
574 /* Acquire image buffer. */
575 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, nullptr);
576 gpu::Texture *atlas_tex = nullptr;
577 gpu::Texture *mapping_tex = nullptr;
578
579 if (ibuf == nullptr) {
581 }
582 else {
583 /* Create atlas and tile mapping textures. */
584 atlas_tex = gpu_texture_create_tile_array(ima, ibuf);
585 if (atlas_tex) {
586 mapping_tex = gpu_texture_create_tile_mapping(ima, atlas_tex);
587 }
588 if (atlas_tex && mapping_tex) {
590 }
591 else {
593 }
594 }
595
596 /* Set error texture if either failed to load. */
597 if (atlas_tex == nullptr || mapping_tex == nullptr) {
598 if (atlas_tex) {
599 GPU_texture_free(atlas_tex);
600 }
601 if (mapping_tex) {
602 GPU_texture_free(mapping_tex);
603 }
604 atlas_tex = GPU_texture_create_error(2, true);
605 mapping_tex = GPU_texture_create_error(1, true);
606 }
607
608 BKE_image_release_ibuf(ima, ibuf, nullptr);
609
610 /* Increase owned references for the result. */
611 if (atlas_tex) {
612 GPU_texture_ref(atlas_tex);
613 }
614 if (mapping_tex) {
615 GPU_texture_ref(mapping_tex);
616 }
617
618 result.texture = atlas_tex;
619 result.tile_mapping = mapping_tex;
620
621 /* Assign to the image buffers, which takes the reference from creation. */
622 IMB_assign_gpu_texture(atlas_ibuf, atlas_tex);
623 IMB_assign_gpu_texture(mapping_ibuf, mapping_tex);
624
625 atlas_ibuf->gpu.partial_update_changeset_id = new_changeset_id;
626 mapping_ibuf->gpu.partial_update_changeset_id = new_changeset_id;
627
628 IMB_freeImBuf(atlas_ibuf);
629 IMB_freeImBuf(mapping_ibuf);
630
631 return result;
632}
633
635{
636 /* Check if this image buffer can fit in a GPU texture at full resolution. */
637 const bool has_cpu_data = ibuf->float_data() || ibuf->byte_data();
638 return !has_cpu_data || (GPU_is_safe_texture_size(ibuf->x, ibuf->y) &&
639 GPU_texture_size_with_limit(ibuf->x) == ibuf->x &&
640 GPU_texture_size_with_limit(ibuf->y) == ibuf->y);
641}
642
644 ImageUser *iuser,
645 const bool use_viewers,
646 const bool only_full_resolution,
647 const bool try_only)
648{
650
651 /* Acquire the image buffer. */
652 void *lock = nullptr;
653 bool cpu_load_failed = false;
655 ima, iuser, use_viewers ? &lock : nullptr, &cpu_load_failed, try_only);
656
657 bool gpu_load_failed = false;
658 if (ibuf != nullptr && (!only_full_resolution || image_gpu_texture_fits_full_resolution(ibuf))) {
659 /* Acquire a reference to the GPU texture. */
662 if (ima->flag & IMA_HIGH_BITDEPTH) {
663 texture_create_flags |= GPUTextureCreateFlags::HighBitDepth;
664 }
666 texture_create_flags |= GPUTextureCreateFlags::Premultiplied;
667 }
668
670 ima->id.name + 2, ibuf, texture_create_flags, try_only);
671 if (tex) {
672 GPU_texture_original_size_set(tex, ibuf->x, ibuf->y);
674 }
675 else if (!try_only) {
677 }
678 if (!try_only) {
680 }
681 result.texture = tex;
682 gpu_load_failed = (tex == nullptr) && (ibuf->gpu.flag & IMB_GPU_LOAD_FAILED);
683 }
684
685 /* Release image buffer. */
686 BKE_image_release_ibuf(ima, ibuf, lock);
687
688 /* Return error texture if failed to load, including in try_only mode. This
689 * way the caller will not consider it as still needing to be loaded. */
690 if (result.texture == nullptr && (!try_only || cpu_load_failed || gpu_load_failed) &&
691 !only_full_resolution)
692 {
693 const ImageLoadError error = gpu_load_failed ? ImageLoadError::GPUTextureFailed :
695 image_gpu_log_load_error_once(ima, iuser, error);
696 ImBuf *error_ibuf = image_gpu_error_imbuf_ensure();
698 ima->id.name + 2, error_ibuf, GPUTextureCreateFlags::EnableMipmaps);
699 }
700
701 return result;
702}
703
704/* Returns the GPU textures representing the given image with the given image user. The image
705 * texture cache is checked first and if cached texture exist, they will be returned. If try_only
706 * is true, nullptr textures will be returned if no cached textures exists, otherwise, the textures
707 * will be generated and added to the cached.
708 *
709 * If use_viewers is true, the image buffer will be acquired with locking to allow retrieval of
710 * images of type viewer. If use_tile_mapping is true and the image is a tiled images, the
711 * returned texture will be a 2D texture array with a mapping texture to sampling the image at
712 * arbitrary tiles, otherwise, only the tile in the image user will be retrieved. */
714 ImageUser *iuser,
715 const bool use_viewers,
716 const bool only_full_resolution,
717 const bool use_tile_mapping,
718 const bool try_only)
719{
720 if (ima == nullptr) {
721 return {};
722 }
723
725
726 const bool tiled = (use_tile_mapping && ima->source == IMA_SRC_TILED);
727 return tiled ?
728 image_get_gpu_texture_tiled(ima, iuser, try_only) :
729 image_get_gpu_texture_single(ima, iuser, use_viewers, only_full_resolution, try_only);
730}
731
733{
734 return image_get_gpu_texture(image, iuser, false, false, false, false).texture;
735}
736
738{
739 /* Re-home an externally-created texture (e.g. a look-dev studio light) onto the image's
740 * #ImBuf, which is where single-image GPU textures live. */
741 void *lock;
742 ImBuf *ibuf = BKE_image_acquire_ibuf(image, nullptr, &lock);
743 if (ibuf != nullptr) {
745 }
747}
748
750 ImageUser *iuser,
751 const bool only_full_resolution)
752{
753 return image_get_gpu_texture(image, iuser, true, only_full_resolution, false, false).texture;
754}
755
757
758/* -------------------------------------------------------------------- */
761
763 ImageUser *iuser,
764 const bool use_tile_mapping,
765 const bool try_only)
766{
767 return image_get_gpu_texture(image, iuser, false, false, use_tile_mapping, try_only);
768}
769
771 ImageUser *iuser,
772 const bool use_tile_mapping)
773{
774 const bool try_only = true;
776 image, iuser, false, false, use_tile_mapping, try_only);
777 const bool has_texture = result.texture != nullptr;
778
779 /* Release reference, stays owned by the image buffer. */
780 if (result.texture) {
781 GPU_texture_free(result.texture);
782 }
783 if (result.tile_mapping) {
784 GPU_texture_free(result.tile_mapping);
785 }
786
787 return has_texture;
788}
789
791 ImageUser *iuser,
792 const bool use_tile_mapping)
793{
795 image, iuser, false, false, use_tile_mapping, false);
796
797 /* Release reference, stays owned by the image buffer. */
798 if (result.texture) {
799 GPU_texture_free(result.texture);
800 }
801 if (result.tile_mapping) {
802 GPU_texture_free(result.tile_mapping);
803 }
804}
805
807
808/* -------------------------------------------------------------------- */
811
817
819{
820 /* For Viewer images, the GPU texture can be generated directly by the compositor
821 * without a CPU buffer, so it's not a cache and must be preserved. This is a
822 * crude check, for future more general GPU image buffer support ImBuf will need
823 * to carry this information. */
824 if (ima->source == IMA_SRC_VIEWER) {
825 return;
826 }
827
828 if (ima->runtime->cache) {
829 std::scoped_lock lock(ima->runtime->cache_mutex);
830
832
833 ImBufCacheIter *iter = IMB_cacheIter_new(ima->runtime->cache);
834 while (!IMB_cacheIter_done(iter)) {
835 ImBuf *ibuf = IMB_cacheIter_getImBuf(iter);
836 if (ibuf != nullptr) {
838 }
839 IMB_cacheIter_step(iter);
840 }
841 IMB_cacheIter_free(iter);
842 }
843}
844
846{
847 if (bmain) {
848 for (Image &ima : bmain->images) {
850 }
851 }
852}
853
855{
856 if (bmain) {
857 for (Image &ima : bmain->images) {
858 if (BKE_image_is_animated(&ima)) {
860 }
861 }
862 }
863}
864
866
868{
869 ImageUser tile_user;
870 BKE_imageuser_default(&tile_user);
871 if (iuser != nullptr) {
872 tile_user = *iuser;
873 }
874
875 for (ImageTile &tile : ima->tiles) {
876 tile_user.tile = tile.tile_number;
877 void *lock;
878 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &tile_user, &lock);
879 if (ibuf != nullptr) {
881 }
882 BKE_image_release_ibuf(ima, ibuf, lock);
883 }
884
886}
887
888} // namespace blender
File and directory operations.
#define FILE_MAX
Platform independent time functions.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
Read Blender memory allocation and freeing.
volatile int lock
#define IMA_INDEX_UDIM_ATLAS
#define IMA_INDEX_UDIM_TILE_MAPPING
#define UNPACK2(a)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const ccl_global KernelWorkTile * tile
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void IMB_cacheIter_step(ImBufCacheIter *iter)
Definition cache.cc:597
void BLI_box_pack_2d_fixedarea(ListBaseT< FixedSizeBoxPack > *boxes, int width, int height, ListBaseT< FixedSizeBoxPack > *packed)
static void image_gpu_log_load_error_once(Image *ima, ImageUser *iuser, ImageLoadError error)
Definition image_gpu.cc:396
bool BKE_image_is_animated(Image *image)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
void IMB_assign_gpu_texture(ImBuf *ibuf, gpu::Texture *texture)
Definition util_gpu.cc:850
GPUTextureCreateFlags
Definition IMB_imbuf.hh:611
int64_t BLI_time_now_seconds_i()
Definition time.cc:36
void BKE_image_ensure_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping)
Definition image_gpu.cc:790
MINLINE int power_of_2_min_i(int n)
void BKE_image_free_gpu_texture_caches(Image *ima)
Definition image_gpu.cc:818
static void image_udim_gpu_ibuf_remove(Image *ima, const int udim_index)
Definition image_gpu.cc:80
void GPU_texture_update_mipmap_chain(gpu::Texture *texture)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
void BKE_image_free_gpu_fallback()
Definition image_gpu.cc:439
bool BKE_image_has_filepath(const Image *ima)
ImageGPUTextures BKE_image_acquire_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping, const bool try_only)
Definition image_gpu.cc:762
void BKE_image_assign_gpu_texture(Image *image, gpu::Texture *texture)
Definition image_gpu.cc:737
ImBuf * IMB_cacheIter_getImBuf(ImBufCacheIter *iter)
Definition cache.cc:602
void BKE_image_user_file_path(const ImageUser *iuser, const Image *ima, char *filepath)
MINLINE int max_ii(int a, int b)
static gpu::Texture * gpu_texture_create_tile_array(Image *ima, ImBuf *main_ibuf)
Definition image_gpu.cc:216
imbuf::ChangesetID IMB_partial_update_changeset_id_current()
void BKE_image_free_all_gpu_texture_caches(Main *bmain)
Definition image_gpu.cc:845
static int smaller_power_of_2_limit(int num)
Definition image_gpu.cc:149
ImBuf * IMB_cache_get(ImBufCache *cache, void *userkey, bool *r_is_cached_empty)
Definition cache.cc:403
static bool is_over_resolution_limit(int w, int h)
Definition image_gpu.cc:144
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
ImBuf * BKE_image_acquire_ibuf_gpu(Image *ima, ImageUser *iuser, void **r_lock, bool *r_load_failed=nullptr, bool cached_only=false)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
ImBufCacheIter * IMB_cacheIter_new(ImBufCache *cache)
Definition cache.cc:577
@ GPU_TEXTURE_USAGE_SHADER_READ
void imagecache_put(Image *image, ImageCacheKey key, ImBuf *ibuf)
BMesh const char void * data
gpu::Texture * BKE_image_acquire_gpu_viewer_texture(Image *image, ImageUser *iuser, bool only_full_resolution=false)
Definition image_gpu.cc:749
static ImBuf * image_udim_gpu_ibuf_ensure(Image *ima, const int udim_index)
Definition image_gpu.cc:69
void GPU_texture_original_size_set(gpu::Texture *texture, int width, int height)
int GPU_texture_width(const gpu::Texture *texture)
bool BKE_image_has_gpu_texture(Image *ima)
Definition image_gpu.cc:95
void IMB_gpu_texture_apply_partial_update(gpu::Texture *tex, ImBuf *ibuf, bool store_premultiplied, const imbuf::partial_update::Changes &changes, int layer=-1, int2 tile_offset=int2(0), int2 tile_size=int2(0))
Definition util_gpu.cc:686
static ImageGPUTextures image_get_gpu_texture_single(Image *ima, ImageUser *iuser, const bool use_viewers, const bool only_full_resolution, const bool try_only)
Definition image_gpu.cc:643
ATTR_WARN_UNUSED_RESULT const size_t num
bool IMB_cacheIter_done(ImBufCacheIter *iter)
Definition cache.cc:592
bool BKE_image_has_gpu_texture_premultiplied_alpha(Image *image, ImBuf *ibuf)
Definition image_gpu.cc:116
void BKE_image_free_gpu_udim_textures(Image *ima)
Definition image_gpu.cc:812
static int compare_packtile(const void *a, const void *b)
Definition image_gpu.cc:208
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
int64_t BKE_image_partial_update_flush(Image *ima, const ImageUser *iuser)
Definition image_gpu.cc:867
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
static ImageGPUTextures image_get_gpu_texture(Image *ima, ImageUser *iuser, const bool use_viewers, const bool only_full_resolution, const bool use_tile_mapping, const bool try_only)
Definition image_gpu.cc:713
void IMB_update_gpu_texture_sub(gpu::Texture *tex, ImBuf *ibuf, int x, int y, int z, int w, int h, bool use_grayscale, bool use_premult)
Definition util_gpu.cc:428
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static ImBuf * image_udim_gpu_ibuf_get(Image *ima, const int udim_index)
Definition image_gpu.cc:64
gpu::Texture * IMB_acquire_gpu_texture(const char *name, ImBuf *ibuf, GPUTextureCreateFlags texture_create_flags, bool try_only=false)
Definition util_gpu.cc:778
static void image_cache_free_inactive_frame_gpu_textures(Image *ima, const ImBuf *current_ibuf)
Definition image_gpu.cc:345
static gpu::Texture * gpu_texture_create_tile_mapping(Image *ima, gpu::Texture *tilearray)
Definition image_gpu.cc:154
int GPU_texture_height(const gpu::Texture *texture)
bool GPU_is_safe_texture_size(int width, int height)
MINLINE float max_ff(float a, float b)
void GPU_texture_free(gpu::Texture *texture)
bool BKE_image_has_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping)
Definition image_gpu.cc:770
static ImBuf * image_udim_gpu_cache_get(Image *image, ImageCacheKey key)
Definition image_gpu.cc:52
void IMB_free_gpu_textures(ImBuf *ibuf)
Definition util_gpu.cc:835
static CLG_LogRef LOG
Definition image_gpu.cc:44
void BKE_image_free_anim_gpu_texture_caches(Main *bmain)
Definition image_gpu.cc:854
static bool image_gpu_texture_fits_full_resolution(const ImBuf *ibuf)
Definition image_gpu.cc:634
static void image_gpu_atlas_try_partial_update(Image *image, ImageUser *iuser)
Definition image_gpu.cc:458
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
bool BKE_image_source_is_file(const Image *ima)
gpu::Texture * GPU_texture_create_error(int dimension, bool array)
void IMB_partial_update_flush(ImBuf *ibuf)
gpu::Texture * BKE_image_acquire_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:732
bool BKE_image_has_packedfile(const Image *image)
static ImBuf * g_error_imbuf
Definition image_gpu.cc:376
void IMB_cache_remove(ImBufCache *cache, void *userkey)
Definition cache.cc:395
void BKE_imageuser_default(ImageUser *iuser)
static ImageGPUTextures image_get_gpu_texture_tiled(Image *ima, ImageUser *iuser, const bool try_only)
Definition image_gpu.cc:529
void IMB_cacheIter_free(ImBufCacheIter *iter)
Definition cache.cc:587
static ImBuf * image_gpu_error_imbuf_ensure()
Definition image_gpu.cc:378
static void image_gpu_clear_load_error(Image *ima)
Definition image_gpu.cc:434
gpu::Texture * IMB_touch_gpu_texture(const char *name, ImBuf *ibuf, int w, int h, int layers, bool use_high_bitdepth, bool use_grayscale)
Definition util_gpu.cc:402
int GPU_texture_size_with_limit(int res)
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void GPU_texture_ref(gpu::Texture *texture)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
std::atomic< imbuf::ChangesetID > partial_update_changeset_id
std::atomic< gpu::Texture * > texture
const uint8_t * byte_data() const
const float * float_data() const
ImColorMode color_mode
ListBaseT< Image > images
Definition BKE_main.hh:394
FixedSizeBoxPack boxpack
Definition image_gpu.cc:203
ImageTile * tile
Definition image_gpu.cc:204