diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-05-16 14:34:30 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-05-17 13:34:35 +0300 |
commit | c7033bdf26bd72f92ab93c4c43f4e54f5f3160b7 (patch) | |
tree | f12606da602e797e6598506d57849b148a5ad67d | |
parent | 65fa34f63fe4bc452249c3f1041d7c79edaeb9e7 (diff) |
DRWWrapper: Add StorageFlexibleBuffer
This buffer resizes on access.
-rw-r--r-- | source/blender/draw/intern/DRW_gpu_wrapper.hh | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/source/blender/draw/intern/DRW_gpu_wrapper.hh b/source/blender/draw/intern/DRW_gpu_wrapper.hh index d6ee50ff5aa..366dd40c220 100644 --- a/source/blender/draw/intern/DRW_gpu_wrapper.hh +++ b/source/blender/draw/intern/DRW_gpu_wrapper.hh @@ -31,6 +31,9 @@ * discarding all data inside it. * Data can be accessed using the [] operator. * + * `draw::StorageFlexibleBuffer<T>` + * Same as StorageArrayBuffer but will auto resize on access when using the [] operator. + * * `draw::StorageBuffer<T>` * A storage buffer object class inheriting from T. * Data can be accessed just like a normal T object. @@ -340,6 +343,38 @@ template< typename T, /** True if created on device and no memory host memory is allocated. */ bool device_only = false> +class StorageFlexibleBuffer : public detail::StorageCommon<T, 1, device_only> { + public: + StorageFlexibleBuffer(const char *name = nullptr) + : detail::StorageCommon<T, 1, device_only>(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, + /** True if created on device and no memory host memory is allocated. */ + bool device_only = false> class StorageBuffer : public T, public detail::StorageCommon<T, 1, device_only> { public: StorageBuffer(const char *name = nullptr) : detail::StorageCommon<T, 1, device_only>(name) |