Monado OpenXR Runtime
comp_compositor.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Main compositor written using Vulkan header.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
8  * @ingroup comp_main
9  */
10 
11 #pragma once
12 
13 #include "main/comp_settings.h"
14 #include "main/comp_window.h"
15 #include "main/comp_renderer.h"
16 
17 #include "xrt/xrt_gfx_vk.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define NUM_FRAME_TIMES 50
24 
25 /*
26  *
27  * Structs
28  *
29  */
30 
31 /*!
32  * A single swapchain image, holds the needed state for tracking image usage.
33  *
34  * @ingroup comp_main
35  */
37 {
38  //! Vulkan image to create view from.
39  VkImage image;
40  //! Exported memory backing the image.
41  VkDeviceMemory memory;
42  //! Sampler used by the renderer and distortion code.
43  VkSampler sampler;
44  //! Views used by the renderer and distortion code, for each array
45  //! layer.
46  VkImageView *views;
47 };
48 
49 /*!
50  * A swapchain that is almost a one to one mapping to a OpenXR swapchain.
51  *
52  * Not used by the window backend that uses the vk_swapchain to render to.
53  *
54  * @ingroup comp_main
55  */
57 {
58  struct xrt_swapchain_fd base;
59 
60  struct comp_compositor *c;
61 
62  struct comp_swapchain_image images[XRT_MAX_SWAPCHAIN_IMAGES];
63 };
64 
65 /*!
66  * Main compositor struct tying everything in the compositor together.
67  *
68  * @ingroup comp_main
69  */
71 {
72  struct xrt_compositor_fd base;
73 
74  //! A link back to the compositor we are presenting to the client.
76 
77  //! Renderer helper.
78  struct comp_renderer *r;
79 
80  //! The window or display we are using.
82 
83  //! The device we are displaying to.
84  struct xrt_device *xdev;
85 
86  //! The settings.
87  struct comp_settings settings;
88 
89  //! Vulkan bundle of things.
90  struct vk_bundle vk;
91 
92  //! Timestamp of last-rendered (immersive) frame.
94 
95  /*!
96  * @brief Data exclusive to the begin_frame/end_frame for computing an
97  * estimate of the app's needs.
98  */
99  struct
100  {
101  int64_t last_begin;
102  int64_t last_end;
103  } app_profiling;
104 
105  //! The time our compositor needs to do rendering
107 
108  struct
109  {
110  //! Current Index for times_ns.
111  int index;
112 
113  //! Timestamps of last-rendered (immersive) frames.
114  int64_t times_ns[NUM_FRAME_TIMES];
115 
116  //! Frametimes between last-rendered (immersive) frames.
117  float timings_ms[NUM_FRAME_TIMES];
118 
119  //! Average FPS of last NUM_FRAME_TIMES rendered frames.
120  float fps;
121 
122  struct u_var_timing *debug_var;
123  } compositor_frame_times;
124 
125  /*!
126  * @brief Estimated rendering time per frame of the application.
127  *
128  * Set by the begin_frame/end_frame code.
129  *
130  * @todo make this atomic.
131  */
133  //! The last time we provided in the results of wait_frame
135 
136  /*!
137  * The current state we are tracking.
138  *
139  * Settings is supposed to be read only.
140  */
141  struct
142  {
143  uint32_t width;
144  uint32_t height;
145  } current;
146 };
147 
148 
149 /*
150  *
151  * Functions and helpers.
152  *
153  */
154 
155 /*!
156  * Convinence function to convert a xrt_swapchain to a comp_swapchain.
157  *
158  * @ingroup comp_main
159  */
160 static inline struct comp_swapchain *
161 comp_swapchain(struct xrt_swapchain *xsc)
162 {
163  return (struct comp_swapchain *)xsc;
164 }
165 
166 /*!
167  * Convinence function to convert a xrt_compositor to a comp_compositor.
168  *
169  * @ingroup comp_main
170  */
171 static inline struct comp_compositor *
173 {
174  return (struct comp_compositor *)xc;
175 }
176 
177 /*!
178  * A compositor function that is implemented in the swapchain code.
179  *
180  * @ingroup comp_main
181  */
182 struct xrt_swapchain *
184  enum xrt_swapchain_create_flags create,
185  enum xrt_swapchain_usage_bits bits,
186  int64_t format,
187  uint32_t sample_count,
188  uint32_t width,
189  uint32_t height,
190  uint32_t face_count,
191  uint32_t array_size,
192  uint32_t mip_count);
193 
194 /*!
195  * Free and destroy any initialized fields on the given image, safe to pass in
196  * images that has one or all fields set to NULL.
197  *
198  * @ingroup comp_main
199  */
200 void
202  uint32_t array_size,
203  struct comp_swapchain_image *image);
204 
205 /*!
206  * Printer helper.
207  *
208  * @ingroup comp_main
209  */
210 void
212  const char *func,
213  const char *fmt,
214  ...) XRT_PRINTF_FORMAT(3, 4);
215 
216 /*!
217  * Spew level logging.
218  *
219  * @ingroup comp_main
220  */
221 #define COMP_SPEW(c, ...) \
222  do { \
223  if (c->settings.print_spew) { \
224  comp_compositor_print(c, __func__, __VA_ARGS__); \
225  } \
226  } while (false)
227 
228 /*!
229  * Debug level logging.
230  *
231  * @ingroup comp_main
232  */
233 #define COMP_DEBUG(c, ...) \
234  do { \
235  if (c->settings.print_debug) { \
236  comp_compositor_print(c, __func__, __VA_ARGS__); \
237  } \
238  } while (false)
239 
240 /*!
241  * Mode printing.
242  *
243  * @ingroup comp_main
244  */
245 #define COMP_PRINT_MODE(c, ...) \
246  do { \
247  if (c->settings.print_modes) { \
248  comp_compositor_print(c, __func__, __VA_ARGS__); \
249  } \
250  } while (false)
251 
252 /*!
253  * Error level logging.
254  *
255  * @ingroup comp_main
256  */
257 #define COMP_ERROR(c, ...) \
258  do { \
259  comp_compositor_print(c, __func__, __VA_ARGS__); \
260  } while (false)
261 
262 
263 #ifdef __cplusplus
264 }
265 #endif
VkImageView * views
Views used by the renderer and distortion code, for each array layer.
Definition: comp_compositor.h:46
Definition: u_var.h:33
Main compositor.
Definition: xrt_compositor.h:474
int64_t expected_app_duration_ns
Estimated rendering time per frame of the application.
Definition: comp_compositor.h:132
Settings for the compositor.
Definition: comp_settings.h:53
struct comp_window * window
The window or display we are using.
Definition: comp_compositor.h:81
struct xrt_swapchain * comp_swapchain_create(struct xrt_compositor *xc, enum xrt_swapchain_create_flags create, enum xrt_swapchain_usage_bits bits, int64_t format, uint32_t sample_count, uint32_t width, uint32_t height, uint32_t face_count, uint32_t array_size, uint32_t mip_count)
A compositor function that is implemented in the swapchain code.
Definition: comp_swapchain.c:215
int64_t last_frame_time_ns
Timestamp of last-rendered (immersive) frame.
Definition: comp_compositor.h:93
Settings struct for compositor header.
Main compositor struct tying everything in the compositor together.
Definition: comp_compositor.h:70
A swapchain that exposes fd to be imported into a client API.
Definition: xrt_compositor.h:462
void comp_compositor_print(struct comp_compositor *c, const char *func, const char *fmt,...) XRT_PRINTF_FORMAT(3
Printer helper.
VkDeviceMemory memory
Exported memory backing the image.
Definition: comp_compositor.h:41
int64_t last_next_display_time
The last time we provided in the results of wait_frame.
Definition: comp_compositor.h:134
Holds associated vulkan objects and state to render with a distortion.
Definition: comp_renderer.c:33
VkSampler sampler
Sampler used by the renderer and distortion code.
Definition: comp_compositor.h:43
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition: xrt_compositor.h:48
A single swapchain image, holds the needed state for tracking image usage.
Definition: comp_compositor.h:36
float fps
Average FPS of last NUM_FRAME_TIMES rendered frames.
Definition: comp_compositor.h:120
Common swapchain base.
Definition: xrt_compositor.h:75
void comp_swapchain_image_cleanup(struct vk_bundle *vk, uint32_t array_size, struct comp_swapchain_image *image)
Free and destroy any initialized fields on the given image, safe to pass in images that has one or al...
Definition: comp_swapchain.c:308
struct comp_renderer * r
Renderer helper.
Definition: comp_compositor.h:78
Compositor rendering code header.
uint32_t array_size
Number of array layers per image.
Definition: xrt_compositor.h:85
Common compositor base.
Definition: xrt_compositor.h:116
A output device or a window, often directly connected to the device.
Definition: comp_window.h:32
int64_t frame_overhead_ns
The time our compositor needs to do rendering.
Definition: comp_compositor.h:106
Compositor window header.
struct xrt_compositor * client
A link back to the compositor we are presenting to the client.
Definition: comp_compositor.h:75
Header defining a XRT graphics provider.
int index
Current Index for times_ns.
Definition: comp_compositor.h:111
struct xrt_device * xdev
The device we are displaying to.
Definition: comp_compositor.h:84
A swapchain that is almost a one to one mapping to a OpenXR swapchain.
Definition: comp_compositor.h:56
VkImage image
Vulkan image to create view from.
Definition: comp_compositor.h:39
A single HMD or input device.
Definition: xrt_device.h:203
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code...
Definition: vk_helpers.h:34
xrt_swapchain_create_flags
Special flags for creating swapchain images.
Definition: xrt_compositor.h:38