metaforce/hecl/lib/Backend/GX.cpp

780 lines
29 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/Backend/GX.hpp"
2015-10-14 02:16:21 +00:00
#include <map>
2015-10-10 06:59:40 +00:00
2016-03-04 23:02:44 +00:00
namespace hecl
2015-10-10 06:59:40 +00:00
{
namespace Backend
{
2015-10-14 02:16:21 +00:00
unsigned GX::addKColor(Diagnostics& diag, const SourceLocation& loc, const Color& color)
2015-10-10 06:59:40 +00:00
{
for (unsigned i=0 ; i<m_kcolorCount ; ++i)
if (m_kcolors[i] == color)
return i;
if (m_kcolorCount >= 4)
2015-10-14 02:16:21 +00:00
diag.reportBackendErr(loc, "GX KColor overflow");
2015-10-10 06:59:40 +00:00
m_kcolors[m_kcolorCount] = color;
m_kcolors[m_kcolorCount].color[3] = 0;
2015-10-10 06:59:40 +00:00
return m_kcolorCount++;
}
2015-10-14 02:16:21 +00:00
unsigned GX::addKAlpha(Diagnostics& diag, const SourceLocation& loc, float alpha)
{
uint8_t ai = uint8_t(std::min(std::max(alpha * 255.f, 0.f), 255.f));
for (unsigned i=0 ; i<m_kcolorCount ; ++i)
{
if (m_kcolors[i].color[3] == ai)
return i;
else if (m_kcolors[i].color[3] == 0)
{
m_kcolors[i].color[3] = ai;
return i;
}
}
if (m_kcolorCount >= 4)
diag.reportBackendErr(loc, "GX KColor overflow");
m_kcolors[m_kcolorCount] = ai;
return m_kcolorCount++;
}
unsigned GX::addTexCoordGen(Diagnostics& diag, const SourceLocation& loc,
TexGenSrc src, TexMtx mtx, bool norm, PTTexMtx pmtx)
2015-10-14 02:16:21 +00:00
{
for (unsigned i=0 ; i<m_tcgCount ; ++i)
{
TexCoordGen& tcg = m_tcgs[i];
if (tcg.m_src == src && tcg.m_mtx == mtx && tcg.m_norm && tcg.m_pmtx)
2015-10-14 02:16:21 +00:00
return i;
}
if (m_tcgCount >= 8)
diag.reportBackendErr(loc, "GX TexCoordGen overflow");
GX::TexCoordGen& newTcg = m_tcgs[m_tcgCount];
newTcg.m_src = src;
newTcg.m_mtx = mtx;
newTcg.m_norm = norm;
newTcg.m_pmtx = pmtx;
2015-10-14 02:16:21 +00:00
return m_tcgCount++;
}
GX::TEVStage& GX::addTEVStage(Diagnostics& diag, const SourceLocation& loc)
{
if (m_tevCount >= 16)
diag.reportBackendErr(loc, "GX TEV stage overflow");
GX::TEVStage& newTEV = m_tevs[m_tevCount];
2015-10-16 03:08:01 +00:00
newTEV.m_loc = loc;
2015-10-14 02:16:21 +00:00
if (m_tevCount)
{
newTEV.m_prev = &m_tevs[m_tevCount-1];
newTEV.m_prev->m_next = &newTEV;
}
++m_tevCount;
return newTEV;
}
unsigned GX::RecursiveTraceTexGen(const IR& ir, Diagnostics& diag, const IR::Instruction& inst, TexMtx mtx,
bool normalize, PTTexMtx pmtx)
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (inst.m_op != IR::OpType::Call)
2015-10-14 02:16:21 +00:00
diag.reportBackendErr(inst.m_loc, "TexCoordGen resolution requires function");
const std::string& tcgName = inst.m_call.m_name;
if (!tcgName.compare("UV"))
{
if (inst.getChildCount() < 1)
diag.reportBackendErr(inst.m_loc, "TexCoordGen UV(layerIdx) requires one argument");
const IR::Instruction& idxInst = inst.getChildInst(ir, 0);
const atVec4f& idxImm = idxInst.getImmVec();
return addTexCoordGen(diag, inst.m_loc, TexGenSrc(TG_TEX0 + unsigned(idxImm.vec[0])), mtx, normalize, pmtx);
2015-10-14 02:16:21 +00:00
}
else if (!tcgName.compare("Normal"))
return addTexCoordGen(diag, inst.m_loc, TG_NRM, mtx, normalize, pmtx);
2015-10-14 02:16:21 +00:00
else if (!tcgName.compare("View"))
return addTexCoordGen(diag, inst.m_loc, TG_POS, mtx, normalize, pmtx);
2015-10-14 02:16:21 +00:00
/* Otherwise treat as game-specific function */
const IR::Instruction& tcgSrcInst = inst.getChildInst(ir, 0);
unsigned idx = RecursiveTraceTexGen(ir, diag, tcgSrcInst, TexMtx(TEXMTX0 + m_texMtxCount * 3),
normalize || tcgName.back() == 'N', PTTexMtx(PTTEXMTX0 + m_texMtxCount * 3));
2015-10-14 02:16:21 +00:00
GX::TexCoordGen& tcg = m_tcgs[idx];
m_texMtxRefs[m_texMtxCount] = &tcg;
++m_texMtxCount;
tcg.m_gameFunction = tcgName;
tcg.m_gameArgs.clear();
2015-10-15 22:24:34 +00:00
for (int i=1 ; i<inst.getChildCount() ; ++i)
2015-10-14 02:16:21 +00:00
{
const IR::Instruction& ci = inst.getChildInst(ir, i);
tcg.m_gameArgs.push_back(ci.getImmVec());
}
return idx;
}
2015-10-16 08:02:59 +00:00
GX::TraceResult GX::RecursiveTraceColor(const IR& ir, Diagnostics& diag, const IR::Instruction& inst,
bool swizzleAlpha)
2015-10-11 05:55:53 +00:00
{
switch (inst.m_op)
{
2015-11-21 01:13:06 +00:00
case IR::OpType::Call:
2015-10-11 05:55:53 +00:00
{
2015-10-14 02:16:21 +00:00
const std::string& name = inst.m_call.m_name;
bool normalize = false;
if (!name.compare("Texture") || (normalize = true && !name.compare("TextureN")))
2015-10-11 05:55:53 +00:00
{
2015-10-14 02:16:21 +00:00
TEVStage& newStage = addTEVStage(diag, inst.m_loc);
if (inst.getChildCount() < 2)
diag.reportBackendErr(inst.m_loc, "Texture(map, texgen) requires 2 arguments");
const IR::Instruction& mapInst = inst.getChildInst(ir, 0);
const atVec4f& mapImm = mapInst.getImmVec();
newStage.m_texMapIdx = unsigned(mapImm.vec[0]);
2015-10-16 08:02:59 +00:00
newStage.m_color[0] = swizzleAlpha ? CC_TEXA : CC_TEXC;
2015-10-14 02:16:21 +00:00
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
newStage.m_texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, IDENTITY, normalize, PTIDENTITY);
2015-10-14 02:16:21 +00:00
return TraceResult(&newStage);
}
else if (!name.compare("ColorReg"))
{
const IR::Instruction& idxInst = inst.getChildInst(ir, 0);
unsigned idx = unsigned(idxInst.getImmVec().vec[0]);
2015-10-17 02:06:27 +00:00
if (swizzleAlpha)
m_aRegMask |= 1 << idx;
else
m_cRegMask |= 1 << idx;
2015-10-16 08:02:59 +00:00
return TraceResult(TevColorArg((swizzleAlpha ? CC_A0 : CC_C0) + idx * 2));
2015-10-14 02:16:21 +00:00
}
else if (!name.compare("Lighting"))
{
2015-10-16 08:02:59 +00:00
return TraceResult(swizzleAlpha ? CC_RASA : CC_RASC);
2015-10-14 02:16:21 +00:00
}
else
diag.reportBackendErr(inst.m_loc, "GX backend unable to interpret '%s'", name.c_str());
break;
}
2015-11-21 01:13:06 +00:00
case IR::OpType::LoadImm:
2015-10-14 02:16:21 +00:00
{
const atVec4f& vec = inst.m_loadImm.m_immVec;
if (vec.vec[0] == 0.f && vec.vec[1] == 0.f && vec.vec[2] == 0.f)
return TraceResult(CC_ZERO);
else if (vec.vec[0] == 1.f && vec.vec[1] == 1.f && vec.vec[2] == 1.f)
return TraceResult(CC_ONE);
unsigned idx = addKColor(diag, inst.m_loc, vec);
return TraceResult(TevKColorSel(TEV_KCSEL_K0 + idx));
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Arithmetic:
2015-10-14 02:16:21 +00:00
{
ArithmeticOp op = inst.m_arithmetic.m_op;
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
const IR::Instruction& bInst = inst.getChildInst(ir, 1);
2015-10-16 00:34:47 +00:00
TraceResult aTrace;
TraceResult bTrace;
2015-11-21 01:13:06 +00:00
if (aInst.m_op != IR::OpType::Arithmetic && bInst.m_op == IR::OpType::Arithmetic)
2015-10-16 00:34:47 +00:00
{
bTrace = RecursiveTraceColor(ir, diag, bInst);
aTrace = RecursiveTraceColor(ir, diag, aInst);
}
else
{
aTrace = RecursiveTraceColor(ir, diag, aInst);
bTrace = RecursiveTraceColor(ir, diag, bInst);
}
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage &&
2015-10-16 08:02:59 +00:00
getStageIdx(aTrace.tevStage) > getStageIdx(bTrace.tevStage))
{
TraceResult tmp = aTrace;
aTrace = bTrace;
bTrace = tmp;
}
2015-10-16 00:34:47 +00:00
TevKColorSel newKColor = TEV_KCSEL_1;
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVKColorSel &&
bTrace.type == TraceResult::Type::TEVKColorSel)
2015-10-16 00:34:47 +00:00
diag.reportBackendErr(inst.m_loc, "unable to handle 2 KColors in one stage");
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVKColorSel)
2015-10-16 00:34:47 +00:00
{
newKColor = aTrace.tevKColorSel;
2015-11-21 01:13:06 +00:00
aTrace.type = TraceResult::Type::TEVColorArg;
2015-10-16 00:34:47 +00:00
aTrace.tevColorArg = CC_KONST;
}
2015-11-21 01:13:06 +00:00
else if (bTrace.type == TraceResult::Type::TEVKColorSel)
2015-10-16 00:34:47 +00:00
{
newKColor = bTrace.tevKColorSel;
2015-11-21 01:13:06 +00:00
bTrace.type = TraceResult::Type::TEVColorArg;
2015-10-16 00:34:47 +00:00
bTrace.tevColorArg = CC_KONST;
}
2015-10-11 05:55:53 +00:00
2015-10-14 02:16:21 +00:00
switch (op)
{
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Add:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
if (b->m_prev != a)
2015-10-16 00:34:47 +00:00
{
2015-10-17 02:06:27 +00:00
a->m_cRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_color[3] = CC_LAZY;
b->m_lazyCInIdx = m_cRegLazy;
2015-10-17 02:06:27 +00:00
a->m_lazyCOutIdx = m_cRegLazy++;
2015-10-16 00:34:47 +00:00
}
2015-10-16 08:02:59 +00:00
else if (b == &m_tevs[m_tevCount-1] &&
a->m_texMapIdx == b->m_texMapIdx && a->m_texGenIdx == b->m_texGenIdx &&
a->m_color[3] == CC_ZERO && b->m_color[0] != CC_ZERO)
{
a->m_color[3] = b->m_color[0];
--m_tevCount;
return TraceResult(a);
}
2015-10-16 00:34:47 +00:00
else
2015-10-16 08:02:59 +00:00
b->m_color[3] = CC_CPREV;
2015-10-16 00:34:47 +00:00
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVColorArg)
2015-10-16 00:34:47 +00:00
{
TEVStage* a = aTrace.tevStage;
if (a->m_color[3] != CC_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for add combine");
a->m_color[3] = bTrace.tevColorArg;
a->m_kColor = newKColor;
return TraceResult(a);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVColorArg &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-16 00:34:47 +00:00
{
TEVStage* b = bTrace.tevStage;
if (b->m_color[3] != CC_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for add combine");
b->m_color[3] = aTrace.tevColorArg;
b->m_kColor = newKColor;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
break;
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Subtract:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
if (b->m_prev != a)
2015-10-16 00:34:47 +00:00
{
2015-10-17 02:06:27 +00:00
a->m_cRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_color[3] = CC_LAZY;
b->m_lazyCInIdx = m_cRegLazy;
2015-10-17 02:06:27 +00:00
a->m_lazyCOutIdx = m_cRegLazy++;
2015-10-16 00:34:47 +00:00
}
else
2015-10-16 08:02:59 +00:00
b->m_color[3] = CC_CPREV;
2015-10-17 02:06:27 +00:00
b->m_cop = TEV_SUB;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVColorArg)
2015-10-16 00:34:47 +00:00
{
TEVStage* a = aTrace.tevStage;
if (a->m_color[3] != CC_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for subtract combine");
a->m_color[3] = bTrace.tevColorArg;
a->m_kColor = newKColor;
2015-10-17 02:06:27 +00:00
a->m_cop = TEV_SUB;
2015-10-16 00:34:47 +00:00
return TraceResult(a);
}
2015-10-14 02:16:21 +00:00
break;
2015-10-11 05:55:53 +00:00
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Multiply:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
2015-10-16 08:02:59 +00:00
if (b->m_color[2] != CC_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
2015-10-14 02:16:21 +00:00
if (b->m_prev != a)
2015-10-16 00:34:47 +00:00
{
2015-10-17 02:06:27 +00:00
a->m_cRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_color[2] = CC_LAZY;
b->m_lazyCInIdx = m_cRegLazy;
2015-10-17 02:06:27 +00:00
a->m_lazyCOutIdx = m_cRegLazy++;
2015-10-16 00:34:47 +00:00
}
else
b->m_color[2] = CC_CPREV;
2015-10-14 02:16:21 +00:00
b->m_color[1] = b->m_color[0];
b->m_color[0] = CC_ZERO;
b->m_color[3] = CC_ZERO;
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVColorArg &&
bTrace.type == TraceResult::Type::TEVColorArg)
2015-10-14 23:06:47 +00:00
{
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_color[1] = aTrace.tevColorArg;
stage.m_color[2] = bTrace.tevColorArg;
2015-10-16 00:34:47 +00:00
stage.m_kColor = newKColor;
2015-10-14 23:06:47 +00:00
return TraceResult(&stage);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVColorArg)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
if (a->m_color[1] != CC_ZERO)
2015-10-16 08:02:59 +00:00
{
2015-10-17 02:06:27 +00:00
if (a->m_cRegOut != TEVPREV)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_color[1] = CC_CPREV;
stage.m_color[2] = bTrace.tevColorArg;
stage.m_kColor = newKColor;
return TraceResult(&stage);
}
2015-10-14 02:16:21 +00:00
a->m_color[1] = a->m_color[0];
a->m_color[0] = CC_ZERO;
a->m_color[2] = bTrace.tevColorArg;
2015-10-16 00:34:47 +00:00
a->m_kColor = newKColor;
2015-10-14 02:16:21 +00:00
return TraceResult(a);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVColorArg &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* b = bTrace.tevStage;
if (b->m_color[1] != CC_ZERO)
2015-10-16 08:02:59 +00:00
{
2015-10-17 02:06:27 +00:00
if (b->m_cRegOut != TEVPREV)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_color[1] = aTrace.tevColorArg;
stage.m_color[2] = CC_CPREV;
stage.m_kColor = newKColor;
return TraceResult(&stage);
}
2015-10-14 02:16:21 +00:00
b->m_color[1] = b->m_color[0];
b->m_color[0] = CC_ZERO;
b->m_color[2] = bTrace.tevColorArg;
2015-10-16 00:34:47 +00:00
b->m_kColor = newKColor;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
break;
}
default:
diag.reportBackendErr(inst.m_loc, "invalid arithmetic op");
}
diag.reportBackendErr(inst.m_loc, "unable to convert arithmetic to TEV stage");
2015-10-11 05:55:53 +00:00
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Swizzle:
2015-10-14 23:06:47 +00:00
{
2015-10-17 02:06:27 +00:00
if (inst.m_swizzle.m_idxs[0] == 3 && inst.m_swizzle.m_idxs[1] == 3 &&
inst.m_swizzle.m_idxs[2] == 3 && inst.m_swizzle.m_idxs[3] == -1)
2015-10-14 23:06:47 +00:00
{
const IR::Instruction& cInst = inst.getChildInst(ir, 0);
2015-11-21 01:13:06 +00:00
if (cInst.m_op != IR::OpType::Call)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "only functions accepted for alpha swizzle");
return RecursiveTraceColor(ir, diag, cInst, true);
2015-10-14 23:06:47 +00:00
}
else
diag.reportBackendErr(inst.m_loc, "only alpha extract may be performed with swizzle operation");
}
2015-10-11 05:55:53 +00:00
default:
2015-10-14 02:16:21 +00:00
diag.reportBackendErr(inst.m_loc, "invalid color op");
2015-10-11 05:55:53 +00:00
}
2015-10-14 02:16:21 +00:00
return TraceResult();
2015-10-11 05:55:53 +00:00
}
2015-10-14 02:16:21 +00:00
GX::TraceResult GX::RecursiveTraceAlpha(const IR& ir, Diagnostics& diag, const IR::Instruction& inst)
2015-10-11 05:55:53 +00:00
{
2015-10-14 02:16:21 +00:00
switch (inst.m_op)
{
2015-11-21 01:13:06 +00:00
case IR::OpType::Call:
2015-10-14 02:16:21 +00:00
{
const std::string& name = inst.m_call.m_name;
bool normalize = false;
if (!name.compare("Texture") || (normalize = true && !name.compare("TextureN")))
2015-10-14 02:16:21 +00:00
{
if (inst.getChildCount() < 2)
diag.reportBackendErr(inst.m_loc, "Texture(map, texgen) requires 2 arguments");
const IR::Instruction& mapInst = inst.getChildInst(ir, 0);
const atVec4f& mapImm = mapInst.getImmVec();
unsigned mapIdx = unsigned(mapImm.vec[0]);
int foundStage = -1;
2015-10-15 22:24:34 +00:00
for (int i=0 ; i<int(m_tevCount) ; ++i)
2015-10-14 02:16:21 +00:00
{
TEVStage& testStage = m_tevs[i];
if (testStage.m_texMapIdx == mapIdx && i > m_alphaTraceStage)
{
foundStage = i;
break;
}
}
if (foundStage >= 0)
{
m_alphaTraceStage = foundStage;
TEVStage& stage = m_tevs[foundStage];
stage.m_alpha[0] = CA_TEXA;
return TraceResult(&stage);
}
TEVStage& newStage = addTEVStage(diag, inst.m_loc);
2015-10-14 23:06:47 +00:00
newStage.m_color[3] = CC_CPREV;
2015-10-14 02:16:21 +00:00
newStage.m_texMapIdx = mapIdx;
newStage.m_alpha[0] = CA_TEXA;
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
newStage.m_texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, IDENTITY, normalize, PTIDENTITY);
2015-10-14 02:16:21 +00:00
return TraceResult(&newStage);
}
else if (!name.compare("ColorReg"))
{
const IR::Instruction& idxInst = inst.getChildInst(ir, 0);
unsigned idx = unsigned(idxInst.getImmVec().vec[0]);
2015-10-17 02:06:27 +00:00
m_aRegMask |= 1 << idx;
2015-10-16 08:02:59 +00:00
return TraceResult(TevAlphaArg(CA_A0 + idx));
2015-10-14 02:16:21 +00:00
}
else if (!name.compare("Lighting"))
{
return TraceResult(CA_RASA);
}
else
diag.reportBackendErr(inst.m_loc, "GX backend unable to interpret '%s'", name.c_str());
break;
}
2015-11-21 01:13:06 +00:00
case IR::OpType::LoadImm:
2015-10-14 02:16:21 +00:00
{
const atVec4f& vec = inst.m_loadImm.m_immVec;
if (vec.vec[0] == 0.f)
return TraceResult(CA_ZERO);
unsigned idx = addKAlpha(diag, inst.m_loc, vec.vec[0]);
return TraceResult(TevKAlphaSel(TEV_KASEL_K0_A + idx));
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Arithmetic:
2015-10-14 02:16:21 +00:00
{
ArithmeticOp op = inst.m_arithmetic.m_op;
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
const IR::Instruction& bInst = inst.getChildInst(ir, 1);
2015-10-16 00:34:47 +00:00
TraceResult aTrace;
TraceResult bTrace;
2015-11-21 01:13:06 +00:00
if (aInst.m_op != IR::OpType::Arithmetic && bInst.m_op == IR::OpType::Arithmetic)
2015-10-16 00:34:47 +00:00
{
bTrace = RecursiveTraceAlpha(ir, diag, bInst);
aTrace = RecursiveTraceAlpha(ir, diag, aInst);
}
else
{
aTrace = RecursiveTraceAlpha(ir, diag, aInst);
bTrace = RecursiveTraceAlpha(ir, diag, bInst);
}
TevKAlphaSel newKAlpha = TEV_KASEL_1;
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVKAlphaSel &&
bTrace.type == TraceResult::Type::TEVKAlphaSel)
2015-10-16 00:34:47 +00:00
diag.reportBackendErr(inst.m_loc, "unable to handle 2 KAlphas in one stage");
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVKAlphaSel)
2015-10-16 00:34:47 +00:00
{
newKAlpha = aTrace.tevKAlphaSel;
2015-11-21 01:13:06 +00:00
aTrace.type = TraceResult::Type::TEVAlphaArg;
2015-10-16 00:34:47 +00:00
aTrace.tevAlphaArg = CA_KONST;
}
2015-11-21 01:13:06 +00:00
else if (bTrace.type == TraceResult::Type::TEVKAlphaSel)
2015-10-16 00:34:47 +00:00
{
newKAlpha = bTrace.tevKAlphaSel;
2015-11-21 01:13:06 +00:00
bTrace.type = TraceResult::Type::TEVAlphaArg;
2015-10-16 00:34:47 +00:00
bTrace.tevAlphaArg = CA_KONST;
}
2015-10-14 02:16:21 +00:00
switch (op)
{
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Add:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
if (b->m_prev != a)
2015-10-16 00:34:47 +00:00
{
2015-10-17 02:06:27 +00:00
a->m_aRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_alpha[3] = CA_LAZY;
2015-10-17 02:06:27 +00:00
if (a->m_lazyAOutIdx != -1)
b->m_lazyAInIdx = a->m_lazyAOutIdx;
2015-10-16 00:34:47 +00:00
else
{
2015-10-17 02:06:27 +00:00
b->m_lazyAInIdx = m_aRegLazy;
a->m_lazyAOutIdx = m_aRegLazy++;
2015-10-16 00:34:47 +00:00
}
}
else
b->m_alpha[3] = CA_APREV;
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVAlphaArg)
2015-10-16 00:34:47 +00:00
{
TEVStage* a = aTrace.tevStage;
if (a->m_alpha[3] != CA_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for add combine");
a->m_alpha[3] = bTrace.tevAlphaArg;
a->m_kAlpha = newKAlpha;
return TraceResult(a);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVAlphaArg &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-16 00:34:47 +00:00
{
TEVStage* b = bTrace.tevStage;
if (b->m_alpha[3] != CA_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for add combine");
b->m_alpha[3] = aTrace.tevAlphaArg;
b->m_kAlpha = newKAlpha;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
break;
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Subtract:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
2015-10-17 02:06:27 +00:00
if (b->m_aop != TEV_SUB)
2015-10-14 02:16:21 +00:00
diag.reportBackendErr(inst.m_loc, "unable to integrate alpha subtraction into stage chain");
2015-10-16 00:34:47 +00:00
if (b->m_prev != a)
{
2015-10-17 02:06:27 +00:00
a->m_aRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_alpha[3] = CA_LAZY;
2015-10-17 02:06:27 +00:00
if (a->m_lazyAOutIdx != -1)
b->m_lazyAInIdx = a->m_lazyAOutIdx;
2015-10-16 00:34:47 +00:00
else
{
2015-10-17 02:06:27 +00:00
b->m_lazyAInIdx = m_aRegLazy;
a->m_lazyAOutIdx = m_aRegLazy++;
2015-10-16 00:34:47 +00:00
}
}
else
b->m_alpha[3] = CA_APREV;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVAlphaArg)
2015-10-16 00:34:47 +00:00
{
TEVStage* a = aTrace.tevStage;
2015-10-17 02:06:27 +00:00
if (a->m_aop != TEV_SUB)
2015-10-16 00:34:47 +00:00
diag.reportBackendErr(inst.m_loc, "unable to integrate alpha subtraction into stage chain");
if (a->m_alpha[3] != CA_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for add combine");
a->m_alpha[3] = bTrace.tevAlphaArg;
a->m_kAlpha = newKAlpha;
return TraceResult(a);
}
2015-10-14 02:16:21 +00:00
break;
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Multiply:
2015-10-14 02:16:21 +00:00
{
2015-11-21 01:13:06 +00:00
if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
TEVStage* b = bTrace.tevStage;
2015-10-16 08:02:59 +00:00
if (b->m_alpha[2] != CA_ZERO)
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
2015-10-14 02:16:21 +00:00
if (b->m_prev != a)
2015-10-16 00:34:47 +00:00
{
2015-10-17 02:06:27 +00:00
a->m_aRegOut = TEVLAZY;
2015-10-16 00:34:47 +00:00
b->m_alpha[2] = CA_LAZY;
2015-10-17 02:06:27 +00:00
b->m_lazyAInIdx = m_aRegLazy;
a->m_lazyAOutIdx = m_aRegLazy++;
2015-10-16 00:34:47 +00:00
}
else
b->m_alpha[2] = CA_APREV;
2015-10-14 02:16:21 +00:00
b->m_alpha[1] = b->m_alpha[0];
b->m_alpha[0] = CA_ZERO;
b->m_alpha[3] = CA_ZERO;
return TraceResult(b);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVAlphaArg &&
bTrace.type == TraceResult::Type::TEVAlphaArg)
2015-10-14 23:06:47 +00:00
{
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_color[3] = CC_CPREV;
stage.m_alpha[1] = aTrace.tevAlphaArg;
stage.m_alpha[2] = bTrace.tevAlphaArg;
2015-10-16 00:34:47 +00:00
stage.m_kAlpha = newKAlpha;
2015-10-14 23:06:47 +00:00
return TraceResult(&stage);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVStage &&
bTrace.type == TraceResult::Type::TEVAlphaArg)
2015-10-14 02:16:21 +00:00
{
TEVStage* a = aTrace.tevStage;
if (a->m_alpha[1] != CA_ZERO)
2015-10-16 08:02:59 +00:00
{
2015-10-17 02:06:27 +00:00
if (a->m_aRegOut != TEVPREV)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_alpha[1] = CA_APREV;
stage.m_alpha[2] = bTrace.tevAlphaArg;
stage.m_kAlpha = newKAlpha;
return TraceResult(&stage);
}
2015-10-14 02:16:21 +00:00
a->m_alpha[1] = a->m_alpha[0];
a->m_alpha[0] = CA_ZERO;
a->m_alpha[2] = bTrace.tevAlphaArg;
2015-10-16 00:34:47 +00:00
a->m_kAlpha = newKAlpha;
2015-10-14 02:16:21 +00:00
return TraceResult(a);
}
2015-11-21 01:13:06 +00:00
else if (aTrace.type == TraceResult::Type::TEVAlphaArg &&
bTrace.type == TraceResult::Type::TEVStage)
2015-10-14 02:16:21 +00:00
{
TEVStage* b = bTrace.tevStage;
if (b->m_alpha[1] != CA_ZERO)
2015-10-16 08:02:59 +00:00
{
2015-10-17 02:06:27 +00:00
if (b->m_aRegOut != TEVPREV)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "unable to modify TEV stage for multiply combine");
TEVStage& stage = addTEVStage(diag, inst.m_loc);
stage.m_alpha[1] = aTrace.tevAlphaArg;
stage.m_alpha[2] = CA_APREV;
stage.m_kAlpha = newKAlpha;
return TraceResult(&stage);
}
2015-10-14 02:16:21 +00:00
b->m_alpha[1] = b->m_alpha[0];
b->m_alpha[0] = CA_ZERO;
b->m_alpha[2] = bTrace.tevAlphaArg;
2015-10-16 00:34:47 +00:00
b->m_kAlpha = newKAlpha;
2015-10-14 02:16:21 +00:00
return TraceResult(b);
}
break;
}
default:
diag.reportBackendErr(inst.m_loc, "invalid arithmetic op");
}
diag.reportBackendErr(inst.m_loc, "unable to convert arithmetic to TEV stage");
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Swizzle:
2015-10-16 08:02:59 +00:00
{
2015-10-17 02:06:27 +00:00
if (inst.m_swizzle.m_idxs[0] == 3 && inst.m_swizzle.m_idxs[1] == 3 &&
inst.m_swizzle.m_idxs[2] == 3 && inst.m_swizzle.m_idxs[3] == -1)
2015-10-16 08:02:59 +00:00
{
const IR::Instruction& cInst = inst.getChildInst(ir, 0);
2015-11-21 01:13:06 +00:00
if (cInst.m_op != IR::OpType::Call)
2015-10-16 08:02:59 +00:00
diag.reportBackendErr(inst.m_loc, "only functions accepted for alpha swizzle");
return RecursiveTraceAlpha(ir, diag, cInst);
}
else
diag.reportBackendErr(inst.m_loc, "only alpha extract may be performed with swizzle operation");
}
2015-10-14 02:16:21 +00:00
default:
diag.reportBackendErr(inst.m_loc, "invalid alpha op");
}
2015-10-11 05:55:53 +00:00
2015-10-14 02:16:21 +00:00
return TraceResult();
2015-10-11 05:55:53 +00:00
}
2015-10-14 02:16:21 +00:00
void GX::reset(const IR& ir, Diagnostics& diag)
2015-10-10 06:59:40 +00:00
{
2015-10-17 02:06:27 +00:00
diag.setBackend("GX");
2015-10-10 06:59:40 +00:00
m_tevCount = 0;
m_tcgCount = 0;
2015-10-14 02:16:21 +00:00
m_texMtxCount = 0;
2015-10-10 06:59:40 +00:00
m_kcolorCount = 0;
2015-10-16 00:34:47 +00:00
m_cRegMask = 0;
m_cRegLazy = 0;
2015-10-17 02:06:27 +00:00
m_aRegMask = 0;
m_aRegLazy = 0;
2015-10-14 02:16:21 +00:00
m_alphaTraceStage = -1;
2015-10-10 06:59:40 +00:00
/* Final instruction is the root call by hecl convention */
2015-10-14 02:16:21 +00:00
const IR::Instruction& rootCall = ir.m_instructions.back();
2015-10-10 06:59:40 +00:00
bool doAlpha = false;
if (!rootCall.m_call.m_name.compare("HECLOpaque"))
{
m_blendSrc = BL_ONE;
m_blendDst = BL_ZERO;
}
else if (!rootCall.m_call.m_name.compare("HECLAlpha"))
{
m_blendSrc = BL_SRCALPHA;
m_blendDst = BL_INVSRCALPHA;
doAlpha = true;
}
else if (!rootCall.m_call.m_name.compare("HECLAdditive"))
{
m_blendSrc = BL_SRCALPHA;
m_blendDst = BL_ONE;
doAlpha = true;
}
2015-10-14 23:06:47 +00:00
else
{
diag.reportBackendErr(rootCall.m_loc, "GX backend doesn't handle '%s' root",
rootCall.m_call.m_name.c_str());
return;
}
2015-10-10 06:59:40 +00:00
2015-10-11 05:55:53 +00:00
/* Follow Color Chain */
2015-10-14 02:16:21 +00:00
const IR::Instruction& colorRoot =
2015-10-11 05:55:53 +00:00
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(0));
2015-10-14 02:16:21 +00:00
RecursiveTraceColor(ir, diag, colorRoot);
2015-10-11 05:55:53 +00:00
/* Follow Alpha Chain */
if (doAlpha)
2015-10-10 06:59:40 +00:00
{
2015-10-14 02:16:21 +00:00
const IR::Instruction& alphaRoot =
2015-10-11 05:55:53 +00:00
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(1));
2015-10-14 02:16:21 +00:00
RecursiveTraceAlpha(ir, diag, alphaRoot);
2015-10-14 23:06:47 +00:00
/* Ensure Alpha reaches end of chain */
if (m_alphaTraceStage >= 0)
2015-10-15 22:24:34 +00:00
for (unsigned i=m_alphaTraceStage+1 ; i<m_tevCount ; ++i)
2015-10-14 23:06:47 +00:00
m_tevs[i].m_alpha[3] = CA_APREV;
2015-10-10 06:59:40 +00:00
}
2015-10-16 00:34:47 +00:00
/* Resolve lazy color/alpha regs */
if (m_cRegLazy)
{
2015-10-16 21:52:18 +00:00
for (int i=0 ; i<int(m_tevCount) ; ++i)
2015-10-16 00:34:47 +00:00
{
TEVStage& stage = m_tevs[i];
2015-10-17 02:06:27 +00:00
if (stage.m_cRegOut == TEVLAZY)
2015-10-16 00:34:47 +00:00
{
2015-10-16 08:02:59 +00:00
int picked = pickCLazy(diag, stage.m_loc, i);
2015-10-17 02:06:27 +00:00
stage.m_cRegOut = TevRegID(TEVREG0 + picked);
2015-10-16 21:52:18 +00:00
for (int j=i+1 ; j<int(m_tevCount) ; ++j)
2015-10-16 00:34:47 +00:00
{
TEVStage& nstage = m_tevs[j];
2015-10-17 02:06:27 +00:00
if (nstage.m_lazyCInIdx == stage.m_lazyCOutIdx)
2015-10-16 00:34:47 +00:00
for (int c=0 ; c<4 ; ++c)
if (nstage.m_color[c] == CC_LAZY)
nstage.m_color[c] = TevColorArg(CC_C0 + picked * 2);
2015-10-17 02:06:27 +00:00
}
}
if (stage.m_aRegOut == TEVLAZY)
{
int picked = pickALazy(diag, stage.m_loc, i);
stage.m_aRegOut = TevRegID(TEVREG0 + picked);
for (int j=i+1 ; j<int(m_tevCount) ; ++j)
{
TEVStage& nstage = m_tevs[j];
if (nstage.m_lazyAInIdx == stage.m_lazyAOutIdx)
2015-10-16 00:34:47 +00:00
for (int c=0 ; c<4 ; ++c)
if (nstage.m_alpha[c] == CA_LAZY)
nstage.m_alpha[c] = TevAlphaArg(CA_A0 + picked);
}
}
}
}
2015-10-10 06:59:40 +00:00
}
}
}