metaforce/hecl/lib/Backend/HLSL.cpp

382 lines
15 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/Backend/HLSL.hpp"
#include "hecl/Runtime.hpp"
#include <athena/MemoryReader.hpp>
#include <athena/MemoryWriter.hpp>
2015-11-18 07:29:54 +00:00
#include <boo/graphicsdev/D3D.hpp>
2016-03-04 23:02:44 +00:00
static logvisor::Module Log("hecl::Backend::HLSL");
2015-11-18 07:29:54 +00:00
2017-12-29 07:56:31 +00:00
namespace hecl::Backend
2015-11-18 07:29:54 +00:00
{
std::string HLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
{
switch (src)
{
2015-11-21 02:59:43 +00:00
case TexGenSrc::Position:
return "objPos.xy\n";
2015-11-21 02:59:43 +00:00
case TexGenSrc::Normal:
return "objNorm.xy\n";
2015-11-21 02:59:43 +00:00
case TexGenSrc::UV:
2016-03-04 23:02:44 +00:00
return hecl::Format("v.uvIn[%u]", uvIdx);
2015-11-18 07:29:54 +00:00
default: break;
}
return std::string();
}
std::string HLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const
{
switch (src)
{
2015-11-21 02:59:43 +00:00
case TexGenSrc::Position:
return "float4(objPos.xyz, 1.0)\n";
2015-11-21 02:59:43 +00:00
case TexGenSrc::Normal:
return "float4(objNorm.xyz, 1.0)\n";
2015-11-21 02:59:43 +00:00
case TexGenSrc::UV:
2016-03-04 23:02:44 +00:00
return hecl::Format("float4(v.uvIn[%u], 0.0, 1.0)", uvIdx);
2015-11-18 07:29:54 +00:00
default: break;
}
return std::string();
}
std::string HLSL::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const
{
std::string retval =
"struct VertData\n"
"{\n"
" float3 posIn : POSITION;\n"
" float3 normIn : NORMAL;\n";
if (col)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float4 colIn[%u] : COLOR;\n", col);
2015-11-18 07:29:54 +00:00
if (uv)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float2 uvIn[%u] : UV;\n", uv);
2015-11-18 07:29:54 +00:00
if (w)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float4 weightIn[%u] : WEIGHT;\n", w);
2015-11-18 07:29:54 +00:00
return retval + "};\n";
}
2017-03-26 18:21:01 +00:00
std::string HLSL::GenerateVertToFragStruct(size_t extTexCount, bool reflectionCoords) const
2015-11-18 07:29:54 +00:00
{
std::string retval =
"struct VertToFrag\n"
"{\n"
" float4 mvpPos : SV_Position;\n"
" float4 mvPos : POSITION;\n"
" float4 mvNorm : NORMAL;\n";
if (m_tcgs.size())
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float2 tcgs[%u] : UV;\n", unsigned(m_tcgs.size()));
2016-08-01 04:34:08 +00:00
if (extTexCount)
retval += hecl::Format(" float2 extTcgs[%u] : EXTUV;\n", unsigned(extTexCount));
2015-11-18 07:29:54 +00:00
2017-03-26 05:51:58 +00:00
if (reflectionCoords)
retval += " float2 reflectTcgs[2] : REFLECTUV;\n"
2017-10-24 03:11:44 +00:00
" float reflectAlpha : REFLECTALPHA;\n";
2017-03-26 05:51:58 +00:00
2015-11-18 07:29:54 +00:00
return retval + "};\n";
}
std::string HLSL::GenerateVertUniformStruct(unsigned skinSlots, bool reflectionCoords) const
2015-11-18 07:29:54 +00:00
{
std::string retval;
2015-11-18 07:29:54 +00:00
if (skinSlots == 0)
{
retval = "cbuffer HECLVertUniform : register(b0)\n"
"{\n"
" float4x4 mv;\n"
" float4x4 mvInv;\n"
" float4x4 proj;\n"
"};\n";
}
else
{
retval = hecl::Format("cbuffer HECLVertUniform : register(b0)\n"
"{\n"
" float4x4 objs[%u];\n"
" float4x4 objsInv[%u];\n"
" float4x4 mv;\n"
" float4x4 mvInv;\n"
" float4x4 proj;\n"
"};\n",
skinSlots, skinSlots);
}
retval += "struct TCGMtx\n"
"{\n"
" float4x4 mtx;\n"
" float4x4 postMtx;\n"
"};\n"
"cbuffer HECLTCGMatrix : register(b1)\n"
"{\n"
" TCGMtx texMtxs[8];\n"
"};\n";
2017-03-26 05:51:58 +00:00
if (reflectionCoords)
retval += "cbuffer HECLReflectMtx : register(b3)\n"
"{\n"
" float4x4 indMtx;\n"
" float4x4 reflectMtx;\n"
" float reflectAlpha;\n"
"};\n"
"\n";
2016-07-19 20:12:04 +00:00
return retval;
2015-11-18 07:29:54 +00:00
}
2016-08-01 04:34:08 +00:00
std::string HLSL::GenerateAlphaTest() const
{
return " if (colorOut.a < 0.01)\n"
" {\n"
" discard;\n"
" }\n";
}
2017-03-26 05:51:58 +00:00
std::string HLSL::GenerateReflectionExpr(ReflectionType type) const
{
switch (type)
{
case ReflectionType::None:
2017-03-26 18:21:01 +00:00
default:
2017-05-29 08:18:54 +00:00
return "float3(0.0, 0.0, 0.0)";
2017-03-26 05:51:58 +00:00
case ReflectionType::Simple:
2018-10-18 23:55:42 +00:00
return "reflectionTex.Sample(reflectSamp, vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha";
2017-03-26 05:51:58 +00:00
case ReflectionType::Indirect:
2018-10-18 23:55:42 +00:00
return "reflectionTex.Sample(reflectSamp, (reflectionIndTex.Sample(samp, vtf.reflectTcgs[0]).rg - "
2017-05-29 08:18:54 +00:00
"float2(0.5, 0.5)) * float2(0.5, 0.5) + vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha";
2017-03-26 05:51:58 +00:00
}
}
2015-11-18 07:29:54 +00:00
void HLSL::reset(const IR& ir, Diagnostics& diag)
{
/* Common programmable interpretation */
ProgrammableCommon::reset(ir, diag, "HLSL");
}
std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
unsigned s, size_t extTexCount,
2017-03-26 05:51:58 +00:00
const TextureInfo* extTexs, ReflectionType reflectionType) const
2015-11-18 07:29:54 +00:00
{
std::string retval =
GenerateVertInStruct(col, uv, w) + "\n" +
2017-03-26 05:51:58 +00:00
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" +
GenerateVertUniformStruct(s, reflectionType != ReflectionType::None) + "\n" +
2015-11-18 07:29:54 +00:00
"VertToFrag main(in VertData v)\n"
"{\n"
" VertToFrag vtf;\n";
if (s)
{
/* skinned */
retval += " float4 objPos = float4(0.0,0.0,0.0,0.0);\n"
" float4 objNorm = float4(0.0,0.0,0.0,0.0);\n";
2015-11-18 07:29:54 +00:00
for (size_t i=0 ; i<s ; ++i)
retval += hecl::Format(" objPos += mul(mv[%" PRISize "], float4(v.posIn, 1.0)) * v.weightIn[%" PRISize "][%" PRISize "];\n"
" objNorm += mul(mvInv[%" PRISize "], float4(v.normIn, 1.0)) * v.weightIn[%" PRISize "][%" PRISize "];\n",
2015-11-18 07:29:54 +00:00
i, i/4, i%4, i, i/4, i%4);
retval += " objPos[3] = 1.0;\n"
" objNorm = float4(normalize(objNorm.xyz), 0.0);\n"
" vtf.mvPos = mul(mv, objPos);\n"
" vtf.mvNorm = float4(normalize(mul(mvInv, objNorm).xyz), 0.0);\n"
" vtf.mvpPos = mul(proj, vtf.mvPos);\n";
2015-11-18 07:29:54 +00:00
}
else
{
/* non-skinned */
retval += " float4 objPos = float4(posIn, 1.0);\n"
" float4 objNorm = float4(normIn, 0.0);\n"
" vtf.mvPos = mul(mv, objPos);\n"
" vtf.mvNorm = mul(mvInv, objNorm);\n"
" gl_Position = mul(proj, vtf.mvPos);\n";
2015-11-18 07:29:54 +00:00
}
retval += " float4 tmpProj;\n";
2015-11-18 07:29:54 +00:00
int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs)
{
if (tcg.m_mtx < 0)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
2015-11-18 07:29:54 +00:00
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
else
retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
" vtf.tcgs[%u] = (tmpProj / tmpProj.w).xy;\n", tcg.m_mtx,
tcg.m_norm ? "normalize" : "", tcg.m_mtx,
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str(), tcgIdx);
2015-11-18 07:29:54 +00:00
++tcgIdx;
}
2016-08-01 04:34:08 +00:00
for (int i=0 ; i<extTexCount ; ++i)
{
const TextureInfo& extTex = extTexs[i];
if (extTex.mtxIdx < 0)
retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else
retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
" vtf.extTcgs[%u] = (tmpProj / tmpProj.w).xy;\n",
extTex.mtxIdx, extTex.normalize ? "normalize" : "", extTex.mtxIdx,
EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str(), i);
2016-08-01 04:34:08 +00:00
}
2017-03-26 18:21:01 +00:00
if (reflectionType != ReflectionType::None)
2017-03-26 05:51:58 +00:00
retval += " vtf.reflectTcgs[0] = normalize(mul(indMtx, float4(v.posIn, 1.0)).xz) * float2(0.5, 0.5) + float2(0.5, 0.5);\n"
" vtf.reflectTcgs[1] = mul(reflectMtx, float4(v.posIn, 1.0)).xy;\n"
" vtf.reflectAlpha = reflectAlpha;\n";
2015-11-18 07:29:54 +00:00
return retval + " return vtf;\n"
"}\n";
}
2018-10-16 03:15:05 +00:00
std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
2018-10-07 02:53:57 +00:00
const Function& lighting) const
2015-11-18 07:29:54 +00:00
{
std::string lightingSrc;
2018-10-07 02:53:57 +00:00
if (!lighting.m_source.empty())
2015-11-18 07:29:54 +00:00
lightingSrc = lighting.m_source;
else
2016-08-03 23:15:59 +00:00
lightingSrc = "static const float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 mulColor = float4(1.0, 1.0, 1.0, 1.0);\n";
2015-11-18 07:29:54 +00:00
std::string texMapDecl;
if (m_texMapEnd)
2016-03-04 23:02:44 +00:00
texMapDecl = hecl::Format("Texture2D texs[%u] : register(t0);\n", m_texMapEnd);
2017-03-26 05:51:58 +00:00
if (reflectionType == ReflectionType::Indirect)
texMapDecl += hecl::Format("Texture2D reflectionIndTex : register(t%u);\n"
"Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd, m_texMapEnd+1);
else if (reflectionType == ReflectionType::Simple)
texMapDecl += hecl::Format("Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd);
2015-11-18 07:29:54 +00:00
std::string retval =
std::string("#define BLEND_SRC_") + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
"SamplerState samp : register(s0);\n"
2018-10-18 23:55:42 +00:00
"SamplerState clampSamp : register(s1);\n"
"SamplerState reflectSamp : register(s2);\n" +
2017-03-26 05:51:58 +00:00
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
2015-11-18 07:29:54 +00:00
texMapDecl + "\n" +
lightingSrc + "\n" +
2016-08-01 04:34:08 +00:00
(!alphaTest ? "\n[earlydepthstencil]\n" : "\n") +
2015-11-18 07:29:54 +00:00
"float4 main(in VertToFrag vtf) : SV_Target0\n{\n";
if (m_lighting)
{
2018-10-07 02:53:57 +00:00
if (!lighting.m_entry.empty())
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz), vtf);\n",
lighting.m_entry.data());
2015-11-18 07:29:54 +00:00
else
2016-04-03 03:31:50 +00:00
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
2015-11-18 07:29:54 +00:00
}
unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float4 sampling%u = texs[%u].Sample(samp, vtf.tcgs[%u]);\n",
2015-11-18 07:29:54 +00:00
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2017-03-26 05:51:58 +00:00
std::string reflectionExpr = GenerateReflectionExpr(reflectionType);
2016-08-01 04:34:08 +00:00
retval += " float4 colorOut;\n";
2015-11-18 07:29:54 +00:00
if (m_alphaExpr.size())
2017-03-26 05:51:58 +00:00
retval += " colorOut = float4(" + m_colorExpr + " + " + reflectionExpr + ", " + m_alphaExpr + ") * mulColor;\n";
2015-11-18 07:29:54 +00:00
else
2017-03-26 05:51:58 +00:00
retval += " colorOut = float4(" + m_colorExpr + " + " + reflectionExpr + ", 1.0) * mulColor;\n";
2015-11-18 07:29:54 +00:00
2016-08-01 04:34:08 +00:00
return retval + (alphaTest ? GenerateAlphaTest() : "") + " return colorOut;\n}\n";
2015-11-18 07:29:54 +00:00
}
2018-10-16 03:15:05 +00:00
std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
2018-10-07 02:53:57 +00:00
const Function& lighting,
const Function& post, size_t extTexCount,
2016-08-01 04:34:08 +00:00
const TextureInfo* extTexs) const
2015-11-18 07:29:54 +00:00
{
std::string lightingSrc;
2018-10-07 02:53:57 +00:00
if (!lighting.m_source.empty())
2015-11-18 07:29:54 +00:00
lightingSrc = lighting.m_source;
else
2016-08-03 23:15:59 +00:00
lightingSrc = "static const float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);\n"
"static const float4 mulColor = float4(1.0, 1.0, 1.0, 1.0);\n";
2015-11-18 07:29:54 +00:00
std::string postSrc;
2018-10-07 02:53:57 +00:00
if (!post.m_source.empty())
2015-11-18 07:29:54 +00:00
postSrc = post.m_source;
std::string postEntry;
2018-10-07 02:53:57 +00:00
if (!post.m_entry.empty())
2015-11-18 07:29:54 +00:00
postEntry = post.m_entry;
std::string texMapDecl;
if (m_texMapEnd)
2016-03-04 23:02:44 +00:00
texMapDecl = hecl::Format("Texture2D texs[%u] : register(t0);\n", m_texMapEnd);
2017-03-26 05:51:58 +00:00
if (reflectionType == ReflectionType::Indirect)
texMapDecl += hecl::Format("Texture2D reflectionIndTex : register(t%u);\n"
"Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd, m_texMapEnd+1);
else if (reflectionType == ReflectionType::Simple)
texMapDecl += hecl::Format("Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd);
2015-11-18 07:29:54 +00:00
uint32_t extTexBits = 0;
2016-08-01 04:34:08 +00:00
for (int i=0 ; i<extTexCount ; ++i)
{
const TextureInfo& extTex = extTexs[i];
if (!(extTexBits & (1 << extTex.mapIdx)))
{
texMapDecl += hecl::Format("Texture2D extTex%u : register(t%u);\n",
extTex.mapIdx, extTex.mapIdx);
extTexBits |= (1 << extTex.mapIdx);
}
2016-08-01 04:34:08 +00:00
}
2015-11-18 07:29:54 +00:00
std::string retval =
std::string("#define BLEND_SRC_") + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
"SamplerState samp : register(s0);\n"
2018-10-18 23:55:42 +00:00
"SamplerState clampSamp : register(s1);\n"
"SamplerState reflectSamp : register(s2);\n" +
2017-03-26 05:51:58 +00:00
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
2015-11-18 07:29:54 +00:00
texMapDecl + "\n" +
lightingSrc + "\n" +
postSrc +
2016-08-01 04:34:08 +00:00
(!alphaTest ? "\n[earlydepthstencil]\n" : "\n") +
"float4 main(in VertToFrag vtf) : SV_Target0\n{\n";
2015-11-18 07:29:54 +00:00
2016-07-12 08:54:42 +00:00
2015-11-18 07:29:54 +00:00
if (m_lighting)
{
2018-10-07 02:53:57 +00:00
if (!lighting.m_entry.empty())
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz), vtf);\n",
lighting.m_entry.data());
2015-11-18 07:29:54 +00:00
else
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
}
unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" float4 sampling%u = texs[%u].Sample(samp, vtf.tcgs[%u]);\n",
2015-11-18 07:29:54 +00:00
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2017-03-26 05:51:58 +00:00
std::string reflectionExpr = GenerateReflectionExpr(reflectionType);
2016-08-01 04:34:08 +00:00
retval += " float4 colorOut;\n";
2015-11-18 07:29:54 +00:00
if (m_alphaExpr.size())
2017-03-26 05:51:58 +00:00
retval += " colorOut = " + postEntry + "(" + (postEntry.size() ? "vtf, " : "") + "float4(" + m_colorExpr + " + " + reflectionExpr + ", " + m_alphaExpr + ")) * mulColor;\n";
2015-11-18 07:29:54 +00:00
else
2017-03-26 05:51:58 +00:00
retval += " colorOut = " + postEntry + "(" + (postEntry.size() ? "vtf, " : "") + "float4(" + m_colorExpr + " + " + reflectionExpr + ", 1.0)) * mulColor;\n";
2015-11-18 07:29:54 +00:00
2016-08-01 04:34:08 +00:00
return retval + (alphaTest ? GenerateAlphaTest() : "") + " return colorOut;\n}\n";
2015-11-18 07:29:54 +00:00
}
}