The open source OpenXR runtime
at mr/scanout-values 200 lines 4.7 kB view raw
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 14#include "render/render_interface.h" 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 */ 28struct chl_scratch 29{ 30 //! Shared render passed for the views. 31 struct render_gfx_render_pass render_pass; 32 33 struct 34 { 35 //! Per-view scratch images. 36 struct comp_scratch_single_images cssi; 37 38 //! Targets for rendering to the scratch buffer. 39 struct render_gfx_target_resources targets[COMP_SCRATCH_NUM_IMAGES]; 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. 55 bool render_pass_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 94chl_scratch_free_resources(struct chl_scratch *scratch, struct render_resources *rr); 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 */ 141struct chl_scratch_state_view 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 */ 154struct chl_scratch_state 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 183chl_scratch_state_discard_or_done(struct chl_scratch_state *scratch_state, struct chl_scratch *scratch) 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