The open source OpenXR runtime
1// Copyright 2019, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Settings struct for compositor.
6 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
7 * @ingroup comp_main
8 */
9
10#include "util/u_debug.h"
11#include "comp_settings.h"
12
13#ifdef XRT_OS_ANDROID
14#define USE_COMPUTE_DEFAULT false
15#else
16#define USE_COMPUTE_DEFAULT true
17#endif
18
19// clang-format off
20DEBUG_GET_ONCE_LOG_OPTION(log, "XRT_COMPOSITOR_LOG", U_LOGGING_INFO)
21DEBUG_GET_ONCE_BOOL_OPTION(print_modes, "XRT_COMPOSITOR_PRINT_MODES", false)
22DEBUG_GET_ONCE_BOOL_OPTION(force_randr, "XRT_COMPOSITOR_FORCE_RANDR", false)
23DEBUG_GET_ONCE_BOOL_OPTION(force_wayland_direct, "XRT_COMPOSITOR_FORCE_WAYLAND_DIRECT", false)
24DEBUG_GET_ONCE_BOOL_OPTION(force_nvidia, "XRT_COMPOSITOR_FORCE_NVIDIA", false)
25DEBUG_GET_ONCE_OPTION(nvidia_display, "XRT_COMPOSITOR_FORCE_NVIDIA_DISPLAY", NULL)
26DEBUG_GET_ONCE_NUM_OPTION(vk_display, "XRT_COMPOSITOR_FORCE_VK_DISPLAY", -1)
27DEBUG_GET_ONCE_BOOL_OPTION(force_xcb, "XRT_COMPOSITOR_FORCE_XCB", false)
28DEBUG_GET_ONCE_BOOL_OPTION(force_wayland, "XRT_COMPOSITOR_FORCE_WAYLAND", false)
29DEBUG_GET_ONCE_NUM_OPTION(force_gpu_index, "XRT_COMPOSITOR_FORCE_GPU_INDEX", -1)
30DEBUG_GET_ONCE_NUM_OPTION(force_client_gpu_index, "XRT_COMPOSITOR_FORCE_CLIENT_GPU_INDEX", -1)
31DEBUG_GET_ONCE_NUM_OPTION(desired_mode, "XRT_COMPOSITOR_DESIRED_MODE", -1)
32DEBUG_GET_ONCE_NUM_OPTION(scale_percentage, "XRT_COMPOSITOR_SCALE_PERCENTAGE", 140)
33DEBUG_GET_ONCE_BOOL_OPTION(xcb_fullscreen, "XRT_COMPOSITOR_XCB_FULLSCREEN", false)
34DEBUG_GET_ONCE_NUM_OPTION(xcb_display, "XRT_COMPOSITOR_XCB_DISPLAY", -1)
35DEBUG_GET_ONCE_NUM_OPTION(default_framerate, "XRT_COMPOSITOR_DEFAULT_FRAMERATE", 60)
36DEBUG_GET_ONCE_BOOL_OPTION(compute, "XRT_COMPOSITOR_COMPUTE", USE_COMPUTE_DEFAULT)
37// clang-format on
38
39static inline void
40add_format(struct comp_settings *s, VkFormat format)
41{
42 uint32_t count = s->format_count;
43
44 // Just in case, but should never happen.
45 if (count >= ARRAY_SIZE(s->formats)) {
46 U_LOG_E("Too many formats!");
47 return;
48 }
49
50 s->formats[count++] = format;
51 s->format_count = count;
52}
53
54
55/*
56 *
57 * 'Exported' functions.
58 *
59 */
60
61void
62comp_settings_init(struct comp_settings *s, struct xrt_device *xdev)
63{
64 int default_framerate = debug_get_num_option_default_framerate();
65
66 uint64_t interval_ns = xdev->hmd->screens[0].nominal_frame_interval_ns;
67 if (interval_ns == 0) {
68 interval_ns = (1000 * 1000 * 1000) / default_framerate;
69 }
70
71 s->use_compute = debug_get_bool_option_compute();
72
73 if (s->use_compute) {
74 // Tested working with a PSVR2 and a patched Mesa. Native format of the PSVR2. 10-bit formats should be
75 // preferred in all cases for lower colour banding.
76 add_format(s, VK_FORMAT_A2B10G10R10_UNORM_PACK32);
77
78 // Default 8-bit channel 32-bit pixel format, most commonly supported UNORM format across Windows and
79 // Linux.
80 add_format(s, VK_FORMAT_B8G8R8A8_UNORM);
81
82 // Next most common UNORM format.
83 add_format(s, VK_FORMAT_R8G8B8A8_UNORM);
84
85 // Seen on some NVIDIA cards.
86 add_format(s, VK_FORMAT_A8B8G8R8_UNORM_PACK32);
87
88 // 32-bit formats should be preferred over a 64-bit format, for performance/memory reasons.
89 add_format(s, VK_FORMAT_R16G16B16A16_SFLOAT);
90
91 // Untested: Super constrained platforms. Absolute last resort.
92 add_format(s, VK_FORMAT_A1R5G5B5_UNORM_PACK16);
93 } else {
94#if defined(XRT_OS_ANDROID)
95 /*
96 * On Android the most ubiquitous sRGB format is R8G8B8A8_SRGB.
97 * https://vulkan.gpuinfo.org/listsurfaceformats.php?platform=android
98 */
99 add_format(s, VK_FORMAT_R8G8B8A8_SRGB);
100
101 // Fallback
102 add_format(s, VK_FORMAT_B8G8R8A8_SRGB);
103#elif defined(XRT_OS_LINUX) || defined(XRT_OS_WINDOWS)
104 /*
105 * On Linux the most ubiquitous sRGB format is B8G8R8A8_SRGB.
106 * https://vulkan.gpuinfo.org/listsurfaceformats.php?platform=linux
107 *
108 * On Windows the most ubiquitous sRGB format is B8G8R8A8_SRGB.
109 * https://vulkan.gpuinfo.org/listsurfaceformats.php?platform=windows
110 */
111 add_format(s, VK_FORMAT_B8G8R8A8_SRGB);
112
113 // Fallback
114 add_format(s, VK_FORMAT_R8G8B8A8_SRGB);
115#else
116#error "Need to pick default swapchain format for this platform!"
117#endif
118
119 // Seen as the only sRGB format on some NVIDIA cards.
120 add_format(s, VK_FORMAT_A8B8G8R8_SRGB_PACK32);
121 }
122
123 s->display = debug_get_num_option_xcb_display();
124 s->color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
125 s->present_mode = VK_PRESENT_MODE_FIFO_KHR;
126 s->fullscreen = debug_get_bool_option_xcb_fullscreen();
127 s->preferred.width = xdev->hmd->screens[0].w_pixels;
128 s->preferred.height = xdev->hmd->screens[0].h_pixels;
129 s->nominal_frame_interval_ns = interval_ns;
130 s->log_level = debug_get_log_option_log();
131 s->print_modes = debug_get_bool_option_print_modes();
132 s->selected_gpu_index = debug_get_num_option_force_gpu_index();
133 s->client_gpu_index = debug_get_num_option_force_client_gpu_index();
134 s->desired_mode = debug_get_num_option_desired_mode();
135 s->viewport_scale = debug_get_num_option_scale_percentage() / 100.0;
136
137
138 s->nvidia_display = debug_get_option_nvidia_display();
139 if (debug_get_bool_option_force_nvidia()) {
140 s->target_identifier = "x11_direct_nvidia";
141 }
142
143 s->vk_display = debug_get_num_option_vk_display();
144 if (s->vk_display >= 0) {
145 s->target_identifier = "vk_display";
146 }
147
148 if (debug_get_bool_option_force_randr()) {
149 s->target_identifier = "x11_direct";
150 }
151
152 if (debug_get_bool_option_force_wayland_direct()) {
153 s->target_identifier = "direct_wayland";
154 }
155
156 if (debug_get_bool_option_force_xcb()) {
157 s->target_identifier = "x11";
158
159 // HMD screen tends to be much larger then monitors.
160 s->preferred.width /= 2;
161 s->preferred.height /= 2;
162 }
163 if (debug_get_bool_option_force_wayland()) {
164 s->target_identifier = "wayland";
165
166 // HMD screen tends to be much larger then monitors.
167 s->preferred.width /= 2;
168 s->preferred.height /= 2;
169 }
170}