Blender V5.3
util_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 "BLI_bit_span.hh"
10#include "BLI_bit_vector.hh"
11#include "BLI_math_base.hh"
13#include "BLI_mutex.hh"
14#include "BLI_rect.hh"
15#include "BLI_time.hh"
16#include "BLI_utildefines.hh"
17#include "BLI_utility_mixins.hh"
18
19#include "MEM_guardedalloc.h"
20
21#include <mutex>
22
23#include "CLG_log.h"
24
25#include "GPU_capabilities.hh"
26#include "GPU_texture.hh"
27
29#include "IMB_filetype.hh"
30#include "IMB_imbuf.hh"
31#include "IMB_imbuf_types.hh"
32#include "IMB_partial_update.hh"
33
34namespace blender {
35
36static CLG_LogRef LOG = {"image.gpu"};
37
38/* gpu ibuf utils */
39
41{
42 if (ibuf->color_mode != ImColorMode::BW) {
43 return false;
44 }
45
46 if (ibuf->byte_data() && !ibuf->float_data()) {
47
50 {
51 /* Grey-scale byte buffers with these color transforms utilize float buffers under the hood
52 * and can therefore be optimized. */
53 return true;
54 }
55 /* TODO: Support gray-scale byte buffers.
56 * The challenge is that Blender always stores byte images as RGBA. */
57 return false;
58 }
59
60 /* Only #IMBuf's with color-space that do not modify the chrominance of the texture data relative
61 * to the scene color space can be uploaded as single channel textures. */
65 {
66 return true;
67 }
68 return false;
69}
70
71static void imb_gpu_get_format(const ImBuf *ibuf,
72 bool high_bitdepth,
73 bool use_grayscale,
74 gpu::TextureFormat *r_texture_format)
75{
76 const bool float_rect = (ibuf->float_data() != nullptr);
77 const bool is_grayscale = use_grayscale && imb_is_grayscale_texture_format_compatible(ibuf);
78
79 if (float_rect) {
80 /* Float. */
81 const bool use_high_bitdepth = (!(ibuf->foptions.flag & OPENEXR_HALF) && high_bitdepth);
82 *r_texture_format = is_grayscale ?
83 (use_high_bitdepth ? gpu::TextureFormat::SFLOAT_32 :
84 gpu::TextureFormat::SFLOAT_16) :
85 (use_high_bitdepth ? gpu::TextureFormat::SFLOAT_32_32_32_32 :
86 gpu::TextureFormat::SFLOAT_16_16_16_16);
87 }
88 else {
91 {
92 /* Non-color data or scene linear, just store buffer as is. */
93 *r_texture_format = (is_grayscale) ? gpu::TextureFormat::UNORM_8 :
94 gpu::TextureFormat::UNORM_8_8_8_8;
95 }
97 /* scene linear + sRGB, store as byte texture that the GPU can decode directly. */
98 *r_texture_format = (is_grayscale) ? gpu::TextureFormat::SFLOAT_16 :
99 gpu::TextureFormat::SRGBA_8_8_8_8;
100 }
101 else {
102 /* Other colorspace, store as half float texture to avoid precision loss. */
103 *r_texture_format = (is_grayscale) ? gpu::TextureFormat::SFLOAT_16 :
104 gpu::TextureFormat::SFLOAT_16_16_16_16;
105 }
106 }
107}
108
109static const char *imb_gpu_get_swizzle(const ImBuf *ibuf)
110{
111 return imb_is_grayscale_texture_format_compatible(ibuf) ? "rrra" : "rgba";
112}
113
114/* Return false if no suitable format was found. */
115bool IMB_gpu_get_compressed_format(const ImBuf *ibuf, gpu::TextureFormat *r_texture_format)
116{
117 if (ibuf->ftype != IMB_FTYPE_DDS) {
118 return false;
119 }
120
121 /* Compressed DDS files can really only express sRGB or data/linear. */
122 const bool use_srgb = (!IMB_colormanagement_space_is_data(ibuf->byte_buffer.colorspace) &&
124 if (ibuf->foptions.flag & DDS_COMPRESSED_DXT1) {
125 *r_texture_format = use_srgb ? gpu::TextureFormat::SRGB_DXT1 : gpu::TextureFormat::SNORM_DXT1;
126 return true;
127 }
128 if (ibuf->foptions.flag & DDS_COMPRESSED_DXT3) {
129 *r_texture_format = use_srgb ? gpu::TextureFormat::SRGB_DXT3 : gpu::TextureFormat::SNORM_DXT3;
130 return true;
131 }
132 if (ibuf->foptions.flag & DDS_COMPRESSED_DXT5) {
133 *r_texture_format = use_srgb ? gpu::TextureFormat::SRGB_DXT5 : gpu::TextureFormat::SNORM_DXT5;
134 return true;
135 }
136 return false;
137}
138
139/* Extract the first channel of an RGBA buffer into a single channel, for uploading grayscale. */
140template<typename T>
142 const T *src, const int channels, const int stride, const int w, const int h, T *r_gray)
143{
144 for (int row = 0; row < h; row++) {
145 for (int col = 0; col < w; col++) {
146 r_gray[size_t(row) * w + col] = src[(size_t(row) * stride + col) * channels];
147 }
148 }
149}
150
151/* Determine image buffer conversion needed before GPU upload. */
152enum class GPUTextureConversion { None, Byte, Float };
153
155 const bool is_grayscale,
156 const bool store_premultiplied)
157{
158 if (ibuf->float_data()) {
159 /* Float images are already in scene linear color space or contain non-color data with
160 * premultiplied alpha by convention, so no color space conversion is needed. But we need to
161 * convert to RGBA and unpremultiply alpha if needed. */
162 return (ibuf->channels != 4 || !store_premultiplied) ? GPUTextureConversion::Float :
164 }
165
166 /* Byte images are in original color space from the file with straight alpha, so we need to
167 * convert to scene linear color space and premultiply alpha if needed. An exception is images
168 * in sRGB color space, see the relevant case below for more information. */
171 /* Non-color data, used as is. */
173 }
175 /* Color space is already linear, we just need to premultiply the alpha if needed. */
176 const bool premultiply = !is_grayscale && store_premultiplied && IMB_alpha_affects_rgb(ibuf);
178 }
180 /* Images in scene linear + sRGB color space are a special case since they can be stored in
181 * sRGB textures on GPU, where scene linear space conversion will happen during sampling in
182 * the shader, however, this is not possible for grayscale images, so we need to do the
183 * conversion here, converting to a float image to prevent precision loss.
184 *
185 * It should be noted that for other color spaces, color space conversion happen before alpha
186 * premultiplication, while for sRGB, premultiplication will happen first since color space
187 * conversion happen in the shader as mentioned above. This will manifest as differences near
188 * alpha edges. But we generally accept this due to the advantages of sRGB textures. If this
189 * is problematic, the source image buffer should be linearised first. */
190 if (is_grayscale) {
192 }
193 const bool premultiply = store_premultiplied && IMB_alpha_affects_rgb(ibuf);
195 }
196 /* Other color-space, convert to linear color space and premultiply alpha if needed.
197 * Conversion happen as a float to avoid precision loss. */
199}
200
210 const void *data = nullptr;
212 int stride = 0;
214 ImBuf *tmp_ibuf = nullptr;
215
216 GPUTextureUpload() = default;
218 {
219 if (tmp_ibuf) {
221 }
222 }
223};
224
225static void get_gpu_texture_data(ImBuf *source_buffer,
226 const int2 src_offset,
227 const int2 src_size,
228 const std::optional<int2> scaled_size,
229 const bool premultiplied_alpha,
230 const bool is_grayscale,
231 GPUTextureUpload &r_upload)
232{
234 source_buffer, is_grayscale, premultiplied_alpha);
235
236 int channels;
237
238 if (conversion == GPUTextureConversion::Byte) {
239 /* Convert to byte buffer. */
240 r_upload.tmp_ibuf = IMB_allocImBuf(
241 src_size.x, src_size.y, ImBufFlags::ByteData | ImBufFlags::UninitializedPixels);
242 if (r_upload.tmp_ibuf == nullptr) {
243 return;
244 }
246 src_offset.x,
247 src_offset.y,
248 src_size.x,
249 src_size.y,
250 source_buffer,
251 premultiplied_alpha);
252 r_upload.data = r_upload.tmp_ibuf->byte_data();
253 r_upload.format = GPU_DATA_UBYTE;
254 r_upload.stride = src_size.x;
255 channels = 4;
256 }
257 else if (conversion == GPUTextureConversion::Float) {
258 /* Convert to float buffer. */
259 r_upload.tmp_ibuf = IMB_allocImBuf(
261 if (r_upload.tmp_ibuf == nullptr) {
262 return;
263 }
265 src_offset.x,
266 src_offset.y,
267 src_size.x,
268 src_size.y,
269 source_buffer,
270 premultiplied_alpha);
271 r_upload.data = r_upload.tmp_ibuf->float_data();
272 r_upload.format = GPU_DATA_FLOAT;
273 r_upload.stride = src_size.x;
274 channels = 4;
275 }
276 else {
277 /* No conversion needed, point directly into the source buffer. */
278 channels = source_buffer->float_data() ? source_buffer->channels : 4;
279 const int64_t offset = int64_t(channels) *
280 (int64_t(src_offset.y) * source_buffer->x + src_offset.x);
281 if (source_buffer->float_data()) {
282 r_upload.data = source_buffer->float_data() + offset;
283 r_upload.format = GPU_DATA_FLOAT;
284 }
285 else {
286 r_upload.data = source_buffer->byte_data() + offset;
287 r_upload.format = GPU_DATA_UBYTE;
288 }
289 r_upload.stride = source_buffer->x;
290 }
291
292 int2 size = src_size;
293
294 /* Rescale. */
295 if (scaled_size.has_value()) {
296 const bool is_float = (r_upload.format == GPU_DATA_FLOAT);
297 ImBuf *buffer = IMB_allocImBuf(scaled_size->x,
298 scaled_size->y,
301
302 if (buffer == nullptr) {
303 r_upload.data = nullptr;
304 return;
305 }
306
307 /* Avoid excessive overhead with small updates. */
308 const bool threaded = int64_t(size.x) * size.y >= 512 * 512;
309
310 if (is_float) {
311 IMB_scale_box(static_cast<const float *>(r_upload.data),
312 size,
313 channels,
314 buffer->float_data_for_write(),
315 *scaled_size,
316 threaded,
317 r_upload.stride);
318 }
319 else {
320 IMB_scale_box(static_cast<const uchar *>(r_upload.data),
321 size,
322 channels,
323 buffer->byte_data_for_write(),
324 *scaled_size,
325 threaded,
326 r_upload.stride);
327 }
328 if (r_upload.tmp_ibuf) {
329 IMB_freeImBuf(r_upload.tmp_ibuf);
330 }
331 r_upload.tmp_ibuf = buffer;
332 r_upload.data = is_float ? static_cast<const void *>(buffer->float_data()) :
333 static_cast<const void *>(buffer->byte_data());
334 r_upload.stride = scaled_size->x;
335 size = *scaled_size;
336 }
337
338 /* Convert to grayscale. */
339 if (is_grayscale) {
340 ImBuf *buffer = IMB_allocImBuf(size.x, size.y, ImBufFlags::Zero);
341
342 if (buffer == nullptr) {
343 r_upload.data = nullptr;
344 return;
345 }
346
347 if (r_upload.format == GPU_DATA_FLOAT) {
348 IMB_alloc_float_pixels(buffer, 1);
349 imb_gpu_extract_first_channel(static_cast<const float *>(r_upload.data),
350 channels,
351 r_upload.stride,
352 size.x,
353 size.y,
354 buffer->float_data_for_write());
355 }
356 else {
357 buffer->color_mode = ImColorMode::BW;
358 buffer->assign_byte_data(
359 MEM_new_array_uninitialized<uint8_t>(size_t(size.x) * size.y, __func__));
360 imb_gpu_extract_first_channel(static_cast<const uchar *>(r_upload.data),
361 channels,
362 r_upload.stride,
363 size.x,
364 size.y,
365 buffer->byte_data_for_write());
366 }
367 if (r_upload.tmp_ibuf) {
368 IMB_freeImBuf(r_upload.tmp_ibuf);
369 }
370 r_upload.tmp_ibuf = buffer;
371 r_upload.data = (r_upload.format == GPU_DATA_FLOAT) ?
372 static_cast<const void *>(buffer->float_data()) :
373 static_cast<const void *>(buffer->byte_data());
374 r_upload.stride = size.x;
375 }
376
377 r_upload.size = size;
378}
379
385
391
393{
394 const char *swizzle = (GPU_texture_component_len(GPU_texture_format(tex)) == 1) ? "rrra" :
395 "rgba";
396 GPU_texture_swizzle_set(tex, swizzle);
399 GPU_texture_mipmap_mode(tex, true, true);
400}
401
403 ImBuf *ibuf,
404 int w,
405 int h,
406 int layers,
407 bool use_high_bitdepth,
408 bool use_grayscale)
409{
410 gpu::TextureFormat tex_format;
411 imb_gpu_get_format(ibuf, use_high_bitdepth, use_grayscale, &tex_format);
412
414 GPU_texture_mipmap_usage(tex_format);
415
416 gpu::Texture *tex;
417 if (layers > 0) {
418 tex = GPU_texture_create_2d_array(name, w, h, layers, 9999, tex_format, usage, nullptr);
420 }
421 else {
422 tex = GPU_texture_create_2d(name, w, h, 9999, tex_format, usage, nullptr);
424 }
425 return tex;
426}
427
429 ImBuf *ibuf,
430 int x,
431 int y,
432 int z,
433 int w,
434 int h,
435 bool use_grayscale,
436 bool use_premult)
437{
438 const std::optional<int2> scaled_size = (ibuf->x != w || ibuf->y != h) ?
439 std::optional<int2>(int2(w, h)) :
440 std::nullopt;
441 const bool is_grayscale = use_grayscale && imb_is_grayscale_texture_format_compatible(ibuf);
442 GPUTextureUpload upload;
444 ibuf, int2(0), int2(ibuf->x, ibuf->y), scaled_size, use_premult, is_grayscale, upload);
445
446 if (upload.data) {
448 tex, upload.format, upload.data, x, y, z, upload.size.x, upload.size.y, 1, upload.stride);
449 }
450}
451
453 ImBuf *ibuf,
454 const GPUTextureCreateFlags flags)
455{
457
458 const bool use_mipmap = flag_is_set(flags, GPUTextureCreateFlags::EnableMipmaps);
459
460 gpu::Texture *tex = nullptr;
461 int size[2] = {ibuf->x, ibuf->y};
462 if (flag_is_set(flags, GPUTextureCreateFlags::LimitSize)) {
463 size[0] = GPU_texture_size_with_limit(ibuf->x);
464 size[1] = GPU_texture_size_with_limit(ibuf->y);
465 }
466 bool do_rescale = (ibuf->x != size[0]) || (ibuf->y != size[1]);
467
468 /* Correct the smaller size to maintain the original aspect ratio of the image. */
469 if (do_rescale && ibuf->x != ibuf->y) {
470 if (ibuf->x > ibuf->y) {
471 size[1] = math::max(1, int(ibuf->y * (float(size[0]) / ibuf->x)));
472 }
473 else {
474 size[0] = math::max(1, int(ibuf->x * (float(size[1]) / ibuf->y)));
475 }
476 }
477
478 /* Compressed textures can't be written to, so upload uncompressed when modified. */
479 if (ibuf->ftype == IMB_FTYPE_DDS && (ibuf->userflags & IB_BITMAPDIRTY) == 0) {
480 gpu::TextureFormat compressed_format;
481 if (!IMB_gpu_get_compressed_format(ibuf, &compressed_format)) {
482 CLOG_WARN(&LOG,
483 "DDS image '%s' is not in a supported GPU compression format",
484 ibuf->filepath.c_str());
485 }
486 else if (do_rescale) {
487 CLOG_WARN(
488 &LOG, "DDS image '%s' can't use compressed due to size limit", ibuf->filepath.c_str());
489 }
490 else if (!is_power_of_2_i(ibuf->x) || !is_power_of_2_i(ibuf->y)) {
491 /* We require POT DXT/S3TC texture sizes not because something in there
492 * intrinsically needs it, but because we flip them upside down at
493 * load time, and that (when mipmaps are involved) is only possible
494 * with POT height. */
495 CLOG_WARN(&LOG,
496 "DDS image '%s' can't use compressed due to non power of two size",
497 ibuf->filepath.c_str());
498 }
499 else {
500
501 int mip_count = 0;
502 uint8_t *compressed_data = imb_load_dds_compressed_data(
503 ibuf->filepath.c_str(), ibuf->x, ibuf->y, mip_count);
504 if (compressed_data != nullptr) {
506 ibuf->x,
507 ibuf->y,
508 use_mipmap ? mip_count : 1,
509 compressed_format,
511 compressed_data);
512 MEM_delete(compressed_data);
513 if (tex != nullptr) {
514 return tex;
515 }
516 CLOG_WARN(&LOG,
517 "DDS image '%s' failed to create compressed GPU texture",
518 ibuf->filepath.c_str());
519 }
520 else {
521 CLOG_WARN(&LOG, "DDS image '%s' failed to load data from file", ibuf->filepath.c_str());
522 }
523 }
524 /* Fallback to uncompressed texture. */
525 CLOG_WARN(&LOG, "DDS image '%s' falling back to uncompressed", ibuf->filepath.c_str());
526 }
527
528 gpu::TextureFormat tex_format;
530 ibuf, flag_is_set(flags, GPUTextureCreateFlags::HighBitDepth), true, &tex_format);
531
532 /* Create Texture. Specify read usage to allow both shader and host reads, the latter is needed
533 * by the GPU compositor. Mipmaps need additional usage flags. */
535 if (use_mipmap) {
536 usage |= GPU_texture_mipmap_usage(tex_format);
537 }
539 name, UNPACK2(size), use_mipmap ? 9999 : 1, tex_format, usage, nullptr);
540 if (tex == nullptr) {
541 size[0] = max_ii(1, size[0] / 2);
542 size[1] = max_ii(1, size[1] / 2);
544 name, UNPACK2(size), use_mipmap ? 9999 : 1, tex_format, usage, nullptr);
545 do_rescale = true;
546 }
547 BLI_assert(tex != nullptr);
548 const std::optional<int2> scaled_size = do_rescale ? std::optional<int2>(int2(size)) :
549 std::nullopt;
550 const bool is_grayscale = imb_is_grayscale_texture_format_compatible(ibuf);
551 GPUTextureUpload upload;
553 int2(0),
554 int2(ibuf->x, ibuf->y),
555 scaled_size,
556 flag_is_set(flags, GPUTextureCreateFlags::Premultiplied),
557 is_grayscale,
558 upload);
559
560 if (upload.data) {
561 GPU_texture_update(tex, upload.format, upload.data);
562 }
563
565
566 return tex;
567}
568
569/* Number of modified chunks to track for mipmap. */
575
576/* For scaled images or UDIM atlases, set the modified chunk bits. */
578 const int2 size,
579 const rcti &bounds)
580{
581 constexpr int chunk_size = GPU_TEXTURE_MIPMAP_UPDATE_CHUNK_SIZE;
583 return;
584 }
585 const int tx_begin = std::max(bounds.xmin, 0) / chunk_size;
586 const int ty_begin = std::max(bounds.ymin, 0) / chunk_size;
587 const int tx_end = std::min((bounds.xmax - 1) / chunk_size, size.x - 1);
588 const int ty_end = std::min((bounds.ymax - 1) / chunk_size, size.y - 1);
589 for (int ty = ty_begin; ty <= ty_end; ty++) {
590 for (int tx = tx_begin; tx <= tx_end; tx++) {
591 modified_chunks[int64_t(ty) * size.x + tx].set();
592 }
593 }
594}
595
596/* Compute offset and size for partial update with scaling. */
598 const int2 size,
599 const int2 limit,
600 const int2 full)
601{
602 const float xratio = limit.x / float(full.x);
603 const float yratio = limit.y / float(full.y);
604
605 /* Find sub coordinates in scaled image. Take ceiling because we will be
606 * losing 1 pixel due to rounding errors in x,y. */
607 offset.x = int(offset.x * xratio);
608 offset.y = int(offset.y * yratio);
609 int2 scaled_size = int2(int(ceil(xratio * size.x)), int(ceil(yratio * size.y)));
610
611 /* ...but take back if we are over the limit! */
612 if (offset.x + scaled_size.x > limit.x) {
613 scaled_size.x--;
614 }
615 if (offset.y + scaled_size.y > limit.y) {
616 scaled_size.y--;
617 }
618
619 return scaled_size;
620}
621
623 ImBuf *ibuf,
624 const bool store_premultiplied,
625 int x,
626 int y,
627 int w,
628 int h,
629 const int layer,
630 const int2 tile_offset,
631 const int2 tile_size,
632 rcti &r_bounds)
633{
634 /* The texture may be smaller than the image when its size was limited. */
635 const int limit_w = (layer >= 0) ? tile_size.x : GPU_texture_width(tex);
636 const int limit_h = (layer >= 0) ? tile_size.y : GPU_texture_height(tex);
637 const bool scaled = (ibuf->x != limit_w) || (ibuf->y != limit_h);
638
639 if (scaled) {
640 /* Extra padding to account for bleed from neighboring pixels. */
641 const int padding = 4;
642 const int xmax = min_ii(x + w + padding, ibuf->x);
643 const int ymax = min_ii(y + h + padding, ibuf->y);
644 x = max_ii(x - padding, 0);
645 y = max_ii(y - padding, 0);
646 w = xmax - x;
647 h = ymax - y;
648 }
649
650 const bool use_grayscale = GPU_texture_component_len(GPU_texture_format(tex)) == 1;
651
652 int2 offset = int2(x, y);
653 std::optional<int2> scaled_size;
654 if (scaled) {
656 offset, int2(w, h), int2(limit_w, limit_h), int2(ibuf->x, ibuf->y));
657 }
658
659 if (layer >= 0) {
660 offset += tile_offset;
661 }
662
663 GPUTextureUpload upload;
665 ibuf, int2(x, y), int2(w, h), scaled_size, store_premultiplied, use_grayscale, upload);
666
667 BLI_rcti_init_minmax(&r_bounds);
668
669 if (upload.data) {
671 upload.format,
672 upload.data,
673 offset.x,
674 offset.y,
675 math::max(layer, 0),
676 upload.size.x,
677 upload.size.y,
678 1,
679 upload.stride);
680
682 &r_bounds, offset.x, offset.x + upload.size.x, offset.y, offset.y + upload.size.y);
683 }
684}
685
687 ImBuf *ibuf,
688 const bool store_premultiplied,
689 const imbuf::partial_update::Changes &changes,
690 const int layer,
691 const int2 tile_offset,
692 const int2 tile_size)
693{
694 /* For simplicity we require partial update and mipmap chunk sizes to match. */
696
697 /* For scaled images and UDIM atlases, we need to recompute the chunks rather than
698 * getting them directly from the changes. */
699 const bool compute_chunks = (GPU_texture_width(tex) != ibuf->x) ||
700 (GPU_texture_height(tex) != ibuf->y) ||
701 (layer >= 0 &&
702 (tile_offset != int2(0, 0) || tile_size != int2(ibuf->x, ibuf->y)));
703 BitVector<> modified_chunks;
704 int2 modified_chunks_size;
705 if (compute_chunks) {
706 modified_chunks_size = imb_gpu_mipmap_modified_chunks_size(tex);
707 modified_chunks.resize(int64_t(modified_chunks_size.x) * modified_chunks_size.y, false);
708 }
709
710 /* Update modified regions and gather modified chunks for mipmap update. */
711 rcti buffer_rect;
712 BLI_rcti_init(&buffer_rect, 0, ibuf->x, 0, ibuf->y);
713 for (const rcti &region : changes.modified_regions()) {
714 rcti clipped;
715 if (!BLI_rcti_isect(&buffer_rect, &region, &clipped)) {
716 continue;
717 }
718 rcti bounds;
720 ibuf,
721 store_premultiplied,
722 clipped.xmin,
723 clipped.ymin,
724 BLI_rcti_size_x(&clipped),
725 BLI_rcti_size_y(&clipped),
726 layer,
727 tile_offset,
728 tile_size,
729 bounds);
730 if (compute_chunks) {
731 imb_gpu_mipmap_modified_chunks_mark(modified_chunks, modified_chunks_size, bounds);
732 }
733 }
734
735 /* Partial mipmap update. */
737 math::max(layer, 0),
738 compute_chunks ? BitSpan(modified_chunks) :
739 BitSpan(changes.modified_chunks));
741}
742
743static void imb_gpu_texture_apply_partial_updates(ImBuf *ibuf, const bool use_premult)
744{
745 if (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr) {
746 return;
747 }
748
753 switch (changes.kind) {
754 case Changes::Kind::Full:
755 case Changes::Kind::Resized:
757 ibuf->gpu.texture = nullptr;
759 break;
760 case Changes::Kind::Partial:
761 /* A compressed texture can't be updated in place, need to recreate. */
764 ibuf->gpu.texture = nullptr;
766 break;
767 }
769 ibuf->gpu.texture, ibuf, use_premult, changes, -1, int2(0), int2(0));
770 ibuf->gpu.partial_update_changeset_id = new_changeset_id;
771 break;
772 case Changes::Kind::None:
773 ibuf->gpu.partial_update_changeset_id = new_changeset_id;
774 break;
775 }
776}
777
779 ImBuf *ibuf,
780 const GPUTextureCreateFlags texture_create_flags,
781 bool try_only)
782{
783 if (ibuf == nullptr || (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr &&
784 ibuf->gpu.texture == nullptr))
785 {
786 return nullptr;
787 }
788
789 std::scoped_lock lock(ibuf->gpu.mutex);
790 if (ibuf->gpu.texture != nullptr) {
792 ibuf, flag_is_set(texture_create_flags, GPUTextureCreateFlags::Premultiplied));
793 if (ibuf->gpu.texture != nullptr) {
796 return ibuf->gpu.texture;
797 }
798 }
799 if (try_only) {
800 return nullptr;
801 }
802
803 /* Ensure update tracking before reading pixels, so changes by other threads
804 * while the creating the texture are preserved. */
807
808 gpu::Texture *tex = IMB_create_gpu_texture(name, ibuf, texture_create_flags);
809 if (tex == nullptr) {
812 return nullptr;
813 }
815
818
819 ibuf->gpu.partial_update_changeset_id = changeset_id;
820 ibuf->gpu.texture = tex;
822 GPU_texture_ref(tex);
823 return tex;
824}
825
827 bool high_bitdepth,
828 bool use_grayscale)
829{
830 gpu::TextureFormat gpu_texture_format;
831 imb_gpu_get_format(ibuf, high_bitdepth, use_grayscale, &gpu_texture_format);
832 return gpu_texture_format;
833}
834
836{
837 if (!ibuf) {
838 return;
839 }
840
841 std::scoped_lock lock(ibuf->gpu.mutex);
842 if (ibuf->gpu.texture) {
844 ibuf->gpu.texture = nullptr;
845 }
848}
849
851{
852 if (!ibuf) {
853 return;
854 }
855
856 std::scoped_lock lock(ibuf->gpu.mutex);
857 if (ibuf->gpu.texture) {
859 ibuf->gpu.texture = nullptr;
860 }
863 ibuf->gpu.texture = texture;
864}
865
867{
868 const float half_min = -65504;
869 const float half_max = 65504;
870 if (!image_buffer->float_data()) {
871 return;
872 }
873
874 float *rect_float = image_buffer->float_data_for_write();
875
876 int rect_float_len = image_buffer->x * image_buffer->y *
877 (image_buffer->channels == 0 ? 4 : image_buffer->channels);
878
879 for (int i = 0; i < rect_float_len; i++) {
880 rect_float[i] = clamp_f(rect_float[i], half_min, half_max);
881 }
882}
883
884} // namespace blender
Platform independent time functions.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define DDS_COMPRESSED_DXT3
#define DDS_COMPRESSED_DXT1
#define DDS_COMPRESSED_DXT5
Read Blender memory allocation and freeing.
volatile int lock
#define UNPACK2(a)
btMatrix3x3 scaled(const btVector3 &s) const
Create a scaled copy of the matrix.
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
BitSpan()=default
void resize(const int64_t new_size_in_bits, const bool value=false)
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
#define layer
#define ceil
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define OPENEXR_HALF
#define T
T max(const T &a, const T &b)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
bool IMB_gpu_get_compressed_format(const ImBuf *ibuf, gpu::TextureFormat *r_texture_format)
Definition util_gpu.cc:115
uint padding(uint offset, uint alignment)
void IMB_assign_gpu_texture(ImBuf *ibuf, gpu::Texture *texture)
Definition util_gpu.cc:850
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_compressed_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
GPUTextureCreateFlags
Definition IMB_imbuf.hh:611
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void imb_gpu_extract_first_channel(const T *src, const int channels, const int stride, const int w, const int h, T *r_gray)
Definition util_gpu.cc:141
int64_t BLI_time_now_seconds_i()
Definition time.cc:36
static int2 imb_gpu_texture_update_offset_size(int2 &offset, const int2 size, const int2 limit, const int2 full)
Definition util_gpu.cc:597
bool BLI_rcti_isect(const struct rcti *src1, const struct rcti *src2, struct rcti *dest)
void GPU_texture_update_mipmap_chain(gpu::Texture *texture)
constexpr int GPU_TEXTURE_MIPMAP_UPDATE_CHUNK_SIZE
static void imb_gpu_texture_default_init_mipmap(gpu::Texture *tex)
Definition util_gpu.cc:386
void BLI_rcti_init_minmax(struct rcti *rect)
Definition rct.cc:488
gpu::TextureFormat IMB_gpu_get_texture_format(const ImBuf *ibuf, bool high_bitdepth, bool use_grayscale)
Definition util_gpu.cc:826
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
MINLINE int max_ii(int a, int b)
static void imb_gpu_texture_apply_partial_updates(ImBuf *ibuf, const bool use_premult)
Definition util_gpu.cc:743
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
static void imb_gpu_texture_update_region(gpu::Texture *tex, ImBuf *ibuf, const bool store_premultiplied, int x, int y, int w, int h, const int layer, const int2 tile_offset, const int2 tile_size, rcti &r_bounds)
Definition util_gpu.cc:622
imbuf::ChangesetID IMB_partial_update_changeset_id_current()
static void imb_gpu_mipmap_modified_chunks_mark(MutableBitSpan modified_chunks, const int2 size, const rcti &bounds)
Definition util_gpu.cc:577
void IMB_colormanagement_imbuf_to_byte_texture(unsigned char *out_buffer, int offset_x, int offset_y, int width, int height, const ImBuf *ibuf, bool store_premultiplied)
void GPU_texture_update_sub(gpu::Texture *texture, eGPUDataFormat data_format, const void *pixels, int offset_x, int offset_y, int offset_z, int width, int height, int depth, uint unpack_row_length=0)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
bool IMB_colormanagement_space_is_scene_linear(const ColorSpace *colorspace)
bool IMB_colormanagement_space_is_scene_linear_srgb(const ColorSpace *colorspace)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
static GPUTextureConversion imb_gpu_texture_conversion(const ImBuf *ibuf, const bool is_grayscale, const bool store_premultiplied)
Definition util_gpu.cc:154
unsigned char uchar
Definition BKE_text.h:58
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_HOST_READ
uint8_t * imb_load_dds_compressed_data(const char *filepath, int width, int height, int &r_mipcount)
void IMB_gpu_clamp_half_float(ImBuf *image_buffer)
Definition util_gpu.cc:866
void GPU_texture_update_mipmap_chain_partial(gpu::Texture *texture, int layer, BitSpan modified_chunks)
void GPU_texture_swizzle_set(gpu::Texture *texture, const char swizzle[4])
unsigned char uint8_t
Definition BKE_text.h:79
int GPU_texture_width(const gpu::Texture *texture)
void IMB_scale_box(const float *src_buffer, int2 src_size, int channels, float *dst_buffer, int2 dst_size, bool threaded, int src_stride=0)
Definition scaling.cc:351
static void imb_gpu_texture_default_init(gpu::Texture *tex, const ImBuf *ibuf)
Definition util_gpu.cc:380
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
size_t GPU_texture_component_len(gpu::TextureFormat format)
bool BLI_rcti_is_empty(const struct rcti *rect)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
static void get_gpu_texture_data(ImBuf *source_buffer, const int2 src_offset, const int2 src_size, const std::optional< int2 > scaled_size, const bool premultiplied_alpha, const bool is_grayscale, GPUTextureUpload &r_upload)
Definition util_gpu.cc:225
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
static void imb_gpu_texture_default_init_array(gpu::Texture *tex)
Definition util_gpu.cc:392
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
bool IMB_alpha_affects_rgb(const ImBuf *ibuf)
Definition conversion.cc:45
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
bool GPU_texture_has_compressed_format(const gpu::Texture *texture)
imbuf::ChangesetID IMB_partial_update_changeset_id_next()
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
bool IMB_colormanagement_space_is_data(const ColorSpace *colorspace)
gpu::Texture * IMB_acquire_gpu_texture(const char *name, ImBuf *ibuf, GPUTextureCreateFlags texture_create_flags, bool try_only=false)
Definition util_gpu.cc:778
gpu::Texture * IMB_create_gpu_texture(const char *name, ImBuf *ibuf, GPUTextureCreateFlags flags)
Definition util_gpu.cc:452
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
void IMB_free_gpu_textures(ImBuf *ibuf)
Definition util_gpu.cc:835
MINLINE uint divide_ceil_u(uint a, uint b)
static bool imb_is_grayscale_texture_format_compatible(const ImBuf *ibuf)
Definition util_gpu.cc:40
static CLG_LogRef LOG
Definition logging.cpp:12
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
void IMB_partial_update_flush(ImBuf *ibuf)
void GPU_texture_unbind(gpu::Texture *texture)
GPUTextureConversion
Definition util_gpu.cc:152
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
MINLINE int is_power_of_2_i(int n)
static int2 imb_gpu_mipmap_modified_chunks_size(gpu::Texture *tex)
Definition util_gpu.cc:570
MINLINE int min_ii(int a, int b)
void IMB_colormanagement_imbuf_to_float_texture(float *out_buffer, int offset_x, int offset_y, int width, int height, const ImBuf *ibuf, bool store_premultiplied)
static const char * imb_gpu_get_swizzle(const ImBuf *ibuf)
Definition util_gpu.cc:109
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
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
static void imb_gpu_get_format(const ImBuf *ibuf, bool high_bitdepth, bool use_grayscale, gpu::TextureFormat *r_texture_format)
Definition util_gpu.cc:71
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
int GPU_texture_size_with_limit(int res)
MINLINE float clamp_f(float value, float min, float max)
void GPU_texture_ref(gpu::Texture *texture)
eGPUTextureUsage GPU_texture_mipmap_usage(gpu::TextureFormat format)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eGPUDataFormat format
Definition util_gpu.cc:211
const ColorSpace * colorspace
const ColorSpace * colorspace
std::atomic< imbuf::ChangesetID > partial_update_changeset_id
std::atomic< gpu::Texture * > texture
blender::Mutex mutex
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
std::string filepath
ImColorMode color_mode
void assign_byte_data(uint8_t *data)
eImbFileType ftype
float * float_data_for_write()
Definition allocimbuf.cc:79