free chunks from memory

pull/1/head
EmaMaker 2023-05-20 21:51:48 +02:00
parent e225babb0c
commit 0acb8c7c1e
2 changed files with 41 additions and 27 deletions

View File

@ -104,6 +104,20 @@ namespace chunkmanager
a.release();
}
Chunk::Chunk* n;
nUnloaded = 0;
while(chunks_todelete.try_pop(n)){
int x = static_cast<uint16_t>(n->getPosition().x);
int y = static_cast<uint16_t>(n->getPosition().y);
int z = static_cast<uint16_t>(n->getPosition().z);
if(x > 1023 || y > 1023 || z > 1023) continue;
const uint32_t index = calculateIndex(x, y, z);
delete n;
chunks.erase(index);
nUnloaded++;
}
}
}

View File

@ -55,14 +55,6 @@ namespace renderer{
theShader->use();
theShader->setVec3("viewPos", cameraPos);
for(auto& n : render_todelete){
// we can get away with unsafe erase as access to the container is only done by this
// thread
n->deleteBuffers();
chunks_torender.unsafe_erase(n);
}
render_todelete.clear();
chunkmesher::MeshData* m;
while(MeshDataQueue.try_pop(m)){
chunkmesher::sendtogpu(m);
@ -73,25 +65,7 @@ namespace renderer{
if(! (c->getState(Chunk::CHUNK_STATE_MESHED))) continue;
float dist = glm::distance(c->getPosition(), cameraChunkPos);
if(dist >= static_cast<float>(RENDER_DISTANCE)*1.75f){
// When the chunk is outside render distance
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
// If chunk was already out and enough time has passed
// Mark the chunk to be unloaded
// And mark is to be removed from the render set
render_todelete.push_back(c);
chunkmanager::getDeleteVector().push(c);
}
} else{
// Mark has out of vision and annotate when it started
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
c->unload_timer = glfwGetTime();
}
}else if(dist <= static_cast<float>(RENDER_DISTANCE)){
if(dist <= static_cast<float>(RENDER_DISTANCE)){
if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue;
// reset out-of-vision and unload flags
@ -133,8 +107,34 @@ namespace renderer{
glBindVertexArray(0);
}
}
}else{
// When the chunk is outside render distance
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
// If chunk was already out and enough time has passed
// Mark the chunk to be unloaded
// And mark is to be removed from the render set
render_todelete.push_back(c);
}
} else{
// Mark has out of vision and annotate when it started
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
c->unload_timer = glfwGetTime();
}
}
}
for(auto& c : render_todelete){
// we can get away with unsafe erase as access to the container is only done by this
// thread
c->deleteBuffers();
chunks_torender.unsafe_erase(c);
chunkmanager::getDeleteVector().push(c);
}
render_todelete.clear();
}
void destroy(){