21bool QOpenXRGraphicsMetal::isExtensionSupported(
const QVector<XrExtensionProperties> &extensions)
const
23 for (
const auto &extension : extensions) {
24 if (!strcmp(XR_KHR_METAL_ENABLE_EXTENSION_NAME,
25 extension.extensionName))
41bool QOpenXRGraphicsMetal::setupGraphics(
const XrInstance &instance, XrSystemId &systemId,
const QQuickGraphicsConfiguration &quickConfig)
44 PFN_xrGetMetalGraphicsRequirementsKHR pfnGetMetalGraphicsRequirementsKHR =
nullptr;
45 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance,
"xrGetMetalGraphicsRequirementsKHR",
reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetMetalGraphicsRequirementsKHR)), instance);
47 if (!pfnGetMetalGraphicsRequirementsKHR) {
48 qWarning(
"Could not resolve pfnGetMetalGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support Metal?");
53 if (!OpenXRHelpers::checkXrResult(pfnGetMetalGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements), instance)) {
54 qWarning(
"Failed to get Metal graphics requirements.");
59 m_device =
static_cast<MTLDevice*>(m_graphicsRequirements.metalDevice);
60 auto commandQueue = [m_device newCommandQueue];
61 m_commandQueue =
static_cast<MTLCommandQueue *>(commandQueue);
62 m_graphicsBinding.commandQueue = m_commandQueue;
74int64_t QOpenXRGraphicsMetal::colorSwapchainFormat(
const QVector<int64_t> &swapchainFormats)
const
76 constexpr MTLPixelFormat supportedDepthSwapchainFormats[] = {MTLPixelFormatRGBA8Unorm_sRGB, MTLPixelFormatRGBA8Unorm};
77 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
78 std::end(supportedDepthSwapchainFormats),
79 swapchainFormats.begin(),
80 swapchainFormats.end());
83int64_t QOpenXRGraphicsMetal::depthSwapchainFormat(
const QVector<int64_t> &swapchainFormats)
const
86 constexpr int64_t supportedDepthSwapchainFormats[] = {
87 MTLPixelFormatDepth24Unorm_Stencil8,
88 MTLPixelFormatDepth32Float,
89 MTLPixelFormatDepth16Unorm
91 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
92 std::end(supportedDepthSwapchainFormats),
93 swapchainFormats.begin(),
94 swapchainFormats.end());
97QVector<XrSwapchainImageBaseHeader *> QOpenXRGraphicsMetal::allocateSwapchainImages(
int count, XrSwapchain swapchain)
99 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
100 QVector<XrSwapchainImageMetalKHR> swapchainImageBuffer(count);
101 for (XrSwapchainImageMetalKHR& image : swapchainImageBuffer) {
102 image.type = XR_TYPE_SWAPCHAIN_IMAGE_METAL_KHR;
103 swapchainImages.append(
reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
105 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
106 return swapchainImages;
109QQuickRenderTarget QOpenXRGraphicsMetal::renderTarget(
const XrSwapchainSubImage &subImage,
110 const XrSwapchainImageBaseHeader *swapchainImage,
111 quint64 swapchainFormat,
114 const XrSwapchainImageBaseHeader *depthSwapchainImage,
115 quint64 depthSwapchainFormat)
const
117 MTLTexture *
const colorTexture =
static_cast<MTLTexture *
const>(
reinterpret_cast<
const XrSwapchainImageMetalKHR*>(swapchainImage)->texture);
118 MTLPixelFormat colorFormat =
static_cast<MTLPixelFormat>(swapchainFormat);
120 QQuickRenderTarget::Flags flags;
122 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
124 QQuickRenderTarget renderTarget = QQuickRenderTarget::fromMetalTexture(colorTexture, colorFormat, colorFormat, QSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height), samples, arraySize, flags);
126 Q_UNUSED(depthSwapchainImage)
127 Q_UNUSED(depthSwapchainFormat)