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
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
namespace hecl
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
namespace Backend
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string HLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
|
|
|
|
{
|
|
|
|
switch (src)
|
|
|
|
{
|
2015-11-21 02:59:43 +00:00
|
|
|
case TexGenSrc::Position:
|
2015-11-18 07:29:54 +00:00
|
|
|
return "v.posIn.xy\n";
|
2015-11-21 02:59:43 +00:00
|
|
|
case TexGenSrc::Normal:
|
2015-11-18 07:29:54 +00:00
|
|
|
return "v.normIn.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:
|
2015-11-18 07:29:54 +00:00
|
|
|
return "float4(v.posIn, 1.0)\n";
|
2015-11-21 02:59:43 +00:00
|
|
|
case TexGenSrc::Normal:
|
2015-11-18 07:29:54 +00:00
|
|
|
return "float4(v.normIn, 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";
|
|
|
|
}
|
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
std::string HLSL::GenerateVertToFragStruct(size_t extTexCount) 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
|
|
|
|
|
|
|
return retval + "};\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs) const
|
|
|
|
{
|
|
|
|
if (skinSlots == 0)
|
|
|
|
skinSlots = 1;
|
2016-03-04 23:02:44 +00:00
|
|
|
std::string retval = hecl::Format("cbuffer HECLVertUniform : register(b0)\n"
|
2015-11-18 07:29:54 +00:00
|
|
|
"{\n"
|
|
|
|
" float4x4 mv[%u];\n"
|
|
|
|
" float4x4 mvInv[%u];\n"
|
2016-07-19 20:12:04 +00:00
|
|
|
" float4x4 proj;\n"
|
|
|
|
"};\n",
|
2015-11-18 07:29:54 +00:00
|
|
|
skinSlots, skinSlots);
|
|
|
|
if (texMtxs)
|
2016-07-19 20:12:04 +00:00
|
|
|
{
|
|
|
|
retval += hecl::Format("struct TCGMtx\n"
|
|
|
|
"{\n"
|
|
|
|
" float4x4 mtx;\n"
|
|
|
|
" float4x4 postMtx;\n"
|
|
|
|
"};\n"
|
|
|
|
"cbuffer HECLTCGMatrix : register(b1)\n"
|
|
|
|
"{\n"
|
|
|
|
" TCGMtx texMtxs[%u];\n"
|
|
|
|
"};\n", texMtxs);
|
|
|
|
}
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
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,
|
2016-08-01 04:34:08 +00:00
|
|
|
unsigned s, unsigned tm, size_t extTexCount,
|
|
|
|
const TextureInfo* extTexs) const
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
std::string retval =
|
|
|
|
GenerateVertInStruct(col, uv, w) + "\n" +
|
2016-08-01 04:34:08 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount) + "\n" +
|
2015-11-18 07:29:54 +00:00
|
|
|
GenerateVertUniformStruct(s, tm) + "\n" +
|
|
|
|
"VertToFrag main(in VertData v)\n"
|
|
|
|
"{\n"
|
|
|
|
" VertToFrag vtf;\n";
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
/* skinned */
|
2016-04-03 03:31:50 +00:00
|
|
|
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";
|
2015-11-18 07:29:54 +00:00
|
|
|
for (size_t i=0 ; i<s ; ++i)
|
2016-03-04 23:02:44 +00:00
|
|
|
retval += hecl::Format(" posAccum += mul(mv[%" PRISize "], float4(v.posIn, 1.0)) * v.weightIn[%" PRISize "][%" PRISize "];\n"
|
2015-11-18 07:29:54 +00:00
|
|
|
" normAccum += mul(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 = mul(proj, posAccum);\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* non-skinned */
|
|
|
|
retval += " vtf.mvPos = mul(mv[0], float4(v.posIn, 1.0));\n"
|
|
|
|
" vtf.mvNorm = mul(mvInv[0], float4(v.normIn, 0.0));\n"
|
|
|
|
" vtf.mvpPos = mul(proj, vtf.mvPos);\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-07-19 20:12:04 +00:00
|
|
|
retval += hecl::Format(" vtf.tcgs[%u] = mul(texMtxs[%u].postMtx, float4(%s(mul(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 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(" vtf.extTcgs[%u] = mul(texMtxs[%u].postMtx, float4(%s(mul(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 07:29:54 +00:00
|
|
|
return retval + " return vtf;\n"
|
|
|
|
"}\n";
|
|
|
|
}
|
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
std::string HLSL::makeFrag(bool alphaTest, const ShaderFunction& lighting) const
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
std::string lightingSrc;
|
|
|
|
if (lighting.m_source)
|
|
|
|
lightingSrc = lighting.m_source;
|
2016-08-01 06:35:35 +00:00
|
|
|
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"
|
2017-02-10 09:00:23 +00:00
|
|
|
"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);
|
2015-11-18 07:29:54 +00:00
|
|
|
|
|
|
|
std::string retval =
|
|
|
|
"SamplerState samp : register(s0);\n" +
|
2016-08-01 04:34:08 +00:00
|
|
|
GenerateVertToFragStruct(0) +
|
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)
|
|
|
|
{
|
|
|
|
if (lighting.m_entry)
|
2016-04-04 19:34:22 +00:00
|
|
|
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry);
|
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);
|
|
|
|
|
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-02-10 09:00:23 +00:00
|
|
|
retval += " colorOut = float4(" + m_colorExpr + ", " + m_alphaExpr + ") * mulColor;\n";
|
2015-11-18 07:29:54 +00:00
|
|
|
else
|
2017-02-10 09:00:23 +00:00
|
|
|
retval += " colorOut = float4(" + m_colorExpr + ", 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
|
|
|
}
|
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
std::string HLSL::makeFrag(bool alphaTest, const ShaderFunction& lighting,
|
|
|
|
const ShaderFunction& post, size_t extTexCount,
|
|
|
|
const TextureInfo* extTexs) const
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
std::string lightingSrc;
|
|
|
|
if (lighting.m_source)
|
|
|
|
lightingSrc = lighting.m_source;
|
2016-08-01 06:35:35 +00:00
|
|
|
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"
|
2017-02-10 09:00:23 +00:00
|
|
|
"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;
|
|
|
|
if (post.m_source)
|
|
|
|
postSrc = post.m_source;
|
|
|
|
|
|
|
|
std::string postEntry;
|
|
|
|
if (post.m_entry)
|
|
|
|
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);
|
2015-11-18 07:29:54 +00:00
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
for (int i=0 ; i<extTexCount ; ++i)
|
|
|
|
{
|
|
|
|
const TextureInfo& extTex = extTexs[i];
|
|
|
|
texMapDecl += hecl::Format("Texture2D extTex%u : register(t%u);\n",
|
|
|
|
extTex.mapIdx, extTex.mapIdx);
|
|
|
|
}
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
std::string retval =
|
|
|
|
"SamplerState samp : register(s0);\n" +
|
2016-08-01 04:34:08 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount) +
|
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)
|
|
|
|
{
|
|
|
|
if (lighting.m_entry)
|
2016-04-04 19:34:22 +00:00
|
|
|
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry);
|
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);
|
|
|
|
|
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-02-10 09:00:23 +00:00
|
|
|
retval += " colorOut = " + postEntry + "(" + (postEntry.size() ? "vtf, " : "") + "float4(" + m_colorExpr + ", " + m_alphaExpr + ")) * mulColor;\n";
|
2015-11-18 07:29:54 +00:00
|
|
|
else
|
2017-02-10 09:00:23 +00:00
|
|
|
retval += " colorOut = " + postEntry + "(" + (postEntry.size() ? "vtf, " : "") + "float4(" + m_colorExpr + ", 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
namespace Runtime
|
|
|
|
{
|
|
|
|
|
|
|
|
struct HLSLBackendFactory : IShaderBackendFactory
|
|
|
|
{
|
|
|
|
Backend::HLSL m_backend;
|
|
|
|
|
|
|
|
ShaderCachedData buildShaderFromIR(const ShaderTag& tag,
|
2016-03-04 23:02:44 +00:00
|
|
|
const hecl::Frontend::IR& ir,
|
|
|
|
hecl::Frontend::Diagnostics& diag,
|
2016-03-30 20:43:09 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2016-02-23 02:33:29 +00:00
|
|
|
boo::IShaderPipeline*& objOut)
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
m_backend.reset(ir, diag);
|
|
|
|
|
|
|
|
std::string vertSource =
|
|
|
|
m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
|
2016-08-01 04:34:08 +00:00
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr);
|
2015-11-18 07:29:54 +00:00
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
std::string fragSource = m_backend.makeFrag(tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha);
|
2015-11-18 07:29:54 +00:00
|
|
|
ComPtr<ID3DBlob> vertBlob;
|
|
|
|
ComPtr<ID3DBlob> fragBlob;
|
|
|
|
ComPtr<ID3DBlob> pipelineBlob;
|
2016-02-23 02:33:29 +00:00
|
|
|
objOut =
|
2016-03-30 20:43:09 +00:00
|
|
|
static_cast<boo::ID3DDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
2017-03-05 23:00:37 +00:00
|
|
|
ReferenceComPtr(vertBlob), ReferenceComPtr(fragBlob), ReferenceComPtr(pipelineBlob),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.newVertexFormat(ctx),
|
2016-08-01 04:34:08 +00:00
|
|
|
boo::BlendFactor(m_backend.m_blendSrc),
|
|
|
|
boo::BlendFactor(m_backend.m_blendDst),
|
|
|
|
tag.getPrimType(),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2016-02-24 20:52:53 +00:00
|
|
|
if (!objOut)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 07:29:54 +00:00
|
|
|
|
|
|
|
atUint32 vertSz = 0;
|
|
|
|
atUint32 fragSz = 0;
|
|
|
|
atUint32 pipelineSz = 0;
|
|
|
|
if (vertBlob)
|
|
|
|
vertSz = vertBlob->GetBufferSize();
|
|
|
|
if (fragBlob)
|
|
|
|
fragSz = fragBlob->GetBufferSize();
|
|
|
|
if (pipelineBlob)
|
|
|
|
pipelineSz = pipelineBlob->GetBufferSize();
|
|
|
|
|
|
|
|
size_t cachedSz = 14 + vertSz + fragSz + pipelineSz;
|
|
|
|
|
|
|
|
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:59:43 +00:00
|
|
|
w.writeUByte(atUint8(m_backend.m_blendSrc));
|
|
|
|
w.writeUByte(atUint8(m_backend.m_blendDst));
|
2015-11-18 07:29:54 +00:00
|
|
|
|
|
|
|
if (vertBlob)
|
|
|
|
{
|
|
|
|
w.writeUint32Big(vertSz);
|
|
|
|
w.writeUBytes((atUint8*)vertBlob->GetBufferPointer(), vertSz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
|
|
|
|
|
|
|
if (fragBlob)
|
|
|
|
{
|
|
|
|
w.writeUint32Big(fragSz);
|
|
|
|
w.writeUBytes((atUint8*)fragBlob->GetBufferPointer(), fragSz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
|
|
|
|
|
|
|
if (pipelineBlob)
|
|
|
|
{
|
|
|
|
w.writeUint32Big(pipelineSz);
|
|
|
|
w.writeUBytes((atUint8*)pipelineBlob->GetBufferPointer(), pipelineSz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
|
|
|
|
|
|
|
return dataOut;
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:43:09 +00:00
|
|
|
boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 18:53:31 +00:00
|
|
|
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
|
2015-11-18 07:29:54 +00:00
|
|
|
boo::BlendFactor blendSrc = boo::BlendFactor(r.readUByte());
|
|
|
|
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
|
|
|
|
|
2016-08-08 18:53:31 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return nullptr;
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
atUint32 vertSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> vertBlob;
|
|
|
|
if (vertSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(vertSz, &vertBlob);
|
|
|
|
r.readUBytesToBuf(vertBlob->GetBufferPointer(), vertSz);
|
|
|
|
}
|
|
|
|
|
|
|
|
atUint32 fragSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> fragBlob;
|
|
|
|
if (fragSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(fragSz, &fragBlob);
|
|
|
|
r.readUBytesToBuf(fragBlob->GetBufferPointer(), fragSz);
|
|
|
|
}
|
|
|
|
|
|
|
|
atUint32 pipelineSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> pipelineBlob;
|
|
|
|
if (pipelineSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(pipelineSz, &pipelineBlob);
|
|
|
|
r.readUBytesToBuf(pipelineBlob->GetBufferPointer(), pipelineSz);
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:53:31 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return nullptr;
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-03-30 20:43:09 +00:00
|
|
|
static_cast<boo::ID3DDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(nullptr, nullptr,
|
2017-03-05 23:00:37 +00:00
|
|
|
ReferenceComPtr(vertBlob), ReferenceComPtr(fragBlob), ReferenceComPtr(pipelineBlob),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.newVertexFormat(ctx),
|
2016-04-02 03:34:03 +00:00
|
|
|
blendSrc, blendDst, tag.getPrimType(),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 07:29:54 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 07:29:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 07:29:54 +00:00
|
|
|
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
|
2016-03-30 20:43:09 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2015-11-18 07:29:54 +00:00
|
|
|
FReturnExtensionShader returnFunc)
|
|
|
|
{
|
|
|
|
m_backend.reset(ir, diag);
|
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
struct Blobs
|
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> vert;
|
|
|
|
ComPtr<ID3DBlob> frag;
|
|
|
|
ComPtr<ID3DBlob> pipeline;
|
|
|
|
};
|
|
|
|
std::vector<Blobs> pipeBlobs;
|
|
|
|
pipeBlobs.reserve(extensionSlots.size());
|
|
|
|
|
|
|
|
size_t cachedSz = 2 + 12 * extensionSlots.size();
|
2015-11-18 07:29:54 +00:00
|
|
|
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
|
|
|
|
{
|
2016-08-01 04:34:08 +00:00
|
|
|
std::string vertSource =
|
|
|
|
m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
|
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), slot.texCount, slot.texs);
|
|
|
|
|
|
|
|
std::string fragSource = m_backend.makeFrag(tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
|
|
|
|
slot.lighting, slot.post, slot.texCount, slot.texs);
|
|
|
|
pipeBlobs.emplace_back();
|
|
|
|
Blobs& thisPipeBlobs = pipeBlobs.back();
|
2015-11-18 07:29:54 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-03-30 20:43:09 +00:00
|
|
|
static_cast<boo::ID3DDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
2017-03-05 23:00:37 +00:00
|
|
|
ReferenceComPtr(thisPipeBlobs.vert), ReferenceComPtr(thisPipeBlobs.frag), ReferenceComPtr(thisPipeBlobs.pipeline),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.newVertexFormat(ctx),
|
2016-08-01 04:34: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),
|
|
|
|
tag.getPrimType(),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 07:29:54 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2016-08-01 04:34:08 +00:00
|
|
|
if (thisPipeBlobs.vert)
|
|
|
|
cachedSz += thisPipeBlobs.vert->GetBufferSize();
|
|
|
|
if (thisPipeBlobs.frag)
|
|
|
|
cachedSz += thisPipeBlobs.frag->GetBufferSize();
|
|
|
|
if (thisPipeBlobs.pipeline)
|
|
|
|
cachedSz += thisPipeBlobs.pipeline->GetBufferSize();
|
2015-11-18 07:29:54 +00:00
|
|
|
returnFunc(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:59:43 +00:00
|
|
|
w.writeUByte(atUint8(m_backend.m_blendSrc));
|
|
|
|
w.writeUByte(atUint8(m_backend.m_blendDst));
|
2015-11-18 07:29:54 +00:00
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
for (const Blobs& blobs : pipeBlobs)
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
2016-08-01 04:34:08 +00:00
|
|
|
if (blobs.vert)
|
|
|
|
{
|
|
|
|
w.writeUint32Big(blobs.vert->GetBufferSize());
|
|
|
|
w.writeUBytes((atUint8*)blobs.vert->GetBufferPointer(), blobs.vert->GetBufferSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
2015-11-18 07:29:54 +00:00
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
if (blobs.frag)
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
2016-08-01 04:34:08 +00:00
|
|
|
w.writeUint32Big(blobs.frag->GetBufferSize());
|
|
|
|
w.writeUBytes((atUint8*)blobs.frag->GetBufferPointer(), blobs.frag->GetBufferSize());
|
2015-11-18 07:29:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
|
|
|
|
2016-08-01 04:34:08 +00:00
|
|
|
if (blobs.pipeline)
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
2016-08-01 04:34:08 +00:00
|
|
|
w.writeUint32Big(blobs.pipeline->GetBufferSize());
|
|
|
|
w.writeUBytes((atUint8*)blobs.pipeline->GetBufferPointer(), blobs.pipeline->GetBufferSize());
|
2015-11-18 07:29:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
w.writeUint32Big(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataOut;
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:53:31 +00:00
|
|
|
bool buildExtendedShaderFromCache(const ShaderCachedData& data,
|
2015-11-18 07:29:54 +00:00
|
|
|
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
|
2016-03-30 20:43:09 +00:00
|
|
|
boo::IGraphicsDataFactory::Context& ctx,
|
2015-11-18 07:29:54 +00:00
|
|
|
FReturnExtensionShader returnFunc)
|
|
|
|
{
|
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 18:53:31 +00:00
|
|
|
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
|
2016-08-01 04:34:08 +00:00
|
|
|
hecl::Backend::BlendFactor blendSrc = hecl::Backend::BlendFactor(r.readUByte());
|
|
|
|
hecl::Backend::BlendFactor blendDst = hecl::Backend::BlendFactor(r.readUByte());
|
2015-11-18 07:29:54 +00:00
|
|
|
|
2016-08-08 18:53:31 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return false;
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
|
|
|
|
{
|
2016-08-01 04:34:08 +00:00
|
|
|
atUint32 vertSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> vertBlob;
|
|
|
|
if (vertSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(vertSz, &vertBlob);
|
|
|
|
r.readUBytesToBuf(vertBlob->GetBufferPointer(), vertSz);
|
|
|
|
}
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
atUint32 fragSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> fragBlob;
|
|
|
|
if (fragSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(fragSz, &fragBlob);
|
|
|
|
r.readUBytesToBuf(fragBlob->GetBufferPointer(), fragSz);
|
|
|
|
}
|
|
|
|
|
|
|
|
atUint32 pipelineSz = r.readUint32Big();
|
|
|
|
ComPtr<ID3DBlob> pipelineBlob;
|
|
|
|
if (pipelineSz)
|
|
|
|
{
|
|
|
|
D3DCreateBlobPROC(pipelineSz, &pipelineBlob);
|
|
|
|
r.readUBytesToBuf(pipelineBlob->GetBufferPointer(), pipelineSz);
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:53:31 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return false;
|
|
|
|
|
2015-11-18 07:29:54 +00:00
|
|
|
boo::IShaderPipeline* ret =
|
2016-03-30 20:43:09 +00:00
|
|
|
static_cast<boo::ID3DDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(nullptr, nullptr,
|
2017-03-05 23:00:37 +00:00
|
|
|
ReferenceComPtr(vertBlob), ReferenceComPtr(fragBlob), ReferenceComPtr(pipelineBlob),
|
2016-03-30 20:43:09 +00:00
|
|
|
tag.newVertexFormat(ctx),
|
2016-08-01 04:34: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-03-30 20:43:09 +00:00
|
|
|
tag.getDepthTest(), tag.getDepthWrite(),
|
|
|
|
tag.getBackfaceCulling());
|
2015-11-18 07:29:54 +00:00
|
|
|
if (!ret)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-18 07:29:54 +00:00
|
|
|
returnFunc(ret);
|
|
|
|
}
|
2016-08-08 18:53:31 +00:00
|
|
|
|
|
|
|
return true;
|
2015-11-18 07:29:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-30 20:43:09 +00:00
|
|
|
IShaderBackendFactory* _NewHLSLBackendFactory()
|
2015-11-18 07:29:54 +00:00
|
|
|
{
|
2016-03-30 20:43:09 +00:00
|
|
|
return new struct HLSLBackendFactory();
|
2015-11-18 07:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|