1
1

GPUStorageBuf: Add GPU_storagebuf_copy_sub_from_vertbuf()

This allows using the Graphic API to copy buffer data.
The GPU module do not expose untyped buffers even if that's what most API
do, so the copy function need to be strongly typed.

Contains GL backend implementation.
This commit is contained in:
2022-05-18 21:49:08 +02:00
parent 9631bb1e17
commit 33c5adba62
6 changed files with 47 additions and 2 deletions

View File

@@ -47,6 +47,17 @@ void GPU_storagebuf_clear(GPUStorageBuf *ssbo,
void *data);
void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo);
/**
* @brief Copy a part of a vertex buffer to a storage buffer.
* \a ssbo: destination storage buffer
* \a src: source vertex buffer
* \a dst_offset: where to start copying to (in bytes).
* \a src_offset: where to start copying from (in bytes).
* \a copy_size: byte size of the segment to copy.
*/
void GPU_storagebuf_copy_sub_from_vertbuf(
GPUStorageBuf *ssbo, GPUVertBuf *src, uint dst_offset, uint src_offset, uint copy_size);
#ifdef __cplusplus
}
#endif

View File

@@ -19,6 +19,7 @@
#include "GPU_storage_buffer.h"
#include "gpu_storage_buffer_private.hh"
#include "gpu_vertex_buffer_private.hh"
/* -------------------------------------------------------------------- */
/** \name Creation & Deletion
@@ -103,4 +104,10 @@ void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo)
GPU_storagebuf_clear(ssbo, GPU_R32UI, GPU_DATA_UINT, &data);
}
void GPU_storagebuf_copy_sub_from_vertbuf(
GPUStorageBuf *ssbo, GPUVertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
{
unwrap(ssbo)->copy_sub(unwrap(src), dst_offset, src_offset, copy_size);
}
/** \} */

View File

@@ -43,6 +43,7 @@ class StorageBuf {
virtual void clear(eGPUTextureFormat internal_format,
eGPUDataFormat data_format,
void *data) = 0;
virtual void copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size) = 0;
};
/* Syntactic sugar. */

View File

@@ -144,6 +144,30 @@ void GLStorageBuf::clear(eGPUTextureFormat internal_format, eGPUDataFormat data_
}
}
void GLStorageBuf::copy_sub(VertBuf *src_, uint dst_offset, uint src_offset, uint copy_size)
{
GLVertBuf *src = static_cast<GLVertBuf *>(src_);
GLStorageBuf *dst = this;
if (dst->ssbo_id_ == 0) {
dst->init();
}
if (src->vbo_id_ == 0) {
src->bind();
}
if (GLContext::direct_state_access_support) {
glCopyNamedBufferSubData(src->vbo_id_, dst->ssbo_id_, src_offset, dst_offset, copy_size);
}
else {
/* This binds the buffer to GL_ARRAY_BUFFER and upload the data if any. */
src->bind();
glBindBuffer(GL_COPY_WRITE_BUFFER, dst->ssbo_id_);
glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, src_offset, dst_offset, copy_size);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
}
}
/** \} */
} // namespace blender::gpu

View File

@@ -36,6 +36,7 @@ class GLStorageBuf : public StorageBuf {
void bind(int slot) override;
void unbind() override;
void clear(eGPUTextureFormat internal_format, eGPUDataFormat data_format, void *data) override;
void copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size) override;
/* Special internal function to bind SSBOs to indirect argument targets. */
void bind_as(GLenum target);

View File

@@ -19,8 +19,9 @@ namespace blender {
namespace gpu {
class GLVertBuf : public VertBuf {
friend class GLTexture; /* For buffer texture. */
friend class GLShader; /* For transform feedback. */
friend class GLTexture; /* For buffer texture. */
friend class GLShader; /* For transform feedback. */
friend class GLStorageBuf; /* For sub copy. */
private:
/** OpenGL buffer handle. Init on first upload. Immutable after that. */