GPUStorageBuf: Add read() function to readback buffer data to host

This is not expected to be fast. This is only for inspecting the content
of the buffer for debugging or validation purpose.
This commit is contained in:
2022-08-30 21:52:09 +02:00
parent 25237d2625
commit fe195f51d1
5 changed files with 31 additions and 0 deletions

View File

@@ -47,6 +47,13 @@ void GPU_storagebuf_clear(GPUStorageBuf *ssbo,
void *data);
void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo);
/**
* Read back content of the buffer to CPU for inspection.
* Slow! Only use for inspection / debugging.
* NOTE: Not synchronized. Use appropriate barrier before reading.
*/
void GPU_storagebuf_read(GPUStorageBuf *ssbo, void *data);
/**
* \brief Copy a part of a vertex buffer to a storage buffer.
*

View File

@@ -109,4 +109,9 @@ void GPU_storagebuf_copy_sub_from_vertbuf(
unwrap(ssbo)->copy_sub(unwrap(src), dst_offset, src_offset, copy_size);
}
void GPU_storagebuf_read(GPUStorageBuf *ssbo, void *data)
{
unwrap(ssbo)->read(data);
}
/** \} */

View File

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

View File

@@ -166,6 +166,23 @@ void GLStorageBuf::copy_sub(VertBuf *src_, uint dst_offset, uint src_offset, uin
}
}
void GLStorageBuf::read(void *data)
{
if (ssbo_id_ == 0) {
this->init();
}
if (GLContext::direct_state_access_support) {
glGetNamedBufferSubData(ssbo_id_, 0, size_in_bytes_, data);
}
else {
/* This binds the buffer to GL_ARRAY_BUFFER and upload the data if any. */
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_id_);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, size_in_bytes_, data);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
}
/** \} */
} // namespace blender::gpu

View File

@@ -35,6 +35,7 @@ class GLStorageBuf : public StorageBuf {
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;
void read(void *data) override;
/* Special internal function to bind SSBOs to indirect argument targets. */
void bind_as(GLenum target);