metaforce/hecl/lib/Backend/GLSL.cpp

664 lines
25 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:
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:
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:
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:
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
}
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())
2016-03-04 23:02:44 +00:00
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;
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("UBINDING1 uniform HECLTexMtxUniform\n"
"{\n"
" mat4 texMtxs[%u];\n"
"};\n", texMtxs);
}
return retval;
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
{
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" +
GenerateVertToFragStruct() + "\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-03-04 23:02:44 +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";
}
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)
2016-03-04 23:02:44 +00:00
texMapDecl = hecl::Format("TBINDING0 uniform sampler2D texs[%u];\n", m_texMapEnd);
2015-11-10 02:06:27 +00:00
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
GenerateVertToFragStruct() +
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" +
"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-03-04 23:02:44 +00:00
retval += hecl::Format(" vec4 sampling%u = texture(texs[%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 + ");\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
}
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)
2016-03-04 23:02:44 +00:00
texMapDecl = hecl::Format("TBINDING0 uniform sampler2D texs[%u];\n", m_texMapEnd);
2015-11-10 02:06:27 +00:00
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
GenerateVertToFragStruct() +
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-03-04 23:02:44 +00:00
retval += hecl::Format(" vec4 sampling%u = texture(texs[%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())
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";
}
}
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
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(),
tag.getSkinSlotCount(), tag.getTexMtxCount());
cachedSz += vertSource.size() + 1;
std::string fragSource = m_backend.makeFrag("#version 330");
cachedSz += fragSource.size() + 1;
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(),
m_backend.m_texMapEnd, "texs",
2, STD_BLOCKNAMES,
2016-04-02 03:34:03 +00:00
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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;
2016-03-04 23:02:44 +00:00
athena::io::MemoryReader r(data.m_data.get(), data.m_sz);
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();
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs",
2, STD_BLOCKNAMES,
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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;
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)
{
size_t bc = 2;
const char** bn = STD_BLOCKNAMES;
if (slot.blockCount)
{
bc = slot.blockCount;
bn = slot.blockNames;
}
2015-11-16 04:30:06 +00:00
fragSources.push_back(m_backend.makeFrag("#version 330", slot.lighting, slot.post));
cachedSz += fragSources.back().size() + 1;
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSources.back().c_str(),
m_backend.m_texMapEnd, "texs", bc, bn,
2016-04-02 03:34:03 +00:00
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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));
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,
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-03-04 23:02:44 +00:00
athena::io::MemoryReader r(data.m_data.get(), data.m_sz);
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();
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
size_t bc = 2;
const char** bn = STD_BLOCKNAMES;
if (slot.blockCount)
{
bc = slot.blockCount;
bn = slot.blockNames;
}
2015-11-16 04:30:06 +00:00
std::string fragSource = r.readString();
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs", bc, bn,
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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);
}
}
};
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(),
tag.getSkinSlotCount(), tag.getTexMtxCount());
std::string fragSource = m_backend.makeFrag("#version 330");
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-04-02 03:34:03 +00:00
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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;
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());
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);
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(nullptr, nullptr,
vertBlob, fragBlob, pipelineBlob,
tag.newVertexFormat(ctx),
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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);
std::string vertSource =
m_backend.makeVert("#version 330",
tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(),
tag.getSkinSlotCount(), tag.getTexMtxCount());
std::vector<unsigned int> vertBlob;
2016-02-23 02:33:29 +00:00
std::vector<std::pair<std::vector<unsigned int>, std::vector<unsigned char>>> fragPipeBlobs;
2016-01-14 22:47:31 +00:00
fragPipeBlobs.reserve(extensionSlots.size());
size_t cachedSz = 7 + 8 * extensionSlots.size();
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
std::string fragSource = m_backend.makeFrag("#version 330", slot.lighting, slot.post);
fragPipeBlobs.emplace_back();
2016-02-23 02:33:29 +00:00
std::pair<std::vector<unsigned int>, std::vector<unsigned char>>& fragPipeBlob = fragPipeBlobs.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(),
vertBlob, fragPipeBlob.first, fragPipeBlob.second,
tag.newVertexFormat(ctx),
2016-04-02 03:34:03 +00:00
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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
cachedSz += fragPipeBlob.first.size() * sizeof(unsigned int);
2016-02-23 02:33:29 +00:00
cachedSz += fragPipeBlob.second.size();
2016-01-14 22:47:31 +00:00
returnFunc(ret);
}
size_t vertBlobSz = vertBlob.size() * sizeof(unsigned int);
cachedSz += vertBlobSz;
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 (vertBlobSz)
{
w.writeUint32Big(vertBlobSz);
w.writeUBytes((atUint8*)vertBlob.data(), vertBlobSz);
}
else
w.writeUint32Big(0);
2016-02-23 02:33:29 +00:00
for (const std::pair<std::vector<unsigned int>, std::vector<unsigned char>>& fragPipeBlob : fragPipeBlobs)
2016-01-14 22:47:31 +00:00
{
size_t fragBlobSz = fragPipeBlob.first.size() * sizeof(unsigned int);
2016-02-23 02:33:29 +00:00
size_t pipeBlobSz = fragPipeBlob.second.size();
2016-01-14 22:47:31 +00:00
if (fragBlobSz)
{
w.writeUint32Big(fragBlobSz);
w.writeUBytes((atUint8*)fragPipeBlob.first.data(), fragBlobSz);
}
else
w.writeUint32Big(0);
if (pipeBlobSz)
{
w.writeUint32Big(pipeBlobSz);
w.writeUBytes((atUint8*)fragPipeBlob.second.data(), pipeBlobSz);
}
else
w.writeUint32Big(0);
}
return dataOut;
}
void buildExtendedShaderFromCache(const ShaderCachedData& data,
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());
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);
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
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);
boo::IShaderPipeline* ret =
2016-03-30 19:15:08 +00:00
static_cast<boo::VulkanDataFactory::Context&>(ctx).
newShaderPipeline(nullptr, nullptr,
vertBlob, fragBlob, pipelineBlob,
tag.newVertexFormat(ctx),
2016-04-02 03:34:03 +00:00
blendSrc, blendDst, tag.getPrimType(),
2016-03-30 19:15:08 +00:00
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
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-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
}
}