Example #1
0
        internal static byte[] Compile(string source, ShaderMacro[] macros, MyShadersDefines.Profiles profile, string sourceDescriptor, bool invalidateCache)
        {
            bool   wasCached;
            string compileLog;
            var    result = Compile(source, macros, profile, sourceDescriptor, !MyRender11.DebugMode, invalidateCache, out wasCached, out compileLog);

            if (!wasCached)
            {
                string message = "WARNING: Shader was not precompiled - " + sourceDescriptor + " @ profile " + profile + " with defines " + macros.GetString();
                MyRender11.Log.WriteLine(message);
            }
            if (!string.IsNullOrEmpty(compileLog))
            {
                string descriptor = sourceDescriptor + " " + MyShadersDefines.ProfileToString(profile) + " " + macros.GetString();

                if (result != null)
                {
                    Debug.WriteLine(String.Format("Compilation of shader {0} notes:\n{1}", descriptor, compileLog));
                }
                else
                {
                    string message = String.Format("Compilation of shader {0} errors:\n{1}", descriptor, compileLog);
                    MyRender11.Log.WriteLine(message);
                    Debug.WriteLine(message);
                }
            }
            return(result);
        }
Example #2
0
        internal static byte[] Compile(string source, ShaderMacro[] macros, MyShadersDefines.Profiles profile, string sourceDescriptor, bool optimize, bool invalidateCache, out bool wasCached, out string compileLog)
        {
            ProfilerShort.Begin("MyShaders.Compile");
            string function    = MyShadersDefines.ProfileEntryPoint(profile);
            string profileName = MyShadersDefines.ProfileToString(profile);

            wasCached  = false;
            compileLog = null;

            ProfilerShort.Begin("MyShaders.Preprocess");
            string preprocessedSource = PreprocessShader(source, macros);

            var key = MyShaderCache.CalculateKey(preprocessedSource, function, profileName);

            if (!invalidateCache)
            {
                var cached = MyShaderCache.TryFetch(key);
                if (cached != null)
                {
                    wasCached = true;
                    ProfilerShort.End();
                    ProfilerShort.End();
                    return(cached);
                }
            }
            ProfilerShort.End();

            try
            {
                string            descriptor        = sourceDescriptor + " " + profile + " " + macros.GetString();
                CompilationResult compilationResult = ShaderBytecode.Compile(preprocessedSource, function, profileName, optimize ? ShaderFlags.OptimizationLevel3 : 0, 0, null, null, descriptor);

                if (DUMP_CODE)
                {
                    var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode |
                                                                             DisassemblyFlags.EnableInstructionNumbering);
                    string asmPath;
                    if (MyRender11.DebugMode)
                    {
                        asmPath = Path.GetFileName(descriptor + "__DEBUG.html");
                    }
                    else
                    {
                        asmPath = Path.GetFileName(descriptor + "__O3.html");
                    }

                    using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath)))
                    {
                        writer.Write(disassembly);
                    }
                }

                if (compilationResult.Message != null)
                {
                    compileLog = ExtendedErrorMessage(source, compilationResult.Message) + DumpShaderSource(key, preprocessedSource);
                }

                if (compilationResult.Bytecode.Data.Length > 0)
                {
                    MyShaderCache.Store(key.ToString(), compilationResult.Bytecode.Data);
                }

                return(compilationResult.Bytecode.Data);
            }
            catch (CompilationException e)
            {
                Debug.WriteLine(preprocessedSource);
                compileLog = ExtendedErrorMessage(source, e.Message) + DumpShaderSource(key, preprocessedSource);
            }
            finally
            {
                ProfilerShort.End();
            }
            return(null);
        }
Example #3
0
        internal static byte[] Compile(string source, ShaderMacro[] macros, MyShadersDefines.Profiles profile, string sourceDescriptor, bool optimize, bool invalidateCache, out bool wasCached, out string compileLog)
        {
            ProfilerShort.Begin("MyShaders.Compile");
            string function = MyShadersDefines.ProfileEntryPoint(profile);
            string profileName = MyShadersDefines.ProfileToString(profile);

            wasCached = false;
            compileLog = null;

            ProfilerShort.Begin("MyShaders.Preprocess");
            string preprocessedSource = PreprocessShader(source, macros);

            var key = MyShaderCache.CalculateKey(preprocessedSource, function, profileName);
            if (!invalidateCache)
            {
                var cached = MyShaderCache.TryFetch(key);
                if (cached != null)
                {
                    wasCached = true;
                    ProfilerShort.End();
                    ProfilerShort.End();
                    return cached;
                }
            }
            ProfilerShort.End();

            try
            {
                string descriptor = sourceDescriptor + " " + profile + " " + macros.GetString();
                CompilationResult compilationResult = ShaderBytecode.Compile(preprocessedSource, function, profileName, optimize ? ShaderFlags.OptimizationLevel3 : 0, 0, null, null, descriptor);

                if (DUMP_CODE)
                {
                    var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode |
                                                                             DisassemblyFlags.EnableInstructionNumbering);
                    string asmPath;
                    if (MyRender11.DebugMode)
                    {
                        asmPath = Path.GetFileName(descriptor + "__DEBUG.html");
                    }
                    else
                    {
                        asmPath = Path.GetFileName(descriptor + "__O3.html");
                    }

                    using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath)))
                    {
                        writer.Write(disassembly);
                    }
                }

                if (compilationResult.Message != null)
                {
                    compileLog = ExtendedErrorMessage(source, compilationResult.Message) + DumpShaderSource(key, preprocessedSource);
                }

                if (compilationResult.Bytecode != null && compilationResult.Bytecode.Data.Length > 0)
                    MyShaderCache.Store(key.ToString(), compilationResult.Bytecode.Data);

                return compilationResult.Bytecode != null ? compilationResult.Bytecode.Data : null;
            }
            catch (CompilationException e)
            {
                Debug.WriteLine(preprocessedSource);
                compileLog = ExtendedErrorMessage(source, e.Message) + DumpShaderSource(key, preprocessedSource);
            }
            finally
            {
                ProfilerShort.End();
            }
            return null;
        }
Example #4
0
        internal static byte[] Compile(string source, ShaderMacro[] macros, MyShadersDefines.Profiles profile, string sourceDescriptor, bool invalidateCache)
        {
            bool wasCached;
            string compileLog;
            var result = Compile(source, macros, profile, sourceDescriptor, !MyRender11.DebugMode, invalidateCache, out wasCached, out compileLog);

            if (!wasCached)
            {
                string message = "WARNING: Shader was not precompiled - " + sourceDescriptor + " @ profile " + profile + " with defines " + macros.GetString();
                MyRender11.Log.WriteLine(message);
            }
            if (!string.IsNullOrEmpty(compileLog))
            {
                string descriptor = sourceDescriptor + " " + MyShadersDefines.ProfileToString(profile) + " " + macros.GetString();

                if (result != null)
                {
                    Debug.WriteLine(String.Format("Compilation of shader {0} notes:\n{1}", descriptor, compileLog));
                    
                }
                else
                {
                    string message = String.Format("Compilation of shader {0} errors:\n{1}", descriptor, compileLog);
                    MyRender11.Log.WriteLine(message);
                    Debug.WriteLine(message);
                    Debugger.Break();
                }
            }
            return result;
        }