additional optimizations

This commit is contained in:
MihailRis 2024-01-24 19:59:12 +03:00
parent 4ea36f8996
commit 5d931eacc6
4 changed files with 58 additions and 67 deletions

View File

@ -9,22 +9,20 @@
#include "../voxels/Block.h" #include "../voxels/Block.h"
LightSolver::LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel) LightSolver::LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel)
: contentIds(contentIds), chunks(chunks), channel(channel) { : contentIds(contentIds),
chunks(chunks),
channel(channel) {
} }
void LightSolver::add(int x, int y, int z, int emission) { void LightSolver::add(int x, int y, int z, int emission) {
if (emission <= 1) if (emission <= 1)
return; return;
lightentry entry;
entry.x = x;
entry.y = y;
entry.z = z;
entry.light = emission;
addqueue.push(entry);
Chunk* chunk = chunks->getChunkByVoxel(entry.x, entry.y, entry.z); addqueue.push(lightentry {x, y, z, ubyte(emission)});
Chunk* chunk = chunks->getChunkByVoxel(x, y, z);
chunk->setModified(true); chunk->setModified(true);
chunk->lightmap->set(entry.x-chunk->x*CHUNK_W, entry.y, entry.z-chunk->z*CHUNK_D, channel, entry.light); chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, emission);
} }
void LightSolver::add(int x, int y, int z) { void LightSolver::add(int x, int y, int z) {
@ -37,19 +35,12 @@ void LightSolver::remove(int x, int y, int z) {
if (chunk == nullptr) if (chunk == nullptr)
return; return;
int light = chunk->lightmap->get(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel); ubyte light = chunk->lightmap->get(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel);
if (light == 0){ if (light == 0){
return; return;
} }
remqueue.push(lightentry {x, y, z, light});
lightentry entry; chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, 0);
entry.x = x;
entry.y = y;
entry.z = z;
entry.light = light;
remqueue.push(entry);
chunk->lightmap->set(entry.x-chunk->x*CHUNK_W, entry.y, entry.z-chunk->z*CHUNK_D, channel, 0);
} }
void LightSolver::solve(){ void LightSolver::solve(){
@ -66,30 +57,20 @@ void LightSolver::solve(){
const lightentry entry = remqueue.front(); const lightentry entry = remqueue.front();
remqueue.pop(); remqueue.pop();
for (size_t i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
int x = entry.x+coords[i*3+0]; int x = entry.x+coords[i*3+0];
int y = entry.y+coords[i*3+1]; int y = entry.y+coords[i*3+1];
int z = entry.z+coords[i*3+2]; int z = entry.z+coords[i*3+2];
Chunk* chunk = chunks->getChunkByVoxel(x,y,z); Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
if (chunk) { if (chunk) {
chunk->setModified(true); chunk->setModified(true);
int light = chunks->getLight(x,y,z, channel); ubyte light = chunks->getLight(x,y,z, channel);
if (light != 0 && light == entry.light-1){ if (light != 0 && light == entry.light-1){
lightentry nentry; remqueue.push(lightentry {x, y, z, light});
nentry.x = x;
nentry.y = y;
nentry.z = z;
nentry.light = light;
remqueue.push(nentry);
chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, 0); chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, 0);
} }
else if (light >= entry.light){ else if (light >= entry.light){
lightentry nentry; addqueue.push(lightentry {x, y, z, light});
nentry.x = x;
nentry.y = y;
nentry.z = z;
nentry.light = light;
addqueue.push(nentry);
} }
} }
} }
@ -100,27 +81,22 @@ void LightSolver::solve(){
const lightentry entry = addqueue.front(); const lightentry entry = addqueue.front();
addqueue.pop(); addqueue.pop();
if (entry.light <= 1) for (int i = 0; i < 6; i++) {
continue;
for (size_t i = 0; i < 6; i++) {
int x = entry.x+coords[i*3+0]; int x = entry.x+coords[i*3+0];
int y = entry.y+coords[i*3+1]; int y = entry.y+coords[i*3+1];
int z = entry.z+coords[i*3+2]; int z = entry.z+coords[i*3+2];
Chunk* chunk = chunks->getChunkByVoxel(x,y,z); Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
if (chunk) { if (chunk) {
chunk->setModified(true); chunk->setModified(true);
int light = chunks->getLight(x,y,z, channel); int light = chunk->lightmap->get(
x - chunk->x * CHUNK_W, y,
z - chunk->z * CHUNK_D,
channel);
voxel* v = chunks->get(x,y,z); voxel* v = chunks->get(x,y,z);
const Block* block = blockDefs[v->id]; const Block* block = blockDefs[v->id];
if (block->lightPassing && light+2 <= entry.light){ if (block->lightPassing && light+2 <= entry.light){
chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, entry.light-1); chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, entry.light-1);
lightentry nentry; addqueue.push(lightentry {x, y, z, ubyte(entry.light-1)});
nentry.x = x;
nentry.y = y;
nentry.z = z;
nentry.light = entry.light-1;
addqueue.push(nentry);
} }
} }
} }

View File

@ -114,18 +114,32 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){
} }
if (expand) { if (expand) {
for (int y = -1; y <= CHUNK_H; y++){ for (int x = 0; x < CHUNK_W; x += CHUNK_W-1) {
for (int z = -1; z <= CHUNK_D; z++){ for (int y = 0; y < CHUNK_H; y++) {
for (int x = -1; x <= CHUNK_W; x++){ for (int z = 0; z < CHUNK_D; z++) {
if (!(x == -1 || x == CHUNK_W || z == -1 || z == CHUNK_D))
continue;
int gx = x + cx * CHUNK_W; int gx = x + cx * CHUNK_W;
int gz = z + cz * CHUNK_D; int gz = z + cz * CHUNK_D;
if (chunks->getLight(x,y,z)){ int rgbs = chunk->lightmap->get(x, y, z);
solverR->add(gx,y,gz); if (rgbs){
solverG->add(gx,y,gz); solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverB->add(gx,y,gz); solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverS->add(gx,y,gz); solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3));
}
}
}
}
for (int z = 0; z < CHUNK_D; z += CHUNK_D-1) {
for (int y = 0; y < CHUNK_H; y++) {
for (int x = 0; x < CHUNK_W; x++) {
int gx = x + cx * CHUNK_W;
int gz = z + cz * CHUNK_D;
int rgbs = chunk->lightmap->get(x, y, z);
if (rgbs){
solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3));
} }
} }
} }

View File

@ -30,7 +30,6 @@ ChunksController::ChunksController(Level* level, uint padding)
} }
ChunksController::~ChunksController(){ ChunksController::~ChunksController(){
delete generator;
} }
void ChunksController::update(int64_t maxDuration) { void ChunksController::update(int64_t maxDuration) {
@ -73,6 +72,7 @@ bool ChunksController::loadVisible(){
} }
chunk->surrounding = surrounding; chunk->surrounding = surrounding;
if (surrounding == MIN_SURROUNDING && !chunk->isLighted()) { if (surrounding == MIN_SURROUNDING && !chunk->isLighted()) {
timeutil::ScopeLogTimer log(555);
bool lightsCache = chunk->isLoadedLights(); bool lightsCache = chunk->isLoadedLights();
if (!lightsCache) { if (!lightsCache) {
lighting->buildSkyLight(chunk->x, chunk->z); lighting->buildSkyLight(chunk->x, chunk->z);

View File

@ -1,6 +1,7 @@
#ifndef VOXELS_CHUNKSCONTROLLER_H_ #ifndef VOXELS_CHUNKSCONTROLLER_H_
#define VOXELS_CHUNKSCONTROLLER_H_ #define VOXELS_CHUNKSCONTROLLER_H_
#include <memory>
#include "../typedefs.h" #include "../typedefs.h"
class Level; class Level;
@ -15,7 +16,7 @@ private:
Chunks* chunks; Chunks* chunks;
Lighting* lighting; Lighting* lighting;
uint padding; uint padding;
WorldGenerator* generator; std::unique_ptr<WorldGenerator> generator;
/* Average measured microseconds duration of loadVisible call */ /* Average measured microseconds duration of loadVisible call */
int64_t avgDurationMcs = 1000; int64_t avgDurationMcs = 1000;