Monado OpenXR Runtime
comp_render_helpers.h
Go to the documentation of this file.
1// Copyright 2023-2024, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Compositor rendering code helpers.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @ingroup comp_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_compositor.h"
13
15
16#include "util/comp_base.h"
17#include "util/comp_render.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23
24/*
25 *
26 * Swapchain helpers.
27 *
28 */
29
30static inline VkImageView
31get_image_view(const struct comp_swapchain_image *image, enum xrt_layer_composition_flags flags, uint32_t array_index)
32{
33 if (flags & XRT_LAYER_COMPOSITION_BLEND_TEXTURE_SOURCE_ALPHA_BIT) {
34 return image->views.alpha[array_index];
35 }
36
37 return image->views.no_alpha[array_index];
38}
39
40
41/*
42 *
43 * View index helpers.
44 *
45 */
46
47static inline bool
48is_view_index_right(uint32_t view_index)
49{
50 return view_index % 2 == 1;
51}
52
53static inline void
54view_index_to_projection_data(uint32_t view_index,
55 const struct xrt_layer_data *data,
56 const struct xrt_layer_projection_view_data **out_vd)
57{
58 const struct xrt_layer_projection_data *proj = &data->proj;
59 *out_vd = &proj->v[view_index];
60}
61
62static inline void
63view_index_to_depth_data(uint32_t view_index,
64 const struct xrt_layer_data *data,
65 const struct xrt_layer_projection_view_data **out_vd,
66 const struct xrt_layer_depth_data **out_dvd)
67{
68 const struct xrt_layer_projection_depth_data *depth = &data->depth;
69 *out_vd = &depth->v[view_index];
70 *out_dvd = &depth->d[view_index];
71}
72
73
74/*
75 *
76 * Layer data helpers.
77 *
78 */
79
80static inline bool
81is_layer_view_visible(const struct xrt_layer_data *data, uint32_t view_index)
82{
83 enum xrt_layer_eye_visibility visibility;
84 switch (data->type) {
85 case XRT_LAYER_CUBE: visibility = data->cube.visibility; break;
86 case XRT_LAYER_CYLINDER: visibility = data->cylinder.visibility; break;
87 case XRT_LAYER_EQUIRECT1: visibility = data->equirect1.visibility; break;
88 case XRT_LAYER_EQUIRECT2: visibility = data->equirect2.visibility; break;
89 case XRT_LAYER_QUAD: visibility = data->quad.visibility; break;
90 case XRT_LAYER_PROJECTION:
91 case XRT_LAYER_PROJECTION_DEPTH: return true;
92 default: return false;
93 };
94
95 switch (visibility) {
96 case XRT_LAYER_EYE_VISIBILITY_LEFT_BIT: return !is_view_index_right(view_index);
97 case XRT_LAYER_EYE_VISIBILITY_RIGHT_BIT: return is_view_index_right(view_index);
98 case XRT_LAYER_EYE_VISIBILITY_BOTH: return true;
99 case XRT_LAYER_EYE_VISIBILITY_NONE:
100 default: return false;
101 }
102}
103
104static inline bool
105is_layer_view_space(const struct xrt_layer_data *data)
106{
107 return (data->flags & XRT_LAYER_COMPOSITION_VIEW_SPACE_BIT) != 0;
108}
109
110static inline bool
111is_layer_unpremultiplied(const struct xrt_layer_data *data)
112{
113 return (data->flags & XRT_LAYER_COMPOSITION_UNPREMULTIPLIED_ALPHA_BIT) != 0;
114}
115
116static inline void
117set_post_transform_rect(const struct xrt_layer_data *data,
118 const struct xrt_normalized_rect *src_norm_rect,
119 bool invert_flip,
120 struct xrt_normalized_rect *out_norm_rect)
121{
122 struct xrt_normalized_rect rect = *src_norm_rect;
123
124 if (data->flip_y ^ invert_flip) {
125 float h = rect.h;
126
127 rect.h = -h;
128 rect.y = rect.y + h;
129 }
130
131 *out_norm_rect = rect;
132}
133
134
135/*
136 *
137 * Command helpers.
138 *
139 */
140
141static inline void
142cmd_barrier_view_images(struct vk_bundle *vk,
143 const struct comp_render_dispatch_data *d,
144 VkCommandBuffer cmd,
145 VkAccessFlags src_access_mask,
146 VkAccessFlags dst_access_mask,
147 VkImageLayout transition_from,
148 VkImageLayout transition_to,
149 VkPipelineStageFlags src_stage_mask,
150 VkPipelineStageFlags dst_stage_mask)
151{
152 VkImageSubresourceRange first_color_level_subresource_range = {
153 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
154 .baseMipLevel = 0,
155 .levelCount = 1,
156 .baseArrayLayer = 0,
157 .layerCount = 1,
158 };
159
160 for (uint32_t i = 0; i < d->view_count; i++) {
161 bool already_barriered = false;
162
163 VkImage image = d->views[i].image;
164
165 uint32_t k = i;
166 while (k > 0) {
167 k--; // k is always greater then zero.
168
169 if (d->views[k].image == image) {
170 already_barriered = true;
171 break;
172 }
173 }
174
175 if (already_barriered) {
176 continue;
177 }
178
180 vk, // vk_bundle
181 cmd, // cmd_buffer
182 image, // image
183 src_access_mask, // src_access_mask
184 dst_access_mask, // dst_access_mask
185 transition_from, // old_image_layout
186 transition_to, // new_image_layout
187 src_stage_mask, // src_stage_mask
188 dst_stage_mask, // dst_stage_mask
189 first_color_level_subresource_range); // subresource_range
190 }
191}
192
193
194#ifdef __cplusplus
195}
196#endif
Helper implementation for native compositors.
Compositor render implementation.
void vk_cmd_image_barrier_locked(struct vk_bundle *vk, VkCommandBuffer cmd_buffer, VkImage image, VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_image_layout, VkImageLayout new_image_layout, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask, VkImageSubresourceRange subresource_range)
Inserts a image barrier command, doesn't take any locks, the calling code will need hold the lock for...
Definition: vk_helpers.c:1683
xrt_layer_composition_flags
Bit field for holding information about how a layer should be composited.
Definition: xrt_compositor.h:94
xrt_layer_eye_visibility
Which view is the layer visible to?
Definition: xrt_compositor.h:161
@ XRT_LAYER_COMPOSITION_VIEW_SPACE_BIT
The layer is locked to the device and the pose should only be adjusted for the IPD.
Definition: xrt_compositor.h:102
The NEW compositor rendering code header.
The input data needed for a complete layer squashing distortion rendering to a target.
Definition: comp_render.h:138
uint32_t view_count
The number of views currently in this dispatch data.
Definition: comp_render.h:142
VkImage image
The layer image for this view (aka scratch image), used for barrier operations.
Definition: comp_render.h:89
A single swapchain image, holds the needed state for tracking image usage.
Definition: comp_swapchain.h:58
struct comp_swapchain_image::@113 views
Views used by the renderer and distortion code, for each array layer.
Definition: m_space.cpp:87
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition: vk_helpers.h:50
All the pure data values associated with a composition layer.
Definition: xrt_compositor.h:396
All the pure data values associated with a depth information attached to a layer.
Definition: xrt_compositor.h:246
All the pure data values associated with a projection layer.
Definition: xrt_compositor.h:234
All the pure data values associated with a projection layer with depth swapchain attached.
Definition: xrt_compositor.h:269
All of the pure data values associated with a single view in a projection layer.
Definition: xrt_compositor.h:220
Normalized image rectangle, coordinates and size in 0 .
Definition: xrt_defines.h:455
Header declaring XRT graphics interfaces.