[Bf-blender-cvs] [04b7c12af25] temp-gpu-compute-shaders: Added support for device only index buffers.
Jeroen Bakker
noreply at git.blender.org
Fri May 7 15:09:17 CEST 2021
Commit: 04b7c12af25f6bf87b1b543c61c2c93217670d85
Author: Jeroen Bakker
Date: Fri May 7 15:08:38 2021 +0200
Branches: temp-gpu-compute-shaders
https://developer.blender.org/rB04b7c12af25f6bf87b1b543c61c2c93217670d85
Added support for device only index buffers.
===================================================================
M source/blender/gpu/GPU_index_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/opengl/gl_index_buffer.cc
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 41861ff6fd5..7746db1092e 100644
--- a/source/blender/gpu/GPU_index_buffer.h
+++ b/source/blender/gpu/GPU_index_buffer.h
@@ -44,8 +44,17 @@ typedef struct GPUIndexBufBuilder {
uint32_t *data;
} GPUIndexBufBuilder;
+typedef enum {
+ GPU_INDEX_U16,
+ GPU_INDEX_U32,
+} GPUIndexBufType;
+
/* supports all primitive types. */
void GPU_indexbuf_init_ex(GPUIndexBufBuilder *, GPUPrimType, uint index_len, uint vertex_len);
+void GPU_indexbuf_init_device_only(GPUIndexBuf *elem,
+ GPUIndexBufType type,
+ GPUPrimType,
+ uint prim_len);
/* supports only GPU_PRIM_POINTS, GPU_PRIM_LINES and GPU_PRIM_TRIS. */
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len);
diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc
index c41deb3b3d8..b13aa1a9741 100644
--- a/source/blender/gpu/intern/gpu_index_buffer.cc
+++ b/source/blender/gpu/intern/gpu_index_buffer.cc
@@ -66,6 +66,15 @@ void GPU_indexbuf_init(GPUIndexBufBuilder *builder,
GPU_indexbuf_init_ex(builder, prim_type, prim_len * (uint)verts_per_prim, vertex_len);
}
+void GPU_indexbuf_init_device_only(GPUIndexBuf *elem_,
+ GPUIndexBufType index_type,
+ GPUPrimType prim_type,
+ uint prim_len)
+{
+ IndexBuf *elem = unwrap(elem_);
+ elem->init_device_only(index_type, prim_type, prim_len);
+}
+
void GPU_indexbuf_add_generic_vert(GPUIndexBufBuilder *builder, uint v)
{
#if TRUST_NO_ONE
@@ -241,6 +250,15 @@ void IndexBuf::init(uint indices_len, uint32_t *indices)
#endif
}
+void IndexBuf::init_device_only(GPUIndexBufType index_type, GPUPrimType prim_type, uint prim_len)
+{
+ is_init_ = true;
+ index_start_ = 0;
+ index_len_ = prim_len * indices_per_primitive(prim_type);
+ index_type_ = index_type;
+ data_ = nullptr;
+}
+
void IndexBuf::init_subrange(IndexBuf *elem_src, uint start, uint length)
{
/* We don't support nested subranges. */
diff --git a/source/blender/gpu/intern/gpu_index_buffer_private.hh b/source/blender/gpu/intern/gpu_index_buffer_private.hh
index 1916c5a5256..b65d3e8122d 100644
--- a/source/blender/gpu/intern/gpu_index_buffer_private.hh
+++ b/source/blender/gpu/intern/gpu_index_buffer_private.hh
@@ -31,11 +31,6 @@
namespace blender::gpu {
-typedef enum {
- GPU_INDEX_U16,
- GPU_INDEX_U32,
-} GPUIndexBufType;
-
static inline size_t to_bytesize(GPUIndexBufType type)
{
return (type == GPU_INDEX_U32) ? sizeof(uint32_t) : sizeof(uint16_t);
@@ -75,6 +70,7 @@ class IndexBuf {
void init(uint indices_len, uint32_t *indices);
void init_subrange(IndexBuf *elem_src, uint start, uint length);
+ void init_device_only(GPUIndexBufType index_type, GPUPrimType prim_type, uint prim_len);
uint32_t index_len_get(void) const
{
diff --git a/source/blender/gpu/opengl/gl_index_buffer.cc b/source/blender/gpu/opengl/gl_index_buffer.cc
index b607ec8d55a..308fec40038 100644
--- a/source/blender/gpu/opengl/gl_index_buffer.cc
+++ b/source/blender/gpu/opengl/gl_index_buffer.cc
@@ -40,17 +40,14 @@ void GLIndexBuf::bind()
return;
}
- if (ibo_id_ == 0) {
+ const bool allocate_on_device = ibo_id_ == 0;
+ if (allocate_on_device) {
glGenBuffers(1, &ibo_id_);
-
- if (data_ == nullptr) {
- debug::raise_gl_error("Trying to use Index Buffer but the buffer contains no data");
- }
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_);
- if (data_ != nullptr) {
+ if (data_ != nullptr || allocate_on_device) {
size_t size = this->size_get();
/* Sends data to GPU. */
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data_, GL_STATIC_DRAW);
diff --git a/source/blender/gpu/tests/gpu_shader_test.cc b/source/blender/gpu/tests/gpu_shader_test.cc
index 5a50a4c8b0e..c3bbb8a72b3 100644
--- a/source/blender/gpu/tests/gpu_shader_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_test.cc
@@ -244,14 +244,8 @@ void main() {
GPU_shader_bind(shader);
/* Construct IBO. */
- GPUIndexBufBuilder ibo_builder;
- const int max_vertex_index = 65536;
- GPU_indexbuf_init(&ibo_builder, GPU_PRIM_POINTS, SIZE, max_vertex_index);
- for (int index = 0; index < SIZE; index++) {
- GPU_indexbuf_set_point_vert(&ibo_builder, index, 57005);
- }
- GPUIndexBuf *ibo = GPU_indexbuf_build(&ibo_builder);
-
+ GPUIndexBuf *ibo = GPU_indexbuf_calloc();
+ GPU_indexbuf_init_device_only(ibo, GPU_INDEX_U16, GPU_PRIM_POINTS, SIZE);
GPU_indexbuf_bind_as_ssbo(ibo, GPU_shader_get_ssbo(shader, "outputIboData"));
/* Dispatch compute task. */
@@ -310,13 +304,8 @@ void main() {
GPU_shader_bind(shader);
/* Construct IBO. */
- GPUIndexBufBuilder ibo_builder;
- GPU_indexbuf_init(&ibo_builder, GPU_PRIM_POINTS, SIZE, 0);
- for (int index = 0; index < SIZE; index++) {
- GPU_indexbuf_set_point_vert(&ibo_builder, index, 65536 * index);
- }
- GPUIndexBuf *ibo = GPU_indexbuf_build(&ibo_builder);
-
+ GPUIndexBuf *ibo = GPU_indexbuf_calloc();
+ GPU_indexbuf_init_device_only(ibo, GPU_INDEX_U32, GPU_PRIM_POINTS, SIZE);
GPU_indexbuf_bind_as_ssbo(ibo, GPU_shader_get_ssbo(shader, "outputIboData"));
/* Dispatch compute task. */
More information about the Bf-blender-cvs
mailing list