blob: b5f5e7d7aab68a3547c6d90b00723433a512c30b [file] [log] [blame]
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +02001/*
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 Pitoiset87fde602024-04-05 16:28:39 +02008 * SPDX-License-Identifier: MIT
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +02009 */
10
11#ifndef RADV_IMAGE_H
12#define RADV_IMAGE_H
13
14#include "ac_surface.h"
15
Samuel Pitoiset150ce132024-04-01 19:28:41 +020016#include "radv_device.h"
17#include "radv_physical_device.h"
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +020018#include "radv_radeon_winsys.h"
19
20#include "vk_format.h"
21#include "vk_image.h"
22
23static 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
27struct radv_image_plane {
28 VkFormat format;
29 struct radeon_surf surface;
Samuel Pitoiset7a8b7252024-10-31 09:09:13 +010030 uint32_t first_mip_pipe_misaligned; /* GFX10-GFX11.5 */
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +020031};
32
33struct radv_image_binding {
34 /* Set when bound */
35 struct radeon_winsys_bo *bo;
36 VkDeviceSize offset;
Samuel Pitoiset50060072024-04-10 08:39:08 +020037 uint64_t bo_va;
Hans-Kristian Arntzen71fdc672024-04-12 12:58:17 +020038 uint64_t range;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +020039};
40
41struct 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 Pitoisetd7141bb2024-04-01 16:37:09 +020050 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
76VK_DEFINE_NONDISP_HANDLE_CASTS(radv_image, vk.base, VkImage, VK_OBJECT_TYPE_IMAGE)
77
Samuel Pitoiset15fe7332024-05-28 11:16:04 +020078static inline uint64_t
79radv_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 Pitoisetd7141bb2024-04-01 16:37:09 +020084static inline bool
85radv_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 */
96static inline bool
97radv_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 */
105static inline bool
106radv_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 */
114static inline bool
115radv_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 */
123static inline bool
124radv_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 */
132static inline bool
133radv_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 */
141static inline bool
142radv_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 */
150static inline bool
151radv_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 */
159static inline bool
160radv_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 */
173static inline bool
174radv_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 */
182static inline bool
183radv_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 */
192static inline bool
193radv_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
207static inline bool
208radv_image_has_clear_value(const struct radv_image *image)
209{
210 return image->clear_value_offset != 0;
211}
212
213static inline uint64_t
214radv_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 Pitoiset15fe7332024-05-28 11:16:04 +0200218 uint64_t va = radv_image_get_va(image, 0);
219 va += image->clear_value_offset + base_level * 8;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200220 return va;
221}
222
223static inline uint64_t
224radv_image_get_fce_pred_va(const struct radv_image *image, uint32_t base_level)
225{
226 assert(image->fce_pred_offset != 0);
227
Samuel Pitoiset15fe7332024-05-28 11:16:04 +0200228 uint64_t va = radv_image_get_va(image, 0);
229 va += image->fce_pred_offset + base_level * 8;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200230 return va;
231}
232
233static inline uint64_t
234radv_image_get_dcc_pred_va(const struct radv_image *image, uint32_t base_level)
235{
236 assert(image->dcc_pred_offset != 0);
237
Samuel Pitoiset15fe7332024-05-28 11:16:04 +0200238 uint64_t va = radv_image_get_va(image, 0);
239 va += image->dcc_pred_offset + base_level * 8;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200240 return va;
241}
242
243static inline uint64_t
244radv_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 Pitoiset15fe7332024-05-28 11:16:04 +0200248 uint64_t va = radv_image_get_va(image, 0);
249 va += image->tc_compat_zrange_offset + base_level * 4;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200250 return va;
251}
252
253static inline uint64_t
254radv_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 Pitoiset15fe7332024-05-28 11:16:04 +0200258 uint64_t va = radv_image_get_va(image, 0);
259 va += image->clear_value_offset + base_level * 8;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200260 return va;
261}
262
263static inline uint32_t
264radv_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
302static inline bool
303radv_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 Pitoiset709452b2024-05-23 14:22:00 +0200308 return pdev->info.gfx_level >= GFX10 && radv_image_is_tc_compat_htile(image) && image->vk.samples > 1;
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200309}
310
311bool radv_are_formats_dcc_compatible(const struct radv_physical_device *pdev, const void *pNext, VkFormat format,
312 VkImageCreateFlags flags, bool *sign_reinterpret);
313
314bool radv_image_use_dcc_image_stores(const struct radv_device *device, const struct radv_image *image);
315
316bool radv_image_use_dcc_predication(const struct radv_device *device, const struct radv_image *image);
317
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200318void radv_compose_swizzle(const struct util_format_description *desc, const VkComponentMapping *mapping,
319 enum pipe_swizzle swizzle[4]);
320
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200321void radv_init_metadata(struct radv_device *device, struct radv_image *image, struct radeon_bo_metadata *metadata);
322
323void radv_image_override_offset_stride(struct radv_device *device, struct radv_image *image, uint64_t offset,
324 uint32_t stride);
325
326bool radv_image_can_fast_clear(const struct radv_device *device, const struct radv_image *image);
327
328struct ac_surf_info radv_get_ac_surf_info(struct radv_device *device, const struct radv_image *image);
329
330struct 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
338VkResult 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
342VkResult radv_image_create(VkDevice _device, const struct radv_image_create_info *info,
343 const VkAllocationCallbacks *alloc, VkImage *pImage, bool is_internal);
344
345unsigned radv_plane_from_aspect(VkImageAspectFlags mask);
346
347VkFormat 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 */
355bool radv_layout_is_htile_compressed(const struct radv_device *device, const struct radv_image *image,
356 VkImageLayout layout, unsigned queue_mask);
357
358bool radv_layout_can_fast_clear(const struct radv_device *device, const struct radv_image *image, unsigned level,
359 VkImageLayout layout, unsigned queue_mask);
360
361bool radv_layout_dcc_compressed(const struct radv_device *device, const struct radv_image *image, unsigned level,
362 VkImageLayout layout, unsigned queue_mask);
363
364enum radv_fmask_compression {
365 RADV_FMASK_COMPRESSION_NONE,
366 RADV_FMASK_COMPRESSION_PARTIAL,
367 RADV_FMASK_COMPRESSION_FULL,
368};
369
370enum 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
374unsigned radv_image_queue_family_mask(const struct radv_image *image, enum radv_queue_family family,
375 enum radv_queue_family queue_family);
376
377bool radv_image_is_renderable(const struct radv_device *device, const struct radv_image *image);
378
Samuel Pitoiset44fa2452024-10-31 10:13:54 +0100379bool radv_image_is_l2_coherent(const struct radv_device *device, const struct radv_image *image,
380 const VkImageSubresourceRange *range);
381
Samuel Pitoisetd7141bb2024-04-01 16:37:09 +0200382#endif /* RADV_IMAGE_H */