|
| 1 | +using ShaderPlayground.Core.Util; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace ShaderPlayground.Core.Compilers.Tint |
| 7 | +{ |
| 8 | + internal sealed class TintCompiler : IShaderCompiler |
| 9 | + { |
| 10 | + public string Name { get; } = CompilerNames.Tint; |
| 11 | + public string DisplayName { get; } = "tint"; |
| 12 | + public string Url { get; } = "https://dawn.googlesource.com/tint/"; |
| 13 | + public string Description { get; } = "WebGPU Shader Language (WGSL) Compiler"; |
| 14 | + |
| 15 | + public string[] InputLanguages { get; } = |
| 16 | + { |
| 17 | + LanguageNames.SpirV, |
| 18 | + LanguageNames.SpirvAssembly, |
| 19 | + LanguageNames.Wgsl, |
| 20 | + }; |
| 21 | + |
| 22 | + private static readonly string ShaderStageName = "ShaderStage"; |
| 23 | + private static readonly string EntryPointName = "EntryPoint"; |
| 24 | + private static readonly string AllShaderStages = "<all>"; |
| 25 | + private static readonly string EntryPointDescription = $"Entry point name.\nOnly used when 'Shader stage' is not '{AllShaderStages}'"; |
| 26 | + |
| 27 | + public ShaderCompilerParameter[] Parameters { get; } = |
| 28 | + { |
| 29 | + CommonParameters.CreateVersionParameter("tint"), |
| 30 | + new ShaderCompilerParameter(ShaderStageName, "Shader stage", ShaderCompilerParameterType.ComboBox, ShaderStageOptions, defaultValue: AllShaderStages), |
| 31 | + new ShaderCompilerParameter(EntryPointName, "Entry Point", ShaderCompilerParameterType.TextBox, defaultValue: "main", description: EntryPointDescription), |
| 32 | + CommonParameters.CreateOutputParameter(new[] { |
| 33 | + LanguageNames.SpirV, |
| 34 | + LanguageNames.SpirvAssembly, |
| 35 | + LanguageNames.Wgsl, |
| 36 | + LanguageNames.Metal, |
| 37 | + LanguageNames.Hlsl |
| 38 | + }) |
| 39 | + }; |
| 40 | + |
| 41 | + private static readonly string[] ShaderStageOptions = |
| 42 | + { |
| 43 | + AllShaderStages, |
| 44 | + "compute", |
| 45 | + "vertex", |
| 46 | + "fragment", |
| 47 | + }; |
| 48 | + |
| 49 | + public ShaderCompilerResult Compile(ShaderCode shaderCode, ShaderCompilerArguments arguments) |
| 50 | + { |
| 51 | + var stage = arguments.GetString(ShaderStageName); |
| 52 | + var entryPoint = arguments.GetString(EntryPointName); |
| 53 | + var outputLanguage = arguments.GetString(CommonParameters.OutputLanguageParameterName); |
| 54 | + var exePath = CommonParameters.GetBinaryPath("tint", arguments, "tint.exe"); |
| 55 | + |
| 56 | + if (!RunTint(exePath, shaderCode, stage, entryPoint, outputLanguage, out var output, out var error)) |
| 57 | + { |
| 58 | + return ErrorResult(outputLanguage, error); |
| 59 | + } |
| 60 | + |
| 61 | + if (outputLanguage == LanguageNames.SpirV) |
| 62 | + { |
| 63 | + // SPIR-V is currently the only binary format that requires disassembling. |
| 64 | + if (!RunTint(exePath, shaderCode, stage, entryPoint, LanguageNames.SpirvAssembly, out var asm, out error)) |
| 65 | + { |
| 66 | + return ErrorResult(outputLanguage, error); |
| 67 | + } |
| 68 | + return new ShaderCompilerResult( |
| 69 | + true, |
| 70 | + new ShaderCode(outputLanguage, output), |
| 71 | + 0, |
| 72 | + new ShaderCompilerOutput("Disassembly", outputLanguage, Encoding.ASCII.GetString(asm)), |
| 73 | + new ShaderCompilerOutput("Build output", null, "")); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + var disassembly = Encoding.ASCII.GetString(output); |
| 78 | + return new ShaderCompilerResult( |
| 79 | + true, |
| 80 | + new ShaderCode(outputLanguage, disassembly), |
| 81 | + 0, |
| 82 | + new ShaderCompilerOutput("Disassembly", outputLanguage, disassembly), |
| 83 | + new ShaderCompilerOutput("Build output", null, "")); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private static ShaderCompilerResult ErrorResult(string outputLanguage, string error) |
| 89 | + { |
| 90 | + return new ShaderCompilerResult( |
| 91 | + false, |
| 92 | + new ShaderCode(outputLanguage, (byte[])null), |
| 93 | + 1, |
| 94 | + new ShaderCompilerOutput("Disassembly", outputLanguage, null), |
| 95 | + new ShaderCompilerOutput("Build output", null, error)); |
| 96 | + } |
| 97 | + |
| 98 | + private static bool RunTint(string exePath, ShaderCode code, string stage, string entryPoint, string outputLanguage, |
| 99 | + out byte[] output, out string error) |
| 100 | + { |
| 101 | + var args = new List<string>(); |
| 102 | + switch (outputLanguage) |
| 103 | + { |
| 104 | + case LanguageNames.SpirV: |
| 105 | + args.Add("--format spirv"); |
| 106 | + break; |
| 107 | + case LanguageNames.SpirvAssembly: |
| 108 | + args.Add("--format spvasm"); |
| 109 | + break; |
| 110 | + case LanguageNames.Wgsl: |
| 111 | + args.Add("--format wgsl"); |
| 112 | + break; |
| 113 | + case LanguageNames.Metal: |
| 114 | + args.Add("--format msl"); |
| 115 | + break; |
| 116 | + case LanguageNames.Hlsl: |
| 117 | + args.Add("--format hlsl"); |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + if (stage != AllShaderStages) |
| 122 | + { |
| 123 | + args.Add($"-ep {stage} {entryPoint}"); |
| 124 | + } |
| 125 | + |
| 126 | + using (var inputFile = TempFile.FromShaderCode(code)) |
| 127 | + { |
| 128 | + var outputFile = $"{inputFile.FilePath}.o"; |
| 129 | + |
| 130 | + args.Add($"-o {outputFile}"); |
| 131 | + args.Add(inputFile); |
| 132 | + |
| 133 | + ProcessHelper.Run( |
| 134 | + exePath, |
| 135 | + String.Join(" ", args), |
| 136 | + out var stdOutput, |
| 137 | + out var stdError); |
| 138 | + |
| 139 | + output = FileHelper.ReadAllBytesIfExists(outputFile); |
| 140 | + FileHelper.DeleteIfExists(outputFile); |
| 141 | + error = stdError; |
| 142 | + if (output == null && error == "") |
| 143 | + { |
| 144 | + error = (stdOutput != "") ? stdOutput : "<no output>"; |
| 145 | + } |
| 146 | + return error == ""; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments