diff --git a/ext/native/gfx_es2/draw_buffer.cpp b/ext/native/gfx_es2/draw_buffer.cpp index ee333ed3bc..527fee59d5 100644 --- a/ext/native/gfx_es2/draw_buffer.cpp +++ b/ext/native/gfx_es2/draw_buffer.cpp @@ -19,8 +19,6 @@ enum { MAX_VERTS = 65536, }; -// #define USE_VBO - DrawBuffer::DrawBuffer() : count_(0), atlas(0) { verts_ = new Vertex[MAX_VERTS]; fontscalex = 1.0f; @@ -37,11 +35,6 @@ void DrawBuffer::Init(Thin3DContext *t3d) { return; t3d_ = t3d; -#ifdef USE_VBO - vbuf_ = t3d_->CreateBuffer(MAX_VERTS * sizeof(Vertex), T3DBufferUsage::DYNAMIC | T3DBufferUsage::VERTEXDATA); -#else - vbuf_ = nullptr; -#endif inited_ = true; std::vector components; @@ -52,6 +45,11 @@ void DrawBuffer::Init(Thin3DContext *t3d) { Thin3DShader *vshader = t3d_->GetVshaderPreset(VS_TEXTURE_COLOR_2D); vformat_ = t3d_->CreateVertexFormat(components, 24, vshader); + if (vformat_->RequiresBuffer()) { + vbuf_ = t3d_->CreateBuffer(MAX_VERTS * sizeof(Vertex), T3DBufferUsage::DYNAMIC | T3DBufferUsage::VERTEXDATA); + } else { + vbuf_ = nullptr; + } } void DrawBuffer::Shutdown() { @@ -83,13 +81,14 @@ void DrawBuffer::Flush(bool set_blend_state) { return; shaderSet_->SetMatrix4x4("WorldViewProj", drawMatrix_); -#ifdef USE_VBO - vbuf_->SubData((const uint8_t *)verts_, 0, sizeof(Vertex) * count_); - int offset = 0; - t3d_->Draw(mode_ == DBMODE_NORMAL ? PRIM_TRIANGLES : PRIM_LINES, shaderSet_, vformat_, vbuf_, count_, offset); -#else - t3d_->DrawUP(mode_ == DBMODE_NORMAL ? PRIM_TRIANGLES : PRIM_LINES, shaderSet_, vformat_, (const void *)verts_, count_); -#endif + + if (vbuf_) { + vbuf_->SubData((const uint8_t *)verts_, 0, sizeof(Vertex) * count_); + int offset = 0; + t3d_->Draw(mode_ == DBMODE_NORMAL ? PRIM_TRIANGLES : PRIM_LINES, shaderSet_, vformat_, vbuf_, count_, offset); + } else { + t3d_->DrawUP(mode_ == DBMODE_NORMAL ? PRIM_TRIANGLES : PRIM_LINES, shaderSet_, vformat_, (const void *)verts_, count_); + } count_ = 0; } diff --git a/ext/native/gfx_es2/gpu_features.cpp b/ext/native/gfx_es2/gpu_features.cpp index 80c55a9957..c2312fc5ff 100644 --- a/ext/native/gfx_es2/gpu_features.cpp +++ b/ext/native/gfx_es2/gpu_features.cpp @@ -262,6 +262,7 @@ void CheckGLExtensions() { gl_extensions.OES_copy_image = strstr(extString, "GL_OES_copy_image") != 0; gl_extensions.EXT_copy_image = strstr(extString, "GL_EXT_copy_image") != 0; gl_extensions.ARB_copy_image = strstr(extString, "GL_ARB_copy_image") != 0; + gl_extensions.ARB_vertex_array_object = strstr(extString, "GL_ARB_vertex_array_object") != 0; if (gl_extensions.IsGLES) { gl_extensions.OES_texture_npot = strstr(extString, "OES_texture_npot") != 0; diff --git a/ext/native/gfx_es2/gpu_features.h b/ext/native/gfx_es2/gpu_features.h index f0421a64f7..9c19579629 100644 --- a/ext/native/gfx_es2/gpu_features.h +++ b/ext/native/gfx_es2/gpu_features.h @@ -54,6 +54,7 @@ struct GLExtensions { bool ARB_shader_image_load_store; bool ARB_conservative_depth; bool ARB_copy_image; + bool ARB_vertex_array_object; // EXT bool EXT_swap_control_tear; diff --git a/ext/native/thin3d/thin3d.h b/ext/native/thin3d/thin3d.h index 3b7d3a17d0..38d1bea866 100644 --- a/ext/native/thin3d/thin3d.h +++ b/ext/native/thin3d/thin3d.h @@ -251,6 +251,7 @@ struct Thin3DVertexComponent { class Thin3DVertexFormat : public Thin3DObject { public: + virtual bool RequiresBuffer() = 0; }; class Thin3DShader : public Thin3DObject { diff --git a/ext/native/thin3d/thin3d_d3d9.cpp b/ext/native/thin3d/thin3d_d3d9.cpp index ea18919e3f..559ac65b51 100644 --- a/ext/native/thin3d/thin3d_d3d9.cpp +++ b/ext/native/thin3d/thin3d_d3d9.cpp @@ -178,6 +178,9 @@ public: void Apply(LPDIRECT3DDEVICE9 device) { device->SetVertexDeclaration(decl_); } + bool RequiresBuffer() override { + return false; + } private: LPDIRECT3DVERTEXDECLARATION9 decl_; int stride_; diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index 08f6383ed5..1b29a59f1a 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -240,13 +240,21 @@ bool Thin3DGLShader::Compile(const char *source) { class Thin3DGLVertexFormat : public Thin3DVertexFormat { public: + ~Thin3DGLVertexFormat(); + void Apply(const void *base = nullptr); void Unapply(); void Compile(); + bool RequiresBuffer() override { + return id_ != 0; + } std::vector components_; int semanticsMask_; // Fast way to check what semantics to enable/disable. int stride_; + GLuint id_; + bool needsEnable_; + intptr_t lastBase_; }; struct UniformInfo { @@ -543,6 +551,12 @@ void Thin3DGLTexture::Finalize(int zim_flags) { } +Thin3DGLVertexFormat::~Thin3DGLVertexFormat() { + if (id_) { + glDeleteVertexArrays(1, &id_); + } +} + void Thin3DGLVertexFormat::Compile() { int sem = 0; for (int i = 0; i < (int)components_.size(); i++) { @@ -550,6 +564,14 @@ void Thin3DGLVertexFormat::Compile() { } semanticsMask_ = sem; // TODO : Compute stride as well? + + if (gl_extensions.ARB_vertex_array_object) { + glGenVertexArrays(1, &id_); + } else { + id_ = 0; + } + needsEnable_ = true; + lastBase_ = -1; } Thin3DDepthStencilState *Thin3DGLContext::CreateDepthStencilState(bool depthTestEnabled, bool depthWriteEnabled, T3DComparison depthCompare) { @@ -753,9 +775,10 @@ void Thin3DGLContext::DrawIndexed(T3DPrimitive prim, Thin3DShaderSet *shaderSet, Thin3DGLVertexFormat *fmt = static_cast(format); vbuf->Bind(); - ibuf->Bind(); fmt->Apply(); ss->Apply(); + // Note: ibuf binding is stored in the VAO, so call this after binding the fmt. + ibuf->Bind(); glDrawElements(primToGL[prim], offset, GL_INT, 0); @@ -804,36 +827,51 @@ Thin3DContext *T3DCreateGLContext() { } void Thin3DGLVertexFormat::Apply(const void *base) { - for (int i = 0; i < SEM_MAX; i++) { - if (semanticsMask_ & (1 << i)) { - glEnableVertexAttribArray(i); - } + if (id_ != 0) { + glBindVertexArray(id_); } - intptr_t b = (intptr_t)base; - for (size_t i = 0; i < components_.size(); i++) { - switch (components_[i].type) { - case FLOATx2: - glVertexAttribPointer(components_[i].semantic, 2, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); - break; - case FLOATx3: - glVertexAttribPointer(components_[i].semantic, 3, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); - break; - case FLOATx4: - glVertexAttribPointer(components_[i].semantic, 4, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); - break; - case UNORM8x4: - glVertexAttribPointer(components_[i].semantic, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride_, (void *)(b + (intptr_t)components_[i].offset)); - break; - case INVALID: - ELOG("Thin3DGLVertexFormat: Invalid component type applied."); + + if (needsEnable_ || id_ == 0) { + for (int i = 0; i < SEM_MAX; i++) { + if (semanticsMask_ & (1 << i)) { + glEnableVertexAttribArray(i); + } } + needsEnable_ = false; + } + + intptr_t b = (intptr_t)base; + if (b != lastBase_) { + for (size_t i = 0; i < components_.size(); i++) { + switch (components_[i].type) { + case FLOATx2: + glVertexAttribPointer(components_[i].semantic, 2, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); + break; + case FLOATx3: + glVertexAttribPointer(components_[i].semantic, 3, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); + break; + case FLOATx4: + glVertexAttribPointer(components_[i].semantic, 4, GL_FLOAT, GL_FALSE, stride_, (void *)(b + (intptr_t)components_[i].offset)); + break; + case UNORM8x4: + glVertexAttribPointer(components_[i].semantic, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride_, (void *)(b + (intptr_t)components_[i].offset)); + break; + case INVALID: + ELOG("Thin3DGLVertexFormat: Invalid component type applied."); + } + } + lastBase_ = b; } } void Thin3DGLVertexFormat::Unapply() { - for (int i = 0; i < SEM_MAX; i++) { - if (semanticsMask_ & (1 << i)) { - glDisableVertexAttribArray(i); + if (id_ == 0) { + for (int i = 0; i < SEM_MAX; i++) { + if (semanticsMask_ & (1 << i)) { + glDisableVertexAttribArray(i); + } } + } else { + glBindVertexArray(0); } }