Пример #1
0
        public void ResourcesUsedInStages()
        {
            Compilation     compilation = TestUtil.GetCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(
                compilation, backend, "TestShaders.UsedResourcesShaders.VS", "TestShaders.UsedResourcesShaders.FS");

            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            GeneratedShaderSet set         = sets[0];
            ShaderModel        shaderModel = set.Model;

            Assert.Equal(4, shaderModel.VertexResources.Length);
            Assert.Contains(shaderModel.VertexResources, rd => rd.Name == "VS_M0");
            Assert.Contains(shaderModel.VertexResources, rd => rd.Name == "VS_M1");
            Assert.Contains(shaderModel.VertexResources, rd => rd.Name == "VS_T0");
            Assert.Contains(shaderModel.VertexResources, rd => rd.Name == "VS_S0");

            Assert.Equal(5, shaderModel.FragmentResources.Length);
            Assert.Contains(shaderModel.FragmentResources, rd => rd.Name == "FS_M0");
            Assert.Contains(shaderModel.FragmentResources, rd => rd.Name == "FS_M1");
            Assert.Contains(shaderModel.FragmentResources, rd => rd.Name == "FS_T0");
            Assert.Contains(shaderModel.FragmentResources, rd => rd.Name == "FS_S0");
            Assert.Contains(shaderModel.FragmentResources, rd => rd.Name == "FS_M2_Indirect");
        }
Пример #2
0
        public void TestVertexShader_VertexSemantics()
        {
            string          functionName = "TestShaders.TestVertexShader.VS";
            Compilation     compilation  = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend      = new HlslBackend(compilation);
            ShaderGenerator sg           = new ShaderGenerator(
                compilation,
                functionName,
                null,
                backend);
            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            ShaderModel shaderModel = sets[0].Model;

            StructureDefinition vsInput = shaderModel.GetStructureDefinition(nameof(TestShaders) + "." + nameof(PositionTexture));

            Assert.Equal(SemanticType.Position, vsInput.Fields[0].SemanticType);
            Assert.Equal(SemanticType.TextureCoordinate, vsInput.Fields[1].SemanticType);

            StructureDefinition fsInput = shaderModel.GetStructureDefinition(
                nameof(TestShaders) + "." + nameof(TestVertexShader) + "+" + nameof(TestVertexShader.VertexOutput));

            Assert.Equal(SemanticType.SystemPosition, fsInput.Fields[0].SemanticType);
            Assert.Equal(SemanticType.TextureCoordinate, fsInput.Fields[1].SemanticType);
        }
Пример #3
0
        public void TestVertexShader_ShaderModel()
        {
            string          functionName = "TestShaders.TestVertexShader.VS";
            Compilation     compilation  = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend      = new HlslBackend(compilation);
            ShaderGenerator sg           = new ShaderGenerator(
                compilation,
                functionName,
                null,
                backend);
            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            ShaderModel shaderModel = sets[0].Model;

            Assert.Equal(2, shaderModel.Structures.Length);
            Assert.Equal(3, shaderModel.AllResources.Length);
            ShaderFunction vsEntry = shaderModel.GetFunction(functionName);

            Assert.Equal("VS", vsEntry.Name);
            Assert.Single(vsEntry.Parameters);
            Assert.True(vsEntry.IsEntryPoint);
            Assert.Equal(ShaderFunctionType.VertexEntryPoint, vsEntry.Type);
        }
Пример #4
0
        public void HlslEndToEnd(string vsName, string fsName)
        {
            Compilation     compilation = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend     = new HlslBackend(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;
                FxcTool.AssertCompilesCode(vsCode, "vs_5_0", vsFunction.Name);
            }
            if (fsName != null)
            {
                ShaderFunction fsFunction = shaderModel.GetFunction(fsName);
                string         fsCode     = set.FragmentShaderCode;
                FxcTool.AssertCompilesCode(fsCode, "ps_5_0", fsFunction.Name);
            }
        }
Пример #5
0
        private void TestProcessor_UsersArgs()
        {
            Compilation     compilation = TestUtil.GetCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            TestProcessor   processor   = new TestProcessor();
            ShaderGenerator sg          = new ShaderGenerator(
                compilation,
                backend,
                "TestShaders.ProcessorTestShaders.VS", "TestShaders.ProcessorTestShaders.FS", null, processor);

            sg.GenerateShaders();
            Assert.Equal("This Sentence Should Be Printed By_Enumerating All Resources In Order", processor.Result);
        }
Пример #6
0
        public void MultipleResourceSets_CorrectlyParsed()
        {
            Compilation     compilation = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(
                compilation,
                "TestShaders.MultipleResourceSets.VS",
                null,
                backend);

            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            GeneratedShaderSet set         = sets[0];
            ShaderModel        shaderModel = set.Model;

            Assert.Equal(13, shaderModel.AllResources.Length);

            Assert.Equal(0, shaderModel.AllResources[0].Set);
            Assert.Equal(0, shaderModel.AllResources[0].Binding);
            Assert.Equal(0, shaderModel.AllResources[1].Set);
            Assert.Equal(1, shaderModel.AllResources[1].Binding);
            Assert.Equal(1, shaderModel.AllResources[2].Set);
            Assert.Equal(0, shaderModel.AllResources[2].Binding);
            Assert.Equal(2, shaderModel.AllResources[3].Set);
            Assert.Equal(0, shaderModel.AllResources[3].Binding);
            Assert.Equal(3, shaderModel.AllResources[4].Set);
            Assert.Equal(0, shaderModel.AllResources[4].Binding);
            Assert.Equal(4, shaderModel.AllResources[5].Set);
            Assert.Equal(0, shaderModel.AllResources[5].Binding);
            Assert.Equal(0, shaderModel.AllResources[6].Set);
            Assert.Equal(2, shaderModel.AllResources[6].Binding);

            Assert.Equal(0, shaderModel.AllResources[7].Set);
            Assert.Equal(3, shaderModel.AllResources[7].Binding);
            Assert.Equal(4, shaderModel.AllResources[8].Set);
            Assert.Equal(1, shaderModel.AllResources[8].Binding);
            Assert.Equal(0, shaderModel.AllResources[9].Set);
            Assert.Equal(4, shaderModel.AllResources[9].Binding);

            Assert.Equal(2, shaderModel.AllResources[10].Set);
            Assert.Equal(1, shaderModel.AllResources[10].Binding);
            Assert.Equal(0, shaderModel.AllResources[11].Set);
            Assert.Equal(5, shaderModel.AllResources[11].Binding);
            Assert.Equal(1, shaderModel.AllResources[12].Set);
            Assert.Equal(1, shaderModel.AllResources[12].Binding);
        }
Пример #7
0
        public void StructureSizes()
        {
            Compilation     compilation = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(
                compilation,
                "TestShaders.StructureSizeTests.VS",
                null,
                backend);

            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            GeneratedShaderSet set         = sets[0];
            ShaderModel        shaderModel = set.Model;

            StructureDefinition test0 = shaderModel.GetStructureDefinition(
                nameof(TestShaders) + "." + nameof(StructureSizeTests) + "+" + nameof(StructureSizeTests.SizeTest_0));

            Assert.Equal(48, test0.Alignment.CSharpSize);
            Assert.True(test0.CSharpMatchesShaderAlignment);

            StructureDefinition test1 = shaderModel.GetStructureDefinition(
                nameof(TestShaders) + "." + nameof(StructureSizeTests) + "+" + nameof(StructureSizeTests.SizeTest_1));

            Assert.Equal(52, test1.Alignment.CSharpSize);
            Assert.True(test1.CSharpMatchesShaderAlignment);

            StructureDefinition test2 = shaderModel.GetStructureDefinition(
                nameof(TestShaders) + "." + nameof(StructureSizeTests) + "+" + nameof(StructureSizeTests.SizeTest_2));

            Assert.Equal(48, test2.Alignment.CSharpSize);
            Assert.False(test2.CSharpMatchesShaderAlignment);

            StructureDefinition test3 = shaderModel.GetStructureDefinition(
                nameof(TestShaders) + "." + nameof(StructureSizeTests) + "+" + nameof(StructureSizeTests.SizeTest_3));

            Assert.Equal(64, test3.Alignment.CSharpSize);
            Assert.False(test3.CSharpMatchesShaderAlignment);

            Assert.Equal(4, shaderModel.GetTypeSize(test3.Fields[0].Type));
            Assert.Equal(12, shaderModel.GetTypeSize(test3.Fields[1].Type));
            Assert.Equal(12, shaderModel.GetTypeSize(test3.Fields[2].Type));
            Assert.Equal(16, shaderModel.GetTypeSize(test3.Fields[3].Type));
            Assert.Equal(4, shaderModel.GetTypeSize(test3.Fields[4].Type));
            Assert.Equal(16, shaderModel.GetTypeSize(test3.Fields[5].Type));
        }
Пример #8
0
        public void PointLightsInfo_CorrectSize()
        {
            Compilation     compilation = TestUtil.GetCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(compilation, backend, "TestShaders.PointLightTestShaders.VS");

            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            GeneratedShaderSet set         = sets[0];
            ShaderModel        shaderModel = set.Model;

            Assert.Single(shaderModel.AllResources);
            Assert.Equal(208, shaderModel.GetTypeSize(shaderModel.AllResources[0].ValueType));
        }
Пример #9
0
        public static void ShaderSetAutoDiscovery()
        {
            Compilation                        compilation = TestUtil.GetTestProjectCompilation();
            HlslBackend                        backend     = new HlslBackend(compilation);
            ShaderGenerator                    sg          = new ShaderGenerator(compilation, new[] { backend });
            ShaderGenerationResult             result      = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> hlslSets    = result.GetOutput(backend);

            Assert.Equal(3, hlslSets.Count);
            GeneratedShaderSet set = hlslSets[0];

            Assert.Equal("VertexAndFragment", set.Name);
            ShaderModel    shaderModel = set.Model;
            ShaderFunction func        = shaderModel.Functions[0];

            FxcTool.AssertCompilesCode(set.VertexShaderCode, "vs_5_0", "VS");
            FxcTool.AssertCompilesCode(set.FragmentShaderCode, "ps_5_0", "FS");
        }
Пример #10
0
        public void PartialFiles()
        {
            Compilation     compilation = TestUtil.GetTestProjectCompilation();
            HlslBackend     backend     = new HlslBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(
                compilation,
                "TestShaders.PartialVertex.VertexShaderFunc",
                null,
                backend);

            ShaderGenerationResult             genResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> sets      = genResult.GetOutput(backend);

            Assert.Equal(1, sets.Count);
            GeneratedShaderSet set         = sets[0];
            ShaderModel        shaderModel = set.Model;
            string             vsCode      = set.VertexShaderCode;

            FxcTool.AssertCompilesCode(vsCode, "vs_5_0", "VertexShaderFunc");
        }
Пример #11
0
        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);
        }