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:
27 struct PresentContext {
28 std::uint32_t img_idx;
31 };
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);
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 = {};
80 DeviceMemory *dev_mem = {};
81 VkSurfaceFormatKHR format = {};
82 VkPresentModeKHR present_mode = {};
83 std::vector<VkImageView> m_img_views = {};
84 VkFormat depth_format = {};
85 Image depth_img = {};
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
108struct SurfaceInfo : Graphics::SurfaceInfo {
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;
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
std::pair< PresentContext, VkResult > acquire_img()
Acquires an image from the swap chain for rendering.
void set_present_mode(VkPresentModeKHR m)
Changes the presentation mode used on the next recreate.
Definition: swapchain.h:48
SwapChain(void)=default
VkPresentModeKHR present_mode
Definition: swapchain.h:82
struct nngn::SwapChain::@14 semaphores
VkSurfaceKHR m_surface
Definition: swapchain.h:78
Image depth_img
Definition: swapchain.h:85
void init(VkInstance inst, DeviceMemory *dev_mem, VkSurfaceFormatKHR format, VkFormat depth_format, VkPresentModeKHR mode)
VkImageView depth_img_view
Definition: swapchain.h:86
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
DeviceMemory * dev_mem
Definition: swapchain.h:80
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.
VkSwapchainKHR h
Definition: swapchain.h:76
VkSurfaceFormatKHR format
Definition: swapchain.h:81
std::vector< VkFramebuffer > m_depth_frame_buffers
Definition: swapchain.h:103
bool create_img_views(const Instance &inst)
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)
bool create_frame_buffers(const Instance &inst, VkExtent2D extent, VkRenderPass render_pass)
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)
struct nngn::SwapChain::@15 fences
void set_surface(VkSurfaceKHR s)
Definition: swapchain.h:49
std::vector< VkFramebuffer > m_frame_buffers
Definition: swapchain.h:103
std::vector< VkImageView > m_img_views
Definition: swapchain.h:83
VkFormat depth_format
Definition: swapchain.h:84
bool create_sync_objects(const Instance &inst)
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
m
Definition: input.lua:23
function f()) end
std::chrono::seconds s
Definition: timing.cpp:6
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
Definition: audio.cpp:7
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT VkFence
Definition: types.h:71
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS SEMAPHORE BUFFER COMMAND_POOL DESCRIPTOR_SET DEVICE_MEMORY FRAMEBUFFER VkImageView
Definition: types.h:77
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE VkRenderPass
Definition: types.h:71
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS VkSemaphore
Definition: types.h:71
PresentMode
Definition: graphics.h:148
VkSurfaceTransformFlagBitsKHR cur_transform
Definition: swapchain.h:109
std::vector< Graphics::PresentMode > present_modes
Definition: swapchain.h:111
void init(VkSurfaceKHR s, VkPhysicalDevice d)
Initializes all members.
VkPresentModeKHR find_present_mode(Graphics::PresentMode m) const
Returns m if supported or a usable mode for the surface.
VkSurfaceFormatKHR find_format(VkSurfaceFormatKHR f) const
Returns f if supported or a usable format for the surface.
std::vector< VkSurfaceFormatKHR > formats
Definition: swapchain.h:110
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