private static string Compile(string shaderCode, string shaderType) { var options = OptionsPagePackage.Options; if (!string.IsNullOrWhiteSpace(options.ExternalCompilerExeFilePath)) { //create temp shader file for external compiler var shaderFileName = Path.Combine(Path.GetTempPath(), $"shader{ShaderContentTypes.DefaultFileExtension(shaderType)}"); try { File.WriteAllText(shaderFileName, shaderCode); using (var process = new Process()) { process.StartInfo.FileName = options.ExternalCompilerExeFilePath; process.StartInfo.Arguments = $"{options.ExternalCompilerArguments} {shaderFileName}"; //arguments process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.CreateNoWindow = true; //do not display a windows VsStatusBar.SetText($"Using external compiler '{Path.GetFileNameWithoutExtension(options.ExternalCompilerExeFilePath)}' with arguments '{options.ExternalCompilerArguments}' on temporal shader file '{shaderFileName}'"); process.Start(); process.WaitForExit(10000); var output = process.StandardOutput.ReadToEnd(); //The output result return(output.Replace(shaderFileName, "0")); //HACK: glslLangValidator produces inconsistent error message format when using vulkan vs glsl compilation } } catch (Exception e) { var message = "Error executing external compiler with message\n" + e.ToString(); VsStatusBar.SetText(message); } } VsStatusBar.SetText("Using driver compiler"); return(CompileOnGPU(shaderCode, shaderType)); }
private static string CompileExternal(string shaderCode, string shaderContentType, ILogger logger, ICompilerSettings settings) { //create temp shader file for external compiler var tempPath = Path.GetTempPath(); var shaderFileName = Path.Combine(tempPath, $"shader{ShaderContentTypes.DefaultFileExtension(shaderContentType)}"); try { File.WriteAllText(shaderFileName, shaderCode); using (var process = new Process()) { process.StartInfo.FileName = VsExpand.EnvironmentVariables(settings.ExternalCompilerExeFilePath); var arguments = VsExpand.EnvironmentVariables(settings.ExternalCompilerArguments); process.StartInfo.Arguments = $"{arguments} {shaderFileName}"; //arguments process.StartInfo.WorkingDirectory = tempPath; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.CreateNoWindow = true; //do not display a windows logger.Log($"Using external compiler '{settings.ExternalCompilerExeFilePath}' with arguments '{arguments}' on temporal shader file '{shaderFileName}'", true); process.Start(); if (!process.WaitForExit(10000)) { logger.Log($"External compiler did take more than 10 seconds to finish. Aborting!", true); } return(process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd()); //The output result } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { var message = "Error executing external compiler with message\n" + e.ToString(); logger.Log(message, true); return(string.Empty); } #pragma warning restore CA1031 // Do not catch general exception types }