vsg  1.1.0
VulkanSceneGraph library
Swapchain.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/state/ImageView.h>
16 #include <vsg/vk/Fence.h>
17 #include <vsg/vk/Surface.h>
18 
19 namespace vsg
20 {
23  {
24  VkSurfaceCapabilitiesKHR capabilities;
25  std::vector<VkSurfaceFormatKHR> formats;
26  std::vector<VkPresentModeKHR> presentModes;
27  };
28 
29  extern VSG_DECLSPEC SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface);
30  extern VSG_DECLSPEC VkSurfaceFormatKHR selectSwapSurfaceFormat(const SwapChainSupportDetails& details, VkSurfaceFormatKHR preferredSurfaceFormat = {VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
31  extern VSG_DECLSPEC VkExtent2D selectSwapExtent(const SwapChainSupportDetails& details, uint32_t width, uint32_t height);
32  extern VSG_DECLSPEC VkPresentModeKHR selectSwapPresentMode(const SwapChainSupportDetails& details, VkPresentModeKHR preferredPresentMode = VK_PRESENT_MODE_MAILBOX_KHR);
33 
36  {
37  uint32_t imageCount = 3; // default to triple buffering
38  VkSurfaceFormatKHR surfaceFormat = {VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
39  VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
40  VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
41  };
42 
44  class VSG_DECLSPEC Swapchain : public Inherit<Object, Swapchain>
45  {
46  public:
47  Swapchain(PhysicalDevice* physicalDevice, Device* device, Surface* surface, uint32_t width, uint32_t height, SwapchainPreferences& preferences, ref_ptr<Swapchain> oldSwapchain = {});
48 
49  operator VkSwapchainKHR() const { return _swapchain; }
50  VkSwapchainKHR vk() const { return _swapchain; }
51 
52  VkFormat getImageFormat() const { return _format; }
53 
54  const VkExtent2D& getExtent() const { return _extent; }
55 
56  ImageViews& getImageViews() { return _imageViews; }
57  const ImageViews& getImageViews() const { return _imageViews; }
58 
60  VkResult acquireNextImage(uint64_t timeout, ref_ptr<Semaphore> semaphore, ref_ptr<Fence> fence, uint32_t& imageIndex);
61 
62  protected:
63  virtual ~Swapchain();
64 
65  vsg::ref_ptr<Device> _device;
66  vsg::ref_ptr<Surface> _surface;
67  VkSwapchainKHR _swapchain;
68  VkFormat _format;
69  VkExtent2D _extent;
70  ImageViews _imageViews;
71  };
72  VSG_type_name(vsg::Swapchain);
73 
74  constexpr bool operator==(const VkExtent2D& lhs, const VkExtent2D& rhs)
75  {
76  return lhs.width == rhs.width && lhs.height == rhs.height;
77  }
78 
79  constexpr bool operator!=(const VkExtent2D& lhs, const VkExtent2D& rhs)
80  {
81  return lhs.width != rhs.width || lhs.height != rhs.height;
82  }
83 
84 } // namespace vsg
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Definition: Inherit.h:28
Definition: PhysicalDevice.h:23
Surface encapsulates VkSurfaceKHR.
Definition: Surface.h:22
Swapchain encapsulates vkSwapchainKHR.
Definition: Swapchain.h:45
VkResult acquireNextImage(uint64_t timeout, ref_ptr< Semaphore > semaphore, ref_ptr< Fence > fence, uint32_t &imageIndex)
call vkAcquireNextImageKHR
Definition: ref_ptr.h:22
struct for holding available swapchain capabilities available on device
Definition: Swapchain.h:23
Swapchain preferences passed via WindowTraits::swapchainPreferences to guide swapchain creation assoc...
Definition: Swapchain.h:36