make chunk lightmap optional

This commit is contained in:
MihailRis
2025-11-23 15:22:18 +03:00
parent 64f7c19766
commit 5250c748be
12 changed files with 103 additions and 73 deletions
+2 -10
View File
@@ -8,7 +8,8 @@
#include "util/data_io.hpp"
#include "voxel.hpp"
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) {
Chunk::Chunk(int xpos, int zpos, std::shared_ptr<Lightmap> lightmap)
: x(xpos), z(zpos), lightmap(std::move(lightmap)) {
bottom = 0;
top = CHUNK_H;
}
@@ -56,15 +57,6 @@ std::shared_ptr<Inventory> Chunk::getBlockInventory(uint x, uint y, uint z)
return found->second;
}
std::unique_ptr<Chunk> Chunk::clone() const {
auto other = std::make_unique<Chunk>(x, z);
for (uint i = 0; i < CHUNK_VOL; i++) {
other->voxels[i] = voxels[i];
}
other->lightmap.set(&lightmap);
return other;
}
/**
Current chunk format:
- byte-order: little-endian
+2 -5
View File
@@ -27,7 +27,7 @@ public:
int x, z;
int bottom, top;
voxel voxels[CHUNK_VOL] {};
Lightmap lightmap;
std::shared_ptr<Lightmap> lightmap;
struct {
bool modified : 1;
bool ready : 1;
@@ -45,14 +45,11 @@ public:
/// @brief Blocks metadata heap
BlocksMetadata blocksMetadata;
Chunk(int x, int z);
Chunk(int x, int z, std::shared_ptr<Lightmap> lightmap=nullptr);
/// @brief Refresh `bottom` and `top` values
void updateHeights();
// unused
std::unique_ptr<Chunk> clone() const;
/// @brief Creates new block inventory given size
/// @return inventory id or 0 if block does not exists
void addBlockInventory(
+8 -4
View File
@@ -115,9 +115,10 @@ ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) const {
if (chunk == nullptr) {
return 0;
}
assert(chunk->lightmap != nullptr);
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
return chunk->lightmap.get(lx, y, lz, channel);
return chunk->lightmap->get(lx, y, lz, channel);
}
light_t Chunks::getLight(int32_t x, int32_t y, int32_t z) const {
@@ -135,9 +136,10 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z) const {
if (chunk == nullptr) {
return 0;
}
assert(chunk->lightmap != nullptr);
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
return chunk->lightmap.get(lx, y, lz);
return chunk->lightmap->get(lx, y, lz);
}
Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) const {
@@ -373,7 +375,8 @@ void Chunks::getVoxels(VoxelsVolume& volume, bool backlight) const {
}
} else {
const voxel* cvoxels = chunk->voxels;
const light_t* clights = chunk->lightmap.getLights();
const light_t* clights =
chunk->lightmap ? chunk->lightmap->getLights() : nullptr;
for (int ly = y; ly < y + h; ly++) {
for (int lz = std::max(z, cz * CHUNK_D);
lz < std::min(z + d, (cz + 1) * CHUNK_D);
@@ -390,7 +393,8 @@ void Chunks::getVoxels(VoxelsVolume& volume, bool backlight) const {
CHUNK_D
);
voxels[vidx] = cvoxels[cidx];
light_t light = clights[cidx];
light_t light = clights ? clights[cidx]
: Lightmap::SUN_LIGHT_ONLY;
if (backlight) {
const auto block =
indices.blocks.get(voxels[vidx].id);
+9 -5
View File
@@ -91,14 +91,16 @@ static inline auto load_inventories(
}
static util::ObjectsPool<Chunk> chunks_pool(1'024);
static util::ObjectsPool<Lightmap> lightmaps_pool;
std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
std::shared_ptr<Chunk> GlobalChunks::create(int x, int z, bool lighting) {
const auto& found = chunksMap.find(keyfrom(x, z));
if (found != chunksMap.end()) {
return found->second;
}
auto chunk = chunks_pool.create(x, z);
auto chunk =
chunks_pool.create(x, z, lighting ? lightmaps_pool.create() : nullptr);
chunksMap[keyfrom(x, z)] = chunk;
World& world = *level.getWorld();
@@ -125,9 +127,11 @@ std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
level.inventories->store(entry.second);
}
}
if (auto lights = regions.getLights(chunk->x, chunk->z)) {
chunk->lightmap.set(lights.get());
chunk->flags.loadedLights = true;
if (chunk->lightmap) {
if (auto lights = regions.getLights(chunk->x, chunk->z)) {
chunk->lightmap->set(lights.get());
chunk->flags.loadedLights = true;
}
}
chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z);
return chunk;
+1 -1
View File
@@ -40,7 +40,7 @@ public:
void setOnUnload(consumer<Chunk&> onUnload);
std::shared_ptr<Chunk> fetch(int x, int z);
std::shared_ptr<Chunk> create(int x, int z);
std::shared_ptr<Chunk> create(int x, int z, bool lighting);
void pinChunk(std::shared_ptr<Chunk> chunk);
void unpinChunk(int x, int z);
+4 -2
View File
@@ -434,7 +434,8 @@ inline void get_voxels_impl(
}
} else {
const voxel* cvoxels = chunk->voxels;
const light_t* clights = chunk->lightmap.getLights();
const light_t* clights =
chunk->lightmap ? chunk->lightmap->getLights() : nullptr;
for (int ly = y; ly < y + h; ly++) {
for (int lz = std::max(z, cz * CHUNK_D);
lz < std::min(z + d, (cz + 1) * CHUNK_D);
@@ -451,7 +452,8 @@ inline void get_voxels_impl(
CHUNK_D
);
voxels[vidx] = cvoxels[cidx];
light_t light = clights[cidx];
light_t light = clights ? clights[cidx]
: Lightmap::SUN_LIGHT_ONLY;
if (backlight) {
const auto block = blocks.get(voxels[vidx].id);
if (block && block->lightPassing) {