GPU: Compute Pipeline.

With the compute pipeline calculation can be offloaded to the GPU.
This patch only adds the framework for compute. So no changes for users at
this moment.

NOTE: As this is an OpenGL4.3 feature it must always have a fallback.

Use `GPU_compute_shader_support` to check if compute pipeline can be used.
Check `gpu_shader_compute*` test cases for usage.

This patch also adds support for shader storage buffer objects and device only
vertex/index buffers.

An alternative that had been discussed was adding this to the `GPUBatch`, this
was eventually not chosen as it would lead to more code when used as part of a
shading group. The idea is that we add an `eDRWCommandType` in the near
future.

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D10913
This commit is contained in:
Jeroen Bakker
2021-05-26 16:49:17 +02:00
committed by Jeroen Bakker
parent e459a25e6c
commit 87055dc71b
34 changed files with 819 additions and 19 deletions

View File

@@ -29,6 +29,10 @@ namespace blender::gpu {
void GLVertBuf::acquire_data()
{
if (usage_ == GPU_USAGE_DEVICE_ONLY) {
return;
}
/* Discard previous data if any. */
MEM_SAFE_FREE(data);
data = (uchar *)MEM_mallocN(sizeof(uchar) * this->size_alloc_get(), __func__);
@@ -36,6 +40,10 @@ void GLVertBuf::acquire_data()
void GLVertBuf::resize_data()
{
if (usage_ == GPU_USAGE_DEVICE_ONLY) {
return;
}
data = (uchar *)MEM_reallocN(data, sizeof(uchar) * this->size_alloc_get());
}
@@ -94,8 +102,10 @@ void GLVertBuf::bind()
vbo_size_ = this->size_used_get();
/* Orphan the vbo to avoid sync then upload data. */
glBufferData(GL_ARRAY_BUFFER, vbo_size_, nullptr, to_gl(usage_));
glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data);
/* Do not transfer data from host to device when buffer is device only. */
if (usage_ != GPU_USAGE_DEVICE_ONLY) {
glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data);
}
memory_usage += vbo_size_;
if (usage_ == GPU_USAGE_STATIC) {
@@ -106,6 +116,37 @@ void GLVertBuf::bind()
}
}
void GLVertBuf::bind_as_ssbo(uint binding)
{
bind();
BLI_assert(vbo_id_ != 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, vbo_id_);
}
const void *GLVertBuf::read() 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;
}
bool GLVertBuf::is_active() const
{
if (!vbo_id_) {
return false;
}
int active_vbo_id = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &active_vbo_id);
return vbo_id_ == active_vbo_id;
}
void GLVertBuf::update_sub(uint start, uint len, void *data)
{
glBufferSubData(GL_ARRAY_BUFFER, start, len, data);