From 3338b007b40ba3182b9a66dbf6421d25ce0b0f3c Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 4 Jun 2025 18:08:23 +0100 Subject: [PATCH] fix: doing wrong alignment on dynamic filling --- .../Generating/Templates/ZoneLoadTemplate.cpp | 20 +++++++++++------- src/ZoneLoading/Game/IW4/ContentLoaderIW4.cpp | 4 ++++ .../Zone/Stream/ZoneInputStream.cpp | 21 +++++++++++++++++++ src/ZoneLoading/Zone/Stream/ZoneInputStream.h | 6 ++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp index bd72e5ea..ad073a2b 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp @@ -901,8 +901,10 @@ namespace { if (info && !info->m_has_matching_cross_platform_structure && StructureComputations(info).GetDynamicMember()) { + LINE("// Alloc first for alignment, then proceed to read as game does") + LINEF("m_stream.Alloc({0});", def->GetAlignment()) LINEF("const auto allocSize = LoadDynamicFill_{0}(m_stream.LoadWithFill(0));", MakeSafeTypeName(def)) - LINEF("*{0} = static_cast<{1}*>(m_stream.AllocOutOfBlock({2}, allocSize));", MakeTypePtrVarName(def), def->GetFullName(), def->GetAlignment()) + LINEF("*{0} = static_cast<{1}*>(m_stream.AllocOutOfBlock(0, allocSize));", MakeTypePtrVarName(def), def->GetFullName()) } else { @@ -1462,19 +1464,23 @@ namespace if (preAllocDynamic) { - LINEF("const auto allocSize = LoadDynamicFill_{0}(m_stream.LoadWithFill(0));", MakeSafeTypeName(member->m_type->m_definition)) - LINE_STARTF("{0} = static_cast<{1}{2}*>(m_stream.AllocOutOfBlock(", MakeMemberAccess(info, member, modifier), typeDecl, followingReferences) - + LINE("// Alloc first for alignment, then proceed to read as game does") if (member->m_alloc_alignment) { - LINE_MIDDLE(MakeEvaluation(member->m_alloc_alignment.get())) + LINEF("m_stream.Alloc({0});", MakeEvaluation(member->m_alloc_alignment.get())) } else { - LINE_MIDDLEF("{0}", modifier.GetAlignment()) + LINEF("m_stream.Alloc({0});", modifier.GetAlignment()) } - LINE_ENDF(", allocSize));", member->m_type->m_definition->GetFullName()) + LINEF("const auto allocSize = LoadDynamicFill_{0}(m_stream.LoadWithFill(0));", MakeSafeTypeName(member->m_type->m_definition)) + + // We do not align again, because we already did previously + LINEF("{0} = static_cast<{1}{2}*>(m_stream.AllocOutOfBlock(0, allocSize));", + MakeMemberAccess(info, member, modifier), + typeDecl, + followingReferences) } else { diff --git a/src/ZoneLoading/Game/IW4/ContentLoaderIW4.cpp b/src/ZoneLoading/Game/IW4/ContentLoaderIW4.cpp index 19c9db93..ea48f387 100644 --- a/src/ZoneLoading/Game/IW4/ContentLoaderIW4.cpp +++ b/src/ZoneLoading/Game/IW4/ContentLoaderIW4.cpp @@ -164,6 +164,10 @@ void ContentLoader::LoadXAssetArray(const bool atStreamStart, const size_t count { LoadXAsset(false); varXAsset++; + +#ifdef DEBUG_OFFSETS + m_stream.DebugOffsets(index); +#endif } } diff --git a/src/ZoneLoading/Zone/Stream/ZoneInputStream.cpp b/src/ZoneLoading/Zone/Stream/ZoneInputStream.cpp index db5ec3ac..503b474d 100644 --- a/src/ZoneLoading/Zone/Stream/ZoneInputStream.cpp +++ b/src/ZoneLoading/Zone/Stream/ZoneInputStream.cpp @@ -9,6 +9,8 @@ #include #include +#include +#include #include ZoneStreamFillReadAccessor::ZoneStreamFillReadAccessor( @@ -433,6 +435,25 @@ namespace return *m_pointer_redirect_lookup[redirectIndex]; } +#ifdef DEBUG_OFFSETS + void DebugOffsets(const size_t assetIndex) const override + { + std::ostringstream ss; + + ss << "Asset " << assetIndex; + for (const auto& block : m_blocks) + { + if (block->m_type != XBlockType::BLOCK_TYPE_NORMAL) + continue; + + ss << " " << m_block_offsets[block->m_index]; + } + + ss << "\n"; + std::cout << ss.str(); + } +#endif + private: void LoadDataFromBlock(const XBlock& block, void* dst, const size_t size) { diff --git a/src/ZoneLoading/Zone/Stream/ZoneInputStream.h b/src/ZoneLoading/Zone/Stream/ZoneInputStream.h index 4f22c665..5e7a5fb6 100644 --- a/src/ZoneLoading/Zone/Stream/ZoneInputStream.h +++ b/src/ZoneLoading/Zone/Stream/ZoneInputStream.h @@ -11,6 +11,8 @@ #include #include +#define DEBUG_OFFSETS 1 + class ZoneStreamFillReadAccessor { public: @@ -170,6 +172,10 @@ public: return static_cast(ConvertOffsetToAliasLookup(static_cast(offset))); } +#ifdef DEBUG_OFFSETS + virtual void DebugOffsets(size_t assetIndex) const = 0; +#endif + static std::unique_ptr Create( unsigned pointerBitCount, unsigned blockBitCount, std::vector& blocks, block_t insertBlock, ILoadingStream& stream, MemoryManager& memory); };