コード例 #1
0
 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);
     }
 }
コード例 #2
0
        protected MyEffectBase(string asset)
        {
            string sourceFX   = asset + ".fx";
            string compiledFX = asset + ".fxo";

            var srcPath = Path.Combine(MyFileSystem.ContentPath, sourceFX);
            var comPath = Path.Combine(MyFileSystem.ContentPath, compiledFX);

            var srcAbsolute = srcPath;

            bool needRecompile = false;

            if (File.Exists(comPath))
            {
                if (File.Exists(srcPath) && !MyRenderProxy.IS_OFFICIAL)
                {
                    var compiledTime = File.GetLastWriteTime(comPath);
                    var sourceTime   = File.GetLastWriteTime(srcAbsolute);

                    m_includeProcessor.Reset(Path.GetDirectoryName(srcAbsolute));
                    ShaderBytecode.PreprocessFromFile(srcAbsolute, null, m_includeProcessor);
                    sourceTime = m_includeProcessor.MostUpdateDateTime > sourceTime ? m_includeProcessor.MostUpdateDateTime : sourceTime;

                    if (sourceTime > compiledTime)
                    {
                        needRecompile = true;
                    }
                }
            }
            else
            {
                if (File.Exists(srcPath))
                {
                    needRecompile = true;
                }
                else
                {
                    throw new FileNotFoundException("Effect not found: " + asset);
                }
            }

            ShaderFlags flags = ShaderFlags.OptimizationLevel3 | ShaderFlags.PartialPrecision | ShaderFlags.SkipValidation;

            if (needRecompile)
            {
//#if DEBUG
//                flags |= ShaderFlags.Debug;
//#endif
                //m_D3DEffect = Effect.FromFile(MySandboxGameDX.Static.GraphicsDevice, sourceFX, flags);

                try
                {
                    var srcDir = Path.GetDirectoryName(srcAbsolute);
                    m_includeProcessor.Reset(srcDir);
                    var result = ShaderBytecode.CompileFromFile(srcAbsolute, "fx_2_0", flags, null, m_includeProcessor);

                    ShaderBytecode shaderByteCode = result;
                    shaderByteCode.Save(Path.Combine(srcDir, Path.GetFileNameWithoutExtension(srcAbsolute) + ".fxo"));
                    result.Dispose();
                }
                catch (Exception e)
                {
                    Debug.Fail(e.Message);
                    throw;
                }
            }

            using (var fs = MyFileSystem.OpenRead(comPath))
            {
                byte[] m = new byte[fs.Length];
                fs.Read(m, 0, (int)fs.Length);

                m_D3DEffect = Effect.FromMemory(MyRender.GraphicsDevice, m, flags);
            }
            Init();
        }