2016-03-04 23:02:44 +00:00
|
|
|
#include "hecl/Backend/Metal.hpp"
|
2016-02-23 02:33:29 +00:00
|
|
|
#if BOO_HAS_METAL
|
2016-03-04 23:02:44 +00:00
|
|
|
#include "hecl/Runtime.hpp"
|
|
|
|
#include <athena/MemoryReader.hpp>
|
|
|
|
#include <athena/MemoryWriter.hpp>
|
2015-11-18 23:56:45 +00:00
|
|
|
#include <boo/graphicsdev/Metal.hpp>
|
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
static logvisor::Module Log("hecl::Backend::Metal");
|
2015-11-18 23:56:45 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
namespace hecl
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
namespace Backend
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string Metal::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
|
|
|
|
{
|
|
|
|
switch (src)
|
|
|
|
{
|
2015-11-21 02:16:54 +00:00
|
|
|
case TexGenSrc::Position:
|
2015-11-18 23:56:45 +00:00
|
|
|
return "v.posIn.xy\n";
|
2015-11-21 02:16:54 +00:00
|
|
|
case TexGenSrc::Normal:
|
2015-11-18 23:56:45 +00:00
|
|
|
return "v.normIn.xy\n";
|
2015-11-21 02:16:54 +00:00
|
|
|
case TexGenSrc::UV:
|
2016-03-04 23:02:44 +00:00
|
|
|
return hecl::Format("v.uvIn%u", uvIdx);
|
2015-11-18 23:56:45 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Metal::EmitTexGenSource4(TexGenSrc src, int uvIdx) const
|
|
|
|
{
|
|
|
|
switch (src)
|
|
|
|
{
|
2015-11-21 02:16:54 +00:00
|
|
|
case TexGenSrc::Position:
|
2015-11-18 23:56:45 +00:00
|
|
|
return "float4(v.posIn, 1.0)\n";
|
2015-11-21 02:16:54 +00:00
|
|
|
case TexGenSrc::Normal:
|
2015-11-18 23:56:45 +00:00
|
|
|
return "float4(v.normIn, 1.0)\n";
|
2015-11-21 02:16:54 +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 23:56:45 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Metal::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const
|
|
|
|
{
|
|
|
|
std::string retval =
|
|
|
|
"struct VertData\n"
|
|
|
|
"{\n"
|
|
|
|
" float3 posIn [[ attribute(0) ]];\n"
|
|
|
|
" float3 normIn [[ attribute(1) ]];\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
unsigned idx = 2;
|
|
|
|
if (col)
|
|
|
|
{
|
|
|
|
for (unsigned i=0 ; i<col ; ++i, ++idx)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" float4 colIn%u [[ attribute(%u) ]];\n", i, idx);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (uv)
|
|
|
|
{
|
|
|
|
for (unsigned i=0 ; i<uv ; ++i, ++idx)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" float2 uvIn%u [[ attribute(%u) ]];\n", i, idx);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (w)
|
|
|
|
{
|
|
|
|
for (unsigned i=0 ; i<w ; ++i, ++idx)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" float4 weightIn%u [[ attribute(%u) ]];\n", i, idx);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
return retval + "};\n";
|
|
|
|
}
|
|
|
|
|
2016-07-31 20:26:08 +00:00
|
|
|
std::string Metal::GenerateVertToFragStruct(size_t extTexCount) const
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
std::string retval =
|
|
|
|
"struct VertToFrag\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 mvpPos [[ position ]];\n"
|
|
|
|
" float4 mvPos;\n"
|
|
|
|
" float4 mvNorm;\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (m_tcgs.size())
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<m_tcgs.size() ; ++i)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" float2 tcgs%" PRISize ";\n", i);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-07-31 20:26:08 +00:00
|
|
|
if (extTexCount)
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<extTexCount ; ++i)
|
|
|
|
retval += hecl::Format(" float2 extTcgs%" PRISize ";\n", i);
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
return retval + "};\n";
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:15:07 +00:00
|
|
|
std::string Metal::GenerateVertUniformStruct(unsigned skinSlots) const
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
if (skinSlots == 0)
|
|
|
|
skinSlots = 1;
|
2016-03-04 23:02:44 +00:00
|
|
|
std::string retval = hecl::Format("struct HECLVertUniform\n"
|
2015-11-18 23:56:45 +00:00
|
|
|
"{\n"
|
|
|
|
" float4x4 mv[%u];\n"
|
|
|
|
" float4x4 mvInv[%u];\n"
|
2016-04-04 06:15:07 +00:00
|
|
|
" float4x4 proj;\n"
|
2016-07-19 20:33:04 +00:00
|
|
|
"};\n"
|
|
|
|
"struct TexMtxs {float4x4 mtx; float4x4 postMtx;};\n",
|
2015-11-18 23:56:45 +00:00
|
|
|
skinSlots, skinSlots);
|
2016-04-04 06:15:07 +00:00
|
|
|
return retval;
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string Metal::GenerateFragOutStruct() const
|
|
|
|
{
|
|
|
|
return "struct FragOut\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 color [[ color(0) ]];\n"
|
|
|
|
" //float depth [[ depth(less) ]];\n"
|
|
|
|
"};\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Metal::GenerateAlphaTest() const
|
|
|
|
{
|
|
|
|
return " if (out.color.a < 0.01)\n"
|
|
|
|
" {\n"
|
|
|
|
" discard_fragment();\n"
|
|
|
|
" }\n";
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
void Metal::reset(const IR& ir, Diagnostics& diag)
|
|
|
|
{
|
|
|
|
/* Common programmable interpretation */
|
|
|
|
ProgrammableCommon::reset(ir, diag, "Metal");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
|
2016-07-31 20:26:08 +00:00
|
|
|
unsigned s, unsigned tm, size_t extTexCount,
|
|
|
|
const TextureInfo* extTexs) const
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
2016-04-04 06:15:07 +00:00
|
|
|
std::string tmStr;
|
|
|
|
if (tm)
|
2016-07-19 20:33:04 +00:00
|
|
|
tmStr = hecl::Format(",\nconstant TexMtxs* texMtxs [[ buffer(3) ]]");
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" +
|
|
|
|
GenerateVertInStruct(col, uv, w) + "\n" +
|
2016-07-31 20:26:08 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount) + "\n" +
|
2016-04-04 06:15:07 +00:00
|
|
|
GenerateVertUniformStruct(s) +
|
|
|
|
"\nvertex VertToFrag vmain(VertData v [[ stage_in ]],\n"
|
|
|
|
" constant HECLVertUniform& vu [[ buffer(2) ]]" + tmStr + ")\n"
|
|
|
|
"{\n"
|
2015-11-18 23:56:45 +00:00
|
|
|
" VertToFrag vtf;\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
/* skinned */
|
|
|
|
retval += " float4 posAccum = float4(0.0,0.0,0.0,0.0);\n"
|
|
|
|
" float4 normAccum = float4(0.0,0.0,0.0,0.0);\n";
|
|
|
|
for (size_t i=0 ; i<s ; ++i)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" posAccum += (vu.mv[%" PRISize "] * float4(v.posIn, 1.0)) * v.weightIn%" PRISize "[%" PRISize "];\n"
|
2015-11-18 23:56:45 +00:00
|
|
|
" normAccum += (vu.mvInv[%" PRISize "] * float4(v.normIn, 1.0)) * v.weightIn%" PRISize "[%" PRISize "];\n",
|
|
|
|
i, i/4, i%4, i, i/4, i%4);
|
|
|
|
retval += " posAccum[3] = 1.0;\n"
|
|
|
|
" vtf.mvPos = posAccum;\n"
|
|
|
|
" vtf.mvNorm = float4(normalize(normAccum.xyz), 0.0);\n"
|
|
|
|
" vtf.mvpPos = vu.proj * posAccum;\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* non-skinned */
|
|
|
|
retval += " vtf.mvPos = vu.mv[0] * float4(v.posIn, 1.0);\n"
|
|
|
|
" vtf.mvNorm = vu.mvInv[0] * float4(v.normIn, 0.0);\n"
|
|
|
|
" vtf.mvpPos = vu.proj * vtf.mvPos;\n";
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +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 23:56:45 +00:00
|
|
|
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
|
|
|
|
else
|
2016-07-19 20:33:04 +00:00
|
|
|
retval += hecl::Format(" vtf.tcgs%u = (texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", tcgIdx, tcg.m_mtx,
|
2016-07-19 06:12:00 +00:00
|
|
|
tcg.m_norm ? "normalize" : "", tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
|
2015-11-18 23:56:45 +00:00
|
|
|
++tcgIdx;
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-07-31 20:26: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(" vtf.extTcgs%u = (texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", i, extTex.mtxIdx,
|
|
|
|
extTex.normalize ? "normalize" : "", extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
return retval + " return vtf;\n}\n";
|
|
|
|
}
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
|
2016-04-04 06:15:07 +00:00
|
|
|
const ShaderFunction& lighting) const
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
std::string lightingSrc;
|
|
|
|
if (lighting.m_source)
|
|
|
|
lightingSrc = lighting.m_source;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string texMapDecl;
|
|
|
|
if (m_texMapEnd)
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<m_texMapEnd ; ++i)
|
2016-04-04 06:15:07 +00:00
|
|
|
texMapDecl += hecl::Format(",\ntexture2d<float> tex%u [[ texture(%u) ]]", i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string blockCall;
|
|
|
|
for (size_t i=0 ; i<blockCount ; ++i)
|
|
|
|
{
|
|
|
|
texMapDecl += hecl::Format(",\nconstant %s& block%" PRISize " [[ buffer(%" PRISize ") ]]", blockNames[i], i, i + 4);
|
|
|
|
if (blockCall.size())
|
|
|
|
blockCall += ", ";
|
|
|
|
blockCall += hecl::Format("block%" PRISize, i);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n"
|
2016-04-03 06:19:03 +00:00
|
|
|
"constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n" +
|
2016-07-31 20:26:08 +00:00
|
|
|
GenerateVertToFragStruct(0) + "\n" +
|
2016-07-31 23:35:46 +00:00
|
|
|
GenerateFragOutStruct() + "\n" +
|
2015-11-18 23:56:45 +00:00
|
|
|
lightingSrc + "\n" +
|
2016-07-31 23:35:46 +00:00
|
|
|
"fragment FragOut fmain(VertToFrag vtf [[ stage_in ]]" + texMapDecl + ")\n"
|
|
|
|
"{\n"
|
|
|
|
" FragOut out;\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-08-03 23:46:25 +00:00
|
|
|
if (lighting.m_source)
|
|
|
|
{
|
|
|
|
retval += " float4 colorReg0 = block0.colorReg0;\n"
|
|
|
|
" float4 colorReg1 = block0.colorReg1;\n"
|
|
|
|
" float4 colorReg2 = block0.colorReg2;\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval += " float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);\n"
|
|
|
|
" float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);\n"
|
|
|
|
" float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);\n";
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (m_lighting)
|
|
|
|
{
|
|
|
|
if (lighting.m_entry)
|
2016-04-04 06:15:07 +00:00
|
|
|
retval += hecl::Format(" float4 lighting = %s(%s, vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry, blockCall.c_str());
|
2015-11-18 23:56:45 +00:00
|
|
|
else
|
|
|
|
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +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 = tex%u.sample(samp, vtf.tcgs%u);\n",
|
2015-11-18 23:56:45 +00:00
|
|
|
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (m_alphaExpr.size())
|
2016-07-31 23:35:46 +00:00
|
|
|
retval += " out.color = float4(" + m_colorExpr + ", " + m_alphaExpr + ");\n";
|
2015-11-18 23:56:45 +00:00
|
|
|
else
|
2016-07-31 23:35:46 +00:00
|
|
|
retval += " out.color = float4(" + m_colorExpr + ", 1.0);\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
return retval + (alphaTest ? GenerateAlphaTest() : "") +
|
|
|
|
" //out.depth = 1.0 - float(int((1.0 - vtf.mvpPos.z) * 16777216.0)) / 16777216.0;\n"
|
|
|
|
" return out;\n"
|
|
|
|
"}\n";
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
|
2016-04-04 06:15:07 +00:00
|
|
|
const ShaderFunction& lighting,
|
2016-07-31 20:26:08 +00:00
|
|
|
const ShaderFunction& post, size_t extTexCount,
|
|
|
|
const TextureInfo* extTexs) const
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
std::string lightingSrc;
|
|
|
|
if (lighting.m_source)
|
|
|
|
lightingSrc = lighting.m_source;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string postSrc;
|
|
|
|
if (post.m_source)
|
|
|
|
postSrc = post.m_source;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string postEntry;
|
|
|
|
if (post.m_entry)
|
|
|
|
postEntry = post.m_entry;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string texMapDecl;
|
|
|
|
if (m_texMapEnd)
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<m_texMapEnd ; ++i)
|
2016-04-04 06:15:07 +00:00
|
|
|
texMapDecl += hecl::Format(",\ntexture2d<float> tex%u [[ texture(%u) ]]", i, i);
|
|
|
|
}
|
|
|
|
|
2016-07-31 20:26:08 +00:00
|
|
|
std::string extTexCall;
|
|
|
|
for (int i=0 ; i<extTexCount ; ++i)
|
|
|
|
{
|
|
|
|
const TextureInfo& extTex = extTexs[i];
|
|
|
|
if (extTexCall.size())
|
|
|
|
extTexCall += ", ";
|
|
|
|
extTexCall += hecl::Format("tex%u", extTex.mapIdx);
|
|
|
|
texMapDecl += hecl::Format(",\ntexture2d<float> tex%u [[ texture(%u) ]]", extTex.mapIdx, extTex.mapIdx);
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:15:07 +00:00
|
|
|
std::string blockCall;
|
|
|
|
for (size_t i=0 ; i<blockCount ; ++i)
|
|
|
|
{
|
|
|
|
texMapDecl += hecl::Format(",\nconstant %s& block%" PRISize " [[ buffer(%" PRISize ") ]]", blockNames[i], i, i + 4);
|
|
|
|
if (blockCall.size())
|
|
|
|
blockCall += ", ";
|
|
|
|
blockCall += hecl::Format("block%" PRISize, i);
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n"
|
2016-04-03 06:19:03 +00:00
|
|
|
"constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n" +
|
2016-07-31 20:26:08 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount) + "\n" +
|
2016-07-31 23:35:46 +00:00
|
|
|
GenerateFragOutStruct() + "\n" +
|
2015-11-18 23:56:45 +00:00
|
|
|
lightingSrc + "\n" +
|
2016-07-31 20:26:08 +00:00
|
|
|
postSrc + "\n" +
|
2016-07-31 23:35:46 +00:00
|
|
|
"fragment FragOut fmain(VertToFrag vtf [[ stage_in ]]" + texMapDecl + ")\n"
|
|
|
|
"{\n"
|
|
|
|
" FragOut out;\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-08-03 23:46:25 +00:00
|
|
|
if (lighting.m_source)
|
|
|
|
{
|
|
|
|
retval += " float4 colorReg0 = block0.colorReg0;\n"
|
|
|
|
" float4 colorReg1 = block0.colorReg1;\n"
|
|
|
|
" float4 colorReg2 = block0.colorReg2;\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval += " float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);\n"
|
|
|
|
" float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);\n"
|
|
|
|
" float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);\n";
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (m_lighting)
|
|
|
|
{
|
|
|
|
if (lighting.m_entry)
|
2016-04-04 06:15:07 +00:00
|
|
|
retval += hecl::Format(" float4 lighting = %s(%s, vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry, blockCall.c_str());
|
2015-11-18 23:56:45 +00:00
|
|
|
else
|
|
|
|
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +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 = tex%u.sample(samp, vtf.tcgs%u);\n",
|
2015-11-18 23:56:45 +00:00
|
|
|
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
if (m_alphaExpr.size())
|
2016-07-31 23:35:46 +00:00
|
|
|
retval += " out.color = " + postEntry + "(" + (postEntry.size() ? ("vtf, " + (blockCall.size() ? (blockCall + ", ") : "") + (extTexCall.size() ? (extTexCall + ", ") : "")) : "") +
|
2016-07-31 20:26:08 +00:00
|
|
|
"float4(" + m_colorExpr + ", " + m_alphaExpr + "));\n";
|
2015-11-18 23:56:45 +00:00
|
|
|
else
|
2016-07-31 23:35:46 +00:00
|
|
|
retval += " out.color = " + postEntry + "(" + (postEntry.size() ? ("vtf, " + (blockCall.size() ? (blockCall + ", ") : "") + (extTexCall.size() ? (extTexCall + ", ") : "")) : "") +
|
2016-07-31 20:26:08 +00:00
|
|
|
"float4(" + m_colorExpr + ", 1.0));\n";
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
return retval + (alphaTest ? GenerateAlphaTest() : "") +
|
|
|
|
" //out.depth = 1.0 - float(int((1.0 - vtf.mvpPos.z) * 16777216.0)) / 16777216.0;\n"
|
|
|
|
" return out;\n"
|
|
|
|
"}\n";
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
namespace Runtime
|
|
|
|
{
|
|
|
|
|
|
|
|
struct MetalBackendFactory : IShaderBackendFactory
|
|
|
|
{
|
|
|
|
Backend::Metal m_backend;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
ShaderCachedData buildShaderFromIR(const ShaderTag& tag,
|
2016-03-04 23:02:44 +00:00
|
|
|
const hecl::Frontend::IR& ir,
|
|
|
|
hecl::Frontend::Diagnostics& diag,
|
2016-04-03 06:19:03 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2016-02-23 02:33:29 +00:00
|
|
|
boo::IShaderPipeline*& objOut)
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
if (!m_rtHint)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal,
|
2015-11-18 23:56:45 +00:00
|
|
|
"ShaderCacheManager::setRenderTargetHint must be called before making metal shaders");
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
m_backend.reset(ir, diag);
|
|
|
|
size_t cachedSz = 2;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string vertSource =
|
|
|
|
m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
|
2016-07-31 20:26:08 +00:00
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr);
|
2015-11-18 23:56:45 +00:00
|
|
|
cachedSz += vertSource.size() + 1;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string fragSource = m_backend.makeFrag(0, nullptr,
|
|
|
|
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha);
|
2015-11-18 23:56:45 +00:00
|
|
|
cachedSz += fragSource.size() + 1;
|
2016-02-23 02:33:29 +00:00
|
|
|
objOut =
|
2016-04-03 06:19:03 +00:00
|
|
|
static_cast<boo::MetalDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
|
|
|
tag.newVertexFormat(ctx), m_rtHint,
|
2016-07-31 20:26:08 +00:00
|
|
|
boo::BlendFactor(m_backend.m_blendSrc),
|
|
|
|
boo::BlendFactor(m_backend.m_blendDst),
|
2016-04-03 06:19:03 +00:00
|
|
|
tag.getPrimType(),
|
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
|
|
|
if (!objOut)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
ShaderCachedData dataOut(tag, cachedSz);
|
2016-03-04 23:02:44 +00:00
|
|
|
athena::io::MemoryWriter w(dataOut.m_data.get(), dataOut.m_sz);
|
2015-11-21 02:16:54 +00:00
|
|
|
w.writeUByte(atUint8(m_backend.m_blendSrc));
|
|
|
|
w.writeUByte(atUint8(m_backend.m_blendDst));
|
2015-11-18 23:56:45 +00:00
|
|
|
w.writeString(vertSource);
|
|
|
|
w.writeString(fragSource);
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
return dataOut;
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
|
|
|
boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
|
|
|
if (!m_rtHint)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal,
|
2015-11-18 23:56:45 +00:00
|
|
|
"ShaderCacheManager::setRenderTargetHint must be called before making metal shaders");
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 19:06:51 +00:00
|
|
|
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
|
2015-11-18 23:56:45 +00:00
|
|
|
boo::BlendFactor blendSrc = boo::BlendFactor(r.readUByte());
|
|
|
|
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
|
|
|
|
std::string vertSource = r.readString();
|
|
|
|
std::string fragSource = r.readString();
|
2016-08-08 19:06:51 +00:00
|
|
|
|
|
|
|
if (r.hasError())
|
|
|
|
return nullptr;
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-04-03 06:19:03 +00:00
|
|
|
static_cast<boo::MetalDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
|
|
|
tag.newVertexFormat(ctx), m_rtHint,
|
|
|
|
blendSrc, blendDst, tag.getPrimType(),
|
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 23:56:45 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 23:56:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
ShaderCachedData buildExtendedShaderFromIR(const ShaderTag& tag,
|
2016-03-04 23:02:44 +00:00
|
|
|
const hecl::Frontend::IR& ir,
|
|
|
|
hecl::Frontend::Diagnostics& diag,
|
2015-11-18 23:56:45 +00:00
|
|
|
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
|
2016-04-03 06:19:03 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2015-11-18 23:56:45 +00:00
|
|
|
FReturnExtensionShader returnFunc)
|
|
|
|
{
|
|
|
|
if (!m_rtHint)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal,
|
2015-11-18 23:56:45 +00:00
|
|
|
"ShaderCacheManager::setRenderTargetHint must be called before making metal shaders");
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
m_backend.reset(ir, diag);
|
|
|
|
size_t cachedSz = 2;
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-07-31 20:26:08 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> sources;
|
|
|
|
sources.reserve(extensionSlots.size());
|
2015-11-18 23:56:45 +00:00
|
|
|
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
|
|
|
|
{
|
2016-07-31 20:26:08 +00:00
|
|
|
sources.emplace_back(m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
|
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), slot.texCount, slot.texs),
|
2016-07-31 23:35:46 +00:00
|
|
|
m_backend.makeFrag(slot.blockCount, slot.blockNames,
|
|
|
|
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
|
|
|
|
slot.lighting, slot.post, slot.texCount, slot.texs));
|
2016-07-31 20:26:08 +00:00
|
|
|
cachedSz += sources.back().first.size() + 1;
|
|
|
|
cachedSz += sources.back().second.size() + 1;
|
2015-11-18 23:56:45 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-04-03 06:19:03 +00:00
|
|
|
static_cast<boo::MetalDataFactory::Context&>(ctx).
|
2016-07-31 20:26:08 +00:00
|
|
|
newShaderPipeline(sources.back().first.c_str(), sources.back().second.c_str(),
|
2016-04-03 06:19:03 +00:00
|
|
|
tag.newVertexFormat(ctx), m_rtHint,
|
2016-07-31 20:26:08 +00:00
|
|
|
boo::BlendFactor((slot.srcFactor == hecl::Backend::BlendFactor::Original) ? m_backend.m_blendSrc : slot.srcFactor),
|
|
|
|
boo::BlendFactor((slot.dstFactor == hecl::Backend::BlendFactor::Original) ? m_backend.m_blendDst : slot.dstFactor),
|
2016-04-03 06:19:03 +00:00
|
|
|
tag.getPrimType(),
|
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 23:56:45 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 23:56:45 +00:00
|
|
|
returnFunc(ret);
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
ShaderCachedData dataOut(tag, cachedSz);
|
2016-03-04 23:02:44 +00:00
|
|
|
athena::io::MemoryWriter w(dataOut.m_data.get(), dataOut.m_sz);
|
2015-11-21 02:16:54 +00:00
|
|
|
w.writeUByte(atUint8(m_backend.m_blendSrc));
|
|
|
|
w.writeUByte(atUint8(m_backend.m_blendDst));
|
2016-07-31 20:26:08 +00:00
|
|
|
for (auto& src : sources)
|
|
|
|
{
|
|
|
|
w.writeString(src.first);
|
|
|
|
w.writeString(src.second);
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
return dataOut;
|
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2016-08-08 19:06:51 +00:00
|
|
|
bool buildExtendedShaderFromCache(const ShaderCachedData& data,
|
2015-11-18 23:56:45 +00:00
|
|
|
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
|
2016-04-03 06:19:03 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2015-11-18 23:56:45 +00:00
|
|
|
FReturnExtensionShader returnFunc)
|
|
|
|
{
|
|
|
|
if (!m_rtHint)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal,
|
2015-11-18 23:56:45 +00:00
|
|
|
"ShaderCacheManager::setRenderTargetHint must be called before making metal shaders");
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 19:06:51 +00:00
|
|
|
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
|
2016-07-31 20:26:08 +00:00
|
|
|
hecl::Backend::BlendFactor blendSrc = hecl::Backend::BlendFactor(r.readUByte());
|
|
|
|
hecl::Backend::BlendFactor blendDst = hecl::Backend::BlendFactor(r.readUByte());
|
2016-08-08 19:06:51 +00:00
|
|
|
|
|
|
|
if (r.hasError())
|
|
|
|
return false;
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
|
|
|
|
{
|
2016-07-31 20:26:08 +00:00
|
|
|
std::string vertSource = r.readString();
|
2015-11-18 23:56:45 +00:00
|
|
|
std::string fragSource = r.readString();
|
2016-08-08 19:06:51 +00:00
|
|
|
|
|
|
|
if (r.hasError())
|
|
|
|
return false;
|
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-04-03 06:19:03 +00:00
|
|
|
static_cast<boo::MetalDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
|
|
|
tag.newVertexFormat(ctx), m_rtHint,
|
2016-07-31 20:26:08 +00:00
|
|
|
boo::BlendFactor((slot.srcFactor == hecl::Backend::BlendFactor::Original) ? blendSrc : slot.srcFactor),
|
|
|
|
boo::BlendFactor((slot.dstFactor == hecl::Backend::BlendFactor::Original) ? blendDst : slot.dstFactor),
|
|
|
|
tag.getPrimType(),
|
2016-04-03 06:19:03 +00:00
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 23:56:45 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 23:56:45 +00:00
|
|
|
returnFunc(ret);
|
|
|
|
}
|
2016-08-08 19:06:51 +00:00
|
|
|
|
|
|
|
return true;
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-03 06:19:03 +00:00
|
|
|
IShaderBackendFactory* _NewMetalBackendFactory()
|
2015-11-18 23:56:45 +00:00
|
|
|
{
|
2016-04-03 06:19:03 +00:00
|
|
|
return new struct MetalBackendFactory();
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
2016-04-03 06:19:03 +00:00
|
|
|
|
2015-11-18 23:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|