radv: Add a list of performance counters.
[platform/upstream/mesa.git] / src / amd / vulkan / radv_private.h
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  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25  * IN THE SOFTWARE.
26  */
27
28 #ifndef RADV_PRIVATE_H
29 #define RADV_PRIVATE_H
30
31 #include <assert.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef HAVE_VALGRIND
38 #include <memcheck.h>
39 #include <valgrind.h>
40 #define VG(x) x
41 #else
42 #define VG(x) ((void)0)
43 #endif
44
45 #include "c11/threads.h"
46 #ifndef _WIN32
47 #include <amdgpu.h>
48 #include <xf86drm.h>
49 #endif
50 #include "compiler/shader_enums.h"
51 #include "util/bitscan.h"
52 #include "util/list.h"
53 #include "util/macros.h"
54 #include "util/rwlock.h"
55 #include "util/xmlconfig.h"
56 #include "vk_alloc.h"
57 #include "vk_buffer.h"
58 #include "vk_command_buffer.h"
59 #include "vk_command_pool.h"
60 #include "vk_debug_report.h"
61 #include "vk_device.h"
62 #include "vk_format.h"
63 #include "vk_instance.h"
64 #include "vk_log.h"
65 #include "vk_physical_device.h"
66 #include "vk_shader_module.h"
67 #include "vk_queue.h"
68 #include "vk_util.h"
69 #include "vk_image.h"
70 #include "vk_framebuffer.h"
71
72 #include "ac_binary.h"
73 #include "ac_gpu_info.h"
74 #include "ac_shader_util.h"
75 #include "ac_spm.h"
76 #include "ac_sqtt.h"
77 #include "ac_surface.h"
78 #include "radv_constants.h"
79 #include "radv_descriptor_set.h"
80 #include "radv_radeon_winsys.h"
81 #include "radv_shader.h"
82 #include "radv_shader_args.h"
83 #include "sid.h"
84
85 #include "radix_sort/radix_sort_vk_devaddr.h"
86
87 /* Pre-declarations needed for WSI entrypoints */
88 struct wl_surface;
89 struct wl_display;
90 typedef struct xcb_connection_t xcb_connection_t;
91 typedef uint32_t xcb_visualid_t;
92 typedef uint32_t xcb_window_t;
93
94 #include <vulkan/vk_android_native_buffer.h>
95 #include <vulkan/vk_icd.h>
96 #include <vulkan/vulkan.h>
97 #include <vulkan/vulkan_android.h>
98
99 #include "radv_entrypoints.h"
100
101 #include "wsi_common.h"
102
103 #ifdef __cplusplus
104 extern "C"
105 {
106 #endif
107
108 /* Helper to determine if we should compile
109  * any of the Android AHB support.
110  *
111  * To actually enable the ext we also need
112  * the necessary kernel support.
113  */
114 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
115 #define RADV_SUPPORT_ANDROID_HARDWARE_BUFFER 1
116 #include <vndk/hardware_buffer.h>
117 #else
118 #define RADV_SUPPORT_ANDROID_HARDWARE_BUFFER 0
119 #endif
120
121 #ifdef _WIN32
122 #define RADV_SUPPORT_CALIBRATED_TIMESTAMPS 0
123 #else
124 #define RADV_SUPPORT_CALIBRATED_TIMESTAMPS 1
125 #endif
126
127 #ifdef _WIN32
128 #define radv_printflike(a, b)
129 #else
130 #define radv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
131 #endif
132
133 static inline uint32_t
134 align_u32(uint32_t v, uint32_t a)
135 {
136    assert(a != 0 && a == (a & -a));
137    return (v + a - 1) & ~(a - 1);
138 }
139
140 static inline uint32_t
141 align_u32_npot(uint32_t v, uint32_t a)
142 {
143    return (v + a - 1) / a * a;
144 }
145
146 static inline uint64_t
147 align_u64(uint64_t v, uint64_t a)
148 {
149    assert(a != 0 && a == (a & -a));
150    return (v + a - 1) & ~(a - 1);
151 }
152
153 static inline int32_t
154 align_i32(int32_t v, int32_t a)
155 {
156    assert(a != 0 && a == (a & -a));
157    return (v + a - 1) & ~(a - 1);
158 }
159
160 /** Alignment must be a power of 2. */
161 static inline bool
162 radv_is_aligned(uintmax_t n, uintmax_t a)
163 {
164    assert(a == (a & -a));
165    return (n & (a - 1)) == 0;
166 }
167
168 static inline uint32_t
169 round_up_u32(uint32_t v, uint32_t a)
170 {
171    return (v + a - 1) / a;
172 }
173
174 static inline uint64_t
175 round_up_u64(uint64_t v, uint64_t a)
176 {
177    return (v + a - 1) / a;
178 }
179
180 static inline uint32_t
181 radv_minify(uint32_t n, uint32_t levels)
182 {
183    if (unlikely(n == 0))
184       return 0;
185    else
186       return MAX2(n >> levels, 1);
187 }
188 static inline float
189 radv_clamp_f(float f, float min, float max)
190 {
191    assert(min < max);
192
193    if (f > max)
194       return max;
195    else if (f < min)
196       return min;
197    else
198       return f;
199 }
200
201 static inline bool
202 radv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
203 {
204    if (*inout_mask & clear_mask) {
205       *inout_mask &= ~clear_mask;
206       return true;
207    } else {
208       return false;
209    }
210 }
211
212 static inline int
213 radv_float_to_sfixed(float value, unsigned frac_bits)
214 {
215    return value * (1 << frac_bits);
216 }
217
218 static inline unsigned int
219 radv_float_to_ufixed(float value, unsigned frac_bits)
220 {
221    return value * (1 << frac_bits);
222 }
223
224 /* Whenever we generate an error, pass it through this function. Useful for
225  * debugging, where we can break on it. Only call at error site, not when
226  * propagating errors. Might be useful to plug in a stack trace here.
227  */
228
229 struct radv_image_view;
230 struct radv_instance;
231
232 /* A non-fatal assert.  Useful for debugging. */
233 #ifdef NDEBUG
234 #define radv_assert(x)                                                                             \
235    do {                                                                                            \
236    } while (0)
237 #else
238 #define radv_assert(x)                                                                             \
239    do {                                                                                            \
240       if (unlikely(!(x)))                                                                          \
241          fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x);                            \
242    } while (0)
243 #endif
244
245 int radv_get_instance_entrypoint_index(const char *name);
246 int radv_get_device_entrypoint_index(const char *name);
247 int radv_get_physical_device_entrypoint_index(const char *name);
248
249 const char *radv_get_instance_entry_name(int index);
250 const char *radv_get_physical_device_entry_name(int index);
251 const char *radv_get_device_entry_name(int index);
252
253 /* queue types */
254 enum radv_queue_family {
255    RADV_QUEUE_GENERAL,
256    RADV_QUEUE_COMPUTE,
257    RADV_QUEUE_TRANSFER,
258    RADV_MAX_QUEUE_FAMILIES,
259    RADV_QUEUE_FOREIGN = RADV_MAX_QUEUE_FAMILIES,
260    RADV_QUEUE_IGNORED,
261 };
262
263 struct radv_perfcounter_desc;
264
265 struct radv_physical_device {
266    struct vk_physical_device vk;
267
268    /* Link in radv_instance::physical_devices */
269    struct list_head link;
270
271    struct radv_instance *instance;
272
273    struct radeon_winsys *ws;
274    struct radeon_info rad_info;
275    char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
276    char marketing_name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
277    uint8_t driver_uuid[VK_UUID_SIZE];
278    uint8_t device_uuid[VK_UUID_SIZE];
279    uint8_t cache_uuid[VK_UUID_SIZE];
280
281    int local_fd;
282    int master_fd;
283    struct wsi_device wsi_device;
284
285    bool out_of_order_rast_allowed;
286
287    /* Whether DCC should be enabled for MSAA textures. */
288    bool dcc_msaa_allowed;
289
290    /* Whether to enable NGG. */
291    bool use_ngg;
292
293    /* Whether to enable NGG culling. */
294    bool use_ngg_culling;
295
296    /* Whether to enable NGG streamout. */
297    bool use_ngg_streamout;
298
299    /* Number of threads per wave. */
300    uint8_t ps_wave_size;
301    uint8_t cs_wave_size;
302    uint8_t ge_wave_size;
303    uint8_t rt_wave_size;
304
305    /* Whether to use the LLVM compiler backend */
306    bool use_llvm;
307
308    /* Whether to emulate ETC2 image support on HW without support. */
309    bool emulate_etc2;
310
311    /* This is the drivers on-disk cache used as a fallback as opposed to
312     * the pipeline cache defined by apps.
313     */
314    struct disk_cache *disk_cache;
315
316    VkPhysicalDeviceMemoryProperties memory_properties;
317    enum radeon_bo_domain memory_domains[VK_MAX_MEMORY_TYPES];
318    enum radeon_bo_flag memory_flags[VK_MAX_MEMORY_TYPES];
319    unsigned heaps;
320
321 #ifndef _WIN32
322    int available_nodes;
323    drmPciBusInfo bus_info;
324
325    dev_t primary_devid;
326    dev_t render_devid;
327 #endif
328
329    nir_shader_compiler_options nir_options[MESA_VULKAN_SHADER_STAGES];
330
331    enum radv_queue_family vk_queue_to_radv[RADV_MAX_QUEUE_FAMILIES];
332    uint32_t num_queues;
333
334    uint32_t gs_table_depth;
335
336    struct ac_hs_info hs;
337    struct ac_task_info task_info;
338
339    /* Performance counters. */
340    struct ac_perfcounters ac_perfcounters;
341
342    uint32_t num_perfcounters;
343    struct radv_perfcounter_desc *perfcounters;
344 };
345
346 struct radv_instance {
347    struct vk_instance vk;
348
349    VkAllocationCallbacks alloc;
350
351    uint64_t debug_flags;
352    uint64_t perftest_flags;
353
354    bool physical_devices_enumerated;
355    struct list_head physical_devices;
356
357    struct driOptionCache dri_options;
358    struct driOptionCache available_dri_options;
359
360    /**
361     * Workarounds for game bugs.
362     */
363    bool enable_mrt_output_nan_fixup;
364    bool disable_tc_compat_htile_in_general;
365    bool disable_shrink_image_store;
366    bool absolute_depth_bias;
367    bool report_apu_as_dgpu;
368    bool disable_aniso_single_level;
369    bool zero_vram;
370    bool disable_sinking_load_input_fs;
371 };
372
373 VkResult radv_init_wsi(struct radv_physical_device *physical_device);
374 void radv_finish_wsi(struct radv_physical_device *physical_device);
375
376 struct cache_entry;
377
378 struct radv_pipeline_cache {
379    struct vk_object_base base;
380    struct radv_device *device;
381    mtx_t mutex;
382    VkPipelineCacheCreateFlags flags;
383
384    uint32_t total_size;
385    uint32_t table_size;
386    uint32_t kernel_count;
387    struct cache_entry **hash_table;
388    bool modified;
389
390    VkAllocationCallbacks alloc;
391 };
392
393 struct radv_shader_binary;
394 struct radv_shader;
395 struct radv_pipeline_shader_stack_size;
396
397 void radv_pipeline_cache_init(struct radv_pipeline_cache *cache, struct radv_device *device);
398 void radv_pipeline_cache_finish(struct radv_pipeline_cache *cache);
399 bool radv_pipeline_cache_load(struct radv_pipeline_cache *cache, const void *data, size_t size);
400
401 bool radv_create_shaders_from_pipeline_cache(
402    struct radv_device *device, struct radv_pipeline_cache *cache, const unsigned char *sha1,
403    struct radv_pipeline *pipeline, struct radv_pipeline_shader_stack_size **stack_sizes,
404    uint32_t *num_stack_sizes, bool *found_in_application_cache);
405
406 void radv_pipeline_cache_insert_shaders(
407    struct radv_device *device, struct radv_pipeline_cache *cache, const unsigned char *sha1,
408    struct radv_pipeline *pipeline, struct radv_shader_binary *const *binaries,
409    const struct radv_pipeline_shader_stack_size *stack_sizes, uint32_t num_stack_sizes);
410
411 VkResult radv_upload_shaders(struct radv_device *device, struct radv_pipeline *pipeline,
412                              struct radv_shader_binary **binaries,
413                              struct radv_shader_binary *gs_copy_binary);
414
415 enum radv_blit_ds_layout {
416    RADV_BLIT_DS_LAYOUT_TILE_ENABLE,
417    RADV_BLIT_DS_LAYOUT_TILE_DISABLE,
418    RADV_BLIT_DS_LAYOUT_COUNT,
419 };
420
421 static inline enum radv_blit_ds_layout
422 radv_meta_blit_ds_to_type(VkImageLayout layout)
423 {
424    return (layout == VK_IMAGE_LAYOUT_GENERAL) ? RADV_BLIT_DS_LAYOUT_TILE_DISABLE
425                                               : RADV_BLIT_DS_LAYOUT_TILE_ENABLE;
426 }
427
428 static inline VkImageLayout
429 radv_meta_blit_ds_to_layout(enum radv_blit_ds_layout ds_layout)
430 {
431    return ds_layout == RADV_BLIT_DS_LAYOUT_TILE_ENABLE ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
432                                                        : VK_IMAGE_LAYOUT_GENERAL;
433 }
434
435 enum radv_meta_dst_layout {
436    RADV_META_DST_LAYOUT_GENERAL,
437    RADV_META_DST_LAYOUT_OPTIMAL,
438    RADV_META_DST_LAYOUT_COUNT,
439 };
440
441 static inline enum radv_meta_dst_layout
442 radv_meta_dst_layout_from_layout(VkImageLayout layout)
443 {
444    return (layout == VK_IMAGE_LAYOUT_GENERAL) ? RADV_META_DST_LAYOUT_GENERAL
445                                               : RADV_META_DST_LAYOUT_OPTIMAL;
446 }
447
448 static inline VkImageLayout
449 radv_meta_dst_layout_to_layout(enum radv_meta_dst_layout layout)
450 {
451    return layout == RADV_META_DST_LAYOUT_OPTIMAL ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
452                                                  : VK_IMAGE_LAYOUT_GENERAL;
453 }
454
455 struct radv_meta_state {
456    VkAllocationCallbacks alloc;
457
458    struct radv_pipeline_cache cache;
459
460    /*
461     * For on-demand pipeline creation, makes sure that
462     * only one thread tries to build a pipeline at the same time.
463     */
464    mtx_t mtx;
465
466    /**
467     * Use array element `i` for images with `2^i` samples.
468     */
469    struct {
470       VkPipeline color_pipelines[NUM_META_FS_KEYS];
471    } color_clear[MAX_SAMPLES_LOG2][MAX_RTS];
472
473    struct {
474       VkPipeline depth_only_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
475       VkPipeline stencil_only_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
476       VkPipeline depthstencil_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
477
478       VkPipeline depth_only_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
479       VkPipeline stencil_only_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
480       VkPipeline depthstencil_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
481    } ds_clear[MAX_SAMPLES_LOG2];
482
483    VkPipelineLayout clear_color_p_layout;
484    VkPipelineLayout clear_depth_p_layout;
485    VkPipelineLayout clear_depth_unrestricted_p_layout;
486
487    /* Optimized compute fast HTILE clear for stencil or depth only. */
488    VkPipeline clear_htile_mask_pipeline;
489    VkPipelineLayout clear_htile_mask_p_layout;
490    VkDescriptorSetLayout clear_htile_mask_ds_layout;
491
492    /* Copy VRS into HTILE. */
493    VkPipeline copy_vrs_htile_pipeline;
494    VkPipelineLayout copy_vrs_htile_p_layout;
495    VkDescriptorSetLayout copy_vrs_htile_ds_layout;
496
497    /* Clear DCC with comp-to-single. */
498    VkPipeline clear_dcc_comp_to_single_pipeline[2]; /* 0: 1x, 1: 2x/4x/8x */
499    VkPipelineLayout clear_dcc_comp_to_single_p_layout;
500    VkDescriptorSetLayout clear_dcc_comp_to_single_ds_layout;
501
502    struct {
503       /** Pipeline that blits from a 1D image. */
504       VkPipeline pipeline_1d_src[NUM_META_FS_KEYS];
505
506       /** Pipeline that blits from a 2D image. */
507       VkPipeline pipeline_2d_src[NUM_META_FS_KEYS];
508
509       /** Pipeline that blits from a 3D image. */
510       VkPipeline pipeline_3d_src[NUM_META_FS_KEYS];
511
512       VkPipeline depth_only_1d_pipeline;
513       VkPipeline depth_only_2d_pipeline;
514       VkPipeline depth_only_3d_pipeline;
515
516       VkPipeline stencil_only_1d_pipeline;
517       VkPipeline stencil_only_2d_pipeline;
518       VkPipeline stencil_only_3d_pipeline;
519       VkPipelineLayout pipeline_layout;
520       VkDescriptorSetLayout ds_layout;
521    } blit;
522
523    struct {
524       VkPipelineLayout p_layouts[5];
525       VkDescriptorSetLayout ds_layouts[5];
526       VkPipeline pipelines[5][NUM_META_FS_KEYS];
527
528       VkPipeline depth_only_pipeline[5];
529
530       VkPipeline stencil_only_pipeline[5];
531    } blit2d[MAX_SAMPLES_LOG2];
532
533    struct {
534       VkPipelineLayout img_p_layout;
535       VkDescriptorSetLayout img_ds_layout;
536       VkPipeline pipeline;
537       VkPipeline pipeline_3d;
538    } itob;
539    struct {
540       VkPipelineLayout img_p_layout;
541       VkDescriptorSetLayout img_ds_layout;
542       VkPipeline pipeline;
543       VkPipeline pipeline_3d;
544    } btoi;
545    struct {
546       VkPipelineLayout img_p_layout;
547       VkDescriptorSetLayout img_ds_layout;
548       VkPipeline pipeline;
549    } btoi_r32g32b32;
550    struct {
551       VkPipelineLayout img_p_layout;
552       VkDescriptorSetLayout img_ds_layout;
553       VkPipeline pipeline[MAX_SAMPLES_LOG2];
554       VkPipeline pipeline_3d;
555    } itoi;
556    struct {
557       VkPipelineLayout img_p_layout;
558       VkDescriptorSetLayout img_ds_layout;
559       VkPipeline pipeline;
560    } itoi_r32g32b32;
561    struct {
562       VkPipelineLayout img_p_layout;
563       VkDescriptorSetLayout img_ds_layout;
564       VkPipeline pipeline[MAX_SAMPLES_LOG2];
565       VkPipeline pipeline_3d;
566    } cleari;
567    struct {
568       VkPipelineLayout img_p_layout;
569       VkDescriptorSetLayout img_ds_layout;
570       VkPipeline pipeline;
571    } cleari_r32g32b32;
572    struct {
573       VkPipelineLayout p_layout;
574       VkDescriptorSetLayout ds_layout;
575       VkPipeline pipeline[MAX_SAMPLES_LOG2];
576    } fmask_copy;
577
578    struct {
579       VkPipelineLayout p_layout;
580       VkPipeline pipeline[NUM_META_FS_KEYS];
581    } resolve;
582
583    struct {
584       VkDescriptorSetLayout ds_layout;
585       VkPipelineLayout p_layout;
586       struct {
587          VkPipeline pipeline;
588          VkPipeline i_pipeline;
589          VkPipeline srgb_pipeline;
590       } rc[MAX_SAMPLES_LOG2];
591
592       VkPipeline depth_zero_pipeline;
593       struct {
594          VkPipeline average_pipeline;
595          VkPipeline max_pipeline;
596          VkPipeline min_pipeline;
597       } depth[MAX_SAMPLES_LOG2];
598
599       VkPipeline stencil_zero_pipeline;
600       struct {
601          VkPipeline max_pipeline;
602          VkPipeline min_pipeline;
603       } stencil[MAX_SAMPLES_LOG2];
604    } resolve_compute;
605
606    struct {
607       VkDescriptorSetLayout ds_layout;
608       VkPipelineLayout p_layout;
609
610       struct {
611          VkPipeline pipeline[NUM_META_FS_KEYS];
612       } rc[MAX_SAMPLES_LOG2];
613
614       VkPipeline depth_zero_pipeline;
615       struct {
616          VkPipeline average_pipeline;
617          VkPipeline max_pipeline;
618          VkPipeline min_pipeline;
619       } depth[MAX_SAMPLES_LOG2];
620
621       VkPipeline stencil_zero_pipeline;
622       struct {
623          VkPipeline max_pipeline;
624          VkPipeline min_pipeline;
625       } stencil[MAX_SAMPLES_LOG2];
626    } resolve_fragment;
627
628    struct {
629       VkPipelineLayout p_layout;
630       VkPipeline decompress_pipeline;
631       VkPipeline resummarize_pipeline;
632    } depth_decomp[MAX_SAMPLES_LOG2];
633
634    VkDescriptorSetLayout expand_depth_stencil_compute_ds_layout;
635    VkPipelineLayout expand_depth_stencil_compute_p_layout;
636    VkPipeline expand_depth_stencil_compute_pipeline;
637
638    struct {
639       VkPipelineLayout p_layout;
640       VkPipeline cmask_eliminate_pipeline;
641       VkPipeline fmask_decompress_pipeline;
642       VkPipeline dcc_decompress_pipeline;
643
644       VkDescriptorSetLayout dcc_decompress_compute_ds_layout;
645       VkPipelineLayout dcc_decompress_compute_p_layout;
646       VkPipeline dcc_decompress_compute_pipeline;
647    } fast_clear_flush;
648
649    struct {
650       VkPipelineLayout fill_p_layout;
651       VkPipelineLayout copy_p_layout;
652       VkPipeline fill_pipeline;
653       VkPipeline copy_pipeline;
654    } buffer;
655
656    struct {
657       VkDescriptorSetLayout ds_layout;
658       VkPipelineLayout p_layout;
659       VkPipeline occlusion_query_pipeline;
660       VkPipeline pipeline_statistics_query_pipeline;
661       VkPipeline tfb_query_pipeline;
662       VkPipeline timestamp_query_pipeline;
663       VkPipeline pg_query_pipeline;
664    } query;
665
666    struct {
667       VkDescriptorSetLayout ds_layout;
668       VkPipelineLayout p_layout;
669       VkPipeline pipeline[MAX_SAMPLES_LOG2];
670    } fmask_expand;
671
672    struct {
673       VkDescriptorSetLayout ds_layout;
674       VkPipelineLayout p_layout;
675       VkPipeline pipeline[32];
676    } dcc_retile;
677
678    struct {
679       VkPipelineLayout leaf_p_layout;
680       VkPipeline leaf_pipeline;
681       VkPipelineLayout morton_p_layout;
682       VkPipeline morton_pipeline;
683       VkPipelineLayout internal_p_layout;
684       VkPipeline internal_pipeline;
685       VkPipelineLayout copy_p_layout;
686       VkPipeline copy_pipeline;
687
688       struct radix_sort_vk *radix_sort;
689       struct radix_sort_vk_sort_devaddr_info radix_sort_info;
690    } accel_struct_build;
691
692    struct {
693       VkDescriptorSetLayout ds_layout;
694       VkPipelineLayout p_layout;
695       VkPipeline pipeline;
696    } etc_decode;
697 };
698
699 #define RADV_NUM_HW_CTX (RADEON_CTX_PRIORITY_REALTIME + 1)
700
701 struct radv_deferred_queue_submission;
702
703 static inline enum radv_queue_family
704 vk_queue_to_radv(const struct radv_physical_device *phys_dev, int queue_family_index)
705 {
706    if (queue_family_index == VK_QUEUE_FAMILY_EXTERNAL ||
707        queue_family_index == VK_QUEUE_FAMILY_FOREIGN_EXT)
708       return RADV_QUEUE_FOREIGN;
709    if (queue_family_index == VK_QUEUE_FAMILY_IGNORED)
710       return RADV_QUEUE_IGNORED;
711
712    assert(queue_family_index < RADV_MAX_QUEUE_FAMILIES);
713    return phys_dev->vk_queue_to_radv[queue_family_index];
714 }
715
716 enum amd_ip_type radv_queue_family_to_ring(struct radv_physical_device *physical_device,
717                                          enum radv_queue_family f);
718
719 struct radv_queue_ring_info {
720    uint32_t scratch_size_per_wave;
721    uint32_t scratch_waves;
722    uint32_t compute_scratch_size_per_wave;
723    uint32_t compute_scratch_waves;
724    uint32_t esgs_ring_size;
725    uint32_t gsvs_ring_size;
726    bool tess_rings;
727    bool task_rings;
728    bool mesh_scratch_ring;
729    bool gds;
730    bool gds_oa;
731    bool sample_positions;
732 };
733
734 struct radv_queue_state {
735    enum radv_queue_family qf;
736    struct radv_queue_ring_info ring_info;
737
738    struct radeon_winsys_bo *scratch_bo;
739    struct radeon_winsys_bo *descriptor_bo;
740    struct radeon_winsys_bo *compute_scratch_bo;
741    struct radeon_winsys_bo *esgs_ring_bo;
742    struct radeon_winsys_bo *gsvs_ring_bo;
743    struct radeon_winsys_bo *tess_rings_bo;
744    struct radeon_winsys_bo *task_rings_bo;
745    struct radeon_winsys_bo *mesh_scratch_ring_bo;
746    struct radeon_winsys_bo *gds_bo;
747    struct radeon_winsys_bo *gds_oa_bo;
748
749    struct radeon_cmdbuf *initial_preamble_cs;
750    struct radeon_cmdbuf *initial_full_flush_preamble_cs;
751    struct radeon_cmdbuf *continue_preamble_cs;
752 };
753
754 struct radv_queue {
755    struct vk_queue vk;
756    struct radv_device *device;
757    struct radeon_winsys_ctx *hw_ctx;
758    enum radeon_ctx_priority priority;
759    struct radv_queue_state state;
760 };
761
762 #define RADV_BORDER_COLOR_COUNT       4096
763 #define RADV_BORDER_COLOR_BUFFER_SIZE (sizeof(VkClearColorValue) * RADV_BORDER_COLOR_COUNT)
764
765 struct radv_device_border_color_data {
766    bool used[RADV_BORDER_COLOR_COUNT];
767
768    struct radeon_winsys_bo *bo;
769    VkClearColorValue *colors_gpu_ptr;
770
771    /* Mutex is required to guarantee vkCreateSampler thread safety
772     * given that we are writing to a buffer and checking color occupation */
773    mtx_t mutex;
774 };
775
776 enum radv_force_vrs {
777    RADV_FORCE_VRS_1x1 = 0,
778    RADV_FORCE_VRS_2x2,
779    RADV_FORCE_VRS_2x1,
780    RADV_FORCE_VRS_1x2,
781 };
782
783 struct radv_notifier {
784    int fd;
785    int watch;
786    bool quit;
787    thrd_t thread;
788 };
789
790 struct radv_device {
791    struct vk_device vk;
792
793    struct radv_instance *instance;
794    struct radeon_winsys *ws;
795
796    struct radeon_winsys_ctx *hw_ctx[RADV_NUM_HW_CTX];
797    struct radv_meta_state meta_state;
798
799    struct radv_queue *queues[RADV_MAX_QUEUE_FAMILIES];
800    int queue_count[RADV_MAX_QUEUE_FAMILIES];
801
802    bool pbb_allowed;
803    uint32_t scratch_waves;
804    uint32_t dispatch_initiator;
805
806    /* MSAA sample locations.
807     * The first index is the sample index.
808     * The second index is the coordinate: X, Y. */
809    float sample_locations_1x[1][2];
810    float sample_locations_2x[2][2];
811    float sample_locations_4x[4][2];
812    float sample_locations_8x[8][2];
813
814    /* GFX7 and later */
815    uint32_t gfx_init_size_dw;
816    struct radeon_winsys_bo *gfx_init;
817
818    struct radeon_winsys_bo *trace_bo;
819    uint32_t *trace_id_ptr;
820
821    /* Whether to keep shader debug info, for debugging. */
822    bool keep_shader_info;
823
824    struct radv_physical_device *physical_device;
825
826    /* Backup in-memory cache to be used if the app doesn't provide one */
827    struct radv_pipeline_cache *mem_cache;
828
829    /*
830     * use different counters so MSAA MRTs get consecutive surface indices,
831     * even if MASK is allocated in between.
832     */
833    uint32_t image_mrt_offset_counter;
834    uint32_t fmask_mrt_offset_counter;
835
836    struct list_head shader_arenas;
837    unsigned shader_arena_shift;
838    uint8_t shader_free_list_mask;
839    struct list_head shader_free_lists[RADV_SHADER_ALLOC_NUM_FREE_LISTS];
840    struct list_head shader_block_obj_pool;
841    mtx_t shader_arena_mutex;
842
843    /* For detecting VM faults reported by dmesg. */
844    uint64_t dmesg_timestamp;
845
846    /* Whether the app has enabled the robustBufferAccess/robustBufferAccess2 features. */
847    bool robust_buffer_access;
848    bool robust_buffer_access2;
849
850    /* Whether to inline the compute dispatch size in user sgprs. */
851    bool load_grid_size_from_user_sgpr;
852
853    /* Whether the driver uses a global BO list. */
854    bool use_global_bo_list;
855
856    /* Whether attachment VRS is enabled. */
857    bool attachment_vrs_enabled;
858
859    /* Whether shader image 32-bit float atomics are enabled. */
860    bool image_float32_atomics;
861
862    /* Whether 2D views of 3D image is enabled. */
863    bool image_2d_view_of_3d;
864
865    /* Whether primitives generated query features are enabled. */
866    bool primitives_generated_query;
867
868    /* Whether anisotropy is forced with RADV_TEX_ANISO (-1 is disabled). */
869    int force_aniso;
870
871    struct radv_device_border_color_data border_color_data;
872
873    /* Thread trace. */
874    struct ac_thread_trace_data thread_trace;
875
876    /* SPM. */
877    struct ac_spm_trace_data spm_trace;
878
879    /* Trap handler. */
880    struct radv_trap_handler_shader *trap_handler_shader;
881    struct radeon_winsys_bo *tma_bo; /* Trap Memory Address */
882    uint32_t *tma_ptr;
883
884    /* Overallocation. */
885    bool overallocation_disallowed;
886    uint64_t allocated_memory_size[VK_MAX_MEMORY_HEAPS];
887    mtx_t overallocation_mutex;
888
889    /* RADV_FORCE_VRS. */
890    struct radv_notifier notifier;
891    enum radv_force_vrs force_vrs;
892
893    /* Depth image for VRS when not bound by the app. */
894    struct {
895       struct radv_image *image;
896       struct radv_buffer *buffer; /* HTILE */
897       struct radv_device_memory *mem;
898    } vrs;
899
900    struct u_rwlock vs_prologs_lock;
901    struct hash_table *vs_prologs;
902
903    /* Prime blit sdma queue */
904    struct radv_queue *private_sdma_queue;
905
906    struct radv_shader_part *simple_vs_prologs[MAX_VERTEX_ATTRIBS];
907    struct radv_shader_part *instance_rate_vs_prologs[816];
908
909    simple_mtx_t trace_mtx;
910
911    /* Whether per-vertex VRS is forced. */
912    bool force_vrs_enabled;
913
914    /* Whether shaders created through application entrypoints are considered internal. */
915    bool app_shaders_internal;
916
917    simple_mtx_t pstate_mtx;
918    unsigned pstate_cnt;
919
920    /* BO to contain some performance counter helpers:
921     * - A lock for profiling cmdbuffers.
922     * - a temporary fence for the end query synchronization.
923     * - the pass to use for profiling. (as an array of bools)
924     */
925    struct radeon_winsys_bo *perf_counter_bo;
926
927    /* Interleaved lock/unlock commandbuffers for perfcounter passes. */
928    struct radeon_cmdbuf **perf_counter_lock_cs;
929 };
930
931 bool radv_device_acquire_performance_counters(struct radv_device *device);
932 void radv_device_release_performance_counters(struct radv_device *device);
933
934 struct radv_device_memory {
935    struct vk_object_base base;
936    struct radeon_winsys_bo *bo;
937    /* for dedicated allocations */
938    struct radv_image *image;
939    struct radv_buffer *buffer;
940    uint32_t heap_index;
941    uint64_t alloc_size;
942    void *map;
943    void *user_ptr;
944
945 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
946    struct AHardwareBuffer *android_hardware_buffer;
947 #endif
948 };
949
950 void radv_device_memory_init(struct radv_device_memory *mem, struct radv_device *device,
951                              struct radeon_winsys_bo *bo);
952 void radv_device_memory_finish(struct radv_device_memory *mem);
953
954 struct radv_descriptor_range {
955    uint64_t va;
956    uint32_t size;
957 };
958
959 struct radv_descriptor_set_header {
960    struct vk_object_base base;
961    struct radv_descriptor_set_layout *layout;
962    uint32_t size;
963    uint32_t buffer_count;
964
965    struct radeon_winsys_bo *bo;
966    uint64_t va;
967    uint32_t *mapped_ptr;
968    struct radv_descriptor_range *dynamic_descriptors;
969 };
970
971 struct radv_descriptor_set {
972    struct radv_descriptor_set_header header;
973
974    struct radeon_winsys_bo *descriptors[];
975 };
976
977 struct radv_push_descriptor_set {
978    struct radv_descriptor_set_header set;
979    uint32_t capacity;
980 };
981
982 struct radv_descriptor_pool_entry {
983    uint32_t offset;
984    uint32_t size;
985    struct radv_descriptor_set *set;
986 };
987
988 struct radv_descriptor_pool {
989    struct vk_object_base base;
990    struct radeon_winsys_bo *bo;
991    uint8_t *host_bo;
992    uint8_t *mapped_ptr;
993    uint64_t current_offset;
994    uint64_t size;
995
996    uint8_t *host_memory_base;
997    uint8_t *host_memory_ptr;
998    uint8_t *host_memory_end;
999
1000    uint32_t entry_count;
1001    uint32_t max_entry_count;
1002    struct radv_descriptor_pool_entry entries[0];
1003 };
1004
1005 struct radv_descriptor_update_template_entry {
1006    VkDescriptorType descriptor_type;
1007
1008    /* The number of descriptors to update */
1009    uint32_t descriptor_count;
1010
1011    /* Into mapped_ptr or dynamic_descriptors, in units of the respective array */
1012    uint32_t dst_offset;
1013
1014    /* In dwords. Not valid/used for dynamic descriptors */
1015    uint32_t dst_stride;
1016
1017    uint32_t buffer_offset;
1018
1019    /* Only valid for combined image samplers and samplers */
1020    uint8_t has_sampler;
1021    uint8_t sampler_offset;
1022
1023    /* In bytes */
1024    size_t src_offset;
1025    size_t src_stride;
1026
1027    /* For push descriptors */
1028    const uint32_t *immutable_samplers;
1029 };
1030
1031 struct radv_descriptor_update_template {
1032    struct vk_object_base base;
1033    uint32_t entry_count;
1034    VkPipelineBindPoint bind_point;
1035    struct radv_descriptor_update_template_entry entry[0];
1036 };
1037
1038 void radv_descriptor_set_layout_destroy(struct radv_device *device,
1039                                         struct radv_descriptor_set_layout *set_layout);
1040
1041 static inline void
1042 radv_descriptor_set_layout_ref(struct radv_descriptor_set_layout *set_layout)
1043 {
1044    assert(set_layout && set_layout->ref_cnt >= 1);
1045    p_atomic_inc(&set_layout->ref_cnt);
1046 }
1047
1048 static inline void
1049 radv_descriptor_set_layout_unref(struct radv_device *device,
1050                                  struct radv_descriptor_set_layout *set_layout)
1051 {
1052    assert(set_layout && set_layout->ref_cnt >= 1);
1053    if (p_atomic_dec_zero(&set_layout->ref_cnt))
1054       radv_descriptor_set_layout_destroy(device, set_layout);
1055 }
1056
1057 struct radv_buffer {
1058    struct vk_buffer vk;
1059
1060    /* Set when bound */
1061    struct radeon_winsys_bo *bo;
1062    VkDeviceSize offset;
1063 };
1064
1065 void radv_buffer_init(struct radv_buffer *buffer, struct radv_device *device,
1066                       struct radeon_winsys_bo *bo, uint64_t size, uint64_t offset);
1067 void radv_buffer_finish(struct radv_buffer *buffer);
1068
1069 enum radv_dynamic_state_bits {
1070    RADV_DYNAMIC_VIEWPORT = 1ull << 0,
1071    RADV_DYNAMIC_SCISSOR = 1ull << 1,
1072    RADV_DYNAMIC_LINE_WIDTH = 1ull << 2,
1073    RADV_DYNAMIC_DEPTH_BIAS = 1ull << 3,
1074    RADV_DYNAMIC_BLEND_CONSTANTS = 1ull << 4,
1075    RADV_DYNAMIC_DEPTH_BOUNDS = 1ull << 5,
1076    RADV_DYNAMIC_STENCIL_COMPARE_MASK = 1ull << 6,
1077    RADV_DYNAMIC_STENCIL_WRITE_MASK = 1ull << 7,
1078    RADV_DYNAMIC_STENCIL_REFERENCE = 1ull << 8,
1079    RADV_DYNAMIC_DISCARD_RECTANGLE = 1ull << 9,
1080    RADV_DYNAMIC_SAMPLE_LOCATIONS = 1ull << 10,
1081    RADV_DYNAMIC_LINE_STIPPLE = 1ull << 11,
1082    RADV_DYNAMIC_CULL_MODE = 1ull << 12,
1083    RADV_DYNAMIC_FRONT_FACE = 1ull << 13,
1084    RADV_DYNAMIC_PRIMITIVE_TOPOLOGY = 1ull << 14,
1085    RADV_DYNAMIC_DEPTH_TEST_ENABLE = 1ull << 15,
1086    RADV_DYNAMIC_DEPTH_WRITE_ENABLE = 1ull << 16,
1087    RADV_DYNAMIC_DEPTH_COMPARE_OP = 1ull << 17,
1088    RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE = 1ull << 18,
1089    RADV_DYNAMIC_STENCIL_TEST_ENABLE = 1ull << 19,
1090    RADV_DYNAMIC_STENCIL_OP = 1ull << 20,
1091    RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE = 1ull << 21,
1092    RADV_DYNAMIC_FRAGMENT_SHADING_RATE = 1ull << 22,
1093    RADV_DYNAMIC_PATCH_CONTROL_POINTS = 1ull << 23,
1094    RADV_DYNAMIC_RASTERIZER_DISCARD_ENABLE = 1ull << 24,
1095    RADV_DYNAMIC_DEPTH_BIAS_ENABLE = 1ull << 25,
1096    RADV_DYNAMIC_LOGIC_OP = 1ull << 26,
1097    RADV_DYNAMIC_PRIMITIVE_RESTART_ENABLE = 1ull << 27,
1098    RADV_DYNAMIC_COLOR_WRITE_ENABLE = 1ull << 28,
1099    RADV_DYNAMIC_VERTEX_INPUT = 1ull << 29,
1100    RADV_DYNAMIC_ALL = (1ull << 30) - 1,
1101 };
1102
1103 enum radv_cmd_dirty_bits {
1104    /* Keep the dynamic state dirty bits in sync with
1105     * enum radv_dynamic_state_bits */
1106    RADV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1ull << 0,
1107    RADV_CMD_DIRTY_DYNAMIC_SCISSOR = 1ull << 1,
1108    RADV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1ull << 2,
1109    RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1ull << 3,
1110    RADV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1ull << 4,
1111    RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1ull << 5,
1112    RADV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1ull << 6,
1113    RADV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1ull << 7,
1114    RADV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1ull << 8,
1115    RADV_CMD_DIRTY_DYNAMIC_DISCARD_RECTANGLE = 1ull << 9,
1116    RADV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS = 1ull << 10,
1117    RADV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE = 1ull << 11,
1118    RADV_CMD_DIRTY_DYNAMIC_CULL_MODE = 1ull << 12,
1119    RADV_CMD_DIRTY_DYNAMIC_FRONT_FACE = 1ull << 13,
1120    RADV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY = 1ull << 14,
1121    RADV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE = 1ull << 15,
1122    RADV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE = 1ull << 16,
1123    RADV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP = 1ull << 17,
1124    RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE = 1ull << 18,
1125    RADV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE = 1ull << 19,
1126    RADV_CMD_DIRTY_DYNAMIC_STENCIL_OP = 1ull << 20,
1127    RADV_CMD_DIRTY_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE = 1ull << 21,
1128    RADV_CMD_DIRTY_DYNAMIC_FRAGMENT_SHADING_RATE = 1ull << 22,
1129    RADV_CMD_DIRTY_DYNAMIC_PATCH_CONTROL_POINTS = 1ull << 23,
1130    RADV_CMD_DIRTY_DYNAMIC_RASTERIZER_DISCARD_ENABLE = 1ull << 24,
1131    RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS_ENABLE = 1ull << 25,
1132    RADV_CMD_DIRTY_DYNAMIC_LOGIC_OP = 1ull << 26,
1133    RADV_CMD_DIRTY_DYNAMIC_PRIMITIVE_RESTART_ENABLE = 1ull << 27,
1134    RADV_CMD_DIRTY_DYNAMIC_COLOR_WRITE_ENABLE = 1ull << 28,
1135    RADV_CMD_DIRTY_DYNAMIC_VERTEX_INPUT = 1ull << 29,
1136    RADV_CMD_DIRTY_DYNAMIC_ALL = (1ull << 30) - 1,
1137    RADV_CMD_DIRTY_PIPELINE = 1ull << 30,
1138    RADV_CMD_DIRTY_INDEX_BUFFER = 1ull << 31,
1139    RADV_CMD_DIRTY_FRAMEBUFFER = 1ull << 32,
1140    RADV_CMD_DIRTY_VERTEX_BUFFER = 1ull << 33,
1141    RADV_CMD_DIRTY_STREAMOUT_BUFFER = 1ull << 34,
1142 };
1143
1144 enum radv_cmd_flush_bits {
1145    /* Instruction cache. */
1146    RADV_CMD_FLAG_INV_ICACHE = 1 << 0,
1147    /* Scalar L1 cache. */
1148    RADV_CMD_FLAG_INV_SCACHE = 1 << 1,
1149    /* Vector L1 cache. */
1150    RADV_CMD_FLAG_INV_VCACHE = 1 << 2,
1151    /* L2 cache + L2 metadata cache writeback & invalidate.
1152     * GFX6-8: Used by shaders only. GFX9-10: Used by everything. */
1153    RADV_CMD_FLAG_INV_L2 = 1 << 3,
1154    /* L2 writeback (write dirty L2 lines to memory for non-L2 clients).
1155     * Only used for coherency with non-L2 clients like CB, DB, CP on GFX6-8.
1156     * GFX6-7 will do complete invalidation, because the writeback is unsupported. */
1157    RADV_CMD_FLAG_WB_L2 = 1 << 4,
1158    /* Invalidate the metadata cache. To be used when the DCC/HTILE metadata
1159     * changed and we want to read an image from shaders. */
1160    RADV_CMD_FLAG_INV_L2_METADATA = 1 << 5,
1161    /* Framebuffer caches */
1162    RADV_CMD_FLAG_FLUSH_AND_INV_CB_META = 1 << 6,
1163    RADV_CMD_FLAG_FLUSH_AND_INV_DB_META = 1 << 7,
1164    RADV_CMD_FLAG_FLUSH_AND_INV_DB = 1 << 8,
1165    RADV_CMD_FLAG_FLUSH_AND_INV_CB = 1 << 9,
1166    /* Engine synchronization. */
1167    RADV_CMD_FLAG_VS_PARTIAL_FLUSH = 1 << 10,
1168    RADV_CMD_FLAG_PS_PARTIAL_FLUSH = 1 << 11,
1169    RADV_CMD_FLAG_CS_PARTIAL_FLUSH = 1 << 12,
1170    RADV_CMD_FLAG_VGT_FLUSH = 1 << 13,
1171    /* Pipeline query controls. */
1172    RADV_CMD_FLAG_START_PIPELINE_STATS = 1 << 14,
1173    RADV_CMD_FLAG_STOP_PIPELINE_STATS = 1 << 15,
1174    RADV_CMD_FLAG_VGT_STREAMOUT_SYNC = 1 << 16,
1175
1176    RADV_CMD_FLUSH_AND_INV_FRAMEBUFFER =
1177       (RADV_CMD_FLAG_FLUSH_AND_INV_CB | RADV_CMD_FLAG_FLUSH_AND_INV_CB_META |
1178        RADV_CMD_FLAG_FLUSH_AND_INV_DB | RADV_CMD_FLAG_FLUSH_AND_INV_DB_META)
1179 };
1180
1181 enum radv_nggc_settings {
1182    radv_nggc_none = 0,
1183    radv_nggc_front_face = 1 << 0,
1184    radv_nggc_back_face = 1 << 1,
1185    radv_nggc_face_is_ccw = 1 << 2,
1186    radv_nggc_small_primitives = 1 << 3,
1187 };
1188
1189 struct radv_vertex_binding {
1190    VkDeviceSize offset;
1191    VkDeviceSize size;
1192    VkDeviceSize stride;
1193 };
1194
1195 struct radv_streamout_binding {
1196    struct radv_buffer *buffer;
1197    VkDeviceSize offset;
1198    VkDeviceSize size;
1199 };
1200
1201 struct radv_streamout_state {
1202    /* Mask of bound streamout buffers. */
1203    uint8_t enabled_mask;
1204
1205    /* State of VGT_STRMOUT_BUFFER_(CONFIG|END) */
1206    uint32_t hw_enabled_mask;
1207
1208    /* State of VGT_STRMOUT_(CONFIG|EN) */
1209    bool streamout_enabled;
1210 };
1211
1212 struct radv_viewport_state {
1213    uint32_t count;
1214    VkViewport viewports[MAX_VIEWPORTS];
1215    struct {
1216       float scale[3];
1217       float translate[3];
1218    } xform[MAX_VIEWPORTS];
1219 };
1220
1221 struct radv_scissor_state {
1222    uint32_t count;
1223    VkRect2D scissors[MAX_SCISSORS];
1224 };
1225
1226 struct radv_discard_rectangle_state {
1227    uint32_t count;
1228    VkRect2D rectangles[MAX_DISCARD_RECTANGLES];
1229 };
1230
1231 struct radv_sample_locations_state {
1232    VkSampleCountFlagBits per_pixel;
1233    VkExtent2D grid_size;
1234    uint32_t count;
1235    VkSampleLocationEXT locations[MAX_SAMPLE_LOCATIONS];
1236 };
1237
1238 struct radv_dynamic_state {
1239    /**
1240     * Bitmask of (1ull << VK_DYNAMIC_STATE_*).
1241     * Defines the set of saved dynamic state.
1242     */
1243    uint64_t mask;
1244
1245    struct radv_viewport_state viewport;
1246
1247    struct radv_scissor_state scissor;
1248
1249    float line_width;
1250
1251    struct {
1252       float bias;
1253       float clamp;
1254       float slope;
1255    } depth_bias;
1256
1257    float blend_constants[4];
1258
1259    struct {
1260       float min;
1261       float max;
1262    } depth_bounds;
1263
1264    struct {
1265       uint32_t front;
1266       uint32_t back;
1267    } stencil_compare_mask;
1268
1269    struct {
1270       uint32_t front;
1271       uint32_t back;
1272    } stencil_write_mask;
1273
1274    struct {
1275       struct {
1276          VkStencilOp fail_op;
1277          VkStencilOp pass_op;
1278          VkStencilOp depth_fail_op;
1279          VkCompareOp compare_op;
1280       } front;
1281
1282       struct {
1283          VkStencilOp fail_op;
1284          VkStencilOp pass_op;
1285          VkStencilOp depth_fail_op;
1286          VkCompareOp compare_op;
1287       } back;
1288    } stencil_op;
1289
1290    struct {
1291       uint32_t front;
1292       uint32_t back;
1293    } stencil_reference;
1294
1295    struct radv_discard_rectangle_state discard_rectangle;
1296
1297    struct radv_sample_locations_state sample_location;
1298
1299    struct {
1300       uint32_t factor;
1301       uint16_t pattern;
1302    } line_stipple;
1303
1304    VkCullModeFlags cull_mode;
1305    VkFrontFace front_face;
1306    unsigned primitive_topology;
1307
1308    bool depth_test_enable;
1309    bool depth_write_enable;
1310    VkCompareOp depth_compare_op;
1311    bool depth_bounds_test_enable;
1312    bool stencil_test_enable;
1313
1314    struct {
1315       VkExtent2D size;
1316       VkFragmentShadingRateCombinerOpKHR combiner_ops[2];
1317    } fragment_shading_rate;
1318
1319    bool depth_bias_enable;
1320    bool primitive_restart_enable;
1321    bool rasterizer_discard_enable;
1322
1323    unsigned logic_op;
1324
1325    uint32_t color_write_enable;
1326 };
1327
1328 extern const struct radv_dynamic_state default_dynamic_state;
1329
1330 const char *radv_get_debug_option_name(int id);
1331
1332 const char *radv_get_perftest_option_name(int id);
1333
1334 int radv_get_int_debug_option(const char *name, int default_value);
1335
1336 struct radv_color_buffer_info {
1337    uint64_t cb_color_base;
1338    uint64_t cb_color_cmask;
1339    uint64_t cb_color_fmask;
1340    uint64_t cb_dcc_base;
1341    uint32_t cb_color_slice;
1342    uint32_t cb_color_view;
1343    uint32_t cb_color_info;
1344    uint32_t cb_color_attrib;
1345    uint32_t cb_color_attrib2; /* GFX9 and later */
1346    uint32_t cb_color_attrib3; /* GFX10 and later */
1347    uint32_t cb_dcc_control;
1348    uint32_t cb_color_cmask_slice;
1349    uint32_t cb_color_fmask_slice;
1350    union {
1351       uint32_t cb_color_pitch; // GFX6-GFX8
1352       uint32_t cb_mrt_epitch;  // GFX9+
1353    };
1354 };
1355
1356 struct radv_ds_buffer_info {
1357    uint64_t db_z_read_base;
1358    uint64_t db_stencil_read_base;
1359    uint64_t db_z_write_base;
1360    uint64_t db_stencil_write_base;
1361    uint64_t db_htile_data_base;
1362    uint32_t db_depth_info;
1363    uint32_t db_z_info;
1364    uint32_t db_stencil_info;
1365    uint32_t db_depth_view;
1366    uint32_t db_depth_size;
1367    uint32_t db_depth_slice;
1368    uint32_t db_htile_surface;
1369    uint32_t pa_su_poly_offset_db_fmt_cntl;
1370    uint32_t db_z_info2;       /* GFX9 only */
1371    uint32_t db_stencil_info2; /* GFX9 only */
1372 };
1373
1374 void radv_initialise_color_surface(struct radv_device *device, struct radv_color_buffer_info *cb,
1375                                    struct radv_image_view *iview);
1376 void radv_initialise_ds_surface(struct radv_device *device, struct radv_ds_buffer_info *ds,
1377                                 struct radv_image_view *iview);
1378 void radv_initialise_vrs_surface(struct radv_image *image, struct radv_buffer *htile_buffer,
1379                                  struct radv_ds_buffer_info *ds);
1380
1381 /**
1382  * Attachment state when recording a renderpass instance.
1383  *
1384  * The clear value is valid only if there exists a pending clear.
1385  */
1386 struct radv_attachment_state {
1387    VkImageAspectFlags pending_clear_aspects;
1388    uint32_t cleared_views;
1389    VkClearValue clear_value;
1390    VkImageLayout current_layout;
1391    VkImageLayout current_stencil_layout;
1392    bool current_in_render_loop;
1393    struct radv_sample_locations_state sample_location;
1394
1395    union {
1396       struct radv_color_buffer_info cb;
1397       struct radv_ds_buffer_info ds;
1398    };
1399    struct radv_image_view *iview;
1400 };
1401
1402 struct radv_descriptor_state {
1403    struct radv_descriptor_set *sets[MAX_SETS];
1404    uint32_t dirty;
1405    uint32_t valid;
1406    struct radv_push_descriptor_set push_set;
1407    bool push_dirty;
1408    uint32_t dynamic_buffers[4 * MAX_DYNAMIC_BUFFERS];
1409 };
1410
1411 struct radv_subpass_sample_locs_state {
1412    uint32_t subpass_idx;
1413    struct radv_sample_locations_state sample_location;
1414 };
1415
1416 enum rgp_flush_bits {
1417    RGP_FLUSH_WAIT_ON_EOP_TS = 0x1,
1418    RGP_FLUSH_VS_PARTIAL_FLUSH = 0x2,
1419    RGP_FLUSH_PS_PARTIAL_FLUSH = 0x4,
1420    RGP_FLUSH_CS_PARTIAL_FLUSH = 0x8,
1421    RGP_FLUSH_PFP_SYNC_ME = 0x10,
1422    RGP_FLUSH_SYNC_CP_DMA = 0x20,
1423    RGP_FLUSH_INVAL_VMEM_L0 = 0x40,
1424    RGP_FLUSH_INVAL_ICACHE = 0x80,
1425    RGP_FLUSH_INVAL_SMEM_L0 = 0x100,
1426    RGP_FLUSH_FLUSH_L2 = 0x200,
1427    RGP_FLUSH_INVAL_L2 = 0x400,
1428    RGP_FLUSH_FLUSH_CB = 0x800,
1429    RGP_FLUSH_INVAL_CB = 0x1000,
1430    RGP_FLUSH_FLUSH_DB = 0x2000,
1431    RGP_FLUSH_INVAL_DB = 0x4000,
1432    RGP_FLUSH_INVAL_L1 = 0x8000,
1433 };
1434
1435 struct radv_cmd_state {
1436    /* Vertex descriptors */
1437    uint64_t vb_va;
1438
1439    bool predicating;
1440    uint64_t dirty;
1441
1442    uint32_t prefetch_L2_mask;
1443
1444    struct radv_graphics_pipeline *graphics_pipeline;
1445    struct radv_graphics_pipeline *emitted_graphics_pipeline;
1446    struct radv_compute_pipeline *compute_pipeline;
1447    struct radv_compute_pipeline *emitted_compute_pipeline;
1448    struct radv_compute_pipeline *rt_pipeline; /* emitted = emitted_compute_pipeline */
1449    struct vk_framebuffer *framebuffer;
1450    struct radv_render_pass *pass;
1451    const struct radv_subpass *subpass;
1452    struct radv_dynamic_state dynamic;
1453    struct radv_vs_input_state dynamic_vs_input;
1454    struct radv_attachment_state *attachments;
1455    struct radv_streamout_state streamout;
1456    VkRect2D render_area;
1457
1458    uint32_t num_subpass_sample_locs;
1459    struct radv_subpass_sample_locs_state *subpass_sample_locs;
1460
1461    /* Index buffer */
1462    struct radv_buffer *index_buffer;
1463    uint64_t index_offset;
1464    uint32_t index_type;
1465    uint32_t max_index_count;
1466    uint64_t index_va;
1467    int32_t last_index_type;
1468
1469    int32_t last_primitive_reset_en;
1470    uint32_t last_primitive_reset_index;
1471    enum radv_cmd_flush_bits flush_bits;
1472    unsigned active_occlusion_queries;
1473    bool perfect_occlusion_queries_enabled;
1474    unsigned active_pipeline_queries;
1475    unsigned active_pipeline_gds_queries;
1476    bool prims_gen_query_enabled;
1477    uint32_t trace_id;
1478    uint32_t last_ia_multi_vgt_param;
1479
1480    uint32_t last_num_instances;
1481    uint32_t last_first_instance;
1482    uint32_t last_vertex_offset;
1483    uint32_t last_drawid;
1484    uint32_t last_subpass_color_count;
1485
1486    uint32_t last_sx_ps_downconvert;
1487    uint32_t last_sx_blend_opt_epsilon;
1488    uint32_t last_sx_blend_opt_control;
1489
1490    /* Whether CP DMA is busy/idle. */
1491    bool dma_is_busy;
1492
1493    /* Whether any images that are not L2 coherent are dirty from the CB. */
1494    bool rb_noncoherent_dirty;
1495
1496    /* Conditional rendering info. */
1497    uint8_t predication_op; /* 32-bit or 64-bit predicate value */
1498    int predication_type;   /* -1: disabled, 0: normal, 1: inverted */
1499    uint64_t predication_va;
1500
1501    /* Inheritance info. */
1502    VkQueryPipelineStatisticFlags inherited_pipeline_statistics;
1503
1504    bool context_roll_without_scissor_emitted;
1505
1506    /* SQTT related state. */
1507    uint32_t current_event_type;
1508    uint32_t num_events;
1509    uint32_t num_layout_transitions;
1510    bool pending_sqtt_barrier_end;
1511    enum rgp_flush_bits sqtt_flush_bits;
1512
1513    /* NGG culling state. */
1514    uint32_t last_nggc_settings;
1515    int8_t last_nggc_settings_sgpr_idx;
1516    bool last_nggc_skip;
1517
1518    /* Mesh shading state. */
1519    bool mesh_shading;
1520
1521    uint8_t cb_mip[MAX_RTS];
1522
1523    /* Whether DRAW_{INDEX}_INDIRECT_MULTI is emitted. */
1524    bool uses_draw_indirect_multi;
1525
1526    uint32_t rt_stack_size;
1527
1528    struct radv_shader_part *emitted_vs_prolog;
1529    uint32_t *emitted_vs_prolog_key;
1530    uint32_t emitted_vs_prolog_key_hash;
1531    uint32_t vbo_misaligned_mask;
1532    uint32_t vbo_bound_mask;
1533
1534    /* Whether the cmdbuffer owns the current render pass rather than the app. */
1535    bool own_render_pass;
1536
1537    /* Per-vertex VRS state. */
1538    uint32_t last_vrs_rates;
1539    int8_t last_vrs_rates_sgpr_idx;
1540
1541    /* Whether to suspend streamout for internal driver operations. */
1542    bool suspend_streamout;
1543
1544    /* Whether this commandbuffer uses performance counters. */
1545    bool uses_perf_counters;
1546 };
1547
1548 struct radv_cmd_pool {
1549    struct vk_command_pool vk;
1550    struct list_head cmd_buffers;
1551    struct list_head free_cmd_buffers;
1552 };
1553
1554 struct radv_cmd_buffer_upload {
1555    uint8_t *map;
1556    unsigned offset;
1557    uint64_t size;
1558    struct radeon_winsys_bo *upload_bo;
1559    struct list_head list;
1560 };
1561
1562 enum radv_cmd_buffer_status {
1563    RADV_CMD_BUFFER_STATUS_INVALID,
1564    RADV_CMD_BUFFER_STATUS_INITIAL,
1565    RADV_CMD_BUFFER_STATUS_RECORDING,
1566    RADV_CMD_BUFFER_STATUS_EXECUTABLE,
1567    RADV_CMD_BUFFER_STATUS_PENDING,
1568 };
1569
1570 struct radv_cmd_buffer {
1571    struct vk_command_buffer vk;
1572
1573    struct radv_device *device;
1574
1575    struct radv_cmd_pool *pool;
1576    struct list_head pool_link;
1577
1578    VkCommandBufferUsageFlags usage_flags;
1579    enum radv_cmd_buffer_status status;
1580    struct radeon_cmdbuf *cs;
1581    struct radv_cmd_state state;
1582    struct radv_buffer *vertex_binding_buffers[MAX_VBS];
1583    struct radv_vertex_binding vertex_bindings[MAX_VBS];
1584    uint32_t used_vertex_bindings;
1585    struct radv_streamout_binding streamout_bindings[MAX_SO_BUFFERS];
1586    enum radv_queue_family qf;
1587
1588    uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
1589    VkShaderStageFlags push_constant_stages;
1590    struct radv_descriptor_set_header meta_push_descriptors;
1591
1592    struct radv_descriptor_state descriptors[MAX_BIND_POINTS];
1593
1594    struct radv_cmd_buffer_upload upload;
1595
1596    uint32_t scratch_size_per_wave_needed;
1597    uint32_t scratch_waves_wanted;
1598    uint32_t compute_scratch_size_per_wave_needed;
1599    uint32_t compute_scratch_waves_wanted;
1600    uint32_t esgs_ring_size_needed;
1601    uint32_t gsvs_ring_size_needed;
1602    bool tess_rings_needed;
1603    bool task_rings_needed;
1604    bool mesh_scratch_ring_needed;
1605    bool gds_needed;    /* for GFX10 streamout and NGG GS queries */
1606    bool gds_oa_needed; /* for GFX10 streamout */
1607    bool sample_positions_needed;
1608
1609    VkResult record_result;
1610
1611    uint64_t gfx9_fence_va;
1612    uint32_t gfx9_fence_idx;
1613    uint64_t gfx9_eop_bug_va;
1614
1615    /**
1616     * Whether a query pool has been resetted and we have to flush caches.
1617     */
1618    bool pending_reset_query;
1619
1620    /**
1621     * Bitmask of pending active query flushes.
1622     */
1623    enum radv_cmd_flush_bits active_query_flush_bits;
1624 };
1625
1626 struct radv_image;
1627 struct radv_image_view;
1628
1629 bool radv_cmd_buffer_uses_mec(struct radv_cmd_buffer *cmd_buffer);
1630
1631 bool radv_is_streamout_enabled(struct radv_cmd_buffer *cmd_buffer);
1632 void radv_emit_streamout_enable(struct radv_cmd_buffer *cmd_buffer);
1633
1634 void si_emit_graphics(struct radv_device *device, struct radeon_cmdbuf *cs);
1635 void si_emit_compute(struct radv_device *device, struct radeon_cmdbuf *cs);
1636
1637 void cik_create_gfx_config(struct radv_device *device);
1638
1639 void si_write_scissors(struct radeon_cmdbuf *cs, int first, int count, const VkRect2D *scissors,
1640                        const VkViewport *viewports, bool can_use_guardband);
1641 uint32_t si_get_ia_multi_vgt_param(struct radv_cmd_buffer *cmd_buffer, bool instanced_draw,
1642                                    bool indirect_draw, bool count_from_stream_output,
1643                                    uint32_t draw_vertex_count, unsigned topology,
1644                                    bool prim_restart_enable);
1645 void si_cs_emit_write_event_eop(struct radeon_cmdbuf *cs, enum amd_gfx_level gfx_level, bool is_mec,
1646                                 unsigned event, unsigned event_flags, unsigned dst_sel,
1647                                 unsigned data_sel, uint64_t va, uint32_t new_fence,
1648                                 uint64_t gfx9_eop_bug_va);
1649
1650 void radv_cp_wait_mem(struct radeon_cmdbuf *cs, uint32_t op, uint64_t va, uint32_t ref,
1651                       uint32_t mask);
1652 void si_cs_emit_cache_flush(struct radeon_cmdbuf *cs, enum amd_gfx_level gfx_level,
1653                             uint32_t *fence_ptr, uint64_t va, bool is_mec,
1654                             enum radv_cmd_flush_bits flush_bits,
1655                             enum rgp_flush_bits *sqtt_flush_bits, uint64_t gfx9_eop_bug_va);
1656 void si_emit_cache_flush(struct radv_cmd_buffer *cmd_buffer);
1657 void si_emit_set_predication_state(struct radv_cmd_buffer *cmd_buffer, bool draw_visible,
1658                                    unsigned pred_op, uint64_t va);
1659 void si_cp_dma_buffer_copy(struct radv_cmd_buffer *cmd_buffer, uint64_t src_va, uint64_t dest_va,
1660                            uint64_t size);
1661 void si_cp_dma_prefetch(struct radv_cmd_buffer *cmd_buffer, uint64_t va, unsigned size);
1662 void si_cp_dma_clear_buffer(struct radv_cmd_buffer *cmd_buffer, uint64_t va, uint64_t size,
1663                             unsigned value);
1664 void si_cp_dma_wait_for_idle(struct radv_cmd_buffer *cmd_buffer);
1665
1666 void radv_set_db_count_control(struct radv_cmd_buffer *cmd_buffer, bool enable_occlusion_queries);
1667
1668 unsigned radv_instance_rate_prolog_index(unsigned num_attributes, uint32_t instance_rate_inputs);
1669 uint32_t radv_hash_vs_prolog(const void *key_);
1670 bool radv_cmp_vs_prolog(const void *a_, const void *b_);
1671
1672 bool radv_cmd_buffer_upload_alloc(struct radv_cmd_buffer *cmd_buffer, unsigned size,
1673                                   unsigned *out_offset, void **ptr);
1674 void radv_cmd_buffer_set_subpass(struct radv_cmd_buffer *cmd_buffer,
1675                                  const struct radv_subpass *subpass);
1676 void radv_cmd_buffer_restore_subpass(struct radv_cmd_buffer *cmd_buffer,
1677                                      const struct radv_subpass *subpass);
1678 bool radv_cmd_buffer_upload_data(struct radv_cmd_buffer *cmd_buffer, unsigned size,
1679                                  const void *data, unsigned *out_offset);
1680
1681 void radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer *cmd_buffer);
1682 void radv_cmd_buffer_resolve_subpass(struct radv_cmd_buffer *cmd_buffer);
1683 void radv_cmd_buffer_resolve_subpass_cs(struct radv_cmd_buffer *cmd_buffer);
1684 void radv_depth_stencil_resolve_subpass_cs(struct radv_cmd_buffer *cmd_buffer,
1685                                            VkImageAspectFlags aspects,
1686                                            VkResolveModeFlagBits resolve_mode);
1687 void radv_cmd_buffer_resolve_subpass_fs(struct radv_cmd_buffer *cmd_buffer);
1688 void radv_depth_stencil_resolve_subpass_fs(struct radv_cmd_buffer *cmd_buffer,
1689                                            VkImageAspectFlags aspects,
1690                                            VkResolveModeFlagBits resolve_mode);
1691 void radv_emit_default_sample_locations(struct radeon_cmdbuf *cs, int nr_samples);
1692 unsigned radv_get_default_max_sample_dist(int log_samples);
1693 void radv_device_init_msaa(struct radv_device *device);
1694 VkResult radv_device_init_vrs_state(struct radv_device *device);
1695
1696 void radv_update_ds_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
1697                                    const struct radv_image_view *iview,
1698                                    VkClearDepthStencilValue ds_clear_value,
1699                                    VkImageAspectFlags aspects);
1700
1701 void radv_update_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
1702                                       const struct radv_image_view *iview, int cb_idx,
1703                                       uint32_t color_values[2]);
1704
1705 bool radv_image_use_dcc_image_stores(const struct radv_device *device,
1706                                      const struct radv_image *image);
1707 bool radv_image_use_dcc_predication(const struct radv_device *device,
1708                                     const struct radv_image *image);
1709
1710 void radv_update_fce_metadata(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
1711                               const VkImageSubresourceRange *range, bool value);
1712
1713 void radv_update_dcc_metadata(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
1714                               const VkImageSubresourceRange *range, bool value);
1715 enum radv_cmd_flush_bits radv_src_access_flush(struct radv_cmd_buffer *cmd_buffer,
1716                                                VkAccessFlags2 src_flags,
1717                                                const struct radv_image *image);
1718 enum radv_cmd_flush_bits radv_dst_access_flush(struct radv_cmd_buffer *cmd_buffer,
1719                                                VkAccessFlags2 dst_flags,
1720                                                const struct radv_image *image);
1721 uint32_t radv_fill_buffer(struct radv_cmd_buffer *cmd_buffer, const struct radv_image *image,
1722                           struct radeon_winsys_bo *bo, uint64_t va, uint64_t size, uint32_t value);
1723 void radv_copy_buffer(struct radv_cmd_buffer *cmd_buffer, struct radeon_winsys_bo *src_bo,
1724                       struct radeon_winsys_bo *dst_bo, uint64_t src_offset, uint64_t dst_offset,
1725                       uint64_t size);
1726
1727 void radv_cmd_buffer_trace_emit(struct radv_cmd_buffer *cmd_buffer);
1728 bool radv_get_memory_fd(struct radv_device *device, struct radv_device_memory *memory, int *pFD);
1729 void radv_free_memory(struct radv_device *device, const VkAllocationCallbacks *pAllocator,
1730                       struct radv_device_memory *mem);
1731
1732 static inline void
1733 radv_emit_shader_pointer_head(struct radeon_cmdbuf *cs, unsigned sh_offset, unsigned pointer_count,
1734                               bool use_32bit_pointers)
1735 {
1736    radeon_emit(cs, PKT3(PKT3_SET_SH_REG, pointer_count * (use_32bit_pointers ? 1 : 2), 0));
1737    radeon_emit(cs, (sh_offset - SI_SH_REG_OFFSET) >> 2);
1738 }
1739
1740 static inline void
1741 radv_emit_shader_pointer_body(struct radv_device *device, struct radeon_cmdbuf *cs, uint64_t va,
1742                               bool use_32bit_pointers)
1743 {
1744    radeon_emit(cs, va);
1745
1746    if (use_32bit_pointers) {
1747       assert(va == 0 || (va >> 32) == device->physical_device->rad_info.address32_hi);
1748    } else {
1749       radeon_emit(cs, va >> 32);
1750    }
1751 }
1752
1753 static inline void
1754 radv_emit_shader_pointer(struct radv_device *device, struct radeon_cmdbuf *cs, uint32_t sh_offset,
1755                          uint64_t va, bool global)
1756 {
1757    bool use_32bit_pointers = !global;
1758
1759    radv_emit_shader_pointer_head(cs, sh_offset, 1, use_32bit_pointers);
1760    radv_emit_shader_pointer_body(device, cs, va, use_32bit_pointers);
1761 }
1762
1763 static inline struct radv_descriptor_state *
1764 radv_get_descriptors_state(struct radv_cmd_buffer *cmd_buffer, VkPipelineBindPoint bind_point)
1765 {
1766    switch (bind_point) {
1767    case VK_PIPELINE_BIND_POINT_GRAPHICS:
1768    case VK_PIPELINE_BIND_POINT_COMPUTE:
1769       return &cmd_buffer->descriptors[bind_point];
1770    case VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR:
1771       return &cmd_buffer->descriptors[2];
1772    default:
1773       unreachable("Unhandled bind point");
1774    }
1775 }
1776
1777 void
1778 radv_get_viewport_xform(const VkViewport *viewport, float scale[3], float translate[3]);
1779
1780 /*
1781  * Takes x,y,z as exact numbers of invocations, instead of blocks.
1782  *
1783  * Limitations: Can't call normal dispatch functions without binding or rebinding
1784  *              the compute pipeline.
1785  */
1786 void radv_unaligned_dispatch(struct radv_cmd_buffer *cmd_buffer, uint32_t x, uint32_t y,
1787                              uint32_t z);
1788
1789 void radv_indirect_dispatch(struct radv_cmd_buffer *cmd_buffer, struct radeon_winsys_bo *bo,
1790                             uint64_t va);
1791
1792 struct radv_event {
1793    struct vk_object_base base;
1794    struct radeon_winsys_bo *bo;
1795    uint64_t *map;
1796 };
1797
1798 #define RADV_HASH_SHADER_CS_WAVE32         (1 << 1)
1799 #define RADV_HASH_SHADER_PS_WAVE32         (1 << 2)
1800 #define RADV_HASH_SHADER_GE_WAVE32         (1 << 3)
1801 #define RADV_HASH_SHADER_LLVM              (1 << 4)
1802 #define RADV_HASH_SHADER_KEEP_STATISTICS   (1 << 8)
1803 #define RADV_HASH_SHADER_USE_NGG_CULLING   (1 << 13)
1804 #define RADV_HASH_SHADER_ROBUST_BUFFER_ACCESS (1 << 14)
1805 #define RADV_HASH_SHADER_ROBUST_BUFFER_ACCESS2 (1 << 15)
1806 #define RADV_HASH_SHADER_EMULATE_RT            (1 << 16)
1807 #define RADV_HASH_SHADER_SPLIT_FMA             (1 << 17)
1808 #define RADV_HASH_SHADER_RT_WAVE64             (1 << 18)
1809
1810 struct radv_pipeline_key;
1811
1812 void radv_pipeline_stage_init(const VkPipelineShaderStageCreateInfo *sinfo,
1813                               struct radv_pipeline_stage *out_stage, gl_shader_stage stage);
1814
1815 void radv_hash_shaders(unsigned char *hash, const struct radv_pipeline_stage *stages,
1816                        const struct radv_pipeline_layout *layout,
1817                        const struct radv_pipeline_key *key, uint32_t flags);
1818
1819 void radv_hash_rt_shaders(unsigned char *hash, const VkRayTracingPipelineCreateInfoKHR *pCreateInfo,
1820                           uint32_t flags);
1821
1822 uint32_t radv_get_hash_flags(const struct radv_device *device, bool stats);
1823
1824 bool radv_rt_pipeline_has_dynamic_stack_size(const VkRayTracingPipelineCreateInfoKHR *pCreateInfo);
1825
1826 bool radv_enable_rt(const struct radv_physical_device *pdevice, bool rt_pipelines);
1827
1828 bool radv_emulate_rt(const struct radv_physical_device *pdevice);
1829
1830 enum {
1831    RADV_RT_STAGE_BITS = (VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR |
1832                          VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR |
1833                          VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR)
1834 };
1835
1836 #define RADV_STAGE_MASK ((1 << MESA_VULKAN_SHADER_STAGES) - 1)
1837
1838 #define radv_foreach_stage(stage, stage_bits)                                                      \
1839    for (gl_shader_stage stage, __tmp = (gl_shader_stage)((stage_bits)&RADV_STAGE_MASK);            \
1840         stage = ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
1841
1842 extern const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS];
1843 unsigned radv_format_meta_fs_key(struct radv_device *device, VkFormat format);
1844
1845 struct radv_multisample_state {
1846    uint32_t db_eqaa;
1847    uint32_t pa_sc_mode_cntl_0;
1848    uint32_t pa_sc_mode_cntl_1;
1849    uint32_t pa_sc_aa_config;
1850    uint32_t pa_sc_aa_mask[2];
1851    unsigned num_samples;
1852 };
1853
1854 struct radv_vrs_state {
1855    uint32_t pa_cl_vrs_cntl;
1856 };
1857
1858 struct radv_prim_vertex_count {
1859    uint8_t min;
1860    uint8_t incr;
1861 };
1862
1863 struct radv_ia_multi_vgt_param_helpers {
1864    uint32_t base;
1865    bool partial_es_wave;
1866    uint8_t primgroup_size;
1867    bool ia_switch_on_eoi;
1868    bool partial_vs_wave;
1869 };
1870
1871 struct radv_binning_state {
1872    uint32_t pa_sc_binner_cntl_0;
1873 };
1874
1875 #define SI_GS_PER_ES 128
1876
1877 enum radv_pipeline_type {
1878    RADV_PIPELINE_GRAPHICS,
1879    /* Compute pipeline (incl raytracing pipeline) */
1880    RADV_PIPELINE_COMPUTE,
1881    /* Pipeline library. This can't actually run and merely is a partial pipeline. */
1882    RADV_PIPELINE_LIBRARY
1883 };
1884
1885 struct radv_pipeline_group_handle {
1886    uint32_t handles[2];
1887 };
1888
1889 struct radv_pipeline_shader_stack_size {
1890    uint32_t recursive_size;
1891    /* anyhit + intersection */
1892    uint32_t non_recursive_size;
1893 };
1894
1895 struct radv_pipeline_slab {
1896    uint32_t ref_count;
1897
1898    union radv_shader_arena_block *alloc;
1899 };
1900
1901 void radv_pipeline_slab_destroy(struct radv_device *device, struct radv_pipeline_slab *slab);
1902
1903 struct radv_vertex_input_info {
1904    uint32_t instance_rate_inputs;
1905    uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
1906    uint8_t vertex_attribute_formats[MAX_VERTEX_ATTRIBS];
1907    uint32_t vertex_attribute_bindings[MAX_VERTEX_ATTRIBS];
1908    uint32_t vertex_attribute_offsets[MAX_VERTEX_ATTRIBS];
1909    uint32_t vertex_attribute_strides[MAX_VERTEX_ATTRIBS];
1910    uint8_t vertex_binding_align[MAX_VBS];
1911    enum radv_vs_input_alpha_adjust vertex_alpha_adjust[MAX_VERTEX_ATTRIBS];
1912    uint32_t vertex_post_shuffle;
1913    uint32_t binding_stride[MAX_VBS];
1914    uint8_t attrib_bindings[MAX_VERTEX_ATTRIBS];
1915    uint32_t attrib_ends[MAX_VERTEX_ATTRIBS];
1916    uint32_t attrib_index_offset[MAX_VERTEX_ATTRIBS];
1917 };
1918
1919 struct radv_input_assembly_info {
1920    uint8_t primitive_topology; /* VkPrimitiveTopology */
1921    bool primitive_restart_enable;
1922 };
1923
1924 struct radv_tessellation_info {
1925    uint8_t patch_control_points;
1926    VkTessellationDomainOrigin domain_origin;
1927 };
1928
1929 struct radv_viewport_info {
1930    bool negative_one_to_one;
1931    uint8_t viewport_count;
1932    uint8_t scissor_count;
1933    VkRect2D scissors[MAX_SCISSORS];
1934    VkViewport viewports[MAX_VIEWPORTS];
1935 };
1936
1937 struct radv_rasterization_info {
1938    bool discard_enable;
1939    VkFrontFace front_face;
1940    VkCullModeFlags cull_mode;
1941    uint8_t polygon_mode; /* VkPolygonMode */
1942    bool depth_bias_enable;
1943    bool depth_clamp_enable;
1944    float line_width;
1945    float depth_bias_constant_factor;
1946    float depth_bias_clamp;
1947    float depth_bias_slope_factor;
1948    VkConservativeRasterizationModeEXT conservative_mode;
1949    bool provoking_vtx_last;
1950    bool stippled_line_enable;
1951    VkLineRasterizationModeEXT line_raster_mode;
1952    uint32_t line_stipple_factor;
1953    uint16_t line_stipple_pattern;
1954    bool depth_clip_disable;
1955    VkRasterizationOrderAMD order;
1956 };
1957
1958 struct radv_discard_rectangle_info {
1959    VkDiscardRectangleModeEXT mode;
1960    VkRect2D rects[MAX_DISCARD_RECTANGLES];
1961    uint8_t count;
1962 };
1963
1964 struct radv_multisample_info {
1965    bool sample_shading_enable;
1966    bool alpha_to_coverage_enable;
1967    bool sample_locs_enable;
1968    VkSampleCountFlagBits raster_samples;
1969    float min_sample_shading;
1970    uint16_t sample_mask;
1971    uint8_t sample_locs_count;
1972    VkSampleCountFlagBits sample_locs_per_pixel;
1973    VkExtent2D sample_locs_grid_size;
1974    VkSampleLocationEXT sample_locs[MAX_SAMPLE_LOCATIONS];
1975 };
1976
1977 struct radv_stencil_op_info {
1978    VkStencilOp fail_op;
1979    VkStencilOp pass_op;
1980    VkStencilOp depth_fail_op;
1981    VkCompareOp compare_op;
1982    uint8_t compare_mask;
1983    uint8_t write_mask;
1984    uint8_t reference;
1985 };
1986
1987 struct radv_depth_stencil_info {
1988    bool stencil_test_enable;
1989    bool depth_test_enable;
1990    bool depth_write_enable;
1991    bool depth_bounds_test_enable;
1992    struct {
1993       float min;
1994       float max;
1995    } depth_bounds;
1996    struct radv_stencil_op_info front;
1997    struct radv_stencil_op_info back;
1998    VkCompareOp depth_compare_op;
1999 };
2000
2001 struct radv_rendering_info {
2002    uint32_t view_mask;
2003    uint32_t color_att_count;
2004    VkFormat color_att_formats[MAX_RTS];
2005    VkFormat depth_att_format;
2006    VkFormat stencil_att_format;
2007 };
2008
2009 struct radv_color_blend_info {
2010    bool logic_op_enable;
2011    uint8_t att_count;
2012    uint16_t logic_op;
2013    uint32_t color_write_enable;
2014    float blend_constants[4];
2015    struct {
2016       uint8_t color_write_mask;
2017       bool blend_enable;
2018       uint16_t color_blend_op;
2019       uint16_t alpha_blend_op;
2020       uint16_t src_color_blend_factor;
2021       uint16_t dst_color_blend_factor;
2022       uint16_t src_alpha_blend_factor;
2023       uint16_t dst_alpha_blend_factor;
2024    } att[MAX_RTS];
2025 };
2026
2027 struct radv_fragment_shading_rate_info {
2028    VkExtent2D size;
2029    VkFragmentShadingRateCombinerOpKHR combiner_ops[2];
2030 };
2031
2032 struct radv_graphics_pipeline_info {
2033    struct radv_vertex_input_info vi;
2034    struct radv_input_assembly_info ia;
2035
2036    struct radv_tessellation_info ts;
2037    struct radv_viewport_info vp;
2038    struct radv_rasterization_info rs;
2039    struct radv_discard_rectangle_info dr;
2040
2041    struct radv_multisample_info ms;
2042    struct radv_depth_stencil_info ds;
2043    struct radv_rendering_info ri;
2044    struct radv_color_blend_info cb;
2045
2046    struct radv_fragment_shading_rate_info fsr;
2047
2048    /* VK_AMD_mixed_attachment_samples */
2049    uint8_t color_att_samples;
2050    uint8_t ds_att_samples;
2051 };
2052
2053 struct radv_pipeline {
2054    struct vk_object_base base;
2055    enum radv_pipeline_type type;
2056
2057    struct radv_device *device;
2058
2059    struct radv_pipeline_slab *slab;
2060    struct radeon_winsys_bo *slab_bo;
2061
2062    bool need_indirect_descriptor_sets;
2063    struct radv_shader *shaders[MESA_VULKAN_SHADER_STAGES];
2064    struct radv_shader *gs_copy_shader;
2065
2066    struct radeon_cmdbuf cs;
2067    uint32_t ctx_cs_hash;
2068    struct radeon_cmdbuf ctx_cs;
2069
2070    uint32_t user_data_0[MESA_VULKAN_SHADER_STAGES];
2071
2072    unsigned max_waves;
2073    unsigned scratch_bytes_per_wave;
2074
2075    /* Unique pipeline hash identifier. */
2076    uint64_t pipeline_hash;
2077
2078    /* Pipeline layout info. */
2079    uint32_t push_constant_size;
2080    uint32_t dynamic_offset_count;
2081 };
2082
2083 struct radv_graphics_pipeline {
2084    struct radv_pipeline base;
2085
2086    VkShaderStageFlags active_stages;
2087
2088    struct radv_dynamic_state dynamic_state;
2089
2090    uint64_t dynamic_states;
2091    struct radv_multisample_state ms;
2092    struct radv_binning_state binning;
2093    struct radv_vrs_state vrs;
2094    uint32_t spi_baryc_cntl;
2095    unsigned esgs_ring_size;
2096    unsigned gsvs_ring_size;
2097    uint32_t vtx_base_sgpr;
2098    struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param;
2099    uint8_t vtx_emit_num;
2100    uint64_t needed_dynamic_state;
2101    unsigned tess_patch_control_points;
2102    unsigned pa_su_sc_mode_cntl;
2103    unsigned db_depth_control;
2104    unsigned pa_cl_clip_cntl;
2105    unsigned cb_color_control;
2106    uint32_t binding_stride[MAX_VBS];
2107    uint8_t attrib_bindings[MAX_VERTEX_ATTRIBS];
2108    uint32_t attrib_ends[MAX_VERTEX_ATTRIBS];
2109    uint32_t attrib_index_offset[MAX_VERTEX_ATTRIBS];
2110    uint8_t last_vertex_attrib_bit;
2111    uint8_t next_vertex_stage : 8;
2112    uint32_t vb_desc_usage_mask;
2113    uint32_t vb_desc_alloc_size;
2114
2115    /* Last pre-PS API stage */
2116    gl_shader_stage last_vgt_api_stage;
2117
2118    /* Used for rbplus */
2119    uint32_t col_format;
2120    uint32_t cb_target_mask;
2121
2122    bool disable_out_of_order_rast_for_occlusion;
2123    bool uses_drawid;
2124    bool uses_baseinstance;
2125    bool can_use_guardband;
2126    bool uses_dynamic_stride;
2127    bool uses_conservative_overestimate;
2128    bool negative_one_to_one;
2129    bool use_per_attribute_vb_descs;
2130    bool can_use_simple_input;
2131    bool uses_user_sample_locations;
2132
2133    /* Whether the pipeline forces per-vertex VRS (GFX10.3+). */
2134    bool force_vrs_per_vertex;
2135
2136    /* Whether the pipeline uses NGG (GFX10+). */
2137    bool is_ngg;
2138    bool has_ngg_culling;
2139
2140    /* Not NULL if graphics pipeline uses streamout. */
2141    struct radv_shader *streamout_shader;
2142 };
2143
2144 struct radv_compute_pipeline {
2145    struct radv_pipeline base;
2146
2147    bool cs_regalloc_hang_bug;
2148
2149    /* Raytracing */
2150    struct radv_pipeline_group_handle *rt_group_handles;
2151    struct radv_pipeline_shader_stack_size *rt_stack_sizes;
2152    bool dynamic_stack_size;
2153    uint32_t group_count;
2154 };
2155
2156 struct radv_library_pipeline {
2157    struct radv_pipeline base;
2158
2159    unsigned stage_count;
2160    VkPipelineShaderStageCreateInfo *stages;
2161    unsigned group_count;
2162    VkRayTracingShaderGroupCreateInfoKHR *groups;
2163    VkPipelineShaderStageModuleIdentifierCreateInfoEXT *identifiers;
2164    struct {
2165       uint8_t sha1[SHA1_DIGEST_LENGTH];
2166    } *hashes;
2167 };
2168
2169 #define RADV_DECL_PIPELINE_DOWNCAST(pipe_type, pipe_enum)            \
2170    static inline struct radv_##pipe_type##_pipeline *                \
2171    radv_pipeline_to_##pipe_type(struct radv_pipeline *pipeline)      \
2172    {                                                                 \
2173       assert(pipeline->type == pipe_enum);                           \
2174       return (struct radv_##pipe_type##_pipeline *) pipeline;        \
2175    }
2176
2177 RADV_DECL_PIPELINE_DOWNCAST(graphics, RADV_PIPELINE_GRAPHICS)
2178 RADV_DECL_PIPELINE_DOWNCAST(compute, RADV_PIPELINE_COMPUTE)
2179 RADV_DECL_PIPELINE_DOWNCAST(library, RADV_PIPELINE_LIBRARY)
2180
2181 struct radv_pipeline_stage {
2182    gl_shader_stage stage;
2183
2184    struct {
2185       const struct vk_object_base *object;
2186       const char *data;
2187       uint32_t size;
2188       unsigned char sha1[20];
2189    } spirv;
2190
2191    const char *entrypoint;
2192    const VkSpecializationInfo *spec_info;
2193
2194    unsigned char shader_sha1[20];
2195
2196    nir_shader *nir;
2197    nir_shader *internal_nir; /* meta shaders */
2198
2199    struct radv_shader_info info;
2200    struct radv_shader_args args;
2201
2202    VkPipelineCreationFeedbackEXT feedback;
2203 };
2204
2205 static inline bool
2206 radv_pipeline_has_stage(const struct radv_graphics_pipeline *pipeline, gl_shader_stage stage)
2207 {
2208    return pipeline->base.shaders[stage];
2209 }
2210
2211 bool radv_pipeline_has_ngg_passthrough(const struct radv_graphics_pipeline *pipeline);
2212
2213 bool radv_pipeline_has_gs_copy_shader(const struct radv_pipeline *pipeline);
2214
2215 struct radv_userdata_info *radv_lookup_user_sgpr(struct radv_pipeline *pipeline,
2216                                                  gl_shader_stage stage, int idx);
2217
2218 struct radv_shader *radv_get_shader(const struct radv_pipeline *pipeline, gl_shader_stage stage);
2219
2220 struct radv_graphics_pipeline_create_info {
2221    bool use_rectlist;
2222    bool db_depth_clear;
2223    bool db_stencil_clear;
2224    bool depth_compress_disable;
2225    bool stencil_compress_disable;
2226    bool resummarize_enable;
2227    uint32_t custom_blend_mode;
2228 };
2229
2230 void radv_pipeline_init(struct radv_device *device, struct radv_pipeline *pipeline,
2231                         enum radv_pipeline_type type);
2232
2233 VkResult radv_graphics_pipeline_create(VkDevice device, VkPipelineCache cache,
2234                                        const VkGraphicsPipelineCreateInfo *pCreateInfo,
2235                                        const struct radv_graphics_pipeline_create_info *extra,
2236                                        const VkAllocationCallbacks *alloc, VkPipeline *pPipeline);
2237
2238 VkResult radv_compute_pipeline_create(VkDevice _device, VkPipelineCache _cache,
2239                                       const VkComputePipelineCreateInfo *pCreateInfo,
2240                                       const VkAllocationCallbacks *pAllocator,
2241                                       const uint8_t *custom_hash,
2242                                       struct radv_pipeline_shader_stack_size *rt_stack_sizes,
2243                                       uint32_t rt_group_count, VkPipeline *pPipeline);
2244
2245 void radv_pipeline_destroy(struct radv_device *device, struct radv_pipeline *pipeline,
2246                            const VkAllocationCallbacks *allocator);
2247
2248 struct radv_binning_settings {
2249    unsigned context_states_per_bin;    /* allowed range: [1, 6] */
2250    unsigned persistent_states_per_bin; /* allowed range: [1, 32] */
2251    unsigned fpovs_per_batch;           /* allowed range: [0, 255], 0 = unlimited */
2252 };
2253
2254 struct radv_binning_settings radv_get_binning_settings(const struct radv_physical_device *pdev);
2255
2256 struct vk_format_description;
2257 uint32_t radv_translate_buffer_dataformat(const struct util_format_description *desc,
2258                                           int first_non_void);
2259 uint32_t radv_translate_buffer_numformat(const struct util_format_description *desc,
2260                                          int first_non_void);
2261 bool radv_is_buffer_format_supported(VkFormat format, bool *scaled);
2262 void radv_translate_vertex_format(const struct radv_physical_device *pdevice, VkFormat format,
2263                                   const struct util_format_description *desc, unsigned *dfmt,
2264                                   unsigned *nfmt, bool *post_shuffle,
2265                                   enum radv_vs_input_alpha_adjust *alpha_adjust);
2266 uint32_t radv_translate_colorformat(VkFormat format);
2267 uint32_t radv_translate_color_numformat(VkFormat format, const struct util_format_description *desc,
2268                                         int first_non_void);
2269 uint32_t radv_colorformat_endian_swap(uint32_t colorformat);
2270 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap);
2271 uint32_t radv_translate_dbformat(VkFormat format);
2272 uint32_t radv_translate_tex_dataformat(VkFormat format, const struct util_format_description *desc,
2273                                        int first_non_void);
2274 uint32_t radv_translate_tex_numformat(VkFormat format, const struct util_format_description *desc,
2275                                       int first_non_void);
2276 bool radv_format_pack_clear_color(VkFormat format, uint32_t clear_vals[2],
2277                                   VkClearColorValue *value);
2278 bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
2279                                             VkFormat format);
2280 bool radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdevice,
2281                                           VkFormat format, bool *blendable);
2282 bool radv_dcc_formats_compatible(enum amd_gfx_level gfx_level, VkFormat format1, VkFormat format2,
2283                                  bool *sign_reinterpret);
2284 bool radv_is_atomic_format_supported(VkFormat format);
2285 bool radv_device_supports_etc(struct radv_physical_device *physical_device);
2286
2287 static const VkImageUsageFlags RADV_IMAGE_USAGE_WRITE_BITS =
2288    VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2289    VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
2290
2291 struct radv_image_plane {
2292    VkFormat format;
2293    struct radeon_surf surface;
2294 };
2295
2296 struct radv_image {
2297    struct vk_image vk;
2298
2299    struct ac_surf_info info;
2300
2301    VkDeviceSize size;
2302    uint32_t alignment;
2303
2304    unsigned queue_family_mask;
2305    bool exclusive;
2306    bool shareable;
2307    bool l2_coherent;
2308    bool dcc_sign_reinterpret;
2309    bool support_comp_to_single;
2310
2311    /* Set when bound */
2312    struct radeon_winsys_bo *bo;
2313    VkDeviceSize offset;
2314    bool tc_compatible_cmask;
2315
2316    uint64_t clear_value_offset;
2317    uint64_t fce_pred_offset;
2318    uint64_t dcc_pred_offset;
2319
2320    /*
2321     * Metadata for the TC-compat zrange workaround. If the 32-bit value
2322     * stored at this offset is UINT_MAX, the driver will emit
2323     * DB_Z_INFO.ZRANGE_PRECISION=0, otherwise it will skip the
2324     * SET_CONTEXT_REG packet.
2325     */
2326    uint64_t tc_compat_zrange_offset;
2327
2328    /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
2329    VkDeviceMemory owned_memory;
2330
2331    unsigned plane_count;
2332    struct radv_image_plane planes[0];
2333 };
2334
2335 /* Whether the image has a htile  that is known consistent with the contents of
2336  * the image and is allowed to be in compressed form.
2337  *
2338  * If this is false reads that don't use the htile should be able to return
2339  * correct results.
2340  */
2341 bool radv_layout_is_htile_compressed(const struct radv_device *device,
2342                                      const struct radv_image *image, VkImageLayout layout,
2343                                      bool in_render_loop, unsigned queue_mask);
2344
2345 bool radv_layout_can_fast_clear(const struct radv_device *device, const struct radv_image *image,
2346                                 unsigned level, VkImageLayout layout, bool in_render_loop,
2347                                 unsigned queue_mask);
2348
2349 bool radv_layout_dcc_compressed(const struct radv_device *device, const struct radv_image *image,
2350                                 unsigned level, VkImageLayout layout, bool in_render_loop,
2351                                 unsigned queue_mask);
2352
2353 bool radv_layout_fmask_compressed(const struct radv_device *device, const struct radv_image *image,
2354                                   VkImageLayout layout, unsigned queue_mask);
2355
2356 /**
2357  * Return whether the image has CMASK metadata for color surfaces.
2358  */
2359 static inline bool
2360 radv_image_has_cmask(const struct radv_image *image)
2361 {
2362    return image->planes[0].surface.cmask_offset;
2363 }
2364
2365 /**
2366  * Return whether the image has FMASK metadata for color surfaces.
2367  */
2368 static inline bool
2369 radv_image_has_fmask(const struct radv_image *image)
2370 {
2371    return image->planes[0].surface.fmask_offset;
2372 }
2373
2374 /**
2375  * Return whether the image has DCC metadata for color surfaces.
2376  */
2377 static inline bool
2378 radv_image_has_dcc(const struct radv_image *image)
2379 {
2380    return !(image->planes[0].surface.flags & RADEON_SURF_Z_OR_SBUFFER) &&
2381           image->planes[0].surface.meta_offset;
2382 }
2383
2384 /**
2385  * Return whether the image is TC-compatible CMASK.
2386  */
2387 static inline bool
2388 radv_image_is_tc_compat_cmask(const struct radv_image *image)
2389 {
2390    return radv_image_has_fmask(image) && image->tc_compatible_cmask;
2391 }
2392
2393 /**
2394  * Return whether DCC metadata is enabled for a level.
2395  */
2396 static inline bool
2397 radv_dcc_enabled(const struct radv_image *image, unsigned level)
2398 {
2399    return radv_image_has_dcc(image) && level < image->planes[0].surface.num_meta_levels;
2400 }
2401
2402 /**
2403  * Return whether the image has CB metadata.
2404  */
2405 static inline bool
2406 radv_image_has_CB_metadata(const struct radv_image *image)
2407 {
2408    return radv_image_has_cmask(image) || radv_image_has_fmask(image) || radv_image_has_dcc(image);
2409 }
2410
2411 /**
2412  * Return whether the image has HTILE metadata for depth surfaces.
2413  */
2414 static inline bool
2415 radv_image_has_htile(const struct radv_image *image)
2416 {
2417    return image->planes[0].surface.flags & RADEON_SURF_Z_OR_SBUFFER &&
2418           image->planes[0].surface.meta_size;
2419 }
2420
2421 /**
2422  * Return whether the image has VRS HTILE metadata for depth surfaces
2423  */
2424 static inline bool
2425 radv_image_has_vrs_htile(const struct radv_device *device, const struct radv_image *image)
2426 {
2427    /* Any depth buffer can potentially use VRS. */
2428    return device->attachment_vrs_enabled && radv_image_has_htile(image) &&
2429           (image->vk.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
2430 }
2431
2432 /**
2433  * Return whether HTILE metadata is enabled for a level.
2434  */
2435 static inline bool
2436 radv_htile_enabled(const struct radv_image *image, unsigned level)
2437 {
2438    return radv_image_has_htile(image) && level < image->planes[0].surface.num_meta_levels;
2439 }
2440
2441 /**
2442  * Return whether the image is TC-compatible HTILE.
2443  */
2444 static inline bool
2445 radv_image_is_tc_compat_htile(const struct radv_image *image)
2446 {
2447    return radv_image_has_htile(image) &&
2448           (image->planes[0].surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE);
2449 }
2450
2451 /**
2452  * Return whether the entire HTILE buffer can be used for depth in order to
2453  * improve HiZ Z-Range precision.
2454  */
2455 static inline bool
2456 radv_image_tile_stencil_disabled(const struct radv_device *device, const struct radv_image *image)
2457 {
2458    if (device->physical_device->rad_info.gfx_level >= GFX9) {
2459       return !vk_format_has_stencil(image->vk.format) && !radv_image_has_vrs_htile(device, image);
2460    } else {
2461       /* Due to a hw bug, TILE_STENCIL_DISABLE must be set to 0 for
2462        * the TC-compat ZRANGE issue even if no stencil is used.
2463        */
2464       return !vk_format_has_stencil(image->vk.format) && !radv_image_is_tc_compat_htile(image);
2465    }
2466 }
2467
2468 static inline bool
2469 radv_image_has_clear_value(const struct radv_image *image)
2470 {
2471    return image->clear_value_offset != 0;
2472 }
2473
2474 static inline uint64_t
2475 radv_image_get_fast_clear_va(const struct radv_image *image, uint32_t base_level)
2476 {
2477    assert(radv_image_has_clear_value(image));
2478
2479    uint64_t va = radv_buffer_get_va(image->bo);
2480    va += image->offset + image->clear_value_offset + base_level * 8;
2481    return va;
2482 }
2483
2484 static inline uint64_t
2485 radv_image_get_fce_pred_va(const struct radv_image *image, uint32_t base_level)
2486 {
2487    assert(image->fce_pred_offset != 0);
2488
2489    uint64_t va = radv_buffer_get_va(image->bo);
2490    va += image->offset + image->fce_pred_offset + base_level * 8;
2491    return va;
2492 }
2493
2494 static inline uint64_t
2495 radv_image_get_dcc_pred_va(const struct radv_image *image, uint32_t base_level)
2496 {
2497    assert(image->dcc_pred_offset != 0);
2498
2499    uint64_t va = radv_buffer_get_va(image->bo);
2500    va += image->offset + image->dcc_pred_offset + base_level * 8;
2501    return va;
2502 }
2503
2504 static inline uint64_t
2505 radv_get_tc_compat_zrange_va(const struct radv_image *image, uint32_t base_level)
2506 {
2507    assert(image->tc_compat_zrange_offset != 0);
2508
2509    uint64_t va = radv_buffer_get_va(image->bo);
2510    va += image->offset + image->tc_compat_zrange_offset + base_level * 4;
2511    return va;
2512 }
2513
2514 static inline uint64_t
2515 radv_get_ds_clear_value_va(const struct radv_image *image, uint32_t base_level)
2516 {
2517    assert(radv_image_has_clear_value(image));
2518
2519    uint64_t va = radv_buffer_get_va(image->bo);
2520    va += image->offset + image->clear_value_offset + base_level * 8;
2521    return va;
2522 }
2523
2524 static inline uint32_t
2525 radv_get_htile_initial_value(const struct radv_device *device, const struct radv_image *image)
2526 {
2527    uint32_t initial_value;
2528
2529    if (radv_image_tile_stencil_disabled(device, image)) {
2530       /* Z only (no stencil):
2531        *
2532        * |31     18|17      4|3     0|
2533        * +---------+---------+-------+
2534        * |  Max Z  |  Min Z  | ZMask |
2535        */
2536       initial_value = 0xfffc000f;
2537    } else {
2538       /* Z and stencil:
2539        *
2540        * |31       12|11 10|9    8|7   6|5   4|3     0|
2541        * +-----------+-----+------+-----+-----+-------+
2542        * |  Z Range  |     | SMem | SR1 | SR0 | ZMask |
2543        *
2544        * SR0/SR1 contains the stencil test results. Initializing
2545        * SR0/SR1 to 0x3 means the stencil test result is unknown.
2546        *
2547        * Z, stencil and 4 bit VRS encoding:
2548        * |31       12|11        10|9    8|7          6|5   4|3     0|
2549        * +-----------+------------+------+------------+-----+-------+
2550        * |  Z Range  | VRS y-rate | SMem | VRS x-rate | SR0 | ZMask |
2551        */
2552       if (radv_image_has_vrs_htile(device, image)) {
2553          /* Initialize the VRS x-rate value at 0, so the hw interprets it as 1 sample. */
2554          initial_value = 0xfffff33f;
2555       } else {
2556          initial_value = 0xfffff3ff;
2557       }
2558    }
2559
2560    return initial_value;
2561 }
2562
2563 static inline bool
2564 radv_image_get_iterate256(struct radv_device *device, struct radv_image *image)
2565 {
2566    /* ITERATE_256 is required for depth or stencil MSAA images that are TC-compatible HTILE. */
2567    return device->physical_device->rad_info.gfx_level >= GFX10 &&
2568           (image->vk.usage &
2569            (VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) &&
2570           radv_image_is_tc_compat_htile(image) && image->info.samples > 1;
2571 }
2572
2573 unsigned radv_image_queue_family_mask(const struct radv_image *image,
2574                                       enum radv_queue_family family,
2575                                       enum radv_queue_family queue_family);
2576
2577 static inline uint32_t
2578 radv_get_layerCount(const struct radv_image *image, const VkImageSubresourceRange *range)
2579 {
2580    return range->layerCount == VK_REMAINING_ARRAY_LAYERS
2581              ? image->info.array_size - range->baseArrayLayer
2582              : range->layerCount;
2583 }
2584
2585 static inline uint32_t
2586 radv_get_levelCount(const struct radv_image *image, const VkImageSubresourceRange *range)
2587 {
2588    return range->levelCount == VK_REMAINING_MIP_LEVELS ? image->info.levels - range->baseMipLevel
2589                                                        : range->levelCount;
2590 }
2591
2592 bool radv_image_is_renderable(struct radv_device *device, struct radv_image *image);
2593
2594 struct radeon_bo_metadata;
2595 void radv_init_metadata(struct radv_device *device, struct radv_image *image,
2596                         struct radeon_bo_metadata *metadata);
2597
2598 void radv_image_override_offset_stride(struct radv_device *device, struct radv_image *image,
2599                                        uint64_t offset, uint32_t stride);
2600
2601 union radv_descriptor {
2602    struct {
2603       uint32_t plane0_descriptor[8];
2604       uint32_t fmask_descriptor[8];
2605    };
2606    struct {
2607       uint32_t plane_descriptors[3][8];
2608    };
2609 };
2610
2611 struct radv_image_view {
2612    struct vk_image_view vk;
2613    struct radv_image *image; /**< VkImageViewCreateInfo::image */
2614
2615    unsigned plane_id;
2616    VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
2617
2618    /* Whether the image iview supports fast clear. */
2619    bool support_fast_clear;
2620
2621    bool disable_dcc_mrt;
2622
2623    union radv_descriptor descriptor;
2624
2625    /* Descriptor for use as a storage image as opposed to a sampled image.
2626     * This has a few differences for cube maps (e.g. type).
2627     */
2628    union radv_descriptor storage_descriptor;
2629 };
2630
2631 struct radv_image_create_info {
2632    const VkImageCreateInfo *vk_info;
2633    bool scanout;
2634    bool no_metadata_planes;
2635    bool prime_blit_src;
2636    const struct radeon_bo_metadata *bo_metadata;
2637 };
2638
2639 VkResult
2640 radv_image_create_layout(struct radv_device *device, struct radv_image_create_info create_info,
2641                          const struct VkImageDrmFormatModifierExplicitCreateInfoEXT *mod_info,
2642                          struct radv_image *image);
2643
2644 VkResult radv_image_create(VkDevice _device, const struct radv_image_create_info *info,
2645                            const VkAllocationCallbacks *alloc, VkImage *pImage);
2646
2647 bool radv_are_formats_dcc_compatible(const struct radv_physical_device *pdev, const void *pNext,
2648                                      VkFormat format, VkImageCreateFlags flags,
2649                                      bool *sign_reinterpret);
2650
2651 bool vi_alpha_is_on_msb(struct radv_device *device, VkFormat format);
2652
2653 VkResult radv_image_from_gralloc(VkDevice device_h, const VkImageCreateInfo *base_info,
2654                                  const VkNativeBufferANDROID *gralloc_info,
2655                                  const VkAllocationCallbacks *alloc, VkImage *out_image_h);
2656 uint64_t radv_ahb_usage_from_vk_usage(const VkImageCreateFlags vk_create,
2657                                       const VkImageUsageFlags vk_usage);
2658 VkResult radv_import_ahb_memory(struct radv_device *device, struct radv_device_memory *mem,
2659                                 unsigned priority,
2660                                 const VkImportAndroidHardwareBufferInfoANDROID *info);
2661 VkResult radv_create_ahb_memory(struct radv_device *device, struct radv_device_memory *mem,
2662                                 unsigned priority, const VkMemoryAllocateInfo *pAllocateInfo);
2663
2664 VkFormat radv_select_android_external_format(const void *next, VkFormat default_format);
2665
2666 bool radv_android_gralloc_supports_format(VkFormat format, VkImageUsageFlagBits usage);
2667
2668 struct radv_image_view_extra_create_info {
2669    bool disable_compression;
2670    bool enable_compression;
2671    bool disable_dcc_mrt;
2672    bool from_client; /**< Set only if this came from vkCreateImage */
2673 };
2674
2675 void radv_image_view_init(struct radv_image_view *view, struct radv_device *device,
2676                           const VkImageViewCreateInfo *pCreateInfo,
2677                           VkImageCreateFlags img_create_flags,
2678                           const struct radv_image_view_extra_create_info *extra_create_info);
2679 void radv_image_view_finish(struct radv_image_view *iview);
2680
2681 VkFormat radv_get_aspect_format(struct radv_image *image, VkImageAspectFlags mask);
2682
2683 struct radv_sampler_ycbcr_conversion_state {
2684    VkFormat format;
2685    VkSamplerYcbcrModelConversion ycbcr_model;
2686    VkSamplerYcbcrRange ycbcr_range;
2687    VkComponentMapping components;
2688    VkChromaLocation chroma_offsets[2];
2689    VkFilter chroma_filter;
2690 };
2691
2692 struct radv_sampler_ycbcr_conversion {
2693    struct vk_object_base base;
2694    /* The state is hashed for the descriptor set layout. */
2695    struct radv_sampler_ycbcr_conversion_state state;
2696 };
2697
2698 struct radv_buffer_view {
2699    struct vk_object_base base;
2700    struct radeon_winsys_bo *bo;
2701    VkFormat vk_format;
2702    uint64_t range; /**< VkBufferViewCreateInfo::range */
2703    uint32_t state[4];
2704 };
2705 void radv_buffer_view_init(struct radv_buffer_view *view, struct radv_device *device,
2706                            const VkBufferViewCreateInfo *pCreateInfo);
2707 void radv_buffer_view_finish(struct radv_buffer_view *view);
2708
2709 static inline bool
2710 radv_image_extent_compare(const struct radv_image *image, const VkExtent3D *extent)
2711 {
2712    if (extent->width != image->info.width || extent->height != image->info.height ||
2713        extent->depth != image->info.depth)
2714       return false;
2715    return true;
2716 }
2717
2718 struct radv_sampler {
2719    struct vk_object_base base;
2720    uint32_t state[4];
2721    struct radv_sampler_ycbcr_conversion *ycbcr_sampler;
2722    uint32_t border_color_slot;
2723 };
2724
2725 struct radv_subpass_barrier {
2726    VkPipelineStageFlags2 src_stage_mask;
2727    VkAccessFlags2 src_access_mask;
2728    VkAccessFlags2 dst_access_mask;
2729 };
2730
2731 void radv_emit_subpass_barrier(struct radv_cmd_buffer *cmd_buffer,
2732                                const struct radv_subpass_barrier *barrier);
2733
2734 struct radv_subpass_attachment {
2735    uint32_t attachment;
2736    VkImageLayout layout;
2737    VkImageLayout stencil_layout;
2738    bool in_render_loop;
2739 };
2740
2741 struct radv_subpass {
2742    uint32_t attachment_count;
2743    struct radv_subpass_attachment *attachments;
2744
2745    uint32_t input_count;
2746    uint32_t color_count;
2747    struct radv_subpass_attachment *input_attachments;
2748    struct radv_subpass_attachment *color_attachments;
2749    struct radv_subpass_attachment *resolve_attachments;
2750    struct radv_subpass_attachment *depth_stencil_attachment;
2751    struct radv_subpass_attachment *ds_resolve_attachment;
2752    struct radv_subpass_attachment *vrs_attachment;
2753    VkResolveModeFlagBits depth_resolve_mode;
2754    VkResolveModeFlagBits stencil_resolve_mode;
2755
2756    /** Subpass has at least one color resolve attachment */
2757    bool has_color_resolve;
2758
2759    struct radv_subpass_barrier start_barrier;
2760
2761    uint32_t view_mask;
2762
2763    VkSampleCountFlagBits color_sample_count;
2764    VkSampleCountFlagBits depth_sample_count;
2765    VkSampleCountFlagBits max_sample_count;
2766
2767    /* Whether the subpass has ingoing/outgoing external dependencies. */
2768    bool has_ingoing_dep;
2769    bool has_outgoing_dep;
2770 };
2771
2772 uint32_t radv_get_subpass_id(struct radv_cmd_buffer *cmd_buffer);
2773
2774 struct radv_render_pass_attachment {
2775    VkFormat format;
2776    uint32_t samples;
2777    VkAttachmentLoadOp load_op;
2778    VkAttachmentLoadOp stencil_load_op;
2779    VkImageLayout initial_layout;
2780    VkImageLayout final_layout;
2781    VkImageLayout stencil_initial_layout;
2782    VkImageLayout stencil_final_layout;
2783
2784    /* The subpass id in which the attachment will be used first/last. */
2785    uint32_t first_subpass_idx;
2786    uint32_t last_subpass_idx;
2787 };
2788
2789 struct radv_render_pass {
2790    struct vk_object_base base;
2791    uint32_t attachment_count;
2792    uint32_t subpass_count;
2793    struct radv_subpass_attachment *subpass_attachments;
2794    struct radv_render_pass_attachment *attachments;
2795    struct radv_subpass_barrier end_barrier;
2796    struct radv_subpass subpasses[0];
2797 };
2798
2799 VkResult radv_device_init_meta(struct radv_device *device);
2800 void radv_device_finish_meta(struct radv_device *device);
2801
2802 struct radv_query_pool {
2803    struct vk_object_base base;
2804    struct radeon_winsys_bo *bo;
2805    uint32_t stride;
2806    uint32_t availability_offset;
2807    uint64_t size;
2808    char *ptr;
2809    VkQueryType type;
2810    uint32_t pipeline_stats_mask;
2811    bool uses_gds; /* For NGG GS on GFX10+ */
2812 };
2813
2814 bool radv_queue_internal_submit(struct radv_queue *queue, struct radeon_cmdbuf *cs);
2815
2816 int radv_queue_init(struct radv_device *device, struct radv_queue *queue, int idx,
2817                     const VkDeviceQueueCreateInfo *create_info,
2818                     const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority);
2819
2820 void radv_set_descriptor_set(struct radv_cmd_buffer *cmd_buffer, VkPipelineBindPoint bind_point,
2821                              struct radv_descriptor_set *set, unsigned idx);
2822
2823 void radv_cmd_update_descriptor_sets(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer,
2824                                      VkDescriptorSet overrideSet, uint32_t descriptorWriteCount,
2825                                      const VkWriteDescriptorSet *pDescriptorWrites,
2826                                      uint32_t descriptorCopyCount,
2827                                      const VkCopyDescriptorSet *pDescriptorCopies);
2828
2829 void radv_cmd_update_descriptor_set_with_template(struct radv_device *device,
2830                                                   struct radv_cmd_buffer *cmd_buffer,
2831                                                   struct radv_descriptor_set *set,
2832                                                   VkDescriptorUpdateTemplate descriptorUpdateTemplate,
2833                                                   const void *pData);
2834
2835 void radv_meta_push_descriptor_set(struct radv_cmd_buffer *cmd_buffer,
2836                                    VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout _layout,
2837                                    uint32_t set, uint32_t descriptorWriteCount,
2838                                    const VkWriteDescriptorSet *pDescriptorWrites);
2839
2840 uint32_t radv_init_dcc(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
2841                        const VkImageSubresourceRange *range, uint32_t value);
2842
2843 uint32_t radv_init_fmask(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
2844                          const VkImageSubresourceRange *range);
2845
2846 /* radv_nir_to_llvm.c */
2847 struct radv_shader_args;
2848 struct radv_nir_compiler_options;
2849 struct radv_shader_info;
2850
2851 void llvm_compile_shader(const struct radv_nir_compiler_options *options,
2852                          const struct radv_shader_info *info, unsigned shader_count,
2853                          struct nir_shader *const *shaders, struct radv_shader_binary **binary,
2854                          const struct radv_shader_args *args);
2855
2856 /* radv_shader_info.h */
2857 struct radv_shader_info;
2858
2859 void radv_nir_shader_info_pass(struct radv_device *device, const struct nir_shader *nir,
2860                                const struct radv_pipeline_layout *layout,
2861                                const struct radv_pipeline_key *pipeline_key,
2862                                struct radv_shader_info *info);
2863
2864 void radv_nir_shader_info_init(struct radv_shader_info *info);
2865
2866 bool radv_thread_trace_init(struct radv_device *device);
2867 void radv_thread_trace_finish(struct radv_device *device);
2868 bool radv_begin_thread_trace(struct radv_queue *queue);
2869 bool radv_end_thread_trace(struct radv_queue *queue);
2870 bool radv_get_thread_trace(struct radv_queue *queue, struct ac_thread_trace *thread_trace);
2871 void radv_emit_thread_trace_userdata(struct radv_cmd_buffer *cmd_buffer, const void *data,
2872                                      uint32_t num_dwords);
2873 bool radv_is_instruction_timing_enabled(void);
2874
2875 void radv_emit_inhibit_clockgating(struct radv_device *device, struct radeon_cmdbuf *cs,
2876                                    bool inhibit);
2877
2878 bool radv_sdma_copy_image(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
2879                           struct radv_buffer *buffer, const VkBufferImageCopy2 *region);
2880
2881 /* radv_sqtt_layer_.c */
2882 struct radv_barrier_data {
2883    union {
2884       struct {
2885          uint16_t depth_stencil_expand : 1;
2886          uint16_t htile_hiz_range_expand : 1;
2887          uint16_t depth_stencil_resummarize : 1;
2888          uint16_t dcc_decompress : 1;
2889          uint16_t fmask_decompress : 1;
2890          uint16_t fast_clear_eliminate : 1;
2891          uint16_t fmask_color_expand : 1;
2892          uint16_t init_mask_ram : 1;
2893          uint16_t reserved : 8;
2894       };
2895       uint16_t all;
2896    } layout_transitions;
2897 };
2898
2899 /**
2900  * Value for the reason field of an RGP barrier start marker originating from
2901  * the Vulkan client (does not include PAL-defined values). (Table 15)
2902  */
2903 enum rgp_barrier_reason {
2904    RGP_BARRIER_UNKNOWN_REASON = 0xFFFFFFFF,
2905
2906    /* External app-generated barrier reasons, i.e. API synchronization
2907     * commands Range of valid values: [0x00000001 ... 0x7FFFFFFF].
2908     */
2909    RGP_BARRIER_EXTERNAL_CMD_PIPELINE_BARRIER = 0x00000001,
2910    RGP_BARRIER_EXTERNAL_RENDER_PASS_SYNC = 0x00000002,
2911    RGP_BARRIER_EXTERNAL_CMD_WAIT_EVENTS = 0x00000003,
2912
2913    /* Internal barrier reasons, i.e. implicit synchronization inserted by
2914     * the Vulkan driver Range of valid values: [0xC0000000 ... 0xFFFFFFFE].
2915     */
2916    RGP_BARRIER_INTERNAL_BASE = 0xC0000000,
2917    RGP_BARRIER_INTERNAL_PRE_RESET_QUERY_POOL_SYNC = RGP_BARRIER_INTERNAL_BASE + 0,
2918    RGP_BARRIER_INTERNAL_POST_RESET_QUERY_POOL_SYNC = RGP_BARRIER_INTERNAL_BASE + 1,
2919    RGP_BARRIER_INTERNAL_GPU_EVENT_RECYCLE_STALL = RGP_BARRIER_INTERNAL_BASE + 2,
2920    RGP_BARRIER_INTERNAL_PRE_COPY_QUERY_POOL_RESULTS_SYNC = RGP_BARRIER_INTERNAL_BASE + 3
2921 };
2922
2923 void radv_describe_begin_cmd_buffer(struct radv_cmd_buffer *cmd_buffer);
2924 void radv_describe_end_cmd_buffer(struct radv_cmd_buffer *cmd_buffer);
2925 void radv_describe_draw(struct radv_cmd_buffer *cmd_buffer);
2926 void radv_describe_dispatch(struct radv_cmd_buffer *cmd_buffer, int x, int y, int z);
2927 void radv_describe_begin_render_pass_clear(struct radv_cmd_buffer *cmd_buffer,
2928                                            VkImageAspectFlagBits aspects);
2929 void radv_describe_end_render_pass_clear(struct radv_cmd_buffer *cmd_buffer);
2930 void radv_describe_begin_render_pass_resolve(struct radv_cmd_buffer *cmd_buffer);
2931 void radv_describe_end_render_pass_resolve(struct radv_cmd_buffer *cmd_buffer);
2932 void radv_describe_barrier_start(struct radv_cmd_buffer *cmd_buffer,
2933                                  enum rgp_barrier_reason reason);
2934 void radv_describe_barrier_end(struct radv_cmd_buffer *cmd_buffer);
2935 void radv_describe_barrier_end_delayed(struct radv_cmd_buffer *cmd_buffer);
2936 void radv_describe_layout_transition(struct radv_cmd_buffer *cmd_buffer,
2937                                      const struct radv_barrier_data *barrier);
2938
2939 uint64_t radv_get_current_time(void);
2940
2941 static inline uint32_t
2942 si_conv_gl_prim_to_vertices(enum shader_prim gl_prim)
2943 {
2944    switch (gl_prim) {
2945    case SHADER_PRIM_POINTS:
2946       return 1;
2947    case SHADER_PRIM_LINES:
2948    case SHADER_PRIM_LINE_STRIP:
2949       return 2;
2950    case SHADER_PRIM_TRIANGLES:
2951    case SHADER_PRIM_TRIANGLE_STRIP:
2952       return 3;
2953    case SHADER_PRIM_LINES_ADJACENCY:
2954       return 4;
2955    case SHADER_PRIM_TRIANGLES_ADJACENCY:
2956       return 6;
2957    case SHADER_PRIM_QUADS:
2958       return V_028A6C_TRISTRIP;
2959    default:
2960       assert(0);
2961       return 0;
2962    }
2963 }
2964
2965 static inline uint32_t
2966 si_conv_prim_to_gs_out(uint32_t topology)
2967 {
2968    switch (topology) {
2969    case V_008958_DI_PT_POINTLIST:
2970    case V_008958_DI_PT_PATCH:
2971       return V_028A6C_POINTLIST;
2972    case V_008958_DI_PT_LINELIST:
2973    case V_008958_DI_PT_LINESTRIP:
2974    case V_008958_DI_PT_LINELIST_ADJ:
2975    case V_008958_DI_PT_LINESTRIP_ADJ:
2976       return V_028A6C_LINESTRIP;
2977    case V_008958_DI_PT_TRILIST:
2978    case V_008958_DI_PT_TRISTRIP:
2979    case V_008958_DI_PT_TRIFAN:
2980    case V_008958_DI_PT_TRILIST_ADJ:
2981    case V_008958_DI_PT_TRISTRIP_ADJ:
2982       return V_028A6C_TRISTRIP;
2983    default:
2984       assert(0);
2985       return 0;
2986    }
2987 }
2988
2989 static inline uint32_t
2990 si_translate_prim(unsigned topology)
2991 {
2992    switch (topology) {
2993    case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
2994       return V_008958_DI_PT_POINTLIST;
2995    case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
2996       return V_008958_DI_PT_LINELIST;
2997    case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
2998       return V_008958_DI_PT_LINESTRIP;
2999    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
3000       return V_008958_DI_PT_TRILIST;
3001    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
3002       return V_008958_DI_PT_TRISTRIP;
3003    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
3004       return V_008958_DI_PT_TRIFAN;
3005    case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
3006       return V_008958_DI_PT_LINELIST_ADJ;
3007    case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
3008       return V_008958_DI_PT_LINESTRIP_ADJ;
3009    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
3010       return V_008958_DI_PT_TRILIST_ADJ;
3011    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
3012       return V_008958_DI_PT_TRISTRIP_ADJ;
3013    case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
3014       return V_008958_DI_PT_PATCH;
3015    default:
3016       unreachable("unhandled primitive type");
3017    }
3018 }
3019
3020 static inline uint32_t
3021 si_translate_stencil_op(enum VkStencilOp op)
3022 {
3023    switch (op) {
3024    case VK_STENCIL_OP_KEEP:
3025       return V_02842C_STENCIL_KEEP;
3026    case VK_STENCIL_OP_ZERO:
3027       return V_02842C_STENCIL_ZERO;
3028    case VK_STENCIL_OP_REPLACE:
3029       return V_02842C_STENCIL_REPLACE_TEST;
3030    case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
3031       return V_02842C_STENCIL_ADD_CLAMP;
3032    case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
3033       return V_02842C_STENCIL_SUB_CLAMP;
3034    case VK_STENCIL_OP_INVERT:
3035       return V_02842C_STENCIL_INVERT;
3036    case VK_STENCIL_OP_INCREMENT_AND_WRAP:
3037       return V_02842C_STENCIL_ADD_WRAP;
3038    case VK_STENCIL_OP_DECREMENT_AND_WRAP:
3039       return V_02842C_STENCIL_SUB_WRAP;
3040    default:
3041       return 0;
3042    }
3043 }
3044
3045 static inline uint32_t
3046 si_translate_blend_logic_op(VkLogicOp op)
3047 {
3048    switch (op) {
3049    case VK_LOGIC_OP_CLEAR:
3050       return V_028808_ROP3_CLEAR;
3051    case VK_LOGIC_OP_AND:
3052       return V_028808_ROP3_AND;
3053    case VK_LOGIC_OP_AND_REVERSE:
3054       return V_028808_ROP3_AND_REVERSE;
3055    case VK_LOGIC_OP_COPY:
3056       return V_028808_ROP3_COPY;
3057    case VK_LOGIC_OP_AND_INVERTED:
3058       return V_028808_ROP3_AND_INVERTED;
3059    case VK_LOGIC_OP_NO_OP:
3060       return V_028808_ROP3_NO_OP;
3061    case VK_LOGIC_OP_XOR:
3062       return V_028808_ROP3_XOR;
3063    case VK_LOGIC_OP_OR:
3064       return V_028808_ROP3_OR;
3065    case VK_LOGIC_OP_NOR:
3066       return V_028808_ROP3_NOR;
3067    case VK_LOGIC_OP_EQUIVALENT:
3068       return V_028808_ROP3_EQUIVALENT;
3069    case VK_LOGIC_OP_INVERT:
3070       return V_028808_ROP3_INVERT;
3071    case VK_LOGIC_OP_OR_REVERSE:
3072       return V_028808_ROP3_OR_REVERSE;
3073    case VK_LOGIC_OP_COPY_INVERTED:
3074       return V_028808_ROP3_COPY_INVERTED;
3075    case VK_LOGIC_OP_OR_INVERTED:
3076       return V_028808_ROP3_OR_INVERTED;
3077    case VK_LOGIC_OP_NAND:
3078       return V_028808_ROP3_NAND;
3079    case VK_LOGIC_OP_SET:
3080       return V_028808_ROP3_SET;
3081    default:
3082       unreachable("Unhandled logic op");
3083    }
3084 }
3085
3086 /*
3087  * Queue helper to get ring.
3088  * placed here as it needs queue + device structs.
3089  */
3090 static inline enum amd_ip_type
3091 radv_queue_ring(struct radv_queue *queue)
3092 {
3093    return radv_queue_family_to_ring(queue->device->physical_device, queue->state.qf);
3094 }
3095
3096 /**
3097  * Helper used for debugging compiler issues by enabling/disabling LLVM for a
3098  * specific shader stage (developers only).
3099  */
3100 static inline bool
3101 radv_use_llvm_for_stage(struct radv_device *device, UNUSED gl_shader_stage stage)
3102 {
3103    return device->physical_device->use_llvm;
3104 }
3105
3106 static inline bool
3107 radv_has_shader_buffer_float_minmax(const struct radv_physical_device *pdevice)
3108 {
3109    return (pdevice->rad_info.gfx_level <= GFX7 && !pdevice->use_llvm) ||
3110           pdevice->rad_info.gfx_level >= GFX10;
3111 }
3112
3113 struct radv_acceleration_structure {
3114    struct vk_object_base base;
3115
3116    struct radeon_winsys_bo *bo;
3117    uint64_t mem_offset;
3118    uint64_t size;
3119 };
3120
3121 static inline uint64_t
3122 radv_accel_struct_get_va(const struct radv_acceleration_structure *accel)
3123 {
3124    return radv_buffer_get_va(accel->bo) + accel->mem_offset;
3125 }
3126
3127 /* radv_perfcounter.c */
3128 void radv_perfcounter_emit_shaders(struct radeon_cmdbuf *cs, unsigned shaders);
3129 void radv_perfcounter_emit_spm_reset(struct radeon_cmdbuf *cs);
3130 void radv_perfcounter_emit_spm_start(struct radv_device *device, struct radeon_cmdbuf *cs,
3131                                      int family);
3132 void radv_perfcounter_emit_spm_stop(struct radv_device *device, struct radeon_cmdbuf *cs,
3133                                     int family);
3134
3135 /* radv_spm.c */
3136 bool radv_spm_init(struct radv_device *device);
3137 void radv_spm_finish(struct radv_device *device);
3138 void radv_emit_spm_setup(struct radv_device *device, struct radeon_cmdbuf *cs);
3139
3140 #define RADV_FROM_HANDLE(__radv_type, __name, __handle) \
3141    VK_FROM_HANDLE(__radv_type, __name, __handle)
3142
3143 VK_DEFINE_HANDLE_CASTS(radv_cmd_buffer, vk.base, VkCommandBuffer,
3144                        VK_OBJECT_TYPE_COMMAND_BUFFER)
3145 VK_DEFINE_HANDLE_CASTS(radv_device, vk.base, VkDevice, VK_OBJECT_TYPE_DEVICE)
3146 VK_DEFINE_HANDLE_CASTS(radv_instance, vk.base, VkInstance, VK_OBJECT_TYPE_INSTANCE)
3147 VK_DEFINE_HANDLE_CASTS(radv_physical_device, vk.base, VkPhysicalDevice,
3148                        VK_OBJECT_TYPE_PHYSICAL_DEVICE)
3149 VK_DEFINE_HANDLE_CASTS(radv_queue, vk.base, VkQueue, VK_OBJECT_TYPE_QUEUE)
3150 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_acceleration_structure, base,
3151                                VkAccelerationStructureKHR,
3152                                VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR)
3153 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_cmd_pool, vk.base, VkCommandPool,
3154                                VK_OBJECT_TYPE_COMMAND_POOL)
3155 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_buffer, vk.base, VkBuffer, VK_OBJECT_TYPE_BUFFER)
3156 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_buffer_view, base, VkBufferView,
3157                                VK_OBJECT_TYPE_BUFFER_VIEW)
3158 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_pool, base, VkDescriptorPool,
3159                                VK_OBJECT_TYPE_DESCRIPTOR_POOL)
3160 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_set, header.base, VkDescriptorSet,
3161                                VK_OBJECT_TYPE_DESCRIPTOR_SET)
3162 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_set_layout, base,
3163                                VkDescriptorSetLayout,
3164                                VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT)
3165 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_update_template, base,
3166                                VkDescriptorUpdateTemplate,
3167                                VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE)
3168 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_device_memory, base, VkDeviceMemory,
3169                                VK_OBJECT_TYPE_DEVICE_MEMORY)
3170 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_event, base, VkEvent, VK_OBJECT_TYPE_EVENT)
3171 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_image, vk.base, VkImage, VK_OBJECT_TYPE_IMAGE)
3172 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_image_view, vk.base, VkImageView,
3173                                VK_OBJECT_TYPE_IMAGE_VIEW);
3174 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline_cache, base, VkPipelineCache,
3175                                VK_OBJECT_TYPE_PIPELINE_CACHE)
3176 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline, base, VkPipeline,
3177                                VK_OBJECT_TYPE_PIPELINE)
3178 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline_layout, base, VkPipelineLayout,
3179                                VK_OBJECT_TYPE_PIPELINE_LAYOUT)
3180 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_query_pool, base, VkQueryPool,
3181                                VK_OBJECT_TYPE_QUERY_POOL)
3182 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_render_pass, base, VkRenderPass,
3183                                VK_OBJECT_TYPE_RENDER_PASS)
3184 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_sampler, base, VkSampler,
3185                                VK_OBJECT_TYPE_SAMPLER)
3186 VK_DEFINE_NONDISP_HANDLE_CASTS(radv_sampler_ycbcr_conversion, base,
3187                                VkSamplerYcbcrConversion,
3188                                VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION)
3189
3190 #ifdef __cplusplus
3191 }
3192 #endif
3193
3194 #endif /* RADV_PRIVATE_H */