metaforce/hecl/lib/Backend/ProgrammableCommon.cpp

319 lines
10 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/Backend/ProgrammableCommon.hpp"
2015-11-10 23:17:53 +00:00
#include <map>
2016-03-04 23:02:44 +00:00
namespace hecl
2015-11-10 23:17:53 +00:00
{
namespace Backend
{
unsigned ProgrammableCommon::addTexCoordGen(TexGenSrc src, int uvIdx, int mtx)
{
for (unsigned i=0 ; i<m_tcgs.size() ; ++i)
{
TexCoordGen& tcg = m_tcgs[i];
if (tcg.m_src == src && tcg.m_uvIdx == uvIdx && tcg.m_mtx == mtx)
return i;
}
m_tcgs.emplace_back();
TexCoordGen& newTcg = m_tcgs.back();
newTcg.m_src = src;
newTcg.m_uvIdx = uvIdx;
newTcg.m_mtx = mtx;
return m_tcgs.size() - 1;
}
unsigned ProgrammableCommon::addTexSampling(unsigned mapIdx, unsigned tcgIdx)
{
for (unsigned i=0 ; i<m_texSamplings.size() ; ++i)
{
TexSampling& samp = m_texSamplings[i];
if (samp.mapIdx == mapIdx && samp.tcgIdx == tcgIdx)
return i;
}
m_texSamplings.emplace_back();
TexSampling& samp = m_texSamplings.back();
samp.mapIdx = mapIdx;
samp.tcgIdx = tcgIdx;
2015-11-16 04:30:06 +00:00
if (m_texMapEnd < mapIdx + 1)
m_texMapEnd = mapIdx + 1;
2015-11-10 23:17:53 +00:00
return m_texSamplings.size() - 1;
}
unsigned ProgrammableCommon::RecursiveTraceTexGen(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst, int mtx)
{
2015-11-21 01:13:06 +00:00
if (inst.m_op != IR::OpType::Call)
2015-11-10 23:17:53 +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();
2015-11-21 01:13:06 +00:00
return addTexCoordGen(TexGenSrc::UV, idxImm.vec[0], mtx);
2015-11-10 23:17:53 +00:00
}
else if (!tcgName.compare("Normal"))
2015-11-21 01:13:06 +00:00
return addTexCoordGen(TexGenSrc::Normal, -1, mtx);
2015-11-10 23:17:53 +00:00
else if (!tcgName.compare("View"))
2015-11-21 01:13:06 +00:00
return addTexCoordGen(TexGenSrc::Position, -1, mtx);
2015-11-10 23:17:53 +00:00
/* Otherwise treat as game-specific function */
const IR::Instruction& tcgSrcInst = inst.getChildInst(ir, 0);
unsigned idx = RecursiveTraceTexGen(ir, diag, tcgSrcInst, m_texMtxRefs.size());
TexCoordGen& tcg = m_tcgs[idx];
m_texMtxRefs.push_back(idx);
tcg.m_gameFunction = tcgName;
tcg.m_gameArgs.clear();
for (int i=1 ; i<inst.getChildCount() ; ++i)
{
const IR::Instruction& ci = inst.getChildInst(ir, i);
tcg.m_gameArgs.push_back(ci.getImmVec());
}
return idx;
}
std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst)
{
switch (inst.m_op)
{
2015-11-21 01:13:06 +00:00
case IR::OpType::Call:
2015-11-10 23:17:53 +00:00
{
const std::string& name = inst.m_call.m_name;
if (!name.compare("Texture"))
{
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]);
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
unsigned texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, -1);
2015-11-16 04:30:06 +00:00
return EmitSamplingUseRGB(addTexSampling(mapIdx, texGenIdx));
2015-11-10 23:17:53 +00:00
}
else if (!name.compare("ColorReg"))
{
const IR::Instruction& idxInst = inst.getChildInst(ir, 0);
unsigned idx = unsigned(idxInst.getImmVec().vec[0]);
return EmitColorRegUse(idx);
}
else if (!name.compare("Lighting"))
{
m_lighting = true;
2016-03-31 18:56:19 +00:00
return EmitLightingRGB();
2015-11-10 23:17:53 +00:00
}
else
diag.reportBackendErr(inst.m_loc, "unable to interpret '%s'", name.c_str());
break;
}
2015-11-21 01:13:06 +00:00
case IR::OpType::LoadImm:
2015-11-10 23:17:53 +00:00
{
const atVec4f& vec = inst.m_loadImm.m_immVec;
return EmitVec3(vec);
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Arithmetic:
2015-11-10 23:17:53 +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);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst);
std::string bTrace = RecursiveTraceColor(ir, diag, bInst);
switch (op)
{
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Add:
2015-11-10 23:17:53 +00:00
{
return EmitAdd(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Subtract:
2015-11-10 23:17:53 +00:00
{
return EmitSub(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Multiply:
2015-11-10 23:17:53 +00:00
{
return EmitMult(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Divide:
2015-11-10 23:17:53 +00:00
{
return EmitDiv(aTrace, bTrace);
}
default:
diag.reportBackendErr(inst.m_loc, "invalid arithmetic op");
}
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Swizzle:
2015-11-10 23:17:53 +00:00
{
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst);
return EmitSwizzle3(diag, inst.m_loc, aTrace, inst.m_swizzle.m_idxs);
}
default:
diag.reportBackendErr(inst.m_loc, "invalid color op");
}
return std::string();
}
std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst)
{
switch (inst.m_op)
{
2015-11-21 01:13:06 +00:00
case IR::OpType::Call:
2015-11-10 23:17:53 +00:00
{
const std::string& name = inst.m_call.m_name;
if (!name.compare("Texture"))
{
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]);
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
unsigned texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, -1);
2015-11-16 04:30:06 +00:00
return EmitSamplingUseAlpha(addTexSampling(mapIdx, texGenIdx));
2015-11-10 23:17:53 +00:00
}
else if (!name.compare("ColorReg"))
{
const IR::Instruction& idxInst = inst.getChildInst(ir, 0);
unsigned idx = unsigned(idxInst.getImmVec().vec[0]);
return EmitColorRegUse(idx);
}
else if (!name.compare("Lighting"))
{
m_lighting = true;
2016-03-31 18:56:19 +00:00
return EmitLightingAlpha();
2015-11-10 23:17:53 +00:00
}
else
diag.reportBackendErr(inst.m_loc, "unable to interpret '%s'", name.c_str());
break;
}
2015-11-21 01:13:06 +00:00
case IR::OpType::LoadImm:
2015-11-10 23:17:53 +00:00
{
const atVec4f& vec = inst.m_loadImm.m_immVec;
return EmitVal(vec.vec[0]);
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Arithmetic:
2015-11-10 23:17:53 +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);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst);
std::string bTrace = RecursiveTraceAlpha(ir, diag, bInst);
switch (op)
{
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Add:
2015-11-10 23:17:53 +00:00
{
return EmitAdd(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Subtract:
2015-11-10 23:17:53 +00:00
{
return EmitSub(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Multiply:
2015-11-10 23:17:53 +00:00
{
return EmitMult(aTrace, bTrace);
}
2015-11-21 01:13:06 +00:00
case ArithmeticOp::Divide:
2015-11-10 23:17:53 +00:00
{
return EmitDiv(aTrace, bTrace);
}
default:
diag.reportBackendErr(inst.m_loc, "invalid arithmetic op");
}
}
2015-11-21 01:13:06 +00:00
case IR::OpType::Swizzle:
2015-11-10 23:17:53 +00:00
{
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst);
return EmitSwizzle1(diag, inst.m_loc, aTrace, inst.m_swizzle.m_idxs);
}
default:
diag.reportBackendErr(inst.m_loc, "invalid alpha op");
}
return std::string();
}
void ProgrammableCommon::reset(const IR& ir, Diagnostics& diag, const char* backendName)
{
diag.setBackend(backendName);
/* Final instruction is the root call by hecl convention */
const IR::Instruction& rootCall = ir.m_instructions.back();
bool doAlpha = false;
if (!rootCall.m_call.m_name.compare("HECLOpaque"))
{
2015-11-21 01:13:06 +00:00
m_blendSrc = boo::BlendFactor::One;
m_blendDst = boo::BlendFactor::Zero;
2015-11-10 23:17:53 +00:00
}
else if (!rootCall.m_call.m_name.compare("HECLAlpha"))
{
2015-11-21 01:13:06 +00:00
m_blendSrc = boo::BlendFactor::SrcAlpha;
m_blendDst = boo::BlendFactor::InvSrcAlpha;
2015-11-10 23:17:53 +00:00
doAlpha = true;
}
else if (!rootCall.m_call.m_name.compare("HECLAdditive"))
{
2015-11-21 01:13:06 +00:00
m_blendSrc = boo::BlendFactor::SrcAlpha;
m_blendDst = boo::BlendFactor::One;
2015-11-10 23:17:53 +00:00
doAlpha = true;
}
else
{
diag.reportBackendErr(rootCall.m_loc, "%s backend doesn't handle '%s' root",
backendName, rootCall.m_call.m_name.c_str());
return;
}
/* Follow Color Chain */
const IR::Instruction& colorRoot =
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(0));
m_colorExpr = RecursiveTraceColor(ir, diag, colorRoot);
/* Follow Alpha Chain */
if (doAlpha)
{
const IR::Instruction& alphaRoot =
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(1));
m_alphaExpr = RecursiveTraceAlpha(ir, diag, alphaRoot);
}
}
static const char SWIZZLE_CHARS[] = "rgba";
std::string ProgrammableCommon::EmitSwizzle3(Diagnostics& diag, const SourceLocation& loc,
const std::string& a, const atInt8 swiz[4]) const
{
std::string retval = a + '.';
for (int i=0 ; i<3 ; ++i)
{
if (swiz[i] < 0 || swiz[i] > 3)
diag.reportBackendErr(loc, "unable to use swizzle as RGB value");
retval += SWIZZLE_CHARS[swiz[i]];
}
return retval;
}
std::string ProgrammableCommon::EmitSwizzle1(Diagnostics& diag, const SourceLocation& loc,
const std::string& a, const atInt8 swiz[4]) const
{
std::string retval = a + '.';
if (swiz[0] < 0 || swiz[0] > 3)
diag.reportBackendErr(loc, "unable to use swizzle as Alpha value");
retval += SWIZZLE_CHARS[swiz[0]];
return retval;
}
}
}