Skip to content

Commit

Permalink
SampleApp: make the sample release swap chain buffers when changing m…
Browse files Browse the repository at this point in the history
…ax frame latency
  • Loading branch information
TheMostDiligent committed Feb 10, 2025
1 parent ffcaacc commit 5fa305c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
12 changes: 10 additions & 2 deletions SampleBase/include/SampleBase.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2025 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -171,7 +171,15 @@ class SampleBase

virtual void Render() = 0;
virtual void Update(double CurrTime, double ElapsedTime) = 0;
virtual void PreWindowResize() {}

/// Called by the framework to let the application release any references to the swap chain buffers.
///
/// \remarks In Direct3D11 and Direct3D12, the swap chain must be released before a new swap chain can be created, see
/// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-flush#deferred-destruction-issues-with-flip-presentation-swap-chains.
/// If the application keeps references to the back buffer, it must implement this function to release
/// these references.
virtual void ReleaseSwapChainBuffers() {}

virtual void WindowResize(Uint32 Width, Uint32 Height) {}
virtual bool HandleNativeMessage(const void* pNativeMsgData) { return false; }

Expand Down
3 changes: 2 additions & 1 deletion SampleBase/src/SampleApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ void SampleApp::UpdateAdaptersDialog()
auto NumFrameLatencyItems = std::max(std::max(m_MaxFrameLatency, SCDesc.BufferCount), Uint32{4});
if (ImGui::Combo("Max frame latency", &m_MaxFrameLatency, FrameLatencies, NumFrameLatencyItems))
{
m_TheSample->ReleaseSwapChainBuffers();
m_pSwapChain->SetMaximumFrameLatency(m_MaxFrameLatency);
}
}
Expand Down Expand Up @@ -868,7 +869,7 @@ void SampleApp::WindowResize(int width, int height)
{
if (m_pSwapChain)
{
m_TheSample->PreWindowResize();
m_TheSample->ReleaseSwapChainBuffers();
m_pSwapChain->Resize(width, height);
auto SCWidth = m_pSwapChain->GetDesc().Width;
auto SCHeight = m_pSwapChain->GetDesc().Height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,17 @@ void Tutorial19_RenderPasses::ReleaseWindowResources()
m_pAmbientLightSRB.Release();
}

void Tutorial19_RenderPasses::PreWindowResize()
void Tutorial19_RenderPasses::ReleaseSwapChainBuffers()
{
// In Direct3D11, all references to the swap chain must be released
// before the swap chain can be resized. WindowResize() is called
// after the swap chain has been resized.
ReleaseWindowResources();
m_FramebufferCache.clear();
}

void Tutorial19_RenderPasses::WindowResize(Uint32 Width, Uint32 Height)
{
// On Android, PreWindowResize() is never called because
// On Android, ReleaseSwapChainBuffers() is never called because
// there is no robust way to detect window size change.
ReleaseWindowResources();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2025 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -48,7 +48,7 @@ class Tutorial19_RenderPasses final : public SampleBase

virtual const Char* GetSampleName() const override final { return "Tutorial19: Render Passes"; }

virtual void PreWindowResize() override final;
virtual void ReleaseSwapChainBuffers() override final;
virtual void WindowResize(Uint32 Width, Uint32 Height) override final;

private:
Expand Down

0 comments on commit 5fa305c

Please sign in to comment.