From 868cd94ee9a5a0638c014a4cc42224f01ff234c8 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 5 Mar 2021 13:03:55 +0000 Subject: Prepare ChunkData for BlockState storage (#5105) * Rename ChunkData Creatable test * Add missing Y-check in RedstoneWireHandler * Remove ChunkDef.h dependency in Scoreboard * Prepare ChunkData for BlockState storage + Split chunk block, meta, block & sky light storage + Load the height map from disk - Reduce duplicated code in ChunkData - Remove saving MCSBiomes, there aren't any - Remove the allocation pool, ref #4315, #3864 * fixed build * fixed test * fixed the debug compile Co-authored-by: 12xx12 <44411062+12xx12@users.noreply.github.com> --- tests/ChunkData/Copies.cpp | 100 ++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 42 deletions(-) (limited to 'tests/ChunkData/Copies.cpp') diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp index 9b9cbbaf1..3555d4301 100644 --- a/tests/ChunkData/Copies.cpp +++ b/tests/ChunkData/Copies.cpp @@ -7,41 +7,50 @@ -/** Performs the entire Copies test. */ -static void test() +/** Helper that copies a data store into a contiguous flat array, filling in a default value for sections that aren't present. */ +template +static void CopyAll(const StoreType & Data, GetType Getter, DefaultType Default, OutType & Out) { - LOGD("Test started"); + constexpr auto SectionCount = std::extent_v / 16; - class cMockAllocationPool - : public cAllocationPool + for (size_t Y = 0; Y != 16; Y++) { - virtual cChunkData::sChunkSection * Allocate() override - { - return new cChunkData::sChunkSection(); - } + const auto Section = (Data.*Getter)(Y); + static_assert(SectionCount == std::tuple_size>::value, "Output array has wrong size"); - virtual void Free(cChunkData::sChunkSection * a_Ptr) override + if (Section == nullptr) { - delete a_Ptr; + std::fill_n(Out + Y * SectionCount, SectionCount, Default); } - - virtual bool DoIsEqual(const cAllocationPool&) const noexcept override + else { - return false; + std::copy(Section->begin(), Section->end(), Out + Y * SectionCount); } - } Pool; + } +} + + + + + +/** Performs the entire Copies test. */ +static void Test() +{ + LOGD("Test started"); + { - cChunkData buffer(Pool); + ChunkBlockData buffer; buffer.SetBlock({ 3, 1, 4 }, 0xDE); buffer.SetMeta({ 3, 1, 4 }, 0xA); - cChunkData copy(Pool); + ChunkBlockData copy; copy.Assign(buffer); TEST_EQUAL(copy.GetBlock({ 3, 1, 4 }), 0xDE); TEST_EQUAL(copy.GetMeta({ 3, 1, 4 }), 0xA); BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]{}; for (int i = 0; i < 16 * 16 * 256; i += 4) { SrcBlockBuffer[i + 0] = 0xde; @@ -50,20 +59,21 @@ static void test() SrcBlockBuffer[i + 3] = 0xef; } - buffer.SetBlockTypes(SrcBlockBuffer); + buffer.SetAll(SrcBlockBuffer, SrcNibbleBuffer); BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; - buffer.CopyBlockTypes(DstBlockBuffer); + CopyAll(buffer, &ChunkBlockData::GetSection, ChunkBlockData::DefaultValue, DstBlockBuffer); TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); - buffer.SetBlockTypes(SrcBlockBuffer); - buffer.CopyBlockTypes(DstBlockBuffer); + buffer.SetAll(SrcBlockBuffer, SrcNibbleBuffer); + CopyAll(buffer, &ChunkBlockData::GetSection, ChunkBlockData::DefaultValue, DstBlockBuffer); TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); } { - cChunkData buffer(Pool); + ChunkBlockData buffer; + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]{}; NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) { @@ -73,19 +83,19 @@ static void test() SrcNibbleBuffer[i + 3] = 0xef; } - buffer.SetMetas(SrcNibbleBuffer); + buffer.SetAll(SrcBlockBuffer, SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; - buffer.CopyMetas(DstNibbleBuffer); + CopyAll(buffer, &ChunkBlockData::GetMetaSection, ChunkBlockData::DefaultMetaValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); - buffer.SetMetas(SrcNibbleBuffer); - buffer.CopyMetas(DstNibbleBuffer); + buffer.SetAll(SrcBlockBuffer, SrcNibbleBuffer); + CopyAll(buffer, &ChunkBlockData::GetMetaSection, ChunkBlockData::DefaultMetaValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } { - cChunkData buffer(Pool); + ChunkLightData buffer; NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) @@ -96,19 +106,19 @@ static void test() SrcNibbleBuffer[i + 3] = 0xef; } - buffer.SetBlockLight(SrcNibbleBuffer); + buffer.SetAll(SrcNibbleBuffer, SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; - buffer.CopyBlockLight(DstNibbleBuffer); + CopyAll(buffer, &ChunkLightData::GetBlockLightSection, ChunkLightData::DefaultBlockLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1), 0); memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); - buffer.SetBlockLight(SrcNibbleBuffer); - buffer.CopyBlockLight(DstNibbleBuffer); + buffer.SetAll(SrcNibbleBuffer, SrcNibbleBuffer); + CopyAll(buffer, &ChunkLightData::GetBlockLightSection, ChunkLightData::DefaultBlockLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1), 0); } { - cChunkData buffer(Pool); + ChunkLightData buffer; NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) @@ -119,38 +129,44 @@ static void test() SrcNibbleBuffer[i + 3] = 0xef; } - buffer.SetSkyLight(SrcNibbleBuffer); + buffer.SetAll(SrcNibbleBuffer, SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; - buffer.CopySkyLight(DstNibbleBuffer); + CopyAll(buffer, &ChunkLightData::GetSkyLightSection, ChunkLightData::DefaultSkyLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); - buffer.SetSkyLight(SrcNibbleBuffer); - buffer.CopySkyLight(DstNibbleBuffer); + buffer.SetAll(SrcNibbleBuffer, SrcNibbleBuffer); + CopyAll(buffer, &ChunkLightData::GetSkyLightSection, ChunkLightData::DefaultSkyLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } { - cChunkData buffer(Pool); + ChunkBlockData buffer; BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; - buffer.CopyBlockTypes(DstBlockBuffer); + CopyAll(buffer, &ChunkBlockData::GetSection, ChunkBlockData::DefaultValue, DstBlockBuffer); TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; - buffer.CopyMetas(DstNibbleBuffer); + CopyAll(buffer, &ChunkBlockData::GetMetaSection, ChunkBlockData::DefaultMetaValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); + } + + { + ChunkLightData buffer; + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); - buffer.CopyBlockLight(DstNibbleBuffer); + NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; + CopyAll(buffer, &ChunkLightData::GetBlockLightSection, ChunkLightData::DefaultBlockLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); - buffer.CopySkyLight(DstNibbleBuffer); + CopyAll(buffer, &ChunkLightData::GetSkyLightSection, ChunkLightData::DefaultSkyLightValue, DstNibbleBuffer); TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } } @@ -160,5 +176,5 @@ static void test() IMPLEMENT_TEST_MAIN("ChunkData Copies", - test() + Test() ) -- cgit v1.2.3