From bb2aeb4504907cab1cf8c4afc4dd1d6495c940e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Sun, 6 Sep 2020 23:45:51 +0200 Subject: GPUVertBuf: Rename GPUVertBuf to VertBuf and add some getters to avoid more typecasts. --- source/blender/gpu/opengl/gl_backend.hh | 6 ++ source/blender/gpu/opengl/gl_batch.cc | 4 +- source/blender/gpu/opengl/gl_batch.hh | 11 ++- source/blender/gpu/opengl/gl_drawlist.cc | 6 +- source/blender/gpu/opengl/gl_shader.cc | 6 +- source/blender/gpu/opengl/gl_texture.cc | 5 +- source/blender/gpu/opengl/gl_vertex_array.cc | 14 ++-- source/blender/gpu/opengl/gl_vertex_buffer.cc | 56 +++++++++++++++ source/blender/gpu/opengl/gl_vertex_buffer.hh | 98 +++++++++++++++++++++++++++ 9 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 source/blender/gpu/opengl/gl_vertex_buffer.cc create mode 100644 source/blender/gpu/opengl/gl_vertex_buffer.hh (limited to 'source/blender/gpu/opengl') diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh index 80fadb2fc0a..c178aa537a0 100644 --- a/source/blender/gpu/opengl/gl_backend.hh +++ b/source/blender/gpu/opengl/gl_backend.hh @@ -35,6 +35,7 @@ #include "gl_shader.hh" #include "gl_texture.hh" #include "gl_uniform_buffer.hh" +#include "gl_vertex_buffer.hh" namespace blender { namespace gpu { @@ -103,6 +104,11 @@ class GLBackend : public GPUBackend { return new GLUniformBuf(size, name); }; + VertBuf *vertbuf_alloc(void) override + { + return new GLVertBuf(); + }; + /* TODO remove */ void buf_free(GLuint buf_id); void tex_free(GLuint tex_id); diff --git a/source/blender/gpu/opengl/gl_batch.cc b/source/blender/gpu/opengl/gl_batch.cc index c735d02500b..db30a57953d 100644 --- a/source/blender/gpu/opengl/gl_batch.cc +++ b/source/blender/gpu/opengl/gl_batch.cc @@ -309,7 +309,7 @@ void GLBatch::bind(int i_first) #if GPU_TRACK_INDEX_RANGE /* Can be removed if GL 4.3 is required. */ if (!GLEW_ARB_ES3_compatibility && (elem != NULL)) { - glPrimitiveRestartIndex(this->gl_elem()->restart_index()); + glPrimitiveRestartIndex(this->elem_()->restart_index()); } #endif @@ -334,7 +334,7 @@ void GLBatch::draw(int v_first, int v_count, int i_first, int i_count) GLenum gl_type = to_gl(prim_type); if (elem) { - const GLIndexBuf *el = this->gl_elem(); + const GLIndexBuf *el = this->elem_(); GLenum index_type = to_gl(el->index_type_); GLint base_index = el->index_base_; void *v_first_ofs = el->offset_ptr(v_first); diff --git a/source/blender/gpu/opengl/gl_batch.hh b/source/blender/gpu/opengl/gl_batch.hh index e6d9542fc3b..0fadde7a70d 100644 --- a/source/blender/gpu/opengl/gl_batch.hh +++ b/source/blender/gpu/opengl/gl_batch.hh @@ -31,6 +31,7 @@ #include "gpu_batch_private.hh" #include "gl_index_buffer.hh" +#include "gl_vertex_buffer.hh" #include "glew-mx.h" @@ -102,10 +103,18 @@ class GLBatch : public Batch { void bind(int i_first); /* Convenience getters. */ - GLIndexBuf *gl_elem(void) + GLIndexBuf *elem_(void) const { return static_cast(unwrap(elem)); } + GLVertBuf *verts_(const int index) const + { + return static_cast(unwrap(verts[index])); + } + GLVertBuf *inst_(const int index) const + { + return static_cast(unwrap(inst[index])); + } MEM_CXX_CLASS_ALLOC_FUNCS("GLBatch"); }; diff --git a/source/blender/gpu/opengl/gl_drawlist.cc b/source/blender/gpu/opengl/gl_drawlist.cc index de729487226..d8c17084457 100644 --- a/source/blender/gpu/opengl/gl_drawlist.cc +++ b/source/blender/gpu/opengl/gl_drawlist.cc @@ -139,10 +139,10 @@ void GLDrawList::append(GPUBatch *gpu_batch, int i_first, int i_count) this->submit(); batch_ = batch; /* Cached for faster access. */ - GLIndexBuf *el = batch_->gl_elem(); + GLIndexBuf *el = batch_->elem_(); base_index_ = el ? el->index_base_ : UINT_MAX; v_first_ = el ? el->index_start_ : 0; - v_count_ = el ? el->index_len_ : batch->verts[0]->vertex_len; + v_count_ = el ? el->index_len_ : batch->verts_(0)->vertex_len; } if (v_count_ == 0) { @@ -202,7 +202,7 @@ void GLDrawList::submit(void) batch_->bind(0); if (MDI_INDEXED) { - GLenum gl_type = to_gl(batch_->gl_elem()->index_type_); + GLenum gl_type = to_gl(batch_->elem_()->index_type_); glMultiDrawElementsIndirect(prim, gl_type, offset, command_len_, 0); } else { diff --git a/source/blender/gpu/opengl/gl_shader.cc b/source/blender/gpu/opengl/gl_shader.cc index 17058a6a5a7..1d74400bc4c 100644 --- a/source/blender/gpu/opengl/gl_shader.cc +++ b/source/blender/gpu/opengl/gl_shader.cc @@ -28,6 +28,8 @@ #include "GPU_extensions.h" #include "GPU_platform.h" +#include "gl_vertex_buffer.hh" + #include "gl_shader.hh" #include "gl_shader_interface.hh" @@ -260,12 +262,14 @@ void GLShader::transform_feedback_names_set(Span name_list, transform_feedback_type_ = geom_type; } -bool GLShader::transform_feedback_enable(GPUVertBuf *buf) +bool GLShader::transform_feedback_enable(GPUVertBuf *buf_) { if (transform_feedback_type_ == GPU_SHADER_TFB_NONE) { return false; } + GLVertBuf *buf = static_cast(unwrap(buf_)); + BLI_assert(buf->vbo_id != 0); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buf->vbo_id); diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index f1779601e33..6151a2fc73e 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -111,6 +111,7 @@ bool GLTexture::init_internal(void) /* Return true on success. */ bool GLTexture::init_internal(GPUVertBuf *vbo) { + GLVertBuf *gl_vbo = static_cast(unwrap(vbo)); target_ = to_gl_target(type_); /* We need to bind once to define the texture type. */ @@ -119,10 +120,10 @@ bool GLTexture::init_internal(GPUVertBuf *vbo) GLenum internal_format = to_gl_internal_format(format_); if (GLEW_ARB_direct_state_access) { - glTextureBuffer(tex_id_, internal_format, vbo->vbo_id); + glTextureBuffer(tex_id_, internal_format, gl_vbo->vbo_id); } else { - glTexBuffer(target_, internal_format, vbo->vbo_id); + glTexBuffer(target_, internal_format, gl_vbo->vbo_id); } #ifndef __APPLE__ diff --git a/source/blender/gpu/opengl/gl_vertex_array.cc b/source/blender/gpu/opengl/gl_vertex_array.cc index bfd3bd6e6cc..463dc18cc60 100644 --- a/source/blender/gpu/opengl/gl_vertex_array.cc +++ b/source/blender/gpu/opengl/gl_vertex_array.cc @@ -28,6 +28,7 @@ #include "gl_batch.hh" #include "gl_context.hh" #include "gl_index_buffer.hh" +#include "gl_vertex_buffer.hh" #include "gl_vertex_array.hh" @@ -62,7 +63,7 @@ static uint16_t vbo_bind(const ShaderInterface *interface, } const GLvoid *pointer = (const GLubyte *)0 + offset + v_first * stride; - const GLenum type = convert_comp_type_to_gl(static_cast(a->comp_type)); + const GLenum type = to_gl(static_cast(a->comp_type)); for (uint n_idx = 0; n_idx < a->name_len; n_idx++) { const char *name = GPU_vertformat_attr_name_get(format, a, n_idx); @@ -108,27 +109,28 @@ static uint16_t vbo_bind(const ShaderInterface *interface, /* Update the Attrib Binding of the currently bound VAO. */ void GLVertArray::update_bindings(const GLuint vao, - const GPUBatch *batch, + const GPUBatch *batch_, /* Should be GLBatch. */ const ShaderInterface *interface, const int base_instance) { + const GLBatch *batch = static_cast(batch_); uint16_t attr_mask = interface->enabled_attr_mask_; glBindVertexArray(vao); /* Reverse order so first VBO'S have more prevalence (in term of attribute override). */ for (int v = GPU_BATCH_VBO_MAX_LEN - 1; v > -1; v--) { - GPUVertBuf *vbo = batch->verts[v]; + GLVertBuf *vbo = batch->verts_(v); if (vbo) { - GPU_vertbuf_use(vbo); + GPU_vertbuf_use(batch->verts[v]); attr_mask &= ~vbo_bind(interface, &vbo->format, 0, vbo->vertex_len, false); } } for (int v = GPU_BATCH_INST_VBO_MAX_LEN - 1; v > -1; v--) { - GPUVertBuf *vbo = batch->inst[v]; + GLVertBuf *vbo = batch->inst_(v); if (vbo) { - GPU_vertbuf_use(vbo); + GPU_vertbuf_use(batch->inst[v]); attr_mask &= ~vbo_bind(interface, &vbo->format, base_instance, vbo->vertex_len, true); } } diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.cc b/source/blender/gpu/opengl/gl_vertex_buffer.cc new file mode 100644 index 00000000000..ee9e0a0b0c9 --- /dev/null +++ b/source/blender/gpu/opengl/gl_vertex_buffer.cc @@ -0,0 +1,56 @@ +/* + * 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. + * + * The Original Code is Copyright (C) 2016 by Mike Erwin. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#include "gl_backend.hh" + +#include "gl_vertex_buffer.hh" + +namespace blender::gpu { + +void GLVertBuf::bind(void) +{ +} + +void GLVertBuf::upload_data(void) +{ +} + +uchar *GLVertBuf::acquire_data(void) +{ + return nullptr; +} + +uchar *GLVertBuf::resize_data(void) +{ + return nullptr; +} + +void GLVertBuf::release_data(void) +{ +} + +void GLVertBuf::duplicate_data(VertBuf *) +{ +} + +} // namespace blender::gpu \ No newline at end of file diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.hh b/source/blender/gpu/opengl/gl_vertex_buffer.hh new file mode 100644 index 00000000000..76c5e86a1c7 --- /dev/null +++ b/source/blender/gpu/opengl/gl_vertex_buffer.hh @@ -0,0 +1,98 @@ +/* + * 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. + * + * The Original Code is Copyright (C) 2020 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#pragma once + +#include "MEM_guardedalloc.h" + +#include "glew-mx.h" + +#include "gpu_vertex_buffer_private.hh" + +namespace blender { +namespace gpu { + +class GLVertBuf : public VertBuf { + friend class GLTexture; /* For buffer texture. */ + friend class GLShader; /* For transform feedback. */ + + private: + // GLuint vbo_id_ = 0; + /** Size on the GPU. */ + // size_t vbo_size_ = 0; + + public: + void bind(void); + + protected: + uchar *acquire_data(void); + uchar *resize_data(void); + void release_data(void); + void upload_data(void); + void duplicate_data(VertBuf *dst); + + MEM_CXX_CLASS_ALLOC_FUNCS("GLVertBuf"); +}; + +static inline GLenum to_gl(GPUUsageType type) +{ + switch (type) { + case GPU_USAGE_STREAM: + return GL_STREAM_DRAW; + case GPU_USAGE_DYNAMIC: + return GL_DYNAMIC_DRAW; + case GPU_USAGE_STATIC: + return GL_STATIC_DRAW; + default: + BLI_assert(0); + return GL_STATIC_DRAW; + } +} + +static inline GLenum to_gl(GPUVertCompType type) +{ + switch (type) { + case GPU_COMP_I8: + return GL_BYTE; + case GPU_COMP_U8: + return GL_UNSIGNED_BYTE; + case GPU_COMP_I16: + return GL_SHORT; + case GPU_COMP_U16: + return GL_UNSIGNED_SHORT; + case GPU_COMP_I32: + return GL_INT; + case GPU_COMP_U32: + return GL_UNSIGNED_INT; + case GPU_COMP_F32: + return GL_FLOAT; + case GPU_COMP_I10: + return GL_INT_2_10_10_10_REV; + default: + BLI_assert(0); + return GL_FLOAT; + } +} + +} // namespace gpu +} // namespace blender -- cgit v1.2.3