diff --git a/streams/region/region_file.cpp b/streams/region/region_file.cpp index a77ee49e..1aeabd86 100644 --- a/streams/region/region_file.cpp +++ b/streams/region/region_file.cpp @@ -303,7 +303,7 @@ const RegionFormat &RegionFile::get_format() const { } Error RegionFile::load_block( - Vector3i position, VoxelBufferInternal &out_block, VoxelBlockSerializerInternal &serializer) { + Vector3i position, VoxelBufferInternal &out_block, zylann::voxel::BlockSerializer &serializer) { // ERR_FAIL_COND_V(_file_access == nullptr, ERR_FILE_CANT_READ); FileAccess *f = _file_access; @@ -336,7 +336,8 @@ Error RegionFile::load_block( return OK; } -Error RegionFile::save_block(Vector3i position, VoxelBufferInternal &block, VoxelBlockSerializerInternal &serializer) { +Error RegionFile::save_block( + Vector3i position, VoxelBufferInternal &block, zylann::voxel::BlockSerializer &serializer) { // ERR_FAIL_COND_V(_header.format.verify_block(block) == false, ERR_INVALID_PARAMETER); @@ -361,7 +362,7 @@ Error RegionFile::save_block(Vector3i position, VoxelBufferInternal &block, Voxe // Check position matches the sectors rule CRASH_COND((block_offset - _blocks_begin_offset) % _header.format.sector_size != 0); - VoxelBlockSerializerInternal::SerializeResult res = serializer.serialize_and_compress(block); + zylann::voxel::BlockSerializer::SerializeResult res = serializer.serialize_and_compress(block); ERR_FAIL_COND_V(!res.success, ERR_INVALID_PARAMETER); f->store_32(res.data.size()); const unsigned int written_size = sizeof(int) + res.data.size(); @@ -389,7 +390,7 @@ Error RegionFile::save_block(Vector3i position, VoxelBufferInternal &block, Voxe const int old_sector_count = block_info.get_sector_count(); CRASH_COND(old_sector_count < 1); - VoxelBlockSerializerInternal::SerializeResult res = serializer.serialize_and_compress(block); + zylann::voxel::BlockSerializer::SerializeResult res = serializer.serialize_and_compress(block); ERR_FAIL_COND_V(!res.success, ERR_INVALID_PARAMETER); const std::vector &data = res.data; const int written_size = sizeof(int) + data.size(); diff --git a/streams/region/region_file.h b/streams/region/region_file.h index 6396a3ca..b9807e2e 100644 --- a/streams/region/region_file.h +++ b/streams/region/region_file.h @@ -8,10 +8,11 @@ #include class FileAccess; -class VoxelBlockSerializerInternal; namespace zylann::voxel { +class BlockSerializer; + struct RegionFormat { static const char *FILE_EXTENSION; static const uint32_t MAX_BLOCKS_ACROSS = 255; @@ -83,8 +84,8 @@ public: bool set_format(const RegionFormat &format); const RegionFormat &get_format() const; - Error load_block(Vector3i position, VoxelBufferInternal &out_block, VoxelBlockSerializerInternal &serializer); - Error save_block(Vector3i position, VoxelBufferInternal &block, VoxelBlockSerializerInternal &serializer); + Error load_block(Vector3i position, VoxelBufferInternal &out_block, zylann::voxel::BlockSerializer &serializer); + Error save_block(Vector3i position, VoxelBufferInternal &block, zylann::voxel::BlockSerializer &serializer); unsigned int get_header_block_count() const; bool has_block(Vector3i position) const; diff --git a/streams/region/voxel_stream_region_files.cpp b/streams/region/voxel_stream_region_files.cpp index ff78c316..653acf37 100644 --- a/streams/region/voxel_stream_region_files.cpp +++ b/streams/region/voxel_stream_region_files.cpp @@ -19,7 +19,7 @@ const char *META_FILE_NAME = "meta.vxrm"; } // namespace -thread_local VoxelBlockSerializerInternal VoxelStreamRegionFiles::_block_serializer; +thread_local zylann::voxel::BlockSerializer VoxelStreamRegionFiles::_block_serializer; // Sorts a sequence without modifying it, returning a sorted list of pointers template diff --git a/streams/region/voxel_stream_region_files.h b/streams/region/voxel_stream_region_files.h index a02b363f..a28e5acd 100644 --- a/streams/region/voxel_stream_region_files.h +++ b/streams/region/voxel_stream_region_files.h @@ -109,7 +109,7 @@ private: } }; - static thread_local VoxelBlockSerializerInternal _block_serializer; + static thread_local zylann::voxel::BlockSerializer _block_serializer; // TODO This is not thread-friendly. // `VoxelRegionFile` is not thread-safe so we have to limit the usage to one thread at once, blocking the others. diff --git a/streams/sqlite/voxel_stream_sqlite.cpp b/streams/sqlite/voxel_stream_sqlite.cpp index b48cf7e3..f8f9499e 100644 --- a/streams/sqlite/voxel_stream_sqlite.cpp +++ b/streams/sqlite/voxel_stream_sqlite.cpp @@ -572,7 +572,7 @@ void VoxelStreamSQLiteInternal::save_meta(Meta meta) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -thread_local VoxelBlockSerializerInternal VoxelStreamSQLite::_voxel_block_serializer; +thread_local zylann::voxel::BlockSerializer VoxelStreamSQLite::_voxel_block_serializer; thread_local std::vector VoxelStreamSQLite::_temp_block_data; thread_local std::vector VoxelStreamSQLite::_temp_compressed_block_data; @@ -885,7 +885,7 @@ void VoxelStreamSQLite::flush_cache(VoxelStreamSQLiteInternal *con) { PRINT_VERBOSE(String("VoxelStreamSQLite: Flushing cache ({0} elements)") .format(varray(_cache.get_indicative_block_count()))); - VoxelBlockSerializerInternal &serializer = _voxel_block_serializer; + zylann::voxel::BlockSerializer &serializer = _voxel_block_serializer; ERR_FAIL_COND(con == nullptr); ERR_FAIL_COND(con->begin_transaction() == false); @@ -909,7 +909,7 @@ void VoxelStreamSQLite::flush_cache(VoxelStreamSQLiteInternal *con) { const std::vector empty; con->save_block(loc, empty, VoxelStreamSQLiteInternal::VOXELS); } else { - VoxelBlockSerializerInternal::SerializeResult res = serializer.serialize_and_compress(block.voxels); + zylann::voxel::BlockSerializer::SerializeResult res = serializer.serialize_and_compress(block.voxels); ERR_FAIL_COND(!res.success); con->save_block(loc, res.data, VoxelStreamSQLiteInternal::VOXELS); } diff --git a/streams/sqlite/voxel_stream_sqlite.h b/streams/sqlite/voxel_stream_sqlite.h index 032488f3..7f8978ba 100644 --- a/streams/sqlite/voxel_stream_sqlite.h +++ b/streams/sqlite/voxel_stream_sqlite.h @@ -28,11 +28,12 @@ public: void immerge_blocks(Span p_blocks) override; bool supports_instance_blocks() const override; - void load_instance_blocks( - Span out_blocks, Span out_results) override; + void load_instance_blocks(Span out_blocks, Span out_results) override; void save_instance_blocks(Span p_blocks) override; - bool supports_loading_all_blocks() const override { return true; } + bool supports_loading_all_blocks() const override { + return true; + } void load_all_blocks(FullLoadingResult &result) override; int get_used_channels_mask() const override; @@ -66,7 +67,7 @@ private: VoxelStreamCache _cache; // TODO I should consider specialized memory allocators - static thread_local VoxelBlockSerializerInternal _voxel_block_serializer; + static thread_local zylann::voxel::BlockSerializer _voxel_block_serializer; static thread_local std::vector _temp_block_data; static thread_local std::vector _temp_compressed_block_data; }; diff --git a/streams/voxel_block_serializer.cpp b/streams/voxel_block_serializer.cpp index a776ef3b..4f297775 100644 --- a/streams/voxel_block_serializer.cpp +++ b/streams/voxel_block_serializer.cpp @@ -12,6 +12,8 @@ #include #include +namespace zylann::voxel { + namespace { const uint8_t BLOCK_VERSION = 2; const unsigned int BLOCK_TRAILING_MAGIC = 0x900df00d; @@ -182,8 +184,7 @@ size_t get_size_in_bytes(const VoxelBufferInternal &buffer, size_t &metadata_siz return size + metadata_size_with_header + BLOCK_TRAILING_MAGIC_SIZE; } -VoxelBlockSerializerInternal::SerializeResult VoxelBlockSerializerInternal::serialize( - const VoxelBufferInternal &voxel_buffer) { +BlockSerializer::SerializeResult BlockSerializer::serialize(const VoxelBufferInternal &voxel_buffer) { // VOXEL_PROFILE_SCOPE(); // Cannot serialize an empty block @@ -262,7 +263,7 @@ VoxelBlockSerializerInternal::SerializeResult VoxelBlockSerializerInternal::seri return SerializeResult(_data, true); } -bool VoxelBlockSerializerInternal::deserialize(Span p_data, VoxelBufferInternal &out_voxel_buffer) { +bool BlockSerializer::deserialize(Span p_data, VoxelBufferInternal &out_voxel_buffer) { // VOXEL_PROFILE_SCOPE(); @@ -364,32 +365,30 @@ bool VoxelBlockSerializerInternal::deserialize(Span p_data, Voxel return true; } -VoxelBlockSerializerInternal::SerializeResult VoxelBlockSerializerInternal::serialize_and_compress( - const VoxelBufferInternal &voxel_buffer) { +BlockSerializer::SerializeResult BlockSerializer::serialize_and_compress(const VoxelBufferInternal &voxel_buffer) { VOXEL_PROFILE_SCOPE(); SerializeResult res = serialize(voxel_buffer); ERR_FAIL_COND_V(!res.success, SerializeResult(_compressed_data, false)); const std::vector &data = res.data; - res.success = zylann::voxel::CompressedData::compress(Span(data.data(), 0, data.size()), - _compressed_data, zylann::voxel::CompressedData::COMPRESSION_LZ4); + res.success = CompressedData::compress( + Span(data.data(), 0, data.size()), _compressed_data, CompressedData::COMPRESSION_LZ4); ERR_FAIL_COND_V(!res.success, SerializeResult(_compressed_data, false)); return SerializeResult(_compressed_data, true); } -bool VoxelBlockSerializerInternal::decompress_and_deserialize( - Span p_data, VoxelBufferInternal &out_voxel_buffer) { +bool BlockSerializer::decompress_and_deserialize(Span p_data, VoxelBufferInternal &out_voxel_buffer) { VOXEL_PROFILE_SCOPE(); - const bool res = zylann::voxel::CompressedData::decompress(p_data, _data); + const bool res = CompressedData::decompress(p_data, _data); ERR_FAIL_COND_V(!res, false); return deserialize(to_span_const(_data), out_voxel_buffer); } -bool VoxelBlockSerializerInternal::decompress_and_deserialize( +bool BlockSerializer::decompress_and_deserialize( FileAccess *f, unsigned int size_to_read, VoxelBufferInternal &out_voxel_buffer) { VOXEL_PROFILE_SCOPE(); ERR_FAIL_COND_V(f == nullptr, false); @@ -407,7 +406,7 @@ bool VoxelBlockSerializerInternal::decompress_and_deserialize( return decompress_and_deserialize(to_span_const(_compressed_data), out_voxel_buffer); } -int VoxelBlockSerializerInternal::serialize(Ref peer, VoxelBufferInternal &voxel_buffer, bool compress) { +int BlockSerializer::serialize(Ref peer, VoxelBufferInternal &voxel_buffer, bool compress) { if (compress) { SerializeResult res = serialize_and_compress(voxel_buffer); ERR_FAIL_COND_V(!res.success, -1); @@ -422,8 +421,7 @@ int VoxelBlockSerializerInternal::serialize(Ref peer, VoxelBufferInt } } -void VoxelBlockSerializerInternal::deserialize( - Ref peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress) { +void BlockSerializer::deserialize(Ref peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress) { if (decompress) { _compressed_data.resize(size); const Error err = peer->get_data(_compressed_data.data(), _compressed_data.size()); @@ -439,6 +437,8 @@ void VoxelBlockSerializerInternal::deserialize( } } +} // namespace zylann::voxel + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int VoxelBlockSerializer::serialize(Ref peer, Ref voxel_buffer, bool compress) { diff --git a/streams/voxel_block_serializer.h b/streams/voxel_block_serializer.h index 8c1d9035..2a3f574b 100644 --- a/streams/voxel_block_serializer.h +++ b/streams/voxel_block_serializer.h @@ -10,15 +10,16 @@ class StreamPeer; class VoxelBufferInternal; -class VoxelBlockSerializerInternal { +namespace zylann::voxel { + +class BlockSerializer { // Had to be named differently to not conflict with the wrapper for Godot script API public: struct SerializeResult { const std::vector &data; bool success; - inline SerializeResult(const std::vector &p_data, bool p_success) : - data(p_data), success(p_success) {} + inline SerializeResult(const std::vector &p_data, bool p_success) : data(p_data), success(p_success) {} }; SerializeResult serialize(const VoxelBufferInternal &voxel_buffer); @@ -39,6 +40,8 @@ private: FileAccessMemory _file_access_memory; }; +} // namespace zylann::voxel + class VoxelBuffer; class VoxelBlockSerializer : public RefCounted { @@ -50,7 +53,7 @@ public: private: static void _bind_methods(); - VoxelBlockSerializerInternal _serializer; + zylann::voxel::BlockSerializer _serializer; }; #endif // VOXEL_BLOCK_SERIALIZER_H diff --git a/streams/voxel_stream_block_files.cpp b/streams/voxel_stream_block_files.cpp index 901755a5..4dab16ab 100644 --- a/streams/voxel_stream_block_files.cpp +++ b/streams/voxel_stream_block_files.cpp @@ -12,7 +12,7 @@ const char *META_FILE_NAME = "meta.vxbm"; const char *BLOCK_FILE_EXTENSION = ".vxb"; } // namespace -thread_local VoxelBlockSerializerInternal VoxelStreamBlockFiles::_block_serializer; +thread_local zylann::voxel::BlockSerializer VoxelStreamBlockFiles::_block_serializer; VoxelStreamBlockFiles::VoxelStreamBlockFiles() { // Defaults @@ -141,7 +141,7 @@ void VoxelStreamBlockFiles::immerge_block(VoxelBufferInternal &buffer, Vector3i f->store_buffer((uint8_t *)FORMAT_BLOCK_MAGIC, 4); f->store_8(FORMAT_VERSION); - VoxelBlockSerializerInternal::SerializeResult res = _block_serializer.serialize_and_compress(buffer); + zylann::voxel::BlockSerializer::SerializeResult res = _block_serializer.serialize_and_compress(buffer); if (!res.success) { memdelete(f); ERR_PRINT("Failed to save block"); diff --git a/streams/voxel_stream_block_files.h b/streams/voxel_stream_block_files.h index f1e05cb8..84a45d39 100644 --- a/streams/voxel_stream_block_files.h +++ b/streams/voxel_stream_block_files.h @@ -35,7 +35,7 @@ private: String get_block_file_path(const Vector3i &block_pos, unsigned int lod) const; Vector3i get_block_position(const Vector3i &origin_in_voxels) const; - static thread_local VoxelBlockSerializerInternal _block_serializer; + static thread_local zylann::voxel::BlockSerializer _block_serializer; String _directory_path; diff --git a/tests/tests.cpp b/tests/tests.cpp index 536fc52c..da4888f6 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1034,8 +1034,8 @@ void test_block_serializer() { voxel_buffer.fill_area(44, Vector3i(1, 2, 3), Vector3i(5, 5, 5), 1); // Serialize - VoxelBlockSerializerInternal serializer; - VoxelBlockSerializerInternal::SerializeResult result = serializer.serialize_and_compress(voxel_buffer); + BlockSerializer serializer; + BlockSerializer::SerializeResult result = serializer.serialize_and_compress(voxel_buffer); ERR_FAIL_COND(!result.success); std::vector data = result.data; @@ -1077,7 +1077,7 @@ void test_region_file() { ERR_FAIL_COND(open_error != OK); // Save block - VoxelBlockSerializerInternal serializer; + BlockSerializer serializer; const Error save_error = region_file.save_block(Vector3i(1, 2, 3), voxel_buffer, serializer); ERR_FAIL_COND(save_error != OK); @@ -1099,7 +1099,7 @@ void test_region_file() { // Read back VoxelBufferInternal loaded_voxel_buffer; - VoxelBlockSerializerInternal serializer; + BlockSerializer serializer; const Error load_error = region_file.load_block(Vector3i(1, 2, 3), loaded_voxel_buffer, serializer); ERR_FAIL_COND(load_error != OK);