Blender V5.3
gpu_texture.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string.hh"
10
11#include "DNA_userdef_types.h"
12
13#include "GPU_framebuffer.hh"
14#include "GPU_texture.hh"
15
16#include "gpu_backend.hh"
19
20#include "gpu_shader_private.hh"
22
23namespace blender {
24
25namespace gpu {
26
27/* -------------------------------------------------------------------- */
30
31Texture::Texture(const char *name)
32{
33 if ((G.debug & G_DEBUG_GPU) && name) {
34 name_ = name;
35 }
36
38}
39
41{
42 fb_attachments_.foreach_item(
43 [&](FrameBuffer *key, const GPUAttachmentType value) { key->attachment_remove(value); });
44 fb_attachments_.clear();
45
46#ifndef GPU_NO_USE_PY_REFERENCES
47 if (this->py_ref) {
48 *this->py_ref = nullptr;
49 }
50#endif
51}
52
53bool Texture::init_1D(int w, int layers, int mip_len, TextureFormat format)
54{
55 w_ = w;
56 h_ = layers;
57 d_ = 0;
58 int mip_len_max = 1 + floorf(log2f(w));
59 mipmaps_ = min_ii(mip_len, mip_len_max);
61 format_ = format;
63 type_ = (layers > 0) ? GPU_TEXTURE_1D_ARRAY : GPU_TEXTURE_1D;
66 }
67 return this->init_internal();
68}
69
70bool Texture::init_2D(int w, int h, int layers, int mip_len, TextureFormat format)
71{
72 w_ = w;
73 h_ = h;
74 d_ = layers;
75 int mip_len_max = 1 + floorf(log2f(max_ii(w, h)));
76 mipmaps_ = min_ii(mip_len, mip_len_max);
78 format_ = format;
80 type_ = (layers > 0) ? GPU_TEXTURE_2D_ARRAY : GPU_TEXTURE_2D;
83 }
84 return this->init_internal();
85}
86
87bool Texture::init_3D(int w, int h, int d, int mip_len, TextureFormat format)
88{
89 w_ = w;
90 h_ = h;
91 d_ = d;
92 int mip_len_max = 1 + floorf(log2f(std::max({w, h, d})));
93 mipmaps_ = min_ii(mip_len, mip_len_max);
95 format_ = format;
100 }
101 return this->init_internal();
102}
103
104bool Texture::init_cubemap(int w, int layers, int mip_len, TextureFormat format)
105{
106 w_ = w;
107 h_ = w;
108 d_ = max_ii(1, layers) * 6;
109 int mip_len_max = 1 + floorf(log2f(w));
110 mipmaps_ = min_ii(mip_len, mip_len_max);
111 mip_max_ = mip_min_ + mipmaps_ - 1;
112 format_ = format;
117 }
118 return this->init_internal();
119}
120
122{
123 /* See to_texture_format(). */
125 h_ = 0;
126 d_ = 0;
127 format_ = format;
130 return this->init_internal(vbo);
131}
132
134 TextureFormat format,
135 GPUTextureType type,
136 int mip_start,
137 int mip_len,
138 int layer_start,
139 int layer_len,
140 bool cube_as_array,
141 bool use_stencil)
142{
143 BLI_assert(source_texture_ == nullptr);
144 source_texture_ = src;
147
148 int view_extent[3]{0, 0, 0};
149 src->mip_size_get(mip_start, view_extent);
150
151 view_layer_start_ = min_ii(layer_start, src->layer_count() - 1);
152
153 type_ = type;
154 if (cube_as_array) {
157 }
158
159 if (type & GPU_TEXTURE_ARRAY) {
160 view_extent[src->dimensions_count() - 1] = std::min(layer_len,
161 (src->layer_count() - view_layer_start_));
162 }
163 else if (src->type_get() & GPU_TEXTURE_ARRAY) {
164 view_extent[src->dimensions_count()] = 0;
165 }
166
167 w_ = view_extent[0];
168 h_ = view_extent[1];
169 d_ = view_extent[2];
170
171 mip_min_ = min_ii(mip_start, src->mipmaps_ - 1);
172 mipmaps_ = min_ii(mip_len, (src->mipmaps_ - mip_min_));
173 mip_max_ = mip_min_ + mipmaps_ - 1;
174 format_ = format;
176
177 return this->init_internal(src, use_stencil);
178}
179
181{
182 gpu_image_usage_flags_ = usage_flags;
183}
184
186
187/* -------------------------------------------------------------------- */
190
192{
193 GPUAttachmentType &current_type = fb_attachments_.lookup_or_add(fb, type);
194 if (current_type != type) {
195 fb->attachment_remove(current_type);
196 current_type = type;
197 }
198}
199
201{
202 std::optional<GPUAttachmentType> type = fb_attachments_.pop_try(fb);
203 if (type.has_value()) {
204 fb->attachment_remove(*type);
205 }
206 else {
207 BLI_assert_msg(0, "GPU: Error: Texture: Framebuffer is not attached");
208 };
209}
210
211void Texture::update(eGPUDataFormat format, const void *data)
212{
213 int mip = 0;
214 int extent[3] = {1, 1, 1};
215 int offset[3] = {0, 0, 0};
216 this->mip_size_get(mip, extent);
217 this->update_sub(mip, offset, extent, format, data);
218}
219
221
222} // namespace gpu
223
224/* -------------------------------------------------------------------- */
227
228using namespace blender::gpu;
229
230/* ------ Memory Management ------ */
231
233{
234 /* TODO(fclem): Do that inside the new Texture class. */
235 return 0;
236}
237
238/* ------ Creation ------ */
239
240static inline gpu::Texture *gpu_texture_create(const char *name,
241 const int w,
242 const int h,
243 const int d,
244 const GPUTextureType type,
245 int mip_len,
246 TextureFormat tex_format,
247 eGPUTextureUsage usage,
248 const void *pixels,
249 eGPUDataFormat data_format = GPU_DATA_FLOAT)
250{
251 BLI_assert(mip_len > 0);
252 Texture *tex = GPUBackend::get()->texture_alloc(name);
253 tex->usage_set(usage);
254
255 bool success = false;
256 switch (type) {
257 case GPU_TEXTURE_1D:
259 success = tex->init_1D(w, h, mip_len, tex_format);
260 break;
261 case GPU_TEXTURE_2D:
263 success = tex->init_2D(w, h, d, mip_len, tex_format);
264 break;
265 case GPU_TEXTURE_3D:
266 success = tex->init_3D(w, h, d, mip_len, tex_format);
267 break;
268 case GPU_TEXTURE_CUBE:
270 success = tex->init_cubemap(w, d, mip_len, tex_format);
271 break;
272 default:
273 break;
274 }
275
276 if (!success) {
277 delete tex;
278 return nullptr;
279 }
280 if (pixels) {
281 tex->update(data_format, pixels);
282 }
283 return reinterpret_cast<gpu::Texture *>(tex);
284}
285
287 int width,
288 int mip_len,
289 TextureFormat format,
290 eGPUTextureUsage usage,
291 const float *data)
292{
293 return gpu_texture_create(name, width, 0, 0, GPU_TEXTURE_1D, mip_len, format, usage, data);
294}
295
297 int width,
298 int layer_len,
299 int mip_len,
300 TextureFormat format,
301 eGPUTextureUsage usage,
302 const float *data)
303{
304 return gpu_texture_create(
305 name, width, layer_len, 0, GPU_TEXTURE_1D_ARRAY, mip_len, format, usage, data);
306}
307
309 int width,
310 int height,
311 int mip_len,
312 TextureFormat format,
313 eGPUTextureUsage usage,
314 const float *data)
315{
316 return gpu_texture_create(name, width, height, 0, GPU_TEXTURE_2D, mip_len, format, usage, data);
317}
318
320 int width,
321 int height,
322 int layer_len,
323 int mip_len,
324 TextureFormat format,
325 eGPUTextureUsage usage,
326 const float *data)
327{
328 return gpu_texture_create(
329 name, width, height, layer_len, GPU_TEXTURE_2D_ARRAY, mip_len, format, usage, data);
330}
331
333 int width,
334 int height,
335 int depth,
336 int mip_len,
337 TextureFormat texture_format,
338 eGPUTextureUsage usage,
339 const void *data)
340{
341 return gpu_texture_create(
342 name, width, height, depth, GPU_TEXTURE_3D, mip_len, texture_format, usage, data);
343}
344
346 int width,
347 int mip_len,
348 TextureFormat format,
349 eGPUTextureUsage usage,
350 const float *data)
351{
352 return gpu_texture_create(name, width, width, 0, GPU_TEXTURE_CUBE, mip_len, format, usage, data);
353}
354
356 int width,
357 int layer_len,
358 int mip_len,
359 TextureFormat format,
360 eGPUTextureUsage usage,
361 const float *data)
362{
363 return gpu_texture_create(
364 name, width, width, layer_len, GPU_TEXTURE_CUBE_ARRAY, mip_len, format, usage, data);
365}
366
368 int width,
369 int height,
370 int mip_len,
371 TextureFormat tex_format,
372 eGPUTextureUsage usage,
373 const void *data)
374{
375 Texture *tex = GPUBackend::get()->texture_alloc(name);
376 tex->usage_set(usage);
377 bool success = tex->init_2D(width, height, 0, mip_len, tex_format);
378
379 if (!success) {
380 delete tex;
381 return nullptr;
382 }
383 if (data) {
384 size_t ofs = 0;
385 for (int mip = 0; mip < mip_len; mip++) {
386 int extent[3] = {1, 1, 1};
387 int offset[3] = {0, 0, 0};
388 tex->mip_size_get(mip, extent);
389
390 size_t size = ((extent[0] + 3) / 4) * ((extent[1] + 3) / 4) * to_block_size(tex_format);
391 tex->update_sub(mip,
392 offset,
393 extent,
394 to_texture_data_format(tex_format),
395 static_cast<uchar *>(const_cast<void *>(data)) + ofs);
396
397 ofs += size;
398 }
399 }
400 return reinterpret_cast<gpu::Texture *>(tex);
401}
402
404{
405#ifndef NDEBUG
406 /* Vertex buffers used for texture buffers must be flagged with:
407 * GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY. */
409 "Vertex Buffers used for textures should have usage flag "
410 "GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY.");
411#endif
413 Texture *tex = GPUBackend::get()->texture_alloc(name);
414
415 bool success = tex->init_buffer(vert, tex_format);
416 if (!success) {
417 delete tex;
418 return nullptr;
419 }
420 return reinterpret_cast<gpu::Texture *>(tex);
421}
422
423gpu::Texture *GPU_texture_create_error(int dimension, bool is_array)
424{
425 const float pixel[4] = {1.0f, 0.0f, 1.0f, 1.0f};
426 int w = 1;
427 int h = (dimension < 2 && !is_array) ? 0 : 1;
428 int d = (dimension < 3 && !is_array) ? 0 : 1;
429
431 type = (dimension == 2) ? (is_array ? GPU_TEXTURE_2D_ARRAY : GPU_TEXTURE_2D) : type;
432 type = (dimension == 1) ? (is_array ? GPU_TEXTURE_1D_ARRAY : GPU_TEXTURE_1D) : type;
433
434 return gpu_texture_create("invalid_tex",
435 w,
436 h,
437 d,
438 type,
439 1,
440 TextureFormat::UNORM_8_8_8_8,
442 pixel);
443}
444
446 gpu::Texture *source_texture,
447 TextureFormat view_format,
448 int mip_start,
449 int mip_len,
450 int layer_start,
451 int layer_len,
452 bool cube_as_array,
453 bool use_stencil)
454{
455 BLI_assert(mip_len > 0);
456 BLI_assert(layer_len > 0);
457 BLI_assert_msg(use_stencil == false ||
459 "Source texture of TextureView must have GPU_TEXTURE_USAGE_FORMAT_VIEW usage "
460 "flag if view texture uses stencil texturing.");
461 BLI_assert_msg((view_format == GPU_texture_format(source_texture)) ||
463 "Source texture of TextureView must have GPU_TEXTURE_USAGE_FORMAT_VIEW usage "
464 "flag if view texture format is different.");
465 Texture *view = GPUBackend::get()->texture_alloc(name);
466 view->init_view(source_texture,
467 view_format,
468 source_texture->type_get(),
469 mip_start,
470 mip_len,
471 layer_start,
472 layer_len,
473 cube_as_array,
474 use_stencil);
475
476 /* On integer textures, disable filtering by default, as this is not guaranteed to be
477 * consistently supported across backends. */
479 view->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, false);
480 view->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_MIPMAP, false);
481 view->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK,
482 false);
483 }
484
485 return view;
486}
487
488/* ------ Usage ------ */
490{
491 return texture->usage_get();
492}
493
494/* ------ Update ------ */
495
497 int mip_level,
498 eGPUDataFormat data_format,
499 const void *pixels,
500 uint unpack_row_length)
501{
502 int extent[3] = {1, 1, 1}, offset[3] = {0, 0, 0};
503 texture->mip_size_get(mip_level, extent);
504 texture->update_sub(mip_level, offset, extent, data_format, pixels, unpack_row_length);
505}
506
508 eGPUDataFormat data_format,
509 const void *pixels,
510 int offset_x,
511 int offset_y,
512 int offset_z,
513 int width,
514 int height,
515 int depth,
516 uint unpack_row_length)
517{
518 int offset[3] = {offset_x, offset_y, offset_z};
519 int extent[3] = {width, height, depth};
520 tex->update_sub(0, offset, extent, data_format, pixels, unpack_row_length);
521}
522
524 eGPUDataFormat data_format,
525 GPUPixelBuffer *pixel_buf,
526 int offset_x,
527 int offset_y,
528 int offset_z,
529 int width,
530 int height,
531 int depth)
532{
533 int offset[3] = {offset_x, offset_y, offset_z};
534 int extent[3] = {width, height, depth};
535 texture->update_sub(offset, extent, data_format, pixel_buf);
536}
537
538void *GPU_texture_read(Texture *texture, eGPUDataFormat data_format, int mip_level)
539{
541 size_t size = texture->read_size_get(mip_level, data_format);
542
543 void *data = MEM_new_uninitialized(size, __func__);
544 GPU_texture_read(texture, data_format, mip_level, data);
545 return data;
546}
547
548void GPU_texture_read(Texture *texture, eGPUDataFormat data_format, int mip_level, void *dst)
549{
553 "The host-read usage flag must be specified up-front. Only textures which require data "
554 "reads should be flagged, allowing the backend to make certain optimizations.");
555 texture->read(mip_level, data_format, dst);
556}
557
558size_t Texture::read_size_get(int mip, eGPUDataFormat format) const
559{
561 BLI_assert(mip <= mipmaps_ || mip == 0);
563 int extent[3] = {1, 1, 1};
564 this->mip_size_get(mip, extent);
565
566 size_t sample_len = extent[0] * std::max(1, extent[1]) * std::max(1, extent[2]);
567 size_t sample_size = to_bytesize(format_, format);
568 size_t size = sample_len * sample_size;
569
570 /* AMD Pro OpenGL drivers have a bug that write 8 bytes past buffer size
571 * if the texture is big. Note, this seemingly only affects cube map arrays. (see #66573) */
573 size += 8;
574 }
575
576 return size;
577}
578
579size_t GPU_texture_read_size_get(const Texture *texture, eGPUDataFormat data_format, int mip_level)
580{
582 return texture->read_size_get(mip_level, data_format);
583}
584
585void GPU_texture_clear(gpu::Texture *tex, eGPUDataFormat data_format, const void *data)
586{
587 BLI_assert(data != nullptr); /* Do not accept nullptr as parameter. */
588 BLI_assert(validate_data_format(tex->format_get(), data_format));
589
590 /* TODO(fclem): Ideally modify the GPU_texture_clear API. */
591 double4 clear_data(0xFFFFFFFFu);
592 int comp_len = to_component_len(tex->format_get());
593 switch (data_format) {
594 case GPU_DATA_FLOAT:
595 for (int i : IndexRange(comp_len)) {
596 clear_data[i] = static_cast<const float *>(data)[i];
597 }
598 break;
599 case GPU_DATA_INT:
600 for (int i : IndexRange(comp_len)) {
601 clear_data[i] = static_cast<const int *>(data)[i];
602 }
603 break;
604 case GPU_DATA_UINT:
605 for (int i : IndexRange(comp_len)) {
606 clear_data[i] = static_cast<const uint *>(data)[i];
607 }
608 break;
609 default:
610 BLI_assert_msg(0, "Unhandled data format");
611 return;
612 }
613 tex->clear(clear_data);
614}
615
616void GPU_texture_update(gpu::Texture *tex, eGPUDataFormat data_format, const void *data)
617{
618 tex->update(data_format, data);
619}
620
621/* ------ Binding ------ */
622
624{
625 Texture *tex = texture;
626 state = (state.type == GPU_SAMPLER_STATE_TYPE_INTERNAL) ? tex->sampler_state : state;
628}
629
631{
632 Texture *tex = texture;
633 Context::get()->state_manager->texture_bind(tex, tex->sampler_state, unit);
634}
635
641
646
648{
650}
651
656
661
663{
664 Texture *src = src_;
665 Texture *dst = dst_;
666 src->copy_to(dst, IndexRange(1));
667}
668
670{
671 Texture *tex = texture;
672 /* Only depth formats does support compare mode. */
673 BLI_assert(!(use_compare) || (tex->format_flag_get() & GPU_FORMAT_DEPTH));
674
675 tex->sampler_state.type = use_compare ? GPU_SAMPLER_STATE_TYPE_CUSTOM :
677 tex->sampler_state.custom_type = GPU_SAMPLER_CUSTOM_COMPARE;
678}
679
681{
682 Texture *tex = texture;
683 /* Stencil and integer format does not support filtering. */
684 BLI_assert(!(use_filter) ||
685 !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER)));
686 tex->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
687}
688
689void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
690{
691 Texture *tex = texture;
692 /* Stencil and integer format does not support filtering. */
693 BLI_assert(!(use_filter || use_mipmap) ||
694 !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER)));
695 tex->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_MIPMAP, use_mipmap);
696 tex->sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
697}
698
700{
701 Texture *tex = texture;
702 int samples = use_aniso ? U.anisotropic_filter : 1;
703 /* Stencil and integer format does not support filtering. */
704 BLI_assert(!(samples > 1) ||
705 !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER)));
707 tex->sampler_state.disable_filtering_flag(GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK);
708 tex->sampler_state.enable_filtering_flag(filtering);
709}
710
712{
713 texture->sampler_state.extend_x = extend_mode;
714}
715
717{
718 texture->sampler_state.extend_yz = extend_mode;
719}
720
722{
723 texture->sampler_state.extend_x = extend_mode;
724 texture->sampler_state.extend_yz = extend_mode;
725}
726
727void GPU_texture_swizzle_set(gpu::Texture *texture, const char swizzle[4])
728{
729 texture->swizzle_set(swizzle);
730}
731
733{
734 Texture *tex = texture;
735 tex->refcount--;
736
737 if (tex->refcount < 0) {
738 fprintf(stderr, "gpu::Texture: negative refcount\n");
739 }
740
741 if (tex->refcount == 0) {
742 delete tex;
743 }
744}
745
747{
748 texture->refcount++;
749}
750
752{
753 GPUTextureType type = texture->type_get();
754 if (type & GPU_TEXTURE_1D) {
755 return 1;
756 }
757 if (type & GPU_TEXTURE_2D) {
758 return 2;
759 }
760 if (type & GPU_TEXTURE_3D) {
761 return 3;
762 }
763 if (type & GPU_TEXTURE_CUBE) {
764 return 2;
765 }
766 /* GPU_TEXTURE_BUFFER */
767 return 1;
768}
769
771{
772 return texture->width_get();
773}
774
776{
777 return texture->height_get();
778}
779
781{
782 return texture->depth_get();
783}
784
786{
787 return texture->layer_count();
788}
789
791{
792 return texture->mip_count();
793}
794
796{
797 return texture->src_w;
798}
799
801{
802 return texture->src_h;
803}
804
806{
807 texture->src_w = w;
808 texture->src_h = h;
809}
810
812{
813 return texture->format_get();
814}
815
816const char *GPU_texture_format_name(TextureFormat texture_format)
817{
818 switch (texture_format) {
819 /* Formats texture & render-buffer */
820 case TextureFormat::UINT_8_8_8_8:
821 return "RGBA8UI";
822 case TextureFormat::SINT_8_8_8_8:
823 return "RGBA8I";
824 case TextureFormat::UNORM_8_8_8_8:
825 return "RGBA8";
826 case TextureFormat::UINT_32_32_32_32:
827 return "RGBA32UI";
828 case TextureFormat::SINT_32_32_32_32:
829 return "RGBA32I";
830 case TextureFormat::SFLOAT_32_32_32_32:
831 return "RGBA32F";
832 case TextureFormat::UINT_16_16_16_16:
833 return "RGBA16UI";
834 case TextureFormat::SINT_16_16_16_16:
835 return "RGBA16I";
836 case TextureFormat::SFLOAT_16_16_16_16:
837 return "RGBA16F";
838 case TextureFormat::UNORM_16_16_16_16:
839 return "RGBA16";
840 case TextureFormat::UINT_8_8:
841 return "RG8UI";
842 case TextureFormat::SINT_8_8:
843 return "RG8I";
844 case TextureFormat::UNORM_8_8:
845 return "RG8";
846 case TextureFormat::UINT_32_32:
847 return "RG32UI";
848 case TextureFormat::SINT_32_32:
849 return "RG32I";
850 case TextureFormat::SFLOAT_32_32:
851 return "RG32F";
852 case TextureFormat::UINT_16_16:
853 return "RG16UI";
854 case TextureFormat::SINT_16_16:
855 return "RG16I";
856 case TextureFormat::SFLOAT_16_16:
857 return "RG16F";
858 case TextureFormat::UNORM_16_16:
859 return "RG16";
860 case TextureFormat::UINT_8:
861 return "R8UI";
862 case TextureFormat::SINT_8:
863 return "R8I";
864 case TextureFormat::UNORM_8:
865 return "R8";
866 case TextureFormat::UINT_32:
867 return "R32UI";
868 case TextureFormat::SINT_32:
869 return "R32I";
870 case TextureFormat::SFLOAT_32:
871 return "R32F";
872 case TextureFormat::UINT_16:
873 return "R16UI";
874 case TextureFormat::SINT_16:
875 return "R16I";
876 case TextureFormat::SFLOAT_16:
877 return "R16F";
878 case TextureFormat::UNORM_16:
879 return "R16";
880 /* Special formats texture & render-buffer */
881 case TextureFormat::UNORM_10_10_10_2:
882 return "RGB10_A2";
883 case TextureFormat::UINT_10_10_10_2:
884 return "RGB10_A2UI";
885 case TextureFormat::UFLOAT_11_11_10:
886 return "R11F_G11F_B10F";
887 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
888 return "DEPTH32F_STENCIL8";
889 case TextureFormat::SRGBA_8_8_8_8:
890 return "SRGB8_A8";
891 /* Texture only formats. */
892 case TextureFormat::SFLOAT_16_16_16:
893 return "RGB16F";
894 case TextureFormat::SNORM_16_16_16:
895 return "RGB16_SNORM";
896 case TextureFormat::SINT_16_16_16:
897 return "RGB16I";
898 case TextureFormat::UINT_16_16_16:
899 return "RGB16UI";
900 case TextureFormat::UNORM_16_16_16:
901 return "RGB16";
902 case TextureFormat::SNORM_16_16_16_16:
903 return "RGBA16_SNORM";
904 case TextureFormat::SNORM_8_8_8_8:
905 return "RGBA8_SNORM";
906 case TextureFormat::SFLOAT_32_32_32:
907 return "RGB32F";
908 case TextureFormat::SINT_32_32_32:
909 return "RGB32I";
910 case TextureFormat::UINT_32_32_32:
911 return "RGB32UI";
912 case TextureFormat::SNORM_8_8_8:
913 return "RGB8_SNORM";
914 case TextureFormat::UNORM_8_8_8:
915 return "RGB8";
916 case TextureFormat::SINT_8_8_8:
917 return "RGB8I";
918 case TextureFormat::UINT_8_8_8:
919 return "RGB8UI";
920 case TextureFormat::SNORM_16_16:
921 return "RG16_SNORM";
922 case TextureFormat::SNORM_8_8:
923 return "RG8_SNORM";
924 case TextureFormat::SNORM_16:
925 return "R16_SNORM";
926 case TextureFormat::SNORM_8:
927 return "R8_SNORM";
928 /* Special formats, texture only. */
929 case TextureFormat::SRGB_DXT1:
930 return "SRGB8_A8_DXT1";
931 case TextureFormat::SRGB_DXT3:
932 return "SRGB8_A8_DXT3";
933 case TextureFormat::SRGB_DXT5:
934 return "SRGB8_A8_DXT5";
935 case TextureFormat::SNORM_DXT1:
936 return "RGBA8_DXT1";
937 case TextureFormat::SNORM_DXT3:
938 return "RGBA8_DXT3";
939 case TextureFormat::SNORM_DXT5:
940 return "RGBA8_DXT5";
941 case TextureFormat::SRGBA_8_8_8:
942 return "SRGB8";
943 case TextureFormat::UFLOAT_9_9_9_EXP_5:
944 return "RGB9_E5";
945 /* Depth Formats. */
946 case TextureFormat::SFLOAT_32_DEPTH:
947 return "DEPTH_COMPONENT32F";
948 case TextureFormat::UNORM_16_DEPTH:
949 return "DEPTH_COMPONENT16";
950
953 return "Invalid";
954 }
956 return "";
957}
958
960{
961 return (texture->format_flag_get() & GPU_FORMAT_DEPTH) != 0;
962}
963
965{
966 return (texture->format_flag_get() & GPU_FORMAT_STENCIL) != 0;
967}
968
970{
971 return (texture->format_flag_get() & GPU_FORMAT_INTEGER) != 0;
972}
973
975{
976 return (texture->format_flag_get() & GPU_FORMAT_FLOAT) != 0;
977}
978
980{
981 return (texture->format_flag_get() & GPU_FORMAT_NORMALIZED_INTEGER) != 0;
982}
983
985{
986 return (texture->format_flag_get() & GPU_FORMAT_SIGNED) != 0;
987}
988
990{
991 return (texture->format_flag_get() & GPU_FORMAT_COMPRESSED) != 0;
992}
993
995{
996 return (texture->type_get() & GPU_TEXTURE_CUBE) != 0;
997}
998
1000{
1001 return texture->is_texture_view();
1002}
1003
1005{
1006 return (texture->type_get() & GPU_TEXTURE_ARRAY) != 0;
1007}
1008
1009#ifndef GPU_NO_USE_PY_REFERENCES
1011{
1012 return texture->py_ref;
1013}
1014
1016{
1017 BLI_assert(py_ref == nullptr || texture->py_ref == nullptr);
1018 texture->py_ref = py_ref;
1019}
1020#endif
1021
1022void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
1023{
1024 texture->mip_size_get(mip_level, r_size);
1025}
1026
1028
1029/* -------------------------------------------------------------------- */
1034
1035GPUPixelBuffer *GPU_pixel_buffer_create(size_t size)
1036{
1037 /* Ensure buffer satisfies the alignment of 256 bytes for copying
1038 * data between buffers and textures. As specified in:
1039 * https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
1040 *
1041 * Ensuring minimal size across all platforms handles cases for small-sized
1042 * textures and avoids issues with zero-sized buffers. */
1043 size = ceil_to_multiple_ul(size, 256);
1044 PixelBuffer *pixbuf = GPUBackend::get()->pixelbuf_alloc(size);
1045 return wrap(pixbuf);
1046}
1047
1048void GPU_pixel_buffer_free(GPUPixelBuffer *pixel_buf)
1049{
1050 PixelBuffer *handle = unwrap(pixel_buf);
1051 delete handle;
1052}
1053
1054void *GPU_pixel_buffer_map(GPUPixelBuffer *pixel_buf)
1055{
1056 return unwrap(pixel_buf)->map();
1057}
1058
1059void GPU_pixel_buffer_unmap(GPUPixelBuffer *pixel_buf)
1060{
1061 unwrap(pixel_buf)->unmap();
1062}
1063
1064size_t GPU_pixel_buffer_size(GPUPixelBuffer *pixel_buf)
1065{
1066 return unwrap(pixel_buf)->get_size();
1067}
1068
1070{
1071 return unwrap(pixel_buf)->get_native_handle();
1072}
1073
1075
1076/* -------------------------------------------------------------------- */
1079
1081{
1082 return to_component_len(tex_format);
1083}
1084
1086{
1087 return to_bytesize(data_format);
1088}
1089
1091
1092} // namespace blender
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr IndexRange()=default
int3 size(int miplvl=0) const
static Context * get()
void attachment_remove(GPUAttachmentType type)
virtual PixelBuffer * pixelbuf_alloc(size_t size)=0
virtual Texture * texture_alloc(const char *name)=0
static GPUBackend * get()
virtual void texture_unbind_all()=0
virtual void texture_unbind(Texture *tex)=0
virtual void image_unbind_all()=0
virtual void image_unbind(Texture *tex)=0
virtual void image_bind(Texture *tex, int unit)=0
virtual void texture_bind(Texture *tex, GPUSamplerState sampler, int unit)=0
void attach_to(FrameBuffer *fb, GPUAttachmentType type)
bool init_1D(int w, int layers, int mip_len, TextureFormat format)
GPUTextureType type_get() const
void update(eGPUDataFormat format, const void *data)
eGPUTextureUsage gpu_image_usage_flags_
size_t read_size_get(int mip, eGPUDataFormat format) const
bool init_cubemap(int w, int layers, int mip_len, TextureFormat format)
bool init_view(Texture *src, TextureFormat format, GPUTextureType type, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
bool init_2D(int w, int h, int layers, int mip_len, TextureFormat format)
virtual void clear(const double4 data)=0
bool init_3D(int w, int h, int d, int mip_len, TextureFormat format)
bool init_buffer(VertBuf *vbo, TextureFormat format)
Map< FrameBuffer *, GPUAttachmentType > fb_attachments_
virtual void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data, uint unpack_row_length=0)=0
virtual bool init_internal()=0
void mip_size_get(int mip, int r_size[3]) const
TextureFormat format_get() const
void usage_set(eGPUTextureUsage usage_flags)
Texture(const char *name)
void detach_from(FrameBuffer *fb)
GPUTextureFormatFlag format_flag_
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
size_t to_block_size(TextureFormat data_type)
int to_bytesize(const DataFormat format)
int to_component_len(TextureFormat format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
BLI_INLINE Texture * wrap(MTLTexture *texture)
GPUTextureFormatFlag to_format_flag(TextureFormat format)
constexpr TextureFormat to_texture_format(TextureTargetFormat format)
BMesh Iterators.
void GPU_texture_compare_mode(gpu::Texture *texture, bool use_compare)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t GPU_texture_read_size_get(const gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
gpu::Texture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
bool GPU_texture_has_depth_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
int GPU_texture_depth(const gpu::Texture *texture)
bool GPU_texture_has_integer_format(const gpu::Texture *texture)
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)
static gpu::Texture * gpu_texture_create(const char *name, const int w, const int h, const int d, const GPUTextureType type, int mip_len, TextureFormat tex_format, eGPUTextureUsage usage, const void *pixels, eGPUDataFormat data_format=GPU_DATA_FLOAT)
bool GPU_texture_is_view(const gpu::Texture *texture)
unsigned int uint
Definition BKE_text.h:55
eGPUTextureUsage GPU_texture_usage(const gpu::Texture *texture)
BLI_INLINE float fb(float length, float L)
static constexpr GPUSamplerFiltering GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK
void GPU_texture_image_unbind(gpu::Texture *texture)
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
void GPU_texture_unbind_all()
bool GPU_texture_has_signed_format(const gpu::Texture *texture)
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)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE int max_ii(int a, int b)
void GPU_pixel_buffer_unmap(GPUPixelBuffer *pixel_buf)
void * GPU_pixel_buffer_map(GPUPixelBuffer *pixel_buf)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
void ** GPU_texture_py_reference_get(gpu::Texture *texture)
bool GPU_texture_has_normalized_format(const gpu::Texture *texture)
int GPU_texture_dimensions(const gpu::Texture *texture)
Global G
Definition blender.cc:57
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)
static GPUSamplerFiltering GPU_anisotropic_filtering_flags(int anisotropic_samples)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
size_t GPU_texture_dataformat_size(eGPUDataFormat data_format)
unsigned char uchar
Definition BKE_text.h:58
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_HOST_READ
int GPU_texture_mip_count(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
BMesh const char void * data
void GPU_texture_swizzle_set(gpu::Texture *texture, const char swizzle[4])
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
bool GPU_texture_has_float_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
void GPU_texture_original_size_set(gpu::Texture *texture, int width, int height)
int GPU_texture_width(const gpu::Texture *texture)
bool GPU_texture_is_cube(const gpu::Texture *texture)
void GPU_texture_update_mipmap(gpu::Texture *texture, int mip_level, eGPUDataFormat data_format, const void *pixels, uint unpack_row_length=0)
size_t GPU_texture_component_len(gpu::TextureFormat format)
void GPU_texture_bind_ex(gpu::Texture *texture, GPUSamplerState state, int unit)
bool GPU_texture_is_array(const gpu::Texture *texture)
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
bool GPU_texture_has_compressed_format(const gpu::Texture *texture)
void GPU_pixel_buffer_free(GPUPixelBuffer *pixel_buf)
@ GPU_SAMPLER_CUSTOM_COMPARE
size_t GPU_pixel_buffer_size(GPUPixelBuffer *pixel_buf)
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
@ GPU_SAMPLER_STATE_TYPE_PARAMETERS
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
GPUPixelBuffer * GPU_pixel_buffer_create(size_t byte_size)
const char * GPU_texture_format_name(gpu::TextureFormat format)
void GPU_texture_image_unbind_all()
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
gpu::Texture * GPU_texture_create_error(int dimension, bool array)
void GPU_texture_unbind(gpu::Texture *texture)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
int GPU_texture_layer_count(const gpu::Texture *texture)
int GPU_texture_original_height(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_view(const char *name, gpu::Texture *source_texture, gpu::TextureFormat view_format, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
MINLINE int min_ii(int a, int b)
void GPU_texture_py_reference_set(gpu::Texture *texture, void **py_ref)
int GPU_texture_original_width(const gpu::Texture *texture)
bool GPU_texture_has_stencil_format(const gpu::Texture *texture)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
unsigned int GPU_texture_memory_usage_get()
VecBase< double, 4 > double4
void GPU_texture_update_sub_from_pixel_buffer(gpu::Texture *texture, eGPUDataFormat data_format, GPUPixelBuffer *pixel_buf, int offset_x, int offset_y, int offset_z, int width, int height, int depth)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
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 GPU_texture_ref(gpu::Texture *texture)
GPUPixelBufferNativeHandle GPU_pixel_buffer_get_native_handle(GPUPixelBuffer *pixel_buf)
#define floorf
GPUSamplerFiltering filtering