Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Shader variable emit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoalan committed Jul 8, 2016
1 parent eeb595c commit cbb9c25
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion extern/boo
14 changes: 12 additions & 2 deletions include/hecl/Backend/ProgrammableCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ struct ProgrammableCommon : IBackend
unsigned addTexCoordGen(TexGenSrc src, int uvIdx, int mtx);
unsigned addTexSampling(unsigned mapIdx, unsigned tcgIdx);
std::string RecursiveTraceColor(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst);
const IR::Instruction& inst, bool toSwizzle);
std::string RecursiveTraceAlpha(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst);
const IR::Instruction& inst, bool toSwizzle);
unsigned RecursiveTraceTexGen(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst,
int mtx);

std::string EmitSamplingUseRaw(unsigned samplingIdx) const
{
return hecl::Format("sampling%u", samplingIdx);
}

std::string EmitSamplingUseRGB(unsigned samplingIdx) const
{
return hecl::Format("sampling%u.rgb", samplingIdx);
Expand All @@ -78,6 +83,11 @@ struct ProgrammableCommon : IBackend
return hecl::Format("colorReg%u", idx);
}

std::string EmitLightingRaw() const
{
return std::string("lighting");
}

std::string EmitLightingRGB() const
{
return std::string("lighting.rgb");
Expand Down
55 changes: 41 additions & 14 deletions lib/Backend/GLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ std::string GLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const
switch (src)
{
case TexGenSrc::Position:
return "posIn.xy\n";
return "posIn.xy";
case TexGenSrc::Normal:
return "normIn.xy\n";
return "normIn.xy";
case TexGenSrc::UV:
return hecl::Format("uvIn[%u]", uvIdx);
default: break;
Expand All @@ -32,9 +32,9 @@ std::string GLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const
switch (src)
{
case TexGenSrc::Position:
return "vec4(posIn, 1.0)\n";
return "vec4(posIn, 1.0)";
case TexGenSrc::Normal:
return "vec4(normIn, 1.0)\n";
return "vec4(normIn, 1.0)";
case TexGenSrc::UV:
return hecl::Format("vec4(uvIn[%u], 0.0, 1.0)", uvIdx);
default: break;
Expand Down Expand Up @@ -166,8 +166,8 @@ std::string GLSL::makeFrag(const char* glslVer,
lightingSrc = lighting.m_source;

std::string texMapDecl;
if (m_texMapEnd)
texMapDecl = hecl::Format("TBINDING0 uniform sampler2D texs[%u];\n", m_texMapEnd);
for (int i=0 ; i<m_texMapEnd ; ++i)
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D tex%u;\n", i, i);

std::string retval = std::string(glslVer) + "\n" BOO_GLSL_BINDING_HEAD +
GenerateVertToFragStruct() +
Expand All @@ -188,7 +188,7 @@ std::string GLSL::makeFrag(const char* glslVer,

unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
retval += hecl::Format(" vec4 sampling%u = texture(texs[%u], vtf.tcgs[%u]);\n",
retval += hecl::Format(" vec4 sampling%u = texture(tex%u, vtf.tcgs[%u]);\n",
sampIdx++, sampling.mapIdx, sampling.tcgIdx);

if (m_alphaExpr.size())
Expand Down Expand Up @@ -216,8 +216,8 @@ std::string GLSL::makeFrag(const char* glslVer,
postEntry = post.m_entry;

std::string texMapDecl;
if (m_texMapEnd)
texMapDecl = hecl::Format("TBINDING0 uniform sampler2D texs[%u];\n", m_texMapEnd);
for (int i=0 ; i<m_texMapEnd ; ++i)
texMapDecl += hecl::Format("TBINDING%u uniform sampler2D tex%u;\n", i, i);

std::string retval = std::string(glslVer) + "\n" BOO_GLSL_BINDING_HEAD +
GenerateVertToFragStruct() +
Expand All @@ -238,7 +238,7 @@ std::string GLSL::makeFrag(const char* glslVer,

unsigned sampIdx = 0;
for (const TexSampling& sampling : m_texSamplings)
retval += hecl::Format(" vec4 sampling%u = texture(texs[%u], vtf.tcgs[%u]);\n",
retval += hecl::Format(" vec4 sampling%u = texture(tex%u, vtf.tcgs[%u]);\n",
sampIdx++, sampling.mapIdx, sampling.tcgIdx);

if (m_alphaExpr.size())
Expand All @@ -256,6 +256,18 @@ namespace Runtime
static const char* STD_BLOCKNAMES[] = {HECL_GLSL_VERT_UNIFORM_BLOCK_NAME,
HECL_GLSL_TEXMTX_UNIFORM_BLOCK_NAME};

static const char* STD_TEXNAMES[] =
{
"tex0",
"tex1",
"tex2",
"tex3",
"tex4",
"tex5",
"tex6",
"tex7"
};

struct GLSLBackendFactory : IShaderBackendFactory
{
Backend::GLSL m_backend;
Expand All @@ -277,10 +289,14 @@ struct GLSLBackendFactory : IShaderBackendFactory

std::string fragSource = m_backend.makeFrag("#version 330");
cachedSz += fragSource.size() + 1;

if (m_backend.m_texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");

objOut =
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
m_backend.m_texMapEnd, "texs",
m_backend.m_texMapEnd, STD_TEXNAMES,
2, STD_BLOCKNAMES,
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
tag.getDepthTest(), tag.getDepthWrite(),
Expand Down Expand Up @@ -309,10 +325,14 @@ struct GLSLBackendFactory : IShaderBackendFactory
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
std::string vertSource = r.readString();
std::string fragSource = r.readString();

if (texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");

boo::IShaderPipeline* ret =
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs",
texMapEnd, STD_TEXNAMES,
2, STD_BLOCKNAMES,
blendSrc, blendDst, tag.getPrimType(),
tag.getDepthTest(), tag.getDepthWrite(),
Expand All @@ -338,6 +358,9 @@ struct GLSLBackendFactory : IShaderBackendFactory
tag.getSkinSlotCount(), tag.getTexMtxCount());
cachedSz += vertSource.size() + 1;

if (m_backend.m_texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");

std::vector<std::string> fragSources;
fragSources.reserve(extensionSlots.size());
for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
Expand All @@ -355,7 +378,7 @@ struct GLSLBackendFactory : IShaderBackendFactory
boo::IShaderPipeline* ret =
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSources.back().c_str(),
m_backend.m_texMapEnd, "texs", bc, bn,
m_backend.m_texMapEnd, STD_TEXNAMES, bc, bn,
m_backend.m_blendSrc, m_backend.m_blendDst, tag.getPrimType(),
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
Expand Down Expand Up @@ -387,6 +410,10 @@ struct GLSLBackendFactory : IShaderBackendFactory
boo::BlendFactor blendSrc = boo::BlendFactor(r.readUByte());
boo::BlendFactor blendDst = boo::BlendFactor(r.readUByte());
std::string vertSource = r.readString();

if (texMapEnd > 8)
Log.report(logvisor::Fatal, "maximum of 8 texture maps supported");

for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots)
{
size_t bc = 2;
Expand All @@ -401,7 +428,7 @@ struct GLSLBackendFactory : IShaderBackendFactory
boo::IShaderPipeline* ret =
static_cast<boo::GLDataFactory::Context&>(ctx).
newShaderPipeline(vertSource.c_str(), fragSource.c_str(),
texMapEnd, "texs", bc, bn,
texMapEnd, STD_TEXNAMES, bc, bn,
blendSrc, blendDst, tag.getPrimType(),
tag.getDepthTest(), tag.getDepthWrite(),
tag.getBackfaceCulling());
Expand Down
38 changes: 24 additions & 14 deletions lib/Backend/ProgrammableCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ unsigned ProgrammableCommon::RecursiveTraceTexGen(const IR& ir, Diagnostics& dia
}

std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst)
const IR::Instruction& inst, bool toSwizzle)
{
switch (inst.m_op)
{
Expand All @@ -94,7 +94,8 @@ 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 EmitSamplingUseRGB(addTexSampling(mapIdx, texGenIdx));
return toSwizzle ? EmitSamplingUseRaw(addTexSampling(mapIdx, texGenIdx)) :
EmitSamplingUseRGB(addTexSampling(mapIdx, texGenIdx));
}
else if (!name.compare("ColorReg"))
{
Expand All @@ -105,7 +106,7 @@ std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& d
else if (!name.compare("Lighting"))
{
m_lighting = true;
return EmitLightingRGB();
return toSwizzle ? EmitLightingRaw() : EmitLightingRGB();
}
else
diag.reportBackendErr(inst.m_loc, "unable to interpret '%s'", name.c_str());
Expand All @@ -121,8 +122,8 @@ std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& d
ArithmeticOp op = inst.m_arithmetic.m_op;
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
const IR::Instruction& bInst = inst.getChildInst(ir, 1);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst);
std::string bTrace = RecursiveTraceColor(ir, diag, bInst);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst, false);
std::string bTrace = RecursiveTraceColor(ir, diag, bInst, false);

switch (op)
{
Expand All @@ -149,7 +150,7 @@ std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& d
case IR::OpType::Swizzle:
{
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst);
std::string aTrace = RecursiveTraceColor(ir, diag, aInst, true);
return EmitSwizzle3(diag, inst.m_loc, aTrace, inst.m_swizzle.m_idxs);
}
default:
Expand All @@ -160,7 +161,7 @@ std::string ProgrammableCommon::RecursiveTraceColor(const IR& ir, Diagnostics& d
}

std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& diag,
const IR::Instruction& inst)
const IR::Instruction& inst, bool toSwizzle)
{
switch (inst.m_op)
{
Expand All @@ -179,7 +180,8 @@ 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 EmitSamplingUseAlpha(addTexSampling(mapIdx, texGenIdx));
return toSwizzle ? EmitSamplingUseRaw(addTexSampling(mapIdx, texGenIdx)) :
EmitSamplingUseAlpha(addTexSampling(mapIdx, texGenIdx));
}
else if (!name.compare("ColorReg"))
{
Expand All @@ -190,7 +192,7 @@ std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& d
else if (!name.compare("Lighting"))
{
m_lighting = true;
return EmitLightingAlpha();
return toSwizzle ? EmitLightingRaw() : EmitLightingAlpha();
}
else
diag.reportBackendErr(inst.m_loc, "unable to interpret '%s'", name.c_str());
Expand All @@ -206,8 +208,8 @@ std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& d
ArithmeticOp op = inst.m_arithmetic.m_op;
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
const IR::Instruction& bInst = inst.getChildInst(ir, 1);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst);
std::string bTrace = RecursiveTraceAlpha(ir, diag, bInst);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst, false);
std::string bTrace = RecursiveTraceAlpha(ir, diag, bInst, false);

switch (op)
{
Expand All @@ -234,7 +236,7 @@ std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& d
case IR::OpType::Swizzle:
{
const IR::Instruction& aInst = inst.getChildInst(ir, 0);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst);
std::string aTrace = RecursiveTraceAlpha(ir, diag, aInst, true);
return EmitSwizzle1(diag, inst.m_loc, aTrace, inst.m_swizzle.m_idxs);
}
default:
Expand All @@ -246,6 +248,14 @@ std::string ProgrammableCommon::RecursiveTraceAlpha(const IR& ir, Diagnostics& d

void ProgrammableCommon::reset(const IR& ir, Diagnostics& diag, const char* backendName)
{
m_lighting = false;
m_texSamplings.clear();
m_texMapEnd = 0;
m_tcgs.clear();
m_texMtxRefs.clear();
m_colorExpr.clear();
m_alphaExpr.clear();

diag.setBackend(backendName);

/* Final instruction is the root call by hecl convention */
Expand Down Expand Up @@ -278,14 +288,14 @@ void ProgrammableCommon::reset(const IR& ir, Diagnostics& diag, const char* back
/* Follow Color Chain */
const IR::Instruction& colorRoot =
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(0));
m_colorExpr = RecursiveTraceColor(ir, diag, colorRoot);
m_colorExpr = RecursiveTraceColor(ir, diag, colorRoot, false);

/* Follow Alpha Chain */
if (doAlpha)
{
const IR::Instruction& alphaRoot =
ir.m_instructions.at(rootCall.m_call.m_argInstIdxs.at(1));
m_alphaExpr = RecursiveTraceAlpha(ir, diag, alphaRoot);
m_alphaExpr = RecursiveTraceAlpha(ir, diag, alphaRoot, false);
}
}

Expand Down

0 comments on commit cbb9c25

Please sign in to comment.