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);
if (UserConfigParams::m_glow && node->hasGlowColor() &&
CVS->isDefferedEnabled())
CVS->isDefferedEnabled() && dc_type == DCT_NORMAL)
{
video::SColorf gc = node->getGlowColor();
unsigned key = gc.toSColor().color;
auto ret = g_glow_meshes.find(key);
@ -1539,7 +1538,7 @@ void updateModelMatrix()
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
(material_id);
texture_names =

View File

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

View File

@ -98,10 +98,16 @@ public:
if (m_update_offset >= 0 && !m_vertices.empty())
{
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_gl_vbo_size = (unsigned)m_vertices.capacity();
m_gl_vbo_size = new_size;
glBufferData(GL_ARRAY_BUFFER, m_gl_vbo_size * 48,
m_vertices.data(), GL_DYNAMIC_DRAW);
}
@ -193,5 +199,3 @@ public:
}
#endif
#include <S3DVertex.h>

View File

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

View File

@ -249,10 +249,6 @@ void SPMeshBuffer::uploadGLMesh()
m_indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
for (unsigned i = 0; i < DCT_FOR_VAO; i++)
{
recreateVAO(i);
}
#endif
} // uploadGLMesh
@ -451,7 +447,9 @@ void SPMeshBuffer::uploadInstanceData()
{
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)
{
// 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++)
{
m_ins_dat_mapped_ptr[i] = NULL;
m_gl_instance_size[i] = 1;
m_gl_instance_size[i] = 0;
m_vao[i] = 0;
m_ins_array[i] = 0;
}
@ -171,7 +171,7 @@ public:
}
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(),
spmb->m_indices.end());
@ -216,7 +216,7 @@ public:
return m_textures[0];
}
// ------------------------------------------------------------------------
std::array<std::shared_ptr<SPTexture>, 6>
const std::array<std::shared_ptr<SPTexture>, 6>&
getSPTexturesByMaterialID(int material_id) const
{
assert((size_t)material_id < m_textures.size());
@ -293,6 +293,12 @@ public:
// ------------------------------------------------------------------------
void reloadTextureCompare();
// ------------------------------------------------------------------------
void shrinkToFit()
{
m_vertices.shrink_to_fit();
m_indices.shrink_to_fit();
}
// ------------------------------------------------------------------------
virtual const video::SMaterial& getMaterial() const
{
static video::SMaterial unused;