|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Veldrid.SPIRV |
| 7 | +{ |
| 8 | + public class VariantStageDescription |
| 9 | + { |
| 10 | + public ShaderStages Stage { get; } |
| 11 | + public string FileName { get; } |
| 12 | + |
| 13 | + public VariantStageDescription(ShaderStages stage, string fileName) |
| 14 | + { |
| 15 | + Stage = stage; |
| 16 | + FileName = fileName; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public class ShaderVariantDescription |
| 21 | + { |
| 22 | + public string Name { get; } |
| 23 | + public VariantStageDescription[] Shaders { get; } |
| 24 | + public MacroDefinition[] Macros { get; } |
| 25 | + public CrossCompileOptions CrossCompileOptions { get; } |
| 26 | + public CrossCompileTarget[] Targets { get; } |
| 27 | + |
| 28 | + public ShaderVariantDescription( |
| 29 | + string name, |
| 30 | + VariantStageDescription[] shaders, |
| 31 | + MacroDefinition[] macros, |
| 32 | + CrossCompileOptions crossCompileOptions, |
| 33 | + CrossCompileTarget[] targets) |
| 34 | + { |
| 35 | + Name = name; |
| 36 | + Shaders = shaders; |
| 37 | + Macros = macros; |
| 38 | + CrossCompileOptions = crossCompileOptions; |
| 39 | + Targets = targets; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public class VariantCompiler |
| 44 | + { |
| 45 | + private readonly List<string> _shaderSearchPaths = new List<string>(); |
| 46 | + private readonly string _outputPath; |
| 47 | + |
| 48 | + public VariantCompiler(List<string> shaderSearchPaths, string outputPath) |
| 49 | + { |
| 50 | + _shaderSearchPaths = shaderSearchPaths; |
| 51 | + _outputPath = outputPath; |
| 52 | + } |
| 53 | + |
| 54 | + public void Compile(ShaderVariantDescription variant) |
| 55 | + { |
| 56 | + if (variant.Shaders.Length == 1) |
| 57 | + { |
| 58 | + if (variant.Shaders[0].Stage == ShaderStages.Vertex) { CompileVertexFragment(variant); } |
| 59 | + if (variant.Shaders[0].Stage == ShaderStages.Compute) { CompileCompute(variant); } |
| 60 | + else |
| 61 | + { |
| 62 | + throw new SpirvCompilationException( |
| 63 | + $"Variant \"{variant.Name}\" has an unsupported set of shader stages."); |
| 64 | + } |
| 65 | + } |
| 66 | + if (variant.Shaders.Length == 2) |
| 67 | + { |
| 68 | + bool hasVertex = false; |
| 69 | + bool hasFragment = false; |
| 70 | + foreach (var shader in variant.Shaders) |
| 71 | + { |
| 72 | + hasVertex |= shader.Stage == ShaderStages.Vertex; |
| 73 | + hasFragment |= shader.Stage == ShaderStages.Fragment; |
| 74 | + } |
| 75 | + |
| 76 | + if (!hasVertex) |
| 77 | + { |
| 78 | + throw new SpirvCompilationException($"Variant \"{variant.Name}\" is missing a vertex shader."); |
| 79 | + } |
| 80 | + if (!hasFragment) |
| 81 | + { |
| 82 | + throw new SpirvCompilationException($"Variant \"{variant.Name}\" is missing a fragment shader."); |
| 83 | + } |
| 84 | + |
| 85 | + CompileVertexFragment(variant); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void CompileVertexFragment(ShaderVariantDescription variant) |
| 90 | + { |
| 91 | + List<Exception> compilationExceptions = new List<Exception>(); |
| 92 | + byte[] vsBytes = null; |
| 93 | + byte[] fsBytes = null; |
| 94 | + |
| 95 | + string vertexFileName = variant.Shaders.FirstOrDefault(vsd => vsd.Stage == ShaderStages.Vertex)?.FileName; |
| 96 | + if (vertexFileName != null) |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + vsBytes = CompileToSpirv(variant, vertexFileName, ShaderStages.Vertex); |
| 101 | + } |
| 102 | + catch (Exception e) |
| 103 | + { |
| 104 | + compilationExceptions.Add(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + string fragmentFileName = variant.Shaders.FirstOrDefault(vsd => vsd.Stage == ShaderStages.Fragment)?.FileName; |
| 109 | + if (fragmentFileName != null) |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + fsBytes = CompileToSpirv(variant, fragmentFileName, ShaderStages.Fragment); |
| 114 | + } |
| 115 | + catch (Exception e) |
| 116 | + { |
| 117 | + compilationExceptions.Add(e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (compilationExceptions.Count > 0) |
| 122 | + { |
| 123 | + throw new AggregateException( |
| 124 | + $"Errors were encountered when compiling from GLSL to SPIR-V.", |
| 125 | + compilationExceptions); |
| 126 | + } |
| 127 | + |
| 128 | + foreach (CrossCompileTarget target in variant.Targets) |
| 129 | + { |
| 130 | + try |
| 131 | + { |
| 132 | + VertexFragmentCompilationResult result = SpirvCompilation.CompileVertexFragment( |
| 133 | + vsBytes, |
| 134 | + fsBytes, |
| 135 | + target, |
| 136 | + variant.CrossCompileOptions); |
| 137 | + if (result.VertexShader != null) |
| 138 | + { |
| 139 | + string vsPath = Path.Combine(_outputPath, $"{variant.Name}_Vertex.{GetExtension(target)}"); |
| 140 | + File.WriteAllText(vsPath, result.VertexShader); |
| 141 | + } |
| 142 | + if (result.FragmentShader != null) |
| 143 | + { |
| 144 | + string fsPath = Path.Combine(_outputPath, $"{variant.Name}_Fragment.{GetExtension(target)}"); |
| 145 | + File.WriteAllText(fsPath, result.FragmentShader); |
| 146 | + } |
| 147 | + } |
| 148 | + catch (Exception e) |
| 149 | + { |
| 150 | + compilationExceptions.Add(e); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if (compilationExceptions.Count > 0) |
| 155 | + { |
| 156 | + throw new AggregateException($"Errors were encountered when compiling shader variant(s).", compilationExceptions); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private string GetExtension(CrossCompileTarget target) |
| 161 | + { |
| 162 | + switch (target) |
| 163 | + { |
| 164 | + case CrossCompileTarget.HLSL: |
| 165 | + return "hlsl"; |
| 166 | + case CrossCompileTarget.GLSL: |
| 167 | + return "glsl"; |
| 168 | + case CrossCompileTarget.ESSL: |
| 169 | + return "essl"; |
| 170 | + case CrossCompileTarget.MSL: |
| 171 | + return "metal"; |
| 172 | + default: |
| 173 | + throw new SpirvCompilationException($"Invalid CrossCompileTarget: {target}"); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private byte[] CompileToSpirv( |
| 178 | + ShaderVariantDescription variant, |
| 179 | + string fileName, |
| 180 | + ShaderStages stage) |
| 181 | + { |
| 182 | + GlslCompileOptions glslOptions = GetOptions(variant); |
| 183 | + string glsl = LoadGlsl(fileName); |
| 184 | + SpirvCompilationResult result = SpirvCompilation.CompileGlslToSpirv( |
| 185 | + glsl, |
| 186 | + fileName, |
| 187 | + stage, |
| 188 | + glslOptions); |
| 189 | + string spvPath = Path.Combine(_outputPath, $"{variant.Name}_{stage.ToString()}.spv"); |
| 190 | + File.WriteAllBytes(spvPath, result.SpirvBytes); |
| 191 | + return result.SpirvBytes; |
| 192 | + } |
| 193 | + |
| 194 | + private GlslCompileOptions GetOptions(ShaderVariantDescription variant) |
| 195 | + { |
| 196 | + return new GlslCompileOptions(true, variant.Macros); |
| 197 | + } |
| 198 | + |
| 199 | + private string LoadGlsl(string fileName) |
| 200 | + { |
| 201 | + if (fileName == null) { return null; } |
| 202 | + |
| 203 | + foreach (string searchPath in _shaderSearchPaths) |
| 204 | + { |
| 205 | + string fullPath = Path.Combine(searchPath, fileName); |
| 206 | + if (File.Exists(fullPath)) |
| 207 | + { |
| 208 | + return File.ReadAllText(fullPath); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + throw new FileNotFoundException($"Unable to find shader file \"{fileName}\"."); |
| 213 | + } |
| 214 | + |
| 215 | + private void CompileCompute(ShaderVariantDescription variant) |
| 216 | + { |
| 217 | + byte[] csBytes = CompileToSpirv(variant, variant.Shaders[0].FileName, ShaderStages.Compute); |
| 218 | + |
| 219 | + List<Exception> compilationExceptions = new List<Exception>(); |
| 220 | + foreach (CrossCompileTarget target in variant.Targets) |
| 221 | + { |
| 222 | + try |
| 223 | + { |
| 224 | + ComputeCompilationResult result = SpirvCompilation.CompileCompute(csBytes, target, variant.CrossCompileOptions); |
| 225 | + string csPath = Path.Combine(_outputPath, $"{variant.Name}_Compute.{GetExtension(target)}"); |
| 226 | + File.WriteAllText(csPath, result.ComputeShader); |
| 227 | + } |
| 228 | + catch (Exception e) |
| 229 | + { |
| 230 | + compilationExceptions.Add(e); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if (compilationExceptions.Count > 0) |
| 235 | + { |
| 236 | + throw new AggregateException($"Errors were encountered when compiling shader variant(s).", compilationExceptions); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments