Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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
        }
Ejemplo n.º 3
0
        private void TaskGlAction()
        {
            //create a game window for rendering context, until run is called it is invisible so no problem
            var context = new GameWindow(1, 1);

            while (!compileRequests.IsAddingCompleted)
            {
                var compileData  = compileRequests.Take();                //block until compile requested
                var expandedCode = ExpandedCode(compileData.ShaderCode, compileData.DocumentDir);
                var log          = Compile(expandedCode, compileData.ShaderType);
                var errorLog     = new ShaderLogParser(log);
                if (!string.IsNullOrWhiteSpace(log) && OptionsPagePackage.Options.PrintCompilationResult)
                {
                    OutMessage.OutputWindowPane(log);
                }
                compileData.CompilationFinished?.Invoke(errorLog.Lines);
            }
        }
Ejemplo n.º 4
0
 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");
     }
 }
Ejemplo n.º 5
0
        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));
            }
        }