Skip to content

Commit a5a85e4

Browse files
Offscreen Swapchain: limit the number of frames in flight to the number of back buffers
1 parent 4cad522 commit a5a85e4

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

Graphics/GraphicsTools/src/OffScreenSwapChain.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,12 @@
2424
* of the possibility of such damages.
2525
*/
2626

27+
#include "OffScreenSwapChain.hpp"
2728

28-
#include "../../../Common/interface/ObjectBase.hpp"
29-
#include "../../../Graphics/GraphicsEngine/include/SwapChainBase.hpp"
30-
#include "../../../Graphics/GraphicsEngine/include/RenderDeviceBase.hpp"
31-
#include "../../../Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp"
29+
#include "ObjectBase.hpp"
30+
#include "SwapChainBase.hpp"
31+
#include "RenderDeviceBase.hpp"
32+
#include "GraphicsAccessories.hpp"
3233

3334
#include "OffScreenSwapChain.hpp"
3435

@@ -46,7 +47,6 @@ class OffScreenSwapChain : public SwapChainBase<ISwapChain>
4647
const SwapChainDesc& SCDesc) :
4748
SwapChainBase{pRefCounters, pDevice, pContext, SCDesc}
4849
{
49-
5050
if (m_DesiredPreTransform != SURFACE_TRANSFORM_OPTIMAL && m_DesiredPreTransform != SURFACE_TRANSFORM_IDENTITY)
5151
{
5252
LOG_WARNING_MESSAGE(GetSurfaceTransformString(m_DesiredPreTransform),
@@ -57,6 +57,19 @@ class OffScreenSwapChain : public SwapChainBase<ISwapChain>
5757
m_DesiredPreTransform = SURFACE_TRANSFORM_OPTIMAL;
5858
m_SwapChainDesc.PreTransform = SURFACE_TRANSFORM_IDENTITY;
5959

60+
const RenderDeviceInfo& DeviceInfo = pDevice->GetDeviceInfo();
61+
62+
if (m_SwapChainDesc.IsPrimary && m_SwapChainDesc.BufferCount > 0 &&
63+
(DeviceInfo.Type == RENDER_DEVICE_TYPE_D3D12 ||
64+
DeviceInfo.Type == RENDER_DEVICE_TYPE_VULKAN ||
65+
DeviceInfo.Type == RENDER_DEVICE_TYPE_METAL))
66+
{
67+
FenceDesc Desc;
68+
Desc.Name = "Off-screen swap chain frame complete fence";
69+
Desc.Type = FENCE_TYPE_CPU_WAIT_ONLY;
70+
pDevice->CreateFence(Desc, &m_FrameCompleteFence);
71+
}
72+
6073
Resize(m_SwapChainDesc.Width, m_SwapChainDesc.Height, m_SwapChainDesc.PreTransform);
6174
}
6275

@@ -75,6 +88,17 @@ class OffScreenSwapChain : public SwapChainBase<ISwapChain>
7588
{
7689
pDeviceContext->FinishFrame();
7790
m_pRenderDevice->ReleaseStaleResources();
91+
92+
if (m_FrameCompleteFence)
93+
{
94+
if (m_FrameNumber > m_SwapChainDesc.BufferCount)
95+
{
96+
// Limit the number of frames in flight to the number of back buffers
97+
m_FrameCompleteFence->Wait(m_FrameNumber - m_SwapChainDesc.BufferCount);
98+
}
99+
pDeviceContext->EnqueueSignal(m_FrameCompleteFence, m_FrameNumber);
100+
}
101+
++m_FrameNumber;
78102
}
79103
}
80104

@@ -157,6 +181,8 @@ class OffScreenSwapChain : public SwapChainBase<ISwapChain>
157181
RefCntAutoPtr<ITexture> m_pDepthBuffer;
158182
RefCntAutoPtr<ITextureView> m_pRTV;
159183
RefCntAutoPtr<ITextureView> m_pDSV;
184+
RefCntAutoPtr<IFence> m_FrameCompleteFence;
185+
Uint64 m_FrameNumber = 1;
160186
};
161187

162188

0 commit comments

Comments
 (0)