Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Red Hat. |
| 3 | * Copyright © 2016 Bas Nieuwenhuizen |
| 4 | * |
| 5 | * based in part on anv driver which is: |
| 6 | * Copyright © 2015 Intel Corporation |
| 7 | * |
Samuel Pitoiset | 87fde60 | 2024-04-05 16:28:39 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: MIT |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef RADV_IMAGE_H |
| 12 | #define RADV_IMAGE_H |
| 13 | |
| 14 | #include "ac_surface.h" |
| 15 | |
Samuel Pitoiset | 150ce13 | 2024-04-01 19:28:41 +0200 | [diff] [blame] | 16 | #include "radv_device.h" |
| 17 | #include "radv_physical_device.h" |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 18 | #include "radv_radeon_winsys.h" |
| 19 | |
| 20 | #include "vk_format.h" |
| 21 | #include "vk_image.h" |
| 22 | |
| 23 | static const VkImageUsageFlags RADV_IMAGE_USAGE_WRITE_BITS = |
| 24 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 25 | VK_IMAGE_USAGE_STORAGE_BIT; |
| 26 | |
| 27 | struct radv_image_plane { |
| 28 | VkFormat format; |
| 29 | struct radeon_surf surface; |
Samuel Pitoiset | 7a8b725 | 2024-10-31 09:09:13 +0100 | [diff] [blame] | 30 | uint32_t first_mip_pipe_misaligned; /* GFX10-GFX11.5 */ |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct radv_image_binding { |
| 34 | /* Set when bound */ |
| 35 | struct radeon_winsys_bo *bo; |
| 36 | VkDeviceSize offset; |
Samuel Pitoiset | 5006007 | 2024-04-10 08:39:08 +0200 | [diff] [blame] | 37 | uint64_t bo_va; |
Hans-Kristian Arntzen | 71fdc67 | 2024-04-12 12:58:17 +0200 | [diff] [blame] | 38 | uint64_t range; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct radv_image { |
| 42 | struct vk_image vk; |
| 43 | |
| 44 | VkDeviceSize size; |
| 45 | uint32_t alignment; |
| 46 | |
| 47 | unsigned queue_family_mask; |
| 48 | bool exclusive; |
| 49 | bool shareable; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 50 | bool dcc_sign_reinterpret; |
| 51 | bool support_comp_to_single; |
| 52 | |
| 53 | struct radv_image_binding bindings[3]; |
| 54 | bool tc_compatible_cmask; |
| 55 | |
| 56 | uint64_t clear_value_offset; |
| 57 | uint64_t fce_pred_offset; |
| 58 | uint64_t dcc_pred_offset; |
| 59 | |
| 60 | /* |
| 61 | * Metadata for the TC-compat zrange workaround. If the 32-bit value |
| 62 | * stored at this offset is UINT_MAX, the driver will emit |
| 63 | * DB_Z_INFO.ZRANGE_PRECISION=0, otherwise it will skip the |
| 64 | * SET_CONTEXT_REG packet. |
| 65 | */ |
| 66 | uint64_t tc_compat_zrange_offset; |
| 67 | |
| 68 | /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */ |
| 69 | VkDeviceMemory owned_memory; |
| 70 | |
| 71 | unsigned plane_count; |
| 72 | bool disjoint; |
| 73 | struct radv_image_plane planes[0]; |
| 74 | }; |
| 75 | |
| 76 | VK_DEFINE_NONDISP_HANDLE_CASTS(radv_image, vk.base, VkImage, VK_OBJECT_TYPE_IMAGE) |
| 77 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 78 | static inline uint64_t |
| 79 | radv_image_get_va(const struct radv_image *image, uint32_t bind_idx) |
| 80 | { |
| 81 | return radv_buffer_get_va(image->bindings[bind_idx].bo) + image->bindings[bind_idx].offset; |
| 82 | } |
| 83 | |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 84 | static inline bool |
| 85 | radv_image_extent_compare(const struct radv_image *image, const VkExtent3D *extent) |
| 86 | { |
| 87 | if (extent->width != image->vk.extent.width || extent->height != image->vk.extent.height || |
| 88 | extent->depth != image->vk.extent.depth) |
| 89 | return false; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return whether the image has CMASK metadata for color surfaces. |
| 95 | */ |
| 96 | static inline bool |
| 97 | radv_image_has_cmask(const struct radv_image *image) |
| 98 | { |
| 99 | return image->planes[0].surface.cmask_offset; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Return whether the image has FMASK metadata for color surfaces. |
| 104 | */ |
| 105 | static inline bool |
| 106 | radv_image_has_fmask(const struct radv_image *image) |
| 107 | { |
| 108 | return image->planes[0].surface.fmask_offset; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Return whether the image has DCC metadata for color surfaces. |
| 113 | */ |
| 114 | static inline bool |
| 115 | radv_image_has_dcc(const struct radv_image *image) |
| 116 | { |
| 117 | return !(image->planes[0].surface.flags & RADEON_SURF_Z_OR_SBUFFER) && image->planes[0].surface.meta_offset; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Return whether the image is TC-compatible CMASK. |
| 122 | */ |
| 123 | static inline bool |
| 124 | radv_image_is_tc_compat_cmask(const struct radv_image *image) |
| 125 | { |
| 126 | return radv_image_has_fmask(image) && image->tc_compatible_cmask; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return whether DCC metadata is enabled for a level. |
| 131 | */ |
| 132 | static inline bool |
| 133 | radv_dcc_enabled(const struct radv_image *image, unsigned level) |
| 134 | { |
| 135 | return radv_image_has_dcc(image) && level < image->planes[0].surface.num_meta_levels; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Return whether the image has CB metadata. |
| 140 | */ |
| 141 | static inline bool |
| 142 | radv_image_has_CB_metadata(const struct radv_image *image) |
| 143 | { |
| 144 | return radv_image_has_cmask(image) || radv_image_has_fmask(image) || radv_image_has_dcc(image); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return whether the image has HTILE metadata for depth surfaces. |
| 149 | */ |
| 150 | static inline bool |
| 151 | radv_image_has_htile(const struct radv_image *image) |
| 152 | { |
| 153 | return image->planes[0].surface.flags & RADEON_SURF_Z_OR_SBUFFER && image->planes[0].surface.meta_size; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return whether the image has VRS HTILE metadata for depth surfaces |
| 158 | */ |
| 159 | static inline bool |
| 160 | radv_image_has_vrs_htile(const struct radv_device *device, const struct radv_image *image) |
| 161 | { |
| 162 | const struct radv_physical_device *pdev = radv_device_physical(device); |
| 163 | const enum amd_gfx_level gfx_level = pdev->info.gfx_level; |
| 164 | |
| 165 | /* Any depth buffer can potentially use VRS on GFX10.3. */ |
| 166 | return gfx_level == GFX10_3 && device->vk.enabled_features.attachmentFragmentShadingRate && |
| 167 | radv_image_has_htile(image) && (image->vk.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return whether HTILE metadata is enabled for a level. |
| 172 | */ |
| 173 | static inline bool |
| 174 | radv_htile_enabled(const struct radv_image *image, unsigned level) |
| 175 | { |
| 176 | return radv_image_has_htile(image) && level < image->planes[0].surface.num_meta_levels; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Return whether the image is TC-compatible HTILE. |
| 181 | */ |
| 182 | static inline bool |
| 183 | radv_image_is_tc_compat_htile(const struct radv_image *image) |
| 184 | { |
| 185 | return radv_image_has_htile(image) && (image->planes[0].surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Return whether the entire HTILE buffer can be used for depth in order to |
| 190 | * improve HiZ Z-Range precision. |
| 191 | */ |
| 192 | static inline bool |
| 193 | radv_image_tile_stencil_disabled(const struct radv_device *device, const struct radv_image *image) |
| 194 | { |
| 195 | const struct radv_physical_device *pdev = radv_device_physical(device); |
| 196 | |
| 197 | if (pdev->info.gfx_level >= GFX9) { |
| 198 | return !vk_format_has_stencil(image->vk.format) && !radv_image_has_vrs_htile(device, image); |
| 199 | } else { |
| 200 | /* Due to a hw bug, TILE_STENCIL_DISABLE must be set to 0 for |
| 201 | * the TC-compat ZRANGE issue even if no stencil is used. |
| 202 | */ |
| 203 | return !vk_format_has_stencil(image->vk.format) && !radv_image_is_tc_compat_htile(image); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static inline bool |
| 208 | radv_image_has_clear_value(const struct radv_image *image) |
| 209 | { |
| 210 | return image->clear_value_offset != 0; |
| 211 | } |
| 212 | |
| 213 | static inline uint64_t |
| 214 | radv_image_get_fast_clear_va(const struct radv_image *image, uint32_t base_level) |
| 215 | { |
| 216 | assert(radv_image_has_clear_value(image)); |
| 217 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 218 | uint64_t va = radv_image_get_va(image, 0); |
| 219 | va += image->clear_value_offset + base_level * 8; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 220 | return va; |
| 221 | } |
| 222 | |
| 223 | static inline uint64_t |
| 224 | radv_image_get_fce_pred_va(const struct radv_image *image, uint32_t base_level) |
| 225 | { |
| 226 | assert(image->fce_pred_offset != 0); |
| 227 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 228 | uint64_t va = radv_image_get_va(image, 0); |
| 229 | va += image->fce_pred_offset + base_level * 8; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 230 | return va; |
| 231 | } |
| 232 | |
| 233 | static inline uint64_t |
| 234 | radv_image_get_dcc_pred_va(const struct radv_image *image, uint32_t base_level) |
| 235 | { |
| 236 | assert(image->dcc_pred_offset != 0); |
| 237 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 238 | uint64_t va = radv_image_get_va(image, 0); |
| 239 | va += image->dcc_pred_offset + base_level * 8; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 240 | return va; |
| 241 | } |
| 242 | |
| 243 | static inline uint64_t |
| 244 | radv_get_tc_compat_zrange_va(const struct radv_image *image, uint32_t base_level) |
| 245 | { |
| 246 | assert(image->tc_compat_zrange_offset != 0); |
| 247 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 248 | uint64_t va = radv_image_get_va(image, 0); |
| 249 | va += image->tc_compat_zrange_offset + base_level * 4; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 250 | return va; |
| 251 | } |
| 252 | |
| 253 | static inline uint64_t |
| 254 | radv_get_ds_clear_value_va(const struct radv_image *image, uint32_t base_level) |
| 255 | { |
| 256 | assert(radv_image_has_clear_value(image)); |
| 257 | |
Samuel Pitoiset | 15fe733 | 2024-05-28 11:16:04 +0200 | [diff] [blame] | 258 | uint64_t va = radv_image_get_va(image, 0); |
| 259 | va += image->clear_value_offset + base_level * 8; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 260 | return va; |
| 261 | } |
| 262 | |
| 263 | static inline uint32_t |
| 264 | radv_get_htile_initial_value(const struct radv_device *device, const struct radv_image *image) |
| 265 | { |
| 266 | uint32_t initial_value; |
| 267 | |
| 268 | if (radv_image_tile_stencil_disabled(device, image)) { |
| 269 | /* Z only (no stencil): |
| 270 | * |
| 271 | * |31 18|17 4|3 0| |
| 272 | * +---------+---------+-------+ |
| 273 | * | Max Z | Min Z | ZMask | |
| 274 | */ |
| 275 | initial_value = 0xfffc000f; |
| 276 | } else { |
| 277 | /* Z and stencil: |
| 278 | * |
| 279 | * |31 12|11 10|9 8|7 6|5 4|3 0| |
| 280 | * +-----------+-----+------+-----+-----+-------+ |
| 281 | * | Z Range | | SMem | SR1 | SR0 | ZMask | |
| 282 | * |
| 283 | * SR0/SR1 contains the stencil test results. Initializing |
| 284 | * SR0/SR1 to 0x3 means the stencil test result is unknown. |
| 285 | * |
| 286 | * Z, stencil and 4 bit VRS encoding: |
| 287 | * |31 12|11 10|9 8|7 6|5 4|3 0| |
| 288 | * +-----------+------------+------+------------+-----+-------+ |
| 289 | * | Z Range | VRS y-rate | SMem | VRS x-rate | SR0 | ZMask | |
| 290 | */ |
| 291 | if (radv_image_has_vrs_htile(device, image)) { |
| 292 | /* Initialize the VRS x-rate value at 0, so the hw interprets it as 1 sample. */ |
| 293 | initial_value = 0xfffff33f; |
| 294 | } else { |
| 295 | initial_value = 0xfffff3ff; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return initial_value; |
| 300 | } |
| 301 | |
| 302 | static inline bool |
| 303 | radv_image_get_iterate256(const struct radv_device *device, struct radv_image *image) |
| 304 | { |
| 305 | const struct radv_physical_device *pdev = radv_device_physical(device); |
| 306 | |
| 307 | /* ITERATE_256 is required for depth or stencil MSAA images that are TC-compatible HTILE. */ |
Samuel Pitoiset | 709452b | 2024-05-23 14:22:00 +0200 | [diff] [blame] | 308 | return pdev->info.gfx_level >= GFX10 && radv_image_is_tc_compat_htile(image) && image->vk.samples > 1; |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | bool radv_are_formats_dcc_compatible(const struct radv_physical_device *pdev, const void *pNext, VkFormat format, |
| 312 | VkImageCreateFlags flags, bool *sign_reinterpret); |
| 313 | |
| 314 | bool radv_image_use_dcc_image_stores(const struct radv_device *device, const struct radv_image *image); |
| 315 | |
| 316 | bool radv_image_use_dcc_predication(const struct radv_device *device, const struct radv_image *image); |
| 317 | |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 318 | void radv_compose_swizzle(const struct util_format_description *desc, const VkComponentMapping *mapping, |
| 319 | enum pipe_swizzle swizzle[4]); |
| 320 | |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 321 | void radv_init_metadata(struct radv_device *device, struct radv_image *image, struct radeon_bo_metadata *metadata); |
| 322 | |
| 323 | void radv_image_override_offset_stride(struct radv_device *device, struct radv_image *image, uint64_t offset, |
| 324 | uint32_t stride); |
| 325 | |
| 326 | bool radv_image_can_fast_clear(const struct radv_device *device, const struct radv_image *image); |
| 327 | |
| 328 | struct ac_surf_info radv_get_ac_surf_info(struct radv_device *device, const struct radv_image *image); |
| 329 | |
| 330 | struct radv_image_create_info { |
| 331 | const VkImageCreateInfo *vk_info; |
| 332 | bool scanout; |
| 333 | bool no_metadata_planes; |
| 334 | bool prime_blit_src; |
| 335 | const struct radeon_bo_metadata *bo_metadata; |
| 336 | }; |
| 337 | |
| 338 | VkResult radv_image_create_layout(struct radv_device *device, struct radv_image_create_info create_info, |
| 339 | const struct VkImageDrmFormatModifierExplicitCreateInfoEXT *mod_info, |
| 340 | const struct VkVideoProfileListInfoKHR *profile_list, struct radv_image *image); |
| 341 | |
| 342 | VkResult radv_image_create(VkDevice _device, const struct radv_image_create_info *info, |
| 343 | const VkAllocationCallbacks *alloc, VkImage *pImage, bool is_internal); |
| 344 | |
| 345 | unsigned radv_plane_from_aspect(VkImageAspectFlags mask); |
| 346 | |
| 347 | VkFormat radv_get_aspect_format(struct radv_image *image, VkImageAspectFlags mask); |
| 348 | |
| 349 | /* Whether the image has a htile that is known consistent with the contents of |
| 350 | * the image and is allowed to be in compressed form. |
| 351 | * |
| 352 | * If this is false reads that don't use the htile should be able to return |
| 353 | * correct results. |
| 354 | */ |
| 355 | bool radv_layout_is_htile_compressed(const struct radv_device *device, const struct radv_image *image, |
| 356 | VkImageLayout layout, unsigned queue_mask); |
| 357 | |
| 358 | bool radv_layout_can_fast_clear(const struct radv_device *device, const struct radv_image *image, unsigned level, |
| 359 | VkImageLayout layout, unsigned queue_mask); |
| 360 | |
| 361 | bool radv_layout_dcc_compressed(const struct radv_device *device, const struct radv_image *image, unsigned level, |
| 362 | VkImageLayout layout, unsigned queue_mask); |
| 363 | |
| 364 | enum radv_fmask_compression { |
| 365 | RADV_FMASK_COMPRESSION_NONE, |
| 366 | RADV_FMASK_COMPRESSION_PARTIAL, |
| 367 | RADV_FMASK_COMPRESSION_FULL, |
| 368 | }; |
| 369 | |
| 370 | enum radv_fmask_compression radv_layout_fmask_compression(const struct radv_device *device, |
| 371 | const struct radv_image *image, VkImageLayout layout, |
| 372 | unsigned queue_mask); |
| 373 | |
| 374 | unsigned radv_image_queue_family_mask(const struct radv_image *image, enum radv_queue_family family, |
| 375 | enum radv_queue_family queue_family); |
| 376 | |
| 377 | bool radv_image_is_renderable(const struct radv_device *device, const struct radv_image *image); |
| 378 | |
Samuel Pitoiset | 44fa245 | 2024-10-31 10:13:54 +0100 | [diff] [blame] | 379 | bool radv_image_is_l2_coherent(const struct radv_device *device, const struct radv_image *image, |
| 380 | const VkImageSubresourceRange *range); |
| 381 | |
Samuel Pitoiset | d7141bb | 2024-04-01 16:37:09 +0200 | [diff] [blame] | 382 | #endif /* RADV_IMAGE_H */ |