static string CalculateCacheKey(string source, string function, string profile) { StringBuilder builder = new StringBuilder(); builder.Append(function); builder.Append(profile); if (MyRender11.DebugMode) { builder.Append("$DEBUG"); } try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, ShadersContentPath)); builder.Append(ShaderBytecode.Preprocess(source, null, includes)); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(builder.ToString()); byte[] hash = m_md5.ComputeHash(inputBytes); builder.Clear(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("X2")); } return(builder.ToString()); } catch (CompilationException e) { return(null); } return(null); }
private static string PreprocessShader(string source, ShaderMacro[] macros) { try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, MyShadersDefines.ShadersContentPath)); return(ShaderBytecode.Preprocess(source, macros, includes)); } catch (CompilationException e) { return(null); } }
private static string PreprocessShader(string filepath, ShaderMacro[] macros, out string errors) { try { var includes = new MyIncludeProcessor(filepath); return(ShaderBytecode.PreprocessFromFile(filepath, macros, includes, out errors)); } catch (Exception e) { errors = e.Message; return(null); } }
internal static byte[] Compile(string source, string function, string profile, string name, bool invalidateCache) { string errors; var dumpResults = MyShaderFactory.DUMP_CODE; var key = CalculateCacheKey(source, function, profile); if(!invalidateCache) { var cached = TryCatcheFetch(key); if (cached != null) { return cached; } } try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, ShadersContentPath)); CompilationResult compilationResult; if(MyRender11.DebugMode) { compilationResult = ShaderBytecode.Compile(source, function, profile, 0, 0, null, includes, name); } else { compilationResult = ShaderBytecode.Compile(source, function, profile, ShaderFlags.OptimizationLevel3, 0, null, includes, name); } if (dumpResults) { var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode | DisassemblyFlags.EnableInstructionNumbering); string asmPath; if(MyRender11.DebugMode) { asmPath = Path.GetFileName(name + "__DEBUG.html"); } else { asmPath = Path.GetFileName(name + "__O3.html"); } using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath))) { writer.Write(disassembly); } } if(compilationResult.Message != null) { Debug.WriteLine(String.Format("Compilation of shader {0}: {1}", name, compilationResult.Message)); ExtendedErrorMessage(source, compilationResult.Message); } if (compilationResult.Bytecode.Data.Length > 0) { StoreInCache(key.ToString(), compilationResult.Bytecode.Data); } return compilationResult.Bytecode.Data; } catch (CompilationException e) { MyRender11.Log.WriteLine(String.Format("Compilation of shader {0} failed: {1}", name, e)); Debug.WriteLine(String.Format("Compilation of shader {0} failed", name)); Debug.WriteLine(e); ExtendedErrorMessage(source, e.Message); } catch (System.IO.FileNotFoundException) { throw; } return null; }
static string CalculateCacheKey(string source, string function, string profile) { StringBuilder builder = new StringBuilder(); builder.Append(function); builder.Append(profile); if(MyRender11.DebugMode) { builder.Append("$DEBUG"); } try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, ShadersContentPath)); builder.Append(ShaderBytecode.Preprocess(source, null, includes)); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(builder.ToString()); byte[] hash = m_md5.ComputeHash(inputBytes); builder.Clear(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("X2")); } return builder.ToString(); } catch (CompilationException e) { return null; } return null; }
internal static byte[] Compile(string source, string function, string profile, string name, bool invalidateCache) { string errors; var dumpResults = MyShaderFactory.DUMP_CODE; var key = CalculateCacheKey(source, function, profile); if (!invalidateCache) { var cached = TryCatcheFetch(key); if (cached != null) { return(cached); } } try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, ShadersContentPath)); CompilationResult compilationResult; if (MyRender11.DebugMode) { compilationResult = ShaderBytecode.Compile(source, function, profile, 0, 0, DebugMacro, includes, name); } else { compilationResult = ShaderBytecode.Compile(source, function, profile, ShaderFlags.OptimizationLevel3, 0, null, includes, name); } if (dumpResults) { var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode | DisassemblyFlags.EnableInstructionNumbering); string asmPath; if (MyRender11.DebugMode) { asmPath = Path.GetFileName(name + "__DEBUG.html"); } else { asmPath = Path.GetFileName(name + "__O3.html"); } using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath))) { writer.Write(disassembly); } } if (compilationResult.Message != null) { Debug.WriteLine(String.Format("Compilation of shader {0}: {1}", name, compilationResult.Message)); ExtendedErrorMessage(source, compilationResult.Message); } if (compilationResult.Bytecode.Data.Length > 0) { StoreInCache(key.ToString(), compilationResult.Bytecode.Data); } return(compilationResult.Bytecode.Data); } catch (CompilationException e) { MyRender11.Log.WriteLine(String.Format("Compilation of shader {0} failed: {1}", name, e)); Debug.WriteLine(String.Format("Compilation of shader {0} failed", name)); Debug.WriteLine(e); ExtendedErrorMessage(source, e.Message); } catch (System.IO.FileNotFoundException) { throw; } return(null); }
private static string ExtendedErrorMessage(string originalCode, string errorMsg) { Regex rx = new Regex(@"^(?<fileName>\S*)\((?<lineNo>\d+),\d+\):(?<message>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); var sb = new StringBuilder(); var errorLines = errorMsg.Split(new[] { "\n" }, StringSplitOptions.None); for (int j = 0; j < errorLines.Length; j++) { var match = rx.Match(errorLines[j]); if (match == null) { sb.AppendLine(errorLines[j]); continue; } var lineStr = match.Groups["lineNo"].Value; if (lineStr != "") { var code = originalCode; var fileName = match.Groups["fileName"].Value; if (fileName != "") { try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, MyShadersDefines.ShadersContentPath)); var stream = includes.Open(IncludeType.System, fileName, null); if (stream == null) { return(""); } using (var reader = new StreamReader(stream, Encoding.UTF8)) { code = reader.ReadToEnd(); } } catch (Exception ex) { return(""); } } sb.AppendFormat("{0} @ {1}: {2}\n", fileName, lineStr, match.Groups["message"].Value); var line = Int32.Parse(lineStr) - 1; var sourceLines = code.Split(new[] { "\n" }, StringSplitOptions.None); for (int i = -2; i <= 2; i++) { var offseted = line + i; if (0 <= offseted && offseted < sourceLines.Length) { sb.AppendFormat("{0}: {1}\n", offseted + 1, sourceLines[offseted]); } } sb.AppendFormat("\n"); } else { sb.AppendLine(errorLines[j]); } } return(sb.ToString()); }
private static string ExtendedErrorMessage(string originalCode, string errorMsg) { Regex rx = new Regex(@"^(?<fileName>\S*)\((?<lineNo>\d+),\d+\):(?<message>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); var sb = new StringBuilder(); var errorLines = errorMsg.Split(new[] { "\n" }, StringSplitOptions.None); for(int j = 0; j < errorLines.Length; j++) { var match = rx.Match(errorLines[j]); if (match == null) { sb.AppendLine(errorLines[j]); continue; } var lineStr = match.Groups["lineNo"].Value; if (lineStr != "") { var code = originalCode; var fileName = match.Groups["fileName"].Value; if (fileName != "") { try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, MyShadersDefines.ShadersContentPath)); var stream = includes.Open(IncludeType.System, fileName, null); if (stream == null) return ""; using (var reader = new StreamReader(stream, Encoding.UTF8)) { code = reader.ReadToEnd(); } } catch (Exception ex) { return ""; } } sb.AppendFormat("{0} @ {1}: {2}\n", fileName, lineStr, match.Groups["message"].Value); var line = Int32.Parse(lineStr) - 1; var sourceLines = code.Split(new[] { "\n" }, StringSplitOptions.None); for (int i = -2; i <= 2; i++) { var offseted = line + i; if (0 <= offseted && offseted < sourceLines.Length) { sb.AppendFormat("{0}: {1}\n", offseted + 1, sourceLines[offseted]); } } sb.AppendFormat("\n"); } else { sb.AppendLine(errorLines[j]); } } return sb.ToString(); }
private static string PreprocessShader(string source, ShaderMacro[] macros) { try { var includes = new MyIncludeProcessor(Path.Combine(MyFileSystem.ContentPath, MyShadersDefines.ShadersContentPath)); return ShaderBytecode.Preprocess(source, macros, includes); } catch (CompilationException e) { return null; } }
private static string PreprocessShader(string filepath, ShaderMacro[] macros, out string errors) { try { var includes = new MyIncludeProcessor(filepath); return ShaderBytecode.PreprocessFromFile(filepath, macros, includes, out errors); } catch (Exception e) { errors = e.Message; return null; } }