[Bf-blender-cvs] [479de155f42] temp-gpu-compute-shaders: Splitted read and unmap in different calls.

Jeroen Bakker noreply at git.blender.org
Wed May 26 16:38:15 CEST 2021


Commit: 479de155f422786628eb9f712088a8112a7d58af
Author: Jeroen Bakker
Date:   Wed May 26 16:33:54 2021 +0200
Branches: temp-gpu-compute-shaders
https://developer.blender.org/rB479de155f422786628eb9f712088a8112a7d58af

Splitted read and unmap in different calls.

Most of the time you just want to access the data without the overhead
of a local copy.

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

M	source/blender/gpu/GPU_index_buffer.h
M	source/blender/gpu/GPU_vertex_buffer.h
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_vertex_buffer.cc
M	source/blender/gpu/intern/gpu_vertex_buffer_private.hh
M	source/blender/gpu/opengl/gl_index_buffer.cc
M	source/blender/gpu/opengl/gl_vertex_buffer.cc
M	source/blender/gpu/opengl/gl_vertex_buffer.hh
M	source/blender/gpu/tests/gpu_shader_test.cc

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

diff --git a/source/blender/gpu/GPU_index_buffer.h b/source/blender/gpu/GPU_index_buffer.h
index 03e39a5d846..d1e68842563 100644
--- a/source/blender/gpu/GPU_index_buffer.h
+++ b/source/blender/gpu/GPU_index_buffer.h
@@ -80,7 +80,14 @@ 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`.
+ */
 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);
 
diff --git a/source/blender/gpu/GPU_vertex_buffer.h b/source/blender/gpu/GPU_vertex_buffer.h
index b528c024e01..605d660126f 100644
--- a/source/blender/gpu/GPU_vertex_buffer.h
+++ b/source/blender/gpu/GPU_vertex_buffer.h
@@ -71,7 +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`.
+ */
 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 *);
 
diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc
index 3586c512fda..e7009addde1 100644
--- a/source/blender/gpu/intern/gpu_index_buffer.cc
+++ b/source/blender/gpu/intern/gpu_index_buffer.cc
@@ -31,6 +31,8 @@
 
 #include "gpu_index_buffer_private.hh"
 
+#include <cstring>
+
 #define KEEP_SINGLE_COPY 1
 
 #define RESTART_INDEX 0xFFFFFFFF
@@ -324,6 +326,13 @@ void IndexBuf::squeeze_indices_short(uint min_idx, uint max_idx)
   }
 }
 
+  uint32_t *IndexBuf::unmap(const uint32_t* mapped_memory) const {
+    size_t size = size_get();
+    uint32_t *result = static_cast<uint32_t *>(MEM_mallocN(size, __func__));
+    memcpy(result, mapped_memory, size);
+    return result;
+  }
+
 }  // namespace blender::gpu
 
 /** \} */
@@ -373,6 +382,11 @@ uint32_t *GPU_indexbuf_read(GPUIndexBuf *elem)
   return unwrap(elem)->read();
 }
 
+uint32_t *GPU_indexbuf_unmap(const GPUIndexBuf *elem, const uint32_t *mapped_buffer)
+{
+  return unwrap(elem)->unmap(mapped_buffer);
+}
+
 void GPU_indexbuf_discard(GPUIndexBuf *elem)
 {
   delete unwrap(elem);
diff --git a/source/blender/gpu/intern/gpu_index_buffer_private.hh b/source/blender/gpu/intern/gpu_index_buffer_private.hh
index 9d81c338417..07c163b000f 100644
--- a/source/blender/gpu/intern/gpu_index_buffer_private.hh
+++ b/source/blender/gpu/intern/gpu_index_buffer_private.hh
@@ -95,6 +95,7 @@ class IndexBuf {
   virtual void bind_as_ssbo(uint binding) = 0;
 
   virtual uint32_t *read() const = 0;
+  uint32_t *unmap(const uint32_t* mapped_memory) const;
 
  private:
   inline void squeeze_indices_short(uint min_idx, uint max_idx);
@@ -110,6 +111,10 @@ static inline IndexBuf *unwrap(GPUIndexBuf *indexbuf)
 {
   return reinterpret_cast<IndexBuf *>(indexbuf);
 }
+static inline const IndexBuf *unwrap(const GPUIndexBuf *indexbuf)
+{
+  return reinterpret_cast<const IndexBuf *>(indexbuf);
+}
 
 static inline int indices_per_primitive(GPUPrimType prim_type)
 {
diff --git a/source/blender/gpu/intern/gpu_vertex_buffer.cc b/source/blender/gpu/intern/gpu_vertex_buffer.cc
index fa27ccfbed8..6f48ef45645 100644
--- a/source/blender/gpu/intern/gpu_vertex_buffer.cc
+++ b/source/blender/gpu/intern/gpu_vertex_buffer.cc
@@ -154,6 +154,11 @@ void *GPU_vertbuf_read(GPUVertBuf *verts)
   return unwrap(verts)->read();
 }
 
+void *GPU_vertbuf_unmap(const GPUVertBuf *verts, const void *mapped_data)
+{
+  return unwrap(verts)->unmap(mapped_data);
+}
+
 /** Same as discard but does not free. */
 void GPU_vertbuf_clear(GPUVertBuf *verts)
 {
diff --git a/source/blender/gpu/intern/gpu_vertex_buffer_private.hh b/source/blender/gpu/intern/gpu_vertex_buffer_private.hh
index 27e36ae3915..9143d5d601f 100644
--- a/source/blender/gpu/intern/gpu_vertex_buffer_private.hh
+++ b/source/blender/gpu/intern/gpu_vertex_buffer_private.hh
@@ -97,7 +97,8 @@ class VertBuf {
   }
 
   virtual void update_sub(uint start, uint len, void *data) = 0;
-  virtual void *read() = 0;
+  virtual void *read() const = 0;
+  virtual void *unmap(const void* mapped_data) const = 0;
 
  protected:
   virtual void acquire_data(void) = 0;
diff --git a/source/blender/gpu/opengl/gl_index_buffer.cc b/source/blender/gpu/opengl/gl_index_buffer.cc
index e74ea45fd21..ac5d4b490f4 100644
--- a/source/blender/gpu/opengl/gl_index_buffer.cc
+++ b/source/blender/gpu/opengl/gl_index_buffer.cc
@@ -66,10 +66,8 @@ void GLIndexBuf::bind_as_ssbo(uint binding)
 uint32_t *GLIndexBuf::read() const
 {
   BLI_assert(is_active());
-  size_t size = size_get();
-  uint32_t *result = static_cast<uint32_t *>(MEM_mallocN(size, __func__));
   void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
-  memcpy(result, data, size);
+  uint32_t *result = static_cast<uint32_t *>(data);
   return result;
 }
 
diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.cc b/source/blender/gpu/opengl/gl_vertex_buffer.cc
index 477af8342f8..69575c396fb 100644
--- a/source/blender/gpu/opengl/gl_vertex_buffer.cc
+++ b/source/blender/gpu/opengl/gl_vertex_buffer.cc
@@ -123,12 +123,16 @@ void GLVertBuf::bind_as_ssbo(uint binding)
   glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, vbo_id_);
 }
 
-void *GLVertBuf::read()
+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__);
-  void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
-  memcpy(result, data, vbo_size_);
+  memcpy(result, mapped_data, vbo_size_);
   return result;
 }
 
diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.hh b/source/blender/gpu/opengl/gl_vertex_buffer.hh
index fd7a63db519..5c25078353a 100644
--- a/source/blender/gpu/opengl/gl_vertex_buffer.hh
+++ b/source/blender/gpu/opengl/gl_vertex_buffer.hh
@@ -47,7 +47,8 @@ class GLVertBuf : public VertBuf {
 
   void update_sub(uint start, uint len, void *data) override;
 
-  void *read() override;
+  void *read() const override;
+  void *unmap(const void* mapped_data) const override;
 
  protected:
   void acquire_data(void) override;
diff --git a/source/blender/gpu/tests/gpu_shader_test.cc b/source/blender/gpu/tests/gpu_shader_test.cc
index 75787e1e83f..9ba6cce60e3 100644
--- a/source/blender/gpu/tests/gpu_shader_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_test.cc
@@ -195,7 +195,6 @@ void main() {
     EXPECT_FLOAT_EQ(data[index * 4 + 2], expected_value);
     EXPECT_FLOAT_EQ(data[index * 4 + 3], expected_value);
   }
-  MEM_freeN(data);
 
   /* Cleanup. */
   GPU_shader_unbind();
@@ -253,7 +252,6 @@ void main() {
     uint32_t expected = index;
     EXPECT_EQ(data[index], expected);
   }
-  MEM_freeN(data);
 
   /* Cleanup. */
   GPU_shader_unbind();



More information about the Bf-blender-cvs mailing list