#include "hecl/Backend/HLSL.hpp" #include "hecl/Runtime.hpp" #include #include #include static logvisor::Module Log("hecl::Backend::HLSL"); namespace hecl::Backend { std::string HLSL::EmitTexGenSource2(TexGenSrc src, int uvIdx) const { switch (src) { case TexGenSrc::Position: return "objPos.xy\n"; case TexGenSrc::Normal: return "objNorm.xy\n"; case TexGenSrc::UV: return hecl::Format("v.uvIn[%u]", uvIdx); default: break; } return std::string(); } std::string HLSL::EmitTexGenSource4(TexGenSrc src, int uvIdx) const { switch (src) { case TexGenSrc::Position: return "float4(objPos.xyz, 1.0)\n"; case TexGenSrc::Normal: return "float4(objNorm.xyz, 1.0)\n"; case TexGenSrc::UV: return hecl::Format("float4(v.uvIn[%u], 0.0, 1.0)", uvIdx); default: break; } return std::string(); } std::string HLSL::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const { std::string retval = "struct VertData\n" "{\n" " float3 posIn : POSITION;\n" " float3 normIn : NORMAL;\n"; if (col) retval += hecl::Format(" float4 colIn[%u] : COLOR;\n", col); if (uv) retval += hecl::Format(" float2 uvIn[%u] : UV;\n", uv); if (w) retval += hecl::Format(" float4 weightIn[%u] : WEIGHT;\n", w); return retval + "};\n"; } std::string HLSL::GenerateVertToFragStruct(size_t extTexCount, bool reflectionCoords) const { std::string retval = "struct VertToFrag\n" "{\n" " float4 mvpPos : SV_Position;\n" " float4 mvPos : POSITION;\n" " float4 mvNorm : NORMAL;\n"; if (m_tcgs.size()) retval += hecl::Format(" float2 tcgs[%u] : UV;\n", unsigned(m_tcgs.size())); if (extTexCount) retval += hecl::Format(" float2 extTcgs[%u] : EXTUV;\n", unsigned(extTexCount)); if (reflectionCoords) retval += " float2 reflectTcgs[2] : REFLECTUV;\n" " float reflectAlpha : REFLECTALPHA;\n"; return retval + "};\n"; } std::string HLSL::GenerateVertUniformStruct(unsigned skinSlots, bool reflectionCoords) const { std::string retval; if (skinSlots == 0) { retval = "cbuffer HECLVertUniform : register(b0)\n" "{\n" " float4x4 mv;\n" " float4x4 mvInv;\n" " float4x4 proj;\n" "};\n"; } else { retval = hecl::Format("cbuffer HECLVertUniform : register(b0)\n" "{\n" " float4x4 objs[%u];\n" " float4x4 objsInv[%u];\n" " float4x4 mv;\n" " float4x4 mvInv;\n" " float4x4 proj;\n" "};\n", skinSlots, skinSlots); } retval += "struct TCGMtx\n" "{\n" " float4x4 mtx;\n" " float4x4 postMtx;\n" "};\n" "cbuffer HECLTCGMatrix : register(b1)\n" "{\n" " TCGMtx texMtxs[8];\n" "};\n"; if (reflectionCoords) retval += "cbuffer HECLReflectMtx : register(b3)\n" "{\n" " float4x4 indMtx;\n" " float4x4 reflectMtx;\n" " float reflectAlpha;\n" "};\n" "\n"; return retval; } std::string HLSL::GenerateAlphaTest() const { return " if (colorOut.a < 0.01)\n" " {\n" " discard;\n" " }\n"; } std::string HLSL::GenerateReflectionExpr(ReflectionType type) const { switch (type) { case ReflectionType::None: default: return "float3(0.0, 0.0, 0.0)"; case ReflectionType::Simple: return "reflectionTex.Sample(reflectSamp, vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha"; case ReflectionType::Indirect: return "reflectionTex.Sample(reflectSamp, (reflectionIndTex.Sample(samp, vtf.reflectTcgs[0]).rg - " "float2(0.5, 0.5)) * float2(0.5, 0.5) + vtf.reflectTcgs[1]).rgb * vtf.reflectAlpha"; } } void HLSL::reset(const IR& ir, Diagnostics& diag) { /* Common programmable interpretation */ ProgrammableCommon::reset(ir, diag, "HLSL"); } std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w, unsigned s, size_t extTexCount, const TextureInfo* extTexs, ReflectionType reflectionType) const { std::string retval = GenerateVertInStruct(col, uv, w) + "\n" + GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" + GenerateVertUniformStruct(s, reflectionType != ReflectionType::None) + "\n" + "VertToFrag main(in VertData v)\n" "{\n" " VertToFrag vtf;\n"; if (s) { /* skinned */ retval += " float4 objPos = float4(0.0,0.0,0.0,0.0);\n" " float4 objNorm = float4(0.0,0.0,0.0,0.0);\n"; for (size_t i=0 ; i