Beispiel #1
0
        private static string Compile(string shaderCode, ShaderType shaderType, string shaderFileDir)
        {
            string GetIncludeCode(string includeName)
            {
                var includeFileName = Path.Combine(shaderFileDir, includeName);

                if (File.Exists(includeFileName))
                {
                    return(File.ReadAllText(includeFileName));
                }
                return($"#error include file '{includeName}' not found");
            }

            try
            {
                using (var shader = new ShaderGL(shaderType))
                {
                    var expandedCode = ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode);
                    shader.Compile(expandedCode);
                    return(shader.Log);
                }
            }
            catch (AccessViolationException)
            {
                return($"(1 1):ERROR: OpenGL shader compiler has crashed");
            }
        }
Beispiel #2
0
        private static string Compile(string shaderCode, ShaderType shaderType)
        {
            var options = OptionsPagePackage.Options;

            if (!string.IsNullOrWhiteSpace(options.ExternalCompilerExeFilePath))
            {
                //create temp shader file for external compiler
                var shaderFileName = GetShaderFileName(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}'");
                        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 cg 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));
        }
Beispiel #3
0
        private static string Compile(string shaderCode, ShaderType shaderType)
        {
            var options = OptionsPagePackage.Options;

            if (File.Exists(options.ExternalCompilerExeFilePath))
            {
                //create temp shader file
                var shaderFileName = GetShaderFileName(shaderType);
                try
                {
                    File.WriteAllText(shaderFileName, shaderCode);
                    using (var process = new Process())
                    {
                        process.StartInfo.FileName               = options.ExternalCompilerExeFilePath;
                        process.StartInfo.Arguments              = shaderFileName;            //arguments
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                        process.StartInfo.CreateNoWindow         = true;                 //do not display a windows
                        process.Start();
                        process.WaitForExit(10000);
                        return(process.StandardOutput.ReadToEnd());                        //The output result
                    }
                }
                catch (Exception) { }
            }
            return(CompileOnGPU(shaderCode, shaderType));
        }
Beispiel #4
0
 private static string CompileOnGPU(string shaderCode, ShaderType shaderType)
 {
     try
     {
         using (var shader = new ShaderGL(shaderType))
         {
             shader.Compile(shaderCode);
             return(shader.Log);
         }
     }
     catch (AccessViolationException)
     {
         return($"(1 1):ERROR: OpenGL shader compiler has crashed");
     }
 }
Beispiel #5
0
        private static string Compile(string shaderCode, ShaderType shaderType, string shaderFileDir)
        {
            string SpecialCommentReplacement(string code, string specialComment)
            {
                var lines = code.Split(new[] { '\n' }, StringSplitOptions.None);                 //if UNIX style line endings still working so do not use Envirnoment.NewLine

                for (int i = 0; i < lines.Length; ++i)
                {
                    var index = lines[i].IndexOf(specialComment);                     // search for special comment
                    if (-1 != index)
                    {
                        lines[i] = lines[i].Substring(index + specialComment.Length);                         // remove everything before special comment
                    }
                }
                return(lines.Combine("\n"));
            }

            string GetIncludeCode(string includeName)
            {
                var includeFileName = Path.Combine(shaderFileDir, includeName);

                if (File.Exists(includeFileName))
                {
                    var includeCode = File.ReadAllText(includeFileName);
                    includeCode = SpecialCommentReplacement(includeCode, "//!");
                    return(includeCode);
                }
                return($"#error include file '{includeName}' not found");
            }

            try
            {
                using (var shader = new ShaderGL(shaderType))
                {
                    shaderCode = SpecialCommentReplacement(shaderCode, "//!");
                    shaderCode = SpecialCommentReplacement(shaderCode, "//?");
                    var expandedCode = ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode);
                    shader.Compile(expandedCode);
                    return(shader.Log);
                }
            }
            catch (AccessViolationException)
            {
                return($"(1 1):ERROR: OpenGL shader compiler has crashed");
            }
        }
Beispiel #6
0
        private static string FileExtension(ShaderType shaderType)
        {
            switch (shaderType)
            {
            case ShaderType.ComputeShader: return("comp");

            case ShaderType.FragmentShader: return("frag");

            case ShaderType.GeometryShader: return("geom");

            case ShaderType.TessControlShader: return("tesc");

            case ShaderType.TessEvaluationShader: return("tese");

            case ShaderType.VertexShader: return("vert");
            }
            return("frag");
        }
Beispiel #7
0
        private static string GetShaderFileName(ShaderType shaderType)
        {
            var extension = FileExtension(shaderType);

            return(Path.Combine(Path.GetTempPath(), $"shader.{extension}"));
        }