From 88c48e8107acf8c05d9dbd7381445e41376af347 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 5 Mar 2021 09:23:37 +0100 Subject: [PATCH] Add dynamic allocation alignment to be able to load T6 MemoryBlock asset which isnt used at all so why am i doing this again --- src/Common/Game/T6/T6_Assets.h | 8 ++++- src/ZoneCode/Game/T6/T6_Commands.txt | 1 + src/ZoneCode/Game/T6/XAssets/MemoryBlock.txt | 17 +++++++++- .../Domain/Information/MemberInformation.h | 1 + .../Generating/Templates/ZoneLoadTemplate.cpp | 9 ++++- .../Parsing/Commands/Impl/CommandsParser.cpp | 2 ++ .../Commands/Sequence/SequenceAllocAlign.cpp | 34 +++++++++++++++++++ .../Commands/Sequence/SequenceAllocAlign.h | 14 ++++++++ src/ZoneLoading/Game/T6/ContentLoaderT6.cpp | 4 +-- .../Zone/Stream/IZoneInputStream.h | 4 +-- .../Zone/Stream/Impl/XBlockInputStream.cpp | 4 +-- .../Zone/Stream/Impl/XBlockInputStream.h | 4 +-- 12 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.cpp create mode 100644 src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.h diff --git a/src/Common/Game/T6/T6_Assets.h b/src/Common/Game/T6/T6_Assets.h index 6c8b25d1..8f476c03 100644 --- a/src/Common/Game/T6/T6_Assets.h +++ b/src/Common/Game/T6/T6_Assets.h @@ -2422,6 +2422,12 @@ namespace T6 int customBool2; }; + union MemoryBlockData + { + char* mainData; + char* videoData; + char* streamData; + }; struct MemoryBlock { @@ -2431,7 +2437,7 @@ namespace T6 bool streamMem; unsigned int size; unsigned int alignment; - char* data; + MemoryBlockData data; }; struct cmodel_t2 diff --git a/src/ZoneCode/Game/T6/T6_Commands.txt b/src/ZoneCode/Game/T6/T6_Commands.txt index c1268254..3bd115d1 100644 --- a/src/ZoneCode/Game/T6/T6_Commands.txt +++ b/src/ZoneCode/Game/T6/T6_Commands.txt @@ -60,6 +60,7 @@ block delay XFILE_BLOCK_DELAY_VIRTUAL default; block delay XFILE_BLOCK_DELAY_PHYSICAL; block normal XFILE_BLOCK_VIRTUAL default; block normal XFILE_BLOCK_PHYSICAL; +block normal XFILE_BLOCK_STREAMER_RESERVE; // Asset commands #include "XAssets/PhysPreset.txt" diff --git a/src/ZoneCode/Game/T6/XAssets/MemoryBlock.txt b/src/ZoneCode/Game/T6/XAssets/MemoryBlock.txt index 5430863a..ea3e104b 100644 --- a/src/ZoneCode/Game/T6/XAssets/MemoryBlock.txt +++ b/src/ZoneCode/Game/T6/XAssets/MemoryBlock.txt @@ -3,4 +3,19 @@ // ========================================= use MemoryBlock; set string name; -set name name; \ No newline at end of file +set name name; + +set condition data::mainData mainMem; +set count data::mainData size; +set allocalign data::mainData alignment; +set block data::mainData XFILE_BLOCK_RUNTIME_VIRTUAL; + +set condition data::videoData videoMem; +set count data::videoData size; +set allocalign data::videoData alignment; +set block data::videoData XFILE_BLOCK_RUNTIME_PHYSICAL; + +set condition data::streamData streamMem; +set count data::streamData size; +set allocalign data::streamData alignment; +set block data::streamData XFILE_BLOCK_STREAMER_RESERVE; \ No newline at end of file diff --git a/src/ZoneCodeGeneratorLib/Domain/Information/MemberInformation.h b/src/ZoneCodeGeneratorLib/Domain/Information/MemberInformation.h index 2c876787..857291ae 100644 --- a/src/ZoneCodeGeneratorLib/Domain/Information/MemberInformation.h +++ b/src/ZoneCodeGeneratorLib/Domain/Information/MemberInformation.h @@ -20,6 +20,7 @@ public: bool m_is_reusable; bool m_is_leaf; std::unique_ptr m_condition; + std::unique_ptr m_alloc_alignment; const FastFileBlock* m_fast_file_block; MemberInformation(StructureInformation* parent, StructureInformation* type, Variable* member); diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp index cf75a4f6..c60e85e9 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp @@ -554,7 +554,14 @@ class ZoneLoadTemplate::Internal final : BaseTemplate // (Alignment specified via `__declspec(align())` showing as correct via intellisense but is incorrect when compiled for types that have a larger alignment than the specified value) // this was changed to make ZoneCodeGenerator calculate what is supposed to be used as alignment when allocating. // This is more reliable when being used with different compilers and the value used can be seen in the source code directly - LINE(MakeMemberAccess(info, member, modifier)<<" = m_stream->Alloc<"<("<m_alloc_alignment) + { + LINE(MakeMemberAccess(info, member, modifier)<<" = m_stream->Alloc<"<("<m_alloc_alignment.get())<<");") + } + else + { + LINE(MakeMemberAccess(info, member, modifier)<<" = m_stream->Alloc<"<("<& CommandsParser::GetTestsForState { static std::vector tests({ new SequenceAction(), + new SequenceAllocAlign(), new SequenceArchitecture(), new SequenceArrayCount(), new SequenceArraySize(), diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.cpp new file mode 100644 index 00000000..52e36253 --- /dev/null +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.cpp @@ -0,0 +1,34 @@ +#include "SequenceAllocAlign.h" + +#include "Parsing/Commands/Matcher/CommandsMatcherFactory.h" +#include "Parsing/Commands/Matcher/CommandsCommonMatchers.h" + +SequenceAllocAlign::SequenceAllocAlign() +{ + const CommandsMatcherFactory create(this); + + AddLabeledMatchers(CommandsCommonMatchers::Typename(this), CommandsCommonMatchers::LABEL_TYPENAME); + AddLabeledMatchers(CommandsCommonMatchers::Evaluation(this), CommandsCommonMatchers::LABEL_EVALUATION); + AddMatchers({ + create.Keyword("set"), + create.Keyword("allocalign"), + create.Label(CommandsCommonMatchers::LABEL_TYPENAME).Capture(CAPTURE_TYPE), + create.Label(CommandsCommonMatchers::LABEL_EVALUATION), + create.Char(';') + }); +} + +void SequenceAllocAlign::ProcessMatch(CommandsParserState* state, SequenceResult& result) const +{ + const auto& typeNameToken = result.NextCapture(CAPTURE_TYPE); + StructureInformation* type; + std::vector memberChain; + if (!state->GetTypenameAndMembersFromTypename(typeNameToken.TypeNameValue(), type, memberChain)) + throw ParsingException(typeNameToken.GetPos(), "Unknown type"); + + if(memberChain.empty()) + throw ParsingException(typeNameToken.GetPos(), "Need to specify a member"); + + auto allocAlignEvaluation = CommandsCommonMatchers::ProcessEvaluation(state, result, type); + memberChain.back()->m_alloc_alignment = std::move(allocAlignEvaluation); +} diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.h b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.h new file mode 100644 index 00000000..d4bc00d8 --- /dev/null +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceAllocAlign.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Parsing/Commands/Impl/CommandsParser.h" + +class SequenceAllocAlign final : public CommandsParser::sequence_t +{ + static constexpr auto CAPTURE_TYPE = 1; + +protected: + void ProcessMatch(CommandsParserState* state, SequenceResult& result) const override; + +public: + SequenceAllocAlign(); +}; diff --git a/src/ZoneLoading/Game/T6/ContentLoaderT6.cpp b/src/ZoneLoading/Game/T6/ContentLoaderT6.cpp index 4d28f06b..6ca7cc74 100644 --- a/src/ZoneLoading/Game/T6/ContentLoaderT6.cpp +++ b/src/ZoneLoading/Game/T6/ContentLoaderT6.cpp @@ -28,7 +28,7 @@ #include "Game/T6/XAssets/mapents/mapents_load_db.h" #include "Game/T6/XAssets/material/material_load_db.h" #include "Game/T6/XAssets/materialtechniqueset/materialtechniqueset_load_db.h" -//#include "Game/T6/XAssets/memoryblock/memoryblock_load_db.h" +#include "Game/T6/XAssets/memoryblock/memoryblock_load_db.h" #include "Game/T6/XAssets/menudef_t/menudef_t_load_db.h" #include "Game/T6/XAssets/menulist/menulist_load_db.h" #include "Game/T6/XAssets/physconstraints/physconstraints_load_db.h" @@ -153,7 +153,7 @@ void ContentLoader::LoadXAsset(const bool atStreamStart) LOAD_ASSET(ASSET_TYPE_SCRIPTPARSETREE, ScriptParseTree, scriptParseTree) LOAD_ASSET(ASSET_TYPE_KEYVALUEPAIRS, KeyValuePairs, keyValuePairs) LOAD_ASSET(ASSET_TYPE_VEHICLEDEF, VehicleDef, vehicleDef) - // LOAD_ASSET(ASSET_TYPE_MEMORYBLOCK, MemoryBlock, memoryBlock); + LOAD_ASSET(ASSET_TYPE_MEMORYBLOCK, MemoryBlock, memoryBlock); LOAD_ASSET(ASSET_TYPE_ADDON_MAP_ENTS, AddonMapEnts, addonMapEnts) LOAD_ASSET(ASSET_TYPE_TRACER, TracerDef, tracerDef) LOAD_ASSET(ASSET_TYPE_SKINNEDVERTS, SkinnedVertsDef, skinnedVertsDef) diff --git a/src/ZoneLoading/Zone/Stream/IZoneInputStream.h b/src/ZoneLoading/Zone/Stream/IZoneInputStream.h index 57c7886b..4738ec43 100644 --- a/src/ZoneLoading/Zone/Stream/IZoneInputStream.h +++ b/src/ZoneLoading/Zone/Stream/IZoneInputStream.h @@ -8,10 +8,10 @@ class IZoneInputStream : public IZoneStream { public: - virtual void* Alloc(int align) = 0; + virtual void* Alloc(unsigned align) = 0; template - T* Alloc(const int align) + T* Alloc(const unsigned align) { return static_cast(Alloc(align)); } diff --git a/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.cpp b/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.cpp index c4b12d9f..a601e479 100644 --- a/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.cpp +++ b/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.cpp @@ -30,7 +30,7 @@ XBlockInputStream::~XBlockInputStream() assert(m_block_stack.empty()); } -void XBlockInputStream::Align(const int align) +void XBlockInputStream::Align(const unsigned align) { assert(!m_block_stack.empty()); @@ -78,7 +78,7 @@ block_t XBlockInputStream::PopBlock() return poppedBlock->m_index; } -void* XBlockInputStream::Alloc(const int align) +void* XBlockInputStream::Alloc(const unsigned align) { assert(!m_block_stack.empty()); diff --git a/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.h b/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.h index 577c247d..1c6c8d36 100644 --- a/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.h +++ b/src/ZoneLoading/Zone/Stream/Impl/XBlockInputStream.h @@ -19,7 +19,7 @@ class XBlockInputStream final : public IZoneInputStream int m_block_bit_count; XBlock* m_insert_block; - void Align(int align); + void Align(unsigned align); public: XBlockInputStream(std::vector& blocks, ILoadingStream* stream, int blockBitCount, block_t insertBlock); @@ -28,7 +28,7 @@ public: void PushBlock(block_t block) override; block_t PopBlock() override; - void* Alloc(int align) override; + void* Alloc(unsigned align) override; void LoadDataRaw(void* dst, size_t size) override; void LoadDataInBlock(void* dst, size_t size) override;