refactor: made renderer clean buffer

This commit is contained in:
JuiceyDev 2026-04-04 00:16:08 +02:00
parent e7f4dfd79e
commit 57641ebcca
3 changed files with 20 additions and 22 deletions

View file

@ -434,6 +434,7 @@ void LevelRenderer::setLevel(int playerIndex, MultiPlayerLevel* level) {
// actually exiting the game, so only when the primary player sets there // actually exiting the game, so only when the primary player sets there
// level to nullptr // level to nullptr
if (playerIndex == InputManager.GetPrimaryPad()) { if (playerIndex == InputManager.GetPrimaryPad()) {
RenderManager.CBuffDeleteAll();
{ {
std::lock_guard<std::mutex> lock(m_csRenderableTileEntities); std::lock_guard<std::mutex> lock(m_csRenderableTileEntities);
renderableTileEntities.clear(); renderableTileEntities.clear();

View file

@ -527,7 +527,6 @@ static void pushRenderState() {
static GLuint s_sVAO_std = 0, s_sVBO_std = 0; static GLuint s_sVAO_std = 0, s_sVBO_std = 0;
static GLsizeiptr s_streamVBOSize = 0; static GLsizeiptr s_streamVBOSize = 0;
static bool s_useMapRange = false;
static void bindStdAttribs() { static void bindStdAttribs() {
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@ -903,27 +902,11 @@ void C4JRender::DrawVertices(ePrimitiveType ptype, int count, void* dataIn,
glBindVertexArray(s_sVAO_std); glBindVertexArray(s_sVAO_std);
glBindBuffer(GL_ARRAY_BUFFER, s_sVBO_std); glBindBuffer(GL_ARRAY_BUFFER, s_sVBO_std);
// orphan buffer
if ((GLsizeiptr)bytes != s_streamVBOSize) {
s_streamVBOSize = (GLsizeiptr)bytes;
glBufferData(GL_ARRAY_BUFFER, s_streamVBOSize, nullptr, GL_STREAM_DRAW);
}
if (s_useMapRange) { // Standard orphaning
void* dst = glMapBufferRange( glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)bytes, nullptr, GL_STREAM_DRAW);
GL_ARRAY_BUFFER, 0, (GLsizeiptr)bytes, glBufferSubData(GL_ARRAY_BUFFER, 0, (GLsizeiptr)bytes, dataIn);
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | s_streamVBOSize = (GLsizeiptr)bytes;
GL_MAP_UNSYNCHRONIZED_BIT);
if (dst) {
memcpy(dst, dataIn, bytes);
glUnmapBuffer(GL_ARRAY_BUFFER);
} else {
s_useMapRange = false;
}
}
if (!s_useMapRange) {
glBufferSubData(GL_ARRAY_BUFFER, 0, (GLsizeiptr)bytes, dataIn);
}
glDrawArrays(glMode, 0, count); glDrawArrays(glMode, 0, count);
@ -959,6 +942,16 @@ void C4JRender::CBuffDelete(int first, int count) {
pthread_mutex_unlock(&s_glCallMtx); pthread_mutex_unlock(&s_glCallMtx);
} }
void C4JRender::CBuffDeleteAll() {
pthread_mutex_lock(&s_glCallMtx);
for (auto& kv : s_chunkPool) {
kv.second.destroy();
}
s_chunkPool.clear();
s_nextListBase = 1;
pthread_mutex_unlock(&s_glCallMtx);
}
void C4JRender::CBuffStart(int index, bool) { void C4JRender::CBuffStart(int index, bool) {
s_recListId = index; s_recListId = index;
s_recVerts.clear(); s_recVerts.clear();
@ -987,7 +980,10 @@ void C4JRender::CBuffEnd() {
void C4JRender::CBuffClear(int index) { void C4JRender::CBuffClear(int index) {
pthread_mutex_lock(&s_glCallMtx); pthread_mutex_lock(&s_glCallMtx);
auto it = s_chunkPool.find(index); auto it = s_chunkPool.find(index);
if (it != s_chunkPool.end()) it->second.destroy(); if (it != s_chunkPool.end()) {
it->second.destroy();
s_chunkPool.erase(it);
}
pthread_mutex_unlock(&s_glCallMtx); pthread_mutex_unlock(&s_glCallMtx);
} }

View file

@ -59,6 +59,7 @@ public:
void CBuffLockStaticCreations(); void CBuffLockStaticCreations();
int CBuffCreate(int count); int CBuffCreate(int count);
void CBuffDelete(int first, int count); void CBuffDelete(int first, int count);
void CBuffDeleteAll();
void CBuffStart(int index, bool full = false); void CBuffStart(int index, bool full = false);
void CBuffClear(int index); void CBuffClear(int index);
int CBuffSize(int index); int CBuffSize(int index);