Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qopenxrgraphics_opengl.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5#include "qopenxrhelpers_p.h"
6
7#include <QtGui/QOpenGLContext>
8#include <QtQuick/QQuickWindow>
9#include <QtQuick/private/qquickrendertarget_p.h>
10
11#include <rhi/qrhi.h>
12
14
15#ifndef GL_RGBA8
16#define GL_RGBA8 0x8058
17#endif
18
19#ifndef GL_SRGB8_ALPHA8_EXT
20#define GL_SRGB8_ALPHA8_EXT 0x8C43
21#endif
22
23#ifndef GL_DEPTH_COMPONENT16
24#define GL_DEPTH_COMPONENT16 0x81A5
25#endif
26
27#ifndef GL_DEPTH_COMPONENT24
28#define GL_DEPTH_COMPONENT24 0x81A6
29#endif
30
31#ifndef GL_DEPTH_COMPONENT32F
32#define GL_DEPTH_COMPONENT32F 0x8CAC
33#endif
34
35#ifndef GL_DEPTH24_STENCIL8
36#define GL_DEPTH24_STENCIL8 0x88F0
37#endif
38
40{
41#ifdef XR_USE_PLATFORM_WIN32
42 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
43#elif defined(XR_USE_PLATFORM_XLIB)
44 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
45#elif defined(XR_USE_PLATFORM_XCB)
46 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XCB_KHR;
47#elif defined(XR_USE_PLATFORM_WAYLAND)
48 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WAYLAND_KHR;
49#endif
50
51 m_graphicsRequirements.type = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR;
52}
53
54
55bool QOpenXRGraphicsOpenGL::isExtensionSupported(const QVector<XrExtensionProperties> &extensions) const
56{
57 for (const auto &extension : extensions) {
58 if (!strcmp(XR_KHR_OPENGL_ENABLE_EXTENSION_NAME,
59 extension.extensionName))
60 return true;
61 }
62 return false;
63}
64
65
67{
68 return XR_KHR_OPENGL_ENABLE_EXTENSION_NAME;
69}
70
71
72const XrBaseInStructure *QOpenXRGraphicsOpenGL::handle() const
73{
74 return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
75}
76
77
78bool QOpenXRGraphicsOpenGL::setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &)
79{
80 // Extension function must be loaded by name
81 PFN_xrGetOpenGLGraphicsRequirementsKHR pfnGetOpenGLGraphicsRequirementsKHR = nullptr;
82 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance, "xrGetOpenGLGraphicsRequirementsKHR",
83 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetOpenGLGraphicsRequirementsKHR)),
84 instance);
85 if (!pfnGetOpenGLGraphicsRequirementsKHR) {
86 qWarning("Could not resolve xrGetOpenGLGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support OpenGL?");
87 return false;
88 }
89 OpenXRHelpers::checkXrResult(pfnGetOpenGLGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
90 instance);
91 return true;
92}
93
95{
96 const QRhiGles2NativeHandles *openglRhi = static_cast<const QRhiGles2NativeHandles *>(rhi->nativeHandles());
97
98 auto context = openglRhi->context;
99
100 const XrVersion desiredApiVersion = XR_MAKE_VERSION(context->format().majorVersion(), context->format().minorVersion(), 0);
101 if (m_graphicsRequirements.minApiVersionSupported > desiredApiVersion) {
102 qDebug() << "Runtime does not support desired Graphics API and/or version";
103 return false;
104 }
105
106# ifdef XR_USE_PLATFORM_WIN32
107 auto nativeContext = context->nativeInterface<QNativeInterface::QWGLContext>();
108 if (nativeContext) {
109 m_graphicsBinding.hGLRC = nativeContext->nativeContext();
110 m_graphicsBinding.hDC = GetDC(reinterpret_cast<HWND>(m_window->winId()));
111 }
112# endif
113
114 m_rhi = rhi;
115
116 return true;
117}
118
119
120int64_t QOpenXRGraphicsOpenGL::colorSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
121{
122 // List of supported color swapchain formats.
123 constexpr int64_t supportedColorSwapchainFormats[] = {
124 GL_RGBA8,
126 };
127
128 auto swapchainFormatIt = std::find_first_of(std::begin(supportedColorSwapchainFormats),
129 std::end(supportedColorSwapchainFormats),
130 swapchainFormats.begin(),
131 swapchainFormats.end());
132 return *swapchainFormatIt;
133}
134
135int64_t QOpenXRGraphicsOpenGL::depthSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
136{
137 // in order of preference
138 constexpr int64_t supportedDepthSwapchainFormats[] = {
143 };
144
145 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
146 std::end(supportedDepthSwapchainFormats),
147 swapchainFormats.begin(),
148 swapchainFormats.end());
149}
150
151QVector<XrSwapchainImageBaseHeader*> QOpenXRGraphicsOpenGL::allocateSwapchainImages(int count, XrSwapchain swapchain)
152{
153 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
154 QVector<XrSwapchainImageOpenGLKHR> swapchainImageBuffer(count);
155 for (XrSwapchainImageOpenGLKHR& image : swapchainImageBuffer) {
156 image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
157 swapchainImages.push_back(reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
158 }
159 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
160 return swapchainImages;
161}
162
163
164QQuickRenderTarget QOpenXRGraphicsOpenGL::renderTarget(const XrSwapchainSubImage &subImage,
165 const XrSwapchainImageBaseHeader *swapchainImage,
166 quint64 swapchainFormat,
167 int samples,
168 int arraySize,
169 const XrSwapchainImageBaseHeader *depthSwapchainImage,
170 quint64 depthSwapchainFormat) const
171{
172 const uint32_t colorTexture = reinterpret_cast<const XrSwapchainImageOpenGLKHR*>(swapchainImage)->image;
173
174 switch (swapchainFormat) {
176 swapchainFormat = GL_RGBA8;
177 break;
178 default:
179 break;
180 }
181
182 QQuickRenderTarget::Flags flags;
183 if (samples > 1)
185
186 const QSize pixelSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height);
187 QQuickRenderTarget rt = QQuickRenderTarget::fromOpenGLTexture(colorTexture,
188 swapchainFormat,
189 pixelSize,
190 samples,
191 arraySize,
192 flags);
193 if (depthSwapchainImage) {
195 switch (depthSwapchainFormat) {
198 break;
201 break;
204 break;
205 }
206 GLuint depthImage = reinterpret_cast<const XrSwapchainImageOpenGLKHR*>(depthSwapchainImage)->image;
207 if (m_depthTexture && (m_depthTexture->format() != format || m_depthTexture->pixelSize() != pixelSize || m_depthTexture->arraySize() != arraySize)) {
208 delete m_depthTexture;
209 m_depthTexture = nullptr;
210 }
211 if (!m_depthTexture) {
212 // this is never multisample, QQuickRt takes care of resolving depth-stencil
213 if (arraySize > 1)
214 m_depthTexture = m_rhi->newTextureArray(format, arraySize, pixelSize, 1, QRhiTexture::RenderTarget);
215 else
216 m_depthTexture = m_rhi->newTexture(format, pixelSize, 1, QRhiTexture::RenderTarget);
217 }
218 m_depthTexture->createFrom({ depthImage, 0 });
219 rt.setDepthTexture(m_depthTexture);
220 }
221 return rt;
222}
223
228
230{
231 delete m_depthTexture;
232 m_depthTexture = nullptr;
233}
234
\inheaderfile QOpenGLContext
const char * extensionName() const override
const XrBaseInStructure * handle() const override
bool finializeGraphics(QRhi *rhi) override
bool isExtensionSupported(const QVector< XrExtensionProperties > &extensions) const override
int64_t colorSwapchainFormat(const QVector< int64_t > &swapchainFormats) const override
bool setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &quickConfig) override
QQuickRenderTarget renderTarget(const XrSwapchainSubImage &subImage, const XrSwapchainImageBaseHeader *swapchainImage, quint64 swapchainFormat, int samples, int arraySize, const XrSwapchainImageBaseHeader *depthSwapchainImage, quint64 depthSwapchainFormat) const override
void setupWindow(QQuickWindow *window) override
int64_t depthSwapchainFormat(const QVector< int64_t > &swapchainFormats) const override
QVector< XrSwapchainImageBaseHeader * > allocateSwapchainImages(int count, XrSwapchain swapchain) override
QQuickGraphicsConfiguration controls lower level graphics settings for the QQuickWindow.
The QQuickRenderTarget class provides an opaque container for native graphics resources specifying a ...
void setDepthTexture(QRhiTexture *texture)
Requests using the given texture as the depth or depth-stencil buffer.
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\variable QRhiGles2InitParams::format
Format format() const
Definition qrhi.h:972
int arraySize() const
Definition qrhi.h:981
@ RenderTarget
Definition qrhi.h:898
Format
Specifies the texture format.
Definition qrhi.h:914
virtual bool createFrom(NativeTexture src)
Similar to create(), except that no new native textures are created.
Definition qrhi.cpp:4487
QSize pixelSize() const
Definition qrhi.h:975
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
QRhiTexture * newTextureArray(QRhiTexture::Format format, int arraySize, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition qrhi.cpp:10636
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition qrhi.cpp:10562
const QRhiNativeHandles * nativeHandles()
Definition qrhi.cpp:10137
\inmodule QtCore
Definition qsize.h:25
void extension()
[6]
Definition dialogs.cpp:230
bool checkXrResult(XrResult result, XrInstance instance)
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLsizei samples
GLenum GLenum GLsizei count
GLbitfield flags
#define GL_SRGB8_ALPHA8_EXT
GLint GLsizei GLsizei GLenum format
#define GL_RGBA8_SNORM
#define GL_DEPTH_COMPONENT32F
Definition qopenglext.h:994
#define GL_DEPTH24_STENCIL8
#define GL_DEPTH_COMPONENT16
Definition qopenglext.h:328
#define GL_DEPTH_COMPONENT24
Definition qopenglext.h:329
#define GL_RGBA8
#define GLuint
unsigned long long quint64
Definition qtypes.h:61
aWidget window() -> setWindowTitle("New Window Title")
[2]