Blender V5.3
GHOST_XrGraphicsBindingD3D.cc
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#ifndef _WIN32
10# error "GHOST_XrGraphcisBindingD3D can only be compiled on Windows platforms."
11#endif
12
13#include <algorithm>
14#include <sstream>
15
16#include "GHOST_SystemWin32.hh"
17#include "GHOST_XrException.hh"
19
20static void ghost_format_to_dx_format(GHOST_TXrSwapchainFormat ghost_format,
21 bool expects_srgb_buffer,
22 DXGI_FORMAT &r_dx_format)
23{
24 r_dx_format = DXGI_FORMAT_UNKNOWN;
25
26 switch (ghost_format) {
27 case GHOST_kXrSwapchainFormatRGBA8:
28 r_dx_format = expects_srgb_buffer ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB :
29 DXGI_FORMAT_R8G8B8A8_UNORM;
30 break;
31 case GHOST_kXrSwapchainFormatRGBA16:
32 r_dx_format = DXGI_FORMAT_R16G16B16A16_UNORM;
33 break;
34 case GHOST_kXrSwapchainFormatRGBA16F:
35 r_dx_format = DXGI_FORMAT_R16G16B16A16_FLOAT;
36 break;
37 case GHOST_kXrSwapchainFormatRGB10_A2:
38 r_dx_format = DXGI_FORMAT_R10G10B10A2_UNORM;
39 break;
40 }
41
42 if (r_dx_format == DXGI_FORMAT_UNKNOWN) {
43 throw GHOST_XrException("No supported DirectX swapchain format found.");
44 }
45}
46
47static std::optional<int64_t> choose_swapchain_format_from_candidates(
48 const std::vector<int64_t> &gpu_binding_formats, const std::vector<int64_t> &runtime_formats)
49{
50 if (gpu_binding_formats.empty()) {
51 return std::nullopt;
52 }
53
54 auto res = std::find_first_of(gpu_binding_formats.begin(),
55 gpu_binding_formats.end(),
56 runtime_formats.begin(),
57 runtime_formats.end());
58 if (res == gpu_binding_formats.end()) {
59 return std::nullopt;
60 }
61
62 return *res;
63}
64
65/* -------------------------------------------------------------------- */
68
79
81{
82#define LOAD_FUNCTION(fn_ptr, name) \
83 if (XR_FAILED(xrGetInstanceProcAddr(instance, #name, (PFN_xrVoidFunction *)&fn_ptr))) { \
84 return false; \
85 }
86
87 LOAD_FUNCTION(xrGetD3D11GraphicsRequirementsKHR_, xrGetD3D11GraphicsRequirementsKHR);
88
89#undef LOAD_FUNCTION
90 return true;
91}
92
94 GHOST_Context & /*ghost_ctx*/, /* Remember: This is the OpenGL context! */
95 XrInstance instance,
96 XrSystemId system_id,
97 std::string *r_requirement_info) const
98{
99 XrGraphicsRequirementsD3D11KHR gpu_requirements = {XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR};
100 xrGetD3D11GraphicsRequirementsKHR_(instance, system_id, &gpu_requirements);
101
102 if (r_requirement_info) {
103 std::ostringstream strstream;
104 strstream << "Minimum DirectX 11 Feature Level " << gpu_requirements.minFeatureLevel
105 << std::endl;
106
107 *r_requirement_info = strstream.str();
108 }
109
110 return ghost_d3d_ctx_->device_->GetFeatureLevel() >= gpu_requirements.minFeatureLevel;
111}
112
114 GHOST_Context & /*ghost_ctx*/ /* Remember: This is the OpenGL context! */,
115 XrInstance /*instance*/,
116 XrSystemId /*system_id*/
117)
118{
119 oxr_binding.d3d11.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
120 oxr_binding.d3d11.device = ghost_d3d_ctx_->device_;
121}
122
124 const std::vector<int64_t> &runtime_formats,
125 GHOST_TXrSwapchainFormat &r_format,
126 bool &r_is_srgb_format) const
127{
128 std::vector<int64_t> gpu_binding_formats = {
129#if 0 /* RGB10A2, RGBA16 don't seem to work with Oculus head-sets, \
130 * so move them after RGBA16F for the time being. */
131 DXGI_FORMAT_R10G10B10A2_UNORM,
132 DXGI_FORMAT_R16G16B16A16_UNORM,
133#endif
134 DXGI_FORMAT_R16G16B16A16_FLOAT,
135#if 1
136 DXGI_FORMAT_R10G10B10A2_UNORM,
137 DXGI_FORMAT_R16G16B16A16_UNORM,
138#endif
139 DXGI_FORMAT_R8G8B8A8_UNORM,
140 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
141 };
142
143 std::optional result = choose_swapchain_format_from_candidates(gpu_binding_formats,
144 runtime_formats);
145 if (result) {
146 switch (*result) {
147 case DXGI_FORMAT_R10G10B10A2_UNORM:
148 r_format = GHOST_kXrSwapchainFormatRGB10_A2;
149 break;
150 case DXGI_FORMAT_R16G16B16A16_UNORM:
151 r_format = GHOST_kXrSwapchainFormatRGBA16;
152 break;
153 case DXGI_FORMAT_R16G16B16A16_FLOAT:
154 r_format = GHOST_kXrSwapchainFormatRGBA16F;
155 break;
156 case DXGI_FORMAT_R8G8B8A8_UNORM:
157 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
158 r_format = GHOST_kXrSwapchainFormatRGBA8;
159 break;
160 }
161 r_is_srgb_format = (*result == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB);
162 }
163 else {
164 r_format = GHOST_kXrSwapchainFormatRGBA8;
165 r_is_srgb_format = false;
166 }
167
168 return result;
169}
170
171std::vector<XrSwapchainImageBaseHeader *> GHOST_XrGraphicsBindingD3D::createSwapchainImages(
172 uint32_t image_count)
173{
174 std::vector<XrSwapchainImageD3D11KHR> d3d_images(image_count);
175 std::vector<XrSwapchainImageBaseHeader *> base_images;
176
177 /* Need to return vector of base header pointers, so of a different type. Need to build a new
178 * list with this type, and keep the initial one alive. */
179 for (XrSwapchainImageD3D11KHR &image : d3d_images) {
180 image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR;
181 base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
182 }
183
184 /* Keep alive. */
185 image_cache_.push_back(std::move(d3d_images));
186
187 return base_images;
188}
189
194
196
197/* -------------------------------------------------------------------- */
200
206
208{
209 if (shared_resource_) {
211 shared_resource_ = nullptr;
212 }
213}
214
216 XrSwapchainImageBaseHeader &swapchain_image, const GHOST_XrDrawViewInfo &draw_info)
217{
218 XrSwapchainImageD3D11KHR &d3d_swapchain_image = reinterpret_cast<XrSwapchainImageD3D11KHR &>(
219 swapchain_image);
220
221#if 0
222 /* Ideally we'd just create a render target view for the OpenXR swap-chain image texture and
223 * blit from the OpenGL context into it. The NV_DX_interop extension doesn't want to work with
224 * this though. At least not with OPTIMUS hardware. See:
225 * https://github.com/mpv-player/mpv/issues/2949#issuecomment-197262807.
226 */
227
228 ID3D11RenderTargetView *rtv;
229 CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D,
230 DXGI_FORMAT_R8G8B8A8_UNORM);
231
232 ghost_ctx_->device_->CreateRenderTargetView(d3d_swapchain_image.texture, &rtv_desc, &rtv);
233 if (!shared_resource_) {
234 DXGI_FORMAT format;
235 ghost_format_to_dx_format(draw_info.swapchain_format, draw_info.expects_srgb_buffer, format);
236 shared_resource_ = ghost_ctx_->createSharedOpenGLResource(
237 draw_info.width, draw_info.height, format, rtv);
238 }
239 ghost_ctx_->blitFromOpenGLContext(shared_resource_, draw_info.width, draw_info.height);
240#else
241 if (!shared_resource_) {
242 DXGI_FORMAT format;
243 ghost_format_to_dx_format(draw_info.swapchain_format, draw_info.expects_srgb_buffer, format);
244 shared_resource_ = ghost_d3d_ctx_->createSharedOpenGLResource(
245 draw_info.width, draw_info.height, format);
246 }
247 ghost_d3d_ctx_->blitFromOpenGLContext(shared_resource_, draw_info.width, draw_info.height);
248
249 ghost_d3d_ctx_->device_ctx_->OMSetRenderTargets(0, nullptr, nullptr);
250 ghost_d3d_ctx_->device_ctx_->CopyResource(d3d_swapchain_image.texture,
251 ghost_d3d_ctx_->getSharedTexture2D(shared_resource_));
252#endif
253}
254
256
257#ifdef WITH_VULKAN_BACKEND
258
259/* -------------------------------------------------------------------- */
262
263GHOST_XrGraphicsBindingVulkanD3D::GHOST_XrGraphicsBindingVulkanD3D(GHOST_Context &ghost_ctx)
264
265 : GHOST_XrGraphicsBindingD3D(), ghost_ctx_(static_cast<GHOST_ContextVK &>(ghost_ctx))
266{
267}
268
269GHOST_XrGraphicsBindingVulkanD3D::~GHOST_XrGraphicsBindingVulkanD3D() {}
270
271void GHOST_XrGraphicsBindingVulkanD3D::submitToSwapchainImage(
272 XrSwapchainImageBaseHeader &swapchain_image, const GHOST_XrDrawViewInfo &draw_info)
273{
274 XrSwapchainImageD3D11KHR &d3d_swapchain_image = reinterpret_cast<XrSwapchainImageD3D11KHR &>(
275 swapchain_image);
276
277 VkDeviceSize component_size = 4 * sizeof(uint8_t);
278 if (draw_info.swapchain_format == GHOST_kXrSwapchainFormatRGBA16F ||
279 draw_info.swapchain_format == GHOST_kXrSwapchainFormatRGBA16)
280 {
281 component_size = 4 * sizeof(uint16_t);
282 }
283
284 ID3D11Device *d3d_device = ghost_d3d_ctx_->device_;
285 ID3D11DeviceContext *d3d_device_ctx = ghost_d3d_ctx_->device_ctx_;
286 DXGI_FORMAT format;
287 ghost_format_to_dx_format(draw_info.swapchain_format, draw_info.expects_srgb_buffer, format);
288
289 /* Acquire frame buffer image. */
290 GHOST_VulkanOpenXRData openxr_data = {GHOST_kVulkanXRModeCPU};
291 ghost_ctx_.openxr_acquire_framebuffer_image_callback_(&openxr_data);
292
293 /* Upload the data to a D3D Texture */
294 D3D11_TEXTURE2D_DESC desc;
295 desc.Width = openxr_data.extent.width;
296 desc.Height = openxr_data.extent.height;
297 desc.MipLevels = 1;
298 desc.ArraySize = 1;
299 desc.Format = format;
300 desc.SampleDesc.Count = 1;
301 desc.SampleDesc.Quality = 0;
302 desc.Usage = D3D11_USAGE_IMMUTABLE;
303 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
304 desc.CPUAccessFlags = 0;
305 desc.MiscFlags = 0;
306
307 D3D11_SUBRESOURCE_DATA data;
308 data.pSysMem = openxr_data.cpu.image_data;
309 data.SysMemPitch = component_size * openxr_data.extent.width;
310 data.SysMemSlicePitch = 0;
311
312 ID3D11Texture2D *texture = nullptr;
313 d3d_device->CreateTexture2D(&desc, &data, &texture);
314
315 /* Copy sub-resource of the uploaded texture to the swap-chain texture. */
316 d3d_device_ctx->CopySubresourceRegion(
317 d3d_swapchain_image.texture, 0, draw_info.ofsx, draw_info.ofsy, 0, texture, 0, nullptr);
318
319 /* Release the texture. */
320 texture->Release();
321
322 /* Release frame buffer image. */
323 ghost_ctx_.openxr_release_framebuffer_image_callback_(&openxr_data);
324}
325
327
328#endif
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)
#define LOAD_FUNCTION(fn_ptr, name)
static void ghost_format_to_dx_format(GHOST_TXrSwapchainFormat ghost_format, bool expects_srgb_buffer, DXGI_FORMAT &r_dx_format)
bool isUpsideDown() const override
GHOST_TSuccess blitFromOpenGLContext(class GHOST_SharedOpenGLResource *shared_res, unsigned int width, unsigned int height)
ID3D11Texture2D * getSharedTexture2D(class GHOST_SharedOpenGLResource *shared_res)
class GHOST_SharedOpenGLResource * createSharedOpenGLResource(unsigned int width, unsigned int height, DXGI_FORMAT format, ID3D11RenderTargetView *render_target)
void disposeSharedOpenGLResource(class GHOST_SharedOpenGLResource *shared_res)
GHOST_IXrGraphicsBinding()=default
union GHOST_IXrGraphicsBinding::@011235222313272276112307021312244353112200226155 oxr_binding
static GHOST_ContextD3D * createOffscreenContextD3D()
static GHOST_TSuccess disposeContextD3D(GHOST_ContextD3D *context)
GHOST_XrException(const char *msg, int result=0)
bool checkVersionRequirements(GHOST_Context &ghost_ctx, XrInstance instance, XrSystemId system_id, std::string *r_requirement_info) const override
std::list< std::vector< XrSwapchainImageD3D11KHR > > image_cache_
std::optional< int64_t > chooseSwapchainFormat(const std::vector< int64_t > &runtime_formats, GHOST_TXrSwapchainFormat &r_format, bool &r_is_srgb_format) const override
std::vector< XrSwapchainImageBaseHeader * > createSwapchainImages(uint32_t image_count) override
bool needsUpsideDownDrawing(GHOST_Context &ghost_ctx) const override
void initFromGhostContext(GHOST_Context &ghost_ctx, XrInstance instance, XrSystemId system_id) override
bool loadExtensionFunctions(XrInstance instance) override
load the functions for the graphics binding.
GHOST_XrGraphicsBindingOpenGLD3D(GHOST_Context &ghost_ctx)
void submitToSwapchainImage(XrSwapchainImageBaseHeader &swapchain_image, const GHOST_XrDrawViewInfo &draw_info) override
#define image(slot, qualifiers, format)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
unsigned short uint16_t
Definition BKE_text.h:80
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
std::string format(const std::string &fmt, Args &&...args)
Definition diagnostic.hh:43