示例#1
0
        private byte[] CompileToSpirv(
            VesselShaderDescription shader,
            string fileName,
            ShaderStages stage)
        {
            GlslCompileOptions     glslOptions = VesselShaderUtil.GetOptions(shader);
            string                 glsl        = VesselShaderUtil.LoadGlsl(fileName);
            SpirvCompilationResult result      = SpirvCompilation.CompileGlslToSpirv(
                glsl,
                fileName,
                stage,
                glslOptions);

            return(result.SpirvBytes);
        }
示例#2
0
        private string[] CompileCompute(VesselShaderDescription shader)
        {
            List <string> generatedFiles = new List <string>();
            var           csBytes        = CompileToSpirv(shader, shader.Shaders[0].FileName, ShaderStages.Compute);

            // Generate metadata for HLSL, cuz we want to know the semantics since theyre important on HLSL
            var crossData = SpirvCompilation.CompileCompute(csBytes, CrossCompileTarget.HLSL);

            WriteShaderBinary(
                shader.Name + ".shdr",
                new byte[][] { csBytes },
                crossData.Reflection);

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

            return(generatedFiles.ToArray());
        }
示例#3
0
        public string[] Compile(VesselShaderDescription shader)
        {
            if (shader.Shaders.Length == 1)
            {
                if (shader.Shaders[0].Stage == ShaderStages.Vertex)
                {
                    return(CompileVertexFragment(shader));
                }
                if (shader.Shaders[0].Stage == ShaderStages.Compute)
                {
                    return(CompileCompute(shader));
                }
            }
            if (shader.Shaders.Length == 2)
            {
                bool hasVertex   = false;
                bool hasFragment = false;
                foreach (var shaderStage in shader.Shaders)
                {
                    hasVertex   |= shaderStage.Stage == ShaderStages.Vertex;
                    hasFragment |= shaderStage.Stage == ShaderStages.Fragment;
                }

                if (!hasVertex)
                {
                    throw new SpirvCompilationException($"Variant \"{shader.Name}\" is missing a vertex shader.");
                }
                if (!hasFragment)
                {
                    throw new SpirvCompilationException($"Variant \"{shader.Name}\" is missing a fragment shader.");
                }

                return(CompileVertexFragment(shader));
            }
            else
            {
                throw new SpirvCompilationException(
                          $"Variant \"{shader.Name}\" has an unsupported combination of shader stages.");
            }
        }
示例#4
0
 public static GlslCompileOptions GetOptions(VesselShaderDescription shader)
 {
     //return new GlslCompileOptions(false, shader.Macros);
     return(new GlslCompileOptions(debugMode));
 }
示例#5
0
        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());
        }