From 088d09cb64ef731fb79af354ec7088b4fa0b77f9 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 14 Oct 2017 14:42:54 +0800 Subject: [PATCH] Don't hardcode first vbo size --- src/graphics/cpu_particle_manager.cpp | 24 +++++++++++------------- src/graphics/cpu_particle_manager.hpp | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/graphics/cpu_particle_manager.cpp b/src/graphics/cpu_particle_manager.cpp index 1daedb1ce..8f9847959 100644 --- a/src/graphics/cpu_particle_manager.cpp +++ b/src/graphics/cpu_particle_manager.cpp @@ -138,17 +138,19 @@ void CPUParticleManager::uploadAll() continue; } bool first_upload = false; + unsigned vbo_size = (unsigned)(m_particles_generated[p.first].size()); if (m_gl_particles.find(p.first) == m_gl_particles.end()) { first_upload = true; - m_gl_particles[p.first] = std::make_tuple(0, 0, 100); + m_gl_particles[p.first] = std::make_tuple(0, 0, vbo_size * 2); } if (first_upload) { glGenBuffers(1, &std::get<1>(m_gl_particles[p.first])); glBindBuffer(GL_ARRAY_BUFFER, std::get<1>(m_gl_particles[p.first])); - glBufferData(GL_ARRAY_BUFFER, 2000, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vbo_size * 2 * 20, NULL, + GL_DYNAMIC_DRAW); glGenVertexArrays(1, &std::get<0>(m_gl_particles[p.first])); glBindVertexArray(std::get<0>(m_gl_particles[p.first])); glBindBuffer(GL_ARRAY_BUFFER, @@ -190,20 +192,16 @@ void CPUParticleManager::uploadAll() } glBindBuffer(GL_ARRAY_BUFFER, std::get<1>(m_gl_particles[p.first])); // Check "real" particle buffer size in opengl - if (std::get<2>(m_gl_particles[p.first]) < - m_particles_generated[p.first].size()) + if (std::get<2>(m_gl_particles[p.first]) < vbo_size) { - std::get<2>(m_gl_particles[p.first]) = - m_particles_generated[p.first].size() * 2; - glBufferData(GL_ARRAY_BUFFER, - m_particles_generated[p.first].size() * 2 * 20, NULL, + std::get<2>(m_gl_particles[p.first]) = vbo_size * 2; + glBufferData(GL_ARRAY_BUFFER, vbo_size * 2 * 20, NULL, GL_DYNAMIC_DRAW); } - void* ptr = glMapBufferRange(GL_ARRAY_BUFFER, 0, - m_particles_generated[p.first].size() * 20, GL_MAP_WRITE_BIT | - GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); - memcpy(ptr, m_particles_generated[p.first].data(), - m_particles_generated[p.first].size() * 20); + void* ptr = glMapBufferRange(GL_ARRAY_BUFFER, 0, vbo_size * 20, + GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | + GL_MAP_INVALIDATE_BUFFER_BIT); + memcpy(ptr, m_particles_generated[p.first].data(), vbo_size * 20); glUnmapBuffer(GL_ARRAY_BUFFER); glBindBuffer(GL_ARRAY_BUFFER, 0); } diff --git a/src/graphics/cpu_particle_manager.hpp b/src/graphics/cpu_particle_manager.hpp index 24e5d4cc2..155548dad 100644 --- a/src/graphics/cpu_particle_manager.hpp +++ b/src/graphics/cpu_particle_manager.hpp @@ -70,7 +70,7 @@ private: m_particles_generated; std::unordered_map > m_gl_particles; + GLuint/*VBO*/, unsigned/*VBO size*/> > m_gl_particles; std::unordered_map m_material_map;