Monado OpenXR Runtime
Loading...
Searching...
No Matches
d3d_winrt_helpers.hpp
Go to the documentation of this file.
1// Copyright 2019-2024, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Helpers for Windows 10+ direct mode code.
6 *
7 * Has to implement comp_target instead of comp_target_swapchain because
8 * we don't get a VkSurfaceKHR, etc: we manually import images instead.
9 *
10 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
11 * @ingroup aux_d3d
12 */
13
14#pragma once
15
16#include "math/m_rational.hpp"
17
18#include "xrt/xrt_compiler.h"
20
21#include <winrt/Windows.Graphics.DirectX.h>
22#include <winrt/Windows.Devices.Display.Core.h>
23
24#include <stdint.h>
25#include <utility>
26#include <tuple>
27
28namespace xrt::auxiliary::d3d::winrt {
29
31parseVerticalRefreshRate(const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &modeInfo)
32{
33 return xrt::auxiliary::math::Rational<uint32_t>{modeInfo.PresentationRate().VerticalSyncRate.Numerator,
34 modeInfo.PresentationRate().VerticalSyncRate.Denominator};
35}
36
37bool
38isModeAcceptable(const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &modeInfo);
39
40/*!
41 * A comparison function (like std::less) for comparing display modes, such as for
42 * sorting or finding the "best" one.
43 *
44 * This sorts with SRGB modes first, and refresh rate in decreasing order.
45 */
46bool
47modeComparison(const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &lhs,
48 const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &rhs);
49
50static inline bool
51colorSpaceFromVulkan(VkColorSpaceKHR colorSpace, ::winrt::Windows::Graphics::DirectX::DirectXColorSpace &out)
52{
53 using ::winrt::Windows::Graphics::DirectX::DirectXColorSpace;
54 switch (colorSpace) {
55 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR: out = DirectXColorSpace::RgbFullG22NoneP709; return true;
56 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT: out = DirectXColorSpace::RgbFullG10NoneP709; return true;
57 }
58 return false;
59}
60
61/*!
62 * Describes the supported Windows API features for direct mode.
63 */
65{
66 //! Supports the minimum WinRT API contract (7.0) for direct mode
68
69 //! Supports the WinRT API contract (14.0) for the improved direct mode that debuted in Windows 11
71
72 //! Populate the fields based on the system we are running on
73 void
74 populate();
75};
76
77/*!
78 * Create a "scanout" object for scanning out a surface to a direct mode display.
79 *
80 * The underlying function CreateSimpleScanout is prone to spurious IllegalArgument failures, so
81 * this wrapper tries twice.
82 *
83 * See https://github.com/MicrosoftDocs/winrt-api/issues/1942
84 *
85 * @param capability A populated @ref SystemApiCapability describing the features available and allowed for use.
86 * @param maxAttempts The maximum number of times to try creating a scanout: consider at least 2, since this is prone to
87 * spurious failure
88 * @param device Display device
89 * @param source Display source
90 * @param primary Primary display surface
91 * @param subResourceIndex as in CreateSimpleScanout. Usually 0 unless you are using hardware stereo (like 3D TV)
92 * @param allowTearing determines the sync interval value in Win10 and Win11, and the flags to create the scanout in
93 * Win11.
94 * @return ::winrt::Windows::Devices::Display::Core::DisplayScanout
95 */
96::winrt::Windows::Devices::Display::Core::DisplayScanout
97createScanout(SystemApiCapability const &capability,
98 int maxAttempts,
99 ::winrt::Windows::Devices::Display::Core::DisplayDevice const &device,
100 ::winrt::Windows::Devices::Display::Core::DisplaySource const &source,
101 ::winrt::Windows::Devices::Display::Core::DisplaySurface const &primary,
102 uint32_t subResourceIndex,
103 bool allowTearing);
104
105
106#define D3D_WINRT_MAKE_STRINGIFY_CASE(WDDC_ENUM) \
107 case ::winrt::Windows::Devices::Display::Core::WDDC_ENUM: return #WDDC_ENUM
108
109//! Convert Windows.Devices.Display.Core.DisplayManagerResult enum values to string
110XRT_CHECK_RESULT constexpr const char *
111to_string(::winrt::Windows::Devices::Display::Core::DisplayManagerResult e)
112{
113 switch (e) {
114 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayManagerResult::Success);
115 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayManagerResult::UnknownFailure);
116 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayManagerResult::TargetAccessDenied);
117 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayManagerResult::TargetStale);
118 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayManagerResult::RemoteSessionNotSupported);
119 }
120 return "DisplayManagerResult::UNKNOWN";
121}
122
123//! Convert Windows.Devices.Display.Core.DisplayStateOperationStatus enum values to string
124XRT_CHECK_RESULT constexpr const char *
125to_string(::winrt::Windows::Devices::Display::Core::DisplayStateOperationStatus e)
126{
127 switch (e) {
128 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::Success);
129 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::PartialFailure);
130 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::UnknownFailure);
131 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::TargetOwnershipLost);
132 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::SystemStateChanged);
133 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::TooManyPathsForAdapter);
134 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::ModesNotSupported);
135 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayStateOperationStatus::RemoteSessionNotSupported);
136 }
137 return "DisplayStateOperationStatus::UNKNOWN";
138}
139
140//! Convert Windows.Devices.Display.Core.DisplayPathStatus enum values to string
141XRT_CHECK_RESULT constexpr const char *
142to_string(::winrt::Windows::Devices::Display::Core::DisplayPathStatus e)
143{
144 switch (e) {
145 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::Unknown);
146 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::Succeeded);
147 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::Pending);
148 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::Failed);
149 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::FailedAsync);
150 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPathStatus::InvalidatedAsync);
151 }
152 return "DisplayPathStatus::UNKNOWN";
153}
154
155//! Convert Windows.Devices.Display.Core.DisplayPresentStatus enum values to string
156XRT_CHECK_RESULT constexpr const char *
157to_string(::winrt::Windows::Devices::Display::Core::DisplayPresentStatus e)
158{
159 switch (e) {
160 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::Success);
161 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::SourceStatusPreventedPresent);
162 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::ScanoutInvalid);
163 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::SourceInvalid);
164 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::DeviceInvalid);
165 D3D_WINRT_MAKE_STRINGIFY_CASE(DisplayPresentStatus::UnknownFailure);
166 }
167 return "DisplayPresentStatus::UNKNOWN";
168}
169#undef D3D_WINRT_MAKE_STRINGIFY_CASE
170} // namespace xrt::auxiliary::d3d::winrt
bool modeComparison(const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &lhs, const ::winrt::Windows::Devices::Display::Core::DisplayModeInfo &rhs)
A comparison function (like std::less) for comparing display modes, such as for sorting or finding th...
Definition d3d_winrt_helpers.cpp:83
XRT_CHECK_RESULT constexpr const char * to_string(::winrt::Windows::Devices::Display::Core::DisplayManagerResult e)
Convert Windows.Devices.Display.Core.DisplayManagerResult enum values to string.
Definition d3d_winrt_helpers.hpp:111
A very simple rational number type.
Describes the supported Windows API features for direct mode.
Definition d3d_winrt_helpers.hpp:65
void populate()
Populate the fields based on the system we are running on.
Definition d3d_winrt_helpers.cpp:112
bool supportsBasicDirectMode
Supports the minimum WinRT API contract (7.0) for direct mode.
Definition d3d_winrt_helpers.hpp:67
bool supportsScanoutOptionsAndTryExecuteTask
Supports the WinRT API contract (14.0) for the improved direct mode that debuted in Windows 11.
Definition d3d_winrt_helpers.hpp:70
A rational (fractional) number type.
Definition m_rational.hpp:26
Header holding common defines.
Include all of the Vulkan headers in one place, and cope with any "messy" includes implied by it.