metaforce/hecl/lib/Backend/GLSL.cpp

537 lines
16 KiB
C++
Raw Normal View History

2015-11-10 02:06:27 +00:00
#include "HECL/Backend/GLSL.hpp"
2015-11-16 04:30:06 +00:00
#include "HECL/Runtime.hpp"
#include <Athena/MemoryReader.hpp>
#include <Athena/MemoryWriter.hpp>
#include <boo/graphicsdev/GL.hpp>
static LogVisor::LogModule Log("HECL::Backend::GLSL");
2015-11-10 02:06:27 +00:00
2016-01-14 21:12:29 +00:00
static const TBuiltInResource DefaultBuiltInResource =
{
32,
6,
32,
32,
64,
4096,
64,
32,
80,
32,
4096,
32,
128,
8,
16,
16,
15,
-8,
7,
8,
65535,
65535,
65535,
1024,
1024,
64,
1024,
16,
8,
8,
1,
60,
64,
64,
128,
128,
8,
8,
8,
0,
0,
0,
0,
0,
8,
8,
16,
256,
1024,
1024,
64,
128,
128,
16,
1024,
4096,
128,
128,
16,
1024,
120,
32,
64,
16,
0,
0,
0,
0,
8,
8,
1,
0,
0,
0,
0,
1,
1,
16384,
4,
64,
8,
8,
4,
{
1,
1,
1,
1,
1,
1,
1,
1,
1
}
};
2015-11-10 02:06:27 +00:00
namespace HECL
{
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:
2015-11-16 04:30:06 +00:00
return "posIn.xy\n";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
2015-11-16 04:30:06 +00:00
return "normIn.xy\n";
2015-11-21 01:13:06 +00:00
case TexGenSrc::UV:
2015-11-10 23:17:53 +00:00
return HECL::Format("uvIn[%u]", uvIdx);
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:
2015-11-16 04:30:06 +00:00
return "vec4(posIn, 1.0)\n";
2015-11-21 01:13:06 +00:00
case TexGenSrc::Normal:
2015-11-16 04:30:06 +00:00
return "vec4(normIn, 1.0)\n";
2015-11-21 01:13:06 +00:00
case TexGenSrc::UV:
2015-11-10 23:17:53 +00:00
return HECL::Format("vec4(uvIn[%u], 0.0, 1.0)", uvIdx);
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
{
2015-11-10 23:17:53 +00:00
retval += HECL::Format("layout(location=%u) in vec4 colIn[%u];\n", idx, col);
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
{
2015-11-10 23:17:53 +00:00
retval += HECL::Format("layout(location=%u) in vec2 uvIn[%u];\n", idx, uv);
idx += uv;
2015-11-10 02:06:27 +00:00
}
2015-11-10 23:17:53 +00:00
if (w)
{
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
}
2015-11-10 23:17:53 +00:00
std::string GLSL::GenerateVertToFragStruct() 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())
retval += HECL::Format(" vec2 tcgs[%u];\n", unsigned(m_tcgs.size()));
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;
2015-11-16 04:30:06 +00:00
std::string retval = HECL::Format("uniform HECLVertUniform\n"
2015-11-10 23:17:53 +00:00
"{\n"
" mat4 mv[%u];\n"
" mat4 mvInv[%u];\n"
" mat4 proj;\n",
skinSlots, skinSlots);
if (texMtxs)
retval += HECL::Format(" mat4 texMtxs[%u];\n", texMtxs);
2015-11-18 23:56:45 +00:00
return retval + "};\n";
2015-11-10 23:17:53 +00:00
}
2015-11-10 02:06:27 +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,
2015-11-16 04:30:06 +00:00
unsigned s, unsigned tm) const
2015-11-10 23:17:53 +00:00
{
std::string retval = std::string(glslVer) + "\n" +
GenerateVertInStruct(col, uv, w) + "\n" +
GenerateVertToFragStruct() + "\n" +
2015-11-16 04:30:06 +00:00
GenerateVertUniformStruct(s, tm) +
2015-11-10 23:17:53 +00:00
"out VertToFrag vtf;\n\n"
"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)
2015-11-18 23:56:45 +00:00
retval += HECL::Format(" posAccum += (mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n"
" 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)
retval += HECL::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
2015-11-10 02:06:27 +00:00
else
2015-11-18 23:56:45 +00:00
retval += HECL::Format(" vtf.tcgs[%u] = (texMtxs[%u] * %s).xy;\n", tcgIdx, tcg.m_mtx,
2015-11-10 23:17:53 +00:00
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
++tcgIdx;
}
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
return retval + "}\n";
}
2016-01-14 21:12:29 +00:00
glslang::TShader GLSL::makeVertAST(const char* glslVer, unsigned col, unsigned uv, unsigned w,
unsigned s, unsigned tm) const
{
std::string src = makeVert(glslVer, col, uv, w, s, tm);
const char* strs[] = {src.data()};
int lens[] = {int(src.size())};
glslang::TShader ret(EShLangVertex);
ret.setStringsWithLengths(strs, lens, 1);
ret.parse(&DefaultBuiltInResource, 110, true, EShMsgDefault);
return ret;
}
2015-11-10 23:17:53 +00:00
std::string GLSL::makeFrag(const char* glslVer,
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;
std::string texMapDecl;
if (m_texMapEnd)
texMapDecl = HECL::Format("uniform sampler2D texs[%u];\n", m_texMapEnd);
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
std::string retval = std::string(glslVer) + "\n" +
GenerateVertToFragStruct() +
2015-11-16 04:30:06 +00:00
"\nlayout(location=0) out vec4 colorOut;\n" +
texMapDecl +
"in VertToFrag vtf;\n\n" +
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)
retval += HECL::Format(" vec4 lighting = %s();\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)
retval += HECL::Format(" vec4 sampling%u = texture(texs[%u], vtf.tcgs[%u]);\n",
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2015-11-10 23:17:53 +00:00
if (m_alphaExpr.size())
retval += " colorOut = vec4(" + m_colorExpr + ", " + m_alphaExpr + ");\n";
else
retval += " colorOut = vec4(" + m_colorExpr + ", 1.0);\n";
2015-11-16 04:30:06 +00:00
return retval + "}\n";
2015-11-10 02:06:27 +00:00
}
2016-01-14 21:12:29 +00:00
glslang::TShader GLSL::makeFragAST(const char* glslVer,
const ShaderFunction& lighting) const
{
std::string src = makeFrag(glslVer, lighting);
const char* strs[] = {src.data()};
int lens[] = {int(src.size())};
glslang::TShader ret(EShLangFragment);
ret.setStringsWithLengths(strs, lens, 1);
ret.parse(&DefaultBuiltInResource, 110, true, EShMsgDefault);
return ret;
}
2015-11-10 23:17:53 +00:00
std::string GLSL::makeFrag(const char* glslVer,
2015-11-16 04:30:06 +00:00
const ShaderFunction& lighting,
const ShaderFunction& post) 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;
std::string postSrc;
if (post.m_source)
postSrc = post.m_source;
std::string postEntry;
if (post.m_entry)
postEntry = post.m_entry;
std::string texMapDecl;
if (m_texMapEnd)
texMapDecl = HECL::Format("uniform sampler2D texs[%u];\n", m_texMapEnd);
2015-11-10 02:06:27 +00:00
2015-11-10 23:17:53 +00:00
std::string retval = std::string(glslVer) + "\n" +
GenerateVertToFragStruct() +
2015-11-16 04:30:06 +00:00
"\nlayout(location=0) out vec4 colorOut;\n" +
texMapDecl +
"in VertToFrag vtf;\n\n" +
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)
retval += HECL::Format(" vec4 lighting = %s();\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)
retval += HECL::Format(" vec4 sampling%u = texture(texs[%u], vtf.tcgs[%u]);\n",
sampIdx++, sampling.mapIdx, sampling.tcgIdx);
2015-11-10 23:17:53 +00:00
if (m_alphaExpr.size())
2015-11-16 04:30:06 +00:00
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", " + m_alphaExpr + "));\n";
2015-11-10 23:17:53 +00:00
else
2015-11-16 04:30:06 +00:00
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", 1.0));\n";
2015-11-10 02:06:27 +00:00
2015-11-16 04:30:06 +00:00
return retval + "}\n";
}
2016-01-14 21:12:29 +00:00
glslang::TShader GLSL::makeFragAST(const char* glslVer,
const ShaderFunction& lighting,
const ShaderFunction& post) const
{
std::string src = makeFrag(glslVer, lighting, post);
const char* strs[] = {src.data()};
int lens[] = {int(src.size())};
glslang::TShader ret(EShLangFragment);
ret.setStringsWithLengths(strs, lens, 1);
ret.parse(&DefaultBuiltInResource, 110, true, EShMsgDefault);
return ret;
}
2015-11-16 04:30:06 +00:00
}
namespace Runtime
{
static const char* STD_BLOCKNAMES[] = {"HECLVertUniform"};
struct GLSLBackendFactory : IShaderBackendFactory
{
Backend::GLSL m_backend;
boo::GLDataFactory* m_gfxFactory;
GLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory)
: m_gfxFactory(dynamic_cast<boo::GLDataFactory*>(gfxFactory)) {}
ShaderCachedData buildShaderFromIR(const ShaderTag& tag,
const HECL::Frontend::IR& ir,
HECL::Frontend::Diagnostics& diag,
boo::IShaderPipeline** objOut)
{
m_backend.reset(ir, diag);
size_t cachedSz = 3;
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
tag.getSkinSlotCount(), tag.getTexMtxCount());
cachedSz += vertSource.size() + 1;
std::string fragSource = m_backend.makeFrag("#version 330");
cachedSz += fragSource.size() + 1;
*objOut =
m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
m_backend.m_texMapEnd, "texs",
1, STD_BLOCKNAMES,
m_backend.m_blendSrc, m_backend.m_blendDst,
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
if (!*objOut)
Log.report(LogVisor::FatalError, "unable to build shader");
ShaderCachedData dataOut(tag, cachedSz);
Athena::io::MemoryWriter w(dataOut.m_data.get(), dataOut.m_sz);
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;
}
boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data)
{
const ShaderTag& tag = data.m_tag;
Athena::io::MemoryReader r(data.m_data.get(), data.m_sz);
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();
boo::IShaderPipeline* ret =
m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs",
1, STD_BLOCKNAMES,
blendSrc, blendDst,
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
if (!ret)
Log.report(LogVisor::FatalError, "unable to build shader");
return ret;
}
ShaderCachedData buildExtendedShaderFromIR(const ShaderTag& tag,
const HECL::Frontend::IR& ir,
HECL::Frontend::Diagnostics& diag,
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
FReturnExtensionShader returnFunc)
{
m_backend.reset(ir, diag);
size_t cachedSz = 3;
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
tag.getSkinSlotCount(), tag.getTexMtxCount());
cachedSz += vertSource.size() + 1;
std::vector<std::string> fragSources;
fragSources.reserve(extensionSlots.size());
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
fragSources.push_back(m_backend.makeFrag("#version 330", slot.lighting, slot.post));
cachedSz += fragSources.back().size() + 1;
boo::IShaderPipeline* ret =
m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSources.back().c_str(),
m_backend.m_texMapEnd, "texs",
1, STD_BLOCKNAMES,
m_backend.m_blendSrc, m_backend.m_blendDst,
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
if (!ret)
Log.report(LogVisor::FatalError, "unable to build shader");
returnFunc(ret);
}
ShaderCachedData dataOut(tag, cachedSz);
Athena::io::MemoryWriter w(dataOut.m_data.get(), dataOut.m_sz);
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);
for (const std::string src : fragSources)
w.writeString(src);
return dataOut;
}
void buildExtendedShaderFromCache(const ShaderCachedData& data,
const std::vector<ShaderCacheExtensions::ExtensionSlot>& extensionSlots,
FReturnExtensionShader returnFunc)
{
const ShaderTag& tag = data.m_tag;
Athena::io::MemoryReader r(data.m_data.get(), data.m_sz);
atUint8 texMapEnd = r.readUByte();
boo::BlendFactor blendSrc = boo::BlendFactor(r.readUByte());
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
std::string vertSource = r.readString();
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
std::string fragSource = r.readString();
boo::IShaderPipeline* ret =
m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs",
1, STD_BLOCKNAMES,
blendSrc, blendDst,
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
if (!ret)
Log.report(LogVisor::FatalError, "unable to build shader");
returnFunc(ret);
}
}
};
IShaderBackendFactory* _NewGLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory)
{
return new struct GLSLBackendFactory(gfxFactory);
2015-11-10 02:06:27 +00:00
}
}
}