HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gfx_surface_vulkan.hpp
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "gfx_surface.hpp"
8#include "gfx_queue_vulkan.hpp"
9#include <vulkan/vulkan.hpp>
10#include <vma/vk_mem_alloc.h>
11#include <optional>
12
13namespace hi::inline v1 {
14class gfx_surface_delegate_vulkan;
15class gfx_device_vulkan;
16namespace pipeline_image {
17class pipeline_image;
18}
19namespace pipeline_box {
20class pipeline_box;
21}
22namespace pipeline_SDF {
23class pipeline_SDF;
24}
25namespace pipeline_alpha {
26class pipeline_alpha;
27}
28namespace pipeline_tone_mapper {
29class pipeline_tone_mapper;
30}
31
33 vk::Image image;
34 vk::ImageView image_view;
35 vk::Framebuffer frame_buffer;
36 aarectangle redraw_rectangle;
37 bool layout_is_present = false;
38};
39
40class gfx_surface_vulkan final : public gfx_surface {
41public:
42 using super = gfx_surface;
43
44 vk::SurfaceKHR intrinsic;
45
46 vk::SwapchainKHR swapchain;
47
48 static constexpr uint32_t defaultNumberOfSwapchainImages = 2;
49
50 uint32_t nrSwapchainImages;
51 vk::Extent2D swapchainImageExtent;
52 vk::SurfaceFormatKHR swapchainImageFormat;
53 std::vector<swapchain_image_info> swapchain_image_infos;
54
55 // static const vk::Format depthImageFormat = vk::Format::eD32Sfloat;
56 static const vk::Format depthImageFormat = vk::Format::eD16Unorm;
57 VmaAllocation depthImageAllocation;
58 vk::Image depthImage;
59 vk::ImageView depthImageView;
60
61 static const vk::Format colorImageFormat = vk::Format::eR16G16B16A16Sfloat;
62 std::array<VmaAllocation, 1> colorImageAllocations;
63 std::array<vk::Image, 1> colorImages;
64 std::array<vk::ImageView, 1> colorImageViews;
65 std::array<vk::DescriptorImageInfo, 1> colorDescriptorImageInfos;
66
67 vk::RenderPass renderPass;
68
69 vk::CommandBuffer commandBuffer;
70
71 vk::Semaphore imageAvailableSemaphore;
72 vk::Semaphore renderFinishedSemaphore;
73 vk::Fence renderFinishedFence;
74
80
81 gfx_surface_vulkan(gfx_system& system, vk::SurfaceKHR surface);
83
85 gfx_surface_vulkan& operator=(const gfx_surface_vulkan&) = delete;
87 gfx_surface_vulkan& operator=(gfx_surface_vulkan&&) = delete;
88
89 void init() override;
90
91 void set_device(gfx_device *device) noexcept override;
92
93 gfx_device_vulkan& vulkan_device() const noexcept;
94 [[nodiscard]] extent2 size() const noexcept override;
95
96 void update(extent2 new_size) noexcept override;
97
98 [[nodiscard]] draw_context render_start(aarectangle redraw_rectangle) override;
99 void render_finish(draw_context const& context) override;
100
101 void add_delegate(gfx_surface_delegate *delegate) noexcept override;
102 void remove_delegate(gfx_surface_delegate *delegate) noexcept override;
103
104protected:
105 void teardown() noexcept override;
106 void build(extent2 new_size) noexcept;
107
108private:
109 struct delegate_type {
111 vk::Semaphore semaphore;
112 };
113
115
116 gfx_queue_vulkan const *_graphics_queue;
117 gfx_queue_vulkan const *_present_queue;
118 extent2 _render_area_granularity;
119
120 gfx_surface_loss build_for_new_device() noexcept;
121 gfx_surface_loss build_for_new_swapchain(extent2 new_size) noexcept;
122
123 void teardown_for_swapchain_lost() noexcept;
124 void teardown_for_device_lost() noexcept;
125 void teardown_for_window_lost() noexcept;
126
127 std::optional<uint32_t> acquire_next_image_from_swapchain();
128 void present_image_to_queue(uint32_t frameBufferIndex, vk::Semaphore renderFinishedSemaphore);
129
134 void fill_command_buffer(swapchain_image_info& current_image, draw_context const& context);
135
140 void submit_command_buffer(vk::Semaphore delegate_semaphore);
141
142 bool read_surface_extent(extent2 minimum_size, extent2 maximum_size);
143 bool check_surface_extent();
144
145 void build_semaphores();
146 void teardown_semaphores();
147 gfx_surface_loss build_swapchain(std::size_t new_count, extent2 new_size);
148 void teardown_swapchain();
149 void build_command_buffers();
150 void teardown_command_buffers();
151 void build_render_passes();
152 void teardown_render_passes();
153 void build_frame_buffers();
154 void teardown_frame_buffers();
155 void build_pipelines();
156 void teardown_pipelines();
157
158 void wait_idle();
159
168 std::tuple<std::size_t, extent2> get_image_count_and_size(std::size_t new_count, extent2 new_size);
169};
170
171} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
gfx_surface_loss
Definition gfx_surface_state.hpp:16
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:52
Definition gfx_device.hpp:22
Definition gfx_device_vulkan.hpp:21
Definition gfx_queue_vulkan.hpp:12
Definition gfx_surface.hpp:16
Definition gfx_surface_delegate.hpp:9
Definition gfx_surface_delegate_vulkan.hpp:15
Definition gfx_surface_vulkan.hpp:32
Definition gfx_surface_vulkan.hpp:40
void set_device(gfx_device *device) noexcept override
Graphics system.
Definition gfx_system.hpp:21