1
1

DRW: GPU wrappers: Fix resize routines for StorageArrayBuffer

Resizing was not resizing the `data_` buffer. Also use `power_of_2_max_u`.
This commit is contained in:
2022-05-19 00:35:24 +02:00
parent b16eff2bb3
commit ae2d2c9361

View File

@@ -217,7 +217,9 @@ class StorageCommon : public DataBuffer<T, len, false>, NonMovable, NonCopyable
if (name) {
name_ = name;
}
init(len);
this->len_ = len;
constexpr GPUUsageType usage = device_only ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_DYNAMIC;
ssbo_ = GPU_storagebuf_create_ex(sizeof(T) * this->len_, nullptr, usage, this->name_);
}
~StorageCommon()
@@ -225,26 +227,6 @@ class StorageCommon : public DataBuffer<T, len, false>, NonMovable, NonCopyable
GPU_storagebuf_free(ssbo_);
}
void resize(int64_t new_size)
{
BLI_assert(new_size > 0);
if (new_size != this->len_) {
GPU_storagebuf_free(ssbo_);
this->init(new_size);
}
}
/* 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);
@@ -260,14 +242,6 @@ class StorageCommon : public DataBuffer<T, len, false>, NonMovable, NonCopyable
{
return &ssbo_;
}
private:
void init(int64_t new_size)
{
this->len_ = new_size;
GPUUsageType usage = device_only ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_DYNAMIC;
ssbo_ = GPU_storagebuf_create_ex(sizeof(T) * this->len_, nullptr, usage, this->name_);
}
};
} // namespace detail
@@ -344,6 +318,34 @@ class StorageArrayBuffer : public detail::StorageCommon<T, len, device_only> {
{
MEM_freeN(this->data_);
}
void resize(int64_t new_size)
{
BLI_assert(new_size > 0);
if (new_size != this->len_) {
/* Manual realloc since MEM_reallocN_aligned does not exists. */
T *new_data_ = (T *)MEM_mallocN_aligned(new_size * sizeof(T), 16, this->name_);
memcpy(new_data_, this->data_, min_uu(this->len_, new_size) * sizeof(T));
MEM_freeN(this->data_);
this->data_ = new_data_;
GPU_storagebuf_free(this->ssbo_);
this->len_ = new_size;
constexpr GPUUsageType usage = device_only ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_DYNAMIC;
this->ssbo_ = GPU_storagebuf_create_ex(sizeof(T) * this->len_, nullptr, usage, this->name_);
}
}
/* Resize on access. */
T &get_or_resize(int64_t index)
{
BLI_assert(index >= 0);
if (index >= this->len_) {
size_t size = power_of_2_max_u(index + 1);
this->resize(size);
}
return this->data_[index];
}
};
template<