public bool InitByEffects(CRenderContext RHICtx, CGfxMesh SceneMesh, UInt32 index, CGfxMaterialInstance mtl_inst, CGfxEffect[] effects, CGfxShadingEnv[] EnvShaderArray, bool preUseEffect) { MtlInst = mtl_inst; mRootSceneMesh = SceneMesh; mAtomIndex = index; if (mPrebuildPassArray == null) { mPrebuildPassArray = new CPass[(int)PrebuildPassIndex.PPI_Num]; } for (int ppi = 0; ppi < (int)PrebuildPassIndex.PPI_Num; ppi++) { var effect = effects[ppi]; if (effect == null) { continue; } CPass refPass = null; if (mPrebuildPassArray[ppi] != null) { refPass = mPrebuildPassArray[ppi]; } else { refPass = RHICtx.CreatePass(); } refPass.InitPass(RHICtx, effect, EnvShaderArray[ppi], this, index); refPass.PassIndex = (PrebuildPassIndex)ppi; //如果Effect已经加载完成,那么设置Pass状态 //否则就要等渲染的时候Pass.PreUse来间接触发了 // if (refPass.Effect.IsValid) { FillPassData(RHICtx, refPass, effect, (PrebuildPassIndex)ppi, true); } else { if (preUseEffect) { effect.PreUse((successed) => { if (successed == false) { return; } FillPassData(RHICtx, refPass, effect, (PrebuildPassIndex)ppi); }); } } //这里一旦PreUse了,那就把所有shader都在初始化的时候就加载了 //effect.PreUse((successed) => //{ // if(successed==false) // { // return; // } // FillPassData(RHICtx, refPass, effect, (PrebuildPassIndex)ppi); //}); mPrebuildPassArray[ppi] = refPass; } return(true); }
public async System.Threading.Tasks.Task <bool> Init(CRenderContext RHICtx, CSwapChain SwapChain, CGfxScreenViewDesc ViewInfo, CGfxShadingEnv ShadingEnv, CGfxMaterialInstance MtlInst, CGfxMesh ViewportMesh) { if (ShadingEnv == null) { return(false); } if (ViewportMesh == null) { var ScreenAlignedTriangle = CEngine.Instance.MeshPrimitivesManager.GetMeshPrimitives(RHICtx, CEngineDesc.ScreenAlignedTriangleName, true); ViewportMesh = CEngine.Instance.MeshManager.CreateMesh(RHICtx, ScreenAlignedTriangle); ViewportMesh.SetMaterialInstance(RHICtx, 0, await CEngine.Instance.MaterialInstanceManager.GetMaterialInstanceAsync(RHICtx, RName.GetRName("Material/defaultmaterial.instmtl")), CEngine.Instance.PrebuildPassData.DefaultShadingEnvs); //await ViewportMesh.AwaitEffects(); } mViewInfo = ViewInfo; mViewport = new CViewport(); mViewport.Width = ViewInfo.Width; mViewport.Height = ViewInfo.Height; mViewport.TopLeftX = 0.0f; mViewport.TopLeftY = 0.0f; mViewport.MinDepth = 0.0f; mViewport.MaxDepth = 1.0f; var ShaderProgram = CEngine.Instance.EffectManager.DefaultEffect.ShaderProgram; mScreenViewCB = RHICtx.CreateConstantBuffer(ShaderProgram, CEngine.Instance.EffectManager.DefaultEffect.CacheData.CBID_View); if (mScreenViewCB == null) { return(false); } mIDViewportSizeAndRcp = mScreenViewCB.FindVar("gViewportSizeAndRcp"); mID_SunPosNDC = mScreenViewCB.FindVar("gSunPosNDC"); mID_DirLightColor_Intensity = mScreenViewCB.FindVar("gDirLightColor_Intensity"); mID_AoParam = mScreenViewCB.FindVar("gAoParam"); CRasterizerStateDesc RSDesc = new CRasterizerStateDesc(); RSDesc.InitForCustom(); mRasterState = CEngine.Instance.RasterizerStateManager.GetRasterizerState(RHICtx, RSDesc); CDepthStencilStateDesc DSSDesc = new CDepthStencilStateDesc(); DSSDesc.InitForCustomLayers(); mDepthStencilState = CEngine.Instance.DepthStencilStateManager.GetDepthStencilState(RHICtx, DSSDesc); CBlendStateDesc BlendDesc = new CBlendStateDesc(); BlendDesc.InitForCustomLayers(); mBlendState = CEngine.Instance.BlendStateManager.GetBlendState(RHICtx, BlendDesc); mPass = RHICtx.CreatePass(); if (false == await mPass.InitPassForViewportView(RHICtx, ShadingEnv, MtlInst, ViewportMesh)) { return(false); } return(OnResize(RHICtx, SwapChain, ViewInfo.Width, ViewInfo.Height)); }
public async System.Threading.Tasks.Task <bool> InitForMultiPassMode(CRenderContext RHICtx, CGfxScreenViewDesc ViewInfo, List <CGfxShadingEnv> ShadingEnvArray, CGfxMaterialInstance MtlInst, CGfxMesh ViewportMesh) { if (ShadingEnvArray == null) { return(false); } if (ViewportMesh == null) { var RectMesh = CEngine.Instance.MeshPrimitivesManager.GetMeshPrimitives(RHICtx, CEngineDesc.FullScreenRectName, true); ViewportMesh = CEngine.Instance.MeshManager.CreateMesh(RHICtx, RectMesh); ViewportMesh.SetMaterialInstance(RHICtx, 0, await CEngine.Instance.MaterialInstanceManager.GetMaterialInstanceAsync(RHICtx, RName.GetRName("Material/defaultmaterial.instmtl")), CEngine.Instance.PrebuildPassData.DefaultShadingEnvs); //await ViewportMesh.AwaitEffects(); } mViewInfo = ViewInfo; mViewport = new CViewport(); mViewport.Width = ViewInfo.Width; mViewport.Height = ViewInfo.Height; mViewport.TopLeftX = 0.0f; mViewport.TopLeftY = 0.0f; mViewport.MinDepth = 0.0f; mViewport.MaxDepth = 1.0f; var ShaderProgram = CEngine.Instance.EffectManager.DefaultEffect.ShaderProgram; mScreenViewCB = RHICtx.CreateConstantBuffer(ShaderProgram, CEngine.Instance.EffectManager.DefaultEffect.CacheData.CBID_View); if (mScreenViewCB == null) { return(false); } mIDViewportSizeAndRcp = mScreenViewCB.FindVar("gViewportSizeAndRcp"); mID_SunPosNDC = mScreenViewCB.FindVar("gSunPosNDC"); mID_AoParam = mScreenViewCB.FindVar("gAoParam"); CRasterizerStateDesc RSDesc = new CRasterizerStateDesc(); RSDesc.InitForCustom(); mRasterState = CEngine.Instance.RasterizerStateManager.GetRasterizerState(RHICtx, RSDesc); CDepthStencilStateDesc DSSDesc = new CDepthStencilStateDesc(); DSSDesc.InitForCustomLayers(); mDepthStencilState = CEngine.Instance.DepthStencilStateManager.GetDepthStencilState(RHICtx, DSSDesc); CBlendStateDesc BlendDesc = new CBlendStateDesc(); BlendDesc.InitForCustomLayers(); mBlendState = CEngine.Instance.BlendStateManager.GetBlendState(RHICtx, BlendDesc); for (UInt32 idx = 0; idx < ShadingEnvArray.Count; idx++) { var refPass = RHICtx.CreatePass(); if (false == await refPass.InitPassForViewportView(RHICtx, ShadingEnvArray[(int)idx], MtlInst, ViewportMesh)) { return(false); } mPassArray.Add(refPass); } return(OnResize(RHICtx, null, ViewInfo.Width, ViewInfo.Height)); }
public async System.Threading.Tasks.Task InitPass(CRenderContext rc, Graphics.CGfxMaterialInstance MtlInst = null, bool isSM3 = false) { if (rc == null) { rc = CEngine.Instance.RenderContext; } if (CRenderContext.ShaderModel >= 4) { UseComputeShader = true; Profiler.Log.WriteLine(Profiler.ELogTag.Info, "Graphics", $"GpuDriven: SM = {CRenderContext.ShaderModel},Use ComputeShader"); } else { UseComputeShader = false; Profiler.Log.WriteLine(Profiler.ELogTag.Info, "Graphics", $"GpuDriven: SM = {CRenderContext.ShaderModel},CPU Culling"); } if (rc.ContextCaps.MaxVertexShaderStorageBlocks == 0) { UseVTF = true; Profiler.Log.WriteLine(Profiler.ELogTag.Info, "Graphics", $"GpuDriven: No SSBO in VertexShader,Use VTF"); } else { UseVTF = false; Profiler.Log.WriteLine(Profiler.ELogTag.Info, "Graphics", $"GpuDriven: Use SSBO in VertexShader"); } if (mMergeInstanceSE == null) { mMergeInstanceSE = mMergeInstanceSE = EngineNS.CEngine.Instance.ShadingEnvManager.GetGfxShadingEnv <CGfxMergeInstanceSE>(); if (UseVTF) { mMergeInstanceSE.SetMacroDefineValue("ENV_USEVTF", "1"); } else { mMergeInstanceSE.SetMacroDefineValue("ENV_USEVTF", "0"); } } if (MtlInst == null) { MtlInst = await CEngine.Instance.MaterialInstanceManager.GetMaterialInstanceAsync(rc, RName.GetRName("Material/defaultmaterial.instmtl")); } if (mPass == null) { var pass = rc.CreatePass(); var meshpri = CEngine.Instance.MeshPrimitivesManager.CreateMeshPrimitives(rc, 1); EngineNS.Graphics.Mesh.CGfxMeshCooker.MakeRect3D(rc, meshpri); //var meshpri = CEngine.Instance.MeshPrimitivesManager.GetMeshPrimitives(rc, CEngineDesc.ScreenAlignedTriangleName, true); var ViewportMesh = CEngine.Instance.MeshManager.CreateMesh(rc, meshpri); ViewportMesh.SetMaterialInstance(rc, 0, MtlInst, CEngine.Instance.PrebuildPassData.DefaultShadingEnvs); var affectLights = new List <GamePlay.SceneGraph.GSceneGraph.AffectLight>(); ViewportMesh.mMeshVars.SetPointLights(affectLights); await pass.InitPassForViewportView(rc, mMergeInstanceSE, MtlInst, ViewportMesh); mPass = pass; var vsDesc = mPass.Effect.ShaderProgram.VertexShader.Desc; var tbInfo = new CTextureBindInfo(); if (vsDesc.GetSRVDesc(vsDesc.FindSRVDesc("AllVertexArray"), ref tbInfo)) { } if (vsDesc.GetSRVDesc(vsDesc.FindSRVDesc("MeshInstanceArray"), ref tbInfo)) { } } //const string CSVersion = "cs_5_0"; var macros = new CShaderDefinitions(); var shaderFile = RName.GetRName("Shaders/Compute/GpuDriven/Cluster.compute", RName.enRNameType.Engine); mCS_MeshBatchDesc = rc.CreateShaderDesc(shaderFile, "CSMain_MeshBatch", EShaderType.EST_ComputeShader, macros, CIPlatform.Instance.PlatformType); mCS_MeshBatch = rc.CreateComputeShader(mCS_MeshBatchDesc); mCS_ClearBatchArgsDesc = rc.CreateShaderDesc(shaderFile, "CSMain_ClearBatchArgs", EShaderType.EST_ComputeShader, macros, CIPlatform.Instance.PlatformType); mCS_ClearBatchArgs = rc.CreateComputeShader(mCS_ClearBatchArgsDesc); var cbIndex = mCS_MeshBatchDesc.FindCBufferDesc("cbMeshBatch"); if (cbIndex != uint.MaxValue) { mCBMeshBatch = rc.CreateConstantBuffer(mCS_MeshBatchDesc, cbIndex); } }