mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-12-09 17:47:43 +00:00
Additions to test
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
#include "HECL/Backend/GLSL.hpp"
|
||||
#include <map>
|
||||
#include "HECL/Runtime.hpp"
|
||||
#include <Athena/MemoryReader.hpp>
|
||||
#include <Athena/MemoryWriter.hpp>
|
||||
#include <boo/graphicsdev/GL.hpp>
|
||||
|
||||
static LogVisor::LogModule Log("HECL::Backend::GLSL");
|
||||
|
||||
namespace HECL
|
||||
{
|
||||
@@ -11,9 +16,9 @@ std::string GLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
|
||||
switch (src)
|
||||
{
|
||||
case TG_POS:
|
||||
return "posIn.xy;\n";
|
||||
return "posIn.xy\n";
|
||||
case TG_NRM:
|
||||
return "normIn.xy;\n";
|
||||
return "normIn.xy\n";
|
||||
case TG_UV:
|
||||
return HECL::Format("uvIn[%u]", uvIdx);
|
||||
default: break;
|
||||
@@ -26,9 +31,9 @@ std::string GLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const
|
||||
switch (src)
|
||||
{
|
||||
case TG_POS:
|
||||
return "vec4(posIn, 1.0);\n";
|
||||
return "vec4(posIn, 1.0)\n";
|
||||
case TG_NRM:
|
||||
return "vec4(normIn, 1.0);\n";
|
||||
return "vec4(normIn, 1.0)\n";
|
||||
case TG_UV:
|
||||
return HECL::Format("vec4(uvIn[%u], 0.0, 1.0)", uvIdx);
|
||||
default: break;
|
||||
@@ -81,7 +86,7 @@ std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs
|
||||
{
|
||||
if (skinSlots == 0)
|
||||
skinSlots = 1;
|
||||
std::string retval = HECL::Format("struct HECLVertUniform\n"
|
||||
std::string retval = HECL::Format("uniform HECLVertUniform\n"
|
||||
"{\n"
|
||||
" mat4 mv[%u];\n"
|
||||
" mat4 mvInv[%u];\n"
|
||||
@@ -89,7 +94,7 @@ std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs
|
||||
skinSlots, skinSlots);
|
||||
if (texMtxs)
|
||||
retval += HECL::Format(" mat4 texMtxs[%u];\n", texMtxs);
|
||||
return retval + "};\n";
|
||||
return retval + "} vu;\n";
|
||||
}
|
||||
|
||||
void GLSL::reset(const IR& ir, Diagnostics& diag)
|
||||
@@ -99,26 +104,25 @@ void GLSL::reset(const IR& ir, Diagnostics& diag)
|
||||
}
|
||||
|
||||
std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsigned w,
|
||||
unsigned skinSlots, unsigned texMtxs) const
|
||||
unsigned s, unsigned tm) const
|
||||
{
|
||||
std::string retval = std::string(glslVer) + "\n" +
|
||||
GenerateVertInStruct(col, uv, w) + "\n" +
|
||||
GenerateVertToFragStruct() + "\n" +
|
||||
GenerateVertUniformStruct(skinSlots, texMtxs) +
|
||||
"layout(location=0) uniform HECLVertUniform vu;\n"
|
||||
GenerateVertUniformStruct(s, tm) +
|
||||
"out VertToFrag vtf;\n\n"
|
||||
"void main()\n{\n";
|
||||
|
||||
if (skinSlots)
|
||||
if (s)
|
||||
{
|
||||
/* 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";
|
||||
for (size_t i=0 ; i<skinSlots ; ++i)
|
||||
retval += HECL::Format(" posAccum += (vu.mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "]\n"
|
||||
" normAccum += (vu.mvInv[%" PRISize "] * vec4(normIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "]\n",
|
||||
for (size_t i=0 ; i<s ; ++i)
|
||||
retval += HECL::Format(" posAccum += (vu.mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n"
|
||||
" normAccum += (vu.mvInv[%" PRISize "] * vec4(normIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n",
|
||||
i, i/4, i%4, i, i/4, i%4);
|
||||
retval += " posAccum[3] = 1.0\n"
|
||||
retval += " posAccum[3] = 1.0;\n"
|
||||
" vtf.mvPos = posAccum;\n"
|
||||
" vtf.mvNorm = vec4(normalize(normAccum.xyz), 0.0);\n"
|
||||
" gl_Position = vu.proj * posAccum;\n";
|
||||
@@ -127,7 +131,7 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
|
||||
{
|
||||
/* non-skinned */
|
||||
retval += " vtf.mvPos = vu.mv[0] * vec4(posIn, 1.0);\n"
|
||||
" vtf.mvNorm = vu.mvInv[0] * vec4(normIn, 0.0)\n"
|
||||
" vtf.mvNorm = vu.mvInv[0] * vec4(normIn, 0.0);\n"
|
||||
" gl_Position = vu.proj * vtf.mvPos;\n";
|
||||
}
|
||||
|
||||
@@ -147,62 +151,243 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
|
||||
}
|
||||
|
||||
std::string GLSL::makeFrag(const char* glslVer,
|
||||
const char* lightingSource, const char* lightingEntry) const
|
||||
const ShaderFunction& lighting) const
|
||||
{
|
||||
std::string lightingSrc;
|
||||
if (lightingSource)
|
||||
lightingSrc = lightingSource;
|
||||
if (lighting.m_source)
|
||||
lightingSrc = lighting.m_source;
|
||||
|
||||
std::string texMapDecl;
|
||||
if (m_texMapEnd)
|
||||
texMapDecl = HECL::Format("uniform sampler2D texs[%u];\n", m_texMapEnd);
|
||||
|
||||
std::string retval = std::string(glslVer) + "\n" +
|
||||
GenerateVertToFragStruct() +
|
||||
"\nlayout(location=0) out vec4 colorOut;\n"
|
||||
"in VertToFrag vtf;\n\n" + lightingSrc +
|
||||
"\nvoid main()\n{\n";
|
||||
"\nlayout(location=0) out vec4 colorOut;\n" +
|
||||
texMapDecl +
|
||||
"in VertToFrag vtf;\n\n" +
|
||||
lightingSrc + "\n" +
|
||||
"void main()\n{\n";
|
||||
|
||||
|
||||
if (m_lighting)
|
||||
{
|
||||
if (lightingEntry)
|
||||
retval += HECL::Format(" vec4 lighting = %s();\n", lightingEntry);
|
||||
if (lighting.m_entry)
|
||||
retval += HECL::Format(" vec4 lighting = %s();\n", lighting.m_entry);
|
||||
else
|
||||
retval += " vec4 lighting = vec4(1.0,1.0,1.0,1.0);\n";
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (m_alphaExpr.size())
|
||||
retval += " colorOut = vec4(" + m_colorExpr + ", " + m_alphaExpr + ");\n";
|
||||
else
|
||||
retval += " colorOut = vec4(" + m_colorExpr + ", 1.0);\n";
|
||||
|
||||
return retval + "};\n";
|
||||
return retval + "}\n";
|
||||
}
|
||||
|
||||
std::string GLSL::makeFrag(const char* glslVer,
|
||||
const char* lightingSource, const char* lightingEntry,
|
||||
const char* postSource, const char* postEntry) const
|
||||
const ShaderFunction& lighting,
|
||||
const ShaderFunction& post) const
|
||||
{
|
||||
std::string lightingSrc;
|
||||
if (lightingSource)
|
||||
lightingSrc = lightingSource;
|
||||
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);
|
||||
|
||||
std::string retval = std::string(glslVer) + "\n" +
|
||||
GenerateVertToFragStruct() +
|
||||
"\nlayout(location=0) out vec4 colorOut;\n"
|
||||
"in VertToFrag vtf;\n\n" + lightingSrc + "\n" + std::string(postSource) +
|
||||
"\nlayout(location=0) out vec4 colorOut;\n" +
|
||||
texMapDecl +
|
||||
"in VertToFrag vtf;\n\n" +
|
||||
lightingSrc + "\n" +
|
||||
postSrc +
|
||||
"\nvoid main()\n{\n";
|
||||
|
||||
if (m_lighting)
|
||||
{
|
||||
if (lightingEntry)
|
||||
retval += HECL::Format(" vec4 lighting = %s();\n", lightingEntry);
|
||||
if (lighting.m_entry)
|
||||
retval += HECL::Format(" vec4 lighting = %s();\n", lighting.m_entry);
|
||||
else
|
||||
retval += " vec4 lighting = vec4(1.0,1.0,1.0,1.0);\n";
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (m_alphaExpr.size())
|
||||
retval += " colorOut = " + std::string(postEntry) + "(vec4(" + m_colorExpr + ", " + m_alphaExpr + "));\n";
|
||||
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", " + m_alphaExpr + "));\n";
|
||||
else
|
||||
retval += " colorOut = " + std::string(postEntry) + "(vec4(" + m_colorExpr + ", 1.0));\n";
|
||||
retval += " colorOut = " + postEntry + "(vec4(" + m_colorExpr + ", 1.0));\n";
|
||||
|
||||
return retval + "};\n";
|
||||
return retval + "}\n";
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
w.writeUByte(m_backend.m_blendSrc);
|
||||
w.writeUByte(m_backend.m_blendDst);
|
||||
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);
|
||||
w.writeUByte(m_backend.m_blendSrc);
|
||||
w.writeUByte(m_backend.m_blendDst);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ unsigned ProgrammableCommon::addTexSampling(unsigned mapIdx, unsigned tcgIdx)
|
||||
TexSampling& samp = m_texSamplings.back();
|
||||
samp.mapIdx = mapIdx;
|
||||
samp.tcgIdx = tcgIdx;
|
||||
if (m_texMapEnd < mapIdx + 1)
|
||||
m_texMapEnd = mapIdx + 1;
|
||||
return m_texSamplings.size() - 1;
|
||||
}
|
||||
|
||||
@@ -92,7 +94,7 @@ std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& d
|
||||
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
|
||||
unsigned texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, -1);
|
||||
|
||||
return EmitSamplingUse(addTexSampling(mapIdx, texGenIdx));
|
||||
return EmitSamplingUseRGB(addTexSampling(mapIdx, texGenIdx));
|
||||
}
|
||||
else if (!name.compare("ColorReg"))
|
||||
{
|
||||
@@ -177,7 +179,7 @@ std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& d
|
||||
const IR::Instruction& tcgInst = inst.getChildInst(ir, 1);
|
||||
unsigned texGenIdx = RecursiveTraceTexGen(ir, diag, tcgInst, -1);
|
||||
|
||||
return EmitSamplingUse(addTexSampling(mapIdx, texGenIdx));
|
||||
return EmitSamplingUseAlpha(addTexSampling(mapIdx, texGenIdx));
|
||||
}
|
||||
else if (!name.compare("ColorReg"))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user