From 7ee816e32f3655d4ca8abb555f885d8de9ba0520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 16 Mar 2022 08:42:12 +0100 Subject: GPU: StorageBuf: Add method to clear the buffer in place. This is a faster way to clear a buffer instead of reuploading new data. It is equivalent to `memset` and runs directly on the GPU. This is better to clear huge buffers and to avoid the sync cost of data upload. --- source/blender/gpu/GPU_storage_buffer.h | 7 ++++++ source/blender/gpu/intern/gpu_storage_buffer.cc | 14 ++++++++++++ .../gpu/intern/gpu_storage_buffer_private.hh | 3 +++ source/blender/gpu/opengl/gl_storage_buffer.cc | 25 ++++++++++++++++++++++ source/blender/gpu/opengl/gl_storage_buffer.hh | 1 + 5 files changed, 50 insertions(+) (limited to 'source/blender') diff --git a/source/blender/gpu/GPU_storage_buffer.h b/source/blender/gpu/GPU_storage_buffer.h index 930b6a1012b..1478d490e23 100644 --- a/source/blender/gpu/GPU_storage_buffer.h +++ b/source/blender/gpu/GPU_storage_buffer.h @@ -13,6 +13,7 @@ #pragma once +#include "GPU_texture.h" #include "GPU_vertex_buffer.h" #ifdef __cplusplus @@ -40,6 +41,12 @@ void GPU_storagebuf_bind(GPUStorageBuf *ssbo, int slot); void GPU_storagebuf_unbind(GPUStorageBuf *ssbo); void GPU_storagebuf_unbind_all(void); +void GPU_storagebuf_clear(GPUStorageBuf *ssbo, + eGPUTextureFormat internal_format, + eGPUDataFormat data_format, + void *data); +void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo); + #ifdef __cplusplus } #endif diff --git a/source/blender/gpu/intern/gpu_storage_buffer.cc b/source/blender/gpu/intern/gpu_storage_buffer.cc index 3ea0bef5403..806acad90fe 100644 --- a/source/blender/gpu/intern/gpu_storage_buffer.cc +++ b/source/blender/gpu/intern/gpu_storage_buffer.cc @@ -89,4 +89,18 @@ void GPU_storagebuf_unbind_all() /* FIXME */ } +void GPU_storagebuf_clear(GPUStorageBuf *ssbo, + eGPUTextureFormat internal_format, + eGPUDataFormat data_format, + void *data) +{ + unwrap(ssbo)->clear(internal_format, data_format, data); +} + +void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo) +{ + uint32_t data = 0u; + GPU_storagebuf_clear(ssbo, GPU_R32UI, GPU_DATA_UINT, &data); +} + /** \} */ diff --git a/source/blender/gpu/intern/gpu_storage_buffer_private.hh b/source/blender/gpu/intern/gpu_storage_buffer_private.hh index e97f3eba697..06e7c2c0e9d 100644 --- a/source/blender/gpu/intern/gpu_storage_buffer_private.hh +++ b/source/blender/gpu/intern/gpu_storage_buffer_private.hh @@ -40,6 +40,9 @@ class StorageBuf { virtual void update(const void *data) = 0; virtual void bind(int slot) = 0; virtual void unbind() = 0; + virtual void clear(eGPUTextureFormat internal_format, + eGPUDataFormat data_format, + void *data) = 0; }; /* Syntactic sugar. */ diff --git a/source/blender/gpu/opengl/gl_storage_buffer.cc b/source/blender/gpu/opengl/gl_storage_buffer.cc index 12f942439ca..9cdde0b5734 100644 --- a/source/blender/gpu/opengl/gl_storage_buffer.cc +++ b/source/blender/gpu/opengl/gl_storage_buffer.cc @@ -112,6 +112,31 @@ void GLStorageBuf::unbind() slot_ = 0; } +void GLStorageBuf::clear(eGPUTextureFormat internal_format, eGPUDataFormat data_format, void *data) +{ + if (ssbo_id_ == 0) { + this->init(); + } + + if (GLContext::direct_state_access_support) { + glClearNamedBufferData(ssbo_id_, + to_gl_internal_format(internal_format), + to_gl_data_format(internal_format), + to_gl(data_format), + data); + } + else { + /* WATCH(@fclem): This should be ok since we only use clear outside of drawing functions. */ + glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_id_); + glClearBufferData(GL_SHADER_STORAGE_BUFFER, + to_gl_internal_format(internal_format), + to_gl_data_format(internal_format), + to_gl(data_format), + data); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); + } +} + /** \} */ } // namespace blender::gpu diff --git a/source/blender/gpu/opengl/gl_storage_buffer.hh b/source/blender/gpu/opengl/gl_storage_buffer.hh index 106d2158273..d657a4de09c 100644 --- a/source/blender/gpu/opengl/gl_storage_buffer.hh +++ b/source/blender/gpu/opengl/gl_storage_buffer.hh @@ -35,6 +35,7 @@ class GLStorageBuf : public StorageBuf { void update(const void *data) override; void bind(int slot) override; void unbind() override; + void clear(eGPUTextureFormat internal_format, eGPUDataFormat data_format, void *data) override; private: void init(); -- cgit v1.2.3