[Bf-blender-cvs] [4ea93029c65] master: GPUIndexBuf: GL backend Isolation

Clément Foucault noreply at git.blender.org
Sun Sep 6 22:15:36 CEST 2020


Commit: 4ea93029c65fd4cdb8b707494855120c0f792952
Author: Clément Foucault
Date:   Sun Sep 6 02:46:51 2020 +0200
Branches: master
https://developer.blender.org/rB4ea93029c65fd4cdb8b707494855120c0f792952

GPUIndexBuf: GL backend Isolation

This is part of the Vulkan backend task T68990.

There is no real change, only making some code re-organisation.
This also make the IndexBuf completely abstract from outside the
GPU module.

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

M	source/blender/draw/intern/draw_cache_inline.h
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_index_buffer.h
M	source/blender/gpu/intern/gpu_backend.hh
M	source/blender/gpu/intern/gpu_batch.cc
M	source/blender/gpu/intern/gpu_batch_private.hh
M	source/blender/gpu/intern/gpu_index_buffer.cc
A	source/blender/gpu/intern/gpu_index_buffer_private.hh
M	source/blender/gpu/opengl/gl_backend.hh
M	source/blender/gpu/opengl/gl_batch.cc
M	source/blender/gpu/opengl/gl_batch.hh
M	source/blender/gpu/opengl/gl_drawlist.cc
M	source/blender/gpu/opengl/gl_drawlist.hh
A	source/blender/gpu/opengl/gl_index_buffer.cc
A	source/blender/gpu/opengl/gl_index_buffer.hh
M	source/blender/gpu/opengl/gl_vertex_array.cc

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

diff --git a/source/blender/draw/intern/draw_cache_inline.h b/source/blender/draw/intern/draw_cache_inline.h
index 0f0e1785a2a..720cc31b0be 100644
--- a/source/blender/draw/intern/draw_cache_inline.h
+++ b/source/blender/draw/intern/draw_cache_inline.h
@@ -80,7 +80,7 @@ BLI_INLINE bool DRW_ibo_requested(GPUIndexBuf *ibo)
 {
   /* TODO do not rely on data uploaded. This prevents multithreading.
    * (need access to a gl context) */
-  return (ibo != NULL && ibo->ibo_id == 0 && ibo->data == NULL);
+  return (ibo != NULL && !GPU_indexbuf_is_init(ibo));
 }
 
 BLI_INLINE void DRW_vbo_request(GPUBatch *batch, GPUVertBuf **vbo)
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index c8a827a993d..3bb188c3fa8 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -93,6 +93,7 @@ set(SRC
   opengl/gl_debug.cc
   opengl/gl_framebuffer.cc
   opengl/gl_immediate.cc
+  opengl/gl_index_buffer.cc
   opengl/gl_shader.cc
   opengl/gl_shader_interface.cc
   opengl/gl_state.cc
@@ -136,6 +137,7 @@ set(SRC
   intern/gpu_drawlist_private.hh
   intern/gpu_framebuffer_private.hh
   intern/gpu_immediate_private.hh
+  intern/gpu_index_buffer_private.hh
   intern/gpu_material_library.h
   intern/gpu_matrix_private.h
   intern/gpu_node_graph.h
@@ -155,6 +157,7 @@ set(SRC
   opengl/gl_drawlist.hh
   opengl/gl_framebuffer.hh
   opengl/gl_immediate.hh
+  opengl/gl_index_buffer.hh
   opengl/gl_primitive.hh
   opengl/gl_shader.hh
   opengl/gl_shader_interface.hh
diff --git a/source/blender/gpu/GPU_index_buffer.h b/source/blender/gpu/GPU_index_buffer.h
index b966eabefae..09c12c6e177 100644
--- a/source/blender/gpu/GPU_index_buffer.h
+++ b/source/blender/gpu/GPU_index_buffer.h
@@ -31,40 +31,21 @@
 extern "C" {
 #endif
 
-#define GPU_TRACK_INDEX_RANGE 1
-
-typedef enum {
-  GPU_INDEX_U16,
-  GPU_INDEX_U32,
-} GPUIndexBufType;
-
-typedef struct GPUIndexBuf {
-  uint index_start;
-  uint index_len;
-  bool is_subrange;
-#if GPU_TRACK_INDEX_RANGE
-  GPUIndexBufType index_type;
-  uint32_t gl_index_type;
-  uint base_index;
-#endif
-  uint32_t ibo_id; /* 0 indicates not yet sent to VRAM */
-  union {
-    void *data;              /* non-NULL indicates not yet sent to VRAM */
-    struct GPUIndexBuf *src; /* if is_subrange is true, this is the source buffer. */
-  };
-} GPUIndexBuf;
+/**
+ * IMPORTANT: Do not allocate manually as the real struct is bigger (i.e: GLIndexBuf). This is only
+ * the common and "public" part of the struct. Use the provided allocator.
+ * TODO(fclem) Make the content of this struct hidden and expose getters/setters.
+ **/
+typedef struct GPUIndexBuf GPUIndexBuf;
 
 GPUIndexBuf *GPU_indexbuf_calloc(void);
 
-void GPU_indexbuf_use(GPUIndexBuf *);
-uint GPU_indexbuf_size_get(const GPUIndexBuf *);
-
 typedef struct GPUIndexBufBuilder {
   uint max_allowed_index;
   uint max_index_len;
   uint index_len;
   GPUPrimType prim_type;
-  uint *data;
+  uint32_t *data;
 } GPUIndexBufBuilder;
 
 /* supports all primitive types. */
@@ -102,6 +83,8 @@ void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
 
 void GPU_indexbuf_discard(GPUIndexBuf *);
 
+bool GPU_indexbuf_is_init(GPUIndexBuf *ibo);
+
 int GPU_indexbuf_primitive_len(GPUPrimType prim_type);
 
 /* Macros */
diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh
index 330c7d59b6d..b5162fd820f 100644
--- a/source/blender/gpu/intern/gpu_backend.hh
+++ b/source/blender/gpu/intern/gpu_backend.hh
@@ -33,6 +33,7 @@ namespace gpu {
 class Batch;
 class DrawList;
 class FrameBuffer;
+class IndexBuf;
 class Shader;
 class Texture;
 class UniformBuf;
@@ -50,6 +51,7 @@ class GPUBackend {
   virtual Batch *batch_alloc(void) = 0;
   virtual DrawList *drawlist_alloc(int list_length) = 0;
   virtual FrameBuffer *framebuffer_alloc(const char *name) = 0;
+  virtual IndexBuf *indexbuf_alloc(void) = 0;
   virtual Shader *shader_alloc(const char *name) = 0;
   virtual Texture *texture_alloc(const char *name) = 0;
   virtual UniformBuf *uniformbuf_alloc(int size, const char *name) = 0;
diff --git a/source/blender/gpu/intern/gpu_batch.cc b/source/blender/gpu/intern/gpu_batch.cc
index 0b0c88a42e2..75419e1a242 100644
--- a/source/blender/gpu/intern/gpu_batch.cc
+++ b/source/blender/gpu/intern/gpu_batch.cc
@@ -38,13 +38,10 @@
 #include "gpu_backend.hh"
 #include "gpu_batch_private.hh"
 #include "gpu_context_private.hh"
+#include "gpu_index_buffer_private.hh"
 #include "gpu_shader_private.hh"
 #include "gpu_vertex_format_private.h"
 
-#include "gl_primitive.hh" /* TODO remove */
-
-#include <limits.h>
-#include <stdlib.h>
 #include <string.h>
 
 using namespace blender::gpu;
@@ -257,12 +254,19 @@ void GPU_batch_draw_instanced(GPUBatch *batch, int i_count)
   GPU_shader_unbind();
 }
 
-void GPU_batch_draw_advanced(GPUBatch *batch, int v_first, int v_count, int i_first, int i_count)
+void GPU_batch_draw_advanced(
+    GPUBatch *gpu_batch, int v_first, int v_count, int i_first, int i_count)
 {
   BLI_assert(GPU_context_active_get()->shader != NULL);
+  Batch *batch = static_cast<Batch *>(gpu_batch);
 
   if (v_count == 0) {
-    v_count = (batch->elem) ? batch->elem->index_len : batch->verts[0]->vertex_len;
+    if (batch->elem) {
+      v_count = batch->elem_()->index_len_get();
+    }
+    else {
+      v_count = batch->verts[0]->vertex_len;
+    }
   }
   if (i_count == 0) {
     i_count = (batch->inst[0]) ? batch->inst[0]->vertex_len : 1;
@@ -277,7 +281,7 @@ void GPU_batch_draw_advanced(GPUBatch *batch, int v_first, int v_count, int i_fi
     return;
   }
 
-  static_cast<Batch *>(batch)->draw(v_first, v_count, i_first, i_count);
+  batch->draw(v_first, v_count, i_first, i_count);
 }
 
 /** \} */
diff --git a/source/blender/gpu/intern/gpu_batch_private.hh b/source/blender/gpu/intern/gpu_batch_private.hh
index c0444647fe1..dae9c885ee1 100644
--- a/source/blender/gpu/intern/gpu_batch_private.hh
+++ b/source/blender/gpu/intern/gpu_batch_private.hh
@@ -29,6 +29,8 @@
 #include "GPU_batch.h"
 #include "GPU_context.h"
 
+#include "gpu_index_buffer_private.hh"
+
 namespace blender {
 namespace gpu {
 
@@ -42,6 +44,12 @@ class Batch : public GPUBatch {
   virtual ~Batch(){};
 
   virtual void draw(int v_first, int v_count, int i_first, int i_count) = 0;
+
+  /* Convenience casts. */
+  IndexBuf *elem_(void)
+  {
+    return unwrap(elem);
+  };
 };
 
 }  // namespace gpu
diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc
index 832c5dc76b2..5448e6efbe4 100644
--- a/source/blender/gpu/intern/gpu_index_buffer.cc
+++ b/source/blender/gpu/intern/gpu_index_buffer.cc
@@ -25,55 +25,22 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "GPU_glew.h"
-#include "GPU_index_buffer.h"
+#include "BLI_utildefines.h"
 
-#include "gpu_context_private.hh"
+#include "gpu_backend.hh"
 
-#include <stdlib.h>
+#include "gpu_index_buffer_private.hh"
 
 #define KEEP_SINGLE_COPY 1
 
 #define RESTART_INDEX 0xFFFFFFFF
 
-static GLenum convert_index_type_to_gl(GPUIndexBufType type)
-{
-#if GPU_TRACK_INDEX_RANGE
-  return (type == GPU_INDEX_U32) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
-#else
-  return GL_UNSIGNED_INT;
-#endif
-}
-
-uint GPU_indexbuf_size_get(const GPUIndexBuf *elem)
-{
-#if GPU_TRACK_INDEX_RANGE
-  return elem->index_len *
-         ((elem->index_type == GPU_INDEX_U32) ? sizeof(GLuint) : sizeof(GLshort));
-#else
-  return elem->index_len * sizeof(GLuint);
-#endif
-}
+/* -------------------------------------------------------------------- */
+/** \name IndexBufBuilder
+ * \{ */
 
-int GPU_indexbuf_primitive_len(GPUPrimType prim_type)
-{
-  switch (prim_type) {
-    case GPU_PRIM_POINTS:
-      return 1;
-    case GPU_PRIM_LINES:
-      return 2;
-    case GPU_PRIM_TRIS:
-      return 3;
-    case GPU_PRIM_LINES_ADJ:
-      return 4;
-    default:
-      break;
-  }
-#if TRUST_NO_ONE
-  assert(false);
-#endif
-  return -1;
-}
+using namespace blender;
+using namespace blender::gpu;
 
 void GPU_indexbuf_init_ex(GPUIndexBufBuilder *builder,
                           GPUPrimType prim_type,
@@ -237,46 +204,69 @@ void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem)
   }
 }
 
-GPUIndexBuf *GPU_indexbuf_create_subrange(GPUIndexBuf *elem_src, uint start, uint length)
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Creation & Deletion
+ * \{ */
+
+namespace blender::gpu {
+
+IndexBuf::~IndexBuf()
 {
-  GPUIndexBuf *elem = (GPUIndexBuf *)MEM_callocN(sizeof(GPUIndexBuf), "GPUIndexBuf");
-  GPU_indexbuf_create_subrange_in_place(elem, elem_src, start, length);
-  return elem;
+  if (!is_subrange_) {
+    MEM_SAFE_FREE(data_);
+  }
 }
 
-void GPU_indexbuf_create_subrange_in_place(GPUIndexBuf *elem,
-                                           GPUIndexBuf *elem_src,
-                                           uint start,
-                                           uint length)
+void IndexBuf::init(uint indices_len, uint32_t *indices)
 {
-  BLI_assert(elem_src && !elem_src->is_subrange);
-  BLI_assert((length == 0) || (start + length <= elem_src->index_len));
+  is_init_ = true;
+  data_ = indices;
+  index_start_ = 0;
+  index_len_ = indices_len;
+
 #if GPU_TRACK_INDEX_RANGE
-  elem->index_type = elem_src->index_type;
-  elem->gl_index_type = elem_src->gl_index_type;
-  elem->base_index = elem_src->base_index;
+  /* Everything remains 32 bit while building to keep things simple.
+   * Find min/max after, then convert to smallest index type possible. */
+  uint min_index, max_index;
+  uint range = this->index_range(&min_index, &max_index);
+  /* count the primitive restart index. */
+  range += 1;
+
+  if (range <= 0xFFFF) {
+    index_type_ = GPU_INDEX_U16;
+    this->squeeze_indices_short(min_index, max_index);
+  }
 #endif
-  elem->is_subrange = true;
-  elem->src = elem_src;
-  elem->index_start = start;
-  elem->index_len = length;
 }
 
-#if GPU_TRACK_INDEX_RANGE
-/* Everything remains 32 bit while building to keep things simple.
- * Find min/max after, then convert to smallest index type possible. */
+void IndexBuf::init_subrange(IndexBuf *elem_src, uint start, uint length)
+{
+  /* We don't support nested subranges. */
+  BLI_assert(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list