From 3eedbd3c3b9475505c6d679214e22cd1083f7419 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Wed, 21 Feb 2018 21:23:15 -1000 Subject: [PATCH] Template-based DNA refactor --- hecl/extern/athena | 2 +- hecl/include/hecl/Backend/GX.hpp | 19 +++++---- hecl/include/hecl/CVar.hpp | 8 ++-- hecl/include/hecl/Frontend.hpp | 20 ++++----- hecl/include/hecl/HMDLMeta.hpp | 4 +- hecl/include/hecl/Runtime.hpp | 4 +- hecl/lib/Backend/GX.cpp | 11 ++++- hecl/lib/Frontend/Frontend.cpp | 71 +++++++------------------------- 8 files changed, 52 insertions(+), 87 deletions(-) diff --git a/hecl/extern/athena b/hecl/extern/athena index 42b97e030..62b6d6792 160000 --- a/hecl/extern/athena +++ b/hecl/extern/athena @@ -1 +1 @@ -Subproject commit 42b97e03064ad0dc37c5057a1f802e8c3c9190d4 +Subproject commit 62b6d6792a04792587f19d8fdf4c34d251fcfce0 diff --git a/hecl/include/hecl/Backend/GX.hpp b/hecl/include/hecl/Backend/GX.hpp index b8a57a175..6006d436a 100644 --- a/hecl/include/hecl/Backend/GX.hpp +++ b/hecl/include/hecl/Backend/GX.hpp @@ -408,7 +408,7 @@ struct GX : IBackend BlendFactor m_blendSrc; BlendFactor m_blendDst; - struct Color : athena::io::DNA + struct Color : athena::io::DNA { union { @@ -440,6 +440,15 @@ struct GX : IBackend color[3] = val; return *this; } + atVec4f toVec4f() const + { + atVec4f out; + out.vec[0] = color[0] / 255.f; + out.vec[1] = color[1] / 255.f; + out.vec[2] = color[2] / 255.f; + out.vec[3] = color[3] / 255.f; + return out; + } Color(const atVec4f& vec) {*this = vec;} Color(const atVec3f& vec) {*this = vec;} Color(uint8_t val) {*this = val;} @@ -447,13 +456,7 @@ struct GX : IBackend bool operator!=(const Color& other) const {return num != other.num;} uint8_t operator[](size_t idx) const {return color[idx];} uint8_t& operator[](size_t idx) {return color[idx];} - - void read(athena::io::IStreamReader& reader) - {reader.readUBytesToBuf(&num, 4);} - void write(athena::io::IStreamWriter& writer) const - {writer.writeUBytes(reinterpret_cast(&num), 4);} - size_t binarySize(size_t __isz) const - {return __isz + 4;} + AT_DECL_EXPLICIT_DNA }; unsigned m_kcolorCount = 0; Color m_kcolors[4]; diff --git a/hecl/include/hecl/CVar.hpp b/hecl/include/hecl/CVar.hpp index 2c1d49c31..7d7acd42a 100755 --- a/hecl/include/hecl/CVar.hpp +++ b/hecl/include/hecl/CVar.hpp @@ -36,17 +36,17 @@ enum EFlags }; ENABLE_BITWISE_ENUM(EFlags) -class CVar : public athena::io::DNAYaml +class CVar : public athena::io::DNA { public: - DECL_YAML + AT_DECL_DNA String<-1> m_name; String<-1> m_value; }; -struct CVarContainer : public athena::io::DNAYaml +struct CVarContainer : public athena::io::DNA { - DECL_YAML + AT_DECL_DNA Value magic = 'CVAR'; Value cvarCount; Vector cvars; diff --git a/hecl/include/hecl/Frontend.hpp b/hecl/include/hecl/Frontend.hpp index eda4aa0ce..b2e22bc2a 100644 --- a/hecl/include/hecl/Frontend.hpp +++ b/hecl/include/hecl/Frontend.hpp @@ -283,11 +283,11 @@ public: std::list parse(); }; -using BigDNA = athena::io::DNA; +using BigDNA = athena::io::DNA; struct IR : BigDNA { - Delete _d; + AT_DECL_EXPLICIT_DNA enum OpType : uint8_t { @@ -302,7 +302,7 @@ struct IR : BigDNA struct Instruction : BigDNA { - Delete _d; + AT_DECL_EXPLICIT_DNA OpType m_op = OpType::None; RegID m_target = RegID(-1); @@ -310,7 +310,7 @@ struct IR : BigDNA struct Call : BigDNA { - DECL_DNA + AT_DECL_DNA String<-1> m_name; Value m_argInstCount; Vector m_argInstIdxs; @@ -318,7 +318,7 @@ struct IR : BigDNA struct LoadImm : BigDNA { - DECL_DNA + AT_DECL_DNA Value m_immVec = {}; } m_loadImm; @@ -333,14 +333,14 @@ struct IR : BigDNA struct Arithmetic : BigDNA { - DECL_DNA + AT_DECL_DNA Value m_op = ArithmeticOpType::None; Value m_instIdxs[2]; } m_arithmetic; struct Swizzle : BigDNA { - DECL_DNA + AT_DECL_DNA Value m_idxs[4] = {-1, -1, -1, -1}; Value m_instIdx; } m_swizzle; @@ -350,9 +350,6 @@ struct IR : BigDNA int getChildCount() const; const IR::Instruction& getChildInst(const IR& ir, size_t idx) const; const atVec4f& getImmVec() const; - void read(athena::io::IStreamReader& reader); - void write(athena::io::IStreamWriter& writer) const; - size_t binarySize(size_t sz) const; Instruction(athena::io::IStreamReader& reader) {read(reader);} }; @@ -367,9 +364,6 @@ struct IR : BigDNA static atInt8 swizzleCompIdx(char aChar); int addInstruction(const IRNode& n, IR::RegID target); - void read(athena::io::IStreamReader& reader); - void write(athena::io::IStreamWriter& writer) const; - size_t binarySize(size_t sz) const; }; class Frontend diff --git a/hecl/include/hecl/HMDLMeta.hpp b/hecl/include/hecl/HMDLMeta.hpp index 66db1ba4d..35fe22bd4 100644 --- a/hecl/include/hecl/HMDLMeta.hpp +++ b/hecl/include/hecl/HMDLMeta.hpp @@ -15,9 +15,9 @@ enum class HMDLTopology : atUint32 #define HECL_HMDL_META_SZ 32 -struct HMDLMeta : athena::io::DNA +struct HMDLMeta : athena::io::DNA { - DECL_DNA + AT_DECL_DNA Value magic = 'TACO'; Value topology; Value vertStride; diff --git a/hecl/include/hecl/Runtime.hpp b/hecl/include/hecl/Runtime.hpp index e40130859..7d478be7f 100644 --- a/hecl/include/hecl/Runtime.hpp +++ b/hecl/include/hecl/Runtime.hpp @@ -238,9 +238,9 @@ class ShaderCacheManager athena::io::FileReader m_idxFr; athena::io::FileReader m_datFr; hecl::Frontend::Frontend FE; - struct IndexEntry : athena::io::DNA + struct IndexEntry : athena::io::DNA { - DECL_DNA + AT_DECL_DNA Value m_hash; Value m_meta; Value m_compOffset; diff --git a/hecl/lib/Backend/GX.cpp b/hecl/lib/Backend/GX.cpp index 59649c0f1..7f00ce22f 100644 --- a/hecl/lib/Backend/GX.cpp +++ b/hecl/lib/Backend/GX.cpp @@ -1,9 +1,18 @@ #include "hecl/Backend/GX.hpp" -#include namespace hecl::Backend { +template <> +void GX::Color::Enumerate::Read>(typename Read::StreamT& reader) +{ reader.readUBytesToBuf(&num, 4); } +template <> +void GX::Color::Enumerate::Write>(typename Write::StreamT& writer) +{ writer.writeUBytes(reinterpret_cast(&num), 4); } +template <> +void GX::Color::Enumerate::BinarySize>(typename BinarySize::StreamT& s) +{ s += 4; } + unsigned GX::addKColor(Diagnostics& diag, const SourceLocation& loc, const Color& color) { for (unsigned i=0 ; i +void IR::Instruction::Enumerate(typename Op::StreamT& s) { - m_op = OpType(reader.readUByte()); - m_target = reader.readUint16Big(); + Do({"op"}, m_op, s); + Do({"target"}, m_target, s); switch (m_op) { default: break; case OpType::Call: - m_call.read(reader); + Do({"call"}, m_call, s); break; case OpType::LoadImm: - m_loadImm.read(reader); + Do({"loadImm"}, m_loadImm, s); break; case OpType::Arithmetic: - m_arithmetic.read(reader); + Do({"arithmetic"}, m_arithmetic, s); break; case OpType::Swizzle: - m_swizzle.read(reader); + Do({"swizzle"}, m_swizzle, s); break; } } -void IR::Instruction::write(athena::io::IStreamWriter& writer) const -{ - writer.writeUByte(m_op); - writer.writeUint16Big(m_target); - switch (m_op) - { - default: break; - case OpType::Call: - m_call.write(writer); - break; - case OpType::LoadImm: - m_loadImm.write(writer); - break; - case OpType::Arithmetic: - m_arithmetic.write(writer); - break; - case OpType::Swizzle: - m_swizzle.write(writer); - break; - } -} - -size_t IR::Instruction::binarySize(size_t sz) const -{ - sz += 3; - switch (m_op) - { - default: break; - case OpType::Call: - sz = m_call.binarySize(sz); - break; - case OpType::LoadImm: - sz = m_loadImm.binarySize(sz); - break; - case OpType::Arithmetic: - sz = m_arithmetic.binarySize(sz); - break; - case OpType::Swizzle: - sz = m_swizzle.binarySize(sz); - break; - } - return sz; -} - atInt8 IR::swizzleCompIdx(char aChar) { switch (aChar) @@ -229,7 +186,8 @@ int IR::addInstruction(const IRNode& n, IR::RegID target) } } -void IR::read(athena::io::IStreamReader& reader) +template <> +void IR::Enumerate(typename Read::StreamT& reader) { m_hash = reader.readUint64Big(); m_regCount = reader.readUint16Big(); @@ -261,7 +219,8 @@ void IR::read(athena::io::IStreamReader& reader) } } -void IR::write(athena::io::IStreamWriter& writer) const +template <> +void IR::Enumerate(typename Write::StreamT& writer) { writer.writeUint64Big(m_hash); writer.writeUint16Big(m_regCount); @@ -270,12 +229,12 @@ void IR::write(athena::io::IStreamWriter& writer) const inst.write(writer); } -size_t IR::binarySize(size_t sz) const +template <> +void IR::Enumerate(typename BinarySize::StreamT& sz) { sz += 12; for (const Instruction& inst : m_instructions) - sz = inst.binarySize(sz); - return sz; + inst.binarySize(sz); } IR Frontend::compileSource(std::string_view source, std::string_view diagName)