From 73b4941aafb221ae45d6a1817ee09a97d6323991 Mon Sep 17 00:00:00 2001 From: Minmin Gong Date: Mon, 22 Apr 2019 22:13:48 -0700 Subject: [PATCH] Separate Msl target into Msl_macOS and Msl_iOS, fix the resource bindings Related work item: Github #44 --- Include/ShaderConductor/ShaderConductor.hpp | 3 ++- Source/Core/ShaderConductor.cpp | 30 ++++++++++++++++++--- Source/Tests/ShaderConductorTest.cpp | 16 +++++------ Source/Tools/ShaderConductorCmd.cpp | 12 ++++++--- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Include/ShaderConductor/ShaderConductor.hpp b/Include/ShaderConductor/ShaderConductor.hpp index 86c5a23..b098e1f 100644 --- a/Include/ShaderConductor/ShaderConductor.hpp +++ b/Include/ShaderConductor/ShaderConductor.hpp @@ -74,7 +74,8 @@ namespace ShaderConductor Hlsl, Glsl, Essl, - Msl, + Msl_macOS, + Msl_iOS, NumShadingLanguages, }; diff --git a/Source/Core/ShaderConductor.cpp b/Source/Core/ShaderConductor.cpp index 0b8b88d..f531f6c 100644 --- a/Source/Core/ShaderConductor.cpp +++ b/Source/Core/ShaderConductor.cpp @@ -441,7 +441,8 @@ namespace case ShadingLanguage::Hlsl: case ShadingLanguage::Glsl: case ShadingLanguage::Essl: - case ShadingLanguage::Msl: + case ShadingLanguage::Msl_macOS: + case ShadingLanguage::Msl_iOS: dxcArgStrings.push_back(L"-spirv"); break; @@ -558,7 +559,8 @@ namespace buildDummySampler = true; break; - case ShadingLanguage::Msl: + case ShadingLanguage::Msl_macOS: + case ShadingLanguage::Msl_iOS: if (source.stage == ShaderStage::GeometryShader) { AppendError(ret, "MSL doesn't have GS."); @@ -642,7 +644,7 @@ namespace hlslCompiler->set_hlsl_options(hlslOpts); } - else if (target.language == ShadingLanguage::Msl) + else if ((target.language == ShadingLanguage::Msl_macOS) || (target.language == ShadingLanguage::Msl_iOS)) { auto* mslCompiler = static_cast(compiler.get()); auto mslOpts = mslCompiler->get_msl_options(); @@ -651,7 +653,26 @@ namespace mslOpts.msl_version = opts.version; } mslOpts.swizzle_texture_samples = false; + mslOpts.platform = (target.language == ShadingLanguage::Msl_iOS) ? spirv_cross::CompilerMSL::Options::iOS + : spirv_cross::CompilerMSL::Options::macOS; + mslCompiler->set_msl_options(mslOpts); + + const auto& resources = mslCompiler->get_shader_resources(); + + uint32_t textureBinding = 0; + for (const auto& image : resources.separate_images) + { + mslCompiler->set_decoration(image.id, spv::DecorationBinding, textureBinding); + ++textureBinding; + } + + uint32_t samplerBinding = 0; + for (const auto& sampler : resources.separate_samplers) + { + mslCompiler->set_decoration(sampler.id, spv::DecorationBinding, samplerBinding); + ++samplerBinding; + } } if (buildDummySampler) @@ -787,7 +808,8 @@ namespace ShaderConductor case ShadingLanguage::Hlsl: case ShadingLanguage::Glsl: case ShadingLanguage::Essl: - case ShadingLanguage::Msl: + case ShadingLanguage::Msl_macOS: + case ShadingLanguage::Msl_iOS: results[i] = ConvertBinary(binaryResult, sourceOverride, targets[i]); break; diff --git a/Source/Tests/ShaderConductorTest.cpp b/Source/Tests/ShaderConductorTest.cpp index 0528fe3..0275632 100644 --- a/Source/Tests/ShaderConductorTest.cpp +++ b/Source/Tests/ShaderConductorTest.cpp @@ -80,7 +80,7 @@ namespace void HlslToAnyTest(const std::string& name, const Compiler::SourceDesc& source, const Compiler::Options& options, const std::vector& targets, const std::vector& expectSuccessFlags) { - static const std::string extMap[] = { "dxil", "spv", "hlsl", "glsl", "essl", "msl" }; + static const std::string extMap[] = { "dxil", "spv", "hlsl", "glsl", "essl", "msl", "msl" }; static_assert(sizeof(extMap) / sizeof(extMap[0]) == static_cast(ShadingLanguage::NumShadingLanguages), "extMap doesn't match with the number of shading languages."); @@ -176,7 +176,7 @@ namespace { ShadingLanguage::Essl, "300" }, { ShadingLanguage::Essl, "310" }, - { ShadingLanguage::Msl }, + { ShadingLanguage::Msl_macOS }, }; // clang-format on @@ -385,7 +385,7 @@ namespace TEST_F(VertexShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } @@ -406,7 +406,7 @@ namespace TEST_F(PixelShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } @@ -427,7 +427,7 @@ namespace TEST_F(GeometryShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } @@ -448,7 +448,7 @@ namespace TEST_F(HullShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } @@ -469,7 +469,7 @@ namespace TEST_F(DomainShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } @@ -490,7 +490,7 @@ namespace TEST_F(ComputeShaderTest, ToMsl) { - RunTests(ShadingLanguage::Msl); + RunTests(ShadingLanguage::Msl_macOS); } TEST(IncludeTest, IncludeExist) diff --git a/Source/Tools/ShaderConductorCmd.cpp b/Source/Tools/ShaderConductorCmd.cpp index 3ebe906..d18c09c 100644 --- a/Source/Tools/ShaderConductorCmd.cpp +++ b/Source/Tools/ShaderConductorCmd.cpp @@ -47,7 +47,7 @@ int main(int argc, char** argv) ("E,entry", "Entry point of the shader", cxxopts::value()->default_value("main")) ("I,input", "Input file name", cxxopts::value())("O,output", "Output file name", cxxopts::value()) ("S,stage", "Shader stage: vs, ps, gs, hs, ds, cs", cxxopts::value()) - ("T,target", "Target shading language: dxil, spirv, hlsl, glsl, essl, msl", cxxopts::value()->default_value("dxil")) + ("T,target", "Target shading language: dxil, spirv, hlsl, glsl, essl, msl_macos, msl_ios", cxxopts::value()->default_value("dxil")) ("V,version", "The version of target shading language", cxxopts::value()->default_value("")); // clang-format on @@ -126,9 +126,13 @@ int main(int argc, char** argv) { targetDesc.language = ShadingLanguage::Essl; } - else if (targetName == "msl") + else if (targetName == "msl_macos") { - targetDesc.language = ShadingLanguage::Msl; + targetDesc.language = ShadingLanguage::Msl_macOS; + } + else if (targetName == "msl_ios") + { + targetDesc.language = ShadingLanguage::Msl_iOS; } else { @@ -139,7 +143,7 @@ int main(int argc, char** argv) std::string outputName; if (opts.count("output") == 0) { - static const std::string extMap[] = { "dxil", "spv", "hlsl", "glsl", "essl", "msl" }; + static const std::string extMap[] = { "dxil", "spv", "hlsl", "glsl", "essl", "msl", "msl" }; static_assert(sizeof(extMap) / sizeof(extMap[0]) == static_cast(ShadingLanguage::NumShadingLanguages), "extMap doesn't match with the number of shading languages."); outputName = fileName + "." + extMap[static_cast(targetDesc.language)];