#include "HECL/Backend/Metal.hpp" #if HECL_HAS_METAL #include "HECL/Runtime.hpp" #include #include #include static LogVisor::LogModule Log("HECL::Backend::Metal"); namespace HECL { namespace Backend { std::string Metal::EmitTexGenSource2(TexGenSrc src, int uvIdx) const { switch (src) { case TG_POS: return "v.posIn.xy\n"; case TG_NRM: return "v.normIn.xy\n"; case TG_UV: return HECL::Format("v.uvIn%u", uvIdx); default: break; } return std::string(); } std::string Metal::EmitTexGenSource4(TexGenSrc src, int uvIdx) const { switch (src) { case TG_POS: return "float4(v.posIn, 1.0)\n"; case TG_NRM: return "float4(v.normIn, 1.0)\n"; case TG_UV: return HECL::Format("float4(v.uvIn%u, 0.0, 1.0)", uvIdx); default: break; } return std::string(); } std::string Metal::GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const { std::string retval = "struct VertData\n" "{\n" " float3 posIn [[ attribute(0) ]];\n" " float3 normIn [[ attribute(1) ]];\n"; unsigned idx = 2; if (col) { for (unsigned i=0 ; i tex%u [[ texture(%u) ]]", i, i); } std::string retval = "#include \nusing namespace metal;\n" "constexpr sampler samp(address::repeat);\n" + GenerateVertToFragStruct() + "\n" + lightingSrc + "\n" + "fragment float4 fmain(VertToFrag vtf [[ stage_in ]]" + texMapDecl + ")\n{\n"; if (m_lighting) { if (lighting.m_entry) retval += HECL::Format(" float4 lighting = %s();\n", lighting.m_entry); else retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; } unsigned sampIdx = 0; for (const TexSampling& sampling : m_texSamplings) retval += HECL::Format(" float4 sampling%u = tex%u.sample(samp, vtf.tcgs%u);\n", sampIdx++, sampling.mapIdx, sampling.tcgIdx); if (m_alphaExpr.size()) retval += " return float4(" + m_colorExpr + ", " + m_alphaExpr + ");\n"; else retval += " return float4(" + m_colorExpr + ", 1.0);\n"; return retval + "}\n"; } std::string Metal::makeFrag(const ShaderFunction& lighting, const ShaderFunction& post) const { std::string lightingSrc; 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) { for (int i=0 ; i tex%u [[ texture(%u) ]],\n", i, i); } std::string retval = "#include \nusing namespace metal;\n" "constexpr sampler samp(address::repeat);\n" + GenerateVertToFragStruct() + "\n" + lightingSrc + "\n" + "fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n" + texMapDecl + ")\n{\n"; if (m_lighting) { if (lighting.m_entry) retval += HECL::Format(" float4 lighting = %s();\n", lighting.m_entry); else retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; } unsigned sampIdx = 0; for (const TexSampling& sampling : m_texSamplings) retval += HECL::Format(" float4 sampling%u = tex%u.sample(samp, vtf.tcgs%u);\n", sampIdx++, sampling.mapIdx, sampling.tcgIdx); if (m_alphaExpr.size()) retval += " return " + postEntry + "(float4(" + m_colorExpr + ", " + m_alphaExpr + "));\n"; else retval += " return " + postEntry + "(float4(" + m_colorExpr + ", 1.0));\n"; return retval + "}\n"; } } namespace Runtime { struct MetalBackendFactory : IShaderBackendFactory { Backend::Metal m_backend; boo::MetalDataFactory* m_gfxFactory; MetalBackendFactory(boo::IGraphicsDataFactory* gfxFactory) : m_gfxFactory(dynamic_cast(gfxFactory)) {} ShaderCachedData buildShaderFromIR(const ShaderTag& tag, const HECL::Frontend::IR& ir, HECL::Frontend::Diagnostics& diag, boo::IShaderPipeline** objOut) { if (!m_rtHint) Log.report(LogVisor::FatalError, "ShaderCacheManager::setRenderTargetHint must be called before making metal shaders"); m_backend.reset(ir, diag); size_t cachedSz = 2; std::string vertSource = m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(), tag.getSkinSlotCount(), tag.getTexMtxCount()); cachedSz += vertSource.size() + 1; std::string fragSource = m_backend.makeFrag(); cachedSz += fragSource.size() + 1; *objOut = m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSource.c_str(), tag.newVertexFormat(m_gfxFactory), m_rtHint, 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_blendSrc); w.writeUByte(m_backend.m_blendDst); w.writeString(vertSource); w.writeString(fragSource); return dataOut; } boo::IShaderPipeline* buildShaderFromCache(const ShaderCachedData& data) { if (!m_rtHint) Log.report(LogVisor::FatalError, "ShaderCacheManager::setRenderTargetHint must be called before making metal shaders"); const ShaderTag& tag = data.m_tag; Athena::io::MemoryReader r(data.m_data.get(), data.m_sz); 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(), tag.newVertexFormat(m_gfxFactory), m_rtHint, 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& extensionSlots, FReturnExtensionShader returnFunc) { if (!m_rtHint) Log.report(LogVisor::FatalError, "ShaderCacheManager::setRenderTargetHint must be called before making metal shaders"); m_backend.reset(ir, diag); size_t cachedSz = 2; std::string vertSource = m_backend.makeVert(tag.getColorCount(), tag.getUvCount(), tag.getWeightCount(), tag.getSkinSlotCount(), tag.getTexMtxCount()); cachedSz += vertSource.size() + 1; std::vector fragSources; fragSources.reserve(extensionSlots.size()); for (const ShaderCacheExtensions::ExtensionSlot& slot : extensionSlots) { fragSources.push_back(m_backend.makeFrag(slot.lighting, slot.post)); cachedSz += fragSources.back().size() + 1; boo::IShaderPipeline* ret = m_gfxFactory->newShaderPipeline(vertSource.c_str(), fragSources.back().c_str(), tag.newVertexFormat(m_gfxFactory), m_rtHint, 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_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& extensionSlots, FReturnExtensionShader returnFunc) { if (!m_rtHint) Log.report(LogVisor::FatalError, "ShaderCacheManager::setRenderTargetHint must be called before making metal shaders"); const ShaderTag& tag = data.m_tag; Athena::io::MemoryReader r(data.m_data.get(), data.m_sz); 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(), tag.newVertexFormat(m_gfxFactory), m_rtHint, blendSrc, blendDst, tag.getDepthTest(), tag.getDepthWrite(), tag.getBackfaceCulling()); if (!ret) Log.report(LogVisor::FatalError, "unable to build shader"); returnFunc(ret); } } }; IShaderBackendFactory* _NewMetalBackendFactory(boo::IGraphicsDataFactory* gfxFactory) { return new struct MetalBackendFactory(gfxFactory); } } } #endif