Monado OpenXR Runtime
comp_high_level_scratch.h
Go to the documentation of this file.
1// Copyright 2019-2024, Collabora, Ltd.
2// Copyright 2024-2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Higher level interface for scratch images.
7 * @author Jakob Bornecrantz <tbornecrantz@nvidia.com>
8 * @ingroup comp_util
9 */
10
11#pragma once
12
13
15
16#include "comp_scratch.h"
17
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23/*!
24 * Scratch images that can be used for staging buffers.
25 *
26 * @ingroup comp_util
27 */
29{
30 //! Shared render passed for the views.
32
33 struct
34 {
35 //! Per-view scratch images.
37
38 //! Targets for rendering to the scratch buffer.
40 } views[XRT_MAX_VIEWS];
41
42 /*!
43 * Number of views that has been ensured and have Vulkan resources,
44 * all comp_scratch_single_images are always inited.
45 */
46 uint32_t view_count;
47
48 //! The extent used to create the images.
49 VkExtent2D extent;
50
51 //! Format requested.
52 VkFormat format;
53
54 //! Has the render pass been initialized.
56};
57
58/*!
59 * Must becalled before used and before the scratch images are registered with
60 * the u_var system.
61 *
62 * @memberof chl_scratch
63 */
64void
65chl_scratch_init(struct chl_scratch *scratch);
66
67/*!
68 * Resources must be manually called before calling this functions, and the
69 * scratch images unregistered from the u_var system.
70 *
71 * @memberof chl_scratch
72 */
73void
74chl_scratch_fini(struct chl_scratch *scratch);
75
76/*!
77 * Ensure the scratch images and the render target resources are created.
78 *
79 * @memberof chl_scratch
80 */
81bool
82chl_scratch_ensure(struct chl_scratch *scratch,
83 struct render_resources *rr,
84 uint32_t view_count,
85 VkExtent2D extent,
86 const VkFormat format);
87
88/*!
89 * Free all Vulkan resources that this scratch has created.
90 *
91 * @memberof chl_scratch
92 */
93void
95
96/*!
97 * Get the image, see @ref comp_scratch_single_images_get_image.
98 *
99 * @memberof chl_scratch
100 */
101static inline VkImage
102chl_scratch_get_image(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
103{
104 return comp_scratch_single_images_get_image(&scratch->views[view_index].cssi, image_index);
105}
106
107/*!
108 * Get the sample view, see @ref comp_scratch_single_images_get_sample_view.
109 *
110 * @memberof chl_scratch
111 */
112static inline VkImageView
113chl_scratch_get_sample_view(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
114{
115 return comp_scratch_single_images_get_sample_view(&scratch->views[view_index].cssi, image_index);
116}
117
118/*!
119 * Get the storage view, see @ref comp_scratch_single_images_get_storage_view.
120 *
121 * @memberof chl_scratch
122 */
123static inline VkImageView
124chl_scratch_get_storage_view(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
125{
126 return comp_scratch_single_images_get_storage_view(&scratch->views[view_index].cssi, image_index);
127}
128
129
130/*
131 *
132 * State
133 *
134 */
135
136/*!
137 * Per view frame state tracking which index was gotten and if it was used.
138 *
139 * @ingroup comp_util
140 */
142{
143 uint32_t index;
144
145 bool used;
146};
147
148/*!
149 * Used to track the index of images gotten for the images, and if it has been
150 * used. The user will need to mark images as used.
151 *
152 * @ingroup comp_util
153 */
155{
156 struct chl_scratch_state_view views[XRT_MAX_VIEWS];
157};
158
159/*!
160 * Zeros out the struct and calls get on all the images, setting the @p index
161 * field on the state for each view.
162 *
163 * @memberof chl_scratch_state
164 */
165static inline void
166chl_scratch_state_init_and_get(struct chl_scratch_state *scratch_state, struct chl_scratch *scratch)
167{
168 U_ZERO(scratch_state);
169
170 // Loop over all the of the images in the scratch view.
171 for (uint32_t i = 0; i < scratch->view_count; i++) {
172 comp_scratch_single_images_get(&scratch->views[i].cssi, &scratch_state->views[i].index);
173 }
174}
175
176/*!
177 * Calls discard or done on all the scratch images, it calls done if the @p used
178 * field is set to true.
179 *
180 * @memberof chl_scratch_state
181 */
182static inline void
184{
185 // Loop over all the of the images in the scratch view.
186 for (uint32_t i = 0; i < scratch->view_count; i++) {
187 if (scratch_state->views[i].used) {
188 comp_scratch_single_images_done(&scratch->views[i].cssi);
189 } else {
190 comp_scratch_single_images_discard(&scratch->views[i].cssi);
191 }
192 }
193
194 U_ZERO(scratch_state);
195}
196
197
198#ifdef __cplusplus
199}
200#endif
Helper implementation for native compositors.
#define COMP_SCRATCH_NUM_IMAGES
The number of images for each view, works like a swapchain.
Definition: comp_scratch.h:33
#define U_ZERO(PTR)
Zeroes the correct amount of memory based on the type pointed-to by the argument.
Definition: u_misc.h:68
static VkImageView comp_scratch_single_images_get_sample_view(struct comp_scratch_single_images *cssi, uint32_t index)
Get the image view for sampling, it will apply any automatic linearization, aka sRGB gamma curve corr...
Definition: comp_scratch.h:173
void comp_scratch_single_images_done(struct comp_scratch_single_images *cssi)
After calling get and rendering to the image you call this function to signal that you are done with ...
Definition: comp_scratch.c:390
void comp_scratch_single_images_get(struct comp_scratch_single_images *cssi, uint32_t *out_index)
Get the next free image, after this function has been called you must call either done or discard bef...
Definition: comp_scratch.c:384
static VkImageView comp_scratch_single_images_get_storage_view(struct comp_scratch_single_images *cssi, uint32_t index)
Get the image view for storage or direct value, no linearization will be done.
Definition: comp_scratch.h:195
static VkImage comp_scratch_single_images_get_image(struct comp_scratch_single_images *cssi, uint32_t index)
Get the image for the given index.
Definition: comp_scratch.h:159
void comp_scratch_single_images_discard(struct comp_scratch_single_images *cssi)
Discard a get call, this clears the image debug part causing no image to be shown in the debug UI.
Definition: comp_scratch.c:408
The NEW compositor rendering code header.
Per view frame state tracking which index was gotten and if it was used.
Definition: comp_high_level_scratch.h:142
Used to track the index of images gotten for the images, and if it has been used.
Definition: comp_high_level_scratch.h:155
static void chl_scratch_state_init_and_get(struct chl_scratch_state *scratch_state, struct chl_scratch *scratch)
Zeros out the struct and calls get on all the images, setting the index field on the state for each v...
Definition: comp_high_level_scratch.h:166
static void chl_scratch_state_discard_or_done(struct chl_scratch_state *scratch_state, struct chl_scratch *scratch)
Calls discard or done on all the scratch images, it calls done if the used field is set to true.
Definition: comp_high_level_scratch.h:183
Scratch images that can be used for staging buffers.
Definition: comp_high_level_scratch.h:29
VkFormat format
Format requested.
Definition: comp_high_level_scratch.h:52
struct render_gfx_render_pass render_pass
Shared render passed for the views.
Definition: comp_high_level_scratch.h:31
struct render_gfx_target_resources targets[(4)]
Targets for rendering to the scratch buffer.
Definition: comp_high_level_scratch.h:39
void chl_scratch_init(struct chl_scratch *scratch)
Must becalled before used and before the scratch images are registered with the u_var system.
Definition: comp_high_level_scratch.c:19
static VkImage chl_scratch_get_image(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
Get the image, see comp_scratch_single_images_get_image.
Definition: comp_high_level_scratch.h:102
static VkImageView chl_scratch_get_storage_view(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
Get the storage view, see comp_scratch_single_images_get_storage_view.
Definition: comp_high_level_scratch.h:124
void chl_scratch_fini(struct chl_scratch *scratch)
Resources must be manually called before calling this functions, and the scratch images unregistered ...
Definition: comp_high_level_scratch.c:27
void chl_scratch_free_resources(struct chl_scratch *scratch, struct render_resources *rr)
Free all Vulkan resources that this scratch has created.
Definition: comp_high_level_scratch.c:121
uint32_t view_count
Number of views that has been ensured and have Vulkan resources, all comp_scratch_single_images are a...
Definition: comp_high_level_scratch.h:46
struct comp_scratch_single_images cssi
Per-view scratch images.
Definition: comp_high_level_scratch.h:36
bool render_pass_initialized
Has the render pass been initialized.
Definition: comp_high_level_scratch.h:55
static VkImageView chl_scratch_get_sample_view(struct chl_scratch *scratch, uint32_t view_index, uint32_t image_index)
Get the sample view, see comp_scratch_single_images_get_sample_view.
Definition: comp_high_level_scratch.h:113
VkExtent2D extent
The extent used to create the images.
Definition: comp_high_level_scratch.h:49
bool chl_scratch_ensure(struct chl_scratch *scratch, struct render_resources *rr, uint32_t view_count, VkExtent2D extent, const VkFormat format)
Ensure the scratch images and the render target resources are created.
Definition: comp_high_level_scratch.c:35
Holds scratch images for a single view, designed to work with render code.
Definition: comp_scratch.h:69
A render pass, while not depending on a VkFramebuffer, does depend on the format of the target image(...
Definition: render_interface.h:706
Each rendering (render_gfx) render to one or more targets (render_gfx_target_resources),...
Definition: render_interface.h:785
Holds all pools and static resources for rendering.
Definition: render_interface.h:352