From 300338667099f2805ffb23f105250c58c262b96b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 31 May 2024 11:59:12 +0300 Subject: [PATCH] chunk flags converted to a bitfield --- src/files/WorldRegions.cpp | 2 +- src/frontend/hud.cpp | 2 +- src/graphics/render/ChunksRenderer.cpp | 7 +--- src/graphics/render/WorldRenderer.cpp | 2 +- src/lighting/LightSolver.cpp | 6 +-- src/logic/BlocksController.cpp | 2 +- src/logic/ChunksController.cpp | 16 ++++---- src/voxels/Chunk.cpp | 4 +- src/voxels/Chunk.hpp | 53 +++++--------------------- src/voxels/Chunks.cpp | 14 +++---- src/voxels/ChunksStorage.cpp | 4 +- src/world/World.cpp | 6 +-- 12 files changed, 41 insertions(+), 77 deletions(-) diff --git a/src/files/WorldRegions.cpp b/src/files/WorldRegions.cpp index 55f3b3d2..af3aadd4 100644 --- a/src/files/WorldRegions.cpp +++ b/src/files/WorldRegions.cpp @@ -374,7 +374,7 @@ void WorldRegions::put(Chunk* chunk){ chunk->encode(), CHUNK_DATA_LEN, true); // Writing lights cache - if (doWriteLights && chunk->isLighted()) { + if (doWriteLights && chunk->flags.lighted) { put(chunk->x, chunk->z, REGION_LAYER_LIGHTS, chunk->lightmap.encode(), LIGHTMAP_DATA_LEN, true); } diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 95aa69f7..ba6546f8 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -333,7 +333,7 @@ void Hud::openInventory( if (blockinv == nullptr) { blockinv = level->inventories->createVirtual(blockUI->getSlotsCount()); } - level->chunks->getChunkByVoxel(block.x, block.y, block.z)->setUnsaved(true); + level->chunks->getChunkByVoxel(block.x, block.y, block.z)->flags.unsaved; blockUI->bind(blockinv, content); blockPos = block; currentblockid = level->chunks->get(block.x, block.y, block.z)->id; diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index 7f3f3959..d1011a10 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -56,19 +56,16 @@ ChunksRenderer::~ChunksRenderer() { } std::shared_ptr ChunksRenderer::render(std::shared_ptr chunk, bool important) { - chunk->setModified(false); - + chunk->flags.modified = false; if (important) { auto mesh = renderer->render(chunk.get(), level->chunksStorage.get()); meshes[glm::ivec2(chunk->x, chunk->z)] = mesh; return mesh; } - glm::ivec2 key(chunk->x, chunk->z); if (inwork.find(key) != inwork.end()) { return nullptr; } - inwork[key] = true; threadPool.enqueueJob(chunk); return nullptr; @@ -86,7 +83,7 @@ std::shared_ptr ChunksRenderer::getOrRender(std::shared_ptr chunk, if (found == meshes.end()) { return render(chunk, important); } - if (chunk->isModified()) { + if (chunk->flags.modified) { render(chunk, important); } return found->second; diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index ad7030a2..8d02a750 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -79,7 +79,7 @@ bool WorldRenderer::drawChunk( bool culling ){ auto chunk = level->chunks->chunks[index]; - if (!chunk->isLighted()) { + if (!chunk->flags.lighted) { return false; } float distance = glm::distance( diff --git a/src/lighting/LightSolver.cpp b/src/lighting/LightSolver.cpp index 0656ee53..f9865956 100644 --- a/src/lighting/LightSolver.cpp +++ b/src/lighting/LightSolver.cpp @@ -21,7 +21,7 @@ void LightSolver::add(int x, int y, int z, int emission) { addqueue.push(lightentry {x, y, z, ubyte(emission)}); Chunk* chunk = chunks->getChunkByVoxel(x, y, z); - chunk->setModified(true); + chunk->flags.modified = true; chunk->lightmap.set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, emission); } @@ -67,7 +67,7 @@ void LightSolver::solve(){ if (chunk) { int lx = x - chunk->x * CHUNK_W; int lz = z - chunk->z * CHUNK_D; - chunk->setModified(true); + chunk->flags.modified = true; ubyte light = chunk->lightmap.get(lx,y,lz, channel); if (light != 0 && light == entry.light-1){ @@ -96,7 +96,7 @@ void LightSolver::solve(){ if (chunk) { int lx = x - chunk->x * CHUNK_W; int lz = z - chunk->z * CHUNK_D; - chunk->setModified(true); + chunk->flags.modified = true; ubyte light = chunk->lightmap.get(lx, y, lz, channel); voxel& v = chunk->voxels[vox_index(lx, y, lz)]; diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 8d1cbebb..8a8f58a8 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -132,7 +132,7 @@ void BlocksController::randomTick(int tickid, int parts) { if ((index + tickid) % parts != 0) continue; auto& chunk = chunks->chunks[index]; - if (chunk == nullptr || !chunk->isLighted()) + if (chunk == nullptr || !chunk->flags.lighted) continue; for (int s = 0; s < segments; s++) { for (int i = 0; i < 4; i++) { diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index 1c039a14..56eab88c 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -61,7 +61,7 @@ bool ChunksController::loadVisible(){ int index = z * w + x; auto& chunk = chunks->chunks[index]; if (chunk != nullptr){ - if (chunk->isLoaded() && !chunk->isLighted()) { + if (chunk->flags.loaded && !chunk->flags.lighted) { if (buildLights(chunk)) { return true; } @@ -99,12 +99,12 @@ bool ChunksController::buildLights(std::shared_ptr chunk) { } } if (surrounding == MIN_SURROUNDING) { - bool lightsCache = chunk->isLoadedLights(); + bool lightsCache = chunk->flags.loadedLights; if (!lightsCache) { lighting->buildSkyLight(chunk->x, chunk->z); } lighting->onChunkLoaded(chunk->x, chunk->z, !lightsCache); - chunk->setLighted(true); + chunk->flags.lighted = true; return true; } return false; @@ -114,20 +114,20 @@ void ChunksController::createChunk(int x, int z) { auto chunk = level->chunksStorage->create(x, z); chunks->putChunk(chunk); - if (!chunk->isLoaded()) { + if (!chunk->flags.loaded) { generator->generate( chunk->voxels, x, z, level->getWorld()->getSeed() ); - chunk->setUnsaved(true); + chunk->flags.unsaved = true; } chunk->updateHeights(); - if (!chunk->isLoadedLights()) { + if (!chunk->flags.loadedLights) { Lighting::prebuildSkyLight( chunk.get(), level->content->getIndices() ); } - chunk->setLoaded(true); - chunk->setReady(true); + chunk->flags.loaded = true; + chunk->flags.ready = true; } diff --git a/src/voxels/Chunk.cpp b/src/voxels/Chunk.cpp index 94100e16..18dabec2 100644 --- a/src/voxels/Chunk.cpp +++ b/src/voxels/Chunk.cpp @@ -42,12 +42,12 @@ void Chunk::updateHeights() { void Chunk::addBlockInventory(std::shared_ptr inventory, uint x, uint y, uint z) { inventories[vox_index(x, y, z)] = inventory; - setUnsaved(true); + flags.unsaved = true; } void Chunk::removeBlockInventory(uint x, uint y, uint z) { if (inventories.erase(vox_index(x, y, z))) { - setUnsaved(true); + flags.unsaved = true; } } diff --git a/src/voxels/Chunk.hpp b/src/voxels/Chunk.hpp index 29b4916c..535b6125 100644 --- a/src/voxels/Chunk.hpp +++ b/src/voxels/Chunk.hpp @@ -9,14 +9,6 @@ #include "voxel.hpp" #include "../lighting/Lightmap.hpp" -struct ChunkFlag { - static const int MODIFIED = 0x1; - static const int READY = 0x2; - static const int LOADED = 0x4; - static const int LIGHTED = 0x8; - static const int UNSAVED = 0x10; - static const int LOADED_LIGHTS = 0x20; -}; inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4; class Lightmap; @@ -31,7 +23,14 @@ public: int bottom, top; voxel voxels[CHUNK_VOL] {}; Lightmap lightmap; - int flags = 0; + struct { + bool modified: 1; + bool ready: 1; + bool loaded: 1; + bool lighted: 1; + bool unsaved: 1; + bool loadedLights: 1; + } flags {}; /* Block inventories map where key is index of block in voxels array */ chunk_inventories_map inventories; @@ -45,14 +44,6 @@ public: // unused std::unique_ptr clone() const; - // flags getters/setters below - inline void setFlags(int mask, bool value){ - if (value) - flags |= mask; - else - flags &= ~(mask); - } - /* Creates new block inventory given size @return inventory id or 0 if block does not exists */ void addBlockInventory(std::shared_ptr inventory, @@ -63,35 +54,11 @@ public: /* @return inventory bound to the given block or nullptr */ std::shared_ptr getBlockInventory(uint x, uint y, uint z) const; - inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;} - - inline bool isModified() const {return flags & ChunkFlag::MODIFIED;} - - inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;} - - inline bool isLoaded() const {return flags & ChunkFlag::LOADED;} - - inline bool isLoadedLights() const {return flags & ChunkFlag::LOADED_LIGHTS;} - - inline bool isReady() const {return flags & ChunkFlag::READY;} - - inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);} - - inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);} - inline void setModifiedAndUnsaved() { - setModified(true); - setUnsaved(true); + flags.modified = true; + flags.unsaved = true; } - inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);} - - inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);} - - inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);} - - inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);} - std::unique_ptr encode() const; /** diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 9b1dbc73..b9a63e9e 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -186,14 +186,14 @@ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, blockstate state) else if (id == 0) chunk->updateHeights(); if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz))) - chunk->setModified(true); + chunk->flags.modified = true; if (lz == 0 && (chunk = getChunk(cx+ox, cz+oz-1))) - chunk->setModified(true); + chunk->flags.modified = true; if (lx == CHUNK_W-1 && (chunk = getChunk(cx+ox+1, cz+oz))) - chunk->setModified(true); + chunk->flags.modified = true; if (lz == CHUNK_D-1 && (chunk = getChunk(cx+ox, cz+oz+1))) - chunk->setModified(true); + chunk->flags.modified = true; } voxel* Chunks::rayCast( @@ -497,12 +497,12 @@ void Chunks::saveAndClear(){ for (size_t i = 0; i < volume; i++){ Chunk* chunk = chunks[i].get(); chunks[i] = nullptr; - if (chunk == nullptr || !chunk->isLighted()) + if (chunk == nullptr || !chunk->flags.lighted) continue; - bool lightsUnsaved = !chunk->isLoadedLights() && + bool lightsUnsaved = !chunk->flags.loadedLights && worldFiles->doesWriteLights(); - if (!chunk->isUnsaved() && !lightsUnsaved) + if (!chunk->flags.unsaved && !lightsUnsaved) continue; regions.put(chunk); } diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index fa28f4f6..8a9c97b4 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -61,7 +61,7 @@ std::shared_ptr ChunksStorage::create(int x, int z) { chunk->decode(data.get()); auto invs = regions.fetchInventories(chunk->x, chunk->z); chunk->setBlockInventories(std::move(invs)); - chunk->setLoaded(true); + chunk->flags.loaded = true; for(auto& entry : chunk->inventories) { level->inventories->store(entry.second); } @@ -71,7 +71,7 @@ std::shared_ptr ChunksStorage::create(int x, int z) { auto lights = regions.getLights(chunk->x, chunk->z); if (lights) { chunk->lightmap.set(lights.get()); - chunk->setLoadedLights(true); + chunk->flags.loadedLights = true; } return chunk; } diff --git a/src/world/World.cpp b/src/world/World.cpp index fdd60446..380e2f48 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -57,11 +57,11 @@ void World::write(Level* level) { for (size_t i = 0; i < chunks->volume; i++) { auto chunk = chunks->chunks[i]; - if (chunk == nullptr || !chunk->isLighted()) + if (chunk == nullptr || !chunk->flags.lighted) continue; - bool lightsUnsaved = !chunk->isLoadedLights() && + bool lightsUnsaved = !chunk->flags.loadedLights && settings.debug.doWriteLights.get(); - if (!chunk->isUnsaved() && !lightsUnsaved) + if (!chunk->flags.unsaved && !lightsUnsaved) continue; regions.put(chunk.get()); }