Пример #1
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);
            }
        }
Пример #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);
        }
        public static void ShaderSetAutoDiscovery()
        {
            ToolChain toolChain = ToolChain.Get(ToolFeatures.ToCompiled);

            if (toolChain == null)
            {
                throw new RequiredToolFeatureMissingException("No tool chain supporting compilation was found!");
            }

            Compilation                        compilation      = TestUtil.GetCompilation();
            LanguageBackend                    backend          = toolChain.CreateBackend(compilation);
            ShaderGenerator                    sg               = new ShaderGenerator(compilation, backend);
            ShaderGenerationResult             generationResult = sg.GenerateShaders();
            IReadOnlyList <GeneratedShaderSet> hlslSets         = generationResult.GetOutput(backend);

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

            Assert.Equal("VertexAndFragment", set.Name);

            CompileResult result = toolChain.Compile(set.VertexShaderCode, Stage.Vertex, "VS");

            Assert.False(result.HasError, result.ToString());

            result = toolChain.Compile(set.FragmentShaderCode, Stage.Fragment, "FS");
            Assert.False(result.HasError, result.ToString());
        }
Пример #4
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");
        }
Пример #5
0
        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);
            }
        }
Пример #6
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);
        }
        public void AllSetsAllLanguagesEndToEnd()
        {
            Compilation compilation = TestUtil.GetTestProjectCompilation();

            LanguageBackend[] backends = new LanguageBackend[]
            {
                new HlslBackend(compilation),
                new Glsl330Backend(compilation),
                new Glsl450Backend(compilation),
            };
            ShaderGenerator sg = new ShaderGenerator(compilation, backends);

            ShaderGenerationResult result = sg.GenerateShaders();

            foreach (LanguageBackend backend in backends)
            {
                IReadOnlyList <GeneratedShaderSet> sets = result.GetOutput(backend);
                foreach (GeneratedShaderSet set in sets)
                {
                    if (set.VertexShaderCode != null)
                    {
                        if (backend is HlslBackend)
                        {
                            FxcTool.AssertCompilesCode(set.VertexShaderCode, "vs_5_0", set.VertexFunction.Name);
                        }
                        else
                        {
                            bool is450 = backend is Glsl450Backend;
                            GlsLangValidatorTool.AssertCompilesCode(set.VertexShaderCode, "vert", is450);
                        }
                    }
                    if (set.FragmentFunction != null)
                    {
                        if (backend is HlslBackend)
                        {
                            FxcTool.AssertCompilesCode(set.FragmentShaderCode, "ps_5_0", set.FragmentFunction.Name);
                        }
                        else
                        {
                            bool is450 = backend is Glsl450Backend;
                            GlsLangValidatorTool.AssertCompilesCode(set.FragmentShaderCode, "frag", is450);
                        }
                    }
                    if (set.ComputeFunction != null)
                    {
                        if (backend is HlslBackend)
                        {
                            FxcTool.AssertCompilesCode(set.ComputeShaderCode, "cs_5_0", set.ComputeFunction.Name);
                        }
                        else
                        {
                            bool is450 = backend is Glsl450Backend;
                            GlsLangValidatorTool.AssertCompilesCode(set.ComputeShaderCode, "comp", is450);
                        }
                    }
                }
            }
        }
Пример #8
0
        private void TestCompile(GraphicsBackend graphicsBackend, string vsName, string fsName, string csName = null)
        {
            Compilation compilation = TestUtil.GetCompilation();
            ToolChain   toolChain   = ToolChain.Require(ToolFeatures.ToCompiled, graphicsBackend);

            LanguageBackend backend = toolChain.CreateBackend(compilation);
            ShaderGenerator sg      = new ShaderGenerator(compilation, backend, vsName, fsName, csName);

            ShaderGenerationResult generationResult = sg.GenerateShaders();

            IReadOnlyList <GeneratedShaderSet> sets = generationResult.GetOutput(backend);

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

            List <CompileResult> results = new List <CompileResult>();

            if (!string.IsNullOrWhiteSpace(vsName))
            {
                ShaderFunction vsFunction = shaderModel.GetFunction(vsName);
                string         vsCode     = set.VertexShaderCode;

                results.Add(toolChain.Compile(vsCode, Stage.Vertex, vsFunction.Name));
            }
            if (!string.IsNullOrWhiteSpace(fsName))
            {
                ShaderFunction fsFunction = shaderModel.GetFunction(fsName);
                string         fsCode     = set.FragmentShaderCode;
                results.Add(toolChain.Compile(fsCode, Stage.Fragment, fsFunction.Name));
            }
            if (!string.IsNullOrWhiteSpace(csName))
            {
                ShaderFunction csFunction = shaderModel.GetFunction(csName);
                string         csCode     = set.ComputeShaderCode;
                results.Add(toolChain.Compile(csCode, Stage.Compute, csFunction.Name));
            }

            // Collate results
            StringBuilder builder = new StringBuilder();

            foreach (CompileResult result in results)
            {
                if (result.HasError)
                {
                    builder.AppendLine(result.ToString());
                }
            }

            Assert.True(builder.Length < 1, builder.ToString());
        }
Пример #9
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);
        }
Пример #10
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));
        }
Пример #11
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));
        }
Пример #12
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");
        }
Пример #13
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");
        }
Пример #14
0
        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);
            }
        }
Пример #15
0
        public void PartialFiles()
        {
            ToolChain toolChain = ToolChain.Get(ToolFeatures.ToCompiled);

            if (toolChain == null)
            {
                throw new RequiredToolFeatureMissingException("No tool chain supporting compilation was found!");
            }

            Compilation     compilation = TestUtil.GetCompilation();
            LanguageBackend backend     = toolChain.CreateBackend(compilation);
            ShaderGenerator sg          = new ShaderGenerator(compilation, backend, "TestShaders.PartialVertex.VertexShaderFunc");

            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;
            CompileResult      result      = toolChain.Compile(vsCode, Stage.Vertex, "VertexShaderFunc");

            Assert.False(result.HasError, result.ToString());
        }
Пример #16
0
        /// <summary>
        /// Executes the specified test.
        /// </summary>
        /// <param name="generationResult">The generation result.</param>
        /// <param name="csFunctionName">Name of the cs function.</param>
        /// <param name="output">The output.</param>
        public void Execute(
            ShaderGenerationResult generationResult,
            string csFunctionName,
            ITestOutputHelper output)
        {
            if (Executed)
            {
                output.WriteLine(
                    $"The {Name} tests have already been executed!");
                return;
            }

            TestSets testSets = TestSets;
            Mappings mappings = testSets.Mappings;

            if (ToolChain == null)
            {
                /*
                 * Generate the test data and the result set data for the CPU.
                 */
                AllocateResults(output);
                using (new TestTimer(output,
                                     $"Running {testSets.TestLoops} iterations on the {Name} backend"))
                {
                    for (int test = 0; test < testSets.TestLoops; test++)
                    {
                        foreach (MethodMap method in mappings.MethodMaps)
                        {
                            method.ExecuteCPU(TestSets.TestData, Results, test);
                        }
                    }

                    return;
                }
            }

            GeneratedShaderSet set;
            CompileResult      compilationResult;

            // Compile shader for this backend.
            using (new TestTimer(output, $"Compiling Compute Shader for {ToolChain.GraphicsBackend}"))
            {
                set = generationResult.GetOutput(Backend).Single();
                compilationResult =
                    ToolChain.Compile(set.ComputeShaderCode, Stage.Compute, set.ComputeFunction.Name);
            }

            if (compilationResult.HasError)
            {
                output.WriteLine($"Failed to compile Compute Shader from set \"{set.Name}\"!");
                output.WriteLine(compilationResult.ToString());
                return;
            }

            Assert.NotNull(compilationResult.CompiledOutput);

            using (GraphicsDevice graphicsDevice = ToolChain.CreateHeadless())
            {
                if (!graphicsDevice.Features.ComputeShader)
                {
                    output.WriteLine(
                        $"The {ToolChain.GraphicsBackend} backend does not support compute shaders, skipping!");
                    return;
                }

                ResourceFactory factory = graphicsDevice.ResourceFactory;
                using (DeviceBuffer inOutBuffer = factory.CreateBuffer(
                           new BufferDescription(
                               (uint)mappings.BufferSize,
                               BufferUsage.StructuredBufferReadWrite,
                               (uint)mappings.StructSize)))

                    using (Shader computeShader = factory.CreateShader(
                               new ShaderDescription(
                                   ShaderStages.Compute,
                                   compilationResult.CompiledOutput,
                                   csFunctionName)))

                        using (ResourceLayout inOutStorageLayout = factory.CreateResourceLayout(
                                   new ResourceLayoutDescription(
                                       new ResourceLayoutElementDescription("InOutBuffer", ResourceKind.StructuredBufferReadWrite,
                                                                            ShaderStages.Compute))))

                            using (Pipeline computePipeline = factory.CreateComputePipeline(new ComputePipelineDescription(
                                                                                                computeShader,
                                                                                                new[] { inOutStorageLayout },
                                                                                                1, 1, 1)))


                                using (ResourceSet computeResourceSet = factory.CreateResourceSet(
                                           new ResourceSetDescription(inOutStorageLayout, inOutBuffer)))

                                    using (CommandList commandList = factory.CreateCommandList())
                                    {
                                        // Ensure the headless graphics device is the backend we expect.
                                        Assert.Equal(ToolChain.GraphicsBackend, graphicsDevice.BackendType);

                                        output.WriteLine($"Created compute pipeline for {Name} backend.");

                                        // Allocate the results buffer
                                        AllocateResults(output);

                                        using (new TestTimer(output,
                                                             $"Running {testSets.TestLoops} iterations on the {Name} backend"))
                                        {
                                            // Loop for each test
                                            for (int test = 0; test < testSets.TestLoops; test++)
                                            {
                                                // Update parameter buffer
                                                graphicsDevice.UpdateBuffer(
                                                    inOutBuffer,
                                                    0,
                                                    // Get the portion of test data for the current test loop
                                                    Marshal.UnsafeAddrOfPinnedArrayElement(testSets.TestData, mappings.BufferSize * test),
                                                    (uint)mappings.BufferSize);
                                                graphicsDevice.WaitForIdle();

                                                // Execute compute shaders
                                                commandList.Begin();
                                                commandList.SetPipeline(computePipeline);
                                                commandList.SetComputeResourceSet(0, computeResourceSet);
                                                commandList.Dispatch((uint)mappings.Methods, 1, 1);
                                                commandList.End();

                                                graphicsDevice.SubmitCommands(commandList);
                                                graphicsDevice.WaitForIdle();

                                                // Read back parameters using a staging buffer
                                                using (DeviceBuffer stagingBuffer =
                                                           factory.CreateBuffer(
                                                               new BufferDescription(inOutBuffer.SizeInBytes, BufferUsage.Staging)))
                                                {
                                                    commandList.Begin();
                                                    commandList.CopyBuffer(inOutBuffer, 0, stagingBuffer, 0, stagingBuffer.SizeInBytes);
                                                    commandList.End();
                                                    graphicsDevice.SubmitCommands(commandList);
                                                    graphicsDevice.WaitForIdle();

                                                    // Read back test results
                                                    MappedResource map = graphicsDevice.Map(stagingBuffer, MapMode.Read);

                                                    mappings.SetResults(map.Data, Results, test);
                                                    graphicsDevice.Unmap(stagingBuffer);
                                                }
                                            }
                                        }
                                    }
            }
        }
Пример #17
0
        public void AllSetsCompile()
        {
            Compilation compilation = TestUtil.GetCompilation();

            // Get all available tool chains.
            LanguageBackend[] backends = TestUtil.GetAllBackends(compilation, ToolFeatures.ToCompiled);

            ShaderGenerator        sg = new ShaderGenerator(compilation, backends);
            ShaderGenerationResult generationResult = sg.GenerateShaders();

            string spacer1 = new string('=', 80);
            string spacer2 = new string('-', 80);

            bool failed = false;

            foreach (LanguageBackend backend in backends)
            {
                ToolChain toolChain = ToolChain.Get(backend);
                IReadOnlyList <GeneratedShaderSet> sets = generationResult.GetOutput(backend);
                _output.WriteLine(spacer1);
                _output.WriteLine($"Generated shader sets for {toolChain.Name} backend.");

                foreach (GeneratedShaderSet set in sets)
                {
                    _output.WriteLine(string.Empty);
                    _output.WriteLine(spacer2);
                    _output.WriteLine(string.Empty);
                    CompileResult result;

                    if (set.VertexShaderCode != null)
                    {
                        result = toolChain.Compile(set.VertexShaderCode, Stage.Vertex, set.VertexFunction.Name);
                        if (result.HasError)
                        {
                            _output.WriteLine($"Failed to compile Vertex Shader from set \"{set.Name}\"!");
                            _output.WriteLine(result.ToString());
                            failed = true;
                        }
                        else
                        {
                            _output.WriteLine($"Compiled Vertex Shader from set \"{set.Name}\"!");
                        }
                    }

                    if (set.FragmentFunction != null)
                    {
                        result = toolChain.Compile(set.FragmentShaderCode, Stage.Fragment, set.FragmentFunction.Name);
                        if (result.HasError)
                        {
                            _output.WriteLine($"Failed to compile Fragment Shader from set \"{set.Name}\"!");
                            _output.WriteLine(result.ToString());
                            failed = true;
                        }
                        else
                        {
                            _output.WriteLine($"Compiled Fragment Shader from set \"{set.Name}\"!");
                        }
                    }

                    if (set.ComputeFunction != null)
                    {
                        // TODO The skipped shaders are not included in the auto discovered shaders, leaving this here for completeness.
                        if (backend is GlslEs300Backend)
                        {
                            string fullname = set.ComputeFunction.DeclaringType + "." + set.ComputeFunction.Name + "_";
                            if (s_glslesSkippedShaders.Contains(fullname))
                            {
                                continue;
                            }
                        }

                        result = toolChain.Compile(set.ComputeShaderCode, Stage.Compute, set.ComputeFunction.Name);
                        if (result.HasError)
                        {
                            _output.WriteLine($"Failed to compile Compute Shader from set \"{set.Name}\"!");
                            _output.WriteLine(result.ToString());
                            failed = true;
                        }
                        else
                        {
                            _output.WriteLine($"Compiled Compute Shader from set \"{set.Name}\"!");
                        }
                    }
                }

                _output.WriteLine(string.Empty);
            }

            Assert.False(failed);
        }