From 4a359a3dbd198b9629274960261a0f6a8341655b Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Fri, 10 Jan 2020 18:56:57 +0000 Subject: [PATCH] Add depth format to region files stream (untested) --- streams/voxel_stream_region_files.cpp | 48 ++++++++++++++++++++++++++- streams/voxel_stream_region_files.h | 2 ++ voxel_buffer.h | 4 ++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/streams/voxel_stream_region_files.cpp b/streams/voxel_stream_region_files.cpp index eff5ada2..07730d74 100644 --- a/streams/voxel_stream_region_files.cpp +++ b/streams/voxel_stream_region_files.cpp @@ -7,7 +7,8 @@ #include namespace { -const uint8_t FORMAT_VERSION = 1; +const uint8_t FORMAT_VERSION = 2; +const uint8_t FORMAT_VERSION_LEGACY_1 = 1; const char *FORMAT_REGION_MAGIC = "VXR_"; const char *META_FILE_NAME = "meta.vxrm"; const int MAGIC_AND_VERSION_SIZE = 4 + 1; @@ -20,6 +21,10 @@ VoxelStreamRegionFiles::VoxelStreamRegionFiles() { _meta.region_size_po2 = 4; _meta.sector_size = 512; // next_power_of_2(_meta.block_size.volume() / 10) // based on compression ratios _meta.lod_count = 1; + + for (unsigned int i = 0; i < _meta.channel_depths.size(); ++i) { + _meta.channel_depths[i] = VoxelBuffer::DEFAULT_CHANNEL_DEPTH; + } } VoxelStreamRegionFiles::~VoxelStreamRegionFiles() { @@ -183,6 +188,11 @@ void VoxelStreamRegionFiles::_immerge_block(Ref voxel_buffer, Vecto ERR_FAIL_COND(err != OK); } + // Verify format + for (unsigned int i = 0; i < VoxelBuffer::MAX_CHANNELS; ++i) { + ERR_FAIL_COND(voxel_buffer->get_channel_depth(i) != _meta.channel_depths[i]); + } + Vector3i block_pos = get_block_position_from_voxels(origin_in_voxels) >> lod; Vector3i region_pos = get_region_position_from_blocks(block_pos); Vector3i block_rpos = block_pos.wrap(region_size); @@ -410,6 +420,14 @@ static bool from_json_varray(Array a, Vector3i &v) { return true; } +static bool depth_from_json_variant(Variant &v, VoxelBuffer::Depth &d) { + uint8_t n; + ERR_FAIL_COND_V(!u8_from_json_variant(v, n), false); + ERR_FAIL_INDEX_V(n, VoxelBuffer::DEPTH_COUNT, false); + d = (VoxelBuffer::Depth)n; + return true; +} + Error VoxelStreamRegionFiles::save_meta() { ERR_FAIL_COND_V(_directory_path == "", ERR_INVALID_PARAMETER); @@ -421,6 +439,13 @@ Error VoxelStreamRegionFiles::save_meta() { d["lod_count"] = _meta.lod_count; d["sector_size"] = _meta.sector_size; + Array channel_depths; + channel_depths.resize(_meta.channel_depths.size()); + for (unsigned int i = 0; i < _meta.channel_depths.size(); ++i) { + channel_depths[i] = _meta.channel_depths[i]; + } + d["channel_depths"] = channel_depths; + String json = JSON::print(d, "\t", true); // Make sure the directory exists @@ -449,6 +474,19 @@ Error VoxelStreamRegionFiles::save_meta() { return OK; } +static void migrate_region_meta_data(Dictionary &data) { + + if (data["version"] == Variant(FORMAT_VERSION_LEGACY_1)) { + Array depths; + depths.resize(VoxelBuffer::MAX_CHANNELS); + for (int i = 0; i < depths.size(); ++i) { + depths[i] = VoxelBuffer::DEFAULT_CHANNEL_DEPTH; + } + data["channel_depths"] = depths; + data["version"] = FORMAT_VERSION; + } +} + Error VoxelStreamRegionFiles::load_meta() { ERR_FAIL_COND_V(_directory_path == "", ERR_INVALID_PARAMETER); @@ -482,6 +520,7 @@ Error VoxelStreamRegionFiles::load_meta() { } Dictionary d = res; + migrate_region_meta_data(d); Meta meta; ERR_FAIL_COND_V(!u8_from_json_variant(d["version"], meta.version), ERR_PARSE_ERROR); ERR_FAIL_COND_V(!u8_from_json_variant(d["block_size_po2"], meta.block_size_po2), ERR_PARSE_ERROR); @@ -490,6 +529,13 @@ Error VoxelStreamRegionFiles::load_meta() { ERR_FAIL_COND_V(!s32_from_json_variant(d["sector_size"], meta.sector_size), ERR_PARSE_ERROR); ERR_FAIL_COND_V(meta.version < 0, ERR_PARSE_ERROR); + + Array channel_depths_data = d["channel_depths"]; + ERR_FAIL_COND_V(channel_depths_data.size() != VoxelBuffer::MAX_CHANNELS, ERR_PARSE_ERROR); + for (int i = 0; i < channel_depths_data.size(); ++i) { + ERR_FAIL_COND_V(depth_from_json_variant(channel_depths_data[i], meta.channel_depths[i]), ERR_PARSE_ERROR); + } + ERR_FAIL_COND_V(!check_meta(meta), ERR_INVALID_PARAMETER); _meta = meta; diff --git a/streams/voxel_stream_region_files.h b/streams/voxel_stream_region_files.h index 3a202e13..bcaec351 100644 --- a/streams/voxel_stream_region_files.h +++ b/streams/voxel_stream_region_files.h @@ -1,6 +1,7 @@ #ifndef VOXEL_STREAM_REGION_H #define VOXEL_STREAM_REGION_H +#include "../util/fixed_array.h" #include "voxel_stream_file.h" class FileAccess; @@ -83,6 +84,7 @@ private: uint8_t lod_count = 0; uint8_t block_size_po2 = 0; // How many voxels in a cubic block uint8_t region_size_po2 = 0; // How many blocks in one cubic region + FixedArray channel_depths; int sector_size = 0; // Blocks are stored at offsets multiple of that size }; diff --git a/voxel_buffer.h b/voxel_buffer.h index a0477693..c2972357 100644 --- a/voxel_buffer.h +++ b/voxel_buffer.h @@ -48,6 +48,8 @@ public: DEPTH_COUNT }; + static const Depth DEFAULT_CHANNEL_DEPTH = DEPTH_8_BIT; + VoxelBuffer(); ~VoxelBuffer(); @@ -180,7 +182,7 @@ private: // Default value when data is null uint64_t defval = 0; - Depth depth = DEPTH_8_BIT; + Depth depth = DEFAULT_CHANNEL_DEPTH; uint32_t size_in_bytes = 0; };