HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gfx_surface_vulkan_intf.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_state.hpp"
8#include "gfx_surface_delegate_vulkan.hpp"
9#include "gfx_device_vulkan_intf.hpp"
10#include "gfx_queue_vulkan.hpp"
11#include "gfx_pipeline_image_vulkan_intf.hpp"
12#include "gfx_pipeline_box_vulkan_intf.hpp"
13#include "gfx_pipeline_SDF_vulkan_intf.hpp"
14#include "gfx_pipeline_override_vulkan_intf.hpp"
15#include "gfx_pipeline_tone_mapper_vulkan_intf.hpp"
16#include "../macros.hpp"
17#include <vulkan/vulkan.hpp>
18#include <vma/vk_mem_alloc.h>
19#include <optional>
20
21hi_export_module(hikogui.GFX : gfx_surface_intf);
22
23hi_export namespace hi::inline v1 {
24
26 vk::Image image;
27 vk::ImageView image_view;
28 vk::Framebuffer frame_buffer;
29 aarectangle redraw_rectangle;
30 bool layout_is_present = false;
31};
32
34public:
35 gfx_surface_state state = gfx_surface_state::has_window;
36 gfx_surface_loss loss = gfx_surface_loss::none;
37
38 vk::SurfaceKHR intrinsic;
39
40 vk::SwapchainKHR swapchain;
41
42 constexpr static uint32_t defaultNumberOfSwapchainImages = 2;
43
44 uint32_t nrSwapchainImages;
45 vk::Extent2D swapchainImageExtent;
46 vk::SurfaceFormatKHR swapchainImageFormat;
47 std::vector<swapchain_image_info> swapchain_image_infos;
48
49 // static const vk::Format depthImageFormat = vk::Format::eD32Sfloat;
50 static const vk::Format depthImageFormat = vk::Format::eD16Unorm;
51 VmaAllocation depthImageAllocation;
52 vk::Image depthImage;
53 vk::ImageView depthImageView;
54
55 static const vk::Format colorImageFormat = vk::Format::eR16G16B16A16Sfloat;
56 std::array<VmaAllocation, 1> colorImageAllocations;
57 std::array<vk::Image, 1> colorImages;
58 std::array<vk::ImageView, 1> colorImageViews;
59 std::array<vk::DescriptorImageInfo, 1> colorDescriptorImageInfos;
60
61 vk::RenderPass renderPass;
62
63 vk::CommandBuffer commandBuffer;
64
65 vk::Semaphore imageAvailableSemaphore;
66 vk::Semaphore renderFinishedSemaphore;
67 vk::Fence renderFinishedFence;
68
74
75 gfx_surface(vk::SurfaceKHR surface) : intrinsic(surface)
76 {
77 box_pipeline = std::make_unique<gfx_pipeline_box>(this);
78 image_pipeline = std::make_unique<gfx_pipeline_image>(this);
79 SDF_pipeline = std::make_unique<gfx_pipeline_SDF>(this);
80 override_pipeline = std::make_unique<gfx_pipeline_override>(this);
81 tone_mapper_pipeline = std::make_unique<gfx_pipeline_tone_mapper>(this);
82 }
83
85 {
86 if (state != gfx_surface_state::no_window) {
87 auto const lock = std::scoped_lock(gfx_system_mutex);
88 loss = gfx_surface_loss::window_lost;
89 teardown();
90 hi_assert(state == gfx_surface_state::no_window);
91 }
92 }
93
94 gfx_surface(const gfx_surface&) = delete;
95 gfx_surface& operator=(const gfx_surface&) = delete;
96 gfx_surface(gfx_surface&&) = delete;
97 gfx_surface& operator=(gfx_surface&&) = delete;
98
104 void set_device(gfx_device *device) noexcept;
105
106 [[nodiscard]] gfx_device *device() const noexcept
107 {
108 return _device;
109 }
110
111 [[nodiscard]] extent2 size() const noexcept;
112
113 void update(extent2 new_size) noexcept;
114
115 [[nodiscard]] draw_context render_start(aarectangle redraw_rectangle);
116 void render_finish(draw_context const& context);
117
118 void add_delegate(gfx_surface_delegate *delegate) noexcept;
119 void remove_delegate(gfx_surface_delegate *delegate) noexcept;
120
121private:
122 struct delegate_type {
123 gfx_surface_delegate *delegate;
124 vk::Semaphore semaphore;
125 };
126
127 gfx_device *_device = nullptr;
128
130
131 gfx_queue_vulkan const *_graphics_queue;
132 gfx_queue_vulkan const *_present_queue;
133 extent2 _render_area_granularity;
134
135 void teardown() noexcept;
136 void build(extent2 new_size) noexcept;
137
138 gfx_surface_loss build_for_new_device() noexcept;
139 gfx_surface_loss build_for_new_swapchain(extent2 new_size) noexcept;
140
141 void teardown_for_swapchain_lost() noexcept;
142 void teardown_for_device_lost() noexcept;
143 void teardown_for_window_lost() noexcept;
144
145 std::optional<uint32_t> acquire_next_image_from_swapchain();
146 void present_image_to_queue(uint32_t frameBufferIndex, vk::Semaphore renderFinishedSemaphore);
147
152 void fill_command_buffer(swapchain_image_info const& current_image, draw_context const& context, vk::Rect2D render_area);
153
158 void submit_command_buffer(vk::Semaphore delegate_semaphore);
159
160 bool read_surface_extent(extent2 minimum_size, extent2 maximum_size);
161 bool check_surface_extent();
162
163 void build_semaphores();
164 void teardown_semaphores();
165 gfx_surface_loss build_swapchain(std::size_t new_count, extent2 new_size);
166 void teardown_swapchain();
167 void build_command_buffers();
168 void teardown_command_buffers();
169 void build_render_passes();
170 void teardown_render_passes();
171 void build_frame_buffers();
172 void teardown_frame_buffers();
173 void build_pipelines();
174 void teardown_pipelines();
175
176 void wait_idle();
177
186 std::tuple<std::size_t, extent2> get_image_count_and_size(std::size_t new_count, extent2 new_size);
187};
188
189[[nodiscard]] std::unique_ptr<gfx_surface> make_unique_gfx_surface(os_handle instance, void *os_window);
190
191[[nodiscard]] inline gfx_device *find_best_device(gfx_surface const &surface)
192{
193 return find_best_device(surface.intrinsic);
194}
195
196} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
gfx_device * find_best_device(gfx_surface const &surface)
Find the best device for a surface.
Definition gfx_surface_vulkan_intf.hpp:191
unfair_recursive_mutex gfx_system_mutex
Global mutex for GUI elements, like gfx_system, gfx_device, Windows and Widgets.
Definition gfx_system_globals.hpp:18
gfx_surface_state
Definition gfx_surface_state.hpp:13
gfx_surface_loss
Definition gfx_surface_state.hpp:20
Definition gfx_device_vulkan_intf.hpp:26
Definition gfx_queue_vulkan.hpp:15
A delegate for drawing on a window below the HikoGUI user interface.
Definition gfx_surface_delegate_vulkan.hpp:24
Definition gfx_surface_vulkan_intf.hpp:25
Definition gfx_surface_vulkan_intf.hpp:33