From ec59e3f573981d38d24acb579849ba6269a6e493 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 29 Dec 2017 23:42:45 +0800 Subject: [PATCH] Don't delete vao each frame when update vertex buffer --- src/graphics/sp/sp_dynamic_draw_call.cpp | 96 +++++++++++------------- src/graphics/sp/sp_dynamic_draw_call.hpp | 27 ++++--- 2 files changed, 55 insertions(+), 68 deletions(-) diff --git a/src/graphics/sp/sp_dynamic_draw_call.cpp b/src/graphics/sp/sp_dynamic_draw_call.cpp index d53dd6343..21482af6a 100644 --- a/src/graphics/sp/sp_dynamic_draw_call.cpp +++ b/src/graphics/sp/sp_dynamic_draw_call.cpp @@ -62,59 +62,7 @@ SPDynamicDrawCall::SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt, SPTextureManager::get()->addGLCommandFunction (std::bind(&SPDynamicDrawCall::initTextureDyDc, this)); glBindBuffer(GL_ARRAY_BUFFER, 0); - recreateDynamicVAO(); -#endif -} // SPDynamicDrawCall -// ---------------------------------------------------------------------------- -bool SPDynamicDrawCall::initTextureDyDc() -{ -#ifndef SERVER_ONLY - for (unsigned i = 0; i < m_stk_material.size(); i++) - { - for (unsigned j = 0; j < 6; j++) - { - if (!m_textures[i][j]->initialized()) - { - return false; - } - } - } - if (!(CVS->useArrayTextures() || CVS->isARBBindlessTextureUsable())) - { - return true; - } - glBindBuffer(GL_ARRAY_BUFFER, m_ibo); - if (CVS->useArrayTextures()) - { - for (unsigned i = 0; i < 6; i++) - { - uint16_t array_index = (uint16_t) - m_textures[0][i]->getTextureArrayIndex(); - glBufferSubData(GL_ARRAY_BUFFER, 32 + (i * 2), 2, &array_index); - } - } - else - { - for (unsigned i = 0; i < 6; i++) - { - glBufferSubData(GL_ARRAY_BUFFER, 32 + (i * 8), 8, - m_textures[0][i]->getTextureHandlePointer()); - } - } - glBindBuffer(GL_ARRAY_BUFFER, 0); -#endif - return true; -} // initTextureDyDc - -// ---------------------------------------------------------------------------- -void SPDynamicDrawCall::recreateDynamicVAO() -{ -#ifndef SERVER_ONLY - if (m_vao[0] != 0) - { - glDeleteVertexArrays(1, &m_vao[0]); - } glGenVertexArrays(1, &m_vao[0]); glBindVertexArray(m_vao[0]); @@ -220,8 +168,48 @@ void SPDynamicDrawCall::recreateDynamicVAO() glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); - #endif -} // recreateVAO +} // SPDynamicDrawCall + +// ---------------------------------------------------------------------------- +bool SPDynamicDrawCall::initTextureDyDc() +{ +#ifndef SERVER_ONLY + for (unsigned i = 0; i < m_stk_material.size(); i++) + { + for (unsigned j = 0; j < 6; j++) + { + if (!m_textures[i][j]->initialized()) + { + return false; + } + } + } + if (!(CVS->useArrayTextures() || CVS->isARBBindlessTextureUsable())) + { + return true; + } + glBindBuffer(GL_ARRAY_BUFFER, m_ibo); + if (CVS->useArrayTextures()) + { + for (unsigned i = 0; i < 6; i++) + { + uint16_t array_index = (uint16_t) + m_textures[0][i]->getTextureArrayIndex(); + glBufferSubData(GL_ARRAY_BUFFER, 32 + (i * 2), 2, &array_index); + } + } + else + { + for (unsigned i = 0; i < 6; i++) + { + glBufferSubData(GL_ARRAY_BUFFER, 32 + (i * 8), 8, + m_textures[0][i]->getTextureHandlePointer()); + } + } + glBindBuffer(GL_ARRAY_BUFFER, 0); +#endif + return true; +} // initTextureDyDc } \ No newline at end of file diff --git a/src/graphics/sp/sp_dynamic_draw_call.hpp b/src/graphics/sp/sp_dynamic_draw_call.hpp index 27514c2b6..b0fcf48c3 100644 --- a/src/graphics/sp/sp_dynamic_draw_call.hpp +++ b/src/graphics/sp/sp_dynamic_draw_call.hpp @@ -61,8 +61,6 @@ private: // ------------------------------------------------------------------------ bool initTextureDyDc(); - // ------------------------------------------------------------------------ - void recreateDynamicVAO(); public: SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt, SPShader* shader, @@ -93,28 +91,29 @@ public: m_texture_trans.X, m_texture_trans.Y, 0.0f, 0); glBindBuffer(GL_ARRAY_BUFFER, m_ibo); glBufferSubData(GL_ARRAY_BUFFER, 0, 32, &id); + glBindBuffer(GL_ARRAY_BUFFER, 0); } if (m_update_offset >= 0 && !m_vertices.empty()) { + glBindBuffer(GL_ARRAY_BUFFER, m_vbo); if ((unsigned)m_vertices.capacity() > m_gl_vbo_size) { m_update_offset = 0; - glDeleteBuffers(1, &m_vbo); - glGenBuffers(1, &m_vbo); - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); m_gl_vbo_size = (unsigned)m_vertices.capacity(); - glBufferData(GL_ARRAY_BUFFER, m_gl_vbo_size * 48, NULL, - GL_DYNAMIC_DRAW); - recreateDynamicVAO(); + glBufferData(GL_ARRAY_BUFFER, m_gl_vbo_size * 48, + m_vertices.data(), GL_DYNAMIC_DRAW); } - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - const int length = ((int)m_vertices.size() - m_update_offset) * 48; - assert(length > 0); - glBufferSubData(GL_ARRAY_BUFFER, m_update_offset * 48, length, - m_vertices.data() + m_update_offset); + else + { + const int length = + ((int)m_vertices.size() - m_update_offset) * 48; + assert(length > 0); + glBufferSubData(GL_ARRAY_BUFFER, m_update_offset * 48, length, + m_vertices.data() + m_update_offset); + } + glBindBuffer(GL_ARRAY_BUFFER, 0); m_update_offset = -1; } - glBindBuffer(GL_ARRAY_BUFFER, 0); #endif } // ------------------------------------------------------------------------