Monado OpenXR Runtime
Loading...
Searching...
No Matches
comp_render_helpers.h
Go to the documentation of this file.
1// Copyright 2023-2024, Collabora, Ltd.
2// Copyright 2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Compositor rendering code helpers.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup comp_util
9 */
10
11#pragma once
12
13#include "xrt/xrt_compositor.h"
14
16
17#include "util/comp_base.h"
18#include "util/comp_render.h"
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24
25/*
26 *
27 * Swapchain helpers.
28 *
29 */
30
31static inline VkImageView
32get_image_view(const struct comp_swapchain_image *image, enum xrt_layer_composition_flags flags, uint32_t array_index)
33{
34 const uint32_t compare_flags =
35 XRT_LAYER_COMPOSITION_BLEND_TEXTURE_SOURCE_ALPHA_BIT | XRT_LAYER_COMPOSITION_ADVANCED_BLENDING_BIT;
36 if (flags & compare_flags) {
37 return image->views.alpha[array_index];
38 }
39
40 return image->views.no_alpha[array_index];
41}
42
43
44/*
45 *
46 * View index helpers.
47 *
48 */
49
50static inline bool
51is_view_index_right(uint32_t view_index)
52{
53 return view_index % 2 == 1;
54}
55
56static inline void
57view_index_to_projection_data(uint32_t view_index,
58 const struct xrt_layer_data *data,
59 const struct xrt_layer_projection_view_data **out_vd)
60{
61 const struct xrt_layer_projection_data *proj = &data->proj;
62 *out_vd = &proj->v[view_index];
63}
64
65static inline void
66view_index_to_depth_data(uint32_t view_index,
67 const struct xrt_layer_data *data,
68 const struct xrt_layer_projection_view_data **out_vd,
69 const struct xrt_layer_depth_data **out_dvd)
70{
71 const struct xrt_layer_projection_depth_data *depth = &data->depth;
72 *out_vd = &depth->v[view_index];
73 *out_dvd = &depth->d[view_index];
74}
75
76
77/*
78 *
79 * Layer data helpers.
80 *
81 */
82
83static inline bool
84is_layer_view_visible(const struct xrt_layer_data *data, uint32_t view_index)
85{
86 enum xrt_layer_eye_visibility visibility;
87 switch (data->type) {
88 case XRT_LAYER_CUBE: visibility = data->cube.visibility; break;
89 case XRT_LAYER_CYLINDER: visibility = data->cylinder.visibility; break;
90 case XRT_LAYER_EQUIRECT1: visibility = data->equirect1.visibility; break;
91 case XRT_LAYER_EQUIRECT2: visibility = data->equirect2.visibility; break;
92 case XRT_LAYER_QUAD: visibility = data->quad.visibility; break;
93 case XRT_LAYER_PROJECTION:
94 case XRT_LAYER_PROJECTION_DEPTH: return true;
95 default: return false;
96 };
97
98 switch (visibility) {
99 case XRT_LAYER_EYE_VISIBILITY_LEFT_BIT: return !is_view_index_right(view_index);
100 case XRT_LAYER_EYE_VISIBILITY_RIGHT_BIT: return is_view_index_right(view_index);
101 case XRT_LAYER_EYE_VISIBILITY_BOTH: return true;
102 case XRT_LAYER_EYE_VISIBILITY_NONE:
103 default: return false;
104 }
105}
106
107static inline bool
108is_layer_view_space(const struct xrt_layer_data *data)
109{
110 return (data->flags & XRT_LAYER_COMPOSITION_VIEW_SPACE_BIT) != 0;
111}
112
113static inline bool
114is_layer_unpremultiplied(const struct xrt_layer_data *data)
115{
116 return (data->flags & XRT_LAYER_COMPOSITION_UNPREMULTIPLIED_ALPHA_BIT) != 0;
117}
118
119static inline bool
120is_layer_alpha_inverted(const struct xrt_layer_data *data)
121{
122#ifndef XRT_FEATURE_OPENXR_INVERTED_ALPHA
123 return false;
124#else
125 // The layers without source alpha bit flag are sent to comp with alpha 1.0, so they don't need to be inverted.
126 return (data->flags & XRT_LAYER_COMPOSITION_BLEND_TEXTURE_SOURCE_ALPHA_BIT) != 0 &&
127 (data->flags & XRT_LAYER_COMPOSITION_INVERTED_ALPHA_BIT) != 0;
128#endif
129}
130
131static inline void
132set_post_transform_rect(const struct xrt_layer_data *data,
133 const struct xrt_normalized_rect *src_norm_rect,
134 bool invert_flip,
135 struct xrt_normalized_rect *out_norm_rect)
136{
137 struct xrt_normalized_rect rect = *src_norm_rect;
138
139 if (data->flip_y ^ invert_flip) {
140 float h = rect.h;
141
142 rect.h = -h;
143 rect.y = rect.y + h;
144 }
145
146 *out_norm_rect = rect;
147}
148
149static inline void
150apply_bias_and_scale_from_layer(const struct xrt_layer_data *data,
151 struct xrt_colour_rgba_f32 *out_scale,
152 struct xrt_colour_rgba_f32 *out_bias)
153{
154 if (data->flags & XRT_LAYER_COMPOSITION_COLOR_BIAS_SCALE) {
155 *out_scale = data->color_scale;
156 *out_bias = data->color_bias;
157 } else {
158 // Use identity scale and zero bias when flag is not set
159 *out_scale = XRT_C11_COMPOUND(struct xrt_colour_rgba_f32){1.0f, 1.0f, 1.0f, 1.0f};
160 *out_bias = XRT_C11_COMPOUND(struct xrt_colour_rgba_f32){0.0f, 0.0f, 0.0f, 0.0f};
161 }
162}
163
164static inline void
165apply_advanced_blend_from_layer(const struct xrt_layer_data *data,
166 uint32_t *out_advanced_blending,
167 struct render_advanced_blend_info *out_advanced_blend)
168{
169 *out_advanced_blending = (data->flags & XRT_LAYER_COMPOSITION_ADVANCED_BLENDING_BIT) != 0;
170 if (*out_advanced_blending == 0) {
171 return;
172 }
173
174 out_advanced_blend->src_factor_color = data->advanced_blend.src_factor_color;
175 out_advanced_blend->dst_factor_color = data->advanced_blend.dst_factor_color;
176 out_advanced_blend->src_factor_alpha = data->advanced_blend.src_factor_alpha;
177 out_advanced_blend->dst_factor_alpha = data->advanced_blend.dst_factor_alpha;
178}
179
180
181/*
182 *
183 * Command helpers.
184 *
185 */
186
187/*!
188 * This inserts a barrier operation that effects all views[X].squash.image
189 * fields (which are VkImages).
190 */
191static inline void
193 const struct comp_render_dispatch_data *d,
194 VkCommandBuffer cmd,
195 VkAccessFlags src_access_mask,
196 VkAccessFlags dst_access_mask,
197 VkImageLayout transition_from,
198 VkImageLayout transition_to,
199 VkPipelineStageFlags src_stage_mask,
200 VkPipelineStageFlags dst_stage_mask)
201{
202 VkImageSubresourceRange first_color_level_subresource_range = {
203 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
204 .baseMipLevel = 0,
205 .levelCount = 1,
206 .baseArrayLayer = 0,
207 .layerCount = 1,
208 };
209
210 for (uint32_t i = 0; i < d->squash_view_count; i++) {
211 bool already_barriered = false;
212
213 VkImage image = d->views[i].squash.image;
214
215 uint32_t k = i;
216 while (k > 0) {
217 k--; // k is always greater then zero.
218
219 if (d->views[k].squash.image == image) {
220 already_barriered = true;
221 break;
222 }
223 }
224
225 if (already_barriered) {
226 continue;
227 }
228
230 vk, // vk_bundle
231 cmd, // cmd_buffer
232 image, // image
233 src_access_mask, // src_access_mask
234 dst_access_mask, // dst_access_mask
235 transition_from, // old_image_layout
236 transition_to, // new_image_layout
237 src_stage_mask, // src_stage_mask
238 dst_stage_mask, // dst_stage_mask
239 first_color_level_subresource_range); // subresource_range
240 }
241}
242
243
244#ifdef __cplusplus
245}
246#endif
Helper implementation for native compositors.
Compositor render implementation.
static void cmd_barrier_view_squash_images(struct vk_bundle *vk, const struct comp_render_dispatch_data *d, VkCommandBuffer cmd, VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout transition_from, VkImageLayout transition_to, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask)
This inserts a barrier operation that effects all views[X].squash.image fields (which are VkImages).
Definition comp_render_helpers.h:192
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:1043
xrt_layer_composition_flags
Bit field for holding information about how a layer should be composited.
Definition xrt_compositor.h:93
xrt_layer_eye_visibility
Which view is the layer visible to?
Definition xrt_compositor.h:182
@ XRT_LAYER_COMPOSITION_ADVANCED_BLENDING_BIT
This layer has advanced blending information, this bit supersedes the behavior of XRT_LAYER_COMPOSITI...
Definition xrt_compositor.h:127
@ 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:101
@ XRT_LAYER_COMPOSITION_INVERTED_ALPHA_BIT
This layer has inverted alpha, see XR_COMPOSITION_LAYER_INVERTED_ALPHA_BIT_EXT.
Definition xrt_compositor.h:139
@ XRT_LAYER_COMPOSITION_COLOR_BIAS_SCALE
If this flag is set the compositor should use the scale and bias from the xrt_layer_data struct.
Definition xrt_compositor.h:107
The NEW compositor rendering code header.
The input data needed for a complete layer squashing distortion rendering to a target.
Definition comp_render.h:174
uint32_t squash_view_count
The number of squash views currently in this dispatch data.
Definition comp_render.h:180
VkImage image
The layer image for this view (aka scratch image), used for barrier operations.
Definition comp_render.h:101
A single swapchain image, holds the needed state for tracking image usage.
Definition comp_swapchain.h:58
struct comp_swapchain_image::@115 views
Views used by the renderer and distortion code, for each array layer.
Definition m_space.cpp:87
Advanced blend factors sent to the compute layer shader.
Definition render_interface.h:1250
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition vk_helpers.h:82
A 4 element colour with floating point channels.
Definition xrt_defines.h:453
All the pure data values associated with a composition layer.
Definition xrt_compositor.h:441
All the pure data values associated with a depth information attached to a layer.
Definition xrt_compositor.h:289
All the pure data values associated with a projection layer.
Definition xrt_compositor.h:275
All the pure data values associated with a projection layer with depth swapchain attached.
Definition xrt_compositor.h:312
All of the pure data values associated with a single view in a projection layer.
Definition xrt_compositor.h:261
Normalized image rectangle, coordinates and size in 0 .
Definition xrt_defines.h:510
Header declaring XRT graphics interfaces.