--- title: Untitled date: 2024-09-26 18:41:24 excerpt: tags: rating: ⭐ --- # RenderBasePass() 传入RenderBasePass()的DepthStencil逻辑如下: ```c++ const FExclusiveDepthStencil::Type BasePassDepthStencilAccess = bAllowReadOnlyDepthBasePass ? FExclusiveDepthStencil::DepthRead_StencilWrite : FExclusiveDepthStencil::DepthWrite_StencilWrite; ``` FDeferredShadingSceneRenderer::RenderBasePass() => FDeferredShadingSceneRenderer::RenderBasePassInternal() => FBasePassMeshProcessor::TryAddMeshBatch => ## 大致流程 1. 绑定GBuffer与Depth。 ```c++ const FExclusiveDepthStencil ExclusiveDepthStencil(BasePassDepthStencilAccess); TStaticArray BasePassTextures; uint32 BasePassTextureCount = SceneTextures.GetGBufferRenderTargets(BasePassTextures); Strata::AppendStrataMRTs(*this, BasePassTextureCount, BasePassTextures); TArrayView BasePassTexturesView = MakeArrayView(BasePassTextures.GetData(), BasePassTextureCount); FRDGTextureRef BasePassDepthTexture = SceneTextures.Depth.Target; ``` 2. GBuffer Clear ```c++ GraphBuilder.AddPass(RDG_EVENT_NAME("GBufferClear"), PassParameters, ERDGPassFlags::Raster, [PassParameters, ColorLoadAction, SceneColorClearValue](FRHICommandList& RHICmdList) { // If no fast-clear action was used, we need to do an MRT shader clear. if (ColorLoadAction == ERenderTargetLoadAction::ENoAction) { const FRenderTargetBindingSlots& RenderTargets = PassParameters->RenderTargets; FLinearColor ClearColors[MaxSimultaneousRenderTargets]; FRHITexture* Textures[MaxSimultaneousRenderTargets]; int32 TextureIndex = 0; RenderTargets.Enumerate([&](const FRenderTargetBinding& RenderTarget) { FRHITexture* TextureRHI = RenderTarget.GetTexture()->GetRHI(); ClearColors[TextureIndex] = TextureIndex == 0 ? SceneColorClearValue : TextureRHI->GetClearColor(); Textures[TextureIndex] = TextureRHI; ++TextureIndex; }); // Clear color only; depth-stencil is fast cleared. DrawClearQuadMRT(RHICmdList, true, TextureIndex, ClearColors, false, 0, false, 0); } }); ``` 3. # SetDepthStencilStateForBasePass() ```c++ void SetDepthStencilStateForBasePass( FMeshPassProcessorRenderState& DrawRenderState, ERHIFeatureLevel::Type FeatureLevel, bool bDitheredLODTransition, const FMaterial& MaterialResource, bool bEnableReceiveDecalOutput, bool bForceEnableStencilDitherState) { const bool bMaskedInEarlyPass = (MaterialResource.IsMasked() || bDitheredLODTransition) && MaskedInEarlyPass(GShaderPlatformForFeatureLevel[FeatureLevel]); if (bEnableReceiveDecalOutput) { if (bMaskedInEarlyPass) { SetDepthStencilStateForBasePass_Internal(DrawRenderState, FeatureLevel); } else if (DrawRenderState.GetDepthStencilAccess() & FExclusiveDepthStencil::DepthWrite) { SetDepthStencilStateForBasePass_Internal(DrawRenderState, FeatureLevel); } else { SetDepthStencilStateForBasePass_Internal(DrawRenderState, FeatureLevel); } } else if (bMaskedInEarlyPass) { DrawRenderState.SetDepthStencilState(TStaticDepthStencilState::GetRHI()); } if (bForceEnableStencilDitherState) { SetDepthStencilStateForBasePass_Internal(DrawRenderState, FeatureLevel); } } ``` #