#include "HECL/Backend/GLSL.hpp" #include namespace HECL { namespace Backend { std::string GLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const { switch (src) { case TG_POS: return "posIn.xy;\n"; case TG_NRM: return "normIn.xy;\n"; case TG_UV: return HECL::Format("uvIn[%u]", uvIdx); default: break; } return std::string(); } std::string GLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const { switch (src) { case TG_POS: return "vec4(posIn, 1.0);\n"; case TG_NRM: return "vec4(normIn, 1.0);\n"; case TG_UV: return HECL::Format("vec4(uvIn[%u], 0.0, 1.0)", uvIdx); default: break; } return std::string(); } std::string GLSL::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const { std::string retval = "layout(location=0) in vec3 posIn;\n" "layout(location=1) in vec3 normIn;\n"; unsigned idx = 2; if (col) { retval += HECL::Format("layout(location=%u) in vec4 colIn[%u];\n", idx, col); idx += col; } if (uv) { retval += HECL::Format("layout(location=%u) in vec2 uvIn[%u];\n", idx, uv); idx += uv; } if (w) { retval += HECL::Format("layout(location=%u) in vec4 weightIn[%u];\n", idx, w); } return retval; } std::string GLSL::GenerateVertToFragStruct() const { std::string retval = "struct VertToFrag\n" "{\n" " vec4 mvPos;\n" " vec4 mvNorm;\n"; if (m_tcgs.size()) retval += HECL::Format(" vec2 tcgs[%u];\n", unsigned(m_tcgs.size())); return retval + "};\n"; } std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs) const { if (skinSlots == 0) skinSlots = 1; std::string retval = HECL::Format("struct HECLVertUniform\n" "{\n" " mat4 mv[%u];\n" " mat4 mvInv[%u];\n" " mat4 proj;\n", skinSlots, skinSlots); if (texMtxs) retval += HECL::Format(" mat4 texMtxs[%u];\n", texMtxs); return retval + "};\n"; } void GLSL::reset(const IR& ir, Diagnostics& diag) { /* Common programmable interpretation */ ProgrammableCommon::reset(ir, diag, "GLSL"); } std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsigned w, unsigned skinSlots, unsigned texMtxs) 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" "out VertToFrag vtf;\n\n" "void main()\n{\n"; if (skinSlots) { /* 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 (int i=0 ; i