Template-based DNA refactor

This commit is contained in:
Jack Andersen 2018-02-21 21:23:15 -10:00
parent d5f2e4a349
commit 3eedbd3c3b
8 changed files with 52 additions and 87 deletions

2
hecl/extern/athena vendored

@ -1 +1 @@
Subproject commit 42b97e03064ad0dc37c5057a1f802e8c3c9190d4 Subproject commit 62b6d6792a04792587f19d8fdf4c34d251fcfce0

View File

@ -408,7 +408,7 @@ struct GX : IBackend
BlendFactor m_blendSrc; BlendFactor m_blendSrc;
BlendFactor m_blendDst; BlendFactor m_blendDst;
struct Color : athena::io::DNA<athena::BigEndian> struct Color : athena::io::DNA<athena::Big>
{ {
union union
{ {
@ -440,6 +440,15 @@ struct GX : IBackend
color[3] = val; color[3] = val;
return *this; 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 atVec4f& vec) {*this = vec;}
Color(const atVec3f& vec) {*this = vec;} Color(const atVec3f& vec) {*this = vec;}
Color(uint8_t val) {*this = val;} Color(uint8_t val) {*this = val;}
@ -447,13 +456,7 @@ struct GX : IBackend
bool operator!=(const Color& other) const {return num != other.num;} 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) const {return color[idx];}
uint8_t& operator[](size_t idx) {return color[idx];} uint8_t& operator[](size_t idx) {return color[idx];}
AT_DECL_EXPLICIT_DNA
void read(athena::io::IStreamReader& reader)
{reader.readUBytesToBuf(&num, 4);}
void write(athena::io::IStreamWriter& writer) const
{writer.writeUBytes(reinterpret_cast<const atUint8*>(&num), 4);}
size_t binarySize(size_t __isz) const
{return __isz + 4;}
}; };
unsigned m_kcolorCount = 0; unsigned m_kcolorCount = 0;
Color m_kcolors[4]; Color m_kcolors[4];

View File

@ -36,17 +36,17 @@ enum EFlags
}; };
ENABLE_BITWISE_ENUM(EFlags) ENABLE_BITWISE_ENUM(EFlags)
class CVar : public athena::io::DNAYaml<athena::BigEndian> class CVar : public athena::io::DNA<athena::Big>
{ {
public: public:
DECL_YAML AT_DECL_DNA
String<-1> m_name; String<-1> m_name;
String<-1> m_value; String<-1> m_value;
}; };
struct CVarContainer : public athena::io::DNAYaml<athena::BigEndian> struct CVarContainer : public athena::io::DNA<athena::Big>
{ {
DECL_YAML AT_DECL_DNA
Value<atUint32> magic = 'CVAR'; Value<atUint32> magic = 'CVAR';
Value<atUint32> cvarCount; Value<atUint32> cvarCount;
Vector<CVar, DNA_COUNT(cvarCount)> cvars; Vector<CVar, DNA_COUNT(cvarCount)> cvars;

View File

@ -283,11 +283,11 @@ public:
std::list<IRNode> parse(); std::list<IRNode> parse();
}; };
using BigDNA = athena::io::DNA<athena::BigEndian>; using BigDNA = athena::io::DNA<athena::Big>;
struct IR : BigDNA struct IR : BigDNA
{ {
Delete _d; AT_DECL_EXPLICIT_DNA
enum OpType : uint8_t enum OpType : uint8_t
{ {
@ -302,7 +302,7 @@ struct IR : BigDNA
struct Instruction : BigDNA struct Instruction : BigDNA
{ {
Delete _d; AT_DECL_EXPLICIT_DNA
OpType m_op = OpType::None; OpType m_op = OpType::None;
RegID m_target = RegID(-1); RegID m_target = RegID(-1);
@ -310,7 +310,7 @@ struct IR : BigDNA
struct Call : BigDNA struct Call : BigDNA
{ {
DECL_DNA AT_DECL_DNA
String<-1> m_name; String<-1> m_name;
Value<atUint16> m_argInstCount; Value<atUint16> m_argInstCount;
Vector<atUint16, DNA_COUNT(m_argInstCount)> m_argInstIdxs; Vector<atUint16, DNA_COUNT(m_argInstCount)> m_argInstIdxs;
@ -318,7 +318,7 @@ struct IR : BigDNA
struct LoadImm : BigDNA struct LoadImm : BigDNA
{ {
DECL_DNA AT_DECL_DNA
Value<atVec4f> m_immVec = {}; Value<atVec4f> m_immVec = {};
} m_loadImm; } m_loadImm;
@ -333,14 +333,14 @@ struct IR : BigDNA
struct Arithmetic : BigDNA struct Arithmetic : BigDNA
{ {
DECL_DNA AT_DECL_DNA
Value<ArithmeticOpType> m_op = ArithmeticOpType::None; Value<ArithmeticOpType> m_op = ArithmeticOpType::None;
Value<atUint16> m_instIdxs[2]; Value<atUint16> m_instIdxs[2];
} m_arithmetic; } m_arithmetic;
struct Swizzle : BigDNA struct Swizzle : BigDNA
{ {
DECL_DNA AT_DECL_DNA
Value<atInt8> m_idxs[4] = {-1, -1, -1, -1}; Value<atInt8> m_idxs[4] = {-1, -1, -1, -1};
Value<atUint16> m_instIdx; Value<atUint16> m_instIdx;
} m_swizzle; } m_swizzle;
@ -350,9 +350,6 @@ struct IR : BigDNA
int getChildCount() const; int getChildCount() const;
const IR::Instruction& getChildInst(const IR& ir, size_t idx) const; const IR::Instruction& getChildInst(const IR& ir, size_t idx) const;
const atVec4f& getImmVec() 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);} Instruction(athena::io::IStreamReader& reader) {read(reader);}
}; };
@ -367,9 +364,6 @@ struct IR : BigDNA
static atInt8 swizzleCompIdx(char aChar); static atInt8 swizzleCompIdx(char aChar);
int addInstruction(const IRNode& n, IR::RegID target); 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 class Frontend

View File

@ -15,9 +15,9 @@ enum class HMDLTopology : atUint32
#define HECL_HMDL_META_SZ 32 #define HECL_HMDL_META_SZ 32
struct HMDLMeta : athena::io::DNA<athena::BigEndian> struct HMDLMeta : athena::io::DNA<athena::Big>
{ {
DECL_DNA AT_DECL_DNA
Value<atUint32> magic = 'TACO'; Value<atUint32> magic = 'TACO';
Value<HMDLTopology> topology; Value<HMDLTopology> topology;
Value<atUint32> vertStride; Value<atUint32> vertStride;

View File

@ -238,9 +238,9 @@ class ShaderCacheManager
athena::io::FileReader m_idxFr; athena::io::FileReader m_idxFr;
athena::io::FileReader m_datFr; athena::io::FileReader m_datFr;
hecl::Frontend::Frontend FE; hecl::Frontend::Frontend FE;
struct IndexEntry : athena::io::DNA<athena::BigEndian> struct IndexEntry : athena::io::DNA<athena::Big>
{ {
DECL_DNA AT_DECL_DNA
Value<atUint64> m_hash; Value<atUint64> m_hash;
Value<atUint64> m_meta; Value<atUint64> m_meta;
Value<atUint64> m_compOffset; Value<atUint64> m_compOffset;

View File

@ -1,9 +1,18 @@
#include "hecl/Backend/GX.hpp" #include "hecl/Backend/GX.hpp"
#include <map>
namespace hecl::Backend namespace hecl::Backend
{ {
template <>
void GX::Color::Enumerate<athena::io::DNA<athena::Big>::Read>(typename Read::StreamT& reader)
{ reader.readUBytesToBuf(&num, 4); }
template <>
void GX::Color::Enumerate<athena::io::DNA<athena::Big>::Write>(typename Write::StreamT& writer)
{ writer.writeUBytes(reinterpret_cast<const atUint8*>(&num), 4); }
template <>
void GX::Color::Enumerate<athena::io::DNA<athena::Big>::BinarySize>(typename BinarySize::StreamT& s)
{ s += 4; }
unsigned GX::addKColor(Diagnostics& diag, const SourceLocation& loc, const Color& color) unsigned GX::addKColor(Diagnostics& diag, const SourceLocation& loc, const Color& color)
{ {
for (unsigned i=0 ; i<m_kcolorCount ; ++i) for (unsigned i=0 ; i<m_kcolorCount ; ++i)

View File

@ -46,72 +46,29 @@ const atVec4f& IR::Instruction::getImmVec() const
return m_loadImm.m_immVec; return m_loadImm.m_immVec;
} }
void IR::Instruction::read(athena::io::IStreamReader& reader) template <class Op>
void IR::Instruction::Enumerate(typename Op::StreamT& s)
{ {
m_op = OpType(reader.readUByte()); Do<Op>({"op"}, m_op, s);
m_target = reader.readUint16Big(); Do<Op>({"target"}, m_target, s);
switch (m_op) switch (m_op)
{ {
default: break; default: break;
case OpType::Call: case OpType::Call:
m_call.read(reader); Do<Op>({"call"}, m_call, s);
break; break;
case OpType::LoadImm: case OpType::LoadImm:
m_loadImm.read(reader); Do<Op>({"loadImm"}, m_loadImm, s);
break; break;
case OpType::Arithmetic: case OpType::Arithmetic:
m_arithmetic.read(reader); Do<Op>({"arithmetic"}, m_arithmetic, s);
break; break;
case OpType::Swizzle: case OpType::Swizzle:
m_swizzle.read(reader); Do<Op>({"swizzle"}, m_swizzle, s);
break; 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) atInt8 IR::swizzleCompIdx(char aChar)
{ {
switch (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<BigDNA::Read>(typename Read::StreamT& reader)
{ {
m_hash = reader.readUint64Big(); m_hash = reader.readUint64Big();
m_regCount = reader.readUint16Big(); 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<BigDNA::Write>(typename Write::StreamT& writer)
{ {
writer.writeUint64Big(m_hash); writer.writeUint64Big(m_hash);
writer.writeUint16Big(m_regCount); writer.writeUint16Big(m_regCount);
@ -270,12 +229,12 @@ void IR::write(athena::io::IStreamWriter& writer) const
inst.write(writer); inst.write(writer);
} }
size_t IR::binarySize(size_t sz) const template <>
void IR::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& sz)
{ {
sz += 12; sz += 12;
for (const Instruction& inst : m_instructions) for (const Instruction& inst : m_instructions)
sz = inst.binarySize(sz); inst.binarySize(sz);
return sz;
} }
IR Frontend::compileSource(std::string_view source, std::string_view diagName) IR Frontend::compileSource(std::string_view source, std::string_view diagName)