Blender V4.5
gpu_texture_private.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_assert.h"
12
13#include "GPU_vertex_buffer.hh"
14
16
17namespace blender::gpu {
18
20 /* The format has a depth component and can be used as depth attachment. */
21 GPU_FORMAT_DEPTH = (1 << 0),
22 /* The format has a stencil component and can be used as stencil attachment. */
24 /* The format represent non-normalized integers data, either signed or unsigned. */
26 /* The format is using normalized integers, either signed or unsigned. */
28 /* The format represent floating point data, either signed or unsigned. */
29 GPU_FORMAT_FLOAT = (1 << 4),
30 /* The format is using block compression. */
32 /* The format is using sRGB encoded storage. */
33 GPU_FORMAT_SRGB = (1 << 6),
34 /* The format can store negative values. */
36
38};
39
41
54
56
57/* Format types for samplers within the shader.
58 * This covers the sampler format type permutations within GLSL/MSL. */
63 /* Special case for depth, as these require differing dummy formats. */
66};
67
69
70#ifndef NDEBUG
71# define DEBUG_NAME_LEN 64
72#else
73# define DEBUG_NAME_LEN 8
74#endif
75
76/* Maximum number of FBOs a texture can be attached to. */
77#define GPU_TEX_MAX_FBO_ATTACHED 32
78
83class Texture {
84 public:
88 int refcount = 1;
90 int src_w = 0, src_h = 0;
91#ifndef GPU_NO_USE_PY_REFERENCES
96 void **py_ref = nullptr;
97#endif
98
99 protected:
100 /* ---- Texture format (immutable after init). ---- */
102 int w_, h_, d_;
111
113 /* TODO(fclem): Should become immutable and the need for mipmaps should be specified upfront. */
114 int mipmaps_ = -1;
116 int mip_min_ = 0, mip_max_ = 0;
117
120
124
125 public:
126 Texture(const char *name);
127 virtual ~Texture();
128
129 /* Return true on success. */
130 bool init_1D(int w, int layers, int mip_len, eGPUTextureFormat format);
131 bool init_2D(int w, int h, int layers, int mip_len, eGPUTextureFormat format);
132 bool init_3D(int w, int h, int d, int mip_len, eGPUTextureFormat format);
133 bool init_cubemap(int w, int layers, int mip_len, eGPUTextureFormat format);
135 bool init_view(GPUTexture *src,
137 eGPUTextureType type,
138 int mip_start,
139 int mip_len,
140 int layer_start,
141 int layer_len,
142 bool cube_as_array,
143 bool use_stencil);
144
145 virtual void generate_mipmap() = 0;
146 virtual void copy_to(Texture *tex) = 0;
147 virtual void clear(eGPUDataFormat format, const void *data) = 0;
148 virtual void swizzle_set(const char swizzle_mask[4]) = 0;
149 virtual void mip_range_set(int min, int max) = 0;
150 virtual void *read(int mip, eGPUDataFormat format) = 0;
151
154 void update(eGPUDataFormat format, const void *data);
155
156 void usage_set(eGPUTextureUsage usage_flags);
157
158 virtual void update_sub(
159 int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data) = 0;
160 virtual void update_sub(int offset[3],
161 int extent[3],
163 GPUPixelBuffer *pixbuf) = 0;
164
165 /* TODO(fclem): Legacy. Should be removed at some point. */
166 virtual uint gl_bindcode_get() const = 0;
167 int width_get() const
168 {
169 return w_;
170 }
171 int height_get() const
172 {
173 return h_;
174 }
175 int depth_get() const
176 {
177 return d_;
178 }
180 {
182 }
183
184 void mip_size_get(int mip, int r_size[3]) const
185 {
186 /* TODO: assert if lvl is below the limit of 1px in each dimension. */
187 int div = 1 << mip;
188 r_size[0] = max_ii(1, w_ / div);
189
191 r_size[1] = h_;
192 }
193 else if (h_ > 0) {
194 r_size[1] = max_ii(1, h_ / div);
195 }
196
198 r_size[2] = d_;
199 }
200 else if (d_ > 0) {
201 r_size[2] = max_ii(1, d_ / div);
202 }
203 }
204
205 int mip_width_get(int mip) const
206 {
207 return max_ii(1, w_ / (1 << mip));
208 }
209 int mip_height_get(int mip) const
210 {
211 return (type_ == GPU_TEXTURE_1D_ARRAY) ? h_ : max_ii(1, h_ / (1 << mip));
212 }
213 int mip_depth_get(int mip) const
214 {
215 return (type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE)) ? d_ : max_ii(1, d_ / (1 << mip));
216 }
217
218 /* Return number of dimension taking the array type into account. */
220 {
221 const int array = (type_ & GPU_TEXTURE_ARRAY) ? 1 : 0;
222 switch (type_ & ~GPU_TEXTURE_ARRAY) {
224 return 1;
225 case GPU_TEXTURE_1D:
226 return 1 + array;
227 case GPU_TEXTURE_2D:
228 return 2 + array;
229 case GPU_TEXTURE_CUBE:
230 case GPU_TEXTURE_3D:
231 default:
232 return 3;
233 }
234 }
235 /* Return number of array layer (or face layer) for texture array or 1 for the others. */
236 int layer_count() const
237 {
238 switch (type_) {
240 return h_;
243 return d_;
244 default:
245 return 1;
246 }
247 }
248
249 int mip_count() const
250 {
251 return mipmaps_;
252 }
253
255 {
256 return format_;
257 }
259 {
260 return format_flag_;
261 }
263 {
264 return type_;
265 }
267 {
268 switch (format_) {
272 BLI_assert(slot == 0);
276 BLI_assert(slot == 0);
278 default:
279 /* Valid color attachment formats. */
280 return GPU_FB_COLOR_ATTACHMENT0 + slot;
281
282 case GPU_RGB16F:
283 case GPU_RGBA16_SNORM:
284 case GPU_RGBA8_SNORM:
285 case GPU_RGB32F:
286 case GPU_RGB32I:
287 case GPU_RGB32UI:
288 case GPU_RGB16_SNORM:
289 case GPU_RGB16I:
290 case GPU_RGB16UI:
291 case GPU_RGB16:
292 case GPU_RGB8_SNORM:
293 case GPU_RGB8:
294 case GPU_RGB8I:
295 case GPU_RGB8UI:
296 case GPU_RG16_SNORM:
297 case GPU_RG8_SNORM:
298 case GPU_R16_SNORM:
299 case GPU_R8_SNORM:
303 case GPU_RGBA8_DXT1:
304 case GPU_RGBA8_DXT3:
305 case GPU_RGBA8_DXT5:
306 case GPU_SRGB8:
307 case GPU_RGB9_E5:
308 BLI_assert_msg(0, "Texture cannot be attached to a framebuffer because of its type");
310 }
311 }
312
313 protected:
314 virtual bool init_internal() = 0;
315 virtual bool init_internal(VertBuf *vbo) = 0;
316 virtual bool init_internal(GPUTexture *src,
317 int mip_offset,
318 int layer_offset,
319 bool use_stencil) = 0;
320};
321
322/* Syntactic sugar. */
323static inline GPUTexture *wrap(Texture *vert)
324{
325 return reinterpret_cast<GPUTexture *>(vert);
326}
327static inline Texture *unwrap(GPUTexture *vert)
328{
329 return reinterpret_cast<Texture *>(vert);
330}
331static inline const Texture *unwrap(const GPUTexture *vert)
332{
333 return reinterpret_cast<const Texture *>(vert);
334}
335
336/* GPU pixel Buffer. */
338 protected:
339 size_t size_ = 0;
340
341 public:
343 virtual ~PixelBuffer() = default;
344
345 virtual void *map() = 0;
346 virtual void unmap() = 0;
348 virtual size_t get_size() = 0;
349};
350
351/* Syntactic sugar. */
352static inline GPUPixelBuffer *wrap(PixelBuffer *pixbuf)
353{
354 return reinterpret_cast<GPUPixelBuffer *>(pixbuf);
355}
356static inline PixelBuffer *unwrap(GPUPixelBuffer *pixbuf)
357{
358 return reinterpret_cast<PixelBuffer *>(pixbuf);
359}
360static inline const PixelBuffer *unwrap(const GPUPixelBuffer *pixbuf)
361{
362 return reinterpret_cast<const PixelBuffer *>(pixbuf);
363}
364
365#undef DEBUG_NAME_LEN
366
368{
369 switch (format) {
370 /* Formats texture & render-buffer */
371 case GPU_RGBA8UI:
372 case GPU_RGBA8I:
373 case GPU_RGBA8:
374 return (4 * 8) / 8;
375 case GPU_RGBA32UI:
376 case GPU_RGBA32I:
377 case GPU_RGBA32F:
378 return (4 * 32) / 8;
379 case GPU_RGBA16UI:
380 case GPU_RGBA16I:
381 case GPU_RGBA16F:
382 case GPU_RGBA16:
383 return (4 * 16) / 8;
384 case GPU_RG8UI:
385 case GPU_RG8I:
386 case GPU_RG8:
387 return (2 * 8) / 8;
388 case GPU_RG32UI:
389 case GPU_RG32I:
390 case GPU_RG32F:
391 return (2 * 32) / 8;
392 case GPU_RG16UI:
393 case GPU_RG16I:
394 case GPU_RG16F:
395 case GPU_RG16:
396 return (2 * 16) / 8;
397 case GPU_R8UI:
398 case GPU_R8I:
399 case GPU_R8:
400 return 8 / 8;
401 case GPU_R32UI:
402 case GPU_R32I:
403 case GPU_R32F:
404 return 32 / 8;
405 case GPU_R16UI:
406 case GPU_R16I:
407 case GPU_R16F:
408 case GPU_R16:
409 return 16 / 8;
410
411 /* Special formats texture & render-buffer */
412 case GPU_RGB10_A2:
413 case GPU_RGB10_A2UI:
414 return (3 * 10 + 2) / 8;
416 return (11 + 11 + 10) / 8;
418 /* 32-bit depth, 8 bits stencil, and 24 unused bits. */
419 return (32 + 8 + 24) / 8;
421 return (24 + 8) / 8;
422 case GPU_SRGB8_A8:
423 return (3 * 8 + 8) / 8;
424
425 /* Texture only formats. */
426 case GPU_RGB16F:
427 case GPU_RGB16_SNORM:
428 case GPU_RGB16I:
429 case GPU_RGB16UI:
430 case GPU_RGB16:
431 return (3 * 16) / 8;
432 case GPU_RGBA16_SNORM:
433 return (4 * 16) / 8;
434 case GPU_RGBA8_SNORM:
435 return (4 * 8) / 8;
436 case GPU_RGB32F:
437 case GPU_RGB32I:
438 case GPU_RGB32UI:
439 return (3 * 32) / 8;
440 case GPU_RGB8_SNORM:
441 case GPU_RGB8:
442 case GPU_RGB8I:
443 case GPU_RGB8UI:
444 return (3 * 8) / 8;
445 case GPU_RG16_SNORM:
446 return (2 * 16) / 8;
447 case GPU_RG8_SNORM:
448 return (2 * 8) / 8;
449 case GPU_R16_SNORM:
450 return (1 * 16) / 8;
451 case GPU_R8_SNORM:
452 return (1 * 8) / 8;
453
454 /* Special formats, texture only. */
458 case GPU_RGBA8_DXT1:
459 case GPU_RGBA8_DXT3:
460 case GPU_RGBA8_DXT5:
461 /* Incorrect but actual size is fractional. */
462 return 1;
463 case GPU_SRGB8:
464 return (3 * 8) / 8;
465 case GPU_RGB9_E5:
466 return (3 * 9 + 5) / 8;
467
468 /* Depth Formats. */
470 return 32 / 8;
472 /* Depth component 24 uses 3 bytes to store the depth value, and reserved 1 byte for
473 * alignment. */
474 return (24 + 8) / 8;
476 return 16 / 8;
477 }
479 return 0;
480}
481
482inline size_t to_block_size(eGPUTextureFormat data_type)
483{
484 switch (data_type) {
486 case GPU_RGBA8_DXT1:
487 return 8;
490 case GPU_RGBA8_DXT3:
491 case GPU_RGBA8_DXT5:
492 return 16;
493 default:
494 BLI_assert_msg(0, "Texture format is not a compressed format");
495 return 0;
496 }
497}
498
500{
501 switch (format) {
502 /* Formats texture & render-buffer */
503 case GPU_RGBA8UI:
504 return GPU_FORMAT_INTEGER;
505 case GPU_RGBA8I:
507 case GPU_RGBA8:
509 case GPU_RGBA32UI:
510 return GPU_FORMAT_INTEGER;
511 case GPU_RGBA32I:
513 case GPU_RGBA32F:
515 case GPU_RGBA16UI:
516 return GPU_FORMAT_INTEGER;
517 case GPU_RGBA16I:
519 case GPU_RGBA16F:
521 case GPU_RGBA16:
523 case GPU_RG8UI:
524 return GPU_FORMAT_INTEGER;
525 case GPU_RG8I:
527 case GPU_RG8:
529 case GPU_RG32UI:
530 return GPU_FORMAT_INTEGER;
531 case GPU_RG32I:
533 case GPU_RG32F:
535 case GPU_RG16UI:
536 return GPU_FORMAT_INTEGER;
537 case GPU_RG16I:
539 case GPU_RG16F:
541 case GPU_RG16:
543 case GPU_R8UI:
544 return GPU_FORMAT_INTEGER;
545 case GPU_R8I:
547 case GPU_R8:
549 case GPU_R32UI:
550 return GPU_FORMAT_INTEGER;
551 case GPU_R32I:
553 case GPU_R32F:
555 case GPU_R16UI:
556 return GPU_FORMAT_INTEGER;
557 case GPU_R16I:
559 case GPU_R16F:
561 case GPU_R16:
563
564 /* Special formats texture & render-buffer */
565 case GPU_RGB10_A2:
567 case GPU_RGB10_A2UI:
568 return GPU_FORMAT_INTEGER;
570 return GPU_FORMAT_FLOAT;
574 case GPU_SRGB8_A8:
576
577 /* Texture only formats. */
578 case GPU_RGB16F:
580 case GPU_RGB16_SNORM:
582 case GPU_RGB16I:
584 case GPU_RGB16UI:
585 return GPU_FORMAT_INTEGER;
586 case GPU_RGB16:
588 case GPU_RGBA16_SNORM:
589 case GPU_RGBA8_SNORM:
591 case GPU_RGB32F:
593 case GPU_RGB32I:
595 case GPU_RGB32UI:
596 return GPU_FORMAT_INTEGER;
597 case GPU_RGB8_SNORM:
599 case GPU_RGB8:
601 case GPU_RGB8I:
603 case GPU_RGB8UI:
604 return GPU_FORMAT_INTEGER;
605 case GPU_RG16_SNORM:
606 case GPU_RG8_SNORM:
607 case GPU_R16_SNORM:
608 case GPU_R8_SNORM:
610
611 /* Special formats, texture only. */
616 case GPU_RGBA8_DXT1:
617 case GPU_RGBA8_DXT3:
618 case GPU_RGBA8_DXT5:
620 case GPU_SRGB8:
622 case GPU_RGB9_E5:
623 return GPU_FORMAT_FLOAT;
624
625 /* Depth Formats. */
629 return GPU_FORMAT_DEPTH;
630 }
632 return GPU_FORMAT_FLOAT;
633}
634
636{
637 switch (format) {
638 /* Formats texture & render-buffer */
639 case GPU_RGBA8UI:
640 case GPU_RGBA8I:
641 case GPU_RGBA8:
642 case GPU_RGBA32UI:
643 case GPU_RGBA32I:
644 case GPU_RGBA32F:
645 case GPU_RGBA16UI:
646 case GPU_RGBA16I:
647 case GPU_RGBA16F:
648 case GPU_RGBA16:
649 return 4;
650 case GPU_RG8UI:
651 case GPU_RG8I:
652 case GPU_RG8:
653 case GPU_RG32UI:
654 case GPU_RG32I:
655 case GPU_RG32F:
656 case GPU_RG16UI:
657 case GPU_RG16I:
658 case GPU_RG16F:
659 case GPU_RG16:
660 return 2;
661 case GPU_R8UI:
662 case GPU_R8I:
663 case GPU_R8:
664 case GPU_R32UI:
665 case GPU_R32I:
666 case GPU_R32F:
667 case GPU_R16UI:
668 case GPU_R16I:
669 case GPU_R16F:
670 case GPU_R16:
671 return 1;
672
673 /* Special formats texture & render-buffer */
674 case GPU_RGB10_A2:
675 case GPU_RGB10_A2UI:
676 return 4;
678 return 3;
681 /* Only count depth component. */
682 return 1;
683 case GPU_SRGB8_A8:
684 return 4;
685
686 /* Texture only formats. */
687 case GPU_RGB16F:
688 case GPU_RGB16_SNORM:
689 case GPU_RGB16I:
690 case GPU_RGB16UI:
691 case GPU_RGB16:
692 return 3;
693 case GPU_RGBA16_SNORM:
694 case GPU_RGBA8_SNORM:
695 return 4;
696 case GPU_RGB32F:
697 case GPU_RGB32I:
698 case GPU_RGB32UI:
699 case GPU_RGB8_SNORM:
700 case GPU_RGB8:
701 case GPU_RGB8I:
702 case GPU_RGB8UI:
703 return 3;
704 case GPU_RG16_SNORM:
705 case GPU_RG8_SNORM:
706 return 2;
707 case GPU_R16_SNORM:
708 case GPU_R8_SNORM:
709 return 1;
710
711 /* Special formats, texture only. */
715 case GPU_RGBA8_DXT1:
716 case GPU_RGBA8_DXT3:
717 case GPU_RGBA8_DXT5:
718 return 4;
719 case GPU_SRGB8:
720 case GPU_RGB9_E5:
721 return 3;
722
723 /* Depth Formats. */
727 return 1;
728 }
730 return 1;
731}
732
733inline size_t to_bytesize(eGPUDataFormat data_format)
734{
735 switch (data_format) {
736 case GPU_DATA_UBYTE:
737 return 1;
739 return 2;
740 case GPU_DATA_FLOAT:
741 case GPU_DATA_INT:
742 case GPU_DATA_UINT:
743 return 4;
747 return 4;
748 }
750 return 0;
751}
752
753inline size_t to_bytesize(eGPUTextureFormat tex_format, eGPUDataFormat data_format)
754{
755 /* Special case for compacted types.
756 * Standard component len calculation does not apply, as the texture formats contain multiple
757 * channels, but associated data format contains several compacted components. */
758 if ((tex_format == GPU_R11F_G11F_B10F && data_format == GPU_DATA_10_11_11_REV) ||
759 ((tex_format == GPU_RGB10_A2 || tex_format == GPU_RGB10_A2UI) &&
760 data_format == GPU_DATA_2_10_10_10_REV))
761 {
762 return 4;
763 }
764
765 return to_component_len(tex_format) * to_bytesize(data_format);
766}
767
768/* Definitely not complete, edit according to the gl specification. */
769constexpr bool validate_data_format(eGPUTextureFormat tex_format, eGPUDataFormat data_format)
770{
771 switch (tex_format) {
772 /* Formats texture & render-buffer */
773 case GPU_RGBA32UI:
774 case GPU_RG32UI:
775 case GPU_R32UI:
776 return ELEM(data_format, GPU_DATA_UINT);
777 case GPU_RGBA16UI:
778 case GPU_RG16UI:
779 case GPU_R16UI:
780 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_USHORT if needed. */
781 case GPU_RGBA8UI:
782 case GPU_RG8UI:
783 case GPU_R8UI:
784 return ELEM(data_format, GPU_DATA_UINT, GPU_DATA_UBYTE);
785
786 case GPU_RGBA32I:
787 case GPU_RG32I:
788 case GPU_R32I:
789 return ELEM(data_format, GPU_DATA_INT);
790 case GPU_RGBA16I:
791 case GPU_RG16I:
792 case GPU_R16I:
793 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_SHORT if needed. */
794 case GPU_RGBA8I:
795 case GPU_RG8I:
796 case GPU_R8I:
797 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_BYTE if needed. */
798
799 case GPU_RGBA32F:
800 case GPU_RG32F:
801 case GPU_R32F:
802 return ELEM(data_format, GPU_DATA_FLOAT);
803 case GPU_RGBA16F:
804 case GPU_RG16F:
805 case GPU_R16F:
806 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_HALF_FLOAT);
807 case GPU_RGBA16:
808 case GPU_RG16:
809 case GPU_R16:
810 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_USHORT if needed. */
811 case GPU_RGBA8:
812 case GPU_RG8:
813 case GPU_R8:
814 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
815
816 /* Special formats texture & render-buffer */
817 case GPU_RGB10_A2:
818 case GPU_RGB10_A2UI:
819 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_2_10_10_10_REV);
821 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_10_11_11_REV);
823 /* Should have its own type. For now, we rely on the backend to do the conversion. */
827 case GPU_SRGB8_A8:
828 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
829
830 /* Texture only formats. */
831 case GPU_RGB32UI:
832 return ELEM(data_format, GPU_DATA_UINT);
833 case GPU_RGB16UI:
834 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_SHORT if needed. */
835 case GPU_RGB8UI:
836 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_BYTE if needed. */
837 case GPU_RGB32I:
838 return ELEM(data_format, GPU_DATA_INT);
839 case GPU_RGB16I:
840 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_USHORT if needed. */
841 case GPU_RGB8I:
842 return ELEM(data_format, GPU_DATA_INT, GPU_DATA_UBYTE);
843 case GPU_RGB16:
844 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_USHORT if needed. */
845 case GPU_RGB8:
846 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
847 case GPU_RGBA16_SNORM:
848 case GPU_RGB16_SNORM:
849 case GPU_RG16_SNORM:
850 case GPU_R16_SNORM:
851 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_SHORT if needed. */
852 case GPU_RGBA8_SNORM:
853 case GPU_RGB8_SNORM:
854 case GPU_RG8_SNORM:
855 case GPU_R8_SNORM:
856 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_BYTE if needed. */
857 case GPU_RGB32F:
858 return ELEM(data_format, GPU_DATA_FLOAT);
859 case GPU_RGB16F:
860 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_HALF_FLOAT);
861
862 /* Special formats, texture only. */
866 case GPU_RGBA8_DXT1:
867 case GPU_RGBA8_DXT3:
868 case GPU_RGBA8_DXT5:
869 /* TODO(fclem): GPU_DATA_COMPRESSED for each compression? Wouldn't it be overkill?
870 * For now, expect format to be set to float. */
871 return ELEM(data_format, GPU_DATA_FLOAT);
872 case GPU_SRGB8:
873 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
874 case GPU_RGB9_E5:
875 return ELEM(data_format, GPU_DATA_FLOAT);
876
877 /* Depth Formats. */
881 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UINT);
882 }
884 return data_format == GPU_DATA_FLOAT;
885}
886
887/* Return default data format for an internal texture format. */
889{
890 switch (tex_format) {
891 /* Formats texture & render-buffer */
892 case GPU_RGBA32UI:
893 case GPU_RG32UI:
894 case GPU_R32UI:
895 case GPU_RGBA16UI:
896 case GPU_RG16UI:
897 case GPU_R16UI:
898 case GPU_RGBA8UI:
899 case GPU_RG8UI:
900 case GPU_R8UI:
901 return GPU_DATA_UINT;
902
903 case GPU_RGBA32I:
904 case GPU_RG32I:
905 case GPU_R32I:
906 case GPU_RGBA16I:
907 case GPU_RG16I:
908 case GPU_R16I:
909 case GPU_RGBA8I:
910 case GPU_RG8I:
911 case GPU_R8I:
912 return GPU_DATA_INT;
913
914 case GPU_RGBA32F:
915 case GPU_RG32F:
916 case GPU_R32F:
917 case GPU_RGBA16F:
918 case GPU_RG16F:
919 case GPU_R16F:
920 case GPU_RGBA16:
921 case GPU_RG16:
922 case GPU_R16:
923 case GPU_RGBA8:
924 case GPU_RG8:
925 case GPU_R8:
926 return GPU_DATA_FLOAT;
927
928 /* Special formats texture & render-buffer */
929 case GPU_RGB10_A2:
930 case GPU_RGB10_A2UI:
935 /* Should have its own type. For now, we rely on the backend to do the conversion. */
938 return GPU_DATA_UINT_24_8;
939 case GPU_SRGB8_A8:
940 return GPU_DATA_FLOAT;
941
942 /* Texture only formats. */
943 case GPU_RGB32UI:
944 case GPU_RGB16UI:
945 case GPU_RGB8UI:
946 return GPU_DATA_UINT;
947 case GPU_RGB32I:
948 case GPU_RGB16I:
949 case GPU_RGB8I:
950 return GPU_DATA_INT;
951 case GPU_RGB16:
952 case GPU_RGB8:
953 return GPU_DATA_FLOAT;
954 case GPU_RGBA16_SNORM:
955 case GPU_RGB16_SNORM:
956 case GPU_RG16_SNORM:
957 case GPU_R16_SNORM:
958 return GPU_DATA_FLOAT;
959 case GPU_RGBA8_SNORM:
960 case GPU_RGB8_SNORM:
961 case GPU_RG8_SNORM:
962 case GPU_R8_SNORM:
963 return GPU_DATA_FLOAT;
964 case GPU_RGB32F:
965 case GPU_RGB16F:
966 return GPU_DATA_FLOAT;
967
968 /* Special formats, texture only. */
972 case GPU_RGBA8_DXT1:
973 case GPU_RGBA8_DXT3:
974 case GPU_RGBA8_DXT5:
975 /* TODO(fclem): GPU_DATA_COMPRESSED for each compression? Wouldn't it be overkill?
976 * For now, expect format to be set to float. */
977 return GPU_DATA_FLOAT;
978 case GPU_SRGB8:
979 return GPU_DATA_FLOAT;
980 case GPU_RGB9_E5:
981 return GPU_DATA_FLOAT;
982
983 /* Depth Formats. */
987 return GPU_DATA_FLOAT;
988 }
990 return GPU_DATA_FLOAT;
991}
992
994{
995 switch (tex_format) {
996 /* Formats texture & render-buffer */
997 case GPU_RGBA32UI:
998 case GPU_RG32UI:
999 case GPU_R32UI:
1000 case GPU_RGBA16UI:
1001 case GPU_RG16UI:
1002 case GPU_R16UI:
1003 case GPU_RGBA8UI:
1004 case GPU_RG8UI:
1005 case GPU_R8UI:
1006 case GPU_RGBA32I:
1007 case GPU_RG32I:
1008 case GPU_R32I:
1009 case GPU_RGBA16I:
1010 case GPU_RG16I:
1011 case GPU_R16I:
1012 case GPU_RGBA8I:
1013 case GPU_RG8I:
1014 case GPU_R8I:
1015 case GPU_RGBA32F:
1016 case GPU_RG32F:
1017 case GPU_R32F:
1018 case GPU_RGBA16F:
1019 case GPU_RG16F:
1020 case GPU_R16F:
1021 case GPU_RGBA16:
1022 case GPU_RG16:
1023 case GPU_R16:
1024 case GPU_RGBA8:
1025 case GPU_RG8:
1026 case GPU_R8:
1027 return GPU_COLOR_BIT;
1028
1029 /* Special formats texture & render-buffer */
1030 case GPU_RGB10_A2:
1031 case GPU_RGB10_A2UI:
1032 case GPU_R11F_G11F_B10F:
1033 case GPU_SRGB8_A8:
1034 return GPU_COLOR_BIT;
1038
1039 /* Depth Formats. */
1043 return GPU_DEPTH_BIT;
1044
1045 /* Texture only formats. */
1046 case GPU_RGB32UI:
1047 case GPU_RGB16UI:
1048 case GPU_RGB8UI:
1049 case GPU_RGB32I:
1050 case GPU_RGB16I:
1051 case GPU_RGB8I:
1052 case GPU_RGB16:
1053 case GPU_RGB8:
1054 case GPU_RGBA16_SNORM:
1055 case GPU_RGB16_SNORM:
1056 case GPU_RG16_SNORM:
1057 case GPU_R16_SNORM:
1058 case GPU_RGBA8_SNORM:
1059 case GPU_RGB8_SNORM:
1060 case GPU_RG8_SNORM:
1061 case GPU_R8_SNORM:
1062 case GPU_RGB32F:
1063 case GPU_RGB16F:
1064 BLI_assert_msg(0, "This texture format is not compatible with framebuffer attachment.");
1065 return GPU_COLOR_BIT;
1066
1067 /* Special formats, texture only. */
1068 case GPU_SRGB8_A8_DXT1:
1069 case GPU_SRGB8_A8_DXT3:
1070 case GPU_SRGB8_A8_DXT5:
1071 case GPU_RGBA8_DXT1:
1072 case GPU_RGBA8_DXT3:
1073 case GPU_RGBA8_DXT5:
1074 case GPU_SRGB8:
1075 case GPU_RGB9_E5:
1076 BLI_assert_msg(0, "This texture format is not compatible with framebuffer attachment.");
1077 return GPU_COLOR_BIT;
1078 }
1080 return GPU_COLOR_BIT;
1081}
1082
1084{
1085 if (format->attr_len == 0) {
1086 BLI_assert_msg(0, "Incorrect vertex format for buffer texture");
1087 return GPU_DEPTH_COMPONENT24;
1088 }
1089 switch (format->attrs[0].comp_len) {
1090 case 1:
1091 switch (format->attrs[0].comp_type) {
1092 case GPU_COMP_I8:
1093 return GPU_R8I;
1094 case GPU_COMP_U8:
1095 return GPU_R8UI;
1096 case GPU_COMP_I16:
1097 return GPU_R16I;
1098 case GPU_COMP_U16:
1099 return GPU_R16UI;
1100 case GPU_COMP_I32:
1101 return GPU_R32I;
1102 case GPU_COMP_U32:
1103 return GPU_R32UI;
1104 case GPU_COMP_F32:
1105 return GPU_R32F;
1106 default:
1107 break;
1108 }
1109 break;
1110 case 2:
1111 switch (format->attrs[0].comp_type) {
1112 case GPU_COMP_I8:
1113 return GPU_RG8I;
1114 case GPU_COMP_U8:
1115 return GPU_RG8UI;
1116 case GPU_COMP_I16:
1117 return GPU_RG16I;
1118 case GPU_COMP_U16:
1119 return GPU_RG16UI;
1120 case GPU_COMP_I32:
1121 return GPU_RG32I;
1122 case GPU_COMP_U32:
1123 return GPU_RG32UI;
1124 case GPU_COMP_F32:
1125 return GPU_RG32F;
1126 default:
1127 break;
1128 }
1129 break;
1130 case 3:
1131 /* Not supported until GL 4.0 */
1132 break;
1133 case 4:
1134 switch (format->attrs[0].comp_type) {
1135 case GPU_COMP_I8:
1136 return GPU_RGBA8I;
1137 case GPU_COMP_U8:
1138 return GPU_RGBA8UI;
1139 case GPU_COMP_I16:
1140 return GPU_RGBA16I;
1141 case GPU_COMP_U16:
1142 /* NOTE: Checking the fetch mode to select the right GPU texture format. This can be
1143 * added to other formats as well. */
1144 switch (format->attrs[0].fetch_mode) {
1145 case GPU_FETCH_INT:
1146 return GPU_RGBA16UI;
1148 return GPU_RGBA16;
1149 case GPU_FETCH_FLOAT:
1150 return GPU_RGBA16F;
1151 }
1152 /* Should be handled above, assert below. */
1153 break;
1154 case GPU_COMP_I32:
1155 return GPU_RGBA32I;
1156 case GPU_COMP_U32:
1157 return GPU_RGBA32UI;
1158 case GPU_COMP_F32:
1159 return GPU_RGBA32F;
1160 default:
1161 break;
1162 }
1163 break;
1164 default:
1165 break;
1166 }
1167 BLI_assert_msg(0, "Unsupported vertex format for buffer texture");
1168 return GPU_DEPTH_COMPONENT24;
1169}
1170
1171} // namespace blender::gpu
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define ATTR_FALLTHROUGH
MINLINE int max_ii(int a, int b)
unsigned int uint
#define ENUM_OPERATORS(_type, _max)
#define ELEM(...)
eGPUFrameBufferBits
@ GPU_DEPTH_BIT
@ GPU_STENCIL_BIT
@ GPU_COLOR_BIT
eGPUDataFormat
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_UINT_24_8
@ GPU_DATA_INT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UBYTE
@ GPU_DATA_UINT
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_FLOAT
eGPUTextureUsage
eGPUTextureFormat
@ GPU_RGB16
@ GPU_R16UI
@ GPU_RGB8
@ GPU_RG16F
@ GPU_DEPTH32F_STENCIL8
@ GPU_R32F
@ GPU_SRGB8
@ GPU_R16I
@ GPU_SRGB8_A8
@ GPU_RG8_SNORM
@ GPU_DEPTH24_STENCIL8
@ GPU_RGB10_A2
@ GPU_RGB8I
@ GPU_R32I
@ GPU_RGBA8_SNORM
@ GPU_RGB10_A2UI
@ GPU_RG8UI
@ GPU_R16F
@ GPU_RGB16I
@ GPU_RGBA16_SNORM
@ GPU_RGB9_E5
@ GPU_SRGB8_A8_DXT5
@ GPU_RG8I
@ GPU_RG16I
@ GPU_RG32UI
@ GPU_RGB32I
@ GPU_RGBA32F
@ GPU_RGBA16F
@ GPU_RG8
@ GPU_RG32I
@ GPU_SRGB8_A8_DXT1
@ GPU_RG16
@ GPU_RGBA32UI
@ GPU_R8I
@ GPU_R16
@ GPU_RG16UI
@ GPU_RGBA8I
@ GPU_RGBA8_DXT1
@ GPU_RGBA8UI
@ GPU_RGB32F
@ GPU_RGBA16UI
@ GPU_RGBA16I
@ GPU_R8UI
@ GPU_RGBA16
@ GPU_SRGB8_A8_DXT3
@ GPU_RGB8_SNORM
@ GPU_RGBA8_DXT3
@ GPU_RGB32UI
@ GPU_R8_SNORM
@ GPU_RG32F
@ GPU_R8
@ GPU_RGB16_SNORM
@ GPU_DEPTH_COMPONENT24
@ GPU_RG16_SNORM
@ GPU_RGB8UI
@ GPU_RGB16F
@ GPU_RGB16UI
@ GPU_R32UI
@ GPU_RGBA32I
@ GPU_RGBA8_DXT5
@ GPU_DEPTH_COMPONENT32F
@ GPU_R16_SNORM
@ GPU_DEPTH_COMPONENT16
@ GPU_R11F_G11F_B10F
@ GPU_RGBA8
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT_TO_FLOAT_UNIT
@ GPU_FETCH_INT
@ GPU_COMP_U16
@ GPU_COMP_F32
@ GPU_COMP_I32
@ GPU_COMP_I8
@ GPU_COMP_U32
@ GPU_COMP_I16
@ GPU_COMP_U8
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
virtual void unmap()=0
virtual void * map()=0
virtual ~PixelBuffer()=default
virtual GPUPixelBufferNativeHandle get_native_handle()=0
virtual size_t get_size()=0
virtual void mip_range_set(int min, int max)=0
virtual uint gl_bindcode_get() const =0
void attach_to(FrameBuffer *fb, GPUAttachmentType type)
bool init_view(GPUTexture *src, eGPUTextureFormat format, eGPUTextureType type, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
virtual bool init_internal(GPUTexture *src, int mip_offset, int layer_offset, bool use_stencil)=0
virtual bool init_internal(VertBuf *vbo)=0
int mip_width_get(int mip) const
int mip_depth_get(int mip) const
eGPUTextureFormatFlag format_flag_get() const
virtual void * read(int mip, eGPUDataFormat format)=0
virtual void generate_mipmap()=0
eGPUTextureFormat format_get() const
eGPUTextureFormatFlag format_flag_
void update(eGPUDataFormat format, const void *data)
eGPUTextureUsage gpu_image_usage_flags_
eGPUTextureUsage usage_get() const
char name_[DEBUG_NAME_LEN]
FrameBuffer * fb_[GPU_TEX_MAX_FBO_ATTACHED]
virtual void swizzle_set(const char swizzle_mask[4])=0
virtual void update_sub(int offset[3], int extent[3], eGPUDataFormat format, GPUPixelBuffer *pixbuf)=0
GPUAttachmentType fb_attachment_[GPU_TEX_MAX_FBO_ATTACHED]
GPUAttachmentType attachment_type(int slot) const
bool init_1D(int w, int layers, int mip_len, eGPUTextureFormat format)
int mip_height_get(int mip) const
virtual void clear(eGPUDataFormat format, const void *data)=0
eGPUTextureType type_get() const
bool init_buffer(VertBuf *vbo, eGPUTextureFormat format)
virtual bool init_internal()=0
bool init_cubemap(int w, int layers, int mip_len, eGPUTextureFormat format)
virtual void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data)=0
void mip_size_get(int mip, int r_size[3]) const
virtual void copy_to(Texture *tex)=0
void usage_set(eGPUTextureUsage usage_flags)
bool init_3D(int w, int h, int d, int mip_len, eGPUTextureFormat format)
Texture(const char *name)
void detach_from(FrameBuffer *fb)
bool init_2D(int w, int h, int layers, int mip_len, eGPUTextureFormat format)
@ GPU_FB_DEPTH_STENCIL_ATTACHMENT
@ GPU_FB_COLOR_ATTACHMENT0
@ GPU_FB_DEPTH_ATTACHMENT
#define DEBUG_NAME_LEN
#define GPU_TEX_MAX_FBO_ATTACHED
BLI_INLINE float fb(float length, float L)
format
static Context * unwrap(GPUContext *ctx)
size_t to_block_size(eGPUTextureFormat data_type)
static GPUContext * wrap(Context *ctx)
constexpr bool validate_data_format(eGPUTextureFormat tex_format, eGPUDataFormat data_format)
eGPUFrameBufferBits to_framebuffer_bits(eGPUTextureFormat tex_format)
int to_bytesize(const DataFormat format)
eGPUTextureFormatFlag to_format_flag(eGPUTextureFormat format)
int to_component_len(eGPUTextureFormat format)
constexpr DataFormat to_data_format(TextureFormat format)
constexpr TextureFormat to_texture_format(TextureTargetFormat format)
#define min(a, b)
Definition sort.cc:36
static constexpr GPUSamplerState default_sampler()
max
Definition text_draw.cc:251