More efficient chunk loaders handling

This commit is contained in:
MihailRis 2022-03-06 03:15:50 +03:00 committed by GitHub
parent 35f8ebe79c
commit f0a74dd821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -310,7 +310,10 @@ int main() {
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
chunksController._buildMeshes(&renderer, frame);
chunksController.loadVisible(wfile);
int freeLoaders = chunksController.countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
chunksController.loadVisible(wfile);
draw_world(camera, assets, chunks, occlusion);

View File

@ -24,6 +24,18 @@ ChunksController::~ChunksController(){
delete[] loaders;
}
int ChunksController::countFreeLoaders(){
if (_totalLoaded < 4)
return loaders[0]->isBusy() ? 0 : 1;
int count = 0;
for (int i = 0; i < LOADERS_COUNT; i++){
if (!loaders[i]->isBusy())
count++;
}
return count;
}
bool ChunksController::loadVisible(WorldFiles* worldFiles){
const int w = chunks->w;
const int h = chunks->h;
@ -61,16 +73,10 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
if (chunk != nullptr)
return false;
static int _totalLoaded = 0;
ChunksLoader* freeLoader = nullptr;
for (int i = 0; i < LOADERS_COUNT; i++){
ChunksLoader* loader = loaders[i];
if (loader->isBusy()){
// Use only one loader at start to prevent near chunks lighting artifacts
if (_totalLoaded < 4){
break;
}
continue;
}
freeLoader = loader;
@ -187,7 +193,6 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
}
mesh = renderer->render(chunk, (const Chunk**)closes);
chunks->meshes[index] = mesh;
//std::cout << "2: built mesh for " << chunk << std::endl;
return true;
}
return false;