Exemple #1
0
        public void ReflectionFromSpirv_Succeeds(
            string vertex, string fragment,
            VertexElementDescription[] verts,
            ResourceLayoutDescription[] layouts)
        {
            byte[] vsBytes = TestUtil.LoadBytes(vertex);
            byte[] fsBytes = TestUtil.LoadBytes(fragment);
            VertexFragmentCompilationResult result = SpirvCompilation.CompileVertexFragment(
                vsBytes,
                fsBytes,
                CrossCompileTarget.HLSL);

            VertexElementDescription[] reflectedVerts = result.Reflection.VertexElements;
            Assert.Equal(verts.Length, reflectedVerts.Length);
            for (int i = 0; i < verts.Length; i++)
            {
                Assert.Equal(verts[i], reflectedVerts[i]);
            }

            ResourceLayoutDescription[] reflectedLayouts = result.Reflection.ResourceLayouts;
            Assert.Equal(layouts.Length, reflectedLayouts.Length);
            for (int i = 0; i < layouts.Length; i++)
            {
                ResourceLayoutDescription layout          = layouts[i];
                ResourceLayoutDescription reflectedLayout = reflectedLayouts[i];
                Assert.Equal(layout.Elements.Length, reflectedLayout.Elements.Length);
                for (int j = 0; j < layout.Elements.Length; j++)
                {
                    Assert.Equal(layout.Elements[j], reflectedLayout.Elements[j]);
                }
            }
        }
        public void VertexFragmentSucceeds(string vs, string fs, CrossCompileTarget target)
        {
            byte[] vsBytes = TestUtil.LoadBytes(vs);
            byte[] fsBytes = TestUtil.LoadBytes(fs);
            SpecializationConstant[] specializations =
            {
                new SpecializationConstant(100,  125u),
                new SpecializationConstant(101, true),
                new SpecializationConstant(102, 0.75f),
            };
            VertexFragmentCompilationResult result = SpirvCompilation.CompileVertexFragment(
                vsBytes,
                fsBytes,
                target,
                new CrossCompileOptions(false, false, specializations));

            Assert.NotNull(result.VertexShader);
            Assert.NotNull(result.FragmentShader);
        }
        private string[] CompileVertexFragment(VesselShaderDescription shader)
        {
            List <string>    generatedFiles        = new List <string>();
            List <Exception> compilationExceptions = new List <Exception>();

            byte[] vsBytes = null;
            byte[] fsBytes = null;

            // Compile vertex shader
            string vertexFileName = shader.Shaders.FirstOrDefault(vsd => vsd.Stage == ShaderStages.Vertex).FileName;

            if (vertexFileName != null)
            {
                try
                {
                    vsBytes = CompileToSpirv(shader, vertexFileName, ShaderStages.Vertex);
                }
                catch (Exception e)
                {
                    compilationExceptions.Add(e);
                }
            }

            // Compile fragment shader
            string fragmentFileName = shader.Shaders.FirstOrDefault(vsd => vsd.Stage == ShaderStages.Fragment).FileName;

            if (fragmentFileName != null)
            {
                try
                {
                    fsBytes = CompileToSpirv(shader, fragmentFileName, ShaderStages.Fragment);
                }
                catch (Exception e)
                {
                    compilationExceptions.Add(e);
                }
            }

            if (compilationExceptions.Count > 0)
            {
                throw new AggregateException(
                          $"Errors were encountered when compiling from GLSL to SPIR-V.",
                          compilationExceptions);
            }

            try
            {
                // Generate reflection data
                VertexFragmentCompilationResult result = SpirvCompilation.CompileVertexFragment(vsBytes, fsBytes, CrossCompileTarget.HLSL);

                WriteShaderBinary(
                    shader.Name + ".shdr",
                    new byte[][] { vsBytes, fsBytes },
                    result.Reflection);
            }
            catch (Exception e)
            {
                compilationExceptions.Add(e);
            }

            if (compilationExceptions.Count > 0)
            {
                throw new AggregateException($"Errors were encountered when compiling shader variant(s).", compilationExceptions);
            }

            Console.WriteLine($"Successfully compiled \"{Path.GetFileNameWithoutExtension(shader.Shaders[0].FileName)}\" as a Vertex, Fragment pair shader!");

            return(generatedFiles.ToArray());
        }