Better memory management for vertex buffers

This commit is contained in:
Benau 2018-01-11 11:30:55 +08:00
parent 4823c46ad9
commit 3374ec66e3
6 changed files with 27 additions and 16 deletions

View File

@ -1333,9 +1333,8 @@ void addObject(SPMeshNode* node)
} }
mb->addInstanceData(id, (DrawCallType)dc_type); mb->addInstanceData(id, (DrawCallType)dc_type);
if (UserConfigParams::m_glow && node->hasGlowColor() && if (UserConfigParams::m_glow && node->hasGlowColor() &&
CVS->isDefferedEnabled()) CVS->isDefferedEnabled() && dc_type == DCT_NORMAL)
{ {
video::SColorf gc = node->getGlowColor(); video::SColorf gc = node->getGlowColor();
unsigned key = gc.toSColor().color; unsigned key = gc.toSColor().color;
auto ret = g_glow_meshes.find(key); auto ret = g_glow_meshes.find(key);
@ -1539,7 +1538,7 @@ void updateModelMatrix()
if (material_id != -1) if (material_id != -1)
{ {
std::array<std::shared_ptr<SPTexture>, 6> textures = const std::array<std::shared_ptr<SPTexture>, 6>& textures =
(*(q.second.begin()))->getSPTexturesByMaterialID (*(q.second.begin()))->getSPTexturesByMaterialID
(material_id); (material_id);
texture_names = texture_names =

View File

@ -21,7 +21,6 @@
#include "graphics/graphics_restrictions.hpp" #include "graphics/graphics_restrictions.hpp"
#include "graphics/material.hpp" #include "graphics/material.hpp"
#include "graphics/sp/sp_texture_manager.hpp" #include "graphics/sp/sp_texture_manager.hpp"
#include "race/race_manager.hpp"
#include "utils/mini_glm.hpp" #include "utils/mini_glm.hpp"
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"

View File

@ -98,10 +98,16 @@ public:
if (m_update_offset >= 0 && !m_vertices.empty()) if (m_update_offset >= 0 && !m_vertices.empty())
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
if ((unsigned)m_vertices.capacity() > m_gl_vbo_size) unsigned new_size = m_gl_vbo_size;
while (m_vertices.size() > new_size)
{
// Power of 2 allocation strategy, like std::vector in gcc
new_size <<= 1;
}
if (new_size != m_gl_vbo_size)
{ {
m_update_offset = 0; m_update_offset = 0;
m_gl_vbo_size = (unsigned)m_vertices.capacity(); m_gl_vbo_size = new_size;
glBufferData(GL_ARRAY_BUFFER, m_gl_vbo_size * 48, glBufferData(GL_ARRAY_BUFFER, m_gl_vbo_size * 48,
m_vertices.data(), GL_DYNAMIC_DRAW); m_vertices.data(), GL_DYNAMIC_DRAW);
} }
@ -193,5 +199,3 @@ public:
} }
#endif #endif
#include <S3DVertex.h>

View File

@ -198,6 +198,11 @@ void SPMesh::finalize()
itr++; itr++;
} }
for (unsigned i = 0; i < m_buffer.size(); i++)
{
m_buffer[i]->shrinkToFit();
}
} // finalize } // finalize
} }

View File

@ -249,10 +249,6 @@ void SPMeshBuffer::uploadGLMesh()
m_indices.data(), GL_STATIC_DRAW); m_indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
for (unsigned i = 0; i < DCT_FOR_VAO; i++)
{
recreateVAO(i);
}
#endif #endif
} // uploadGLMesh } // uploadGLMesh
@ -451,7 +447,9 @@ void SPMeshBuffer::uploadInstanceData()
{ {
continue; continue;
} }
unsigned new_size = m_gl_instance_size[i];
unsigned new_size =
m_gl_instance_size[i] == 0 ? 1 : m_gl_instance_size[i];
while (m_ins_dat[i].size() > new_size) while (m_ins_dat[i].size() > new_size)
{ {
// Power of 2 allocation strategy, like std::vector in gcc // Power of 2 allocation strategy, like std::vector in gcc

View File

@ -94,7 +94,7 @@ public:
for (unsigned i = 0; i < DCT_FOR_VAO; i++) for (unsigned i = 0; i < DCT_FOR_VAO; i++)
{ {
m_ins_dat_mapped_ptr[i] = NULL; m_ins_dat_mapped_ptr[i] = NULL;
m_gl_instance_size[i] = 1; m_gl_instance_size[i] = 0;
m_vao[i] = 0; m_vao[i] = 0;
m_ins_array[i] = 0; m_ins_array[i] = 0;
} }
@ -171,7 +171,7 @@ public:
} }
else else
{ {
std::get<1>(m_stk_material[0]) += spmb->m_indices.size(); std::get<1>(m_stk_material[0]) += (unsigned)spmb->m_indices.size();
} }
m_indices.insert(m_indices.end(), spmb->m_indices.begin(), m_indices.insert(m_indices.end(), spmb->m_indices.begin(),
spmb->m_indices.end()); spmb->m_indices.end());
@ -216,7 +216,7 @@ public:
return m_textures[0]; return m_textures[0];
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
std::array<std::shared_ptr<SPTexture>, 6> const std::array<std::shared_ptr<SPTexture>, 6>&
getSPTexturesByMaterialID(int material_id) const getSPTexturesByMaterialID(int material_id) const
{ {
assert((size_t)material_id < m_textures.size()); assert((size_t)material_id < m_textures.size());
@ -293,6 +293,12 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void reloadTextureCompare(); void reloadTextureCompare();
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void shrinkToFit()
{
m_vertices.shrink_to_fit();
m_indices.shrink_to_fit();
}
// ------------------------------------------------------------------------
virtual const video::SMaterial& getMaterial() const virtual const video::SMaterial& getMaterial() const
{ {
static video::SMaterial unused; static video::SMaterial unused;