private static string CompileExternal(string shaderCode, string shaderContentType) { var options = OptionsPagePackage.Options; //create temp shader file for external compiler var shaderFileName = Path.Combine(Path.GetTempPath(), $"shader{ShaderContentTypes.DefaultFileExtension(shaderContentType)}"); 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 OutMessage.PaneAndBar($"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, string.Empty)); //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(); OutMessage.PaneAndBar(message); return(string.Empty); } }
private static string CompileOnGPU(string shaderCode, string shaderType) { // detect shader type if (!mappingContentTypeToShaderType.TryGetValue(shaderType, out ShaderType glShaderType)) { OutMessage.PaneAndBar($"Unsupported shader type '{shaderType}' by OpenTK shader compiler. Use an external compiler"); } try { var id = GL.CreateShader(glShaderType); if (0 == id) { throw new Exception($"Could not create {shaderType} instance."); } GL.ShaderSource(id, shaderCode); GL.CompileShader(id); GL.GetShader(id, ShaderParameter.CompileStatus, out int status_code); string log = string.Empty; if (1 != status_code) { log = GL.GetShaderInfoLog(id); } GL.DeleteShader(id); return(log); } #pragma warning disable CA1031 // Do not catch general exception types catch (AccessViolationException) { return($"(1 1):ERROR: OpenGL shader compiler has crashed"); } #pragma warning restore CA1031 // Do not catch general exception types }
private static string CompileOnGPU(string shaderCode, string shaderType) { // detect shader type if (!mappingContentTypeToShaderType.TryGetValue(shaderType, out ShaderType glShaderType)) { OutMessage.PaneAndBar($"Unsupported shader type '{shaderType}' by OpenTK shader compiler. Use an external compiler"); } try { using (var shader = new ShaderGL(glShaderType)) { shader.Compile(shaderCode); return(shader.Log); } } catch (AccessViolationException) { return($"(1 1):ERROR: OpenGL shader compiler has crashed"); } }
private static string Compile(string shaderCode, string shaderContentType) { if (ShaderContentTypes.AutoDetect == shaderContentType) { shaderContentType = AutoDetectShaderContentType(shaderCode); OutMessage.PaneAndBar($"{DateTime.Now.ToString("HH.mm.ss.fff")} Auto detecting shader type to '{shaderContentType}'"); } var externalCompiler = OptionsPagePackage.Options.ExternalCompilerExeFilePath; if (File.Exists(externalCompiler)) { return(CompileExternal(shaderCode, shaderContentType)); } else { if (!string.IsNullOrWhiteSpace(externalCompiler)) { OutMessage.PaneAndBar($"External compiler '{externalCompiler}' not found using GPU"); } return(CompileOnGPU(shaderCode, shaderContentType)); } }