Removed block size cache in VoxelDataMap, takes space for no good use

master
Marc Gilleron 2022-08-29 13:45:25 +01:00
parent d2b20f676e
commit 74052885ad
5 changed files with 32 additions and 29 deletions

View File

@ -228,6 +228,8 @@ void initialize_voxel_module(ModuleInitializationLevel p_level) {
ZN_PRINT_VERBOSE(format("Size of Node: {}", sizeof(Node))); ZN_PRINT_VERBOSE(format("Size of Node: {}", sizeof(Node)));
ZN_PRINT_VERBOSE(format("Size of Node3D: {}", sizeof(Node3D))); ZN_PRINT_VERBOSE(format("Size of Node3D: {}", sizeof(Node3D)));
ZN_PRINT_VERBOSE(format("Size of RWLock: {}", sizeof(zylann::RWLock))); ZN_PRINT_VERBOSE(format("Size of RWLock: {}", sizeof(zylann::RWLock)));
ZN_PRINT_VERBOSE(format("Size of Mutex: {}", sizeof(zylann::Mutex)));
ZN_PRINT_VERBOSE(format("Size of BinaryMutex: {}", sizeof(zylann::BinaryMutex)));
ZN_PRINT_VERBOSE(format("Size of gd::VoxelBuffer: {}", sizeof(gd::VoxelBuffer))); ZN_PRINT_VERBOSE(format("Size of gd::VoxelBuffer: {}", sizeof(gd::VoxelBuffer)));
ZN_PRINT_VERBOSE(format("Size of VoxelBufferInternal: {}", sizeof(VoxelBufferInternal))); ZN_PRINT_VERBOSE(format("Size of VoxelBufferInternal: {}", sizeof(VoxelBufferInternal)));
ZN_PRINT_VERBOSE(format("Size of VoxelMeshBlock: {}", sizeof(VoxelMeshBlock))); ZN_PRINT_VERBOSE(format("Size of VoxelMeshBlock: {}", sizeof(VoxelMeshBlock)));

View File

@ -34,7 +34,7 @@ void VoxelData::reset_maps_no_lock() {
Lod &data_lod = _lods[lod_index]; Lod &data_lod = _lods[lod_index];
// Instance new maps if we have more lods, or clear them otherwise // Instance new maps if we have more lods, or clear them otherwise
if (lod_index < _lod_count) { if (lod_index < _lod_count) {
data_lod.map.create(data_lod.map.get_block_size_pow2(), lod_index); data_lod.map.create(lod_index);
} else { } else {
data_lod.map.clear(); data_lod.map.clear();
} }

View File

@ -11,27 +11,28 @@ namespace zylann::voxel {
VoxelDataMap::VoxelDataMap() { VoxelDataMap::VoxelDataMap() {
// This is not planned to change at runtime at the moment. // This is not planned to change at runtime at the moment.
set_block_size_pow2(constants::DEFAULT_BLOCK_SIZE_PO2); //set_block_size_pow2(constants::DEFAULT_BLOCK_SIZE_PO2);
} }
VoxelDataMap::~VoxelDataMap() { VoxelDataMap::~VoxelDataMap() {
clear(); clear();
} }
void VoxelDataMap::create(unsigned int block_size_po2, int lod_index) { void VoxelDataMap::create(unsigned int lod_index) {
ZN_ASSERT(lod_index < constants::MAX_LOD);
clear(); clear();
set_block_size_pow2(block_size_po2); //set_block_size_pow2(block_size_po2);
set_lod_index(lod_index); set_lod_index(lod_index);
} }
void VoxelDataMap::set_block_size_pow2(unsigned int p) { // void VoxelDataMap::set_block_size_pow2(unsigned int p) {
ZN_ASSERT_RETURN_MSG(p >= 1, "Block size is too small"); // ZN_ASSERT_RETURN_MSG(p >= 1, "Block size is too small");
ZN_ASSERT_RETURN_MSG(p <= 8, "Block size is too big"); // ZN_ASSERT_RETURN_MSG(p <= 8, "Block size is too big");
_block_size_pow2 = p; // _block_size_pow2 = p;
_block_size = 1 << _block_size_pow2; // _block_size = 1 << _block_size_pow2;
_block_size_mask = _block_size - 1; // _block_size_mask = _block_size - 1;
} // }
void VoxelDataMap::set_lod_index(int lod_index) { void VoxelDataMap::set_lod_index(int lod_index) {
ZN_ASSERT_RETURN_MSG(lod_index >= 0, "LOD index can't be negative"); ZN_ASSERT_RETURN_MSG(lod_index >= 0, "LOD index can't be negative");
@ -56,7 +57,7 @@ int VoxelDataMap::get_voxel(Vector3i pos, unsigned int c) const {
VoxelDataBlock *VoxelDataMap::create_default_block(Vector3i bpos) { VoxelDataBlock *VoxelDataMap::create_default_block(Vector3i bpos) {
std::shared_ptr<VoxelBufferInternal> buffer = make_shared_instance<VoxelBufferInternal>(); std::shared_ptr<VoxelBufferInternal> buffer = make_shared_instance<VoxelBufferInternal>();
buffer->create(_block_size, _block_size, _block_size); buffer->create(get_block_size(), get_block_size(), get_block_size());
//buffer->set_default_values(_default_voxel); //buffer->set_default_values(_default_voxel);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
ZN_ASSERT_RETURN_V(!has_block(bpos), nullptr); ZN_ASSERT_RETURN_V(!has_block(bpos), nullptr);
@ -190,7 +191,7 @@ void VoxelDataMap::copy(Vector3i min_pos, VoxelBufferInternal &dst_buffer, unsig
const Vector3i min_block_pos = voxel_to_block(min_pos); const Vector3i min_block_pos = voxel_to_block(min_pos);
const Vector3i max_block_pos = voxel_to_block(max_pos - Vector3i(1, 1, 1)) + Vector3i(1, 1, 1); const Vector3i max_block_pos = voxel_to_block(max_pos - Vector3i(1, 1, 1)) + Vector3i(1, 1, 1);
const Vector3i block_size_v(_block_size, _block_size, _block_size); const Vector3i block_size_v(get_block_size(), get_block_size(), get_block_size());
unsigned int channels_count; unsigned int channels_count;
FixedArray<uint8_t, VoxelBufferInternal::MAX_CHANNELS> channels = FixedArray<uint8_t, VoxelBufferInternal::MAX_CHANNELS> channels =
@ -217,7 +218,7 @@ void VoxelDataMap::copy(Vector3i min_pos, VoxelBufferInternal &dst_buffer, unsig
} }
} else if (gen_func != nullptr) { } else if (gen_func != nullptr) {
const Box3i box = Box3i(bpos << _block_size_pow2, Vector3iUtil::create(_block_size)) const Box3i box = Box3i(bpos << get_block_size_pow2(), block_size_v)
.clipped(Box3i(min_pos, dst_buffer.get_size())); .clipped(Box3i(min_pos, dst_buffer.get_size()));
// TODO Format? // TODO Format?

View File

@ -25,6 +25,11 @@ class VoxelGenerator;
// //
class VoxelDataMap { class VoxelDataMap {
public: public:
// This is block size in VOXELS. To convert to space units, use `block_size << lod_index`.
static const unsigned int BLOCK_SIZE_PO2 = constants::DEFAULT_BLOCK_SIZE_PO2;
static const unsigned int BLOCK_SIZE = 1 << BLOCK_SIZE_PO2;
static const unsigned int BLOCK_SIZE_MASK = BLOCK_SIZE - 1;
// Converts voxel coodinates into block coordinates. // Converts voxel coodinates into block coordinates.
// Don't use division because it introduces an offset in negative coordinates. // Don't use division because it introduces an offset in negative coordinates.
static inline Vector3i voxel_to_block_b(Vector3i pos, int block_size_pow2) { static inline Vector3i voxel_to_block_b(Vector3i pos, int block_size_pow2) {
@ -32,31 +37,31 @@ public:
} }
inline Vector3i voxel_to_block(Vector3i pos) const { inline Vector3i voxel_to_block(Vector3i pos) const {
return voxel_to_block_b(pos, _block_size_pow2); return voxel_to_block_b(pos, BLOCK_SIZE_PO2);
} }
inline Vector3i to_local(Vector3i pos) const { inline Vector3i to_local(Vector3i pos) const {
return Vector3i(pos.x & _block_size_mask, pos.y & _block_size_mask, pos.z & _block_size_mask); return Vector3i(pos.x & BLOCK_SIZE_MASK, pos.y & BLOCK_SIZE_MASK, pos.z & BLOCK_SIZE_MASK);
} }
// Converts block coodinates into voxel coordinates // Converts block coodinates into voxel coordinates
inline Vector3i block_to_voxel(Vector3i bpos) const { inline Vector3i block_to_voxel(Vector3i bpos) const {
return bpos * _block_size; return bpos * BLOCK_SIZE;
} }
VoxelDataMap(); VoxelDataMap();
~VoxelDataMap(); ~VoxelDataMap();
void create(unsigned int block_size_po2, int lod_index); void create(unsigned int lod_index);
inline unsigned int get_block_size() const { inline unsigned int get_block_size() const {
return _block_size; return BLOCK_SIZE;
} }
inline unsigned int get_block_size_pow2() const { inline unsigned int get_block_size_pow2() const {
return _block_size_pow2; return BLOCK_SIZE_PO2;
} }
inline unsigned int get_block_size_mask() const { inline unsigned int get_block_size_mask() const {
return _block_size_mask; return BLOCK_SIZE_MASK;
} }
void set_lod_index(int lod_index); void set_lod_index(int lod_index);
@ -180,7 +185,7 @@ private:
VoxelDataBlock *get_or_create_block_at_voxel_pos(Vector3i pos); VoxelDataBlock *get_or_create_block_at_voxel_pos(Vector3i pos);
VoxelDataBlock *create_default_block(Vector3i bpos); VoxelDataBlock *create_default_block(Vector3i bpos);
void set_block_size_pow2(unsigned int p); //void set_block_size_pow2(unsigned int p);
private: private:
// Blocks stored with a spatial hash in all 3D directions. // Blocks stored with a spatial hash in all 3D directions.
@ -198,11 +203,6 @@ private:
// To prevent too much hashing, this reference is checked before. // To prevent too much hashing, this reference is checked before.
//mutable VoxelDataBlock *_last_accessed_block = nullptr; //mutable VoxelDataBlock *_last_accessed_block = nullptr;
// This is block size in VOXELS. To convert to space units, use `block_size << lod_index`.
unsigned int _block_size;
unsigned int _block_size_pow2;
unsigned int _block_size_mask;
unsigned int _lod_index = 0; unsigned int _lod_index = 0;
}; };

View File

@ -184,7 +184,7 @@ Ref<VoxelGenerator> VoxelTerrain::get_generator() const {
}*/ }*/
void VoxelTerrain::_set_block_size_po2(int p_block_size_po2) { void VoxelTerrain::_set_block_size_po2(int p_block_size_po2) {
_data_map.create(p_block_size_po2, 0); _data_map.create(0);
} }
unsigned int VoxelTerrain::get_data_block_size_pow2() const { unsigned int VoxelTerrain::get_data_block_size_pow2() const {
@ -790,7 +790,7 @@ void VoxelTerrain::reset_map() {
_data_map.for_each_block([this](const Vector3i &bpos, VoxelDataBlock &block) { // _data_map.for_each_block([this](const Vector3i &bpos, VoxelDataBlock &block) { //
emit_data_block_unloaded(block, bpos); emit_data_block_unloaded(block, bpos);
}); });
_data_map.create(get_data_block_size_pow2(), 0); _data_map.create(0);
_mesh_map.clear(); _mesh_map.clear();