Blender V5.3
GHOST_XrGraphicsBinding.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <list>
11#include <sstream>
12
13#if defined(WITH_GHOST_X11)
14# if defined(WITH_OPENGL_BACKEND)
15# include "GHOST_ContextEGL.hh"
16# include "GHOST_ContextGLX.hh"
17# endif
18# include "GHOST_SystemX11.hh"
19#endif
20#if defined(WITH_GHOST_WAYLAND)
21# if defined(WITH_OPENGL_BACKEND)
22# include "GHOST_ContextEGL.hh"
23# endif
24# include "GHOST_SystemWayland.hh"
25#endif
26#if defined(WIN32)
27# include "GHOST_ContextD3D.hh"
28# include "GHOST_ContextWGL.hh"
29# include "GHOST_SystemWin32.hh"
31#endif
32#ifdef WITH_VULKAN_BACKEND
34#endif
35#ifdef WITH_METAL_BACKEND
37#endif
38
40
41#if defined(WITH_OPENGL_BACKEND)
42static std::optional<int64_t> choose_swapchain_format_from_candidates(
43 const std::vector<int64_t> &gpu_binding_formats, const std::vector<int64_t> &runtime_formats)
44{
45 if (gpu_binding_formats.empty()) {
46 return std::nullopt;
47 }
48
49 auto res = std::find_first_of(gpu_binding_formats.begin(),
50 gpu_binding_formats.end(),
51 runtime_formats.begin(),
52 runtime_formats.end());
53 if (res == gpu_binding_formats.end()) {
54 return std::nullopt;
55 }
56
57 return *res;
58}
59
60class GHOST_XrGraphicsBindingOpenGL : public GHOST_IXrGraphicsBinding {
61 PFN_xrGetOpenGLGraphicsRequirementsKHR xrGetOpenGLGraphicsRequirementsKHR_ = nullptr;
62
63 public:
64 ~GHOST_XrGraphicsBindingOpenGL()
65 {
66 if (fbo_ != 0) {
67 glDeleteFramebuffers(1, &fbo_);
68 }
69 }
70
71 bool loadExtensionFunctions(XrInstance instance) override
72 {
73# define LOAD_FUNCTION(fn_ptr, name) \
74 if (XR_FAILED(xrGetInstanceProcAddr(instance, #name, (PFN_xrVoidFunction *)&fn_ptr))) { \
75 return false; \
76 }
77
78 LOAD_FUNCTION(xrGetOpenGLGraphicsRequirementsKHR_, xrGetOpenGLGraphicsRequirementsKHR);
79
80# undef LOAD_FUNCTION
81 return true;
82 }
83
84 bool checkVersionRequirements(GHOST_Context &ghost_ctx,
85 XrInstance instance,
86 XrSystemId system_id,
87 std::string *r_requirement_info) const override
88 {
89 int gl_major_version, gl_minor_version;
90# if defined(WIN32)
91 GHOST_ContextWGL &ctx_gl = static_cast<GHOST_ContextWGL &>(ghost_ctx);
92 gl_major_version = ctx_gl.context_major_version_;
93 gl_minor_version = ctx_gl.context_minor_version_;
94# elif defined(WITH_GHOST_X11) || defined(WITH_GHOST_WAYLAND)
95 if (dynamic_cast<GHOST_ContextEGL *>(&ghost_ctx)) {
96 GHOST_ContextEGL &ctx_gl = static_cast<GHOST_ContextEGL &>(ghost_ctx);
97 gl_major_version = ctx_gl.context_major_version_;
98 gl_minor_version = ctx_gl.context_minor_version_;
99 }
100# if defined(WITH_GHOST_X11)
101 else {
102 GHOST_ContextGLX &ctx_gl = static_cast<GHOST_ContextGLX &>(ghost_ctx);
103 gl_major_version = ctx_gl.context_major_version_;
104 gl_minor_version = ctx_gl.context_minor_version_;
105 }
106# endif
107# endif
108 // static XrInstance s_instance = XR_NULL_HANDLE;
109 XrGraphicsRequirementsOpenGLKHR gpu_requirements = {XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR};
110 const XrVersion gl_version = XR_MAKE_VERSION(gl_major_version, gl_minor_version, 0);
111
112 xrGetOpenGLGraphicsRequirementsKHR_(instance, system_id, &gpu_requirements);
113
114 if (r_requirement_info) {
115 std::ostringstream strstream;
116 strstream << "Min OpenGL version "
117 << XR_VERSION_MAJOR(gpu_requirements.minApiVersionSupported) << "."
118 << XR_VERSION_MINOR(gpu_requirements.minApiVersionSupported) << std::endl;
119 strstream << "Max OpenGL version "
120 << XR_VERSION_MAJOR(gpu_requirements.maxApiVersionSupported) << "."
121 << XR_VERSION_MINOR(gpu_requirements.maxApiVersionSupported) << std::endl;
122
123 *r_requirement_info = strstream.str();
124 }
125
126 return (gl_version >= gpu_requirements.minApiVersionSupported) &&
127 (gl_version <= gpu_requirements.maxApiVersionSupported);
128 }
129
130 void initFromGhostContext(GHOST_Context &ghost_ctx,
131 XrInstance /*instance*/,
132 XrSystemId /*system_id*/) override
133 {
134# if defined(WITH_GHOST_X11) || defined(WITH_GHOST_WAYLAND)
135 /* WAYLAND/X11 may be dynamically selected at load time but both may also be
136 * supported at compile time individually.
137 * Without `is_ctx_egl` & `is_wayland` preprocessor checks become an unmanageable soup. */
138 const bool is_ctx_egl = dynamic_cast<GHOST_ContextEGL *>(&ghost_ctx) != nullptr;
139 if (is_ctx_egl) {
140 GHOST_ContextEGL &ctx_egl = static_cast<GHOST_ContextEGL &>(ghost_ctx);
141 const bool is_wayland = (
142# if defined(WITH_GHOST_WAYLAND)
143 dynamic_cast<const GHOST_SystemWayland *const>(ctx_egl.system_) != nullptr
144# else
145 false
146# endif
147 );
148
149 if (is_wayland) {
150# if defined(WITH_GHOST_WAYLAND)
151 /* #GHOST_SystemWayland */
152 oxr_binding.wl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WAYLAND_KHR;
153 oxr_binding.wl.display = (wl_display *)ctx_egl.native_display_;
154# else
155 GHOST_ASSERT(false, "Unexpected State: logical error, unreachable!");
156# endif /* !WITH_GHOST_WAYLAND */
157 }
158 else { /* `!is_wayland` */
159# if defined(WITH_GHOST_X11)
160 /* #GHOST_SystemX11. */
161 oxr_binding.egl.type = XR_TYPE_GRAPHICS_BINDING_EGL_MNDX;
162# if XR_CURRENT_API_VERSION >= XR_MAKE_VERSION(1, 0, 29)
163 oxr_binding.egl.getProcAddress = reinterpret_cast<PFN_xrEglGetProcAddressMNDX>(
164 eglGetProcAddress);
165# else
166 oxr_binding.egl.getProcAddress = reinterpret_cast<PFNEGLGETPROCADDRESSPROC>(
167 eglGetProcAddress);
168# endif
169 oxr_binding.egl.display = ctx_egl.getDisplay();
170 oxr_binding.egl.config = ctx_egl.getConfig();
171 oxr_binding.egl.context = ctx_egl.getContext();
172# else
173 GHOST_ASSERT(false, "Unexpected State: built with only WAYLAND and no System found!");
174# endif /* !WITH_GHOST_X11 */
175 }
176 }
177 else { /* `!is_ctx_egl` */
178# if defined(WITH_GHOST_X11)
179 GHOST_ContextGLX &ctx_glx = static_cast<GHOST_ContextGLX &>(ghost_ctx);
180 XVisualInfo *visual_info = glXGetVisualFromFBConfig(ctx_glx.display_, ctx_glx.fbconfig_);
181
182 oxr_binding.glx.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
183 oxr_binding.glx.xDisplay = ctx_glx.display_;
184 oxr_binding.glx.glxFBConfig = ctx_glx.fbconfig_;
185 oxr_binding.glx.glxDrawable = ctx_glx.window_;
186 oxr_binding.glx.glxContext = ctx_glx.context_;
187 oxr_binding.glx.visualid = visual_info->visualid;
188
189 XFree(visual_info);
190# else
191 GHOST_ASSERT(false, "Unexpected State: built without X11 and no EGL context is available!");
192# endif /* !WITH_GHOST_X11 */
193 }
194# elif defined(WIN32)
195 GHOST_ContextWGL &ctx_wgl = static_cast<GHOST_ContextWGL &>(ghost_ctx);
196
197 oxr_binding.wgl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
198 oxr_binding.wgl.hDC = ctx_wgl.h_DC_;
199 oxr_binding.wgl.hGLRC = ctx_wgl.h_GLRC_;
200# endif /* WIN32 */
201
202 /* Generate a frame-buffer to use for blitting into the texture. */
203 glGenFramebuffers(1, &fbo_);
204 }
205
206 std::optional<int64_t> chooseSwapchainFormat(const std::vector<int64_t> &runtime_formats,
207 GHOST_TXrSwapchainFormat &r_format,
208 bool &r_is_srgb_format) const override
209 {
210 std::vector<int64_t> gpu_binding_formats = {
211# if 0 /* RGB10A2, RGBA16 don't seem to work with Oculus head-sets, \
212 * so move them after RGBA16F for the time being. */
213 GL_RGB10_A2,
214 GL_RGBA16,
215# endif
216 GL_RGBA16F,
217# if 1
218 GL_RGB10_A2,
219 GL_RGBA16,
220# endif
221 GL_RGBA8,
222 GL_SRGB8_ALPHA8,
223 };
224
225 std::optional result = choose_swapchain_format_from_candidates(gpu_binding_formats,
226 runtime_formats);
227 if (result) {
228 switch (*result) {
229 case GL_RGB10_A2:
230 r_format = GHOST_kXrSwapchainFormatRGB10_A2;
231 break;
232 case GL_RGBA16:
233 r_format = GHOST_kXrSwapchainFormatRGBA16;
234 break;
235 case GL_RGBA16F:
236 r_format = GHOST_kXrSwapchainFormatRGBA16F;
237 break;
238 case GL_RGBA8:
239 case GL_SRGB8_ALPHA8:
240 r_format = GHOST_kXrSwapchainFormatRGBA8;
241 break;
242 }
243 r_is_srgb_format = (*result == GL_SRGB8_ALPHA8);
244 }
245 else {
246 r_format = GHOST_kXrSwapchainFormatRGBA8;
247 r_is_srgb_format = false;
248 }
249
250 return result;
251 }
252
253 std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
254 {
255 std::vector<XrSwapchainImageOpenGLKHR> ogl_images(image_count);
256 std::vector<XrSwapchainImageBaseHeader *> base_images;
257
258 /* Need to return vector of base header pointers, so of a different type. Need to build a new
259 * list with this type, and keep the initial one alive. */
260 for (XrSwapchainImageOpenGLKHR &image : ogl_images) {
261 image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
262 base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
263 }
264
265 /* Keep alive. */
266 image_cache_.push_back(std::move(ogl_images));
267
268 return base_images;
269 }
270
271 void submitToSwapchainBegin() override {}
272 void submitToSwapchainImage(XrSwapchainImageBaseHeader &swapchain_image,
273 const GHOST_XrDrawViewInfo &draw_info) override
274 {
275 XrSwapchainImageOpenGLKHR &ogl_swapchain_image = reinterpret_cast<XrSwapchainImageOpenGLKHR &>(
276 swapchain_image);
277
278 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_);
279
280 glFramebufferTexture2D(
281 GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ogl_swapchain_image.image, 0);
282
283 glBlitFramebuffer(draw_info.ofsx,
284 draw_info.ofsy,
285 draw_info.ofsx + draw_info.width,
286 draw_info.ofsy + draw_info.height,
287 draw_info.ofsx,
288 draw_info.ofsy,
289 draw_info.ofsx + draw_info.width,
290 draw_info.ofsy + draw_info.height,
291 GL_COLOR_BUFFER_BIT,
292 GL_LINEAR);
293
294 glBindFramebuffer(GL_FRAMEBUFFER, 0);
295 }
296 void submitToSwapchainEnd() override {}
297
298 bool needsUpsideDownDrawing(GHOST_Context &ghost_ctx) const override
299 {
300 return ghost_ctx.isUpsideDown();
301 }
302
303 private:
304 std::list<std::vector<XrSwapchainImageOpenGLKHR>> image_cache_;
305 GLuint fbo_ = 0;
306};
307#endif
308
309std::unique_ptr<GHOST_IXrGraphicsBinding> GHOST_XrGraphicsBindingCreateFromType(
310 GHOST_TXrGraphicsBinding type, GHOST_Context &context)
311{
312 switch (type) {
313#ifdef WITH_OPENGL_BACKEND
314 case GHOST_kXrGraphicsOpenGL:
315 return std::make_unique<GHOST_XrGraphicsBindingOpenGL>();
316#endif
317#ifdef WITH_VULKAN_BACKEND
318 case GHOST_kXrGraphicsVulkan:
319 return std::make_unique<GHOST_XrGraphicsBindingVulkan>(context);
320#endif
321#ifdef WITH_METAL_BACKEND
322 case GHOST_kXrGraphicsMetal:
323 return std::make_unique<GHOST_XrGraphicsBindingMetal>(context);
324#endif
325#ifdef WIN32
326# ifdef WITH_OPENGL_BACKEND
327 case GHOST_kXrGraphicsOpenGLD3D11:
328 return std::make_unique<GHOST_XrGraphicsBindingOpenGLD3D>(context);
329# endif
330# ifdef WITH_VULKAN_BACKEND
331 case GHOST_kXrGraphicsVulkanD3D11:
332 return std::make_unique<GHOST_XrGraphicsBindingVulkanD3D>(context);
333# endif
334#endif
335 default:
336 return nullptr;
337 }
338
339 (void)context; /* Might be unused. */
340}
#define wl_display
#define GHOST_ASSERT(x, info)
std::unique_ptr< GHOST_IXrGraphicsBinding > GHOST_XrGraphicsBindingCreateFromType(GHOST_TXrGraphicsBinding type, GHOST_Context &context)
#define LOAD_FUNCTION(fn_ptr, name)
EGLConfig getConfig() const
EGLDisplay getDisplay() const
EGLContext getContext() const
virtual bool isUpsideDown() const
#define image(slot, qualifiers, format)