/// <summary>
        /// Searches the PATH environment and common SDK path environment variables for the glslangvalidator
        /// </summary>
        public static string Locate(SPIRVExtensionPackage package)
        {
            if (package.OptionGlslangValidatorBinaryPath != "")
            {
                OutputWindow.Add("Using glslangvalidator from options path: " + Path.Combine(package.OptionGlslangValidatorBinaryPath, "glslangvalidator.exe"));
                return(Path.Combine(package.OptionGlslangValidatorBinaryPath, "glslangvalidator.exe"));
            }

            var           pathEnv         = Environment.GetEnvironmentVariable("PATH");
            List <string> paths           = new List <string>(pathEnv.Split(';'));
            var           additionalPaths = new[] { "VK_SDK_PATH", "VULKAN_SDK" };

            foreach (string path in additionalPaths)
            {
                if (Environment.GetEnvironmentVariable(path) != null)
                {
                    paths.Add(Environment.GetEnvironmentVariable(path));
                }
            }

            foreach (var path in paths)
            {
                var filePath = Path.Combine(path, "glslangvalidator.exe");
                if (File.Exists(filePath))
                {
                    return(filePath);
                }
            }
            return(null);
        }
        private static bool Run(string fileName, string commandLine, out List <string> validatorOutput, SPIRVExtensionPackage package)
        {
            var startInfo = new ProcessStartInfo();

            startInfo.FileName               = Locate(package);
            startInfo.Arguments              = commandLine;
            startInfo.UseShellExecute        = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow         = true;

            var process = new Process();

            process.StartInfo = startInfo;

            try
            {
                process.Start();
            }
            catch (Exception e)
            {
                //todo: Display error message
                OutputWindow.Add("Exception while running: " + e.Message);
                throw;
            }

            OutputWindow.Add(Locate(package));
            OutputWindow.Add(commandLine);

            List <string> output = new List <string>();

            while (!process.StandardOutput.EndOfStream)
            {
                output.Add(process.StandardOutput.ReadLine());
            }

            validatorOutput = output;

            return(process.ExitCode == 0);
        }
        /// <summary>
        /// Converts the shader to human readable SPIR-V and returns the reference compiler output
        /// </summary>
        public static bool GetHumanReadableSPIRV(string fileName, out List <string> validatorOutput, SPIRVExtensionPackage package)
        {
            string commandLine = string.Format(CultureInfo.CurrentCulture, "-H \"{0}\"", fileName);

            if (package.OptionTargetEnv != "")
            {
                commandLine += " --target-env " + package.OptionTargetEnv;
            }
            return(Run(fileName, commandLine, out validatorOutput, package));
        }
        /// <summary>
        /// Compile the shader using OpenGL semantics and output to a binary file (.spv)
        /// </summary>
        public static bool CompileToOpenGL(string fileName, out List <string> validatorOutput, SPIRVExtensionPackage package)
        {
            string commandLine = string.Format(CultureInfo.CurrentCulture, "-G \"{0}\" -o \"{1}\"", fileName, fileName + ".spv");

            return(Run(fileName, commandLine, out validatorOutput, package));
        }
        /// <summary>
        /// Compile the shader using Vulkan semantics and output to a binary file (.spv)
        /// </summary>
        public static bool CompileToVulkan(string fileName, out List <string> validatorOutput, SPIRVExtensionPackage package)
        {
            string commandLine = string.Format(CultureInfo.CurrentCulture, "-V \"{0}\" -o \"{1}\"", fileName, fileName + ".spv");

            if (package.OptionTargetEnv != "")
            {
                commandLine += " --target-env " + package.OptionTargetEnv;
            }
            else
            {
                // Ray tracing shaders require at least SPIR-V 1.4
                if (Utils.IsRayTracingShaderFile(fileName))
                {
                    commandLine += " --target-env spirv1.4";
                }
            }
            return(Run(fileName, commandLine, out validatorOutput, package));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Compile the shader using Vulkan semantics and output to a binary file (.spv)
        /// </summary>
        public static bool CompileToVulkan(string fileName, out List <string> validatorOutput, SPIRVExtensionPackage package)
        {
            // Get profile and additional options based on file extension
            var profileDictionary = new Dictionary <string, string>
            {
                { ".vert", "vs_6_1" },
                { ".frag", "ps_6_1" },
                { ".comp", "cs_6_1" },
                { ".geom", "gs_6_1" },
                { ".tesc", "hs_6_1" },
                { ".tese", "ds_6_1" },
                { ".rgen", "lib_6_3" },
                { ".rchit", "lib_6_3" },
                { ".rmiss", "lib_6_3" },
            };
            string fileExt = Path.GetExtension(fileName).ToLower();

            if (!profileDictionary.ContainsKey(fileExt))
            {
                // @todo: add message
                List <string> output = new List <string>();
                output.Add("Could not match file extension to HLSL shader profile");
                validatorOutput = output;
                return(false);
            }
            string profile = profileDictionary[fileExt];

            List <string> commandLineArgs = new List <string>();

            commandLineArgs.Add("-spirv");
            commandLineArgs.Add("-T " + profile);
            commandLineArgs.Add("-E main");
            if (package.OptionTargetEnv != "")
            {
                commandLineArgs.Add("-fspv-target-env=" + package.OptionTargetEnv);
            }
            else
            {
                // Ray tracing shaders require at least SPIR-V 1.4
                if (Utils.IsRayTracingShaderFile(fileName))
                {
                    commandLineArgs.Add("-fspv-target-env=vulkan1.2");
                }
            }
            commandLineArgs.Add(fileName);
            commandLineArgs.Add("-Fo");
            commandLineArgs.Add(fileName + ".spv");

            string commandLine = string.Join(" ", commandLineArgs);

            return(Run(fileName, commandLine, out validatorOutput, package));
        }