Don't delete vao each frame when update vertex buffer

This commit is contained in:
Benau 2017-12-29 23:42:45 +08:00
parent 63267391ef
commit ec59e3f573
2 changed files with 55 additions and 68 deletions

View File

@ -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
}

View File

@ -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
}
// ------------------------------------------------------------------------