Sky lights data saving (optimization)

This commit is contained in:
MihailRis 2023-12-06 16:15:44 +03:00
parent 6b423b441f
commit a102821076
12 changed files with 147 additions and 38 deletions

View File

@ -13,6 +13,7 @@
#include "../typedefs.h" #include "../typedefs.h"
#include "../maths/voxmaths.h" #include "../maths/voxmaths.h"
#include "../world/World.h" #include "../world/World.h"
#include "../lighting/Lightmap.h"
#include "../util/data_io.h" #include "../util/data_io.h"
#include "../coders/json.h" #include "../coders/json.h"
@ -87,8 +88,10 @@ uint WorldRegion::getSize(uint x, uint z) {
return sizes[z * REGION_SIZE + x]; return sizes[z * REGION_SIZE + x];
} }
WorldFiles::WorldFiles(path directory, bool generatorTestMode) WorldFiles::WorldFiles(path directory, const DebugSettings& settings)
: directory(directory), generatorTestMode(generatorTestMode) { : directory(directory),
generatorTestMode(settings.generatorTestMode),
doWriteLights(settings.doWriteLights) {
compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2]; compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2];
} }
@ -108,6 +111,17 @@ WorldRegion* WorldFiles::getRegion(unordered_map<ivec2, WorldRegion*>& regions,
return found->second; return found->second;
} }
WorldRegion* WorldFiles::getOrCreateRegion(
unordered_map<ivec2, WorldRegion*>& regions,
int x, int z) {
WorldRegion* region = getRegion(regions, x, z);
if (region == nullptr) {
region = new WorldRegion();
regions[ivec2(x, z)] = region;
}
return region;
}
ubyte* WorldFiles::compress(ubyte* src, size_t srclen, size_t& len) { ubyte* WorldFiles::compress(ubyte* src, size_t srclen, size_t& len) {
len = extrle::encode(src, srclen, compressionBuffer); len = extrle::encode(src, srclen, compressionBuffer);
ubyte* data = new ubyte[len]; ubyte* data = new ubyte[len];
@ -128,30 +142,38 @@ void WorldFiles::put(Chunk* chunk){
int regionX = floordiv(chunk->x, REGION_SIZE); int regionX = floordiv(chunk->x, REGION_SIZE);
int regionZ = floordiv(chunk->z, REGION_SIZE); int regionZ = floordiv(chunk->z, REGION_SIZE);
WorldRegion* region = getRegion(regions, regionX, regionZ);
if (region == nullptr) {
region = new WorldRegion();
regions[ivec2(regionX, regionZ)] = region;
}
region->setUnsaved(true);
int localX = chunk->x - (regionX * REGION_SIZE); int localX = chunk->x - (regionX * REGION_SIZE);
int localZ = chunk->z - (regionZ * REGION_SIZE); int localZ = chunk->z - (regionZ * REGION_SIZE);
/* Writing Voxels */ {
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
region->setUnsaved(true);
unique_ptr<ubyte[]> chunk_data (chunk->encode()); unique_ptr<ubyte[]> chunk_data (chunk->encode());
size_t compressedSize; size_t compressedSize;
ubyte* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize); ubyte* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize);
region->put(localX, localZ, data, compressedSize); region->put(localX, localZ, data, compressedSize);
} }
if (doWriteLights) {
WorldRegion* region = getOrCreateRegion(lights, regionX, regionZ);
region->setUnsaved(true);
unique_ptr<ubyte[]> light_data (chunk->lightmap->encode());
size_t compressedSize;
ubyte* data = compress(light_data.get(), LIGHTMAP_DATA_LEN, compressedSize);
region->put(localX, localZ, data, compressedSize);
}
}
path WorldFiles::getRegionsFolder() const { path WorldFiles::getRegionsFolder() const {
return directory/path("regions"); return directory/path("regions");
} }
path WorldFiles::getRegionFile(int x, int y) const { path WorldFiles::getLightsFolder() const {
return directory/path("lights");
}
path WorldFiles::getRegionFilename(int x, int y) const {
string filename = std::to_string(x) + "_" + std::to_string(y) + ".bin"; string filename = std::to_string(x) + "_" + std::to_string(y) + ".bin";
return getRegionsFolder()/path(filename); return path(filename);
} }
path WorldFiles::getPlayerFile() const { path WorldFiles::getPlayerFile() const {
@ -175,6 +197,19 @@ path WorldFiles::getOldWorldFile() const {
} }
ubyte* WorldFiles::getChunk(int x, int z){ ubyte* WorldFiles::getChunk(int x, int z){
return getData(regions, getRegionsFolder(), x, z);
}
light_t* WorldFiles::getLights(int x, int z) {
ubyte* data = getData(lights, getLightsFolder(), x, z);
if (data == nullptr)
return nullptr;
return Lightmap::decode(data);
}
ubyte* WorldFiles::getData(unordered_map<ivec2, WorldRegion*>& regions,
const path& folder,
int x, int z) {
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(x, REGION_SIZE);
int regionZ = floordiv(z, REGION_SIZE); int regionZ = floordiv(z, REGION_SIZE);
@ -190,7 +225,8 @@ ubyte* WorldFiles::getChunk(int x, int z){
ubyte* data = region->get(localX, localZ); ubyte* data = region->get(localX, localZ);
if (data == nullptr) { if (data == nullptr) {
uint32_t size; uint32_t size;
data = readChunkData(x, z, size, getRegionFile(regionX, regionZ)); data = readChunkData(x, z, size,
folder/getRegionFilename(regionX, regionZ));
if (data) { if (data) {
region->put(localX, localZ, data, size); region->put(localX, localZ, data, size);
} }
@ -278,21 +314,36 @@ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){
} }
} }
void WorldFiles::writeRegions(unordered_map<ivec2, WorldRegion*>& regions,
const path& folder) {
for (auto it : regions){
WorldRegion* region = it.second;
if (region->getChunks() == nullptr || !region->isUnsaved())
continue;
ivec2 key = it.first;
writeRegion(key.x, key.y, region, folder/getRegionFilename(key.x, key.y));
}
}
void WorldFiles::write(const World* world, const Content* content) { void WorldFiles::write(const World* world, const Content* content) {
{
path directory = getRegionsFolder(); path directory = getRegionsFolder();
if (!fs::is_directory(directory)) { if (!fs::is_directory(directory)) {
fs::create_directories(directory); fs::create_directories(directory);
} }
}
{
path directory = getLightsFolder();
if (!fs::is_directory(directory)) {
fs::create_directories(directory);
}
}
writeWorldInfo(world); writeWorldInfo(world);
if (generatorTestMode) if (generatorTestMode)
return; return;
writeIndices(content->indices); writeIndices(content->indices);
for (auto it = regions.begin(); it != regions.end(); it++){ writeRegions(regions, getRegionsFolder());
if (it->second->getChunks() == nullptr || !it->second->isUnsaved()) writeRegions(lights, getLightsFolder());
continue;
ivec2 key = it->first;
writeRegion(key.x, key.y, it->second, getRegionFile(key.x, key.y));
}
} }
void WorldFiles::writeIndices(const ContentIndices* indices) { void WorldFiles::writeIndices(const ContentIndices* indices) {

View File

@ -12,6 +12,7 @@
#include "glm/gtx/hash.hpp" #include "glm/gtx/hash.hpp"
#include "../typedefs.h" #include "../typedefs.h"
#include "../settings.h"
const uint REGION_SIZE_BIT = 5; const uint REGION_SIZE_BIT = 5;
const uint REGION_SIZE = (1 << (REGION_SIZE_BIT)); const uint REGION_SIZE = (1 << (REGION_SIZE_BIT));
@ -30,7 +31,7 @@ class World;
class WorldRegion { class WorldRegion {
ubyte** chunksData; ubyte** chunksData;
uint32_t* sizes; uint32_t* sizes;
bool unsaved = true; bool unsaved = false;
public: public:
WorldRegion(); WorldRegion();
~WorldRegion(); ~WorldRegion();
@ -49,7 +50,8 @@ public:
class WorldFiles { class WorldFiles {
void writeWorldInfo(const World* world); void writeWorldInfo(const World* world);
std::filesystem::path getRegionsFolder() const; std::filesystem::path getRegionsFolder() const;
std::filesystem::path getRegionFile(int x, int y) const; std::filesystem::path getLightsFolder() const;
std::filesystem::path getRegionFilename(int x, int y) const;
std::filesystem::path getPlayerFile() const; std::filesystem::path getPlayerFile() const;
std::filesystem::path getWorldFile() const; std::filesystem::path getWorldFile() const;
std::filesystem::path getIndicesFile() const; std::filesystem::path getIndicesFile() const;
@ -64,6 +66,10 @@ class WorldFiles {
WorldRegion* getRegion(std::unordered_map<glm::ivec2, WorldRegion*>& regions, WorldRegion* getRegion(std::unordered_map<glm::ivec2, WorldRegion*>& regions,
int x, int z); int x, int z);
WorldRegion* getOrCreateRegion(
std::unordered_map<glm::ivec2, WorldRegion*>& regions,
int x, int z);
/* Compress buffer with extrle /* Compress buffer with extrle
@param src source buffer @param src source buffer
@param srclen length of source buffer @param srclen length of source buffer
@ -80,18 +86,27 @@ class WorldFiles {
ubyte* readChunkData(int x, int y, ubyte* readChunkData(int x, int y,
uint32_t& length, uint32_t& length,
std::filesystem::path file); std::filesystem::path file);
void writeRegions(std::unordered_map<glm::ivec2, WorldRegion*>& regions,
const std::filesystem::path& folder);
ubyte* getData(std::unordered_map<glm::ivec2, WorldRegion*>& regions,
const std::filesystem::path& folder,
int x, int z);
public: public:
std::unordered_map<glm::ivec2, WorldRegion*> regions; std::unordered_map<glm::ivec2, WorldRegion*> regions;
std::unordered_map<glm::ivec2, WorldRegion*> lights; std::unordered_map<glm::ivec2, WorldRegion*> lights;
std::filesystem::path directory; std::filesystem::path directory;
ubyte* compressionBuffer; ubyte* compressionBuffer;
bool generatorTestMode; bool generatorTestMode;
bool doWriteLights;
WorldFiles(std::filesystem::path directory, bool generatorTestMode); WorldFiles(std::filesystem::path directory, const DebugSettings& settings);
~WorldFiles(); ~WorldFiles();
void put(Chunk* chunk); void put(Chunk* chunk);
ubyte* getChunk(int x, int y); ubyte* getChunk(int x, int y);
light_t* getLights(int x, int y);
bool readWorldInfo(World* world); bool readWorldInfo(World* world);
bool readPlayer(Player* player); bool readPlayer(Player* player);

View File

@ -38,6 +38,7 @@ toml::Wrapper create_wrapper(EngineSettings& settings) {
toml::Section& debug = wrapper.add("debug"); toml::Section& debug = wrapper.add("debug");
debug.add("generator-test-mode", &settings.debug.generatorTestMode); debug.add("generator-test-mode", &settings.debug.generatorTestMode);
debug.add("show-chunk-borders", &settings.debug.showChunkBorders); debug.add("show-chunk-borders", &settings.debug.showChunkBorders);
debug.add("do-write-lights", &settings.debug.doWriteLights);
return wrapper; return wrapper;
} }

View File

@ -20,12 +20,27 @@ void Lightmap::set(const Lightmap* lightmap) {
} }
} }
void Lightmap::set(light_t* map) {
delete[] this->map;
this->map = map;
}
static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t"); static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t");
ubyte* Lightmap::encode() const { ubyte* Lightmap::encode() const {
ubyte* buffer = new ubyte[CHUNK_VOL * sizeof(light_t)]; ubyte* buffer = new ubyte[LIGHTMAP_DATA_LEN];
for (uint i = 0; i < CHUNK_VOL; i++) { for (uint i = 0; i < CHUNK_VOL; i+=2) {
dataio::write_int16_big(map[i], buffer, i * sizeof(light_t)); buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0);
} }
return buffer; return buffer;
} }
light_t* Lightmap::decode(ubyte* buffer) {
light_t* lights = new light_t[CHUNK_VOL];
for (uint i = 0; i < CHUNK_VOL; i+=2) {
ubyte b = buffer[i/2];
lights[i] = ((b & 0xF) << 12);
lights[i+1] = ((b & 0xF0) << 8);
}
return lights;
}

View File

@ -5,6 +5,8 @@
#include "../typedefs.h" #include "../typedefs.h"
#include "../voxels/Chunk.h" #include "../voxels/Chunk.h"
const int LIGHTMAP_DATA_LEN = CHUNK_VOL/2;
// Lichtkarte // Lichtkarte
class Lightmap { class Lightmap {
public: public:
@ -15,6 +17,8 @@ public:
void set(const Lightmap* lightmap); void set(const Lightmap* lightmap);
void set(light_t* map);
inline unsigned short get(int x, int y, int z){ inline unsigned short get(int x, int y, int z){
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x]); return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x]);
} }
@ -81,6 +85,7 @@ public:
} }
ubyte* encode() const; ubyte* encode() const;
static light_t* decode(ubyte* buffer);
}; };
#endif /* LIGHTING_LIGHTMAP_H_ */ #endif /* LIGHTING_LIGHTMAP_H_ */

View File

@ -79,7 +79,9 @@ bool ChunksController::loadVisible(){
} }
chunk->surrounding = surrounding; chunk->surrounding = surrounding;
if (surrounding == MIN_SURROUNDING && !chunk->isLighted()) { if (surrounding == MIN_SURROUNDING && !chunk->isLighted()) {
if (!chunk->isLoadedLights()) {
lighting->buildSkyLight(chunk->x, chunk->z); lighting->buildSkyLight(chunk->x, chunk->z);
}
lighting->onChunkLoaded(chunk->x, chunk->z); lighting->onChunkLoaded(chunk->x, chunk->z);
chunk->setLighted(true); chunk->setLighted(true);
return true; return true;
@ -123,6 +125,8 @@ bool ChunksController::loadVisible(){
chunk->voxels[i].id = 11; chunk->voxels[i].id = 11;
} }
} }
if (!chunk->isLoadedLights()) {
lighting->prebuildSkyLight(chunk->x, chunk->z); lighting->prebuildSkyLight(chunk->x, chunk->z);
}
return true; return true;
} }

View File

@ -58,6 +58,7 @@ struct DebugSettings {
/* Turns off chunks saving/loading */ /* Turns off chunks saving/loading */
bool generatorTestMode = false; bool generatorTestMode = false;
bool showChunkBorders = false; bool showChunkBorders = false;
bool doWriteLights = true;
}; };
struct EngineSettings { struct EngineSettings {

View File

@ -10,6 +10,7 @@ struct ChunkFlag{
static const int LOADED = 0x4; static const int LOADED = 0x4;
static const int LIGHTED = 0x8; static const int LIGHTED = 0x8;
static const int UNSAVED = 0x10; static const int UNSAVED = 0x10;
static const int LOADED_LIGHTS = 0x20;
}; };
#define CHUNK_DATA_LEN (CHUNK_VOL*2) #define CHUNK_DATA_LEN (CHUNK_VOL*2)
@ -57,6 +58,8 @@ public:
inline bool isLoaded() const {return flags & ChunkFlag::LOADED;} 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 bool isReady() const {return flags & ChunkFlag::READY;}
inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);} inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);}
@ -65,6 +68,8 @@ public:
inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);} 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 setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);}
inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);} inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);}

View File

@ -50,6 +50,12 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
chunk->decode(data.get()); chunk->decode(data.get());
chunk->setLoaded(true); chunk->setLoaded(true);
} }
light_t* lights = level->world->wfile->getLights(chunk->x, chunk->z);
if (lights) {
chunk->lightmap->set(lights);
chunk->setLoadedLights(true);
}
return chunk; return chunk;
} }

View File

@ -21,8 +21,10 @@ World::World(string name,
path directory, path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings) EngineSettings& settings)
: name(name), seed(seed) { : settings(settings),
wfile = new WorldFiles(directory, settings.debug.generatorTestMode); name(name),
seed(seed) {
wfile = new WorldFiles(directory, settings.debug);
} }
World::~World(){ World::~World(){
@ -41,7 +43,10 @@ void World::write(Level* level) {
for (size_t i = 0; i < chunks->volume; i++) { for (size_t i = 0; i < chunks->volume; i++) {
shared_ptr<Chunk> chunk = chunks->chunks[i]; shared_ptr<Chunk> chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isUnsaved()) if (chunk == nullptr)
continue;
bool lightsUnsaved = !chunk->isLoadedLights() && settings.debug.doWriteLights;
if (!chunk->isUnsaved() && !lightsUnsaved)
continue; continue;
wfile->put(chunk.get()); wfile->put(chunk.get());
} }

View File

@ -14,6 +14,7 @@ class Level;
class Player; class Player;
class World { class World {
EngineSettings& settings;
public: public:
std::string name; std::string name;
WorldFiles* wfile; WorldFiles* wfile;