[Bf-blender-cvs] [87055dc71b0] master: GPU: Compute Pipeline.

Jeroen Bakker noreply at git.blender.org
Wed May 26 16:53:00 CEST 2021


Commit: 87055dc71b0d50cd25660969b55cda7d44af6a12
Author: Jeroen Bakker
Date:   Wed May 26 16:49:17 2021 +0200
Branches: master
https://developer.blender.org/rB87055dc71b0d50cd25660969b55cda7d44af6a12

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

===================================================================

M	source/blender/draw/intern/draw_manager_shader.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_capabilities.h
A	source/blender/gpu/GPU_compute.h
M	source/blender/gpu/GPU_index_buffer.h
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/GPU_state.h
M	source/blender/gpu/GPU_vertex_buffer.h
M	source/blender/gpu/intern/gpu_backend.hh
M	source/blender/gpu/intern/gpu_capabilities.cc
M	source/blender/gpu/intern/gpu_capabilities_private.hh
A	source/blender/gpu/intern/gpu_compute.cc
M	source/blender/gpu/intern/gpu_index_buffer.cc
M	source/blender/gpu/intern/gpu_index_buffer_private.hh
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/gpu/intern/gpu_shader_interface.cc
M	source/blender/gpu/intern/gpu_shader_interface.hh
M	source/blender/gpu/intern/gpu_shader_private.hh
M	source/blender/gpu/intern/gpu_vertex_buffer.cc
M	source/blender/gpu/intern/gpu_vertex_buffer_private.hh
M	source/blender/gpu/opengl/gl_backend.cc
M	source/blender/gpu/opengl/gl_backend.hh
A	source/blender/gpu/opengl/gl_compute.cc
A	source/blender/gpu/opengl/gl_compute.hh
M	source/blender/gpu/opengl/gl_index_buffer.cc
M	source/blender/gpu/opengl/gl_index_buffer.hh
M	source/blender/gpu/opengl/gl_shader.cc
M	source/blender/gpu/opengl/gl_shader.hh
M	source/blender/gpu/opengl/gl_shader_interface.cc
M	source/blender/gpu/opengl/gl_state.hh
M	source/blender/gpu/opengl/gl_texture.cc
M	source/blender/gpu/opengl/gl_vertex_buffer.cc
M	source/blender/gpu/opengl/gl_vertex_buffer.hh
A	source/blender/gpu/tests/gpu_shader_test.cc

===================================================================

diff --git a/source/blender/draw/intern/draw_manager_shader.c b/source/blender/draw/intern/draw_manager_shader.c
index 2aad1f10154..83d0030f89b 100644
--- a/source/blender/draw/intern/draw_manager_shader.c
+++ b/source/blender/draw/intern/draw_manager_shader.c
@@ -396,6 +396,7 @@ GPUShader *DRW_shader_create_with_transform_feedback(const char *vert,
                               datatoc_gpu_shader_depth_only_frag_glsl,
                               geom,
                               NULL,
+                              NULL,
                               defines,
                               prim_type,
                               varying_names,
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index f1ffd7827b8..cf6009c2881 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -62,6 +62,7 @@ set(SRC
   intern/gpu_buffers.c
   intern/gpu_capabilities.cc
   intern/gpu_codegen.c
+  intern/gpu_compute.cc
   intern/gpu_context.cc
   intern/gpu_debug.cc
   intern/gpu_drawlist.cc
@@ -91,6 +92,7 @@ set(SRC
 
   opengl/gl_backend.cc
   opengl/gl_batch.cc
+  opengl/gl_compute.cc
   opengl/gl_context.cc
   opengl/gl_debug.cc
   opengl/gl_debug_layer.cc
@@ -113,6 +115,7 @@ set(SRC
   GPU_buffers.h
   GPU_capabilities.h
   GPU_common.h
+  GPU_compute.h
   GPU_context.h
   GPU_debug.h
   GPU_drawlist.h
@@ -163,6 +166,7 @@ set(SRC
 
   opengl/gl_backend.hh
   opengl/gl_batch.hh
+  opengl/gl_compute.hh
   opengl/gl_context.hh
   opengl/gl_debug.hh
   opengl/gl_drawlist.hh
@@ -390,6 +394,7 @@ if(WITH_GTESTS)
   if(WITH_OPENGL_DRAW_TESTS)
     set(TEST_SRC
       tests/gpu_testing.cc
+      tests/gpu_shader_test.cc
 
       tests/gpu_testing.hh
     )
diff --git a/source/blender/gpu/GPU_capabilities.h b/source/blender/gpu/GPU_capabilities.h
index f54ecece659..45c656b49be 100644
--- a/source/blender/gpu/GPU_capabilities.h
+++ b/source/blender/gpu/GPU_capabilities.h
@@ -55,6 +55,8 @@ bool GPU_use_main_context_workaround(void);
 bool GPU_use_hq_normals_workaround(void);
 bool GPU_crappy_amd_driver(void);
 
+bool GPU_compute_shader_support(void);
+bool GPU_shader_storage_buffer_objects_support(void);
 bool GPU_shader_image_load_store_support(void);
 
 bool GPU_mem_stats_supported(void);
diff --git a/source/blender/gpu/GPU_compute.h b/source/blender/gpu/GPU_compute.h
new file mode 100644
index 00000000000..a048f72c0a0
--- /dev/null
+++ b/source/blender/gpu/GPU_compute.h
@@ -0,0 +1,38 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup gpu
+ */
+
+#pragma once
+
+#include "BLI_sys_types.h"
+
+#include "GPU_shader.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void GPU_compute_dispatch(GPUShader *shader,
+                          uint groups_x_len,
+                          uint groups_y_len,
+                          uint groups_z_len);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/gpu/GPU_index_buffer.h b/source/blender/gpu/GPU_index_buffer.h
index 76aab3c196b..bdacfe6fc0f 100644
--- a/source/blender/gpu/GPU_index_buffer.h
+++ b/source/blender/gpu/GPU_index_buffer.h
@@ -49,6 +49,7 @@ void GPU_indexbuf_init_ex(GPUIndexBufBuilder *, GPUPrimType, uint index_len, uin
 
 /* supports only GPU_PRIM_POINTS, GPU_PRIM_LINES and GPU_PRIM_TRIS. */
 void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len);
+GPUIndexBuf *GPU_indexbuf_build_on_device(uint index_len);
 
 void GPU_indexbuf_add_generic_vert(GPUIndexBufBuilder *, uint v);
 void GPU_indexbuf_add_primitive_restart(GPUIndexBufBuilder *);
@@ -70,6 +71,8 @@ void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem);
 GPUIndexBuf *GPU_indexbuf_build(GPUIndexBufBuilder *);
 void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *, GPUIndexBuf *);
 
+void GPU_indexbuf_bind_as_ssbo(GPUIndexBuf *elem, int binding);
+
 /* Create a sub-range of an existing index-buffer. */
 GPUIndexBuf *GPU_indexbuf_create_subrange(GPUIndexBuf *elem_src, uint start, uint length);
 void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
@@ -77,6 +80,15 @@ void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
                                            uint start,
                                            uint length);
 
+/**
+ * (Download and) return a pointer containing the data of an 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`.
+ */
+const uint32_t *GPU_indexbuf_read(GPUIndexBuf *elem);
+uint32_t *GPU_indexbuf_unmap(const GPUIndexBuf *elem, const uint32_t *mapped_data);
+
 void GPU_indexbuf_discard(GPUIndexBuf *elem);
 
 bool GPU_indexbuf_is_init(GPUIndexBuf *elem);
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index 9824c7016dc..3923c920c9e 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -27,6 +27,7 @@
 extern "C" {
 #endif
 
+struct GPUIndexBuf;
 struct GPUVertBuf;
 
 /** Opaque type hiding #blender::gpu::Shader */
@@ -45,6 +46,10 @@ GPUShader *GPU_shader_create(const char *vertcode,
                              const char *libcode,
                              const char *defines,
                              const char *shname);
+GPUShader *GPU_shader_create_compute(const char *computecode,
+                                     const char *libcode,
+                                     const char *defines,
+                                     const char *shname);
 GPUShader *GPU_shader_create_from_python(const char *vertcode,
                                          const char *fragcode,
                                          const char *geomcode,
@@ -53,6 +58,7 @@ GPUShader *GPU_shader_create_from_python(const char *vertcode,
 GPUShader *GPU_shader_create_ex(const char *vertcode,
                                 const char *fragcode,
                                 const char *geomcode,
+                                const char *computecode,
                                 const char *libcode,
                                 const char *defines,
                                 const eGPUShaderTFBType tf_type,
@@ -126,6 +132,7 @@ int GPU_shader_get_uniform(GPUShader *shader, const char *name);
 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
 int GPU_shader_get_builtin_block(GPUShader *shader, int builtin);
 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name);
+int GPU_shader_get_ssbo(GPUShader *shader, const char *name);
 
 int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name);
 int GPU_shader_get_texture_binding(GPUShader *shader, const char *name);
diff --git a/source/blender/gpu/GPU_state.h b/source/blender/gpu/GPU_state.h
index 0687f271670..a338728804c 100644
--- a/source/blender/gpu/GPU_state.h
+++ b/source/blender/gpu/GPU_state.h
@@ -39,6 +39,7 @@ typedef enum eGPUBarrier {
   GPU_BARRIER_NONE = 0,
   GPU_BARRIER_SHADER_IMAGE_ACCESS = (1 << 0),
   GPU_BARRIER_TEXTURE_FETCH = (1 << 1),
+  GPU_BARRIER_SHADER_STORAGE = (1 << 2),
 } eGPUBarrier;
 
 ENUM_OPERATORS(eGPUBarrier, GPU_BARRIER_TEXTURE_FETCH)
diff --git a/source/blender/gpu/GPU_vertex_buffer.h b/source/blender/gpu/GPU_vertex_buffer.h
index aae58de533b..2c54016daa7 100644
--- a/source/blender/gpu/GPU_vertex_buffer.h
+++ b/source/blender/gpu/GPU_vertex_buffer.h
@@ -59,6 +59,7 @@ typedef enum {
   GPU_USAGE_STREAM,
   GPU_USAGE_STATIC, /* do not keep data in memory */
   GPU_USAGE_DYNAMIC,
+  GPU_USAGE_DEVICE_ONLY, /* Do not do host->device data transfers. */
 } GPUUsageType;
 
 /** Opaque type hiding blender::gpu::VertBuf. */
@@ -70,6 +71,14 @@ GPUVertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat *, GPUUsageTyp
 #define GPU_vertbuf_create_with_format(format) \
   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`.
+ */
+const void *GPU_vertbuf_read(GPUVertBuf *verts);
+void *GPU_vertbuf_unmap(const GPUVertBuf *verts, const void *mapped_data);
 void GPU_vertbuf_clear(GPUVertBuf *verts);
 void GPU_vertbuf_discard(GPUVertBuf *);
 
@@ -138,6 +147,7 @@ uint GPU_vertbuf_get_vertex_len(const GPUVertBuf *verts);
 GPUVertBufStatus GPU_vertbuf_get_status(const GPUVertBuf *verts);
 
 void GPU_vertbuf_use(GPUVertBuf *);
+void GPU_vertbuf_bind_as_ssbo(struct GPUVertBuf *verts, int binding);
 
 /* XXX do not use. */
 void GPU_vertbuf_update_sub(GPUVertBuf *verts, uint start, uint len, void *data);
diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh
index 04ec82a9213..73792215569 100644
--- a/source/blender/gpu/intern/gpu_backend.hh
+++ b/source/blender/gpu/intern/gpu_backend.hh
@@ -47,6 +47,7 @@ class GPUBackend {
   static GPUBackend *get(void);
 
   virtual void samplers_update(void) = 0;
+  virtual void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) = 0;
 
   virtual Context *context_alloc(void *ghost_window) = 0;
 
diff --git a/source/blender/gpu/intern/gpu_capabilities.cc b/source/blender/gpu/intern/gpu_capabilities.cc
index d8764502800..bedc9ad3092 100644
--- a/source/blender/gpu/intern/gpu_capabilities.cc
+++ b/source/blender/gpu/intern/gpu_capabilities.cc
@@ -148,6 +148,16 @@ bool GPU_use_hq_normals_workaround(void)
   return GCaps.use_hq_no

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list