From b30f16d7c9a832ed09b5cd03d73f343f933ca872 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Wed, 28 Mar 2018 19:21:50 +0500 Subject: Minor Section optimizations --- src/RendererSectionData.cpp | 4 ++-- src/Section.cpp | 29 +++++++++-------------------- src/Section.hpp | 6 +++--- src/World.cpp | 4 ++-- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 96d78e0..6554d16 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -213,7 +213,7 @@ RendererSectionData ParseSection(const SectionsData §ions) } unsigned char SectionsData::GetLight(const Vector & pos) const { - const Vector directions[] = { + static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), @@ -252,7 +252,7 @@ unsigned char SectionsData::GetLight(const Vector & pos) const { } unsigned char SectionsData::GetSkyLight(const Vector & pos) const { - const Vector directions[] = { + static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), diff --git a/src/Section.cpp b/src/Section.cpp index 1f60471..c8c67dc 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -13,17 +13,12 @@ void Section::CalculateHash() const { size_t offset = 0; std::vector rawData; - rawData.resize(block.size() * sizeof(long long) + light.size() + sky.size()); + rawData.resize(block.size() * sizeof(long long) + 4096); - std::memcpy(rawData.data() + offset, block.data(), block.size() * sizeof(BlockId)); - offset += block.size() * sizeof(BlockId); - - std::memcpy(rawData.data() + offset, light.data(), light.size() * sizeof(unsigned char)); - offset += light.size() * sizeof(unsigned char); - - if (!sky.empty()) - std::memcpy(rawData.data() + offset, sky.data(), sky.size() * sizeof(unsigned char)); - + std::memcpy(rawData.data(), light, 2048); + std::memcpy(rawData.data() + 2048, sky, 2048); + std::memcpy(rawData.data() + 4096, block.data(), block.size() * sizeof(long long)); + for (auto& it : overrideList) { rawData.push_back(*reinterpret_cast (&it.second) & 0xF); rawData.push_back(*reinterpret_cast (&it.second) >> 0xF); @@ -36,7 +31,7 @@ void Section::CalculateHash() const { hash = std::hash{}(str); } -Section::Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, std::vector lightData, std::vector skyData) { +Section::Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, const std::vector &lightData, const std::vector &skyData) { if (bitsPerBlock < 4) bitsPerBlock = 4; if (bitsPerBlock > 8) @@ -46,8 +41,8 @@ Section::Section(Vector pos, unsigned char bitsPerBlock, std::vectorworldPosition = pos; this->block = std::move(blockData); this->palette = std::move(palette); - this->light = std::move(lightData); - this->sky = std::move(skyData); + std::copy(lightData.begin(), lightData.end(), light); + std::copy(skyData.begin(), skyData.end(), sky); hash = -1; } @@ -99,9 +94,6 @@ BlockId Section::GetBlockId(Vector pos) const { unsigned char Section::GetBlockLight(Vector pos) const { - if (light.empty()) - return 0; - int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; unsigned char lightValue = this->light[blockNumber / 2]; return (blockNumber % 2 == 0) ? (lightValue & 0xF) : (lightValue >> 4); @@ -109,10 +101,7 @@ unsigned char Section::GetBlockLight(Vector pos) const unsigned char Section::GetBlockSkyLight(Vector pos) const { - if (sky.empty()) - return 0; - - int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; + int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; unsigned char skyValue = this->sky[blockNumber / 2]; return (blockNumber % 2 == 0) ? (skyValue & 0xF) : (skyValue >> 4); } diff --git a/src/Section.hpp b/src/Section.hpp index bc8b70b..b85eb43 100644 --- a/src/Section.hpp +++ b/src/Section.hpp @@ -8,8 +8,8 @@ class Section { std::vector block; - std::vector light; - std::vector sky; + unsigned char light[2048] = {}; + unsigned char sky[2048] = {}; unsigned char bitsPerBlock = 0; std::vector palette; @@ -20,7 +20,7 @@ class Section { std::map overrideList; public: - Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, std::vector lightData, std::vector skyData); + Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, const std::vector &lightData, const std::vector &skyData); Section() = default; diff --git a/src/World.cpp b/src/World.cpp index f9eb560..76598e3 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -421,7 +421,7 @@ unsigned char World::GetBlockLight(Vector pos) unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) { - const Vector directions[] = { + static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), @@ -482,7 +482,7 @@ unsigned char World::GetBlockSkyLight(Vector pos) unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) { - const Vector directions[] = { + static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), -- cgit v1.2.3