Blender V5.3
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.hh"
12#include "BLI_enum_flags.hh"
13#include "BLI_map.hh"
14
15#include "GPU_vertex_buffer.hh"
16
18
19namespace blender::gpu {
20
21inline bool is_half_float(TextureFormat format)
22{
23 switch (format) {
24 case TextureFormat::SFLOAT_16_16_16_16:
25 case TextureFormat::SFLOAT_16_16_16:
26 case TextureFormat::SFLOAT_16_16:
27 case TextureFormat::SFLOAT_16:
28 return true;
29 default:
30 return false;
31 }
32}
33
34inline bool is_half_integer(TextureFormat format)
35{
36 switch (format) {
37 case TextureFormat::SINT_16_16_16_16:
38 case TextureFormat::SINT_16_16_16:
39 case TextureFormat::SINT_16_16:
40 case TextureFormat::SINT_16:
41 return true;
42 default:
43 return false;
44 }
45}
46
48 /* The format has a depth component and can be used as depth attachment. */
49 GPU_FORMAT_DEPTH = (1 << 0),
50 /* The format has a stencil component and can be used as stencil attachment. */
52 /* The format represent non-normalized integers data, either signed or unsigned. */
54 /* The format is using normalized integers, either signed or unsigned. */
56 /* The format represent floating point data, either signed or unsigned. */
57 GPU_FORMAT_FLOAT = (1 << 4),
58 /* The format is using block compression. */
60 /* The format is using sRGB encoded storage. */
61 GPU_FORMAT_SRGB = (1 << 6),
62 /* The format can store negative values. */
64
66};
67
69
82
84
85/* Format types for samplers within the shader.
86 * This covers the sampler format type permutations within GLSL/MSL. */
91 /* Special case for depth, as these require differing dummy formats. */
94};
95
97
98/* Maximum number of image units. */
99#define GPU_MAX_IMAGE 8
100
105class Texture {
106 public:
110 int refcount = 1;
112 int src_w = 0, src_h = 0;
113#ifndef GPU_NO_USE_PY_REFERENCES
118 void **py_ref = nullptr;
119#endif
120
121 protected:
122 /* ---- Texture format (immutable after init). ---- */
124 int w_, h_, d_;
133
135 /* TODO(fclem): Should become immutable and the need for mipmaps should be specified upfront. */
136 int mipmaps_ = -1;
137 int mip_min_ = 0, mip_max_ = 0;
138
142
144 std::string name_;
145
150
151 public:
152 Texture(const char *name);
153 virtual ~Texture();
154
155 /* Return true on success. */
156 bool init_1D(int w, int layers, int mip_len, TextureFormat format);
157 bool init_2D(int w, int h, int layers, int mip_len, TextureFormat format);
158 bool init_3D(int w, int h, int d, int mip_len, TextureFormat format);
159 bool init_cubemap(int w, int layers, int mip_len, TextureFormat format);
160 bool init_buffer(VertBuf *vbo, TextureFormat format);
161 bool init_view(Texture *src,
162 TextureFormat format,
163 GPUTextureType type,
164 int mip_start,
165 int mip_len,
166 int layer_start,
167 int layer_len,
168 bool cube_as_array,
169 bool use_stencil);
170
171 virtual void generate_mipmap() = 0;
172 virtual void copy_to(Texture *tex, IndexRange mip_levels) = 0;
173 virtual void clear(const double4 data) = 0;
174 virtual void swizzle_set(const char swizzle_mask[4]) = 0;
175 virtual void read(int mip, eGPUDataFormat format, void *dst) = 0;
176
179 void update(eGPUDataFormat format, const void *data);
180
181 void usage_set(eGPUTextureUsage usage_flags);
182
183 virtual void update_sub(int mip,
184 int offset[3],
185 int extent[3],
186 eGPUDataFormat format,
187 const void *data,
188 uint unpack_row_length = 0) = 0;
189 virtual void update_sub(int offset[3],
190 int extent[3],
191 eGPUDataFormat format,
192 GPUPixelBuffer *pixbuf) = 0;
193
194 int width_get() const
195 {
196 return w_;
197 }
198 int height_get() const
199 {
200 return h_;
201 }
202 int depth_get() const
203 {
204 return d_;
205 }
207 {
209 }
210 bool is_texture_view() const
211 {
212 return source_texture_ != nullptr;
213 }
214
215 size_t read_size_get(int mip, eGPUDataFormat format) const;
216
217 void mip_size_get(int mip, int r_size[3]) const
218 {
219 /* TODO: assert if lvl is below the limit of 1px in each dimension. */
220 int div = 1 << mip;
221 r_size[0] = max_ii(1, w_ / div);
222
224 r_size[1] = h_;
225 }
226 else if (h_ > 0) {
227 r_size[1] = max_ii(1, h_ / div);
228 }
229
231 r_size[2] = d_;
232 }
233 else if (d_ > 0) {
234 r_size[2] = max_ii(1, d_ / div);
235 }
236 }
237 int3 mip_size_get(int mip) const
238 {
239 int3 size = int3(1);
240 mip_size_get(mip, size);
241 return size;
242 }
243 int mip_width_get(int mip) const
244 {
245 return max_ii(1, w_ / (1 << mip));
246 }
247 int mip_height_get(int mip) const
248 {
249 return (type_ == GPU_TEXTURE_1D_ARRAY) ? h_ : max_ii(1, h_ / (1 << mip));
250 }
251 int mip_depth_get(int mip) const
252 {
253 return (type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE)) ? d_ : max_ii(1, d_ / (1 << mip));
254 }
255
256 /* Return number of dimension taking the array type into account. */
258 {
259 const int array = (type_ & GPU_TEXTURE_ARRAY) ? 1 : 0;
260 switch (type_ & ~GPU_TEXTURE_ARRAY) {
262 return 1;
263 case GPU_TEXTURE_1D:
264 return 1 + array;
265 case GPU_TEXTURE_2D:
266 return 2 + array;
267 case GPU_TEXTURE_CUBE:
268 case GPU_TEXTURE_3D:
269 default:
270 return 3;
271 }
272 }
273 /* Return number of array layer (or face layer) for texture array or 1 for the others. */
274 int layer_count() const
275 {
276 switch (type_) {
278 return h_;
280 case GPU_TEXTURE_CUBE:
282 return d_;
283 default:
284 return 1;
285 }
286 }
287
288 int mip_count() const
289 {
290 return mipmaps_;
291 }
292
294 {
295 return IndexRange(mip_min_, mip_max_ - mip_min_ + 1);
296 }
297
299 {
301 }
302
304 {
305 return format_;
306 }
308 {
309 return format_flag_;
310 }
312 {
313 return type_;
314 }
316 {
317 switch (format_) {
318 case TextureFormat::SFLOAT_32_DEPTH:
319 case TextureFormat::UNORM_16_DEPTH:
320 BLI_assert(slot == 0);
322 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
323 BLI_assert(slot == 0);
325 default:
326 /* Valid color attachment formats. */
327 return GPU_FB_COLOR_ATTACHMENT0 + slot;
328
329 case TextureFormat::SFLOAT_16_16_16:
330 case TextureFormat::SNORM_16_16_16_16:
331 case TextureFormat::SNORM_8_8_8_8:
332 case TextureFormat::SFLOAT_32_32_32:
333 case TextureFormat::SINT_32_32_32:
334 case TextureFormat::UINT_32_32_32:
335 case TextureFormat::SNORM_16_16_16:
336 case TextureFormat::SINT_16_16_16:
337 case TextureFormat::UINT_16_16_16:
338 case TextureFormat::UNORM_16_16_16:
339 case TextureFormat::SNORM_8_8_8:
340 case TextureFormat::UNORM_8_8_8:
341 case TextureFormat::SINT_8_8_8:
342 case TextureFormat::UINT_8_8_8:
343 case TextureFormat::SNORM_16_16:
344 case TextureFormat::SNORM_8_8:
345 case TextureFormat::SNORM_16:
346 case TextureFormat::SNORM_8:
347 case TextureFormat::SRGB_DXT1:
348 case TextureFormat::SRGB_DXT3:
349 case TextureFormat::SRGB_DXT5:
350 case TextureFormat::SNORM_DXT1:
351 case TextureFormat::SNORM_DXT3:
352 case TextureFormat::SNORM_DXT5:
353 case TextureFormat::SRGBA_8_8_8:
354 case TextureFormat::UFLOAT_9_9_9_EXP_5:
355 BLI_assert_msg(0, "Texture cannot be attached to a framebuffer because of its type");
357 }
358 }
359
360 protected:
361 virtual bool init_internal() = 0;
362 virtual bool init_internal(VertBuf *vbo) = 0;
363 virtual bool init_internal(gpu::Texture *src, bool use_stencil) = 0;
364};
365
366/* GPU pixel Buffer. */
368 protected:
369 size_t size_ = 0;
370
371 public:
372 PixelBuffer(size_t size) : size_(size) {};
373 virtual ~PixelBuffer() = default;
374
375 virtual void *map() = 0;
376 virtual void unmap() = 0;
378 virtual size_t get_size() = 0;
379};
380
381/* Syntactic sugar. */
382static inline GPUPixelBuffer *wrap(PixelBuffer *pixbuf)
383{
384 return reinterpret_cast<GPUPixelBuffer *>(pixbuf);
385}
386static inline PixelBuffer *unwrap(GPUPixelBuffer *pixbuf)
387{
388 return reinterpret_cast<PixelBuffer *>(pixbuf);
389}
390static inline const PixelBuffer *unwrap(const GPUPixelBuffer *pixbuf)
391{
392 return reinterpret_cast<const PixelBuffer *>(pixbuf);
393}
394
395inline size_t to_bytesize(TextureFormat format)
396{
397 return to_bytesize(DataFormat(format));
398}
399
400inline size_t to_block_size(TextureFormat data_type)
401{
402 switch (data_type) {
403 case TextureFormat::SRGB_DXT1:
404 case TextureFormat::SNORM_DXT1:
405 return 8;
406 case TextureFormat::SRGB_DXT3:
407 case TextureFormat::SRGB_DXT5:
408 case TextureFormat::SNORM_DXT3:
409 case TextureFormat::SNORM_DXT5:
410 return 16;
411 default:
412 BLI_assert_msg(0, "Texture format is not a compressed format");
413 return 0;
414 }
415}
416
418{
419 switch (format) {
420 /* Formats texture & render-buffer */
421 case TextureFormat::UINT_8_8_8_8:
422 return GPU_FORMAT_INTEGER;
423 case TextureFormat::SINT_8_8_8_8:
425 case TextureFormat::UNORM_8_8_8_8:
427 case TextureFormat::UINT_32_32_32_32:
428 return GPU_FORMAT_INTEGER;
429 case TextureFormat::SINT_32_32_32_32:
431 case TextureFormat::SFLOAT_32_32_32_32:
433 case TextureFormat::UINT_16_16_16_16:
434 return GPU_FORMAT_INTEGER;
435 case TextureFormat::SINT_16_16_16_16:
437 case TextureFormat::SFLOAT_16_16_16_16:
439 case TextureFormat::UNORM_16_16_16_16:
441 case TextureFormat::UINT_8_8:
442 return GPU_FORMAT_INTEGER;
443 case TextureFormat::SINT_8_8:
445 case TextureFormat::UNORM_8_8:
447 case TextureFormat::UINT_32_32:
448 return GPU_FORMAT_INTEGER;
449 case TextureFormat::SINT_32_32:
451 case TextureFormat::SFLOAT_32_32:
453 case TextureFormat::UINT_16_16:
454 return GPU_FORMAT_INTEGER;
455 case TextureFormat::SINT_16_16:
457 case TextureFormat::SFLOAT_16_16:
459 case TextureFormat::UNORM_16_16:
461 case TextureFormat::UINT_8:
462 return GPU_FORMAT_INTEGER;
463 case TextureFormat::SINT_8:
465 case TextureFormat::UNORM_8:
467 case TextureFormat::UINT_32:
468 return GPU_FORMAT_INTEGER;
469 case TextureFormat::SINT_32:
471 case TextureFormat::SFLOAT_32:
473 case TextureFormat::UINT_16:
474 return GPU_FORMAT_INTEGER;
475 case TextureFormat::SINT_16:
477 case TextureFormat::SFLOAT_16:
479 case TextureFormat::UNORM_16:
481
482 /* Special formats texture & render-buffer */
483 case TextureFormat::UNORM_10_10_10_2:
485 case TextureFormat::UINT_10_10_10_2:
486 return GPU_FORMAT_INTEGER;
487 case TextureFormat::UFLOAT_11_11_10:
488 return GPU_FORMAT_FLOAT;
489 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
491 case TextureFormat::SRGBA_8_8_8_8:
493
494 /* Texture only formats. */
495 case TextureFormat::SFLOAT_16_16_16:
497 case TextureFormat::SNORM_16_16_16:
499 case TextureFormat::SINT_16_16_16:
501 case TextureFormat::UINT_16_16_16:
502 return GPU_FORMAT_INTEGER;
503 case TextureFormat::UNORM_16_16_16:
505 case TextureFormat::SNORM_16_16_16_16:
506 case TextureFormat::SNORM_8_8_8_8:
508 case TextureFormat::SFLOAT_32_32_32:
510 case TextureFormat::SINT_32_32_32:
512 case TextureFormat::UINT_32_32_32:
513 return GPU_FORMAT_INTEGER;
514 case TextureFormat::SNORM_8_8_8:
516 case TextureFormat::UNORM_8_8_8:
518 case TextureFormat::SINT_8_8_8:
520 case TextureFormat::UINT_8_8_8:
521 return GPU_FORMAT_INTEGER;
522 case TextureFormat::SNORM_16_16:
523 case TextureFormat::SNORM_8_8:
524 case TextureFormat::SNORM_16:
525 case TextureFormat::SNORM_8:
527
528 /* Special formats, texture only. */
529 case TextureFormat::SRGB_DXT1:
530 case TextureFormat::SRGB_DXT3:
531 case TextureFormat::SRGB_DXT5:
533 case TextureFormat::SNORM_DXT1:
534 case TextureFormat::SNORM_DXT3:
535 case TextureFormat::SNORM_DXT5:
537 case TextureFormat::SRGBA_8_8_8:
539 case TextureFormat::UFLOAT_9_9_9_EXP_5:
540 return GPU_FORMAT_FLOAT;
541
542 /* Depth Formats. */
543 case TextureFormat::SFLOAT_32_DEPTH:
544 case TextureFormat::UNORM_16_DEPTH:
545 return GPU_FORMAT_DEPTH;
546
549 }
551 return GPU_FORMAT_FLOAT;
552}
553
555{
556 return format_component_len(DataFormat(format));
557}
558
559inline size_t to_bytesize(eGPUDataFormat data_format)
560{
561 switch (data_format) {
562 case GPU_DATA_UBYTE:
563 return 1;
565 return 2;
566 case GPU_DATA_FLOAT:
567 case GPU_DATA_INT:
568 case GPU_DATA_UINT:
569 return 4;
573 return 4;
574 }
576 return 0;
577}
578
579inline size_t to_bytesize(TextureFormat tex_format, eGPUDataFormat data_format)
580{
581 /* Special case for compacted types.
582 * Standard component len calculation does not apply, as the texture formats contain multiple
583 * channels, but associated data format contains several compacted components. */
584 if ((tex_format == TextureFormat::UFLOAT_11_11_10 && data_format == GPU_DATA_10_11_11_REV) ||
585 ((tex_format == TextureFormat::UNORM_10_10_10_2 ||
586 tex_format == TextureFormat::UINT_10_10_10_2) &&
587 data_format == GPU_DATA_2_10_10_10_REV))
588 {
589 return 4;
590 }
591
592 return to_component_len(tex_format) * to_bytesize(data_format);
593}
594
595/* Definitely not complete, edit according to the gl specification. */
596constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
597{
598 switch (tex_format) {
599 /* Formats texture & render-buffer */
600 case TextureFormat::UINT_32_32_32_32:
601 case TextureFormat::UINT_32_32:
602 case TextureFormat::UINT_32:
603 return ELEM(data_format, GPU_DATA_UINT);
604 case TextureFormat::UINT_16_16_16_16:
605 case TextureFormat::UINT_16_16:
606 case TextureFormat::UINT_16:
607 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_USHORT if needed. */
608 case TextureFormat::UINT_8_8_8_8:
609 case TextureFormat::UINT_8_8:
610 case TextureFormat::UINT_8:
611 return ELEM(data_format, GPU_DATA_UINT, GPU_DATA_UBYTE);
612
613 case TextureFormat::SINT_32_32_32_32:
614 case TextureFormat::SINT_32_32:
615 case TextureFormat::SINT_32:
616 return ELEM(data_format, GPU_DATA_INT);
617 case TextureFormat::SINT_16_16_16_16:
618 case TextureFormat::SINT_16_16:
619 case TextureFormat::SINT_16:
620 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_SHORT if needed. */
621 case TextureFormat::SINT_8_8_8_8:
622 case TextureFormat::SINT_8_8:
623 case TextureFormat::SINT_8:
624 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_BYTE if needed. */
625
626 case TextureFormat::SFLOAT_32_32_32_32:
627 case TextureFormat::SFLOAT_32_32:
628 case TextureFormat::SFLOAT_32:
629 return ELEM(data_format, GPU_DATA_FLOAT);
630 case TextureFormat::SFLOAT_16_16_16_16:
631 case TextureFormat::SFLOAT_16_16:
632 case TextureFormat::SFLOAT_16:
633 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_HALF_FLOAT);
634 case TextureFormat::UNORM_16_16_16_16:
635 case TextureFormat::UNORM_16_16:
636 case TextureFormat::UNORM_16:
637 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_USHORT if needed. */
638 case TextureFormat::UNORM_8_8_8_8:
639 case TextureFormat::UNORM_8_8:
640 case TextureFormat::UNORM_8:
641 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
642
643 /* Special formats texture & render-buffer */
644 case TextureFormat::UNORM_10_10_10_2:
645 case TextureFormat::UINT_10_10_10_2:
646 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_2_10_10_10_REV);
647 case TextureFormat::UFLOAT_11_11_10:
648 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_10_11_11_REV);
649 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
650 /* Should have its own type. For now, we rely on the backend to do the conversion. */
652 case TextureFormat::SRGBA_8_8_8_8:
653 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
654
655 /* Texture only formats. */
656 case TextureFormat::UINT_32_32_32:
657 return ELEM(data_format, GPU_DATA_UINT);
658 case TextureFormat::UINT_16_16_16:
659 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_SHORT if needed. */
660 case TextureFormat::UINT_8_8_8:
661 return ELEM(data_format, GPU_DATA_UINT); /* Also GPU_DATA_BYTE if needed. */
662 case TextureFormat::SINT_32_32_32:
663 return ELEM(data_format, GPU_DATA_INT);
664 case TextureFormat::SINT_16_16_16:
665 return ELEM(data_format, GPU_DATA_INT); /* Also GPU_DATA_USHORT if needed. */
666 case TextureFormat::SINT_8_8_8:
667 return ELEM(data_format, GPU_DATA_INT, GPU_DATA_UBYTE);
668 case TextureFormat::UNORM_16_16_16:
669 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_USHORT if needed. */
670 case TextureFormat::UNORM_8_8_8:
671 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
672 case TextureFormat::SNORM_16_16_16_16:
673 case TextureFormat::SNORM_16_16_16:
674 case TextureFormat::SNORM_16_16:
675 case TextureFormat::SNORM_16:
676 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_SHORT if needed. */
677 case TextureFormat::SNORM_8_8_8_8:
678 case TextureFormat::SNORM_8_8_8:
679 case TextureFormat::SNORM_8_8:
680 case TextureFormat::SNORM_8:
681 return ELEM(data_format, GPU_DATA_FLOAT); /* Also GPU_DATA_BYTE if needed. */
682 case TextureFormat::SFLOAT_32_32_32:
683 return ELEM(data_format, GPU_DATA_FLOAT);
684 case TextureFormat::SFLOAT_16_16_16:
685 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_HALF_FLOAT);
686
687 /* Special formats, texture only. */
688 case TextureFormat::SRGB_DXT1:
689 case TextureFormat::SRGB_DXT3:
690 case TextureFormat::SRGB_DXT5:
691 case TextureFormat::SNORM_DXT1:
692 case TextureFormat::SNORM_DXT3:
693 case TextureFormat::SNORM_DXT5:
694 /* TODO(fclem): GPU_DATA_COMPRESSED for each compression? Wouldn't it be overkill?
695 * For now, expect format to be set to float. */
696 return ELEM(data_format, GPU_DATA_FLOAT);
697 case TextureFormat::SRGBA_8_8_8:
698 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UBYTE);
699 case TextureFormat::UFLOAT_9_9_9_EXP_5:
700 return ELEM(data_format, GPU_DATA_FLOAT);
701
702 /* Depth Formats. */
703 case TextureFormat::SFLOAT_32_DEPTH:
704 case TextureFormat::UNORM_16_DEPTH:
705 return ELEM(data_format, GPU_DATA_FLOAT, GPU_DATA_UINT);
706
709 }
711 return data_format == GPU_DATA_FLOAT;
712}
713
714/* Return default data format for an internal texture format. */
716{
717 switch (tex_format) {
718 /* Formats texture & render-buffer */
719 case TextureFormat::UINT_32_32_32_32:
720 case TextureFormat::UINT_32_32:
721 case TextureFormat::UINT_32:
722 case TextureFormat::UINT_16_16_16_16:
723 case TextureFormat::UINT_16_16:
724 case TextureFormat::UINT_16:
725 case TextureFormat::UINT_8_8_8_8:
726 case TextureFormat::UINT_8_8:
727 case TextureFormat::UINT_8:
728 return GPU_DATA_UINT;
729
730 case TextureFormat::SINT_32_32_32_32:
731 case TextureFormat::SINT_32_32:
732 case TextureFormat::SINT_32:
733 case TextureFormat::SINT_16_16_16_16:
734 case TextureFormat::SINT_16_16:
735 case TextureFormat::SINT_16:
736 case TextureFormat::SINT_8_8_8_8:
737 case TextureFormat::SINT_8_8:
738 case TextureFormat::SINT_8:
739 return GPU_DATA_INT;
740
741 case TextureFormat::SFLOAT_32_32_32_32:
742 case TextureFormat::SFLOAT_32_32:
743 case TextureFormat::SFLOAT_32:
744 case TextureFormat::SFLOAT_16_16_16_16:
745 case TextureFormat::SFLOAT_16_16:
746 case TextureFormat::SFLOAT_16:
747 case TextureFormat::UNORM_16_16_16_16:
748 case TextureFormat::UNORM_16_16:
749 case TextureFormat::UNORM_16:
750 case TextureFormat::UNORM_8_8_8_8:
751 case TextureFormat::UNORM_8_8:
752 case TextureFormat::UNORM_8:
753 return GPU_DATA_FLOAT;
754
755 /* Special formats texture & render-buffer */
756 case TextureFormat::UNORM_10_10_10_2:
757 case TextureFormat::UINT_10_10_10_2:
759 case TextureFormat::UFLOAT_11_11_10:
761 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
762 /* Should have its own type. For now, we rely on the backend to do the conversion. */
764 case TextureFormat::SRGBA_8_8_8_8:
765 return GPU_DATA_FLOAT;
766
767 /* Texture only formats. */
768 case TextureFormat::UINT_32_32_32:
769 case TextureFormat::UINT_16_16_16:
770 case TextureFormat::UINT_8_8_8:
771 return GPU_DATA_UINT;
772 case TextureFormat::SINT_32_32_32:
773 case TextureFormat::SINT_16_16_16:
774 case TextureFormat::SINT_8_8_8:
775 return GPU_DATA_INT;
776 case TextureFormat::UNORM_16_16_16:
777 case TextureFormat::UNORM_8_8_8:
778 return GPU_DATA_FLOAT;
779 case TextureFormat::SNORM_16_16_16_16:
780 case TextureFormat::SNORM_16_16_16:
781 case TextureFormat::SNORM_16_16:
782 case TextureFormat::SNORM_16:
783 return GPU_DATA_FLOAT;
784 case TextureFormat::SNORM_8_8_8_8:
785 case TextureFormat::SNORM_8_8_8:
786 case TextureFormat::SNORM_8_8:
787 case TextureFormat::SNORM_8:
788 return GPU_DATA_FLOAT;
789 case TextureFormat::SFLOAT_32_32_32:
790 case TextureFormat::SFLOAT_16_16_16:
791 return GPU_DATA_FLOAT;
792
793 /* Special formats, texture only. */
794 case TextureFormat::SRGB_DXT1:
795 case TextureFormat::SRGB_DXT3:
796 case TextureFormat::SRGB_DXT5:
797 case TextureFormat::SNORM_DXT1:
798 case TextureFormat::SNORM_DXT3:
799 case TextureFormat::SNORM_DXT5:
800 /* TODO(fclem): GPU_DATA_COMPRESSED for each compression? Wouldn't it be overkill?
801 * For now, expect format to be set to float. */
802 return GPU_DATA_FLOAT;
803 case TextureFormat::SRGBA_8_8_8:
804 return GPU_DATA_FLOAT;
805 case TextureFormat::UFLOAT_9_9_9_EXP_5:
806 return GPU_DATA_FLOAT;
807
808 /* Depth Formats. */
809 case TextureFormat::SFLOAT_32_DEPTH:
810 case TextureFormat::UNORM_16_DEPTH:
811 return GPU_DATA_FLOAT;
814 }
816 return GPU_DATA_FLOAT;
817}
818
820{
821 switch (tex_format) {
822 /* Formats texture & render-buffer */
823 case TextureFormat::UINT_32_32_32_32:
824 case TextureFormat::UINT_32_32:
825 case TextureFormat::UINT_32:
826 case TextureFormat::UINT_16_16_16_16:
827 case TextureFormat::UINT_16_16:
828 case TextureFormat::UINT_16:
829 case TextureFormat::UINT_8_8_8_8:
830 case TextureFormat::UINT_8_8:
831 case TextureFormat::UINT_8:
832 case TextureFormat::SINT_32_32_32_32:
833 case TextureFormat::SINT_32_32:
834 case TextureFormat::SINT_32:
835 case TextureFormat::SINT_16_16_16_16:
836 case TextureFormat::SINT_16_16:
837 case TextureFormat::SINT_16:
838 case TextureFormat::SINT_8_8_8_8:
839 case TextureFormat::SINT_8_8:
840 case TextureFormat::SINT_8:
841 case TextureFormat::SFLOAT_32_32_32_32:
842 case TextureFormat::SFLOAT_32_32:
843 case TextureFormat::SFLOAT_32:
844 case TextureFormat::SFLOAT_16_16_16_16:
845 case TextureFormat::SFLOAT_16_16:
846 case TextureFormat::SFLOAT_16:
847 case TextureFormat::UNORM_16_16_16_16:
848 case TextureFormat::UNORM_16_16:
849 case TextureFormat::UNORM_16:
850 case TextureFormat::UNORM_8_8_8_8:
851 case TextureFormat::UNORM_8_8:
852 case TextureFormat::UNORM_8:
853 return GPU_COLOR_BIT;
854
855 /* Special formats texture & render-buffer */
856 case TextureFormat::UNORM_10_10_10_2:
857 case TextureFormat::UINT_10_10_10_2:
858 case TextureFormat::UFLOAT_11_11_10:
859 case TextureFormat::SRGBA_8_8_8_8:
860 return GPU_COLOR_BIT;
861 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
863
864 /* Depth Formats. */
865 case TextureFormat::SFLOAT_32_DEPTH:
866 case TextureFormat::UNORM_16_DEPTH:
867 return GPU_DEPTH_BIT;
868
869 /* Texture only formats. */
870 case TextureFormat::UINT_32_32_32:
871 case TextureFormat::UINT_16_16_16:
872 case TextureFormat::UINT_8_8_8:
873 case TextureFormat::SINT_32_32_32:
874 case TextureFormat::SINT_16_16_16:
875 case TextureFormat::SINT_8_8_8:
876 case TextureFormat::UNORM_16_16_16:
877 case TextureFormat::UNORM_8_8_8:
878 case TextureFormat::SNORM_16_16_16_16:
879 case TextureFormat::SNORM_16_16_16:
880 case TextureFormat::SNORM_16_16:
881 case TextureFormat::SNORM_16:
882 case TextureFormat::SNORM_8_8_8_8:
883 case TextureFormat::SNORM_8_8_8:
884 case TextureFormat::SNORM_8_8:
885 case TextureFormat::SNORM_8:
886 case TextureFormat::SFLOAT_32_32_32:
887 case TextureFormat::SFLOAT_16_16_16:
888 BLI_assert_msg(0, "This texture format is not compatible with framebuffer attachment.");
889 return GPU_COLOR_BIT;
890
891 /* Special formats, texture only. */
892 case TextureFormat::SRGB_DXT1:
893 case TextureFormat::SRGB_DXT3:
894 case TextureFormat::SRGB_DXT5:
895 case TextureFormat::SNORM_DXT1:
896 case TextureFormat::SNORM_DXT3:
897 case TextureFormat::SNORM_DXT5:
898 case TextureFormat::SRGBA_8_8_8:
899 case TextureFormat::UFLOAT_9_9_9_EXP_5:
900 BLI_assert_msg(0, "This texture format is not compatible with framebuffer attachment.");
901 return GPU_COLOR_BIT;
902
905 }
907 return GPU_COLOR_BIT;
908}
909
911{
912 if (format->attr_len == 0) {
913 BLI_assert_msg(0, "Incorrect vertex format for buffer texture");
914 return TextureFormat(0);
915 }
916 return TextureFormat(format->attrs[0].type.format);
917}
918
919} // namespace blender::gpu
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr IndexRange()=default
virtual void unmap()=0
virtual void * map()=0
virtual ~PixelBuffer()=default
virtual GPUPixelBufferNativeHandle get_native_handle()=0
virtual size_t get_size()=0
void attach_to(FrameBuffer *fb, GPUAttachmentType type)
virtual bool init_internal(VertBuf *vbo)=0
int mip_width_get(int mip) const
virtual void read(int mip, eGPUDataFormat format, void *dst)=0
int mip_depth_get(int mip) const
bool init_1D(int w, int layers, int mip_len, TextureFormat format)
virtual void generate_mipmap()=0
GPUTextureType type_get() const
GPUTextureFormatFlag format_flag_get() const
int3 mip_size_get(int mip) const
void update(eGPUDataFormat format, const void *data)
IndexRange layer_range() const
virtual bool init_internal(gpu::Texture *src, bool use_stencil)=0
eGPUTextureUsage gpu_image_usage_flags_
eGPUTextureUsage usage_get() const
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 swizzle_set(const char swizzle_mask[4])=0
virtual void clear(const double4 data)=0
virtual void update_sub(int offset[3], int extent[3], eGPUDataFormat format, GPUPixelBuffer *pixbuf)=0
IndexRange mip_map_range() const
bool init_3D(int w, int h, int d, int mip_len, TextureFormat format)
GPUAttachmentType attachment_type(int slot) const
int mip_height_get(int mip) const
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
virtual void copy_to(Texture *tex, IndexRange mip_levels)=0
void usage_set(eGPUTextureUsage usage_flags)
Texture(const char *name)
void detach_from(FrameBuffer *fb)
GPUTextureFormatFlag format_flag_
ENUM_OPERATORS(Layer::Flags)
int format_component_len(const DataFormat format)
static Context * unwrap(GPUContext *ctx)
bool is_half_float(TextureFormat format)
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
static GPUContext * wrap(Context *ctx)
bool is_half_integer(TextureFormat format)
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)
GPUTextureFormatFlag to_format_flag(TextureFormat format)
constexpr TextureFormat to_texture_format(TextureTargetFormat format)
GPUFrameBufferBits to_framebuffer_bits(TextureFormat tex_format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE float fb(float length, float L)
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
MINLINE int max_ii(int a, int b)
VecBase< int32_t, 3 > int3
BMesh const char void * data
VecBase< double, 4 > double4
static constexpr GPUSamplerState default_sampler()