metaforce/hecl/lib/Backend/GLSL.cpp

367 lines
13 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/Backend/GLSL.hpp"
#include "hecl/Runtime.hpp"
2018-10-07 02:53:57 +00:00
#include "athena/MemoryReader.hpp"
#include "athena/MemoryWriter.hpp"
#include "boo/graphicsdev/GLSLMacros.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:
return "vtf.mvPos.xy";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
return "vtf.mvNorm.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:
return "vec4(vtf.mvPos.xyz, 1.0)";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
return "vec4(vtf.mvNorm.xyz, 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
std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, 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);
retval += "struct HECLTCGMatrix\n"
"{\n"
" mat4 mtx;\n"
" mat4 postMtx;\n"
"};\n"
"UBINDING1 uniform HECLTexMtxUniform\n"
"{\n"
" HECLTCGMatrix texMtxs[8];\n"
"};\n";
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
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:
return "vec3(0.0, 0.0, 0.0)";
2017-03-26 05:51:58 +00:00
case ReflectionType::Simple:
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 - "
"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
2018-10-07 02:53:57 +00:00
std::string GLSL::makeVert(unsigned col, unsigned uv, unsigned w,
unsigned s, size_t extTexCount,
2017-03-26 05:51:58 +00:00
const TextureInfo* extTexs, ReflectionType reflectionType) const
2015-11-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()));
2018-10-07 02:53:57 +00:00
std::string retval =
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, 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
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
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
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";
}
2018-10-07 02:53:57 +00:00
std::string GLSL::makeFrag(bool alphaTest,
ReflectionType reflectionType, const Function& lighting) const
2015-11-10 23:17:53 +00:00
{
std::string lightingSrc;
2018-10-07 02:53:57 +00:00
if (!lighting.m_source.empty())
2015-11-16 04:30:06 +00:00
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);
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
2018-10-07 02:53:57 +00:00
std::string retval =
std::string("#extension GL_ARB_shader_image_load_store: enable\n") +
2017-03-26 05:51:58 +00:00
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
(!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
{
2018-10-07 02:53:57 +00:00
if (!lighting.m_entry.empty())
retval += hecl::Format(" vec4 lighting = %s(vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz));\n",
lighting.m_entry.data());
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
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
2015-11-10 02:06:27 +00:00
}
2018-10-07 02:53:57 +00:00
std::string GLSL::makeFrag(bool alphaTest,
2017-03-26 05:51:58 +00:00
ReflectionType reflectionType,
2018-10-07 02:53:57 +00:00
const Function& lighting,
const Function& 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;
2018-10-07 02:53:57 +00:00
if (!lighting.m_source.empty())
2015-11-16 04:30:06 +00:00
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;
2018-10-07 02:53:57 +00:00
if (!post.m_source.empty())
2015-11-16 04:30:06 +00:00
postSrc = post.m_source;
std::string postEntry;
2018-10-07 02:53:57 +00:00
if (!post.m_entry.empty())
2015-11-16 04:30:06 +00:00
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);
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];
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D extTex%u;\n",
2016-07-31 04:45:28 +00:00
extTex.mapIdx, extTex.mapIdx);
}
2018-10-07 02:53:57 +00:00
std::string retval =
std::string("#extension GL_ARB_shader_image_load_store: enable\n") +
2017-03-26 05:51:58 +00:00
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
(!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
{
2018-10-07 02:53:57 +00:00
if (!lighting.m_entry.empty())
retval += hecl::Format(" vec4 lighting = %s(vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz));\n",
lighting.m_entry.data());
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
return retval + (alphaTest ? GenerateAlphaTest() : "") + "}\n";
2015-11-16 04:30:06 +00:00
}
}
2017-12-29 07:56:31 +00:00