Skip to content

Commit ff3f8fa

Browse files
Updated usage of SetRenderTargets, ClearRenderTarget and ClearDepthStencil methods
1 parent 2982e66 commit ff3f8fa

30 files changed

Lines changed: 144 additions & 98 deletions

File tree

‎SampleBase/src/SampleApp.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,14 @@ void SampleApp::Render()
638638
if (!m_pImmediateContext)
639639
return;
640640

641-
m_pImmediateContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
641+
ITextureView* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
642+
ITextureView* pDSV = m_pSwapChain->GetDepthBufferDSV();
643+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
642644

643645
m_TheSample->Render();
644646

645647
// Restore default render target in case the sample has changed it
646-
m_pImmediateContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
648+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
647649
if (m_pImGui)
648650
{
649651
m_pImGui->Render(m_pImmediateContext);
@@ -675,7 +677,7 @@ void SampleApp::Present()
675677
while (auto Capture = m_pScreenCapture->GetCapture())
676678
{
677679
MappedTextureSubresource TexData;
678-
m_pImmediateContext->MapTextureSubresource(Capture.pTexture, 0, 0, MAP_READ, MAP_FLAG_DO_NOT_SYNCHRONIZE, nullptr, TexData);
680+
m_pImmediateContext->MapTextureSubresource(Capture.pTexture, 0, 0, MAP_READ, MAP_FLAG_DO_NOT_WAIT, nullptr, TexData);
679681
const auto& TexDesc = Capture.pTexture->GetDesc();
680682

681683
Image::EncodeInfo Info;

‎Samples/Atmosphere/src/AtmosphereSample.cpp‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,6 @@ void AtmosphereSample::RenderShadowMap(IDeviceContext* pContext,
529529

530530
m_EarthHemisphere.Render(m_pImmediateContext, m_TerrainRenderParams, m_f3CameraPos, WorldToLightProjSpaceMatr, nullptr, nullptr, nullptr, true);
531531
}
532-
533-
pContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
534532
}
535533

536534

@@ -572,26 +570,21 @@ void AtmosphereSample::Render()
572570
// render targets
573571
auto* pAmbientSkyLightSRV = m_pLightSctrPP->GetAmbientSkyLightSRV(m_pDevice, m_pImmediateContext);
574572

575-
m_pImmediateContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
573+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
574+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
575+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
576576

577577
const float ClearColor[] = {0.350f, 0.350f, 0.350f, 1.0f};
578578
const float Zero[] = {0.f, 0.f, 0.f, 0.f};
579-
m_pImmediateContext->ClearRenderTarget(nullptr, m_bEnableLightScattering ? Zero : ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
579+
m_pImmediateContext->ClearRenderTarget(pRTV, m_bEnableLightScattering ? Zero : ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
580580

581-
ITextureView *pRTV = nullptr, *pDSV = nullptr;
582581
if (m_bEnableLightScattering)
583582
{
584583
pRTV = m_pOffscreenColorBuffer->GetDefaultView(TEXTURE_VIEW_RENDER_TARGET);
585584
pDSV = m_pOffscreenDepthBuffer->GetDefaultView(TEXTURE_VIEW_DEPTH_STENCIL);
586585
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
587586
m_pImmediateContext->ClearRenderTarget(pRTV, Zero, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
588587
}
589-
else
590-
{
591-
pRTV = nullptr;
592-
pDSV = nullptr;
593-
m_pImmediateContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
594-
}
595588

596589
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
597590

@@ -658,8 +651,9 @@ void AtmosphereSample::Render()
658651
FrameAttribs.ptex2DSrcColorBufferRTV = m_pOffscreenColorBuffer->GetDefaultView(TEXTURE_VIEW_RENDER_TARGET);
659652
FrameAttribs.ptex2DSrcDepthBufferSRV = m_pOffscreenDepthBuffer->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
660653
FrameAttribs.ptex2DSrcDepthBufferDSV = m_pOffscreenDepthBuffer->GetDefaultView(TEXTURE_VIEW_DEPTH_STENCIL);
654+
FrameAttribs.ptex2DDstColorBufferRTV = m_pSwapChain->GetCurrentBackBufferRTV();
655+
FrameAttribs.ptex2DDstDepthBufferDSV = m_pSwapChain->GetDepthBufferDSV();
661656
FrameAttribs.ptex2DShadowMapSRV = m_ShadowMapMgr.GetSRV();
662-
FrameAttribs.pDstRTV = nullptr; // mpBackBufferRTV;
663657

664658
// Perform the post processing
665659
m_pLightSctrPP->PerformPostProcessing(FrameAttribs, m_PPAttribs);

‎Samples/Atmosphere/src/Terrain/EarthHemisphere.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,6 @@ void EarthHemsiphere::RenderNormalMap(IRenderDevice* pDevice,
633633

634634
// Remove elevation map from resource mapping to release the resource
635635
m_pResMapping->RemoveResourceByName("g_tex2DElevationMap");
636-
637-
// Restore default render target
638-
pContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
639636
}
640637

641638

‎Samples/GLTFViewer/src/GLTFViewer.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ GLTFViewer::~GLTFViewer()
368368
// Render a frame
369369
void GLTFViewer::Render()
370370
{
371+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
372+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
371373
// Clear the back buffer
372374
const float ClearColor[] = {0.032f, 0.032f, 0.032f, 1.0f};
373-
m_pImmediateContext->ClearRenderTarget(nullptr, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
374-
m_pImmediateContext->ClearDepthStencil(nullptr, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
375+
m_pImmediateContext->ClearRenderTarget(pRTV, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
376+
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
375377

376378
float4x4 CameraView = m_CameraRotation.ToMatrix() * float4x4::Translation(0.f, 0.0f, m_CameraDist);
377379
float4x4 CameraWorld = CameraView.Inverse();

‎Samples/ImguiDemo/src/ImguiDemo.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void ImguiDemo::UpdateUI()
8383
// Render a frame
8484
void ImguiDemo::Render()
8585
{
86-
m_pImmediateContext->ClearRenderTarget(nullptr, &m_ClearColor.x, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
86+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
87+
m_pImmediateContext->ClearRenderTarget(pRTV, &m_ClearColor.x, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
8788
}
8889

8990

‎Samples/NuklearDemo/src/NuklearDemo.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ void NuklearDemo::UpdateUI()
8181
// Render a frame
8282
void NuklearDemo::Render()
8383
{
84+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
85+
8486
const float4 ClearColor = {0.45f, 0.55f, 0.60f, 1.00f};
85-
m_pImmediateContext->ClearRenderTarget(nullptr, &ClearColor.x, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
87+
m_pImmediateContext->ClearRenderTarget(pRTV, &ClearColor.x, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
8688

8789
nk_diligent_render(m_pNkDlgCtx, m_pImmediateContext, NK_ANTI_ALIASING_ON);
8890
}

‎Samples/Shadows/src/ShadowsSample.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,14 @@ void ShadowsSample::Render()
562562
RenderShadowMap();
563563

564564
// Reset default framebuffer
565-
m_pImmediateContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
565+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
566+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
567+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
568+
566569
// Clear the back buffer
567570
const float ClearColor[] = {0.23f, 0.5f, 0.74f, 1.0f};
568-
m_pImmediateContext->ClearRenderTarget(nullptr, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
569-
m_pImmediateContext->ClearDepthStencil(nullptr, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
571+
m_pImmediateContext->ClearRenderTarget(pRTV, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
572+
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
570573

571574
{
572575
MapHelper<LightAttribs> LightData(m_pImmediateContext, m_LightAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD);

‎Tutorials/Tutorial00_HelloLinux/src/Tutorial00_HelloLinux.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,17 @@ class Tutorial00App
272272

273273
void Render()
274274
{
275+
// Set render targets before issuing any draw command.
276+
// Note that Present() unbinds the back buffer if it is set as render target.
277+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
278+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
279+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
280+
275281
// Clear the back buffer
276282
const float ClearColor[] = {0.350f, 0.350f, 0.350f, 1.0f};
277-
m_pImmediateContext->ClearRenderTarget(nullptr, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
278-
m_pImmediateContext->ClearDepthStencil(nullptr, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
283+
// Let the engine perform required state transitions
284+
m_pImmediateContext->ClearRenderTarget(pRTV, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
285+
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
279286

280287
// Set pipeline state in the immediate context
281288
m_pImmediateContext->SetPipelineState(m_pPSO);

‎Tutorials/Tutorial00_HelloWin32/src/Tutorial00_HelloWin32.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ class Tutorial00App
168168
# endif
169169
EngineD3D12CreateInfo EngineCI;
170170
# ifdef _DEBUG
171-
EngineCI.EnableDebugLayer = true;
171+
// There is a bug in D3D12 debug layer that causes memory leaks in this tutorial
172+
//EngineCI.EnableDebugLayer = true;
172173
# endif
173174
auto* pFactoryD3D12 = GetEngineFactoryD3D12();
174175
pFactoryD3D12->CreateDeviceAndContextsD3D12(EngineCI, &m_pDevice, &m_pImmediateContext);
@@ -355,11 +356,17 @@ class Tutorial00App
355356

356357
void Render()
357358
{
359+
// Set render targets before issuing any draw command.
360+
// Note that Present() unbinds the back buffer if it is set as render target.
361+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
362+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
363+
m_pImmediateContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
364+
358365
// Clear the back buffer
359366
const float ClearColor[] = {0.350f, 0.350f, 0.350f, 1.0f};
360367
// Let the engine perform required state transitions
361-
m_pImmediateContext->ClearRenderTarget(nullptr, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
362-
m_pImmediateContext->ClearDepthStencil(nullptr, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
368+
m_pImmediateContext->ClearRenderTarget(pRTV, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
369+
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
363370

364371
// Set the pipeline state in the immediate context
365372
m_pImmediateContext->SetPipelineState(m_pPSO);

‎Tutorials/Tutorial01_HelloTriangle/readme.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,16 @@ Before rendering anything on the screen we want to clear it:
178178
179179
```cpp
180180
const float ClearColor[] = { 0.350f, 0.350f, 0.350f, 1.0f };
181-
m_pImmediateContext->ClearRenderTarget(nullptr, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
182-
m_pImmediateContext->ClearDepthStencil(nullptr, CLEAR_DEPTH_FLAG, 1.f, 0,
181+
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
182+
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
183+
m_pImmediateContext->ClearRenderTarget(pRTV, ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
184+
m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f, 0,
183185
RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
184186
```
185187

186-
Passing `nullptr` makes the engine clear default (i.e. the swap chain's) render
187-
and depth buffers. Clearing the depth buffer is not really necessary, but we will
188-
keep it here for consistency. Using `RESOURCE_STATE_TRANSITION_MODE_TRANSITION` flag tells the
189-
engine to perform required state transitions.
188+
Clearing the depth buffer is not really necessary, but we will keep it here for consistency.
189+
Using `RESOURCE_STATE_TRANSITION_MODE_TRANSITION` flag tells the engine to perform required
190+
state transitions.
190191

191192
Next, we need to set our pipeline state in the immediate device context:
192193

0 commit comments

Comments
 (0)