Blender V5.3
GHOST_XrGraphicsBindingMetal.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "GHOST_ContextMTL.hh"
10
12
13static std::optional<int64_t> choose_swapchain_format_from_candidates(
14 const std::vector<int64_t> &gpu_binding_formats, const std::vector<int64_t> &runtime_formats)
15{
16 if (gpu_binding_formats.empty()) {
17 return std::nullopt;
18 }
19
20 auto res = std::find_first_of(gpu_binding_formats.begin(),
21 gpu_binding_formats.end(),
22 runtime_formats.begin(),
23 runtime_formats.end());
24 if (res == gpu_binding_formats.end()) {
25 return std::nullopt;
26 }
27
28 return *res;
29}
30
35
37{
38#define LOAD_FUNCTION(fn_ptr, name) \
39 if (XR_FAILED(xrGetInstanceProcAddr(instance, #name, (PFN_xrVoidFunction *)&fn_ptr))) { \
40 return false; \
41 }
42
43 LOAD_FUNCTION(xrGetMetalGraphicsRequirementsKHR_, xrGetMetalGraphicsRequirementsKHR);
44
45#undef LOAD_FUNCTION
46 return true;
47}
48
50 XrInstance instance,
51 XrSystemId system_id,
52 std::string *r_requirement_info) const
53{
54 XrGraphicsRequirementsMetalKHR gpu_requirements;
55 gpu_requirements.type = XR_TYPE_GRAPHICS_REQUIREMENTS_METAL_KHR;
56 gpu_requirements.next = nullptr;
57 gpu_requirements.metalDevice = nullptr;
58
59 xrGetMetalGraphicsRequirementsKHR_(instance, system_id, &gpu_requirements);
60
61 GHOST_ContextMTL &ghost_metal_ctx = dynamic_cast<GHOST_ContextMTL &>(ghost_ctx);
62
63 const bool metal_devices_match = (ghost_metal_ctx.metalDevice() == gpu_requirements.metalDevice);
64
65 if (!metal_devices_match) {
66 *r_requirement_info = "Metal Render Device used by Blender and OpenXR do not match";
67 return false;
68 }
69
70 return true;
71}
72
74 XrInstance /*instance*/,
75 XrSystemId /*system_id*/)
76{
77 GHOST_ContextMTL &ghost_metal_ctx = dynamic_cast<GHOST_ContextMTL &>(ghost_ctx);
78
79 oxr_binding.metal.type = XR_TYPE_GRAPHICS_BINDING_METAL_KHR;
80 oxr_binding.metal.next = nullptr;
81 oxr_binding.metal.commandQueue = (__bridge void *)ghost_metal_ctx.metalCommandQueue();
82}
83
85 const std::vector<int64_t> &runtime_formats,
86 GHOST_TXrSwapchainFormat &r_format,
87 bool &r_is_srgb_format) const
88{
89 std::vector<int64_t> gpu_binding_formats = {
90 MTLPixelFormatRGBA16Float,
91 MTLPixelFormatRGB10A2Unorm,
92 MTLPixelFormatRGBA16Unorm,
93 MTLPixelFormatRGBA8Unorm,
94 MTLPixelFormatRGBA8Unorm_sRGB,
95 };
96
97 const std::optional result = choose_swapchain_format_from_candidates(gpu_binding_formats,
98 runtime_formats);
99
100 if (!result) {
101 r_format = GHOST_kXrSwapchainFormatRGBA8;
102 r_is_srgb_format = false;
103 return result;
104 }
105
106 switch (*result) {
107 case MTLPixelFormatRGB10A2Unorm:
108 r_format = GHOST_kXrSwapchainFormatRGB10_A2;
109 break;
110 case MTLPixelFormatRGBA16Unorm:
111 r_format = GHOST_kXrSwapchainFormatRGBA16;
112 break;
113 case MTLPixelFormatRGBA16Float:
114 r_format = GHOST_kXrSwapchainFormatRGBA16F;
115 break;
116 case MTLPixelFormatRGBA8Unorm:
117 case MTLPixelFormatRGBA8Unorm_sRGB:
118 r_format = GHOST_kXrSwapchainFormatRGBA8;
119 break;
120 }
121
122 r_is_srgb_format = (*result == MTLPixelFormatRGBA8Unorm_sRGB);
123
124 return result;
125}
126
127std::vector<XrSwapchainImageBaseHeader *> GHOST_XrGraphicsBindingMetal::createSwapchainImages(
128 uint32_t image_count)
129{
130 std::vector<XrSwapchainImageMetalKHR> metal_images(image_count);
131 std::vector<XrSwapchainImageBaseHeader *> base_images;
132
133 /* Need to return vector of base header pointers, so of a different type. Need to build a new
134 * list with this type, and keep the initial one alive. */
135 for (XrSwapchainImageMetalKHR &image : metal_images) {
136 image.type = XR_TYPE_SWAPCHAIN_IMAGE_METAL_KHR;
137 base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
138 }
139
140 /* Keep alive. */
141 image_cache_.push_back(std::move(metal_images));
142
143 return base_images;
144}
145
147
149 XrSwapchainImageBaseHeader &swapchain_image, const GHOST_XrDrawViewInfo &draw_info)
150{
151 XrSwapchainImageMetalKHR &metal_swapchain_image = reinterpret_cast<XrSwapchainImageMetalKHR &>(
152 swapchain_image);
153 id<MTLTexture> metal_xr_texture = static_cast<id<MTLTexture>>(metal_swapchain_image.texture);
154
155 ghost_metal_ctx_->xrBlitCallback(
156 metal_xr_texture, draw_info.ofsx, draw_info.ofsy, draw_info.width, draw_info.height);
157}
158
160
162{
163 GHOST_ContextMTL &ghost_metal_ctx = dynamic_cast<GHOST_ContextMTL &>(ghost_ctx);
164
165 return !ghost_metal_ctx.isUpsideDown();
166}
#define LOAD_FUNCTION(fn_ptr, name)
static std::optional< int64_t > choose_swapchain_format_from_candidates(const std::vector< int64_t > &gpu_binding_formats, const std::vector< int64_t > &runtime_formats)
MTLCommandQueue * metalCommandQueue()
MTLDevice * metalDevice()
virtual bool isUpsideDown() const
union GHOST_IXrGraphicsBinding::@011235222313272276112307021312244353112200226155 oxr_binding
std::optional< int64_t > chooseSwapchainFormat(const std::vector< int64_t > &runtime_formats, GHOST_TXrSwapchainFormat &r_format, bool &r_is_srgb_format) const override
bool checkVersionRequirements(GHOST_Context &ghost_ctx, XrInstance instance, XrSystemId system_id, std::string *r_requirement_info) const override
std::vector< XrSwapchainImageBaseHeader * > createSwapchainImages(uint32_t image_count) override
void submitToSwapchainImage(XrSwapchainImageBaseHeader &swapchain_image, const GHOST_XrDrawViewInfo &draw_info) override
bool needsUpsideDownDrawing(GHOST_Context &ghost_ctx) const override
std::list< std::vector< XrSwapchainImageMetalKHR > > image_cache_
bool loadExtensionFunctions(XrInstance instance) override
load the functions for the graphics binding.
void initFromGhostContext(GHOST_Context &ghost_ctx, XrInstance instance, XrSystemId system_id) override
GHOST_XrGraphicsBindingMetal(GHOST_Context &ghost_ctx)
#define image(slot, qualifiers, format)