nngn
Loading...
Searching...
No Matches
swapchain.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_VULKAN_SWAPCHAIN_H
2#define NNGN_GRAPHICS_VULKAN_SWAPCHAIN_H
3
4#include <cstdint>
5#include <span>
6#include <tuple>
7
8#include "graphics/graphics.h"
9#include "math/vec2.h"
10
11#include "resource.h"
12#include "vulkan.h"
13
14namespace nngn {
15
16class Device;
17class DeviceMemory;
18class Instance;
19
21class SwapChain {
22public:
33 SwapChain(void) = default;
34 ~SwapChain(void);
35 VkSwapchainKHR id(void) const { return this->h; }
36 VkSurfaceKHR surface(void) const { return this->m_surface; }
38 std::span<const VkImageView> img_views() const
39 { return std::span{this->m_img_views}; }
41 std::span<const VkFramebuffer> frame_buffers() const
42 { return std::span{this->m_frame_buffers}; }
44 std::span<const VkFramebuffer> depth_frame_buffers() const
45 { return std::span{this->m_depth_frame_buffers}; }
46 std::size_t cur_frame() const { return this->i_frame; }
48 void set_present_mode(VkPresentModeKHR m) { this->present_mode = m; }
49 void set_surface(VkSurfaceKHR s) { this->m_surface = s; }
50 void init(
51 VkInstance inst, DeviceMemory *dev_mem,
52 VkSurfaceFormatKHR format, VkFormat depth_format,
53 VkPresentModeKHR mode);
55 bool recreate(
56 const Instance &inst, const Device &dev, std::uint32_t n_images,
57 VkRenderPass depth_pass, VkRenderPass render_pass,
58 VkExtent2D extent, VkSurfaceTransformFlagBitsKHR transform,
59 VkExtent2D shadow_map_size, VkExtent2D shadow_cube_size,
60 std::span<const VkImageView> shadow_map,
61 std::span<const VkImageView> shadow_cube);
63 std::pair<PresentContext, VkResult> acquire_img();
64private:
65 bool create_img_views(const Instance &inst);
66 bool create_depth_img(const Instance &inst, VkExtent2D extent);
67 bool create_sync_objects(const Instance &inst);
69 const Instance &inst, VkExtent2D extent, VkRenderPass render_pass);
71 const Instance &inst, VkRenderPass render_pass,
72 VkExtent2D shadow_map_size, VkExtent2D shadow_cube_size,
73 std::span<const VkImageView> shadow_map,
74 std::span<const VkImageView> shadow_cube);
75 void destroy();
76 VkSwapchainKHR h = {};
77 VkInstance instance = {};
78 VkSurfaceKHR m_surface = {};
79 VkDevice dev = {};
81 VkSurfaceFormatKHR format = {};
82 VkPresentModeKHR present_mode = {};
83 std::vector<VkImageView> m_img_views = {};
84 VkFormat depth_format = {};
86 VkImageView depth_img_view = {};
87 struct {
89 std::vector<VkSemaphore> render;
91 std::vector<VkSemaphore> present;
92 } semaphores = {};
93 struct {
95 std::vector<VkFence> render;
101 std::vector<VkFence> present;
102 } fences = {};
103 std::vector<VkFramebuffer> m_frame_buffers = {}, m_depth_frame_buffers = {};
104 std::size_t i_frame = {};
105};
106
109 VkSurfaceTransformFlagBitsKHR cur_transform = {};
110 std::vector<VkSurfaceFormatKHR> formats = {};
111 std::vector<Graphics::PresentMode> present_modes = {};
113 void init(VkSurfaceKHR s, VkPhysicalDevice d);
115 VkSurfaceFormatKHR find_format(VkSurfaceFormatKHR f) const;
117 VkPresentModeKHR find_present_mode(Graphics::PresentMode m) const;
118};
119
120}
121
122#endif
Manages device memory queries, allocations, and lifetime.
Definition memory.h:20
Owning wrapper for a VkDevice.
Definition device.h:16
Owning wrapper for a Vulkan instance.
Definition instance.h:19
Presentation swap chain and associated objects.
Definition swapchain.h:21
void set_present_mode(VkPresentModeKHR m)
Changes the presentation mode used on the next recreate.
Definition swapchain.h:48
VkPresentModeKHR present_mode
Definition swapchain.h:82
std::vector< VkFramebuffer > m_depth_frame_buffers
Definition swapchain.h:103
std::vector< VkFence > present
Signaled when all rendering commands targeting an image finished.
Definition swapchain.h:101
struct nngn::SwapChain::@14 semaphores
void destroy()
Definition swapchain.cpp:61
VkSurfaceKHR m_surface
Definition swapchain.h:78
Image depth_img
Definition swapchain.h:85
VkImageView depth_img_view
Definition swapchain.h:86
std::vector< VkFence > render
Signaled when an image has been acquired and rendering can begin.
Definition swapchain.h:95
std::vector< VkSemaphore > present
Signaled when an image has been presented.
Definition swapchain.h:91
VkSurfaceKHR surface(void) const
Definition swapchain.h:36
VkInstance instance
Definition swapchain.h:77
std::size_t cur_frame() const
Definition swapchain.h:46
std::span< const VkFramebuffer > frame_buffers() const
Frame buffers for each swap chain image.
Definition swapchain.h:41
bool recreate(const Instance &inst, const Device &dev, std::uint32_t n_images, VkRenderPass depth_pass, VkRenderPass render_pass, VkExtent2D extent, VkSurfaceTransformFlagBitsKHR transform, VkExtent2D shadow_map_size, VkExtent2D shadow_cube_size, std::span< const VkImageView > shadow_map, std::span< const VkImageView > shadow_cube)
Destroys resources and recreate them.
Definition swapchain.cpp:79
VkSwapchainKHR h
Definition swapchain.h:76
VkSurfaceFormatKHR format
Definition swapchain.h:81
bool create_img_views(const Instance &inst)
Definition swapchain.cpp:125
std::span< const VkFramebuffer > depth_frame_buffers() const
Frame buffers for each depth pass image.
Definition swapchain.h:44
bool create_depth_img(const Instance &inst, VkExtent2D extent)
Definition swapchain.cpp:158
bool create_frame_buffers(const Instance &inst, VkExtent2D extent, VkRenderPass render_pass)
Definition swapchain.cpp:214
std::vector< VkImageView > m_img_views
Definition swapchain.h:83
std::vector< VkSemaphore > render
Signaled when an image has been acquired and rendering can begin.
Definition swapchain.h:89
VkDevice dev
Definition swapchain.h:79
bool create_depth_frame_buffers(const Instance &inst, VkRenderPass render_pass, VkExtent2D shadow_map_size, VkExtent2D shadow_cube_size, std::span< const VkImageView > shadow_map, std::span< const VkImageView > shadow_cube)
Definition swapchain.cpp:240
struct nngn::SwapChain::@15 fences
void set_surface(VkSurfaceKHR s)
Definition swapchain.h:49
VkFormat depth_format
Definition swapchain.h:84
std::vector< VkFramebuffer > m_frame_buffers
Definition swapchain.h:103
DeviceMemory * dev_mem
Definition swapchain.h:80
bool create_sync_objects(const Instance &inst)
Definition swapchain.cpp:179
std::span< const VkImageView > img_views() const
Views for the swap chain images.
Definition swapchain.h:38
std::size_t i_frame
Definition swapchain.h:104
std::pair< PresentContext, VkResult > acquire_img()
Acquires an image from the swap chain for rendering.
Definition swapchain.cpp:278
init
Definition img_common.lua:34
Definition audio.cpp:7
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT VkFence
Definition types.h:72
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE VkRenderPass
Definition types.h:72
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS VkSemaphore
Definition types.h:72
local m
Definition input.lua:28
local function f()) end
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
Definition graphics.h:215
PresentMode
Definition graphics.h:148
Definition resource.h:69
Aggregate type for information about a surface.
Definition swapchain.h:108
std::vector< VkSurfaceFormatKHR > formats
Definition swapchain.h:110
VkSurfaceTransformFlagBitsKHR cur_transform
Definition swapchain.h:109
std::vector< Graphics::PresentMode > present_modes
Definition swapchain.h:111
VkPresentModeKHR find_present_mode(Graphics::PresentMode m) const
Returns m if supported or a usable mode for the surface.
Definition swapchain.cpp:335
VkSurfaceFormatKHR find_format(VkSurfaceFormatKHR f) const
Returns f if supported or a usable format for the surface.
Definition swapchain.cpp:326
Information used to present an image.
Definition swapchain.h:27
std::uint32_t img_idx
Definition swapchain.h:28
VkSemaphore wait
Definition swapchain.h:29
VkSemaphore signal
Definition swapchain.h:29
VkFence fence
Definition swapchain.h:30
std::chrono::seconds s
Definition timing.cpp:6