61#ifdef XR_USE_PLATFORM_EGL
62 if (hasExtension(extensions, XR_MNDX_EGL_ENABLE_EXTENSION_NAME)) {
67 if (ctx.create() && ctx.nativeInterface<QNativeInterface::QEGLContext>()) {
68 m_selectedPlatform = PLATFORM_EGL;
69 m_requiredExtensions = { XR_MNDX_EGL_ENABLE_EXTENSION_NAME };
73 if (hasExtension(extensions, XR_KHR_OPENGL_ENABLE_EXTENSION_NAME)) {
74 m_requiredExtensions.append(XR_KHR_OPENGL_ENABLE_EXTENSION_NAME);
82 if (hasExtension(extensions, XR_KHR_OPENGL_ENABLE_EXTENSION_NAME)) {
84#ifdef XR_USE_PLATFORM_WAYLAND
85 auto wlApp = qApp->nativeInterface<QNativeInterface::QWaylandApplication>();
86 if (wlApp && wlApp->display()) {
87 m_selectedPlatform = PLATFORM_WAYLAND;
88 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
93#ifdef XR_USE_PLATFORM_XLIB
94 auto x11App = qApp->nativeInterface<QNativeInterface::QX11Application>();
95 if (x11App && x11App->display()) {
96 m_selectedPlatform = PLATFORM_XLIB;
97 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
101#ifdef XR_USE_PLATFORM_WIN32
102 m_selectedPlatform = PLATFORM_WIN32;
103 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
108 qWarning(
"Qt Quick 3D XR (OpenGL): no supported platforms");
127 PFN_xrGetOpenGLGraphicsRequirementsKHR pfnGetOpenGLGraphicsRequirementsKHR =
nullptr;
128 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance,
"xrGetOpenGLGraphicsRequirementsKHR",
129 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetOpenGLGraphicsRequirementsKHR)),
131 if (!pfnGetOpenGLGraphicsRequirementsKHR) {
132 qWarning(
"Could not resolve xrGetOpenGLGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support OpenGL?");
135 OpenXRHelpers::checkXrResult(pfnGetOpenGLGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
142 const QRhiGles2NativeHandles *openglRhi =
static_cast<
const QRhiGles2NativeHandles *>(rhi->nativeHandles());
144 QOpenGLContext *openGLContext = openglRhi->context;
146 const XrVersion desiredApiVersion = XR_MAKE_VERSION(openGLContext->format().majorVersion(),
147 openGLContext->format().minorVersion(),
149 if (m_graphicsRequirements.minApiVersionSupported > desiredApiVersion) {
150 qWarning(
"Qt Quick 3D XR (OpenGL): Runtime does not support desired graphics API and/or version");
154 switch (m_selectedPlatform) {
156#ifdef XR_USE_PLATFORM_EGL
158 auto eglContext = openGLContext->nativeInterface<QNativeInterface::QEGLContext>();
160 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QEGLContext");
163 auto binding = typed_calloc<XrGraphicsBindingEGLMNDX>();
164 binding->type = XR_TYPE_GRAPHICS_BINDING_EGL_MNDX;
165 binding->getProcAddress = &eglGetProcAddress;
166 binding->display = eglContext->display();
167 binding->config = eglContext->config();
168 binding->context = eglContext->nativeContext();
169 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
174#ifdef XR_USE_PLATFORM_WAYLAND
175 case PLATFORM_WAYLAND: {
176 auto wlApp = qApp->nativeInterface<QNativeInterface::QWaylandApplication>();
177 auto wlDisplay = wlApp->display();
178 auto binding = typed_calloc<XrGraphicsBindingOpenGLWaylandKHR>();
179 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WAYLAND_KHR;
180 binding->display = wlDisplay;
181 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
186#ifdef XR_USE_PLATFORM_XLIB
187 case PLATFORM_XLIB: {
188 auto glxContext = openGLContext->nativeInterface<QNativeInterface::QGLXContext>();
190 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QGLXContext");
194 auto x11App = qApp->nativeInterface<QNativeInterface::QX11Application>();
195 auto xDisplay = x11App->display();
196 auto glxDrawable = m_window->winId();
198 QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
199 auto glxFBConfig =
static_cast<GLXFBConfig>(native->nativeResourceForContext(
"glxconfig", openGLContext));
201 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get GLXFBConfig");
206 if (glXGetFBConfigAttrib(xDisplay, glxFBConfig, GLX_VISUAL_ID, &visualId) != Success) {
207 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get visualId");
211 auto binding = typed_calloc<XrGraphicsBindingOpenGLXlibKHR>();
212 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
213 binding->xDisplay = xDisplay;
214 binding->visualid = visualId;
215 binding->glxFBConfig = glxFBConfig;
216 binding->glxDrawable = glxDrawable;
217 binding->glxContext = glxContext->nativeContext();
218 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
223#ifdef XR_USE_PLATFORM_WIN32
224 case PLATFORM_WIN32: {
225 auto wglContext = openGLContext->nativeInterface<QNativeInterface::QWGLContext>();
227 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QWGLContext");
230 auto binding = typed_calloc<XrGraphicsBindingOpenGLWin32KHR>();
231 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
232 binding->hDC = GetDC(
reinterpret_cast<HWND>(m_window->winId()));
233 binding->hGLRC = wglContext->nativeContext();
234 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
280 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
281 QVector<XrSwapchainImageOpenGLKHR> swapchainImageBuffer(count);
282 for (XrSwapchainImageOpenGLKHR& image : swapchainImageBuffer) {
283 image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
284 swapchainImages.push_back(
reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
286 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
287 return swapchainImages;
292 const XrSwapchainImageBaseHeader *swapchainImage,
293 quint64 swapchainFormat,
296 const XrSwapchainImageBaseHeader *depthSwapchainImage,
297 quint64 depthSwapchainFormat)
const
299 const uint32_t colorTexture =
reinterpret_cast<
const XrSwapchainImageOpenGLKHR*>(swapchainImage)->image;
301 switch (swapchainFormat) {
309 QQuickRenderTarget::Flags flags;
311 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
313 const QSize pixelSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height);
314 QQuickRenderTarget rt = QQuickRenderTarget::fromOpenGLTexture(colorTexture,
320 if (depthSwapchainImage) {
321 QRhiTexture::Format format = QRhiTexture::D24S8;
322 switch (depthSwapchainFormat) {
324 format = QRhiTexture::D32F;
327 format = QRhiTexture::D24;
330 format = QRhiTexture::D16;
333 GLuint depthImage =
reinterpret_cast<
const XrSwapchainImageOpenGLKHR*>(depthSwapchainImage)->image;
334 if (m_depthTexture && (m_depthTexture->format() != format || m_depthTexture->pixelSize() != pixelSize || m_depthTexture->arraySize() != arraySize)) {
335 delete m_depthTexture;
336 m_depthTexture =
nullptr;
338 if (!m_depthTexture) {
341 m_depthTexture = m_rhi->newTextureArray(format, arraySize, pixelSize, 1, QRhiTexture::RenderTarget);
343 m_depthTexture = m_rhi->newTexture(format, pixelSize, 1, QRhiTexture::RenderTarget);
345 m_depthTexture->createFrom({ depthImage, 0 });
346 rt.setDepthTexture(m_depthTexture);
QQuickRenderTarget renderTarget(const XrSwapchainSubImage &subImage, const XrSwapchainImageBaseHeader *swapchainImage, quint64 swapchainFormat, int samples, int arraySize, const XrSwapchainImageBaseHeader *depthSwapchainImage, quint64 depthSwapchainFormat) const override