Download

How to set custom DepthStencil texture correctly?

For custom shaders, I want to create and set depth stencil buffer with c++. There is my code:

void FDemoRender::RenderDemoInternal(ERHIFeatureLevel::Type RHIFeatureLevel, UTextureRenderTarget2D* RenderTarget)
{
check(IsInRenderingThread());

FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();

FTexture2DRHIRef RT_Texture = RenderTarget->GetRenderTargetResource()->GetRenderTargetTexture();

FRHIRenderTargetView BackBufferView = FRHIRenderTargetView(RT_Texture, ERenderTargetLoadAction::EClear);

FRHIResourceCreateInfo DepthStencilTexCreateInfo(FClearValueBinding(1.0f, 0));

FTexture2DRHIRef DepthStencilTex = RHICmdList.CreateTexture2D(
RenderTarget->SizeX, RenderTarget->SizeY, PF_DepthStencil, 1, 1, TexCreate_None, DepthStencilTexCreateInfo);

FRHIDepthRenderTargetView DepthStencilView(DepthStencilTex,
ERenderTargetLoadAction::EClear, ERenderTargetStoreAction::ENoAction,
ERenderTargetLoadAction::EClear, ERenderTargetStoreAction::ENoAction);

RHICmdList.SetRenderTargetsAndClear(FRHISetRenderTargetsInfo(1, &BackBufferView, DepthStencilView));

}

When I run my project on windows, the program stop at the breakpoint: Source\Runtime\Windows\D3D11RHI\Private\D3D11Commands.cpp:791 ensure(CurrentDepthTexture); the CurrentDepthTexture is null.

How to set custom DepthStencil texture correctly? Thank you.

ps. ue4 version is 4.19.2