36bool QOpenXRGraphicsMetal::setupGraphics(
const XrInstance &instance, XrSystemId &systemId,
const QQuickGraphicsConfiguration &quickConfig)
39 PFN_xrGetMetalGraphicsRequirementsKHR pfnGetMetalGraphicsRequirementsKHR =
nullptr;
40 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance,
"xrGetMetalGraphicsRequirementsKHR",
reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetMetalGraphicsRequirementsKHR)), instance);
42 if (!pfnGetMetalGraphicsRequirementsKHR) {
43 qWarning(
"Could not resolve pfnGetMetalGraphicsRequirementsKHR; perhaps the OpenXR implementation does not support Metal?");
48 if (!OpenXRHelpers::checkXrResult(pfnGetMetalGraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements), instance)) {
49 qWarning(
"Failed to get Metal graphics requirements.");
54 m_device =
static_cast<MTLDevice*>(m_graphicsRequirements.metalDevice);
55 auto commandQueue = [m_device newCommandQueue];
56 m_commandQueue =
static_cast<MTLCommandQueue *>(commandQueue);
57 m_graphicsBinding.commandQueue = m_commandQueue;
69int64_t QOpenXRGraphicsMetal::colorSwapchainFormat(
const QVector<int64_t> &swapchainFormats)
const
71 constexpr MTLPixelFormat supportedDepthSwapchainFormats[] = {MTLPixelFormatRGBA8Unorm_sRGB, MTLPixelFormatRGBA8Unorm};
72 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
73 std::end(supportedDepthSwapchainFormats),
74 swapchainFormats.begin(),
75 swapchainFormats.end());
78int64_t QOpenXRGraphicsMetal::depthSwapchainFormat(
const QVector<int64_t> &swapchainFormats)
const
81 constexpr int64_t supportedDepthSwapchainFormats[] = {
82 MTLPixelFormatDepth24Unorm_Stencil8,
83 MTLPixelFormatDepth32Float,
84 MTLPixelFormatDepth16Unorm
86 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
87 std::end(supportedDepthSwapchainFormats),
88 swapchainFormats.begin(),
89 swapchainFormats.end());
92QVector<XrSwapchainImageBaseHeader *> QOpenXRGraphicsMetal::allocateSwapchainImages(
int count, XrSwapchain swapchain)
94 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
95 QVector<XrSwapchainImageMetalKHR> swapchainImageBuffer(count);
96 for (XrSwapchainImageMetalKHR& image : swapchainImageBuffer) {
97 image.type = XR_TYPE_SWAPCHAIN_IMAGE_METAL_KHR;
98 swapchainImages.append(
reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
100 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
101 return swapchainImages;
104QQuickRenderTarget QOpenXRGraphicsMetal::renderTarget(
const XrSwapchainSubImage &subImage,
105 const XrSwapchainImageBaseHeader *swapchainImage,
106 quint64 swapchainFormat,
109 const XrSwapchainImageBaseHeader *depthSwapchainImage,
110 quint64 depthSwapchainFormat)
const
112 MTLTexture *
const colorTexture =
static_cast<MTLTexture *
const>(
reinterpret_cast<
const XrSwapchainImageMetalKHR*>(swapchainImage)->texture);
113 MTLPixelFormat colorFormat =
static_cast<MTLPixelFormat>(swapchainFormat);
115 QQuickRenderTarget::Flags flags;
117 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
119 QQuickRenderTarget renderTarget = QQuickRenderTarget::fromMetalTexture(colorTexture, colorFormat, colorFormat, QSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height), samples, arraySize, flags);
121 Q_UNUSED(depthSwapchainImage)
122 Q_UNUSED(depthSwapchainFormat)