metaforce/hecl/lib/Backend/GLSL.cpp

868 lines
34 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/Backend/GLSL.hpp"
#include "hecl/Runtime.hpp"
#include <athena/MemoryReader.hpp>
#include <athena/MemoryWriter.hpp>
2015-11-16 04:30:06 +00:00
#include <boo/graphicsdev/GL.hpp>
2016-01-14 22:47:31 +00:00
#include <boo/graphicsdev/Vulkan.hpp>
2015-11-16 04:30:06 +00:00
2016-03-04 23:02:44 +00:00
static logvisor::Module Log("hecl::Backend::GLSL");
2015-11-10 02:06:27 +00:00
2016-03-04 23:02:44 +00:00
namespace hecl
2015-11-10 02:06:27 +00:00
{
namespace Backend
{
2015-11-10 23:17:53 +00:00
std::string GLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
switch (src)
2015-11-10 02:06:27 +00:00
{
2015-11-21 01:13:06 +00:00
case TexGenSrc::Position:
2016-07-08 00:05:45 +00:00
return "posIn.xy";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
2016-07-08 00:05:45 +00:00
return "normIn.xy";
2015-11-21 01:13:06 +00:00
case TexGenSrc::UV:
2016-03-04 23:02:44 +00:00
return hecl::Format("uvIn[%u]", uvIdx);
2015-11-10 23:17:53 +00:00
default: break;
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
return std::string();
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
std::string GLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
switch (src)
2015-11-10 02:06:27 +00:00
{
2015-11-21 01:13:06 +00:00
case TexGenSrc::Position:
2016-07-08 00:05:45 +00:00
return "vec4(posIn, 1.0)";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
2016-07-08 00:05:45 +00:00
return "vec4(normIn, 1.0)";
2015-11-21 01:13:06 +00:00
case TexGenSrc::UV:
2016-03-04 23:02:44 +00:00
return hecl::Format("vec4(uvIn[%u], 0.0, 1.0)", uvIdx);
2015-11-10 23:17:53 +00:00
default: break;
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
return std::string();
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
std::string GLSL::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
std::string retval =
"layout(location=0) in vec3 posIn;\n"
"layout(location=1) in vec3 normIn;\n";
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
unsigned idx = 2;
if (col)
2015-11-10 02:06:27 +00:00
{
2016-03-04 23:02:44 +00:00
retval += hecl::Format("layout(location=%u) in vec4 colIn[%u];\n", idx, col);
2015-11-10 23:17:53 +00:00
idx += col;
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
if (uv)
2015-11-10 02:06:27 +00:00
{
2016-03-04 23:02:44 +00:00
retval += hecl::Format("layout(location=%u) in vec2 uvIn[%u];\n", idx, uv);
2015-11-10 23:17:53 +00:00
idx += uv;
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
if (w)
{
2016-03-04 23:02:44 +00:00
retval += hecl::Format("layout(location=%u) in vec4 weightIn[%u];\n", idx, w);
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
return retval;
2015-11-10 02:06:27 +00:00
}
2016-07-31 02:06:13 +00:00
std::string GLSL::GenerateVertToFragStruct(size_t extTexCount) const
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
std::string retval =
"struct VertToFrag\n"
"{\n"
" vec4 mvPos;\n"
" vec4 mvNorm;\n";
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
if (m_tcgs.size())
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" vec2 tcgs[%u];\n", unsigned(m_tcgs.size()));
2016-07-31 02:06:13 +00:00
if (extTexCount)
retval += hecl::Format(" vec2 extTcgs[%u];\n", unsigned(extTexCount));
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
return retval + "};\n";
}
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs) const
{
if (skinSlots == 0)
skinSlots = 1;
2016-03-04 23:02:44 +00:00
std::string retval = hecl::Format("UBINDING0 uniform HECLVertUniform\n"
2015-11-10 23:17:53 +00:00
"{\n"
" mat4 mv[%u];\n"
" mat4 mvInv[%u];\n"
2016-04-04 02:27:16 +00:00
" mat4 proj;\n"
"};\n",
2015-11-10 23:17:53 +00:00
skinSlots, skinSlots);
if (texMtxs)
2016-04-04 02:27:16 +00:00
{
retval += hecl::Format("struct HECLTCGMatrix\n"
2016-04-04 02:27:16 +00:00
"{\n"
" mat4 mtx;\n"
" mat4 postMtx;\n"
"};\n"
"UBINDING1 uniform HECLTexMtxUniform\n"
"{\n"
" HECLTCGMatrix texMtxs[%u];\n"
2016-07-09 21:57:03 +00:00
"};\n", texMtxs);
2016-04-04 02:27:16 +00:00
}
return retval;
2015-11-10 23:17:53 +00:00
}
2015-11-10 02:06:27 +00:00
std::string GLSL::GenerateAlphaTest() const
{
return " if (colorOut.a < 0.01)\n"
" {\n"
" discard;\n"
" }\n";
}
2015-11-10 23:17:53 +00:00
void GLSL::reset(const IR& ir, Diagnostics& diag)
{
/* Common programmable interpretation */
ProgrammableCommon::reset(ir, diag, "GLSL");
}
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsigned w,
2016-07-31 02:06:13 +00:00
unsigned s, unsigned tm, size_t extTexCount,
const TextureInfo* extTexs) const
2015-11-10 23:17:53 +00:00
{
2016-07-31 02:06:13 +00:00
extTexCount = std::min(int(extTexCount), BOO_GLSL_MAX_TEXTURE_COUNT - int(m_tcgs.size()));
2016-02-24 03:18:19 +00:00
std::string retval = std::string(glslVer) + "\n" BOO_GLSL_BINDING_HEAD +
2015-11-10 23:17:53 +00:00
GenerateVertInStruct(col, uv, w) + "\n" +
2016-07-31 02:06:13 +00:00
GenerateVertToFragStruct(extTexCount) + "\n" +
2015-11-16 04:30:06 +00:00
GenerateVertUniformStruct(s, tm) +
2016-07-01 02:31:23 +00:00
"SBINDING(0) out VertToFrag vtf;\n\n"
2015-11-10 23:17:53 +00:00
"void main()\n{\n";
2015-11-16 04:30:06 +00:00
if (s)
2015-11-10 23:17:53 +00:00
{
/* skinned */
retval += " vec4 posAccum = vec4(0.0,0.0,0.0,0.0);\n"
" vec4 normAccum = vec4(0.0,0.0,0.0,0.0);\n";
2015-11-16 04:30:06 +00:00
for (size_t i=0 ; i<s ; ++i)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" posAccum += (mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n"
2015-11-18 23:56:45 +00:00
" normAccum += (mvInv[%" PRISize "] * vec4(normIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n",
2015-11-10 23:17:53 +00:00
i, i/4, i%4, i, i/4, i%4);
2015-11-16 04:30:06 +00:00
retval += " posAccum[3] = 1.0;\n"
2015-11-10 23:17:53 +00:00
" vtf.mvPos = posAccum;\n"
" vtf.mvNorm = vec4(normalize(normAccum.xyz), 0.0);\n"
2015-11-18 23:56:45 +00:00
" gl_Position = proj * posAccum;\n";
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
else
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
/* non-skinned */
2015-11-18 23:56:45 +00:00
retval += " vtf.mvPos = mv[0] * vec4(posIn, 1.0);\n"
" vtf.mvNorm = mvInv[0] * vec4(normIn, 0.0);\n"
" gl_Position = proj * vtf.mvPos;\n";
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs)
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
if (tcg.m_mtx < 0)
2016-03-04 23:02:44 +00:00
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
2015-11-10 23:17:53 +00:00
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
2015-11-10 02:06:27 +00:00
else
2016-07-31 02:06:13 +00:00
retval += hecl::Format(" vtf.tcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n",
tcgIdx, tcg.m_mtx, tcg.m_norm ? "normalize" : "",
tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
2015-11-10 23:17:53 +00:00
++tcgIdx;
}
2015-11-10 02:06:27 +00:00
2016-07-31 02:06:13 +00:00
for (int i=0 ; i<extTexCount ; ++i)
{
const TextureInfo& extTex = extTexs[i];
2016-07-31 04:45:28 +00:00
if (extTex.mtxIdx < 0)
2016-08-01 04:34:08 +00:00
retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i,
2016-07-31 02:06:13 +00:00
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else
retval += hecl::Format(" vtf.extTcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n",
2016-08-01 04:34:08 +00:00
i, extTex.mtxIdx, extTex.normalize ? "normalize" : "",
2016-07-31 04:45:28 +00:00
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str());
2016-07-31 02:06:13 +00:00
}
2015-11-10 23:17:53 +00:00
return retval + "}\n";
}
std::string GLSL::makeFrag(const char* glslVer, bool alphaTest,
2015-11-16 04:30:06 +00:00
const ShaderFunction& lighting) const
2015-11-10 23:17:53 +00:00
{
std::string lightingSrc;
2015-11-16 04:30:06 +00:00
if (lighting.m_source)
lightingSrc = lighting.m_source;
else
2016-08-02 05:54:40 +00:00
lightingSrc = "const vec4 colorReg0 = vec4(1.0);\n"
"const vec4 colorReg1 = vec4(1.0);\n"
"const vec4 colorReg2 = vec4(1.0);\n"
"const vec4 mulColor = vec4(1.0);\n"
2016-08-02 05:54:40 +00:00
"\n";
2015-11-16 04:30:06 +00:00
std::string texMapDecl;
for (unsigned i=0 ; i<m_texMapEnd ; ++i)
2016-07-08 00:05:45 +00:00
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D tex%u;\n", i, i);
2015-11-10 02:06:27 +00:00
std::string retval = std::string(glslVer) +
"\n#extension GL_ARB_shader_image_load_store: enable\n" BOO_GLSL_BINDING_HEAD +
2016-07-31 02:06:13 +00:00
GenerateVertToFragStruct(0) +
(!alphaTest ?
"#ifdef GL_ARB_shader_image_load_store\n"
"layout(early_fragment_tests) in;\n"
"#endif\n" : "") +
"layout(location=0) out vec4 colorOut;\n" +
2015-11-16 04:30:06 +00:00
texMapDecl +
2016-07-01 02:31:23 +00:00
"SBINDING(0) in VertToFrag vtf;\n\n" +
2015-11-16 04:30:06 +00:00
lightingSrc + "\n" +
"void main()\n{\n";
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
if (m_lighting)
2015-11-10 02:06:27 +00:00
{
2015-11-16 04:30:06 +00:00
if (lighting.m_entry)
2016-04-04 02:27:16 +00:00
retval += hecl::Format(" vec4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry);
2015-11-10 02:06:27 +00:00
else
2015-11-10 23:17:53 +00:00
retval += " vec4 lighting = vec4(1.0,1.0,1.0,1.0);\n";
2015-11-10 02:06:27 +00:00
}
2015-11-16 04:30:06 +00:00
unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
2016-07-08 00:05:45 +00:00
retval += hecl::Format(" vec4 sampling%u = texture(tex%u, vtf.tcgs[%u]);\n",
2015-11-16 04:30:06 +00:00
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2015-11-10 23:17:53 +00:00
if (m_alphaExpr.size())
retval += " colorOut = vec4(" + m_colorExpr + ", " + m_alphaExpr + ") * mulColor;\n";
2015-11-10 23:17:53 +00:00
else
retval += " colorOut = vec4(" + m_colorExpr + ", 1.0) * mulColor;\n";
2015-11-10 23:17:53 +00:00
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
2015-11-10 02:06:27 +00:00
}
std::string GLSL::makeFrag(const char* glslVer, bool alphaTest,
2015-11-16 04:30:06 +00:00
const ShaderFunction& lighting,
2016-07-31 02:06:13 +00:00
const ShaderFunction& post,
2016-07-31 04:45:28 +00:00
size_t extTexCount, const TextureInfo* extTexs) const
2015-11-10 02:06:27 +00:00
{
2015-11-10 23:17:53 +00:00
std::string lightingSrc;
2015-11-16 04:30:06 +00:00
if (lighting.m_source)
lightingSrc = lighting.m_source;
else
2016-08-02 05:54:40 +00:00
lightingSrc = "const vec4 colorReg0 = vec4(1.0);\n"
"const vec4 colorReg1 = vec4(1.0);\n"
"const vec4 colorReg2 = vec4(1.0);\n"
"const vec4 mulColor = vec4(1.0);\n"
2016-08-02 05:54:40 +00:00
"\n";
2015-11-16 04:30:06 +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;
for (unsigned i=0 ; i<m_texMapEnd ; ++i)
2016-07-08 00:05:45 +00:00
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D tex%u;\n", i, i);
2015-11-10 02:06:27 +00:00
2016-07-31 04:45:28 +00:00
for (int i=0 ; i<extTexCount ; ++i)
{
const TextureInfo& extTex = extTexs[i];
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D extTex%u;\n",
2016-07-31 04:45:28 +00:00
extTex.mapIdx, extTex.mapIdx);
}
std::string retval = std::string(glslVer) +
"\n#extension GL_ARB_shader_image_load_store: enable\n" BOO_GLSL_BINDING_HEAD +
2016-07-31 02:06:13 +00:00
GenerateVertToFragStruct(extTexCount) +
(!alphaTest ?
"\n#ifdef GL_ARB_shader_image_load_store\n"
"layout(early_fragment_tests) in;\n"
"#endif\n" : "") +
2015-11-16 04:30:06 +00:00
"\nlayout(location=0) out vec4 colorOut;\n" +
texMapDecl +
2016-07-01 02:31:23 +00:00
"SBINDING(0) in VertToFrag vtf;\n\n" +
2015-11-16 04:30:06 +00:00
lightingSrc + "\n" +
postSrc +
2015-11-10 23:17:53 +00:00
"\nvoid main()\n{\n";
if (m_lighting)
2015-11-10 02:06:27 +00:00
{
2015-11-16 04:30:06 +00:00
if (lighting.m_entry)
2016-04-04 02:27:16 +00:00
retval += hecl::Format(" vec4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry);
2015-11-10 23:17:53 +00:00
else
retval += " vec4 lighting = vec4(1.0,1.0,1.0,1.0);\n";
2015-11-10 02:06:27 +00:00
}
2015-11-16 04:30:06 +00:00
unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
2016-07-08 00:05:45 +00:00
retval += hecl::Format(" vec4 sampling%u = texture(tex%u, vtf.tcgs[%u]);\n",
2015-11-16 04:30:06 +00:00
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2015-11-10 23:17:53 +00:00
if (m_alphaExpr.size())
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", " + m_alphaExpr + ")) * mulColor;\n";
2015-11-10 23:17:53 +00:00
else
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", 1.0)) * mulColor;\n";
2015-11-10 02:06:27 +00:00
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
2015-11-16 04:30:06 +00:00
}
}
namespace Runtime
{
static const char* STD_BLOCKNAMES[] = {HECL_GLSL_VERT_UNIFORM_BLOCK_NAME,
HECL_GLSL_TEXMTX_UNIFORM_BLOCK_NAME};
2015-11-16 04:30:06 +00:00
2016-07-08 00:05:45 +00:00
static const char* STD_TEXNAMES[] =
{
"tex0",
"tex1",
"tex2",
"tex3",
"tex4",
"tex5",
"tex6",
"tex7"
};
2015-11-16 04:30:06 +00:00
struct GLSLBackendFactory : IShaderBackendFactory
{
Backend::GLSL 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 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2016-02-23 02:33:29 +00:00
boo::IShaderPipeline*& objOut)
2015-11-16 04:30:06 +00:00
{
m_backend.reset(ir, diag);
size_t cachedSz = 3;
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
2016-07-31 02:06:13 +00:00
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr);
2015-11-16 04:30:06 +00:00
cachedSz += vertSource.size() + 1;
std::string fragSource = m_backend.makeFrag("#version 330",
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha);
2015-11-16 04:30:06 +00:00
cachedSz += fragSource.size() + 1;
2016-07-08 00:05:45 +00:00
if (m_backend.m_texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
2016-02-23 02:33:29 +00:00
objOut =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
2016-07-08 00:05:45 +00:00
m_backend.m_texMapEnd, STD_TEXNAMES,
2, STD_BLOCKNAMES,
2016-07-31 04:45:28 +00:00
boo::BlendFactor(m_backend.m_blendSrc),
boo::BlendFactor(m_backend.m_blendDst),
2017-03-14 07:03:22 +00:00
tag.getPrimType(), tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None,
tag.getDepthWrite(), true, false,
2017-03-10 20:38:34 +00:00
tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None);
2016-02-23 02:33:29 +00:00
if (!objOut)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2015-11-16 04:30:06 +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-16 04:30:06 +00:00
w.writeUByte(m_backend.m_texMapEnd);
2015-11-21 01:13:06 +00:00
w.writeUByte(atUint8(m_backend.m_blendSrc));
w.writeUByte(atUint8(m_backend.m_blendDst));
2015-11-16 04:30:06 +00:00
w.writeString(vertSource);
w.writeString(fragSource);
return dataOut;
}
2016-03-30 19:15:08 +00:00
boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data,
boo::IGraphicsDataFactory::Context& ctx)
2015-11-16 04:30:06 +00:00
{
const ShaderTag& tag = data.m_tag;
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
2015-11-16 04:30:06 +00:00
atUint8 texMapEnd = r.readUByte();
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-07-08 00:05:45 +00:00
if (r.hasError())
return nullptr;
2016-07-08 00:05:45 +00:00
if (texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
2015-11-16 04:30:06 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
2016-07-08 00:05:45 +00:00
texMapEnd, STD_TEXNAMES,
2, STD_BLOCKNAMES,
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2017-03-14 07:03:22 +00:00
tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None,
tag.getDepthWrite(), true, false,
2017-03-10 20:38:34 +00:00
tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None);
2015-11-16 04:30:06 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2015-11-16 04:30:06 +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-16 04:30:06 +00:00
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
2016-03-30 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2015-11-16 04:30:06 +00:00
FReturnExtensionShader returnFunc)
{
m_backend.reset(ir, diag);
size_t cachedSz = 3;
2016-07-08 00:05:45 +00:00
if (m_backend.m_texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
2016-07-31 02:06:13 +00:00
std::vector<std::pair<std::string, std::string>> sources;
sources.reserve(extensionSlots.size());
2015-11-16 04:30:06 +00:00
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
size_t bc = 2;
const char** bn = STD_BLOCKNAMES;
if (slot.blockCount)
{
bc = slot.blockCount;
bn = slot.blockNames;
}
2016-07-31 02:06:13 +00:00
sources.emplace_back(m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
tag.getSkinSlotCount(), tag.getTexMtxCount(), slot.texCount,
slot.texs),
m_backend.makeFrag("#version 330",
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
slot.lighting, slot.post, slot.texCount, slot.texs));
2016-07-31 02:06:13 +00:00
cachedSz += sources.back().first.size() + 1;
cachedSz += sources.back().second.size() + 1;
2017-03-14 07:03:22 +00:00
boo::ZTest zTest;
switch (slot.depthTest)
{
case hecl::Backend::ZTest::Original:
default:
zTest = tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None;
break;
case hecl::Backend::ZTest::None:
zTest = boo::ZTest::None;
break;
case hecl::Backend::ZTest::LEqual:
zTest = boo::ZTest::LEqual;
break;
case hecl::Backend::ZTest::Greater:
zTest = boo::ZTest::Greater;
break;
case hecl::Backend::ZTest::Equal:
zTest = boo::ZTest::Equal;
break;
}
2015-11-16 04:30:06 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
2016-07-31 02:06:13 +00:00
newShaderPipeline(sources.back().first.c_str(), sources.back().second.c_str(),
2016-07-31 04:45:28 +00:00
8, STD_TEXNAMES, bc, bn,
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),
2017-03-14 07:03:22 +00:00
tag.getPrimType(), zTest, slot.noDepthWrite ? false : tag.getDepthWrite(), !slot.noColorWrite, !slot.noAlphaWrite,
slot.frontfaceCull ? boo::CullMode::Frontface :
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None));
2015-11-16 04:30:06 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2015-11-16 04:30:06 +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-16 04:30:06 +00:00
w.writeUByte(m_backend.m_texMapEnd);
2015-11-21 01:13:06 +00:00
w.writeUByte(atUint8(m_backend.m_blendSrc));
w.writeUByte(atUint8(m_backend.m_blendDst));
2016-07-31 02:06:13 +00:00
for (const std::pair<std::string, std::string>& pair : sources)
{
w.writeString(pair.first);
w.writeString(pair.second);
}
2015-11-16 04:30:06 +00:00
return dataOut;
}
bool buildExtendedShaderFromCache(const ShaderCachedData& data,
2015-11-16 04:30:06 +00:00
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
2016-03-30 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2015-11-16 04:30:06 +00:00
FReturnExtensionShader returnFunc)
{
const ShaderTag& tag = data.m_tag;
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
2015-11-16 04:30:06 +00:00
atUint8 texMapEnd = r.readUByte();
2016-07-31 04:45:28 +00:00
hecl::Backend::BlendFactor blendSrc = hecl::Backend::BlendFactor(r.readUByte());
hecl::Backend::BlendFactor blendDst = hecl::Backend::BlendFactor(r.readUByte());
2016-07-08 00:05:45 +00:00
if (r.hasError())
return false;
2016-07-08 00:05:45 +00:00
if (texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
2015-11-16 04:30:06 +00:00
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
size_t bc = 2;
const char** bn = STD_BLOCKNAMES;
if (slot.blockCount)
{
bc = slot.blockCount;
bn = slot.blockNames;
}
2016-07-31 02:06:13 +00:00
std::string vertSource = r.readString();
2015-11-16 04:30:06 +00:00
std::string fragSource = r.readString();
if (r.hasError())
return false;
2017-03-14 07:03:22 +00:00
boo::ZTest zTest;
switch (slot.depthTest)
{
case hecl::Backend::ZTest::Original:
default:
zTest = tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None;
break;
case hecl::Backend::ZTest::None:
zTest = boo::ZTest::None;
break;
case hecl::Backend::ZTest::LEqual:
zTest = boo::ZTest::LEqual;
break;
case hecl::Backend::ZTest::Greater:
zTest = boo::ZTest::Greater;
break;
case hecl::Backend::ZTest::Equal:
zTest = boo::ZTest::Equal;
break;
}
2015-11-16 04:30:06 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
2016-07-31 04:45:28 +00:00
8, STD_TEXNAMES, bc, bn,
boo::BlendFactor((slot.srcFactor == hecl::Backend::BlendFactor::Original) ? blendSrc : slot.srcFactor),
boo::BlendFactor((slot.dstFactor == hecl::Backend::BlendFactor::Original) ? blendDst : slot.dstFactor),
2017-03-14 07:03:22 +00:00
tag.getPrimType(), zTest, slot.noDepthWrite ? false : tag.getDepthWrite(), !slot.noColorWrite, !slot.noAlphaWrite,
slot.frontfaceCull ? boo::CullMode::Frontface :
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None));
2015-11-16 04:30:06 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2015-11-16 04:30:06 +00:00
returnFunc(ret);
}
return true;
2015-11-16 04:30:06 +00:00
}
};
2016-03-30 19:15:08 +00:00
IShaderBackendFactory* _NewGLSLBackendFactory()
2016-02-23 02:33:29 +00:00
{
2016-03-30 19:15:08 +00:00
return new struct GLSLBackendFactory();
2016-02-23 02:33:29 +00:00
}
#if BOO_HAS_VULKAN
2016-01-14 22:47:31 +00:00
struct SPIRVBackendFactory : IShaderBackendFactory
{
Backend::GLSL 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 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2016-02-23 02:33:29 +00:00
boo::IShaderPipeline*& objOut)
2016-01-14 22:47:31 +00:00
{
m_backend.reset(ir, diag);
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
2016-07-31 02:06:13 +00:00
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr);
2016-01-14 22:47:31 +00:00
2016-07-31 23:47:41 +00:00
std::string fragSource = m_backend.makeFrag("#version 330",
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha);
2016-01-14 22:47:31 +00:00
std::vector<unsigned int> vertBlob;
std::vector<unsigned int> fragBlob;
2016-02-23 02:33:29 +00:00
std::vector<unsigned char> pipelineBlob;
2016-01-14 22:47:31 +00:00
2016-02-23 02:33:29 +00:00
objOut =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
&vertBlob, &fragBlob, &pipelineBlob, tag.newVertexFormat(ctx),
2016-07-31 04:45:28 +00:00
boo::BlendFactor(m_backend.m_blendSrc), boo::BlendFactor(m_backend.m_blendDst),
2017-03-17 23:32:18 +00:00
tag.getPrimType(), tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None,
tag.getDepthWrite(), true, false,
2017-03-10 20:38:34 +00:00
tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None);
2016-02-23 02:33:29 +00:00
if (!objOut)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2016-01-14 22:47:31 +00:00
atUint32 vertSz = vertBlob.size() * sizeof(unsigned int);
atUint32 fragSz = fragBlob.size() * sizeof(unsigned int);
2016-07-01 02:31:23 +00:00
atUint32 pipelineSz = pipelineBlob.size();
2016-01-14 22:47:31 +00:00
size_t cachedSz = 15 + 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);
2016-01-14 22:47:31 +00:00
w.writeUByte(atUint8(m_backend.m_texMapEnd));
w.writeUByte(atUint8(m_backend.m_blendSrc));
w.writeUByte(atUint8(m_backend.m_blendDst));
if (vertBlob.size())
{
w.writeUint32Big(vertSz);
w.writeUBytes((atUint8*)vertBlob.data(), vertSz);
}
else
w.writeUint32Big(0);
if (fragBlob.size())
{
w.writeUint32Big(fragSz);
w.writeUBytes((atUint8*)fragBlob.data(), fragSz);
}
else
w.writeUint32Big(0);
if (pipelineBlob.size())
{
w.writeUint32Big(pipelineSz);
w.writeUBytes((atUint8*)pipelineBlob.data(), pipelineSz);
}
else
w.writeUint32Big(0);
return dataOut;
}
2016-03-30 19:15:08 +00:00
boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data,
boo::IGraphicsDataFactory::Context& ctx)
2016-01-14 22:47:31 +00:00
{
const ShaderTag& tag = data.m_tag;
athena::io::MemoryReader r(data.m_data.get(), data.m_sz, false, false);
2016-01-14 22:47:31 +00:00
size_t texCount = size_t(r.readByte());
boo::BlendFactor blendSrc = boo::BlendFactor(r.readUByte());
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
atUint32 vertSz = r.readUint32Big();
std::vector<unsigned int> vertBlob(vertSz / sizeof(unsigned int));
if (vertSz)
r.readUBytesToBuf(vertBlob.data(), vertSz);
atUint32 fragSz = r.readUint32Big();
std::vector<unsigned int> fragBlob(fragSz / sizeof(unsigned int));
if (fragSz)
r.readUBytesToBuf(fragBlob.data(), fragSz);
atUint32 pipelineSz = r.readUint32Big();
2016-02-23 02:33:29 +00:00
std::vector<unsigned char> pipelineBlob(pipelineSz);
2016-01-14 22:47:31 +00:00
if (pipelineSz)
r.readUBytesToBuf(pipelineBlob.data(), pipelineSz);
if (r.hasError())
return nullptr;
2016-01-14 22:47:31 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(nullptr, nullptr,
&vertBlob, &fragBlob, &pipelineBlob,
2016-03-30 19:15:08 +00:00
tag.newVertexFormat(ctx),
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2017-03-17 23:32:18 +00:00
tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None,
tag.getDepthWrite(), true, false,
2017-03-10 20:38:34 +00:00
tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None);
2016-01-14 22:47:31 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2016-01-14 22:47:31 +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,
2016-01-14 22:47:31 +00:00
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
2016-03-30 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2016-01-14 22:47:31 +00:00
FReturnExtensionShader returnFunc)
{
m_backend.reset(ir, diag);
2016-07-31 02:06:13 +00:00
struct Blobs
{
std::vector<unsigned int> vert;
std::vector<unsigned int> frag;
std::vector<unsigned char> pipeline;
};
std::vector<Blobs> pipeBlobs;
pipeBlobs.reserve(extensionSlots.size());
size_t cachedSz = 3 + 12 * extensionSlots.size();
2016-01-14 22:47:31 +00:00
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
2016-07-31 02:06:13 +00:00
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
tag.getSkinSlotCount(), tag.getTexMtxCount(), slot.texCount, slot.texs);
2016-07-31 23:47:41 +00:00
std::string fragSource = m_backend.makeFrag("#version 330",
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
slot.lighting, slot.post, slot.texCount, slot.texs);
2016-07-31 02:06:13 +00:00
pipeBlobs.emplace_back();
Blobs& pipeBlob = pipeBlobs.back();
2016-01-14 22:47:31 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
&pipeBlob.vert, &pipeBlob.frag, &pipeBlob.pipeline,
2016-03-30 19:15:08 +00:00
tag.newVertexFormat(ctx),
2016-07-31 04:45:28 +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),
2017-03-17 23:32:18 +00:00
tag.getPrimType(), tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None,
slot.noDepthWrite ? false : tag.getDepthWrite(),
2017-03-14 07:03:22 +00:00
!slot.noColorWrite, !slot.noAlphaWrite,
2017-03-17 23:32:18 +00:00
slot.frontfaceCull ? boo::CullMode::Frontface :
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None));
2016-01-14 22:47:31 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2016-07-31 02:06:13 +00:00
cachedSz += pipeBlob.vert.size() * sizeof(unsigned int);
cachedSz += pipeBlob.frag.size() * sizeof(unsigned int);
cachedSz += pipeBlob.pipeline.size();
2016-01-14 22:47:31 +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);
2016-01-14 22:47:31 +00:00
w.writeUByte(atUint8(m_backend.m_texMapEnd));
w.writeUByte(atUint8(m_backend.m_blendSrc));
w.writeUByte(atUint8(m_backend.m_blendDst));
2016-07-31 02:06:13 +00:00
for (const Blobs& pipeBlob : pipeBlobs)
2016-01-14 22:47:31 +00:00
{
2016-07-31 02:06:13 +00:00
size_t vertBlobSz = pipeBlob.vert.size() * sizeof(unsigned int);
size_t fragBlobSz = pipeBlob.frag.size() * sizeof(unsigned int);
size_t pipeBlobSz = pipeBlob.pipeline.size();
2016-01-14 22:47:31 +00:00
2016-07-31 02:06:13 +00:00
if (vertBlobSz)
{
w.writeUint32Big(vertBlobSz);
w.writeUBytes((atUint8*)pipeBlob.vert.data(), vertBlobSz);
}
else
w.writeUint32Big(0);
2016-01-14 22:47:31 +00:00
if (fragBlobSz)
{
w.writeUint32Big(fragBlobSz);
2016-07-31 02:06:13 +00:00
w.writeUBytes((atUint8*)pipeBlob.frag.data(), fragBlobSz);
2016-01-14 22:47:31 +00:00
}
else
w.writeUint32Big(0);
if (pipeBlobSz)
{
w.writeUint32Big(pipeBlobSz);
2016-07-31 02:06:13 +00:00
w.writeUBytes((atUint8*)pipeBlob.pipeline.data(), pipeBlobSz);
2016-01-14 22:47:31 +00:00
}
else
w.writeUint32Big(0);
}
return dataOut;
}
bool buildExtendedShaderFromCache(const ShaderCachedData& data,
2016-01-14 22:47:31 +00:00
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
2016-03-30 19:15:08 +00:00
boo::IGraphicsDataFactory::Context& ctx,
2016-01-14 22:47:31 +00:00
FReturnExtensionShader returnFunc)
{
const ShaderTag& tag = data.m_tag;
2016-03-04 23:02:44 +00:00
athena::io::MemoryReader r(data.m_data.get(), data.m_sz);
2016-01-14 22:47:31 +00:00
size_t texCount = size_t(r.readByte());
2016-07-31 04:45:28 +00:00
hecl::Backend::BlendFactor blendSrc = hecl::Backend::BlendFactor(r.readUByte());
hecl::Backend::BlendFactor blendDst = hecl::Backend::BlendFactor(r.readUByte());
2016-01-14 22:47:31 +00:00
if (r.hasError())
return false;
2016-01-14 22:47:31 +00:00
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
2016-07-31 02:06:13 +00:00
atUint32 vertSz = r.readUint32Big();
std::vector<unsigned int> vertBlob(vertSz / sizeof(unsigned int));
if (vertSz)
r.readUBytesToBuf(vertBlob.data(), vertSz);
2016-01-14 22:47:31 +00:00
atUint32 fragSz = r.readUint32Big();
std::vector<unsigned int> fragBlob(fragSz / sizeof(unsigned int));
if (fragSz)
r.readUBytesToBuf(fragBlob.data(), fragSz);
atUint32 pipelineSz = r.readUint32Big();
2016-02-23 02:33:29 +00:00
std::vector<unsigned char> pipelineBlob(pipelineSz);
2016-01-14 22:47:31 +00:00
if (pipelineSz)
r.readUBytesToBuf(pipelineBlob.data(), pipelineSz);
if (r.hasError())
return false;
2017-03-17 23:32:18 +00:00
boo::ZTest zTest;
switch (slot.depthTest)
{
case hecl::Backend::ZTest::Original:
default:
zTest = tag.getDepthTest() ? boo::ZTest::LEqual : boo::ZTest::None;
break;
case hecl::Backend::ZTest::None:
zTest = boo::ZTest::None;
break;
case hecl::Backend::ZTest::LEqual:
zTest = boo::ZTest::LEqual;
break;
case hecl::Backend::ZTest::Greater:
zTest = boo::ZTest::Greater;
break;
case hecl::Backend::ZTest::Equal:
zTest = boo::ZTest::Equal;
break;
}
2016-01-14 22:47:31 +00:00
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(nullptr, nullptr,
&vertBlob, &fragBlob, &pipelineBlob,
2016-03-30 19:15:08 +00:00
tag.newVertexFormat(ctx),
2016-07-31 04:45:28 +00:00
boo::BlendFactor((slot.srcFactor == hecl::Backend::BlendFactor::Original) ? blendSrc : slot.srcFactor),
boo::BlendFactor((slot.dstFactor == hecl::Backend::BlendFactor::Original) ? blendDst : slot.dstFactor),
2017-03-17 23:32:18 +00:00
tag.getPrimType(), zTest, slot.noDepthWrite ? false : tag.getDepthWrite(),
2017-03-14 07:03:22 +00:00
!slot.noColorWrite, !slot.noAlphaWrite,
2017-03-17 23:32:18 +00:00
slot.frontfaceCull ? boo::CullMode::Frontface :
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None));
2016-01-14 22:47:31 +00:00
if (!ret)
2016-03-04 23:02:44 +00:00
Log.report(logvisor::Fatal, "unable to build shader");
2016-01-14 22:47:31 +00:00
returnFunc(ret);
}
return true;
2016-01-14 22:47:31 +00:00
}
};
2016-03-30 19:15:08 +00:00
IShaderBackendFactory* _NewSPIRVBackendFactory()
2016-01-14 22:47:31 +00:00
{
2016-03-30 19:15:08 +00:00
return new struct SPIRVBackendFactory();
2016-01-14 22:47:31 +00:00
}
2016-02-23 02:33:29 +00:00
#endif
2015-11-10 02:06:27 +00:00
}
}