public void Glsl450EndToEnd(string vsName, string fsName) { Compilation compilation = TestUtil.GetTestProjectCompilation(); LanguageBackend backend = new Glsl450Backend(compilation); ShaderGenerator sg = new ShaderGenerator( compilation, vsName, fsName, backend); ShaderGenerationResult result = sg.GenerateShaders(); IReadOnlyList <GeneratedShaderSet> sets = result.GetOutput(backend); Assert.Equal(1, sets.Count); GeneratedShaderSet set = sets[0]; ShaderModel shaderModel = set.Model; if (vsName != null) { ShaderFunction vsFunction = shaderModel.GetFunction(vsName); string vsCode = set.VertexShaderCode; GlsLangValidatorTool.AssertCompilesCode(vsCode, "vert", true); } if (fsName != null) { ShaderFunction fsFunction = shaderModel.GetFunction(fsName); string fsCode = set.FragmentShaderCode; GlsLangValidatorTool.AssertCompilesCode(fsCode, "frag", true); } }
public void DummyTest() { string vsName = "TestShaders.VeldridShaders.VertexAndFragment.VS"; string fsName = "TestShaders.VeldridShaders.VertexAndFragment.FS"; Compilation compilation = TestUtil.GetTestProjectCompilation(); using (TempFile fp = new TempFile()) { Microsoft.CodeAnalysis.Emit.EmitResult emitResult = compilation.Emit(fp); Assert.True(emitResult.Success); } LanguageBackend backend = new Glsl450Backend(compilation); ShaderGenerator sg = new ShaderGenerator( compilation, vsName, fsName, backend); ShaderGenerationResult result = sg.GenerateShaders(); IReadOnlyList <GeneratedShaderSet> sets = result.GetOutput(backend); Assert.Equal(1, sets.Count); GeneratedShaderSet set = sets[0]; ShaderModel shaderModel = set.Model; if (vsName != null) { ShaderFunction vsFunction = shaderModel.GetFunction(vsName); string vsCode = set.VertexShaderCode; File.WriteAllText(@"C:\Users\raver\Documents\forward-vertex.glsl", vsCode); GlsLangValidatorTool.AssertCompilesCode(vsCode, "vert", true); } if (fsName != null) { ShaderFunction fsFunction = shaderModel.GetFunction(fsName); string fsCode = set.FragmentShaderCode; File.WriteAllText(@"C:\Users\raver\Documents\forward-frag.glsl", fsCode); GlsLangValidatorTool.AssertCompilesCode(fsCode, "frag", true); } }
public static int Main(string[] args) { string referenceItemsResponsePath = null; string compileItemsResponsePath = null; string outputPath = null; string genListFilePath = null; bool listAllFiles = false; string processorPath = null; string processorArgs = null; for (int i = 0; i < args.Length; i++) { args[i] = args[i].Replace("\\\\", "\\"); } ArgumentSyntax.Parse(args, syntax => { syntax.DefineOption("ref", ref referenceItemsResponsePath, true, "The semicolon-separated list of references to compile against."); syntax.DefineOption("src", ref compileItemsResponsePath, true, "The semicolon-separated list of source files to compile."); syntax.DefineOption("out", ref outputPath, true, "The output path for the generated shaders."); syntax.DefineOption("genlist", ref genListFilePath, true, "The output file to store the list of generated files."); syntax.DefineOption("listall", ref listAllFiles, false, "Forces all generated files to be listed in the list file. By default, only bytecode files will be listed and not the original shader code."); syntax.DefineOption("processor", ref processorPath, false, "The path of an assembly containing IShaderSetProcessor types to be used to post-process GeneratedShaderSet objects."); syntax.DefineOption("processorargs", ref processorArgs, false, "Custom information passed to IShaderSetProcessor."); }); referenceItemsResponsePath = NormalizePath(referenceItemsResponsePath); compileItemsResponsePath = NormalizePath(compileItemsResponsePath); outputPath = NormalizePath(outputPath); genListFilePath = NormalizePath(genListFilePath); processorPath = NormalizePath(processorPath); if (!File.Exists(referenceItemsResponsePath)) { Console.Error.WriteLine("Reference items response file does not exist: " + referenceItemsResponsePath); return(-1); } if (!File.Exists(compileItemsResponsePath)) { Console.Error.WriteLine("Compile items response file does not exist: " + compileItemsResponsePath); return(-1); } if (!Directory.Exists(outputPath)) { try { Directory.CreateDirectory(outputPath); } catch { Console.Error.WriteLine($"Unable to create the output directory \"{outputPath}\"."); return(-1); } } string[] referenceItems = File.ReadAllLines(referenceItemsResponsePath); string[] compileItems = File.ReadAllLines(compileItemsResponsePath); List <MetadataReference> references = new List <MetadataReference>(); foreach (string referencePath in referenceItems) { if (!File.Exists(referencePath)) { Console.Error.WriteLine("Error: reference does not exist: " + referencePath); return(1); } using (FileStream fs = File.OpenRead(referencePath)) { references.Add(MetadataReference.CreateFromStream(fs, filePath: referencePath)); } } List <SyntaxTree> syntaxTrees = new List <SyntaxTree>(); foreach (string sourcePath in compileItems) { string fullSourcePath = Path.Combine(Environment.CurrentDirectory, sourcePath); if (!File.Exists(fullSourcePath)) { Console.Error.WriteLine("Error: source file does not exist: " + fullSourcePath); return(1); } using (FileStream fs = File.OpenRead(fullSourcePath)) { SourceText text = SourceText.From(fs); syntaxTrees.Add(CSharpSyntaxTree.ParseText(text, path: fullSourcePath)); } } Compilation compilation = CSharpCompilation.Create( "ShaderGen.App.GenerateShaders", syntaxTrees, references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); HlslBackend hlsl = new HlslBackend(compilation); Glsl330Backend glsl330 = new Glsl330Backend(compilation); GlslEs300Backend glsles300 = new GlslEs300Backend(compilation); Glsl450Backend glsl450 = new Glsl450Backend(compilation); MetalBackend metal = new MetalBackend(compilation); LanguageBackend[] languages = new LanguageBackend[] { hlsl, glsl330, glsles300, glsl450, metal, }; List <IShaderSetProcessor> processors = new List <IShaderSetProcessor>(); if (processorPath != null) { try { Assembly assm = Assembly.LoadFrom(processorPath); IEnumerable <Type> processorTypes = assm.GetTypes().Where( t => t.GetInterface(nameof(ShaderGen) + "." + nameof(IShaderSetProcessor)) != null); foreach (Type type in processorTypes) { IShaderSetProcessor processor = (IShaderSetProcessor)Activator.CreateInstance(type); processor.UserArgs = processorArgs; processors.Add(processor); } } catch (ReflectionTypeLoadException rtle) { string msg = string.Join(Environment.NewLine, rtle.LoaderExceptions.Select(e => e.ToString())); Console.WriteLine("FAIL: " + msg); throw new Exception(msg); } } ShaderGenerator sg = new ShaderGenerator(compilation, languages, processors.ToArray()); ShaderGenerationResult shaderGenResult; try { shaderGenResult = sg.GenerateShaders(); } catch (Exception e) when(!Debugger.IsAttached) { StringBuilder sb = new StringBuilder(); sb.AppendLine("An error was encountered while generating shader code:"); sb.AppendLine(e.ToString()); Console.Error.WriteLine(sb.ToString()); return(-1); } Encoding outputEncoding = new UTF8Encoding(false); List <string> generatedFilePaths = new List <string>(); foreach (LanguageBackend lang in languages) { string extension = BackendExtension(lang); IReadOnlyList <GeneratedShaderSet> sets = shaderGenResult.GetOutput(lang); foreach (GeneratedShaderSet set in sets) { string name = set.Name; if (set.VertexShaderCode != null) { string vsOutName = name + "-vertex." + extension; string vsOutPath = Path.Combine(outputPath, vsOutName); File.WriteAllText(vsOutPath, set.VertexShaderCode, outputEncoding); bool succeeded = CompileCode( lang, vsOutPath, set.VertexFunction.Name, ShaderFunctionType.VertexEntryPoint, out string[] genPaths); if (succeeded) { generatedFilePaths.AddRange(genPaths); } if (!succeeded || listAllFiles) { generatedFilePaths.Add(vsOutPath); } } if (set.FragmentShaderCode != null) { string fsOutName = name + "-fragment." + extension; string fsOutPath = Path.Combine(outputPath, fsOutName); File.WriteAllText(fsOutPath, set.FragmentShaderCode, outputEncoding); bool succeeded = CompileCode( lang, fsOutPath, set.FragmentFunction.Name, ShaderFunctionType.FragmentEntryPoint, out string[] genPaths); if (succeeded) { generatedFilePaths.AddRange(genPaths); } if (!succeeded || listAllFiles) { generatedFilePaths.Add(fsOutPath); } } if (set.ComputeShaderCode != null) { string csOutName = name + "-compute." + extension; string csOutPath = Path.Combine(outputPath, csOutName); File.WriteAllText(csOutPath, set.ComputeShaderCode, outputEncoding); bool succeeded = CompileCode( lang, csOutPath, set.ComputeFunction.Name, ShaderFunctionType.ComputeEntryPoint, out string[] genPaths); if (succeeded) { generatedFilePaths.AddRange(genPaths); } if (!succeeded || listAllFiles) { generatedFilePaths.Add(csOutPath); } } } } File.WriteAllLines(genListFilePath, generatedFilePaths); return(0); }
/// <summary> /// Compilation function (must be overrided for a specific graphic library implementation). /// </summary> /// <param name="shader">Shader to compile.</param> /// <returns>Shader instance with compiled binary.</returns> protected override ShaderInstance CompileShader(ShaderInstance shader) { var outShader = new VKShaderInstance(base.CompileShader(shader)); // - Gets all shader referenced assemblies var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var mathAsm = typeof(System.Numerics.Vector4).Assembly.GetReferencedAssemblies(); assemblies = assemblies.Concat(mathAsm.Select(x => Assembly.Load(x.FullName))).ToArray(); var assemblyMeta = assemblies.Select(x => MetadataReference.CreateFromFile(x.Location)); // - Compile the shader code obtaining an IL version CSharpCompilation compilation = CSharpCompilation.Create ( "pEngine", syntaxTrees: new[] { CSharpSyntaxTree.ParseText(outShader.CSourceCode) }, references: assemblyMeta, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) ); // - Get the assembly class path string fullName = outShader.Type.FullName; // - We want to compile targetting GLSL450 compatible for Vulkan var backend = new Glsl450Backend(compilation); // - Prepare a GLSL compiler providing the C# IL binary var generator = new ShaderGenerator ( compilation, backend, outShader.HasVertexShader ? $"{fullName}.{shader.VertexFunctionName}" : null, outShader.HasFragmentShader ? $"{fullName}.{shader.FragmentFunctionName}" : null, outShader.HasComputeShader ? $"{fullName}.{shader.ComputeFunctionName}" : null ); // - Compile to the GLSL code var shaders = generator.GenerateShaders().GetOutput(backend); // - Stores all shader sources outShader.VertexSource = outShader.HasVertexShader ? shaders.Where(x => x.VertexShaderCode != null).First().VertexShaderCode : ""; outShader.FragmentSource = outShader.HasFragmentShader ? shaders.Where(x => x.FragmentShaderCode != null).First().FragmentShaderCode : ""; outShader.ComputeSource = outShader.HasComputeShader ? shaders.Where(x => x.ComputeShaderCode != null).First().ComputeShaderCode : ""; // - This class will compile the GLSL code to the SPIR-V binary ShaderCompiler compiler = new ShaderCompiler(); if (outShader.HasVertexShader) { var res = compiler.CompileToSpirV(outShader.VertexSource, ShaderCompiler.ShaderKind.VertexShader, "main"); if (res.ErrorCount > 0) { throw new InvalidProgramException(res.Errors); } outShader.VertexBinary = res.Binary; } if (outShader.HasFragmentShader) { var res = compiler.CompileToSpirV(outShader.FragmentSource, ShaderCompiler.ShaderKind.FragmentShader, "main"); if (res.ErrorCount > 0) { throw new InvalidProgramException(res.Errors); } outShader.FragmentBinary = res.Binary; } if (outShader.HasComputeShader) { var res = compiler.CompileToSpirV(outShader.ComputeSource, ShaderCompiler.ShaderKind.ComputeShader, "main"); if (res.ErrorCount > 0) { throw new InvalidProgramException(res.Errors); } outShader.ComputeBinary = res.Binary; } return(outShader); }