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_opengles.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/private/qquickrendertarget_p.h>
9#include <rhi/qrhi.h>
10
12
13#ifndef GL_DEPTH_COMPONENT32F
14#define GL_DEPTH_COMPONENT32F 0x8CAC
15#endif
16
18{
19#ifdef XR_USE_PLATFORM_ANDROID
20 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR;
21#endif
22 m_graphicsRequirements.type = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_ES_KHR;
23}
24
25
26bool QOpenXRGraphicsOpenGLES::isExtensionSupported(const QVector<XrExtensionProperties> &extensions) const
27{
28 for (const auto &extension : extensions) {
29 if (!strcmp(XR_KHR_OPENGL_ES_ENABLE_EXTENSION_NAME,
30 extension.extensionName))
31 return true;
32 }
33 return false;
34}
35
36
38{
39 return XR_KHR_OPENGL_ES_ENABLE_EXTENSION_NAME;
40}
41
42
43const XrBaseInStructure *QOpenXRGraphicsOpenGLES::handle() const
44{
45 return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
46}
47
48
49bool QOpenXRGraphicsOpenGLES::setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &)
50{
51 // Extension function must be loaded by name
52 PFN_xrGetOpenGLESGraphicsRequirementsKHR pfnGetOpenGLESGraphicsRequirementsKHR = nullptr;
53 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance, "xrGetOpenGLESGraphicsRequirementsKHR",
54 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetOpenGLESGraphicsRequirementsKHR)),
55 instance);
56
57 if (!pfnGetOpenGLESGraphicsRequirementsKHR) {
58 qWarning("Could not resolve xrGetOpenGLESGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support OpenGL ES?");
59 return false;
60 }
61 OpenXRHelpers::checkXrResult(pfnGetOpenGLESGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
62 instance);
63 return true;
64}
65
67{
68 const QRhiGles2NativeHandles *openglRhi = static_cast<const QRhiGles2NativeHandles *>(rhi->nativeHandles());
69
70 auto context = openglRhi->context;
71 const XrVersion desiredApiVersion = XR_MAKE_VERSION(context->format().majorVersion(), context->format().minorVersion(), 0);
72 if (m_graphicsRequirements.minApiVersionSupported > desiredApiVersion) {
73 qDebug() << "Runtime does not support desired Graphics API and/or version";
74 return false;
75 }
76
77#ifdef XR_USE_PLATFORM_ANDROID
78 auto nativeContext = context->nativeInterface<QNativeInterface::QEGLContext>();
79 if (nativeContext) {
80 m_graphicsBinding.display = nativeContext->display();
81 m_graphicsBinding.config = nativeContext->config();
82 m_graphicsBinding.context = nativeContext->nativeContext();
83 }
84#endif
85
86 m_rhi = rhi;
87
88 return true;
89}
90
91
92int64_t QOpenXRGraphicsOpenGLES::colorSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
93{
94 // List of supported color swapchain formats.
95 constexpr int64_t supportedColorSwapchainFormats[] = {
98 };
99
100 auto swapchainFormatIt = std::find_first_of(std::begin(supportedColorSwapchainFormats),
101 std::end(supportedColorSwapchainFormats),
102 swapchainFormats.begin(),
103 swapchainFormats.end());
104 return *swapchainFormatIt;
105}
106
107int64_t QOpenXRGraphicsOpenGLES::depthSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
108{
109 // in order of preference
110 constexpr int64_t supportedDepthSwapchainFormats[] = {
115 };
116
117 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
118 std::end(supportedDepthSwapchainFormats),
119 swapchainFormats.begin(),
120 swapchainFormats.end());
121}
122
123
124QVector<XrSwapchainImageBaseHeader*> QOpenXRGraphicsOpenGLES::allocateSwapchainImages(int count, XrSwapchain swapchain)
125{
126 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
127 QVector<XrSwapchainImageOpenGLESKHR> swapchainImageBuffer(count);
128 for (XrSwapchainImageOpenGLESKHR& image : swapchainImageBuffer) {
129 image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_ES_KHR;
130 swapchainImages.push_back(reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
131 }
132 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
133 return swapchainImages;
134}
135
136
138 const XrSwapchainImageBaseHeader *swapchainImage,
139 quint64 swapchainFormat,
140 int samples,
141 int arraySize,
142 const XrSwapchainImageBaseHeader *depthSwapchainImage,
143 quint64 depthSwapchainFormat) const
144{
145 const uint32_t colorTexture = reinterpret_cast<const XrSwapchainImageOpenGLESKHR*>(swapchainImage)->image;
146
147 switch (swapchainFormat) {
149 swapchainFormat = GL_RGBA8_OES;
150 break;
151 default:
152 break;
153 }
154
155 QQuickRenderTarget::Flags flags;
156 if (samples > 1)
158
159 const QSize pixelSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height);
160 QQuickRenderTarget rt = QQuickRenderTarget::fromOpenGLTexture(colorTexture,
161 swapchainFormat,
162 pixelSize,
163 samples,
164 arraySize,
165 flags);
166 if (depthSwapchainImage) {
168 switch (depthSwapchainFormat) {
171 break;
174 break;
177 break;
178 }
179 GLuint depthImage = reinterpret_cast<const XrSwapchainImageOpenGLESKHR*>(depthSwapchainImage)->image;
180 if (m_depthTexture && (m_depthTexture->format() != format || m_depthTexture->pixelSize() != pixelSize || m_depthTexture->arraySize() != arraySize)) {
181 delete m_depthTexture;
182 m_depthTexture = nullptr;
183 }
184 if (!m_depthTexture) {
185 // this is never multisample, QQuickRt takes care of resolving depth-stencil
186 if (arraySize > 1)
187 m_depthTexture = m_rhi->newTextureArray(format, arraySize, pixelSize, 1, QRhiTexture::RenderTarget);
188 else
189 m_depthTexture = m_rhi->newTexture(format, pixelSize, 1, QRhiTexture::RenderTarget);
190 }
191 m_depthTexture->createFrom({ depthImage, 0 });
192 rt.setDepthTexture(m_depthTexture);
193 }
194 return rt;
195}
196
198{
199 delete m_depthTexture;
200 m_depthTexture = nullptr;
201}
202
QQuickRenderTarget renderTarget(const XrSwapchainSubImage &subImage, const XrSwapchainImageBaseHeader *swapchainImage, quint64 swapchainFormat, int samples, int arraySize, const XrSwapchainImageBaseHeader *depthSwapchainImage, quint64 depthSwapchainFormat) const override
const XrBaseInStructure * handle() const override
const char * extensionName() const override
int64_t depthSwapchainFormat(const QVector< int64_t > &swapchainFormats) const override
QVector< XrSwapchainImageBaseHeader * > allocateSwapchainImages(int count, XrSwapchain swapchain) override
bool isExtensionSupported(const QVector< XrExtensionProperties > &extensions) const override
bool finializeGraphics(QRhi *rhi) override
bool setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &quickConfig) override
int64_t colorSwapchainFormat(const QVector< int64_t > &swapchainFormats) const 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.
\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
#define GL_RGBA8_OES
GLbitfield flags
#define GL_DEPTH_COMPONENT16_OES
#define GL_SRGB8_ALPHA8_EXT
#define GL_DEPTH24_STENCIL8_OES
#define GL_DEPTH_COMPONENT24_OES
GLint GLsizei GLsizei GLenum format
#define GL_RGBA8_SNORM
#define GL_DEPTH_COMPONENT32F
Definition qopenglext.h:994
#define GLuint
unsigned long long quint64
Definition qtypes.h:61