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
|
|
|
|
2017-12-30 01:07:15 +00:00
|
|
|
static logvisor::Module Log("hecl::Backend::GLSL");
|
2015-11-10 02:06:27 +00:00
|
|
|
|
2017-12-29 07:56:31 +00:00
|
|
|
namespace hecl::Backend
|
2015-11-10 02:06:27 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
std::string GLSL::GenerateVertToFragStruct(size_t extTexCount, bool reflectionCoords) 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
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
if (reflectionCoords)
|
|
|
|
retval += " vec2 reflectTcgs[2];\n"
|
|
|
|
" float reflectAlpha;\n";
|
|
|
|
|
2015-11-10 23:17:53 +00:00
|
|
|
return retval + "};\n";
|
|
|
|
}
|
2015-11-10 02:06:27 +00:00
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs, bool reflectionCoords) const
|
2015-11-10 23:17:53 +00:00
|
|
|
{
|
|
|
|
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-07-10 18:44:33 +00:00
|
|
|
retval += hecl::Format("struct HECLTCGMatrix\n"
|
2016-04-04 02:27:16 +00:00
|
|
|
"{\n"
|
2016-07-10 18:44:33 +00:00
|
|
|
" 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);
|
2017-03-26 05:51:58 +00:00
|
|
|
|
|
|
|
if (reflectionCoords)
|
|
|
|
retval += "UBINDING3 uniform HECLReflectMtx\n"
|
|
|
|
"{\n"
|
|
|
|
" mat4 indMtx;\n"
|
|
|
|
" mat4 reflectMtx;\n"
|
|
|
|
" float reflectAlpha;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n";
|
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
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string GLSL::GenerateAlphaTest() const
|
|
|
|
{
|
|
|
|
return " if (colorOut.a < 0.01)\n"
|
|
|
|
" {\n"
|
|
|
|
" discard;\n"
|
|
|
|
" }\n";
|
|
|
|
}
|
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
std::string GLSL::GenerateReflectionExpr(ReflectionType type) const
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ReflectionType::None:
|
2017-09-10 08:08:52 +00:00
|
|
|
default:
|
2017-05-29 06:00:17 +00:00
|
|
|
return "vec3(0.0, 0.0, 0.0)";
|
2017-03-26 05:51:58 +00:00
|
|
|
case ReflectionType::Simple:
|
2017-05-29 06:00:17 +00:00
|
|
|
return "texture(reflectionTex, vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha";
|
2017-03-26 05:51:58 +00:00
|
|
|
case ReflectionType::Indirect:
|
|
|
|
return "texture(reflectionTex, (texture(reflectionIndTex, vtf.reflectTcgs[0]).rg - "
|
2017-05-29 06:00:17 +00:00
|
|
|
"vec2(0.5, 0.5)) * vec2(0.5, 0.5) + vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha";
|
2017-03-26 05:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2017-03-26 05:51:58 +00:00
|
|
|
const TextureInfo* extTexs, ReflectionType reflectionType) 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" +
|
2017-03-26 05:51:58 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" +
|
|
|
|
GenerateVertUniformStruct(s, tm, reflectionType != ReflectionType::None) +
|
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
|
|
|
|
2017-10-01 04:25:49 +00:00
|
|
|
retval += " vec4 tmpProj;\n";
|
|
|
|
|
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
|
2017-10-01 04:25:49 +00:00
|
|
|
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((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-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
|
2017-10-01 04:25:49 +00:00
|
|
|
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((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-07-31 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
if (reflectionType != ReflectionType::None)
|
2017-10-22 06:10:59 +00:00
|
|
|
retval += " vtf.reflectTcgs[0] = normalize((indMtx * vec4(posIn, 1.0)).xz) * vec2(0.5, 0.5) + vec2(0.5, 0.5);\n"
|
|
|
|
" vtf.reflectTcgs[1] = (reflectMtx * vec4(posIn, 1.0)).xy;\n"
|
2017-03-26 05:51:58 +00:00
|
|
|
" vtf.reflectAlpha = reflectAlpha;\n";
|
|
|
|
|
2015-11-10 23:17:53 +00:00
|
|
|
return retval + "}\n";
|
|
|
|
}
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string GLSL::makeFrag(const char* glslVer, bool alphaTest,
|
2017-03-26 05:51:58 +00:00
|
|
|
ReflectionType reflectionType, 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;
|
2016-08-01 06:35:35 +00:00
|
|
|
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"
|
2017-02-10 09:00:23 +00:00
|
|
|
"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;
|
2016-07-14 21:30:32 +00:00
|
|
|
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);
|
2017-03-26 05:51:58 +00:00
|
|
|
if (reflectionType == ReflectionType::Indirect)
|
|
|
|
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D reflectionIndTex;\n"
|
|
|
|
"TBINDING%u uniform sampler2D reflectionTex;\n",
|
|
|
|
m_texMapEnd, m_texMapEnd+1);
|
|
|
|
else if (reflectionType == ReflectionType::Simple)
|
|
|
|
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D reflectionTex;\n",
|
|
|
|
m_texMapEnd);
|
2015-11-10 02:06:27 +00:00
|
|
|
|
2016-08-01 00:12:10 +00:00
|
|
|
std::string retval = std::string(glslVer) +
|
|
|
|
"\n#extension GL_ARB_shader_image_load_store: enable\n" BOO_GLSL_BINDING_HEAD +
|
2017-03-26 05:51:58 +00:00
|
|
|
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
|
2016-08-01 00:12:10 +00:00
|
|
|
(!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);
|
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
std::string reflectionExpr = GenerateReflectionExpr(reflectionType);
|
|
|
|
|
2015-11-10 23:17:53 +00:00
|
|
|
if (m_alphaExpr.size())
|
2017-03-26 05:51:58 +00:00
|
|
|
retval += " colorOut = vec4(" + m_colorExpr + " + " + reflectionExpr + ", " + m_alphaExpr + ") * mulColor;\n";
|
2015-11-10 23:17:53 +00:00
|
|
|
else
|
2017-03-26 05:51:58 +00:00
|
|
|
retval += " colorOut = vec4(" + m_colorExpr + " + " + reflectionExpr + ", 1.0) * mulColor;\n";
|
2015-11-10 23:17:53 +00:00
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
|
2015-11-10 02:06:27 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string GLSL::makeFrag(const char* glslVer, bool alphaTest,
|
2017-03-26 05:51:58 +00:00
|
|
|
ReflectionType reflectionType,
|
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;
|
2016-08-01 06:35:35 +00:00
|
|
|
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"
|
2017-02-10 09:00:23 +00:00
|
|
|
"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;
|
2016-07-14 21:30:32 +00:00
|
|
|
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);
|
2017-03-26 05:51:58 +00:00
|
|
|
if (reflectionType == ReflectionType::Indirect)
|
|
|
|
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D reflectionIndTex;\n"
|
|
|
|
"TBINDING%u uniform sampler2D reflectionTex;\n",
|
|
|
|
m_texMapEnd, m_texMapEnd+1);
|
|
|
|
else if (reflectionType == ReflectionType::Simple)
|
|
|
|
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D reflectionTex;\n",
|
|
|
|
m_texMapEnd);
|
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];
|
2017-03-05 23:00:37 +00:00
|
|
|
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D extTex%u;\n",
|
2016-07-31 04:45:28 +00:00
|
|
|
extTex.mapIdx, extTex.mapIdx);
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:12:10 +00:00
|
|
|
std::string retval = std::string(glslVer) +
|
|
|
|
"\n#extension GL_ARB_shader_image_load_store: enable\n" BOO_GLSL_BINDING_HEAD +
|
2017-03-26 05:51:58 +00:00
|
|
|
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
|
2016-08-01 00:12:10 +00:00
|
|
|
(!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);
|
|
|
|
|
2017-03-26 05:51:58 +00:00
|
|
|
std::string reflectionExpr = GenerateReflectionExpr(reflectionType);
|
|
|
|
|
2015-11-10 23:17:53 +00:00
|
|
|
if (m_alphaExpr.size())
|
2017-03-26 05:51:58 +00:00
|
|
|
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + " + " + reflectionExpr + ", " + m_alphaExpr + ")) * mulColor;\n";
|
2015-11-10 23:17:53 +00:00
|
|
|
else
|
2017-03-26 05:51:58 +00:00
|
|
|
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + " + " + reflectionExpr + ", 1.0)) * mulColor;\n";
|
2015-11-10 02:06:27 +00:00
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-12-29 07:56:31 +00:00
|
|
|
|
|
|
|
namespace hecl::Runtime
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
|
|
|
|
2016-04-04 05:00:40 +00:00
|
|
|
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,
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<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(),
|
2017-03-26 05:51:58 +00:00
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr, tag.getReflectionType());
|
2015-11-16 04:30:06 +00:00
|
|
|
cachedSz += vertSource.size() + 1;
|
|
|
|
|
2016-07-31 23:35:46 +00:00
|
|
|
std::string fragSource = m_backend.makeFrag("#version 330",
|
2017-03-26 05:51:58 +00:00
|
|
|
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
|
|
|
|
tag.getReflectionType());
|
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)
|
2017-12-30 01:07:15 +00:00
|
|
|
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
|
2016-07-08 00:05:45 +00:00
|
|
|
|
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,
|
2016-04-04 05:00:40 +00:00
|
|
|
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)
|
2017-12-30 01:07:15 +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;
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> buildShaderFromCache(const ShaderCachedData& data,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 18:22:32 +00:00
|
|
|
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
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return nullptr;
|
|
|
|
|
2016-07-08 00:05:45 +00:00
|
|
|
if (texMapEnd > 8)
|
2017-12-30 01:07:15 +00:00
|
|
|
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
|
2016-07-08 00:05:45 +00:00
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
auto 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,
|
2016-04-04 05:00:40 +00:00
|
|
|
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)
|
2017-12-30 01:07:15 +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)
|
2017-12-30 01:07:15 +00:00
|
|
|
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
|
2016-07-08 00:05:45 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2016-04-04 05:00:40 +00:00
|
|
|
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,
|
2017-03-26 05:51:58 +00:00
|
|
|
slot.texs, tag.getReflectionType()),
|
2016-07-31 23:35:46 +00:00
|
|
|
m_backend.makeFrag("#version 330",
|
|
|
|
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
|
2017-03-26 05:51:58 +00:00
|
|
|
tag.getReflectionType(), 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;
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
auto 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,
|
2017-12-10 05:29:23 +00:00
|
|
|
(slot.cullMode == hecl::Backend::CullMode::Original) ?
|
|
|
|
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None) :
|
|
|
|
boo::CullMode(slot.cullMode));
|
2015-11-16 04:30:06 +00:00
|
|
|
if (!ret)
|
2017-12-30 01:07:15 +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;
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
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;
|
2016-08-08 18:22:32 +00:00
|
|
|
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
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return false;
|
|
|
|
|
2016-07-08 00:05:45 +00:00
|
|
|
if (texMapEnd > 8)
|
2017-12-30 01:07:15 +00:00
|
|
|
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");
|
2016-07-08 00:05:45 +00:00
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
|
|
|
|
{
|
2016-04-04 05:00:40 +00:00
|
|
|
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();
|
2016-08-08 18:22:32 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
auto 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,
|
2017-12-10 05:29:23 +00:00
|
|
|
(slot.cullMode == hecl::Backend::CullMode::Original) ?
|
|
|
|
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None) :
|
|
|
|
boo::CullMode(slot.cullMode));
|
2015-11-16 04:30:06 +00:00
|
|
|
if (!ret)
|
2017-12-30 01:07:15 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to build shader");
|
2015-11-16 04:30:06 +00:00
|
|
|
returnFunc(ret);
|
|
|
|
}
|
2016-08-08 18:22:32 +00:00
|
|
|
|
|
|
|
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,
|
2017-11-06 06:56:17 +00:00
|
|
|
boo::ObjToken<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(),
|
2017-03-26 18:21:01 +00:00
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), 0, nullptr,
|
|
|
|
tag.getReflectionType());
|
2016-01-14 22:47:31 +00:00
|
|
|
|
2016-07-31 23:47:41 +00:00
|
|
|
std::string fragSource = m_backend.makeFrag("#version 330",
|
2017-03-26 18:21:01 +00:00
|
|
|
tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
|
|
|
|
tag.getReflectionType());
|
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(),
|
2017-03-05 23:00:37 +00:00
|
|
|
&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;
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:56:17 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline>
|
|
|
|
buildShaderFromCache(const ShaderCachedData& data,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2016-01-14 22:47:31 +00:00
|
|
|
{
|
|
|
|
const ShaderTag& tag = data.m_tag;
|
2016-08-08 18:22:32 +00:00
|
|
|
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);
|
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
if (r.hasError())
|
|
|
|
return nullptr;
|
|
|
|
|
2017-11-06 06:56:17 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> ret =
|
2016-03-30 19:15:08 +00:00
|
|
|
static_cast<boo::VulkanDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(nullptr, nullptr,
|
2017-03-05 23:00:37 +00:00
|
|
|
&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(),
|
2017-03-26 18:21:01 +00:00
|
|
|
tag.getSkinSlotCount(), tag.getTexMtxCount(), slot.texCount, slot.texs,
|
|
|
|
tag.getReflectionType());
|
2016-07-31 02:06:13 +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,
|
2017-03-26 18:21:01 +00:00
|
|
|
tag.getReflectionType(), slot.lighting, slot.post, slot.texCount, slot.texs);
|
2016-07-31 02:06:13 +00:00
|
|
|
pipeBlobs.emplace_back();
|
|
|
|
Blobs& pipeBlob = pipeBlobs.back();
|
2017-11-06 06:56:17 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> ret =
|
2016-03-30 19:15:08 +00:00
|
|
|
static_cast<boo::VulkanDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
|
2017-03-05 23:00:37 +00:00
|
|
|
&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-12-10 05:29:23 +00:00
|
|
|
(slot.cullMode == hecl::Backend::CullMode::Original) ?
|
|
|
|
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None) :
|
|
|
|
boo::CullMode(slot.cullMode));
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
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
|
|
|
|
2016-08-08 18:22:32 +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);
|
|
|
|
|
2016-08-08 18:22:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:56:17 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> ret =
|
2016-03-30 19:15:08 +00:00
|
|
|
static_cast<boo::VulkanDataFactory::Context&>(ctx).
|
|
|
|
newShaderPipeline(nullptr, nullptr,
|
2017-03-05 23:00:37 +00:00
|
|
|
&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-12-10 05:29:23 +00:00
|
|
|
(slot.cullMode == hecl::Backend::CullMode::Original) ?
|
|
|
|
(tag.getBackfaceCulling() ? boo::CullMode::Backface : boo::CullMode::None) :
|
|
|
|
boo::CullMode(slot.cullMode));
|
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);
|
|
|
|
}
|
2016-08-08 18:22:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|