Diligent Engine  v.2.4.g
SwapChainVkImpl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include "EngineVkImplTraits.hpp"
34 #include "SwapChainVk.h"
35 #include "SwapChainBase.hpp"
38 #include "ManagedVulkanObject.hpp"
39 
40 namespace Diligent
41 {
42 
44 class SwapChainVkImpl final : public SwapChainBase<ISwapChainVk>
45 {
46 public:
48 
49  SwapChainVkImpl(IReferenceCounters* pRefCounters,
51  class RenderDeviceVkImpl* pRenderDeviceVk,
52  class DeviceContextVkImpl* pDeviceContextVk,
53  const NativeWindow& Window);
55 
57 
58 
59  virtual void DILIGENT_CALL_TYPE Present(Uint32 SyncInterval) override final;
60 
62  virtual void DILIGENT_CALL_TYPE Resize(Uint32 NewWidth, Uint32 NewHeight, SURFACE_TRANSFORM NewPreTransform) override final;
63 
65  virtual void DILIGENT_CALL_TYPE SetFullscreenMode(const DisplayModeAttribs& DisplayMode) override final;
66 
68  virtual void DILIGENT_CALL_TYPE SetWindowedMode() override final;
69 
71  virtual VkSwapchainKHR DILIGENT_CALL_TYPE GetVkSwapChain() override final { return m_VkSwapChain; }
72 
75  {
76  VERIFY_EXPR(m_BackBufferIndex >= 0 && m_BackBufferIndex < m_SwapChainDesc.BufferCount);
77  return m_pBackBufferRTV[m_BackBufferIndex];
78  }
79 
81  virtual ITextureViewVk* DILIGENT_CALL_TYPE GetDepthBufferDSV() override final { return m_pDepthBufferDSV; }
82 
83 private:
84  void CreateSurface();
85  void CreateVulkanSwapChain();
86  void InitBuffersAndViews();
87  VkResult AcquireNextImage(DeviceContextVkImpl* pDeviceCtxVk);
88  void RecreateVulkanSwapchain(DeviceContextVkImpl* pImmediateCtxVk);
89  void WaitForImageAcquiredFences();
90  void ReleaseSwapChainResources(DeviceContextVkImpl* pImmediateCtxVk, bool DestroyVkSwapChain);
91 
92  const NativeWindow m_Window;
93 
94  std::shared_ptr<const VulkanUtilities::VulkanInstance> m_VulkanInstance;
95 
96  Uint32 m_DesiredBufferCount = 0;
97 
98  VkSurfaceKHR m_VkSurface = VK_NULL_HANDLE;
99  VkSwapchainKHR m_VkSwapChain = VK_NULL_HANDLE;
100  VkFormat m_VkColorFormat = VK_FORMAT_UNDEFINED;
101 
102 #if PLATFORM_ANDROID
103  // Surface extent corresponding to identity transform. We have to store this value,
104  // because on Android vkGetPhysicalDeviceSurfaceCapabilitiesKHR is not reliable and
105  // starts reporting incorrect dimensions after few rotations.
106  VkExtent2D m_SurfaceIdentityExtent = {};
107 
108  // Keep track of current surface transform to detect orientation changes.
109  VkSurfaceTransformFlagBitsKHR m_CurrentSurfaceTransform = VK_SURFACE_TRANSFORM_FLAG_BITS_MAX_ENUM_KHR;
110 #endif
111 
112  std::vector<RefCntAutoPtr<ManagedSemaphore>> m_ImageAcquiredSemaphores;
113  std::vector<RefCntAutoPtr<ManagedSemaphore>> m_DrawCompleteSemaphores;
114  std::vector<VulkanUtilities::FenceWrapper> m_ImageAcquiredFences;
115 
116  std::vector<RefCntAutoPtr<ITextureViewVk>, STDAllocatorRawMem<RefCntAutoPtr<ITextureViewVk>>> m_pBackBufferRTV;
117 
118  std::vector<bool, STDAllocatorRawMem<bool>> m_SwapChainImagesInitialized;
119  std::vector<bool, STDAllocatorRawMem<bool>> m_ImageAcquiredFenceSubmitted;
120 
121  RefCntAutoPtr<ITextureViewVk> m_pDepthBufferDSV;
122 
123  Uint32 m_SemaphoreIndex = 0;
124  uint32_t m_BackBufferIndex = 0;
125  bool m_IsMinimized = false;
126  bool m_VSyncEnabled = true;
127 };
128 
129 } // namespace Diligent
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
Diligent::RenderDeviceVkImpl
Render device implementation in Vulkan backend.
Definition: RenderDeviceVkImpl.hpp:58
Diligent::SwapChainVkImpl::Present
virtual void Present(Uint32 SyncInterval) override final
Implementation of ISwapChain::Present() in Vulkan backend.
Definition: SwapChainVkImpl.cpp:680
SwapChainBase.hpp
Diligent::SwapChainVkImpl::GetCurrentBackBufferRTV
virtual ITextureViewVk * GetCurrentBackBufferRTV() override final
Implementation of ISwapChain::GetCurrentBackBufferRTV() in Vulkan backend.
Definition: SwapChainVkImpl.hpp:74
Diligent::SwapChainVkImpl::Resize
virtual void Resize(Uint32 NewWidth, Uint32 NewHeight, SURFACE_TRANSFORM NewPreTransform) override final
Implementation of ISwapChain::Resize() in Vulkan backend.
Definition: SwapChainVkImpl.cpp:870
Diligent::SURFACE_TRANSFORM
SURFACE_TRANSFORM
The transform applied to the image content prior to presentation.
Definition: GraphicsTypes.h:1315
Diligent::SwapChainVkImpl::SetWindowedMode
virtual void SetWindowedMode() override final
Implementation of ISwapChain::SetWindowedMode() in Vulkan backend.
Definition: SwapChainVkImpl.cpp:973
SwapChainVk.h
IMPLEMENT_QUERY_INTERFACE_IN_PLACE
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
ManagedVulkanObject.hpp
Diligent::DisplayModeAttribs
Display mode attributes.
Definition: GraphicsTypes.h:1269
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::SwapChainDesc::BufferCount
Uint32 BufferCount
The number of buffers in the swap chain.
Definition: GraphicsTypes.h:1379
Diligent::SwapChainBase
Base implementation of the swap chain.
Definition: SwapChainBase.hpp:51
Diligent::SwapChainVkImpl::~SwapChainVkImpl
~SwapChainVkImpl()
Definition: SwapChainVkImpl.cpp:526
Diligent::SwapChainDesc
Swap chain description.
Definition: GraphicsTypes.h:1347
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::SwapChainVkImpl::GetDepthBufferDSV
virtual ITextureViewVk * GetDepthBufferDSV() override final
Implementation of ISwapChain::GetDepthBufferDSV() in Vulkan backend.
Definition: SwapChainVkImpl.hpp:81
Diligent::DeviceContextVkImpl
Device context implementation in Vulkan backend.
Definition: DeviceContextVkImpl.hpp:67
Diligent::SwapChainVkImpl::SetFullscreenMode
virtual void SetFullscreenMode(const DisplayModeAttribs &DisplayMode) override final
Implementation of ISwapChain::SetFullscreenMode() in Vulkan backend.
Definition: SwapChainVkImpl.cpp:969
Diligent::SwapChainBase< ISwapChainVk >::m_SwapChainDesc
SwapChainDesc m_SwapChainDesc
Swap chain description.
Definition: SwapChainBase.hpp:122
Diligent::SwapChainVkImpl
Swap chain implementation in Vulkan backend.
Definition: SwapChainVkImpl.hpp:44
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::SwapChainVkImpl::GetVkSwapChain
virtual VkSwapchainKHR GetVkSwapChain() override final
Implementation of ISwapChainVk::GetVkSwapChain().
Definition: SwapChainVkImpl.hpp:71
VulkanObjectWrappers.hpp
EngineVkImplTraits.hpp
Diligent::ITextureViewVk
Exposes Vulkan-specific functionality of a texture view object.
Definition: TextureViewVk.h:51
Diligent::SwapChainVkImpl::SwapChainVkImpl
SwapChainVkImpl(IReferenceCounters *pRefCounters, const SwapChainDesc &SwapChainDesc, class RenderDeviceVkImpl *pRenderDeviceVk, class DeviceContextVkImpl *pDeviceContextVk, const NativeWindow &Window)
Definition: SwapChainVkImpl.cpp:39
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
VulkanInstance.hpp