Blender V5.0
GHOST_ContextVK.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#ifndef WITH_VULKAN_BACKEND
12# error "ContextVK requires WITH_VULKAN_BACKEND"
13#endif
14
15#include "GHOST_Context.hh"
16
17#ifdef _WIN32
18# include "GHOST_SystemWin32.hh"
19#elif defined(__APPLE__)
20# include "GHOST_SystemCocoa.hh"
21#else
22# ifdef WITH_GHOST_X11
23# include "GHOST_SystemX11.hh"
24# else
25# define Display void
26# define Window void *
27# endif
28# ifdef WITH_GHOST_WAYLAND
29# include "GHOST_SystemWayland.hh"
30# else
31# define wl_surface void
32# define wl_display void
33# endif
34#endif
35
36#include <map>
37#include <optional>
38#include <vector>
39
40#ifndef GHOST_OPENGL_VK_CONTEXT_FLAGS
41/* leave as convenience define for the future */
42# define GHOST_OPENGL_VK_CONTEXT_FLAGS 0
43#endif
44
45#ifndef GHOST_OPENGL_VK_RESET_NOTIFICATION_STRATEGY
46# define GHOST_OPENGL_VK_RESET_NOTIFICATION_STRATEGY 0
47#endif
48
51#ifdef WITH_GHOST_X11
52 GHOST_kVulkanPlatformX11 = 1,
53#endif
54#ifdef WITH_GHOST_WAYLAND
55 GHOST_kVulkanPlatformWayland = 2,
56#endif
57};
58
60 int size[2];
61};
62
64 std::vector<VkSwapchainKHR> swapchains;
65 std::vector<VkSemaphore> semaphores;
66
67 void destroy(VkDevice vk_device);
68};
69
72 VkImage vk_image = VK_NULL_HANDLE;
73
77 VkSemaphore present_semaphore = VK_NULL_HANDLE;
78
79 void destroy(VkDevice vk_device);
80};
81
87 VkFence submission_fence = VK_NULL_HANDLE;
89 VkSemaphore acquire_semaphore = VK_NULL_HANDLE;
90
92
93 void destroy(VkDevice vk_device);
94};
95
99
100 public:
104 GHOST_ContextVK(const GHOST_ContextParams &context_params,
105#ifdef _WIN32
106 HWND hwnd,
107#elif defined(__APPLE__)
108 /* FIXME CAMetalLayer but have issue with linking. */
109 void *metal_layer,
110#else
112 /* X11 */
113 Window window,
114 Display *display,
115 /* Wayland */
116 wl_surface *wayland_surface,
117 wl_display *wayland_display,
118 const GHOST_ContextVK_WindowInfo *wayland_window_info,
119#endif
120 int contextMajorVersion,
121 int contextMinorVersion,
122 const GHOST_GPUDevice &preferred_device,
123 const GHOST_WindowHDRInfo *hdr_info_ = nullptr);
124
128 ~GHOST_ContextVK() override;
129
137
143
149
155
162
167 GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) override;
168
169 GHOST_TSuccess getVulkanSwapChainFormat(GHOST_VulkanSwapChainData *r_swap_chain_data) override;
170
172 std::function<void(const GHOST_VulkanSwapChainData *)> swap_buffer_draw_callback,
173 std::function<void(void)> swap_buffer_acquired_callback,
174 std::function<void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback,
175 std::function<void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback)
176 override;
177
183 GHOST_TSuccess setSwapInterval(int /*interval*/) override
184 {
185 return GHOST_kFailure;
186 }
187
193 GHOST_TSuccess getSwapInterval(int & /*interval_out*/) override
194 {
195 return GHOST_kFailure;
196 };
197
203 bool isUpsideDown() const override
204 {
205 return true;
206 }
207
208 private:
209#ifdef _WIN32
210 HWND hwnd_;
211#elif defined(__APPLE__)
212 CAMetalLayer *metal_layer_;
213#else /* Linux */
215 /* X11 */
216 Display *display_;
217 Window window_;
218 /* Wayland */
219 wl_surface *wayland_surface_;
220 wl_display *wayland_display_;
221 const GHOST_ContextVK_WindowInfo *wayland_window_info_;
222#endif
223
224 const int context_major_version_;
225 const int context_minor_version_;
226 const GHOST_GPUDevice preferred_device_;
227
228 /* Optional HDR info updated by window. */
229 const GHOST_WindowHDRInfo *hdr_info_;
230
231 /* For display only. */
232 VkSurfaceKHR surface_;
233 VkSwapchainKHR swapchain_;
234 std::vector<GHOST_SwapchainImage> swapchain_images_;
235 std::vector<GHOST_Frame> frame_data_;
236 uint64_t render_frame_;
237 uint64_t image_count_;
238
239 VkExtent2D render_extent_;
240 VkExtent2D render_extent_min_;
241 VkSurfaceFormatKHR surface_format_;
242 bool use_hdr_swapchain_;
243
244 std::optional<uint32_t> acquired_swapchain_image_index_;
245
246 std::function<void(const GHOST_VulkanSwapChainData *)> swap_buffer_draw_callback_;
247 std::function<void(void)> swap_buffer_acquired_callback_;
248 std::function<void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback_;
249 std::function<void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback_;
250
251 std::vector<VkFence> fence_pile_;
252 std::map<VkSwapchainKHR, std::vector<VkFence>> present_fences_;
253
254 const char *getPlatformSpecificSurfaceExtension() const;
255 GHOST_TSuccess recreateSwapchain(bool use_hdr_swapchain);
256 GHOST_TSuccess initializeFrameData();
257 GHOST_TSuccess destroySwapchain();
258
259 VkFence getFence();
260 void setPresentFence(VkSwapchainKHR swapchain, VkFence fence);
261 void destroySwapchainPresentFences(VkSwapchainKHR swapchain);
262};
#define Window
GHOST_TVulkanPlatformType
@ GHOST_kVulkanPlatformHeadless
#define wl_display
#define Display
#define wl_surface
GHOST_TSuccess
Definition GHOST_Types.h:57
@ GHOST_kFailure
Definition GHOST_Types.h:57
unsigned long long int uint64_t
GHOST_TSuccess swapBufferRelease() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess getVulkanSwapChainFormat(GHOST_VulkanSwapChainData *r_swap_chain_data) override
GHOST_TSuccess getSwapInterval(int &) override
GHOST_TSuccess setSwapInterval(int) override
GHOST_ContextVK(const GHOST_ContextParams &context_params, GHOST_TVulkanPlatformType platform, Window window, Display *display, wl_surface *wayland_surface, wl_display *wayland_display, const GHOST_ContextVK_WindowInfo *wayland_window_info, int contextMajorVersion, int contextMinorVersion, const GHOST_GPUDevice &preferred_device, const GHOST_WindowHDRInfo *hdr_info_=nullptr)
GHOST_TSuccess initializeDrawingContext() override
friend class GHOST_XrGraphicsBindingVulkanD3D
friend class GHOST_XrGraphicsBindingVulkan
GHOST_TSuccess setVulkanSwapBuffersCallbacks(std::function< void(const GHOST_VulkanSwapChainData *)> swap_buffer_draw_callback, std::function< void(void)> swap_buffer_acquired_callback, std::function< void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback, std::function< void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback) override
GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) override
GHOST_TSuccess releaseDrawingContext() override
GHOST_TSuccess swapBufferAcquire() override
GHOST_TSuccess releaseNativeHandles() override
bool isUpsideDown() const override
~GHOST_ContextVK() override
GHOST_Context(const GHOST_ContextParams &context_params)
std::vector< VkSwapchainKHR > swapchains
void destroy(VkDevice vk_device)
std::vector< VkSemaphore > semaphores
VkFence submission_fence
VkSemaphore acquire_semaphore
GHOST_FrameDiscard discard_pile
void destroy(VkDevice vk_device)
void destroy(VkDevice vk_device)
VkSemaphore present_semaphore