From c544d99f98117b534e514f10025b781b6bc1b1ee Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Thu, 7 Jul 2016 14:05:45 -1000 Subject: [PATCH] Shader variable emit fixes --- hecl/extern/boo | 2 +- .../hecl/Backend/ProgrammableCommon.hpp | 14 ++++- hecl/lib/Backend/GLSL.cpp | 55 ++++++++++++++----- hecl/lib/Backend/ProgrammableCommon.cpp | 38 ++++++++----- 4 files changed, 78 insertions(+), 31 deletions(-) diff --git a/hecl/extern/boo b/hecl/extern/boo index 5c91ba8da..44e8317d4 160000 --- a/hecl/extern/boo +++ b/hecl/extern/boo @@ -1 +1 @@ -Subproject commit 5c91ba8da7951b2b039da5fb07cf5dcd49be2790 +Subproject commit 44e8317d49cd5be5776a48ebc9715495cb47552a diff --git a/hecl/include/hecl/Backend/ProgrammableCommon.hpp b/hecl/include/hecl/Backend/ProgrammableCommon.hpp index e16d3e3bb..48d517294 100644 --- a/hecl/include/hecl/Backend/ProgrammableCommon.hpp +++ b/hecl/include/hecl/Backend/ProgrammableCommon.hpp @@ -56,13 +56,18 @@ private: 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); @@ -78,6 +83,11 @@ private: return hecl::Format("colorReg%u", idx); } + std::string EmitLightingRaw() const + { + return std::string("lighting"); + } + std::string EmitLightingRGB() const { return std::string("lighting.rgb"); diff --git a/hecl/lib/Backend/GLSL.cpp b/hecl/lib/Backend/GLSL.cpp index ba427fc2c..6969290dd 100644 --- a/hecl/lib/Backend/GLSL.cpp +++ b/hecl/lib/Backend/GLSL.cpp @@ -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; @@ -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; @@ -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 8) + Log.report(logvisor::Fatal, "maximum of 8 texture maps supported"); + objOut = static_cast(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(), @@ -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(ctx). newShaderPipeline(vertSource.c_str(), fragSource.c_str(), - texMapEnd, "texs", + texMapEnd, STD_TEXNAMES, 2, STD_BLOCKNAMES, blendSrc, blendDst, tag.getPrimType(), tag.getDepthTest(), tag.getDepthWrite(), @@ -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 fragSources; fragSources.reserve(extensionSlots.size()); for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots) @@ -355,7 +378,7 @@ struct GLSLBackendFactory : IShaderBackendFactory boo::IShaderPipeline* ret = static_cast(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()); @@ -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; @@ -401,7 +428,7 @@ struct GLSLBackendFactory : IShaderBackendFactory boo::IShaderPipeline* ret = static_cast(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()); diff --git a/hecl/lib/Backend/ProgrammableCommon.cpp b/hecl/lib/Backend/ProgrammableCommon.cpp index 120a30025..ee40b3c11 100644 --- a/hecl/lib/Backend/ProgrammableCommon.cpp +++ b/hecl/lib/Backend/ProgrammableCommon.cpp @@ -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) { @@ -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")) { @@ -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()); @@ -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) { @@ -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: @@ -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) { @@ -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")) { @@ -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()); @@ -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) { @@ -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: @@ -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 */ @@ -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); } }