mirror of https://github.com/AxioDL/metaforce.git
Metal shader generation
This commit is contained in:
parent
e5c4008cfb
commit
daa446588b
|
@ -1 +1 @@
|
||||||
Subproject commit b97ad76c45b4965f3dd9f499e5975738714f5fa8
|
Subproject commit 62fae6004226348bdf5e3b0f5401d091128ec86e
|
|
@ -0,0 +1,47 @@
|
||||||
|
#ifndef HECLBACKEND_METAL_HPP
|
||||||
|
#define HECLBACKEND_METAL_HPP
|
||||||
|
#if __APPLE__
|
||||||
|
|
||||||
|
#include "ProgrammableCommon.hpp"
|
||||||
|
#include <Availability.h>
|
||||||
|
|
||||||
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
|
||||||
|
#define HECL_HAS_METAL 1
|
||||||
|
|
||||||
|
namespace HECL
|
||||||
|
{
|
||||||
|
namespace Backend
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Metal : ProgrammableCommon
|
||||||
|
{
|
||||||
|
void reset(const IR& ir, Diagnostics& diag);
|
||||||
|
std::string makeVert(unsigned col, unsigned uv, unsigned w,
|
||||||
|
unsigned skinSlots, unsigned texMtxs) const;
|
||||||
|
std::string makeFrag(const ShaderFunction& lighting=ShaderFunction()) const;
|
||||||
|
std::string makeFrag(const ShaderFunction& lighting,
|
||||||
|
const ShaderFunction& post) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string GenerateVertInStruct(unsigned col, unsigned uv, unsigned w) const;
|
||||||
|
std::string GenerateVertToFragStruct() const;
|
||||||
|
std::string GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs) const;
|
||||||
|
|
||||||
|
std::string EmitVec3(const atVec4f& vec) const
|
||||||
|
{
|
||||||
|
return HECL::Format("float3(%g,%g,%g)", vec.vec[0], vec.vec[1], vec.vec[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string EmitTexGenSource2(TexGenSrc src, int uvIdx) const;
|
||||||
|
std::string EmitTexGenSource4(TexGenSrc src, int uvIdx) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define HECL_HAS_METAL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // __APPLE__
|
||||||
|
#endif // HECLBACKEND_METAL_HPP
|
|
@ -149,6 +149,7 @@ class IShaderBackendFactory
|
||||||
{
|
{
|
||||||
friend class ShaderCacheManager;
|
friend class ShaderCacheManager;
|
||||||
protected:
|
protected:
|
||||||
|
boo::ITextureR* m_rtHint = nullptr;
|
||||||
using FReturnExtensionShader = std::function<void(boo::IShaderPipeline*)>;
|
using FReturnExtensionShader = std::function<void(boo::IShaderPipeline*)>;
|
||||||
virtual ShaderCachedData buildShaderFromIR(const ShaderTag& tag,
|
virtual ShaderCachedData buildShaderFromIR(const ShaderTag& tag,
|
||||||
const HECL::Frontend::IR& ir,
|
const HECL::Frontend::IR& ir,
|
||||||
|
@ -205,6 +206,10 @@ public:
|
||||||
: ShaderCacheManager(storeMgr, gfxFactory, ShaderCacheExtensions()) {}
|
: ShaderCacheManager(storeMgr, gfxFactory, ShaderCacheExtensions()) {}
|
||||||
void reload();
|
void reload();
|
||||||
|
|
||||||
|
/* Some platforms (like Metal) require information about the render target
|
||||||
|
* for encoding the pipeline state. This must be called before building shaders */
|
||||||
|
void setRenderTargetHint(boo::ITextureR* tex) {m_factory->m_rtHint = tex;}
|
||||||
|
|
||||||
boo::IShaderPipeline* buildShader(const ShaderTag& tag, const std::string& source,
|
boo::IShaderPipeline* buildShader(const ShaderTag& tag, const std::string& source,
|
||||||
const std::string& diagName);
|
const std::string& diagName);
|
||||||
boo::IShaderPipeline* buildShader(const ShaderTag& tag, const HECL::Frontend::IR& ir,
|
boo::IShaderPipeline* buildShader(const ShaderTag& tag, const HECL::Frontend::IR& ir,
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
|
if(WIN32)
|
||||||
|
set(PLAT_SRCS HLSL.cpp)
|
||||||
|
endif()
|
||||||
|
if(APPLE)
|
||||||
|
set(PLAT_SRCS Metal.cpp)
|
||||||
|
endif()
|
||||||
|
|
||||||
add_library(HECLBackend
|
add_library(HECLBackend
|
||||||
GX.cpp
|
GX.cpp
|
||||||
ProgrammableCommon.cpp
|
ProgrammableCommon.cpp
|
||||||
GLSL.cpp
|
GLSL.cpp
|
||||||
HLSL.cpp)
|
${PLAT_SRCS})
|
||||||
|
|
|
@ -94,7 +94,7 @@ std::string GLSL::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs
|
||||||
skinSlots, skinSlots);
|
skinSlots, skinSlots);
|
||||||
if (texMtxs)
|
if (texMtxs)
|
||||||
retval += HECL::Format(" mat4 texMtxs[%u];\n", texMtxs);
|
retval += HECL::Format(" mat4 texMtxs[%u];\n", texMtxs);
|
||||||
return retval + "} vu;\n";
|
return retval + "};\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSL::reset(const IR& ir, Diagnostics& diag)
|
void GLSL::reset(const IR& ir, Diagnostics& diag)
|
||||||
|
@ -119,20 +119,20 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
|
||||||
retval += " vec4 posAccum = vec4(0.0,0.0,0.0,0.0);\n"
|
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";
|
" vec4 normAccum = vec4(0.0,0.0,0.0,0.0);\n";
|
||||||
for (size_t i=0 ; i<s ; ++i)
|
for (size_t i=0 ; i<s ; ++i)
|
||||||
retval += HECL::Format(" posAccum += (vu.mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n"
|
retval += HECL::Format(" posAccum += (mv[%" PRISize "] * vec4(posIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n"
|
||||||
" normAccum += (vu.mvInv[%" PRISize "] * vec4(normIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n",
|
" normAccum += (mvInv[%" PRISize "] * vec4(normIn, 1.0)) * weightIn[%" PRISize "][%" PRISize "];\n",
|
||||||
i, i/4, i%4, i, i/4, i%4);
|
i, i/4, i%4, i, i/4, i%4);
|
||||||
retval += " posAccum[3] = 1.0;\n"
|
retval += " posAccum[3] = 1.0;\n"
|
||||||
" vtf.mvPos = posAccum;\n"
|
" vtf.mvPos = posAccum;\n"
|
||||||
" vtf.mvNorm = vec4(normalize(normAccum.xyz), 0.0);\n"
|
" vtf.mvNorm = vec4(normalize(normAccum.xyz), 0.0);\n"
|
||||||
" gl_Position = vu.proj * posAccum;\n";
|
" gl_Position = proj * posAccum;\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* non-skinned */
|
/* non-skinned */
|
||||||
retval += " vtf.mvPos = vu.mv[0] * vec4(posIn, 1.0);\n"
|
retval += " vtf.mvPos = mv[0] * vec4(posIn, 1.0);\n"
|
||||||
" vtf.mvNorm = vu.mvInv[0] * vec4(normIn, 0.0);\n"
|
" vtf.mvNorm = mvInv[0] * vec4(normIn, 0.0);\n"
|
||||||
" gl_Position = vu.proj * vtf.mvPos;\n";
|
" gl_Position = proj * vtf.mvPos;\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
int tcgIdx = 0;
|
int tcgIdx = 0;
|
||||||
|
@ -142,7 +142,7 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
|
||||||
retval += HECL::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
|
retval += HECL::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
|
||||||
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
|
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
|
||||||
else
|
else
|
||||||
retval += HECL::Format(" vtf.tcgs[%u] = (vu.texMtxs[%u] * %s).xy;\n", tcgIdx, tcg.m_mtx,
|
retval += HECL::Format(" vtf.tcgs[%u] = (texMtxs[%u] * %s).xy;\n", tcgIdx, tcg.m_mtx,
|
||||||
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
|
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
|
||||||
++tcgIdx;
|
++tcgIdx;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,407 @@
|
||||||
|
#include "HECL/Backend/Metal.hpp"
|
||||||
|
#if HECL_HAS_METAL
|
||||||
|
#include "HECL/Runtime.hpp"
|
||||||
|
#include <Athena/MemoryReader.hpp>
|
||||||
|
#include <Athena/MemoryWriter.hpp>
|
||||||
|
#include <boo/graphicsdev/Metal.hpp>
|
||||||
|
|
||||||
|
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<col ; ++i, ++idx)
|
||||||
|
retval += HECL::Format(" float4 colIn%u [[ attribute(%u) ]];\n", i, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uv)
|
||||||
|
{
|
||||||
|
for (unsigned i=0 ; i<uv ; ++i, ++idx)
|
||||||
|
retval += HECL::Format(" float2 uvIn%u [[ attribute(%u) ]];\n", i, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (w)
|
||||||
|
{
|
||||||
|
for (unsigned i=0 ; i<w ; ++i, ++idx)
|
||||||
|
retval += HECL::Format(" float4 weightIn%u [[ attribute(%u) ]];\n", i, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval + "};\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Metal::GenerateVertToFragStruct() const
|
||||||
|
{
|
||||||
|
std::string retval =
|
||||||
|
"struct VertToFrag\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 mvpPos [[ position ]];\n"
|
||||||
|
" float4 mvPos;\n"
|
||||||
|
" float4 mvNorm;\n";
|
||||||
|
|
||||||
|
if (m_tcgs.size())
|
||||||
|
{
|
||||||
|
for (size_t i=0 ; i<m_tcgs.size() ; ++i)
|
||||||
|
retval += HECL::Format(" float2 tcgs%" PRISize ";\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval + "};\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Metal::GenerateVertUniformStruct(unsigned skinSlots, unsigned texMtxs) const
|
||||||
|
{
|
||||||
|
if (skinSlots == 0)
|
||||||
|
skinSlots = 1;
|
||||||
|
std::string retval = HECL::Format("struct HECLVertUniform\n"
|
||||||
|
"{\n"
|
||||||
|
" float4x4 mv[%u];\n"
|
||||||
|
" float4x4 mvInv[%u];\n"
|
||||||
|
" float4x4 proj;\n",
|
||||||
|
skinSlots, skinSlots);
|
||||||
|
if (texMtxs)
|
||||||
|
retval += HECL::Format(" float4x4 texMtxs[%u];\n", texMtxs);
|
||||||
|
return retval + "};\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Metal::reset(const IR& ir, Diagnostics& diag)
|
||||||
|
{
|
||||||
|
/* Common programmable interpretation */
|
||||||
|
ProgrammableCommon::reset(ir, diag, "Metal");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
|
||||||
|
unsigned s, unsigned tm) const
|
||||||
|
{
|
||||||
|
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" +
|
||||||
|
GenerateVertInStruct(col, uv, w) + "\n" +
|
||||||
|
GenerateVertToFragStruct() + "\n" +
|
||||||
|
GenerateVertUniformStruct(s, tm) +
|
||||||
|
"\nvertex VertToFrag vmain(VertData v [[ stage_in ]], constant HECLVertUniform& vu [[ buffer(1) ]])\n{\n"
|
||||||
|
" VertToFrag vtf;\n";
|
||||||
|
|
||||||
|
if (s)
|
||||||
|
{
|
||||||
|
/* skinned */
|
||||||
|
retval += " float4 posAccum = float4(0.0,0.0,0.0,0.0);\n"
|
||||||
|
" float4 normAccum = float4(0.0,0.0,0.0,0.0);\n";
|
||||||
|
for (size_t i=0 ; i<s ; ++i)
|
||||||
|
retval += HECL::Format(" posAccum += (vu.mv[%" PRISize "] * float4(v.posIn, 1.0)) * v.weightIn%" PRISize "[%" PRISize "];\n"
|
||||||
|
" normAccum += (vu.mvInv[%" PRISize "] * float4(v.normIn, 1.0)) * v.weightIn%" PRISize "[%" PRISize "];\n",
|
||||||
|
i, i/4, i%4, i, i/4, i%4);
|
||||||
|
retval += " posAccum[3] = 1.0;\n"
|
||||||
|
" vtf.mvPos = posAccum;\n"
|
||||||
|
" vtf.mvNorm = float4(normalize(normAccum.xyz), 0.0);\n"
|
||||||
|
" vtf.mvpPos = vu.proj * posAccum;\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* non-skinned */
|
||||||
|
retval += " vtf.mvPos = vu.mv[0] * float4(v.posIn, 1.0);\n"
|
||||||
|
" vtf.mvNorm = vu.mvInv[0] * float4(v.normIn, 0.0);\n"
|
||||||
|
" vtf.mvpPos = vu.proj * vtf.mvPos;\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int tcgIdx = 0;
|
||||||
|
for (const TexCoordGen& tcg : m_tcgs)
|
||||||
|
{
|
||||||
|
if (tcg.m_mtx < 0)
|
||||||
|
retval += HECL::Format(" vtf.tcgs%u = %s;\n", tcgIdx,
|
||||||
|
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
|
||||||
|
else
|
||||||
|
retval += HECL::Format(" vtf.tcgs%u = (vu.texMtxs[%u] * %s).xy;\n", tcgIdx, tcg.m_mtx,
|
||||||
|
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
|
||||||
|
++tcgIdx;
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval + " return vtf;\n}\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Metal::makeFrag(const ShaderFunction& lighting) const
|
||||||
|
{
|
||||||
|
std::string lightingSrc;
|
||||||
|
if (lighting.m_source)
|
||||||
|
lightingSrc = lighting.m_source;
|
||||||
|
|
||||||
|
std::string texMapDecl;
|
||||||
|
if (m_texMapEnd)
|
||||||
|
{
|
||||||
|
for (int i=0 ; i<m_texMapEnd ; ++i)
|
||||||
|
texMapDecl += HECL::Format("\n, texture2d<float> tex%u [[ texture(%u) ]]", i, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string retval = "#include <metal_stdlib>\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<m_texMapEnd ; ++i)
|
||||||
|
texMapDecl += HECL::Format("texture2d<float> tex%u [[ texture(%u) ]],\n", i, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string retval = "#include <metal_stdlib>\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<boo::MetalDataFactory*>(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<ShaderCacheExtensions::ExtensionSlot>& 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<std::string> 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<ShaderCacheExtensions::ExtensionSlot>& 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
|
|
@ -22,6 +22,7 @@ add_library(HECLCommon
|
||||||
../include/HECL/Backend/ProgrammableCommon.hpp
|
../include/HECL/Backend/ProgrammableCommon.hpp
|
||||||
../include/HECL/Backend/GLSL.hpp
|
../include/HECL/Backend/GLSL.hpp
|
||||||
../include/HECL/Backend/HLSL.hpp
|
../include/HECL/Backend/HLSL.hpp
|
||||||
|
../include/HECL/Backend/Metal.hpp
|
||||||
../include/HECL/Frontend.hpp
|
../include/HECL/Frontend.hpp
|
||||||
../include/HECL/Database.hpp
|
../include/HECL/Database.hpp
|
||||||
../include/HECL/Runtime.hpp
|
../include/HECL/Runtime.hpp
|
||||||
|
|
|
@ -6,13 +6,20 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
#include "HECL/Backend/GLSL.hpp"
|
#include "HECL/Backend/GLSL.hpp"
|
||||||
|
#if __APPLE__
|
||||||
|
#include "HECL/Backend/Metal.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace HECL
|
namespace HECL
|
||||||
{
|
{
|
||||||
namespace Runtime
|
namespace Runtime
|
||||||
{
|
{
|
||||||
IShaderBackendFactory* _NewGLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory);
|
IShaderBackendFactory* _NewGLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory);
|
||||||
|
#if _WIN32
|
||||||
IShaderBackendFactory* _NewHLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory);
|
IShaderBackendFactory* _NewHLSLBackendFactory(boo::IGraphicsDataFactory* gfxFactory);
|
||||||
|
#elif __APPLE__
|
||||||
|
IShaderBackendFactory* _NewMetalBackendFactory(boo::IGraphicsDataFactory* gfxFactory);
|
||||||
|
#endif
|
||||||
|
|
||||||
static LogVisor::LogModule Log("ShaderCacheManager");
|
static LogVisor::LogModule Log("ShaderCacheManager");
|
||||||
static uint64_t IDX_MAGIC = SBig(uint64_t(0xDEADFEEDC001D00D));
|
static uint64_t IDX_MAGIC = SBig(uint64_t(0xDEADFEEDC001D00D));
|
||||||
|
@ -110,10 +117,16 @@ ShaderCacheManager::ShaderCacheManager(const FileStoreManager& storeMgr,
|
||||||
case boo::IGraphicsDataFactory::PlatformOGL:
|
case boo::IGraphicsDataFactory::PlatformOGL:
|
||||||
m_factory.reset(_NewGLSLBackendFactory(gfxFactory));
|
m_factory.reset(_NewGLSLBackendFactory(gfxFactory));
|
||||||
break;
|
break;
|
||||||
|
#if _WIN32
|
||||||
case boo::IGraphicsDataFactory::PlatformD3D11:
|
case boo::IGraphicsDataFactory::PlatformD3D11:
|
||||||
case boo::IGraphicsDataFactory::PlatformD3D12:
|
case boo::IGraphicsDataFactory::PlatformD3D12:
|
||||||
m_factory.reset(_NewHLSLBackendFactory(gfxFactory));
|
m_factory.reset(_NewHLSLBackendFactory(gfxFactory));
|
||||||
break;
|
break;
|
||||||
|
#elif __APPLE__ && HECL_HAS_METAL
|
||||||
|
case boo::IGraphicsDataFactory::PlatformMetal:
|
||||||
|
m_factory.reset(_NewMetalBackendFactory(gfxFactory));
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
default:
|
default:
|
||||||
Log.report(LogVisor::FatalError, _S("unsupported backend %s"), gfxFactory->platformName());
|
Log.report(LogVisor::FatalError, _S("unsupported backend %s"), gfxFactory->platformName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,6 +80,7 @@ struct HECLApplicationCallback : boo::IApplicationCallback
|
||||||
/* HECL managers */
|
/* HECL managers */
|
||||||
HECL::Runtime::FileStoreManager fileMgr(app->getUniqueName());
|
HECL::Runtime::FileStoreManager fileMgr(app->getUniqueName());
|
||||||
HECL::Runtime::ShaderCacheManager shaderMgr(fileMgr, gfxF);
|
HECL::Runtime::ShaderCacheManager shaderMgr(fileMgr, gfxF);
|
||||||
|
shaderMgr.setRenderTargetHint(renderTex);
|
||||||
|
|
||||||
/* Compile HECL shader */
|
/* Compile HECL shader */
|
||||||
static std::string testShader = "HECLOpaque(Texture(0, UV(0)))";
|
static std::string testShader = "HECLOpaque(Texture(0, UV(0)))";
|
||||||
|
|
Loading…
Reference in New Issue