Blender V5.3
DRW_gpu_wrapper.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
59
60#include "MEM_guardedalloc.h"
61
62#include "BKE_global.hh"
63
64#include "BLI_math_base_c.hh"
66#include "BLI_span.hh"
67#include "BLI_utildefines.hh"
68#include "BLI_utility_mixins.hh"
69#include "BLI_vector.hh"
70
71#include "GPU_framebuffer.hh"
72#include "GPU_storage_buffer.hh"
73#include "GPU_texture.hh"
74#include "GPU_texture_pool.hh"
75#include "GPU_uniform_buffer.hh"
76
77namespace blender::draw {
78
79/* -------------------------------------------------------------------- */
82
83namespace detail {
84
85template<
87 typename T,
91 bool device_only>
93 protected:
94 T *data_ = nullptr;
96
97 BLI_STATIC_ASSERT(((sizeof(T) * len) % 16) == 0,
98 "Buffer size need to be aligned to size of float4.");
99
100 public:
105 const T &operator[](int64_t index) const
106 {
107 BLI_STATIC_ASSERT(!device_only, "");
108 BLI_assert(index >= 0);
110 return data_[index];
111 }
112
114 {
115 BLI_STATIC_ASSERT(!device_only, "");
116 BLI_assert(index >= 0);
118 return data_[index];
119 }
120
124 const T *data() const
125 {
126 BLI_STATIC_ASSERT(!device_only, "");
127 return data_;
128 }
130 {
131 BLI_STATIC_ASSERT(!device_only, "");
132 return data_;
133 }
134
138 const T *begin() const
139 {
140 BLI_STATIC_ASSERT(!device_only, "");
141 return data_;
142 }
143 const T *end() const
144 {
145 BLI_STATIC_ASSERT(!device_only, "");
146 return data_ + len_;
147 }
148
150 {
151 BLI_STATIC_ASSERT(!device_only, "");
152 return data_;
153 }
155 {
156 BLI_STATIC_ASSERT(!device_only, "");
157 return data_ + len_;
158 }
159
160 operator Span<T>() const
161 {
162 BLI_STATIC_ASSERT(!device_only, "");
163 return Span<T>(data_, len_);
164 }
165};
166
167template<typename T, int64_t len, bool device_only>
168class UniformCommon : public DataBuffer<T, len, false>, NonMovable, NonCopyable {
169 protected:
171
172#ifndef NDEBUG
173 const char *name_ = typeid(T).name();
174#else
175 const char *name_ = "UniformBuffer";
176#endif
177
178 public:
179 UniformCommon(const char *name = nullptr)
180 {
181 if (name) {
182 name_ = name;
183 }
184 ubo_ = GPU_uniformbuf_create_ex(sizeof(T) * len, nullptr, name_);
185 }
186
191
193 {
194 GPU_uniformbuf_update(ubo_, this->data_);
195 }
196
197 /* To be able to use it with DRW_shgroup_*_ref(). */
198 operator gpu::UniformBuf *() const
199 {
200 return ubo_;
201 }
202
203 /* To be able to use it with DRW_shgroup_*_ref(). */
205 {
206 return &ubo_;
207 }
208};
209
210template<typename T, int64_t len, bool device_only>
211class StorageCommon : public DataBuffer<T, len, false>, NonMovable, NonCopyable {
212 protected:
214
215#ifndef NDEBUG
216 const char *name_ = typeid(T).name();
217#else
218 const char *name_ = "StorageBuffer";
219#endif
220
221 public:
222 StorageCommon(const char *name = nullptr)
223 {
224 if (name) {
225 name_ = name;
226 }
227 this->len_ = len;
228 constexpr GPUUsageType usage = device_only ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_DYNAMIC;
229 ssbo_ = GPU_storagebuf_create_ex(sizeof(T) * this->len_, nullptr, usage, this->name_);
230 }
231
236
238 {
239 BLI_assert(device_only == false);
240 GPU_storagebuf_update(ssbo_, this->data_);
241 }
242
247
252
253 void read()
254 {
255 GPU_storagebuf_read(ssbo_, this->data_);
256 }
257
258 operator gpu::StorageBuf *() const
259 {
260 return ssbo_;
261 }
262 /* To be able to use it with DRW_shgroup_*_ref(). */
264 {
265 return &ssbo_;
266 }
267};
268
269} // namespace detail
270
272
273/* -------------------------------------------------------------------- */
276
277template<
279 typename T,
283 /* TODO(@fclem): Currently unsupported. */
284 /* bool device_only = false */>
285class UniformArrayBuffer : public detail::UniformCommon<T, len, false> {
286 public:
287 UniformArrayBuffer(const char *name = nullptr) : detail::UniformCommon<T, len, false>(name)
288 {
289 /* TODO(@fclem): We should map memory instead. */
290 this->data_ = static_cast<T *>(
291 MEM_new_uninitialized_aligned(len * sizeof(T), 16, this->name_));
292 }
294 {
295 MEM_delete(this->data_);
296 }
297};
298
299template<
301 typename T
303 /* TODO(@fclem): Currently unsupported. */
304 /* bool device_only = false */>
305class UniformBuffer : public T, public detail::UniformCommon<T, 1, false> {
306 public:
307 UniformBuffer(const char *name = nullptr) : T{}, detail::UniformCommon<T, 1, false>(name)
308 {
309 /* TODO(@fclem): How could we map this? */
310 this->data_ = static_cast<T *>(this);
311 }
312
314 {
315 *static_cast<T *>(this) = other;
316 return *this;
317 }
318};
319
321
322/* -------------------------------------------------------------------- */
325
326template<
328 typename T,
330 int64_t len = (512u + (sizeof(T) - 1)) / sizeof(T),
332 bool device_only = false>
333class StorageArrayBuffer : public detail::StorageCommon<T, len, device_only> {
334 public:
335 StorageArrayBuffer(const char *name = nullptr) : detail::StorageCommon<T, len, device_only>(name)
336 {
337 /* TODO(@fclem): We should map memory instead. */
338 this->data_ = static_cast<T *>(
339 MEM_new_uninitialized_aligned(len * sizeof(T), 16, this->name_));
340 }
342 {
343 MEM_delete(this->data_);
344 }
345
346 /* Resize to \a new_size elements. */
347 void resize(int64_t new_size)
348 {
349 BLI_assert(new_size > 0);
350 if (new_size != this->len_) {
351 /* Manual realloc since MEM_realloc_uninitialized_aligned does not exists. */
352 T *new_data_ = static_cast<T *>(
353 MEM_new_uninitialized_aligned(new_size * sizeof(T), 16, this->name_));
354 memcpy(reinterpret_cast<void *>(new_data_),
355 this->data_,
356 min_uu(this->len_, new_size) * sizeof(T));
357 MEM_delete(this->data_);
358 this->data_ = new_data_;
360
361 this->len_ = new_size;
362 constexpr GPUUsageType usage = device_only ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_DYNAMIC;
363 this->ssbo_ = GPU_storagebuf_create_ex(sizeof(T) * this->len_, nullptr, usage, this->name_);
364 }
365 }
366
367 /* Resize on access. */
369 {
370 BLI_assert(index >= 0);
371 if (index >= this->len_) {
372 size_t size = power_of_2_max_u(index + 1);
373 this->resize(size);
374 }
375 return this->data_[index];
376 }
377
378 /*
379 * Ensure the allocated size is not much larger than the currently required size,
380 * using the same heuristic as `get_or_resize`.
381 */
383 {
384 /* Don't go below the size used at creation. */
385 required_size = std::max(required_size, len);
386 size_t target_size = power_of_2_max_u(required_size);
387 if (this->len_ > target_size) {
388 this->resize(target_size);
389 }
390 }
391
392 int64_t size() const
393 {
394 return this->len_;
395 }
396
398 {
399 return {this->data_, this->len_};
400 }
401
403 {
404 std::swap(a.data_, b.data_);
405 std::swap(a.ssbo_, b.ssbo_);
406 std::swap(a.len_, b.len_);
407 std::swap(a.name_, b.name_);
408 }
409};
410
411template<
413 typename T,
415 int64_t len = (512u + (sizeof(T) - 1)) / sizeof(T)>
416class StorageVectorBuffer : public StorageArrayBuffer<T, len, false> {
417 private:
418 /* Number of items, not the allocated length. */
419 int64_t item_len_ = 0;
420
421 public:
422 StorageVectorBuffer(const char *name = nullptr) : StorageArrayBuffer<T, len, false>(name) {};
424
428 void clear()
429 {
430 item_len_ = 0;
431 }
432
438 {
439 this->trim_to_next_power_of_2(item_len_);
440 clear();
441 }
442
449 void append(const T &value)
450 {
451 this->append_as(value);
452 }
453 void append(T &&value)
454 {
455 this->append_as(std::move(value));
456 }
457 template<typename... ForwardT> void append_as(ForwardT &&...value)
458 {
459 if (item_len_ >= this->len_) {
460 size_t size = power_of_2_max_u(item_len_ + 1);
461 this->resize(size);
462 }
463 T *ptr = &this->data_[item_len_++];
464 new (ptr) T(std::forward<ForwardT>(value)...);
465 }
466
467 void extend(const Span<T> values)
468 {
469 /* TODO(fclem): Optimize to a single memcpy. */
470 for (auto v : values) {
471 this->append(v);
472 }
473 }
474
475 int64_t size() const
476 {
477 return item_len_;
478 }
479
480 bool is_empty() const
481 {
482 return this->size() == 0;
483 }
484
485 /* Avoid confusion with the other clear. */
486 void clear_to_zero() = delete;
487
489 {
491 std::swap(a.item_len_, b.item_len_);
492 }
493};
494
495template<
497 typename T,
499 bool device_only = false>
500class StorageBuffer : public T, public detail::StorageCommon<T, 1, device_only> {
501 public:
502 StorageBuffer(const char *name = nullptr) : T{}, detail::StorageCommon<T, 1, device_only>(name)
503 {
504 /* TODO(@fclem): How could we map this? */
505 this->data_ = static_cast<T *>(this);
506 }
507
509 {
510 *static_cast<T *>(this) = other;
511 return *this;
512 }
513
515 {
516 /* Swap content, but not `data_` pointers since they point to `this`. */
517 SWAP(T, static_cast<T>(a), static_cast<T>(b));
518 std::swap(a.ssbo_, b.ssbo_);
519 }
520};
521
523
524/* -------------------------------------------------------------------- */
527
529 protected:
530 gpu::Texture *tx_ = nullptr;
535 const char *name_;
536
537 public:
538 Texture(const char *name = "gpu::Texture") : name_(name) {}
539
540 Texture(const char *name,
541 gpu::TextureFormat format,
542 eGPUTextureUsage usage,
543 int extent,
544 const float *data = nullptr,
545 bool cubemap = false,
546 int mip_len = 1)
547 : name_(name)
548 {
549 tx_ = create(extent, 0, 0, mip_len, format, usage, data, false, cubemap);
550 }
551
552 Texture(const char *name,
553 gpu::TextureFormat format,
554 eGPUTextureUsage usage,
555 int extent,
556 int layers,
557 const float *data = nullptr,
558 bool cubemap = false,
559 int mip_len = 1)
560 : name_(name)
561 {
562 tx_ = create(extent, layers, 0, mip_len, format, usage, data, true, cubemap);
563 }
564
565 Texture(const char *name,
566 gpu::TextureFormat format,
567 eGPUTextureUsage usage,
568 int2 extent,
569 const float *data = nullptr,
570 int mip_len = 1)
571 : name_(name)
572 {
573 tx_ = create(UNPACK2(extent), 0, mip_len, format, usage, data, false, false);
574 }
575
576 Texture(const char *name,
577 gpu::TextureFormat format,
578 eGPUTextureUsage usage,
579 int2 extent,
580 int layers,
581 const float *data = nullptr,
582 int mip_len = 1)
583 : name_(name)
584 {
585 tx_ = create(UNPACK2(extent), layers, mip_len, format, usage, data, true, false);
586 }
587
588 Texture(const char *name,
589 gpu::TextureFormat format,
590 eGPUTextureUsage usage,
591 int3 extent,
592 const float *data = nullptr,
593 int mip_len = 1)
594 : name_(name)
595 {
596 tx_ = create(UNPACK3(extent), mip_len, format, usage, data, false, false);
597 }
598
599 Texture(Texture &&other) = default;
601 {
602 free();
603 }
604
606 {
607 return tx_;
608 }
609
610 /* To be able to use it with DRW_shgroup_uniform_texture(). */
611 operator gpu::Texture *() const
612 {
613 BLI_assert(tx_ != nullptr);
614 return tx_;
615 }
616
617 /* To be able to use it with DRW_shgroup_uniform_texture_ref(). */
619 {
620 return &tx_;
621 }
622
625 {
626 return this;
627 }
628
630 {
631 if (this != std::addressof(a)) {
632 this->free();
633
634 this->tx_ = a.tx_;
635 this->name_ = a.name_;
636 this->stencil_view_ = a.stencil_view_;
637 this->layer_range_view_ = a.layer_range_view_;
638 this->mip_views_ = std::move(a.mip_views_);
639 this->layer_views_ = std::move(a.layer_views_);
640
641 a.tx_ = nullptr;
642 a.name_ = nullptr;
643 a.stencil_view_ = nullptr;
644 a.layer_range_view_ = nullptr;
645 a.mip_views_.clear();
646 a.layer_views_.clear();
647 }
648 return *this;
649 }
650
656 int extent,
658 const float *data = nullptr,
659 int mip_len = 1)
660 {
661 return ensure_impl(extent, 0, 0, mip_len, format, usage, data, false, false);
662 }
663
669 int extent,
670 int layers,
672 const float *data = nullptr,
673 int mip_len = 1)
674 {
675 BLI_assert(layers > 0);
676 return ensure_impl(extent, layers, 0, mip_len, format, usage, data, true, false);
677 }
678
684 int2 extent,
686 const float *data = nullptr,
687 int mip_len = 1)
688 {
689 return ensure_impl(UNPACK2(extent), 0, mip_len, format, usage, data, false, false);
690 }
691
697 int2 extent,
698 int layers,
700 const float *data = nullptr,
701 int mip_len = 1)
702 {
703 BLI_assert(layers > 0);
704 return ensure_impl(UNPACK2(extent), layers, mip_len, format, usage, data, true, false);
705 }
706
712 int3 extent,
714 const float *data = nullptr,
715 int mip_len = 1)
716 {
717 return ensure_impl(UNPACK3(extent), mip_len, format, usage, data, false, false);
718 }
719
725 int extent,
727 float *data = nullptr,
728 int mip_len = 1)
729 {
730 return ensure_impl(extent, extent, 0, mip_len, format, usage, data, false, true);
731 }
732
738 int extent,
739 int layers,
741 const float *data = nullptr,
742 int mip_len = 1)
743 {
744 return ensure_impl(extent, extent, layers, mip_len, format, usage, data, true, true);
745 }
746
751 bool ensure_mip_views(bool cube_as_array = false)
752 {
753 int mip_len = GPU_texture_mip_count(tx_);
754 if (mip_views_.size() != mip_len) {
755 for (gpu::Texture *&view : mip_views_) {
757 }
759 for (auto i : IndexRange(mip_len)) {
760 mip_views_.append(
761 GPU_texture_create_view(name_, tx_, format, i, 1, 0, 9999, cube_as_array, false));
762 }
763 return true;
764 }
765 return false;
766 }
767
769 {
770 BLI_assert_msg(miplvl < mip_views_.size(),
771 "Incorrect mip level requested. "
772 "Might be missing call to ensure_mip_views().");
773 return mip_views_[miplvl];
774 }
775
776 int mip_count() const
777 {
779 }
780
786 bool ensure_layer_views(bool cube_as_array = false)
787 {
788 int layer_len = GPU_texture_layer_count(tx_);
789 if (layer_views_.size() != layer_len) {
790 for (gpu::Texture *&view : layer_views_) {
792 }
794 for (auto i : IndexRange(layer_len)) {
795 layer_views_.append(
796 GPU_texture_create_view(name_, tx_, format, 0, 9999, i, 1, cube_as_array, false));
797 }
798 return true;
799 }
800 return false;
801 }
802
804 {
805 return layer_views_[layer];
806 }
807
808 gpu::Texture *stencil_view(bool cube_as_array = false)
809 {
810 if (stencil_view_ == nullptr) {
813 name_, tx_, format, 0, 9999, 0, 9999, cube_as_array, true);
814 }
815 return stencil_view_;
816 }
817
827 gpu::Texture *layer_range_view(int layer_start, int layer_len, bool cube_as_array = false)
828 {
829 BLI_assert(this->is_valid());
830 /* Make sure the range is valid as the GPU_texture_layer_count only returns the effective
831 * (clipped) range and not the requested range. */
832 BLI_assert_msg((layer_start + layer_len) <= GPU_texture_layer_count(tx_),
833 "Layer range needs to be valid");
834
835 int view_layer_len = (layer_range_view_) ? GPU_texture_layer_count(layer_range_view_) : -1;
836 if (layer_len != view_layer_len) {
840 name_, tx_, format, 0, 9999, layer_start, layer_len, cube_as_array, false);
841 }
842 return layer_range_view_;
843 }
844
848 bool is_valid() const
849 {
850 return tx_ != nullptr;
851 }
852
853 int width() const
854 {
855 return GPU_texture_width(tx_);
856 }
857
858 int height() const
859 {
860 return GPU_texture_height(tx_);
861 }
862
863 int depth() const
864 {
865 return GPU_texture_depth(tx_);
866 }
867
868 int pixel_count() const
869 {
871 }
872
873 bool is_depth() const
874 {
876 }
877
878 bool is_stencil() const
879 {
881 }
882
883 bool is_integer() const
884 {
886 }
887
888 bool is_cube() const
889 {
890 return GPU_texture_is_cube(tx_);
891 }
892
893 bool is_array() const
894 {
896 }
897
898 int3 size(int miplvl = 0) const
899 {
900 int3 size(1);
902 return size;
903 }
904
908 void clear(float4 values)
909 {
911 }
912
916 void clear(uint4 values)
917 {
918 GPU_texture_clear(tx_, GPU_DATA_UINT, &values[0]);
919 }
920
924 void clear(int4 values)
925 {
926 GPU_texture_clear(tx_, GPU_DATA_INT, &values[0]);
927 }
928
934 {
935 if (GPU_texture_dimensions(this->tx_) == 1) {
936 /* Clearing of 1D texture is currently unsupported. */
937 return;
938 }
939
941 this->clear(float4(NAN_FLT));
942 }
943 else if (GPU_texture_has_integer_format(this->tx_)) {
944 if (GPU_texture_has_signed_format(this->tx_)) {
945 this->clear(int4(0xF0F0F0F0));
946 }
947 else {
948 this->clear(uint4(0xF0F0F0F0));
949 }
950 }
951 }
952
957 template<typename T> T *read(eGPUDataFormat format, int miplvl = 0)
958 {
959 return reinterpret_cast<T *>(GPU_texture_read(tx_, format, miplvl));
960 }
961
962 void filter_mode(bool do_filter)
963 {
964 GPU_texture_filter_mode(tx_, do_filter);
965 }
966
970 void free()
971 {
974 }
975
979 static void swap(Texture &a, Texture &b)
980 {
981 std::swap(a.tx_, b.tx_);
982 std::swap(a.name_, b.name_);
983 std::swap(a.stencil_view_, b.stencil_view_);
984 std::swap(a.layer_range_view_, b.layer_range_view_);
985 std::swap(a.mip_views_, b.mip_views_);
986 std::swap(a.layer_views_, b.layer_views_);
987 }
988
989 protected:
991 {
992 for (gpu::Texture *&view : mip_views_) {
994 }
995 for (gpu::Texture *&view : layer_views_) {
997 }
1000 mip_views_.clear();
1001 layer_views_.clear();
1002 }
1003
1004 private:
1005 bool ensure_impl(int w,
1006 int h = 0,
1007 int d = 0,
1008 int mip_len = 1,
1009 gpu::TextureFormat format = gpu::TextureFormat::UNORM_8_8_8_8,
1011 const float *data = nullptr,
1012 bool layered = false,
1013 bool cubemap = false)
1014
1015 {
1016 /* TODO(@fclem): In the future, we need to check if mip_count did not change.
1017 * For now it's ok as we always define all MIP level. */
1018 if (tx_) {
1019 int3 size(0);
1021 if (size != int3(w, h, d) || GPU_texture_format(tx_) != format ||
1022 GPU_texture_is_cube(tx_) != cubemap || GPU_texture_is_array(tx_) != layered)
1023 {
1024 free();
1025 }
1026 }
1027 if (tx_ == nullptr) {
1028 tx_ = create(w, h, d, mip_len, format, usage, data, layered, cubemap);
1029 if (is_valid() && data == nullptr && (G.debug & G_DEBUG_GPU)) {
1030 debug_clear();
1031 }
1032 return true;
1033 }
1034 return false;
1035 }
1036
1037 gpu::Texture *create(int w,
1038 int h,
1039 int d,
1040 int mip_len,
1041 gpu::TextureFormat format,
1042 eGPUTextureUsage usage,
1043 const float *data,
1044 bool layered,
1045 bool cubemap)
1046 {
1047 if (h == 0) {
1048 return GPU_texture_create_1d(name_, w, mip_len, format, usage, data);
1049 }
1050 if (cubemap) {
1051 if (layered) {
1052 return GPU_texture_create_cube_array(name_, w, d, mip_len, format, usage, data);
1053 }
1054 return GPU_texture_create_cube(name_, w, mip_len, format, usage, data);
1055 }
1056 if (d == 0) {
1057 if (layered) {
1058 return GPU_texture_create_1d_array(name_, w, h, mip_len, format, usage, data);
1059 }
1060 return GPU_texture_create_2d(name_, w, h, mip_len, format, usage, data);
1061 }
1062 if (layered) {
1063 return GPU_texture_create_2d_array(name_, w, h, d, mip_len, format, usage, data);
1064 }
1065 return GPU_texture_create_3d(name_, w, h, d, mip_len, format, usage, data);
1066 }
1067};
1068
1070 /* Object may be released on a different `GPUContext`; track the owning pool. */
1071 gpu::TexturePool *pool_ = nullptr;
1072
1073 public:
1074 TextureFromPool(const char *name = "draw::TextureFromPool") : Texture(name) {};
1075
1076 /* On destructor, textures after `::retain()` may need to be released. */
1078 {
1079 release();
1080 }
1081
1086 bool acquire_1d(int extent,
1087 gpu::TextureFormat format,
1089 int mip_len = 1)
1090 {
1091 return acquire_impl(extent, 0, 0, mip_len, format, usage, false, false);
1092 }
1093
1098 bool acquire_1d_array(int extent,
1099 int layers,
1100 gpu::TextureFormat format,
1102 int mip_len = 1)
1103 {
1104 return acquire_impl(extent, layers, 0, mip_len, format, usage, true, false);
1105 }
1106
1111 bool acquire_2d(int2 extent,
1112 gpu::TextureFormat format,
1114 int mip_len = 1)
1115 {
1116 return acquire_impl(extent.x, extent.y, 0, mip_len, format, usage, false, false);
1117 }
1118
1124 int layers,
1125 gpu::TextureFormat format,
1127 int mip_len = 1)
1128 {
1129 return acquire_impl(extent.x, extent.y, layers, mip_len, format, usage, true, false);
1130 }
1131
1136 bool acquire_3d(int3 extent,
1137 gpu::TextureFormat format,
1139 int mip_len = 1)
1140 {
1141 return acquire_impl(extent.x, extent.y, extent.z, mip_len, format, usage, false, false);
1142 }
1143
1148 bool acquire_cube(int extent,
1149 gpu::TextureFormat format,
1151 int mip_len = 1)
1152 {
1153 return acquire_impl(extent, 0, 0, mip_len, format, usage, false, true);
1154 }
1155
1160 bool acquire_cube_array(int extent,
1161 int layers,
1162 gpu::TextureFormat format,
1164 int mip_len = 1)
1165 {
1166 return acquire_impl(extent, layers, 0, mip_len, format, usage, true, true);
1167 }
1168
1169 /* Invalidate the acquired texture for this frame. Multiple releases can be done safely. */
1170 void release()
1171 {
1172 if (tx_ == nullptr) {
1173 return;
1174 }
1176 pool_->release_texture(tx_);
1177 tx_ = nullptr;
1178 pool_ = nullptr;
1179 }
1180
1181 /* Allow for the texture to survive into the next cycle. */
1182 void retain()
1183 {
1184 pool_->offset_users_count(tx_, -1);
1185 }
1186
1187 /* Swap the contents of the two textures as well as their owning pool. */
1189 {
1190 Texture::swap(a, b);
1191 std::swap(a.pool_, b.pool_);
1192 }
1193
1196 {
1197 return this;
1198 }
1199
1201 bool ensure_1d(int, int, blender::gpu::TextureFormat, eGPUTextureUsage, const float *) = delete;
1202 bool ensure_1d_array(int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *) =
1203 delete;
1204 bool ensure_2d(int, int, int, gpu::TextureFormat, eGPUTextureUsage, float *) = delete;
1205 bool ensure_2d_array(int, int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *) =
1206 delete;
1207 bool ensure_3d(int, int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *) = delete;
1208 bool ensure_cube(int, int, gpu::TextureFormat, eGPUTextureUsage, const float *) = delete;
1209 void filter_mode(bool) = delete;
1210 void free() = delete;
1211
1212 private:
1213 /* Underlying `acquire_*` forwards to the texture pool. */
1214 bool acquire_impl(int w,
1215 int h,
1216 int d,
1217 int mip_len,
1218 gpu::TextureFormat format,
1219 eGPUTextureUsage usage,
1220 bool layered,
1221 bool cubemap)
1222 {
1223 if (tx_ != nullptr) {
1224 if (GPU_texture_width(tx_) != w || GPU_texture_height(tx_) != h ||
1225 GPU_texture_depth(tx_) != d || GPU_texture_format(tx_) != format ||
1226 (GPU_texture_usage(tx_) & usage) != usage)
1227 {
1228 release();
1229 }
1230 else {
1231 pool_->offset_users_count(tx_, 1);
1232 return false;
1233 }
1234 }
1235
1236 pool_ = &gpu::TexturePool::get();
1237
1238 if (h == 0) {
1239 tx_ = pool_->acquire_texture_1d(w, mip_len, format, usage, name_);
1240 }
1241 else if (cubemap) {
1242 if (layered) {
1243 tx_ = pool_->acquire_texture_cube_array(w, h, mip_len, format, usage, name_);
1244 }
1245 else {
1246 tx_ = pool_->acquire_texture_cube(w, mip_len, format, usage, name_);
1247 }
1248 }
1249 else if (d == 0) {
1250 if (layered) {
1251 tx_ = pool_->acquire_texture_1d_array(w, h, mip_len, format, usage, name_);
1252 }
1253 else {
1254 tx_ = pool_->acquire_texture_2d({w, h}, mip_len, format, usage, name_);
1255 }
1256 }
1257 else if (layered) {
1258 tx_ = pool_->acquire_texture_2d_array({w, h}, d, mip_len, format, usage, name_);
1259 }
1260 else {
1261 tx_ = pool_->acquire_texture_3d({w, h, d}, mip_len, format, usage, name_);
1262 }
1263
1264 if (G.debug & G_DEBUG_GPU) {
1265 debug_clear();
1266 }
1267
1268 return true;
1269 }
1270};
1271
1272class TextureRef : public Texture {
1273 public:
1274 TextureRef() = default;
1275
1277 {
1278 this->tx_ = nullptr;
1279 }
1280
1282 {
1283 if (assign_if_different(this->tx_, tex)) {
1285 }
1286 }
1287
1289 bool ensure_1d(int, int, gpu::TextureFormat, const float *) = delete;
1290 bool ensure_1d_array(int, int, int, gpu::TextureFormat, const float *) = delete;
1291 bool ensure_2d(int, int, int, gpu::TextureFormat, const float *) = delete;
1292 bool ensure_2d_array(int, int, int, int, gpu::TextureFormat, const float *) = delete;
1293 bool ensure_3d(int, int, int, int, gpu::TextureFormat, const float *) = delete;
1294 bool ensure_cube(int, int, gpu::TextureFormat, const float *) = delete;
1295 bool ensure_cube_array(int, int, int, gpu::TextureFormat, const float *) = delete;
1296 void filter_mode(bool) = delete;
1297 void free() = delete;
1298 gpu::Texture *mip_view(int) = delete;
1299 gpu::Texture *layer_view(int) = delete;
1301};
1302
1307class Image {};
1308
1309static inline Image *as_image(gpu::Texture *tex)
1310{
1311 return reinterpret_cast<Image *>(tex);
1312}
1313
1314static inline Image **as_image(gpu::Texture **tex)
1315{
1316 return reinterpret_cast<Image **>(tex);
1317}
1318
1319static inline gpu::Texture *as_texture(Image *img)
1320{
1321 return reinterpret_cast<gpu::Texture *>(img);
1322}
1323
1324static inline gpu::Texture **as_texture(Image **img)
1325{
1326 return reinterpret_cast<gpu::Texture **>(img);
1327}
1328
1330
1331/* -------------------------------------------------------------------- */
1334
1336 private:
1337 gpu::FrameBuffer *fb_ = nullptr;
1338 const char *name_;
1339
1340 public:
1341 Framebuffer() : name_("") {};
1342 Framebuffer(const char *name) : name_(name) {};
1343
1345 {
1347 }
1348
1358 {
1359 if (fb_ == nullptr) {
1360 fb_ = GPU_framebuffer_create(name_);
1361 }
1362 GPUAttachment config[] = {
1363 depth, color1, color2, color3, color4, color5, color6, color7, color8};
1364 GPU_framebuffer_config_array(fb_, config, sizeof(config) / sizeof(GPUAttachment));
1365 }
1366
1370 void ensure(int2 target_size)
1371 {
1372 if (fb_ == nullptr) {
1373 fb_ = GPU_framebuffer_create(name_);
1374 }
1375 GPU_framebuffer_default_size(fb_, UNPACK2(target_size));
1376 }
1377
1378 void bind()
1379 {
1381 }
1382
1383 void clear_depth(float depth)
1384 {
1385 GPU_framebuffer_clear_depth(fb_, depth);
1386 }
1387
1392
1394 {
1395 if (*this != a) {
1396 this->fb_ = a.fb_;
1397 this->name_ = a.name_;
1398 a.fb_ = nullptr;
1399 }
1400 return *this;
1401 }
1402
1403 operator gpu::FrameBuffer *() const
1404 {
1405 return fb_;
1406 }
1407
1409 {
1410 return &fb_;
1411 }
1412
1416 static void swap(Framebuffer &a, Framebuffer &b)
1417 {
1418 std::swap(a.fb_, b.fb_);
1419 std::swap(a.name_, b.name_);
1420 }
1421};
1422
1424
1425/* -------------------------------------------------------------------- */
1430
1431template<typename T, int64_t len> class SwapChain {
1432 private:
1433 BLI_STATIC_ASSERT(len > 1, "A swap-chain needs more than 1 unit in length.");
1434 std::array<T, len> chain_;
1435
1436 public:
1437 void swap()
1438 {
1439 for (auto i : IndexRange(len - 1)) {
1440 auto i_next = (i + 1) % len;
1441 if constexpr (std::is_trivial_v<T>) {
1442 std::swap(chain_[i], chain_[i_next]);
1443 }
1444 else {
1445 T::swap(chain_[i], chain_[i_next]);
1446 }
1447 }
1448 }
1449
1450 constexpr int64_t size()
1451 {
1452 return len;
1453 }
1454
1456 {
1457 return chain_[0];
1458 }
1459
1461 {
1462 /* Avoid modulo operation with negative numbers. */
1463 return chain_[(0 + len - 1) % len];
1464 }
1465
1467 {
1468 return chain_[(0 + 1) % len];
1469 }
1470
1471 const T &current() const
1472 {
1473 return chain_[0];
1474 }
1475
1476 const T &previous() const
1477 {
1478 /* Avoid modulo operation with negative numbers. */
1479 return chain_[(0 + len - 1) % len];
1480 }
1481
1482 const T &next() const
1483 {
1484 return chain_[(0 + 1) % len];
1485 }
1486};
1487
1489
1490} // namespace blender::draw
#define NAN_FLT
#define GPU_FRAMEBUFFER_FREE_SAFE(fb)
#define GPU_ATTACHMENT_NONE
#define GPU_TEXTURE_FREE_SAFE(texture)
Read Blender memory allocation and freeing.
#define UNPACK2(a)
#define SWAP(type, a, b)
#define UNPACK3(a)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr IndexRange()=default
constexpr Span()=default
int64_t size() const
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
static void swap(Framebuffer &a, Framebuffer &b)
void ensure(int2 target_size)
gpu::FrameBuffer ** operator&()
Framebuffer & operator=(Framebuffer &&a)
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
StorageArrayBuffer(const char *name=nullptr)
static void swap(StorageArrayBuffer &a, StorageArrayBuffer &b)
MutableSpan< T > as_span() const
void trim_to_next_power_of_2(int64_t required_size)
static void swap(StorageBuffer< T > &a, StorageBuffer< T > &b)
StorageBuffer< T > & operator=(const T &other)
StorageBuffer(const char *name=nullptr)
StorageVectorBuffer(const char *name=nullptr)
static void swap(StorageVectorBuffer &a, StorageVectorBuffer &b)
void extend(const Span< T > values)
bool ensure_2d_array(int, int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *)=delete
bool acquire_3d(int3 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool acquire_cube(int extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_2d(int, int, int, gpu::TextureFormat, eGPUTextureUsage, float *)=delete
static void swap(TextureFromPool &a, TextureFromPool &b)
bool ensure_cube(int, int, gpu::TextureFormat, eGPUTextureUsage, const float *)=delete
void filter_mode(bool)=delete
TextureFromPool(const char *name="draw::TextureFromPool")
bool ensure_1d_array(int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *)=delete
bool acquire_1d_array(int extent, int layers, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool acquire_1d(int extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool acquire_2d_array(int2 extent, int layers, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_1d(int, int, blender::gpu::TextureFormat, eGPUTextureUsage, const float *)=delete
bool acquire_cube_array(int extent, int layers, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_3d(int, int, int, int, gpu::TextureFormat, eGPUTextureUsage, const float *)=delete
gpu::Texture * layer_view(int)=delete
bool ensure_cube(int, int, gpu::TextureFormat, const float *)=delete
gpu::Texture * mip_view(int)=delete
bool ensure_cube_array(int, int, int, gpu::TextureFormat, const float *)=delete
void filter_mode(bool)=delete
gpu::Texture * stencil_view()=delete
bool ensure_1d(int, int, gpu::TextureFormat, const float *)=delete
bool ensure_3d(int, int, int, int, gpu::TextureFormat, const float *)=delete
bool ensure_1d_array(int, int, int, gpu::TextureFormat, const float *)=delete
bool ensure_2d(int, int, int, gpu::TextureFormat, const float *)=delete
void wrap(gpu::Texture *tex)
bool ensure_2d_array(int, int, int, int, gpu::TextureFormat, const float *)=delete
bool ensure_mip_views(bool cube_as_array=false)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
static void swap(Texture &a, Texture &b)
bool ensure_3d(gpu::TextureFormat format, int3 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * gpu_texture()
Texture(const char *name="gpu::Texture")
Texture(const char *name, gpu::TextureFormat format, eGPUTextureUsage usage, int extent, const float *data=nullptr, bool cubemap=false, int mip_len=1)
void clear(float4 values)
T * read(eGPUDataFormat format, int miplvl=0)
gpu::Texture ** operator&()
Texture(const char *name, gpu::TextureFormat format, eGPUTextureUsage usage, int3 extent, const float *data=nullptr, int mip_len=1)
void clear(uint4 values)
Vector< gpu::Texture *, 0 > layer_views_
void clear(int4 values)
bool ensure_cube_array(gpu::TextureFormat format, int extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
void filter_mode(bool do_filter)
bool ensure_1d_array(gpu::TextureFormat format, int extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
bool ensure_2d_array(gpu::TextureFormat format, int2 extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
bool ensure_layer_views(bool cube_as_array=false)
gpu::Texture * layer_view(int layer)
Vector< gpu::Texture *, 0 > mip_views_
gpu::Texture * layer_range_view_
Texture(const char *name, gpu::TextureFormat format, eGPUTextureUsage usage, int2 extent, const float *data=nullptr, int mip_len=1)
gpu::Texture * layer_range_view(int layer_start, int layer_len, bool cube_as_array=false)
Texture & operator=(Texture &&a)
bool ensure_1d(gpu::TextureFormat format, int extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
int3 size(int miplvl=0) const
gpu::Texture * mip_view(int miplvl)
bool ensure_cube(gpu::TextureFormat format, int extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, float *data=nullptr, int mip_len=1)
Texture(Texture &&other)=default
Texture(const char *name, gpu::TextureFormat format, eGPUTextureUsage usage, int2 extent, int layers, const float *data=nullptr, int mip_len=1)
gpu::Texture * stencil_view(bool cube_as_array=false)
Texture(const char *name, gpu::TextureFormat format, eGPUTextureUsage usage, int extent, int layers, const float *data=nullptr, bool cubemap=false, int mip_len=1)
UniformArrayBuffer(const char *name=nullptr)
UniformBuffer< T > & operator=(const T &other)
UniformBuffer(const char *name=nullptr)
BLI_STATIC_ASSERT(((sizeof(T) *len) % 16)==0, "Buffer size need to be aligned to size of float4.")
const T & operator[](int64_t index) const
StorageCommon(const char *name=nullptr)
UniformCommon(const char *name=nullptr)
TextureFromPool(const char *name="draw::TextureFromPool")
Texture(const char *name="gpu::Texture")
static TexturePool & get()
#define layer
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
#define G(x, y, z)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
static gpu::Texture * as_texture(Image *img)
static Image * as_image(gpu::Texture *tex)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_storagebuf_update(gpu::StorageBuf *ssbo, const void *data)
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)
__int64 int64_t
Definition BKE_text.h:90
int GPU_texture_depth(const gpu::Texture *texture)
bool GPU_texture_has_integer_format(const gpu::Texture *texture)
void GPU_storagebuf_clear_to_zero(gpu::StorageBuf *ssbo)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
VecBase< int32_t, 4 > int4
eGPUTextureUsage GPU_texture_usage(const gpu::Texture *texture)
void GPU_uniformbuf_free(gpu::UniformBuf *ubo)
bool assign_if_different(T &old_value, T &&new_value)
gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
VecBase< uint32_t, 4 > uint4
bool GPU_texture_has_signed_format(const gpu::Texture *texture)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
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)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE unsigned int power_of_2_max_u(unsigned int x)
bool GPU_texture_has_normalized_format(const gpu::Texture *texture)
int GPU_texture_dimensions(const gpu::Texture *texture)
Global G
Definition blender.cc:57
VecBase< float, 4 > float4
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_GENERAL
int GPU_texture_mip_count(const gpu::Texture *texture)
BMesh const char void * data
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)
int GPU_texture_width(const gpu::Texture *texture)
bool GPU_texture_is_cube(const gpu::Texture *texture)
void GPU_framebuffer_config_array(gpu::FrameBuffer *fb, const GPUAttachment *config, int config_len)
void GPU_storagebuf_sync_to_host(gpu::StorageBuf *ssbo)
bool GPU_texture_is_array(const gpu::Texture *texture)
gpu::UniformBuf * GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
void GPU_uniformbuf_update(gpu::UniformBuf *ubo, const void *data)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_framebuffer_default_size(gpu::FrameBuffer *fb, int width, int height)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
int GPU_texture_height(const 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)
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
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)
bool GPU_texture_has_stencil_format(const gpu::Texture *texture)
VecBase< double, 4 > double4
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)
MINLINE uint min_uu(uint a, uint b)
void GPU_storagebuf_read(gpu::StorageBuf *ssbo, void *data)