From 4a7433bd433481be694339a91573e7cf2db05792 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 27 Nov 2015 18:05:27 -1000 Subject: [PATCH] Added Metal shader support --- specter/include/Specter/View.hpp | 5 ++ specter/lib/TextView.cpp | 80 ++++++++++++++++++++++++++++++++ specter/lib/View.cpp | 55 ++++++++++++++++++++++ 3 files changed, 140 insertions(+) diff --git a/specter/include/Specter/View.hpp b/specter/include/Specter/View.hpp index b61086504..d0bede7b7 100644 --- a/specter/include/Specter/View.hpp +++ b/specter/include/Specter/View.hpp @@ -47,6 +47,11 @@ protected: "{\n"\ " float4x4 mv;\n"\ "};\n" +#define SPECTER_VIEW_VERT_BLOCK_METAL\ + "struct SpecterViewBlock\n"\ + "{\n"\ + " float4x4 mv;\n"\ + "};\n" boo::IGraphicsBufferD* m_viewVertBlockBuf; public: diff --git a/specter/lib/TextView.cpp b/specter/lib/TextView.cpp index bd4f33c4f..251656369 100644 --- a/specter/lib/TextView.cpp +++ b/specter/lib/TextView.cpp @@ -79,6 +79,8 @@ void TextView::System::init(boo::GLDataFactory* factory, FontCache* fcache) false, false, false); } +#if _WIN32 + void TextView::System::init(boo::ID3DDataFactory* factory, FontCache* fcache) { m_fcache = fcache; @@ -178,6 +180,84 @@ void TextView::System::init(boo::ID3DDataFactory* factory, FontCache* fcache) boo::BlendFactor::SrcColor1, boo::BlendFactor::InvSrcColor1, false, false, false); } + +#elif BOO_HAS_METAL + +void TextView::System::init(boo::MetalDataFactory* factory, FontCache* fcache) +{ + m_fcache = fcache; + + static const char* VS = + "#include \n" + "using namespace metal;\n" + "struct VertData\n" + "{\n" + " float3 posIn[4];\n" + " float4x4 mvMtx;\n" + " float3 uvIn[4];\n" + " float4 colorIn;\n" + "};\n" + SPECTER_VIEW_VERT_BLOCK_METAL + "struct VertToFrag\n" + "{\n" + " float4 position [[ position ]];\n" + " float3 uv;\n" + " float4 color;\n" + "};\n" + "vertex VertToFrag vmain(constant VertData* va [[ buffer(1) ]],\n" + " uint vertId [[ vertex_id ]], uint instId [[ instance_id ]],\n" + " constant SpecterViewBlock& view [[ buffer(2) ]])\n" + "{\n" + " VertToFrag vtf;\n" + " constant VertData& v = va[instId];\n" + " vtf.uv = v.uvIn[vertId];\n" + " vtf.color = v.colorIn;\n" + " vtf.position = view.mv * v.mvMtx * float4(v.posIn[vertId], 1.0);\n" + " return vtf;\n" + "}\n"; + + static const char* FSReg = + "#include \n" + "using namespace metal;\n" + "constexpr sampler samp(address::repeat);\n" + "struct VertToFrag\n" + "{\n" + " float4 position [[ position ]];\n" + " float3 uv;\n" + " float4 color;\n" + "};\n" + "fragment float4 fmain(VertToFrag vtf [[ stage_in ]], texture2d_array fontTex [[ texture(0) ]])\n" + "{\n" + " float4 colorOut = vtf.color;\n" + " colorOut.a = fontTex.sample(samp, vtf.uv.xy, vtf.uv.z).r;\n" + " return colorOut;\n" + "}\n"; + + boo::VertexElementDescriptor vdescs[] = + { + {nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0}, + {nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1}, + {nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2}, + {nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3}, + {nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 0}, + {nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 1}, + {nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 2}, + {nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 3}, + {nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0}, + {nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1}, + {nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2}, + {nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3}, + {nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced} + }; + m_vtxFmt = factory->newVertexFormat(13, vdescs); + + m_regular = + factory->newShaderPipeline(VS, FSReg, m_vtxFmt, 1, + boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha, + false, false, false); +} + +#endif TextView::TextView(ViewSystem& system, FontTag font, size_t capacity) : View(system), diff --git a/specter/lib/View.cpp b/specter/lib/View.cpp index 075c4a1a6..5dcdd4de7 100644 --- a/specter/lib/View.cpp +++ b/specter/lib/View.cpp @@ -41,6 +41,8 @@ void View::System::init(boo::GLDataFactory* factory) boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha, false, false, false); } + +#if _WIN32 void View::System::init(boo::ID3DDataFactory* factory) { @@ -89,6 +91,59 @@ void View::System::init(boo::ID3DDataFactory* factory) boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha, false, false, false); } + +#elif BOO_HAS_METAL + +void View::System::init(boo::MetalDataFactory* factory) +{ + static const char* VS = + "#include \n" + "using namespace metal;\n" + "struct VertData\n" + "{\n" + " float3 posIn [[ attribute(0) ]];\n" + " float4 colorIn [[ attribute(1) ]];\n" + "};\n" + SPECTER_VIEW_VERT_BLOCK_METAL + "struct VertToFrag\n" + "{\n" + " float4 position [[ position ]];\n" + " float4 color;\n" + "};\n" + "vertex VertToFrag vmain(VertData v [[ stage_in ]], constant SpecterViewBlock& view [[ buffer(2) ]])\n" + "{\n" + " VertToFrag vtf;\n" + " vtf.color = v.colorIn;\n" + " vtf.position = view.mv * float4(v.posIn, 1.0);\n" + " return vtf;\n" + "}\n"; + + static const char* FS = + "#include \n" + "using namespace metal;\n" + "struct VertToFrag\n" + "{\n" + " float4 position [[ position ]];\n" + " float4 color;\n" + "};\n" + "fragment float4 fmain(VertToFrag vtf [[ stage_in ]])\n" + "{\n" + " return vtf.color;\n" + "}\n"; + + boo::VertexElementDescriptor vdescs[] = + { + {nullptr, nullptr, boo::VertexSemantic::Position4}, + {nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced} + }; + m_vtxFmt = factory->newVertexFormat(2, vdescs); + + m_solidShader = factory->newShaderPipeline(VS, FS, m_vtxFmt, 1, + boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha, + false, false, false); +} + +#endif View::View(ViewSystem& system) {