From b0f5f80ec4f80d606aeedfe54aa71437ea882247 Mon Sep 17 00:00:00 2001 From: UIS Date: Mon, 14 Dec 2020 21:33:21 +0300 Subject: [PATCH] Optimize and fix Section.cpp --- cwd/assets/altcraft/code/lua/init.lua | 13 ------------- src/ModLoader.cpp | 6 ++++++ src/Platform.hpp | 21 +++++++++------------ src/RendererSection.cpp | 9 +++++++-- src/RendererSection.hpp | 6 +++--- src/Section.cpp | 7 ++++--- 6 files changed, 29 insertions(+), 33 deletions(-) diff --git a/cwd/assets/altcraft/code/lua/init.lua b/cwd/assets/altcraft/code/lua/init.lua index 5ef73105..b72b9013 100644 --- a/cwd/assets/altcraft/code/lua/init.lua +++ b/cwd/assets/altcraft/code/lua/init.lua @@ -19,19 +19,6 @@ function plugin.onUnload () AC.LogInfo("AC Core unloaded") end -function plugin.onTick (deltaTime) - if AC.GetGameState() and AC.GetGameState():GetPlayer() and AC.GetGameState():GetTimeStatus().worldAge > 0 then - -- local player = AC.GetGameState():GetPlayer() - -- player.pos.x = player.pos.x + deltaTime * 0.5 - - -- local playerPos = AC.GetGameState():GetPlayer().pos - -- local wrld = AC.GetGameState():GetWorld() - -- playerPosV = Vector.new(playerPos.x, playerPos.y - 1, playerPos.z) - -- bid = wrld:GetBlockId(playerPosV) - -- print(bid.id..":"..bid.state) - end -end - local blocks = require("altcraft/blocks") blocks.RegisterBlocks() diff --git a/src/ModLoader.cpp b/src/ModLoader.cpp index 9fb13781..e5bcc488 100644 --- a/src/ModLoader.cpp +++ b/src/ModLoader.cpp @@ -117,6 +117,9 @@ void ModLoader::LoadModinfo(AssetTreeNode &node) noexcept { } LOG(INFO) << "Module " << (mod->name.empty() ? mod->modid : mod->name) << " loaded"; + + node.data.clear(); + node.data.shrink_to_fit(); } void ModLoader::LoadMcmeta(AssetTreeNode &node) { @@ -138,6 +141,9 @@ void ModLoader::LoadMcmeta(AssetTreeNode &node) { existing->description = pack["description"]; mods.push_back(existing); } + + node.data.clear(); + node.data.shrink_to_fit(); } void ModLoader::ParseAssetTexture(AssetTreeNode &node) noexcept { diff --git a/src/Platform.hpp b/src/Platform.hpp index 0d51a5c4..2ec2a0f2 100644 --- a/src/Platform.hpp +++ b/src/Platform.hpp @@ -33,12 +33,21 @@ # ifdef __GNUC__ # define AC_API __attribute__((visibility("default"))) # define AC_INTERNAL __attribute__((visibility("internal"))) +# define bswap_64(x) __builtin_bswap64(x) # else # define AC_API # define AC_INTERNAL # endif #endif +#if defined(_WIN32)&&defined(_MSC_VER) +# include +# define bswap_64(x) _byteswap_uint64(x) +#elif !defined(__GNUC__) +# define mv64(x, i) ((x&((uint64_t)0xFF<<(i*8)))>>(i*8)<<((7-i)*8)) +# define bswap_64(x) ( mv64(x, 0) | mv64(x, 1) | mv64(x, 2) | mv64(x, 3) | mv64(x, 4) | mv64(x, 5) | mv64(x, 6) | mv64(x, 7)) +#endif + #ifdef __linux__ # include # undef AC_THREAD_SET_NAME @@ -58,15 +67,3 @@ #define floorASR(value, shmat, dest) (dest=std::floor(value / (float)(2 << shmat))) #define floorASRQ(value, shmat, dest) (dest=std::floor(value / (float)(2 << shmat))) #endif - -#ifndef __GNUC__ -# ifndef WIN32 -# error This does not works -# define mv64(x, i) (x&(0xFF<<(i*8)))>>((7-i)*8) -# define bswap_64(x) ( mv64(x, 0) | mv64(x, 1) | mv64(x, 2) | mv64(x, 3) | mv64(x, 4) | mv64(x, 5) | mv64(x, 6) | mv64(x, 7)) -# else -# define bswap_64(x) _byteswap_uint64(x) -# endif -#else -# include -#endif diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index ede653fe..ba82bb3e 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -17,7 +17,7 @@ static const GLfloat uv_coords[] = { GLuint RendererSection::VboUvs = 0; -RendererSection::RendererSection(const RendererSectionData &data) { +RendererSection::RendererSection(RendererSectionData &data) { OPTICK_EVENT(); if (!VboUvs) { glGenBuffers(1, &VboUvs); @@ -122,7 +122,7 @@ size_t RendererSection::GetHash() { return hash; } -void RendererSection::UpdateData(const RendererSectionData & data) { +void RendererSection::UpdateData(RendererSectionData & data) { OPTICK_EVENT(); numOfFaces = data.verts.size()/4; @@ -157,4 +157,9 @@ void RendererSection::UpdateData(const RendererSectionData & data) { glBindBuffer(GL_ARRAY_BUFFER, 0); glCheckError(); + + data.quadInfo.clear(); + data.quadInfo.shrink_to_fit(); + data.verts.clear(); + data.verts.shrink_to_fit(); } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 4fd0c89e..bfc5b5ee 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -23,9 +23,9 @@ class RendererSection { size_t hash; Vector sectionPos; - RendererSection(const RendererSection &other) = delete; + RendererSection(RendererSection &other) = delete; public: - RendererSection(const RendererSectionData &data); + RendererSection(RendererSectionData &data); RendererSection(RendererSection &&other); @@ -41,5 +41,5 @@ class RendererSection { friend void swap(RendererSection &lhs, RendererSection &rhs); - void UpdateData(const RendererSectionData &data); + void UpdateData(RendererSectionData &data); }; diff --git a/src/Section.cpp b/src/Section.cpp index 3305bb95..1483a66a 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -216,16 +216,17 @@ void Section::ExpandBPB() noexcept { size_t sz = (16*16*16/2) << (this->pow - 1); uint16_t *expanded = reinterpret_cast(malloc(sz)); - if (this->pow == 2) {//Expand to 8 bPB + if (this->pow == 2) {//Expand to 8 BpB from 4 uint8_t *old = reinterpret_cast(blocks); uint8_t *new_blocks = reinterpret_cast(expanded); for (size_t i = 0; i < 16*16*16; i++) { uint8_t nibble = i&1; new_blocks[i] = (old[i>>1] >> (nibble << 2)) & 0x0F; } - } else if (this->pow == 3) {//Expand to 16 bPB + } else if (this->pow == 3) {//Expand to 16 BpB from 8 + uint8_t *old = reinterpret_cast(blocks); for (size_t i = 0; i < 16*16*16; i++) { - expanded[i] = palette[blocks[i]]; + expanded[i] = palette[old[i]]; } }