Esempio n. 1
0
        public void InitPass(CRenderContext RHICtx, Graphics.CGfxEffect effect, Graphics.CGfxShadingEnv shadingEnv, Graphics.Mesh.CGfxMtlMesh mesh, UInt32 index)
        {
            if (IsEffectValid == 2)
            {
                if (Effect != effect)
                {
                    Effect = effect;
                    if (Effect.CacheData.PerFrameId != UInt32.MaxValue)
                    {
                        BindCBuffer(Effect.ShaderProgram, Effect.CacheData.PerFrameId, CEngine.Instance.PerFrameCBuffer);
                    }
                }
            }
            ShadingEnv = shadingEnv;
            Effect     = effect;
            MtlMesh    = mesh;
            MeshIndex  = index;

            if (mRenderPipeline == null)
            {
                var RplDesc = new CRenderPipelineDesc();
                RenderPipeline = RHICtx.CreateRenderPipeline(RplDesc);
            }

            this.BindGeometry(MtlMesh.mRootSceneMesh.MeshPrimitives, index, 0);
        }
Esempio n. 2
0
        public async System.Threading.Tasks.Task <bool> Init(CRenderContext rc)
        {
            if (CEngine.Instance.MaterialManager.DefaultMaterial == null ||
                CEngine.Instance.ShadingEnvManager.DefaultShadingEnv == null)
            {
                return(false);
            }
            var desc = CGfxEffectDesc.CreateDesc(CEngine.Instance.MaterialManager.DefaultMaterial,
                                                 new Mesh.CGfxMdfQueue(),
                                                 CEngine.Instance.ShadingEnvManager.DefaultShadingEnv.EnvCode);

            mDefaultEffect = GetEffect(rc, desc);
            await mDefaultEffect.AwaitLoad();

            if (mDefaultEffect.CacheData.CBID_View == UInt32.MaxValue)
            {
                Profiler.Log.WriteLine(Profiler.ELogTag.Fatal, "Shader", $"DefaultEffect({desc.EnvShaderPatch.ShaderName}) CBID_View is invalid");
                return(false);
            }
            if (mDefaultEffect.CacheData.CBID_Camera == UInt32.MaxValue)
            {
                Profiler.Log.WriteLine(Profiler.ELogTag.Fatal, "Shader", $"DefaultEffect({desc.EnvShaderPatch.ShaderName}) CBID_Camera is invalid");
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
 public CGfxEffect GetEffect(CRenderContext rc, CGfxEffectDesc desc)
 {
     lock (Effects)
     {
         CGfxEffect result;
         var        saved = desc.String;
         var        hash  = desc.GetHash64();
         if (Effects.TryGetValue(hash, out result) == true)
         {
             if (hash != result.Desc.GetHash64())
             {
                 Effects.Remove(hash);
                 hash          = result.Desc.GetHash64();
                 Effects[hash] = result;
                 //System.Diagnostics.Debug.Assert(false);
             }
             return(result);
         }
         else
         {
             result = new CGfxEffect(desc);
             result.PreUse();
             Effects.Add(desc.GetHash64(), result);
             return(result);
         }
     }
 }
Esempio n. 4
0
 public void RegEffect(Hash64 hash, CGfxEffect effect)
 {
     lock (Effects)
     {
         if (effect.Desc.GetHash64() != hash)
         {
             hash = effect.Desc.GetHash64();
         }
         Effects[hash] = effect;
     }
 }
Esempio n. 5
0
        public static async System.Threading.Tasks.Task LoadAllShaders(CRenderContext rc)
        {
            var sm         = CRenderContext.ShaderModelString;
            var shaderPath = CEngine.Instance.FileManager.DDCDirectory + sm + "/";
            var shaders    = CEngine.Instance.FileManager.GetFiles(shaderPath, "*.shader");

            Profiler.Log.WriteLine(Profiler.ELogTag.Info, "LoadShaders", $"Begin LoadShaders Number = {shaders.Count}");
            var t1  = Support.Time.HighPrecision_GetTickCount();
            var smp = Thread.ASyncSemaphore.CreateSemaphore(shaders.Count);

            foreach (var i in shaders)
            {
                if (MultiThreadCompile)
                {
                    CEngine.Instance.EventPoster.RunOn(async() =>
                    {
                        var fileName = CEngine.Instance.FileManager.GetPureFileFromFullName(i, false);
                        Hash64 hash  = Hash64.TryParse(fileName);
                        var effect   = new CGfxEffect();
                        if (await effect.LoadFromXndAsync(rc, hash) == 0)
                        {
                            CEngine.Instance.EffectManager.RegEffect(hash, effect);
                        }
                        else
                        {
                            CEngine.Instance.FileManager.DeleteFile(shaderPath + hash.ToString() + ".shader");
                        }
                        smp.Release();
                        return(true);
                    }, Thread.Async.EAsyncTarget.TPools);
                }
                else
                {
                    var    fileName = CEngine.Instance.FileManager.GetPureFileFromFullName(i, false);
                    Hash64 hash     = Hash64.TryParse(fileName);
                    var    effect   = new CGfxEffect();
                    if (await effect.LoadFromXndAsync(rc, hash) == 0)
                    {
                        CEngine.Instance.EffectManager.RegEffect(hash, effect);
                    }
                    else
                    {
                        CEngine.Instance.FileManager.DeleteFile(shaderPath + hash.ToString() + ".shader");
                    }
                    smp.Release();
                }
            }
            await smp.Await();

            var t2 = Support.Time.HighPrecision_GetTickCount();

            Profiler.Log.WriteLine(Profiler.ELogTag.Info, "LoadShaders", $"End LoadShaders, Times = {(t2 - t1) / 1000} ms");
        }
Esempio n. 6
0
        public static async System.Threading.Tasks.Task BuildCachesWhenCleaned(CRenderContext rc)
        {
            var sm         = CRenderContext.ShaderModelString;
            var shaderPath = CEngine.Instance.FileManager.DDCDirectory + sm + "/";
            var shaders    = CEngine.Instance.FileManager.GetFiles(shaderPath, "*.shader");

            if (shaders != null && shaders.Count > 0)
            {
                return;
            }
            var t1 = Support.Time.HighPrecision_GetTickCount();

            Profiler.Log.WriteLine(Profiler.ELogTag.Info, "BuildShaderCache", "Begin Shader Cache");
            var path  = CEngine.Instance.FileManager.DDCDirectory + "shaderinfo";
            var files = CEngine.Instance.FileManager.GetFiles(path, "*.xml");
            var descs = new List <CGfxEffectDesc>(files.Count);

            foreach (var i in files)
            {
                using (var xml = IO.XmlHolder.LoadXML(i))
                {
                    CGfxEffectDesc desc = await CGfxEffectDesc.LoadEffectDescFromXml(rc, xml.RootNode);

                    if (desc != null && desc.MdfQueueShaderPatch != null)
                    {
                        descs.Add(desc);
                    }
                }
            }

            if (MultiThreadCompile == false)
            {
                foreach (var i in descs)
                {
                    CGfxEffect effect = new CGfxEffect(i);
                    if (effect.CreateEffectByD11Editor(rc, i, EPlatformType.PLATFORM_WIN))
                    {
                        CEngine.Instance.EffectManager.RegEffect(i.GetHash64(), effect);
                    }
                }
            }
            else
            {
                var DescNum = new Thread.ThreadSafeNumber(descs.Count);
                await CEngine.Instance.EventPoster.AwaitMTS_Foreach(descs.Count, (idx, smp) =>
                {
                    var remain        = DescNum.Release();
                    CGfxEffect effect = new CGfxEffect(descs[idx]);
                    if (effect.CreateEffectByD11Editor(rc, descs[idx], EPlatformType.PLATFORM_WIN))
                    {
                        Profiler.Log.WriteLine(Profiler.ELogTag.Info, "BuildShaderCache", $"CacheShader = {descs[idx].GetHash64()}; Remain = {remain}");
                        CEngine.Instance.EffectManager.RegEffect(descs[idx].GetHash64(), effect);
                    }
                    else
                    {
                        Profiler.Log.WriteLine(Profiler.ELogTag.Info, "BuildShaderCache", $"CacheShader Failed = {descs[idx].GetHash64()}; Remain = {remain}");
                    }
                });
            }
            var t2 = Support.Time.HighPrecision_GetTickCount();

            Profiler.Log.WriteLine(Profiler.ELogTag.Info, "BuildShaderCache", $"End Shader Cache, Times = {(t2 - t1) / 1000} ms");
            System.GC.Collect();
        }