[Bf-blender-cvs] [7948267d8eb] temp-gpu-compute-shaders: Added device only vertex buffer.

Jeroen Bakker noreply at git.blender.org
Wed Apr 28 11:55:15 CEST 2021


Commit: 7948267d8eb6f8eae31249c6939d6d327d3fe74f
Author: Jeroen Bakker
Date:   Wed Apr 28 11:54:08 2021 +0200
Branches: temp-gpu-compute-shaders
https://developer.blender.org/rB7948267d8eb6f8eae31249c6939d6d327d3fe74f

Added device only vertex buffer.

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

M	source/blender/gpu/GPU_vertex_buffer.h
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_vertex_buffer.h b/source/blender/gpu/GPU_vertex_buffer.h
index aae58de533b..01ab252a6c1 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. */
diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.cc b/source/blender/gpu/opengl/gl_vertex_buffer.cc
index a56d5269fde..1a18d1f5f8e 100644
--- a/source/blender/gpu/opengl/gl_vertex_buffer.cc
+++ b/source/blender/gpu/opengl/gl_vertex_buffer.cc
@@ -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());
 }
 
@@ -55,6 +63,8 @@ void GLVertBuf::duplicate_data(VertBuf *dst_)
   BLI_assert(GLContext::get() != nullptr);
   GLVertBuf *src = this;
   GLVertBuf *dst = static_cast<GLVertBuf *>(dst_);
+  /* TODO(jbakker): Device only duplication of vertex buffers not supported yet. */
+  BLI_assert((src->usage_ != GPU_USAGE_DEVICE_ONLY) && (dst->usage_ != GPU_USAGE_DEVICE_ONLY));
 
   if (src->vbo_id_ != 0) {
     dst->vbo_size_ = src->size_used_get();
@@ -94,8 +104,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) {
diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.hh b/source/blender/gpu/opengl/gl_vertex_buffer.hh
index e2bf6cd00e8..61bc7bc00b0 100644
--- a/source/blender/gpu/opengl/gl_vertex_buffer.hh
+++ b/source/blender/gpu/opengl/gl_vertex_buffer.hh
@@ -66,6 +66,8 @@ static inline GLenum to_gl(GPUUsageType type)
       return GL_DYNAMIC_DRAW;
     case GPU_USAGE_STATIC:
       return GL_STATIC_DRAW;
+    case GPU_USAGE_DEVICE_ONLY:
+      return GL_STATIC_DRAW;
     default:
       BLI_assert(0);
       return GL_STATIC_DRAW;
diff --git a/source/blender/gpu/tests/gpu_shader_test.cc b/source/blender/gpu/tests/gpu_shader_test.cc
index 937e13cd4a6..c421acf2fe2 100644
--- a/source/blender/gpu/tests/gpu_shader_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_test.cc
@@ -178,15 +178,8 @@ void main() {
   /* Construct VBO. */
   static GPUVertFormat format = {0};
   GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
-  /* TODO: Should add a new GPU_USAGE_TYPE. (device only) that won't upload the data. */
-  GPUVertBuf *vbo = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_DYNAMIC);
+  GPUVertBuf *vbo = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_DEVICE_ONLY);
   GPU_vertbuf_data_alloc(vbo, SIZE);
-  /* Store vbo with known values for easier debugging. */
-  float dummy[4] = {57005.0f, 57005.0f, 57005.0f, 57005.0f};
-  for (int i = 0; i < SIZE; i++) {
-    GPU_vertbuf_vert_set(vbo, i, dummy);
-  }
-
   GPU_shader_attach_vertex_buffer(shader, vbo, 0);
 
   /* Dispatch compute task. */



More information about the Bf-blender-cvs mailing list