From 80811c5638ac5ccb425eaea2886273c4a3121aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 18 May 2022 21:39:56 +0200 Subject: DRW: Replace StorageFlexibleBuffer with explicit `get_or_resize()` This is to avoid hiding resize inside the `[]` operator. --- source/blender/draw/intern/DRW_gpu_wrapper.hh | 46 +++++++-------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/source/blender/draw/intern/DRW_gpu_wrapper.hh b/source/blender/draw/intern/DRW_gpu_wrapper.hh index 366dd40c220..361ea743bb6 100644 --- a/source/blender/draw/intern/DRW_gpu_wrapper.hh +++ b/source/blender/draw/intern/DRW_gpu_wrapper.hh @@ -31,9 +31,6 @@ * discarding all data inside it. * Data can be accessed using the [] operator. * - * `draw::StorageFlexibleBuffer` - * Same as StorageArrayBuffer but will auto resize on access when using the [] operator. - * * `draw::StorageBuffer` * A storage buffer object class inheriting from T. * Data can be accessed just like a normal T object. @@ -237,6 +234,17 @@ class StorageCommon : public DataBuffer, NonMovable, NonCopyable } } + /* Resize on access. */ + T &get_or_resize(int64_t index) + { + BLI_assert(index >= 0); + if (index >= this->len_) { + size_t size = 1u << log2_ceil_u(index); + this->resize(size); + } + return this->data_[index]; + } + void push_update(void) { BLI_assert(device_only == false); @@ -338,38 +346,6 @@ class StorageArrayBuffer : public detail::StorageCommon { } }; -template< - /** Type of the values stored in this uniform buffer. */ - typename T, - /** True if created on device and no memory host memory is allocated. */ - bool device_only = false> -class StorageFlexibleBuffer : public detail::StorageCommon { - public: - StorageFlexibleBuffer(const char *name = nullptr) - : detail::StorageCommon(name) - { - /* TODO(@fclem): We should map memory instead. */ - this->data_ = (T *)MEM_mallocN_aligned(sizeof(T), 16, this->name_); - } - ~StorageFlexibleBuffer() - { - MEM_freeN(this->data_); - } - - /* Resize on access. */ - T &operator[](int64_t index) - { - BLI_STATIC_ASSERT(!device_only, ""); - BLI_assert(index >= 0); - if (index >= this->len_) { - this->resize(this->len_ * 2); - } - return this->data_[index]; - } - - /* TODO(fclem): Implement shrinking. */ -}; - template< /** Type of the values stored in this uniform buffer. */ typename T, -- cgit v1.2.3