Vulkan: Use guardedalloc for driver allocations. #104434

Merged
Jeroen Bakker merged 3 commits from Jeroen-Bakker/blender:vulkan-guardedalloc into main 2023-02-13 08:37:44 +01:00
19 changed files with 37 additions and 90 deletions
Showing only changes of commit fc1c3e90ca - Show all commits

View File

@ -89,13 +89,11 @@ void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
uint length);
/**
* (Download and) return a pointer containing the data of an index buffer.
* (Download and) fill data with the contents of the index buffer.
*
* Note that the returned pointer is still owned by the driver. To get an
* local copy, use `GPU_indexbuf_unmap` after calling `GPU_indexbuf_read`.
* NOTE: caller is responsible to reserve enough memory.
*/
const uint32_t *GPU_indexbuf_read(GPUIndexBuf *elem);
uint32_t *GPU_indexbuf_unmap(const GPUIndexBuf *elem, const uint32_t *mapped_buffer);
void GPU_indexbuf_read(GPUIndexBuf *elem, uint32_t *data);
void GPU_indexbuf_discard(GPUIndexBuf *elem);

View File

@ -64,13 +64,10 @@ GPUVertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat *, GPUUsageTyp
GPU_vertbuf_create_with_format_ex(format, GPU_USAGE_STATIC)
/**
* (Download and) return a pointer containing the data of a vertex buffer.
*
* Note that the returned pointer is still owned by the driver. To get an
* local copy, use `GPU_vertbuf_unmap` after calling `GPU_vertbuf_read`.
* (Download and) fill data with the data from the vertex buffer.
* NOTE: caller is responsible to reserve enough memory of the data parameter.
*/
const void *GPU_vertbuf_read(GPUVertBuf *verts);
void *GPU_vertbuf_unmap(const GPUVertBuf *verts, const void *mapped_data);
void GPU_vertbuf_read(GPUVertBuf *verts, void *data);
/** Same as discard but does not free. */
void GPU_vertbuf_clear(GPUVertBuf *verts);
void GPU_vertbuf_discard(GPUVertBuf *);

View File

@ -399,14 +399,6 @@ void IndexBuf::squeeze_indices_short(uint min_idx,
}
}
uint32_t *IndexBuf::unmap(const uint32_t *mapped_memory) const
{
size_t size = size_get();
uint32_t *result = static_cast<uint32_t *>(MEM_mallocN(size, __func__));
memcpy(result, mapped_memory, size);
return result;
}
} // namespace blender::gpu
/** \} */
@ -456,14 +448,9 @@ void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
unwrap(elem)->init_subrange(unwrap(elem_src), start, length);
}
const uint32_t *GPU_indexbuf_read(GPUIndexBuf *elem)
void GPU_indexbuf_read(GPUIndexBuf *elem, uint32_t *data)
{
return unwrap(elem)->read();
}
uint32_t *GPU_indexbuf_unmap(const GPUIndexBuf *elem, const uint32_t *mapped_buffer)
{
return unwrap(elem)->unmap(mapped_buffer);
return unwrap(elem)->read(data);
}
void GPU_indexbuf_discard(GPUIndexBuf *elem)

View File

@ -98,8 +98,7 @@ class IndexBuf {
virtual void bind_as_ssbo(uint binding) = 0;
virtual const uint32_t *read() const = 0;
uint32_t *unmap(const uint32_t *mapped_memory) const;
virtual void read(uint32_t *data) const = 0;
virtual void update_sub(uint start, uint len, const void *data) = 0;

View File

@ -153,14 +153,9 @@ GPUVertBuf *GPU_vertbuf_duplicate(GPUVertBuf *verts_)
return wrap(unwrap(verts_)->duplicate());
}
const void *GPU_vertbuf_read(GPUVertBuf *verts)
void GPU_vertbuf_read(GPUVertBuf *verts, void *data)
{
return unwrap(verts)->read();
}
void *GPU_vertbuf_unmap(const GPUVertBuf *verts, const void *mapped_data)
{
return unwrap(verts)->unmap(mapped_data);
unwrap(verts)->read(data);
}
void GPU_vertbuf_clear(GPUVertBuf *verts)

View File

@ -94,8 +94,7 @@ class VertBuf {
}
virtual void update_sub(uint start, uint len, const void *data) = 0;
virtual const void *read() const = 0;
virtual void *unmap(const void *mapped_data) const = 0;
virtual void read(void *data) const = 0;
protected:
virtual void acquire_data() = 0;

View File

@ -48,7 +48,7 @@ class MTLIndexBuf : public IndexBuf {
~MTLIndexBuf();
void bind_as_ssbo(uint32_t binding) override;
const uint32_t *read() const override;
void read(uint32_t *data) const override;
void upload_data() override;
void update_sub(uint32_t start, uint32_t len, const void *data) override;

View File

@ -46,16 +46,13 @@ void MTLIndexBuf::bind_as_ssbo(uint32_t binding)
MTL_LOG_WARNING("MTLIndexBuf::bind_as_ssbo not yet implemented!\n");
}
const uint32_t *MTLIndexBuf::read() const
void MTLIndexBuf::read(uint32_t *data) const
{
if (ibo_ != nullptr) {
/* Return host pointer. */
void *data = ibo_->get_host_ptr();
return static_cast<uint32_t *>(data);
void *host_ptr = ibo_->get_host_ptr();
memcpy(data, host_ptr, size_get());
}
BLI_assert(false && "Index buffer not ready to be read.");
return nullptr;
}
void MTLIndexBuf::upload_data()

View File

@ -56,8 +56,7 @@ class MTLVertBuf : public VertBuf {
void update_sub(uint start, uint len, const void *data) override;
const void *read() const override;
void *unmap(const void *mapped_data) const override;
void read(void *data) const override;
void wrap_handle(uint64_t handle) override;

View File

@ -328,21 +328,12 @@ void MTLVertBuf::bind_as_texture(uint binding)
GPU_texture_bind(buffer_texture_, binding);
}
const void *MTLVertBuf::read() const
void MTLVertBuf::read(void *data) const
{
BLI_assert(vbo_ != nullptr);
BLI_assert(usage_ != GPU_USAGE_DEVICE_ONLY);
void *return_ptr = vbo_->get_host_ptr();
BLI_assert(return_ptr != nullptr);
return return_ptr;
}
void *MTLVertBuf::unmap(const void *mapped_data) const
{
void *result = MEM_mallocN(alloc_size_, __func__);
memcpy(result, mapped_data, alloc_size_);
return result;
void *host_ptr = vbo_->get_host_ptr();
memcpy(data, host_ptr, alloc_size_);
}
void MTLVertBuf::wrap_handle(uint64_t handle)

View File

@ -46,12 +46,12 @@ void GLIndexBuf::bind_as_ssbo(uint binding)
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, ibo_id_);
}
const uint32_t *GLIndexBuf::read() const
void GLIndexBuf::read(uint32_t *data) const
{
BLI_assert(is_active());
void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
uint32_t *result = static_cast<uint32_t *>(data);
return result;
void *buffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
memcpy(data, buffer, size_get());
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
bool GLIndexBuf::is_active() const

View File

@ -29,7 +29,7 @@ class GLIndexBuf : public IndexBuf {
void bind();
void bind_as_ssbo(uint binding) override;
const uint32_t *read() const override;
void read(uint32_t *data) const override;
void *offset_ptr(uint additional_vertex_offset) const
{

View File

@ -125,18 +125,12 @@ void GLVertBuf::bind_as_texture(uint binding)
GPU_texture_bind(buffer_texture_, binding);
}
const void *GLVertBuf::read() const
void GLVertBuf::read(void *data) const
{
BLI_assert(is_active());
void *result = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
return result;
}
void *GLVertBuf::unmap(const void *mapped_data) const
{
void *result = MEM_mallocN(vbo_size_, __func__);
memcpy(result, mapped_data, vbo_size_);
return result;
memcpy(data, result, size_used_get());
glUnmapBuffer(GL_ARRAY_BUFFER);
}
void GLVertBuf::wrap_handle(uint64_t handle)

View File

@ -37,8 +37,7 @@ class GLVertBuf : public VertBuf {
void update_sub(uint start, uint len, const void *data) override;
const void *read() const override;
void *unmap(const void *mapped_data) const override;
void read(void *data) const override;
void wrap_handle(uint64_t handle) override;

View File

@ -151,8 +151,8 @@ static void test_gpu_shader_compute_vbo()
GPU_memory_barrier(GPU_BARRIER_SHADER_STORAGE);
/* Download the vertex buffer. */
const float *data = static_cast<const float *>(GPU_vertbuf_read(vbo));
ASSERT_NE(data, nullptr);
float data[SIZE * 4];
GPU_vertbuf_read(vbo, data);
for (int index = 0; index < SIZE; index++) {
float expected_value = index;
EXPECT_FLOAT_EQ(data[index * 4 + 0], expected_value);
@ -195,8 +195,8 @@ static void test_gpu_shader_compute_ibo()
GPU_memory_barrier(GPU_BARRIER_SHADER_STORAGE);
/* Download the index buffer. */
const uint32_t *data = GPU_indexbuf_read(ibo);
ASSERT_NE(data, nullptr);
uint32_t data[SIZE];
GPU_indexbuf_read(ibo, data);
for (int index = 0; index < SIZE; index++) {
uint32_t expected = index;
EXPECT_EQ(data[index], expected);

View File

@ -17,9 +17,8 @@ void VKIndexBuffer::bind_as_ssbo(uint /*binding*/)
{
}
const uint32_t *VKIndexBuffer::read() const
void VKIndexBuffer::read(uint32_t * /*data*/) const
{
return 0;
}
void VKIndexBuffer::update_sub(uint /*start*/, uint /*len*/, const void * /*data*/)

View File

@ -17,7 +17,7 @@ class VKIndexBuffer : public IndexBuf {
void bind_as_ssbo(uint binding) override;
const uint32_t *read() const override;
void read(uint32_t *data) const override;
void update_sub(uint start, uint len, const void *data) override;

View File

@ -32,14 +32,8 @@ void VKVertexBuffer::update_sub(uint /*start*/, uint /*len*/, const void * /*dat
{
}
const void *VKVertexBuffer::read() const
void VKVertexBuffer::read(void * /*data*/) const
{
return nullptr;
}
void *VKVertexBuffer::unmap(const void * /*mapped_data*/) const
{
return nullptr;
}
void VKVertexBuffer::acquire_data()

View File

@ -20,8 +20,7 @@ class VKVertexBuffer : public VertBuf {
void wrap_handle(uint64_t handle) override;
void update_sub(uint start, uint len, const void *data) override;
const void *read() const override;
void *unmap(const void *mapped_data) const override;
void read(void *data) const override;
protected:
void acquire_data() override;