63#ifdef XR_USE_PLATFORM_EGL
64 if (hasExtension(extensions, XR_MNDX_EGL_ENABLE_EXTENSION_NAME)) {
69 if (ctx.create() && ctx.nativeInterface<QNativeInterface::QEGLContext>()) {
70 m_selectedPlatform = PLATFORM_EGL;
71 m_requiredExtensions = { XR_MNDX_EGL_ENABLE_EXTENSION_NAME };
75 if (hasExtension(extensions, XR_KHR_OPENGL_ENABLE_EXTENSION_NAME)) {
76 m_requiredExtensions.append(XR_KHR_OPENGL_ENABLE_EXTENSION_NAME);
84 if (hasExtension(extensions, XR_KHR_OPENGL_ENABLE_EXTENSION_NAME)) {
86#ifdef XR_USE_PLATFORM_WAYLAND
87 auto wlApp = qApp->nativeInterface<QNativeInterface::QWaylandApplication>();
88 if (wlApp && wlApp->display()) {
89 m_selectedPlatform = PLATFORM_WAYLAND;
90 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
95#ifdef XR_USE_PLATFORM_XLIB
96 auto x11App = qApp->nativeInterface<QNativeInterface::QX11Application>();
97 if (x11App && x11App->display()) {
98 m_selectedPlatform = PLATFORM_XLIB;
99 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
103#ifdef XR_USE_PLATFORM_WIN32
104 m_selectedPlatform = PLATFORM_WIN32;
105 m_requiredExtensions = { XR_KHR_OPENGL_ENABLE_EXTENSION_NAME };
110 qWarning(
"Qt Quick 3D XR (OpenGL): no supported platforms");
129 PFN_xrGetOpenGLGraphicsRequirementsKHR pfnGetOpenGLGraphicsRequirementsKHR =
nullptr;
131 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetOpenGLGraphicsRequirementsKHR)),
133 if (!pfnGetOpenGLGraphicsRequirementsKHR) {
134 qWarning(
"Could not resolve xrGetOpenGLGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support OpenGL?");
137 OpenXRHelpers::checkXrResult(pfnGetOpenGLGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
144 const QRhiGles2NativeHandles *openglRhi =
static_cast<
const QRhiGles2NativeHandles *>(rhi->nativeHandles());
146 QOpenGLContext *openGLContext = openglRhi->context;
148 const XrVersion desiredApiVersion = XR_MAKE_VERSION(openGLContext->format().majorVersion(),
149 openGLContext->format().minorVersion(),
151 if (m_graphicsRequirements.minApiVersionSupported > desiredApiVersion) {
152 qWarning(
"Qt Quick 3D XR (OpenGL): Runtime does not support desired graphics API and/or version");
156 switch (m_selectedPlatform) {
158#ifdef XR_USE_PLATFORM_EGL
160 auto eglContext = openGLContext->nativeInterface<QNativeInterface::QEGLContext>();
162 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QEGLContext");
165 auto binding = typed_calloc<XrGraphicsBindingEGLMNDX>();
166 binding->type = XR_TYPE_GRAPHICS_BINDING_EGL_MNDX;
167 binding->getProcAddress = &eglGetProcAddress;
168 binding->display = eglContext->display();
169 binding->config = eglContext->config();
170 binding->context = eglContext->nativeContext();
171 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
176#ifdef XR_USE_PLATFORM_WAYLAND
177 case PLATFORM_WAYLAND: {
178 auto wlApp = qApp->nativeInterface<QNativeInterface::QWaylandApplication>();
179 auto wlDisplay = wlApp->display();
180 auto binding = typed_calloc<XrGraphicsBindingOpenGLWaylandKHR>();
181 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WAYLAND_KHR;
182 binding->display = wlDisplay;
183 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
188#ifdef XR_USE_PLATFORM_XLIB
189 case PLATFORM_XLIB: {
190 auto glxContext = openGLContext->nativeInterface<QNativeInterface::QGLXContext>();
192 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QGLXContext");
196 auto x11App = qApp->nativeInterface<QNativeInterface::QX11Application>();
197 auto xDisplay = x11App->display();
198 auto glxDrawable = m_window->winId();
200 QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
201 auto glxFBConfig =
static_cast<GLXFBConfig>(native->nativeResourceForContext(
"glxconfig", openGLContext));
203 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get GLXFBConfig");
208 if (glXGetFBConfigAttrib(xDisplay, glxFBConfig, GLX_VISUAL_ID, &visualId) != Success) {
209 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get visualId");
213 auto binding = typed_calloc<XrGraphicsBindingOpenGLXlibKHR>();
214 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
215 binding->xDisplay = xDisplay;
216 binding->visualid = visualId;
217 binding->glxFBConfig = glxFBConfig;
218 binding->glxDrawable = glxDrawable;
219 binding->glxContext = glxContext->nativeContext();
220 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
225#ifdef XR_USE_PLATFORM_WIN32
226 case PLATFORM_WIN32: {
227 auto wglContext = openGLContext->nativeInterface<QNativeInterface::QWGLContext>();
229 qWarning(
"Qt Quick 3D XR (OpenGL): Failed to get QNativeInterface::QWGLContext");
232 auto binding = typed_calloc<XrGraphicsBindingOpenGLWin32KHR>();
233 binding->type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
234 binding->hDC = GetDC(
reinterpret_cast<HWND>(m_window->winId()));
235 binding->hGLRC = wglContext->nativeContext();
236 m_graphicsBinding.reset(
reinterpret_cast<XrBaseInStructure *>(binding));
282 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
283 QVector<XrSwapchainImageOpenGLKHR> swapchainImageBuffer(count);
284 for (XrSwapchainImageOpenGLKHR& image : swapchainImageBuffer) {
285 image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
286 swapchainImages.push_back(
reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
288 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
289 return swapchainImages;
294 const XrSwapchainImageBaseHeader *swapchainImage,
295 quint64 swapchainFormat,
298 const XrSwapchainImageBaseHeader *depthSwapchainImage,
299 quint64 depthSwapchainFormat)
const
301 const uint32_t colorTexture =
reinterpret_cast<
const XrSwapchainImageOpenGLKHR*>(swapchainImage)->image;
303 switch (swapchainFormat) {
311 QQuickRenderTarget::Flags flags;
313 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
315 const QSize pixelSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height);
316 QQuickRenderTarget rt = QQuickRenderTarget::fromOpenGLTexture(colorTexture,
322 if (depthSwapchainImage) {
323 QRhiTexture::Format format = QRhiTexture::D24S8;
324 switch (depthSwapchainFormat) {
326 format = QRhiTexture::D32F;
329 format = QRhiTexture::D24;
332 format = QRhiTexture::D16;
335 GLuint depthImage =
reinterpret_cast<
const XrSwapchainImageOpenGLKHR*>(depthSwapchainImage)->image;
336 if (m_depthTexture && (m_depthTexture->format() != format || m_depthTexture->pixelSize() != pixelSize || m_depthTexture->arraySize() != arraySize)) {
337 delete m_depthTexture;
338 m_depthTexture =
nullptr;
340 if (!m_depthTexture) {
343 m_depthTexture = m_rhi->newTextureArray(format, arraySize, pixelSize, 1, QRhiTexture::RenderTarget);
345 m_depthTexture = m_rhi->newTexture(format, pixelSize, 1, QRhiTexture::RenderTarget);
347 m_depthTexture->createFrom({ depthImage, 0 });
348 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