Blender V5.3
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#if defined(WITH_GHOST_SDL)
18/* Required to be first as other platforms defines would take precedent otherwise. */
19struct SDL_Window; /* Avoid pulling in the full SDL3 headers. */
20#elif defined(_WIN32)
21# include "GHOST_SystemWin32.hh"
22#elif defined(__APPLE__)
23# include "GHOST_SystemCocoa.hh"
24# include <vulkan/vulkan_metal.h>
25#else
26# ifdef WITH_GHOST_X11
27# include "GHOST_SystemX11.hh"
28# else
29# define Display void
30# define Window void *
31# endif
32# ifdef WITH_GHOST_WAYLAND
33# include "GHOST_SystemWayland.hh"
34# else
35# define wl_surface void
36# define wl_display void
37# endif
38#endif
39
40#include <map>
41#include <optional>
42#include <vector>
43
44namespace blender {
45class StringRefNull;
46}
47
48#ifndef GHOST_OPENGL_VK_CONTEXT_FLAGS
49/* leave as convenience define for the future */
50# define GHOST_OPENGL_VK_CONTEXT_FLAGS 0
51#endif
52
53#ifndef GHOST_OPENGL_VK_RESET_NOTIFICATION_STRATEGY
54# define GHOST_OPENGL_VK_RESET_NOTIFICATION_STRATEGY 0
55#endif
56namespace volk {
57struct VolkDeviceTable;
58}
59
62#ifdef WITH_GHOST_X11
63 GHOST_kVulkanPlatformX11 = 1,
64#endif
65#ifdef WITH_GHOST_WAYLAND
66 GHOST_kVulkanPlatformWayland = 2,
67#endif
68};
69
71 int size[2];
72};
73
75 std::vector<VkSwapchainKHR> swapchains;
76 std::vector<VkSemaphore> semaphores;
77
78 void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions);
79};
80
83 VkImage vk_image = VK_NULL_HANDLE;
84
88 VkSemaphore present_semaphore = VK_NULL_HANDLE;
89
90 void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions);
91};
92
98 VkFence submission_fence = VK_NULL_HANDLE;
100 VkSemaphore acquire_semaphore = VK_NULL_HANDLE;
101
103
104 void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions);
105};
106
110
111 public:
115 GHOST_ContextVK(const GHOST_ContextParams &context_params,
116#if defined(WITH_GHOST_SDL)
117 /* SDL */
118 SDL_Window *sdl_window,
119#elif defined(_WIN32)
120 HWND hwnd,
121#elif defined(__APPLE__)
122 /* FIXME CAMetalLayer but have issue with linking. */
123 void *metal_layer,
124#else
126 /* X11 */
127 Window window,
128 Display *display,
129 /* Wayland */
130 wl_surface *wayland_surface,
131 wl_display *wayland_display,
132 const GHOST_ContextVK_WindowInfo *wayland_window_info,
133#endif
134 int contextMajorVersion,
135 int contextMinorVersion,
136 const GHOST_GPUDevice &preferred_device,
137 const GHOST_WindowHDRInfo *hdr_info_ = nullptr);
138
142 ~GHOST_ContextVK() override;
143
151
157
163
169
176
181 GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) override;
182
183 GHOST_TSuccess getVulkanSwapChainFormat(GHOST_VulkanSwapChainData *r_swap_chain_data) override;
184
186 std::function<void(const GHOST_VulkanSwapChainData *, bool)> swap_buffer_draw_callback,
187 std::function<void(void)> swap_buffer_acquired_callback,
188 std::function<void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback,
189 std::function<void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback)
190 override;
191
192#ifdef WITH_GHOST_WAYLAND
199 static GHOST_TSuccess supportsWaylandColorManagement();
200#endif
201
207 GHOST_TSuccess setSwapInterval(int /*interval*/) override
208 {
209 return GHOST_kFailure;
210 }
211
217 GHOST_TSuccess getSwapInterval(int & /*interval_out*/) override
218 {
219 return GHOST_kFailure;
220 };
221
227 bool isUpsideDown() const override
228 {
229 return true;
230 }
231
238 static bool is_instance_extension_enabled(blender::StringRefNull extension_name);
239
247 static bool is_device_extension_enabled(blender::StringRefNull extension_name);
248
249 private:
250#if defined(WITH_GHOST_SDL)
251 /* SDL */
252 SDL_Window *sdl_window_;
253#elif defined(_WIN32)
254 HWND hwnd_;
255#elif defined(__APPLE__)
256 /* Is CAMetalLayer* */
257 void *metal_layer_;
258#else /* Linux */
260 /* X11 */
261 Display *display_;
262 Window window_;
263 /* Wayland */
264 wl_surface *wayland_surface_;
265 wl_display *wayland_display_;
266 const GHOST_ContextVK_WindowInfo *wayland_window_info_;
267#endif
268
269 const int context_major_version_;
270 const int context_minor_version_;
271 const GHOST_GPUDevice preferred_device_;
272
273 /* Optional HDR info updated by window. */
274 const GHOST_WindowHDRInfo *hdr_info_;
275
276 /* For display only. */
277 VkSurfaceKHR surface_;
278 VkSwapchainKHR swapchain_;
279 std::vector<GHOST_SwapchainImage> swapchain_images_;
280 std::vector<GHOST_Frame> frame_data_;
281 uint64_t render_frame_;
282 uint64_t image_count_;
283
284 VkExtent2D render_extent_;
285 VkExtent2D render_extent_min_;
286 VkSurfaceFormatKHR surface_format_;
287 bool use_hdr_swapchain_;
288
289 std::optional<uint32_t> acquired_swapchain_image_index_;
290
291 std::function<void(const GHOST_VulkanSwapChainData *, bool)> swap_buffer_draw_callback_;
292 std::function<void(void)> swap_buffer_acquired_callback_;
293 std::function<void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback_;
294 std::function<void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback_;
295
296 std::vector<VkFence> fence_pile_;
297 std::map<VkSwapchainKHR, std::vector<VkFence>> present_fences_;
298
299 std::vector<const char *> getPlatformSpecificSurfaceExtensions() const;
300 GHOST_TSuccess recreateSwapchain(bool use_hdr_swapchain);
301 GHOST_TSuccess initializeFrameData();
302 GHOST_TSuccess destroySwapchain();
303
304 VkFence getFence();
305 void setPresentFence(VkSwapchainKHR swapchain, VkFence fence);
306 void destroySwapchainPresentFences(VkSwapchainKHR swapchain);
307};
#define Window
GHOST_TVulkanPlatformType
@ GHOST_kVulkanPlatformHeadless
#define wl_display
#define Display
#define wl_surface
GHOST_TSuccess
@ GHOST_kFailure
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 getVulkanHandles(GHOST_VulkanHandles &r_handles) override
static bool is_instance_extension_enabled(blender::StringRefNull extension_name)
Is the given extension name enabled on instance level?
GHOST_TSuccess releaseDrawingContext() override
GHOST_TSuccess swapBufferAcquire() override
GHOST_TSuccess releaseNativeHandles() override
bool isUpsideDown() const override
GHOST_TSuccess setVulkanSwapBuffersCallbacks(std::function< void(const GHOST_VulkanSwapChainData *, bool)> 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
static bool is_device_extension_enabled(blender::StringRefNull extension_name)
Is the given extension name enabled on device level?
GHOST_Context(const GHOST_ContextParams &context_params)
BMesh Iterators.
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
std::vector< VkSwapchainKHR > swapchains
std::vector< VkSemaphore > semaphores
VkFence submission_fence
VkSemaphore acquire_semaphore
GHOST_FrameDiscard discard_pile
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
VkSemaphore present_semaphore