• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdlib.h>
32 #include <stdio.h>
33 #include <stdbool.h>
34 #include <pthread.h>
35 #include <assert.h>
36 #include <stdint.h>
37 #include <string.h>
38 #ifdef HAVE_VALGRIND
39 #include <valgrind.h>
40 #include <memcheck.h>
41 #define VG(x) x
42 #else
43 #define VG(x) ((void)0)
44 #endif
45 
46 #include "c11/threads.h"
47 #include <amdgpu.h>
48 #include "compiler/shader_enums.h"
49 #include "util/macros.h"
50 #include "util/list.h"
51 #include "util/rwlock.h"
52 #include "util/xmlconfig.h"
53 #include "vk_alloc.h"
54 #include "vk_debug_report.h"
55 #include "vk_object.h"
56 
57 #include "radv_radeon_winsys.h"
58 #include "ac_binary.h"
59 #include "ac_nir_to_llvm.h"
60 #include "ac_gpu_info.h"
61 #include "ac_surface.h"
62 #include "ac_llvm_build.h"
63 #include "ac_llvm_util.h"
64 #include "radv_constants.h"
65 #include "radv_descriptor_set.h"
66 #include "radv_extensions.h"
67 #include "sid.h"
68 
69 /* Pre-declarations needed for WSI entrypoints */
70 struct wl_surface;
71 struct wl_display;
72 typedef struct xcb_connection_t xcb_connection_t;
73 typedef uint32_t xcb_visualid_t;
74 typedef uint32_t xcb_window_t;
75 
76 #include <vulkan/vulkan.h>
77 #include <vulkan/vulkan_intel.h>
78 #include <vulkan/vulkan_android.h>
79 #include <vulkan/vk_icd.h>
80 #include <vulkan/vk_android_native_buffer.h>
81 
82 #include "radv_entrypoints.h"
83 
84 #include "wsi_common.h"
85 #include "wsi_common_display.h"
86 
87 /* Helper to determine if we should compile
88  * any of the Android AHB support.
89  *
90  * To actually enable the ext we also need
91  * the necessary kernel support.
92  */
93 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
94 #define RADV_SUPPORT_ANDROID_HARDWARE_BUFFER 1
95 #else
96 #define RADV_SUPPORT_ANDROID_HARDWARE_BUFFER 0
97 #endif
98 
99 #define radv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
100 
101 static inline uint32_t
align_u32(uint32_t v,uint32_t a)102 align_u32(uint32_t v, uint32_t a)
103 {
104 	assert(a != 0 && a == (a & -a));
105 	return (v + a - 1) & ~(a - 1);
106 }
107 
108 static inline uint32_t
align_u32_npot(uint32_t v,uint32_t a)109 align_u32_npot(uint32_t v, uint32_t a)
110 {
111 	return (v + a - 1) / a * a;
112 }
113 
114 static inline uint64_t
align_u64(uint64_t v,uint64_t a)115 align_u64(uint64_t v, uint64_t a)
116 {
117 	assert(a != 0 && a == (a & -a));
118 	return (v + a - 1) & ~(a - 1);
119 }
120 
121 static inline int32_t
align_i32(int32_t v,int32_t a)122 align_i32(int32_t v, int32_t a)
123 {
124 	assert(a != 0 && a == (a & -a));
125 	return (v + a - 1) & ~(a - 1);
126 }
127 
128 /** Alignment must be a power of 2. */
129 static inline bool
radv_is_aligned(uintmax_t n,uintmax_t a)130 radv_is_aligned(uintmax_t n, uintmax_t a)
131 {
132 	assert(a == (a & -a));
133 	return (n & (a - 1)) == 0;
134 }
135 
136 static inline uint32_t
round_up_u32(uint32_t v,uint32_t a)137 round_up_u32(uint32_t v, uint32_t a)
138 {
139 	return (v + a - 1) / a;
140 }
141 
142 static inline uint64_t
round_up_u64(uint64_t v,uint64_t a)143 round_up_u64(uint64_t v, uint64_t a)
144 {
145 	return (v + a - 1) / a;
146 }
147 
148 static inline uint32_t
radv_minify(uint32_t n,uint32_t levels)149 radv_minify(uint32_t n, uint32_t levels)
150 {
151 	if (unlikely(n == 0))
152 		return 0;
153 	else
154 		return MAX2(n >> levels, 1);
155 }
156 static inline float
radv_clamp_f(float f,float min,float max)157 radv_clamp_f(float f, float min, float max)
158 {
159 	assert(min < max);
160 
161 	if (f > max)
162 		return max;
163 	else if (f < min)
164 		return min;
165 	else
166 		return f;
167 }
168 
169 static inline bool
radv_clear_mask(uint32_t * inout_mask,uint32_t clear_mask)170 radv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
171 {
172 	if (*inout_mask & clear_mask) {
173 		*inout_mask &= ~clear_mask;
174 		return true;
175 	} else {
176 		return false;
177 	}
178 }
179 
180 #define for_each_bit(b, dword)                          \
181 	for (uint32_t __dword = (dword);		\
182 	     (b) = __builtin_ffs(__dword) - 1, __dword;	\
183 	     __dword &= ~(1 << (b)))
184 
185 #define typed_memcpy(dest, src, count) ({				\
186 			STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
187 			memcpy((dest), (src), (count) * sizeof(*(src))); \
188 		})
189 
190 /* Whenever we generate an error, pass it through this function. Useful for
191  * debugging, where we can break on it. Only call at error site, not when
192  * propagating errors. Might be useful to plug in a stack trace here.
193  */
194 
195 struct radv_image_view;
196 struct radv_instance;
197 
198 VkResult __vk_errorv(struct radv_instance *instance, const void *object,
199 		     VkDebugReportObjectTypeEXT type, VkResult error,
200 		     const char *file, int line, const char *format,
201 		     va_list args);
202 
203 VkResult __vk_errorf(struct radv_instance *instance, const void *object,
204 		     VkDebugReportObjectTypeEXT type, VkResult error,
205 		     const char *file, int line, const char *format, ...)
206 	radv_printflike(7, 8);
207 
208 #define vk_error(instance, error) \
209 	__vk_errorf(instance, NULL, \
210 		    VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, \
211 		    error, __FILE__, __LINE__, NULL);
212 #define vk_errorf(instance, error, format, ...) \
213 	__vk_errorf(instance, NULL, \
214 		    VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, \
215 		    error, __FILE__, __LINE__, format, ## __VA_ARGS__);
216 
217 void __radv_finishme(const char *file, int line, const char *format, ...)
218 	radv_printflike(3, 4);
219 void radv_loge(const char *format, ...) radv_printflike(1, 2);
220 void radv_loge_v(const char *format, va_list va);
221 void radv_logi(const char *format, ...) radv_printflike(1, 2);
222 void radv_logi_v(const char *format, va_list va);
223 
224 /**
225  * Print a FINISHME message, including its source location.
226  */
227 #define radv_finishme(format, ...)					\
228 	do { \
229 		static bool reported = false; \
230 		if (!reported) { \
231 			__radv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
232 			reported = true; \
233 		} \
234 	} while (0)
235 
236 /* A non-fatal assert.  Useful for debugging. */
237 #ifdef DEBUG
238 #define radv_assert(x) ({						\
239 			if (unlikely(!(x)))				\
240 				fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
241 		})
242 #else
243 #define radv_assert(x) do {} while(0)
244 #endif
245 
246 #define stub_return(v)					\
247 	do {						\
248 		radv_finishme("stub %s", __func__);	\
249 		return (v);				\
250 	} while (0)
251 
252 #define stub()						\
253 	do {						\
254 		radv_finishme("stub %s", __func__);	\
255 		return;					\
256 	} while (0)
257 
258 int radv_get_instance_entrypoint_index(const char *name);
259 int radv_get_device_entrypoint_index(const char *name);
260 int radv_get_physical_device_entrypoint_index(const char *name);
261 
262 const char *radv_get_instance_entry_name(int index);
263 const char *radv_get_physical_device_entry_name(int index);
264 const char *radv_get_device_entry_name(int index);
265 
266 bool radv_instance_entrypoint_is_enabled(int index, uint32_t core_version,
267 					 const struct radv_instance_extension_table *instance);
268 bool radv_physical_device_entrypoint_is_enabled(int index, uint32_t core_version,
269 						const struct radv_instance_extension_table *instance);
270 bool radv_device_entrypoint_is_enabled(int index, uint32_t core_version,
271 				       const struct radv_instance_extension_table *instance,
272 				       const struct radv_device_extension_table *device);
273 
274 void *radv_lookup_entrypoint(const char *name);
275 
276 struct radv_physical_device {
277 	VK_LOADER_DATA                              _loader_data;
278 
279 	/* Link in radv_instance::physical_devices */
280 	struct list_head                            link;
281 
282 	struct radv_instance *                       instance;
283 
284 	struct radeon_winsys *ws;
285 	struct radeon_info rad_info;
286 	char                                        name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
287 	uint8_t                                     driver_uuid[VK_UUID_SIZE];
288 	uint8_t                                     device_uuid[VK_UUID_SIZE];
289 	uint8_t                                     cache_uuid[VK_UUID_SIZE];
290 
291 	int local_fd;
292 	int master_fd;
293 	struct wsi_device                       wsi_device;
294 
295 	bool out_of_order_rast_allowed;
296 
297 	/* Whether DCC should be enabled for MSAA textures. */
298 	bool dcc_msaa_allowed;
299 
300 	/* Whether to enable NGG. */
301 	bool use_ngg;
302 
303 	/* Whether to enable NGG streamout. */
304 	bool use_ngg_streamout;
305 
306 	/* Number of threads per wave. */
307 	uint8_t ps_wave_size;
308 	uint8_t cs_wave_size;
309 	uint8_t ge_wave_size;
310 
311 	/* Whether to use the LLVM compiler backend */
312 	bool use_llvm;
313 
314 	/* This is the drivers on-disk cache used as a fallback as opposed to
315 	 * the pipeline cache defined by apps.
316 	 */
317 	struct disk_cache *                          disk_cache;
318 
319 	VkPhysicalDeviceMemoryProperties memory_properties;
320 	enum radeon_bo_domain memory_domains[VK_MAX_MEMORY_TYPES];
321 	enum radeon_bo_flag memory_flags[VK_MAX_MEMORY_TYPES];
322 	unsigned heaps;
323 
324 	drmPciBusInfo bus_info;
325 
326 	struct radv_device_extension_table supported_extensions;
327 };
328 
329 struct radv_instance {
330 	struct vk_object_base                       base;
331 
332 	VkAllocationCallbacks                       alloc;
333 
334 	uint32_t                                    apiVersion;
335 
336 	char *                                      applicationName;
337 	uint32_t                                    applicationVersion;
338 	char *                                      engineName;
339 	uint32_t                                    engineVersion;
340 
341 	uint64_t debug_flags;
342 	uint64_t perftest_flags;
343 
344 	struct vk_debug_report_instance             debug_report_callbacks;
345 
346 	struct radv_instance_extension_table enabled_extensions;
347 	struct radv_instance_dispatch_table          dispatch;
348 	struct radv_physical_device_dispatch_table   physical_device_dispatch;
349 	struct radv_device_dispatch_table            device_dispatch;
350 
351 	bool                                        physical_devices_enumerated;
352 	struct list_head                            physical_devices;
353 
354 	struct driOptionCache dri_options;
355 	struct driOptionCache available_dri_options;
356 
357 	/**
358 	 * Workarounds for game bugs.
359 	 */
360 	bool enable_mrt_output_nan_fixup;
361 	bool disable_tc_compat_htile_in_general;
362 };
363 
364 VkResult radv_init_wsi(struct radv_physical_device *physical_device);
365 void radv_finish_wsi(struct radv_physical_device *physical_device);
366 
367 bool radv_instance_extension_supported(const char *name);
368 uint32_t radv_physical_device_api_version(struct radv_physical_device *dev);
369 bool radv_physical_device_extension_supported(struct radv_physical_device *dev,
370 					      const char *name);
371 
372 struct cache_entry;
373 
374 struct radv_pipeline_cache {
375 	struct vk_object_base                        base;
376 	struct radv_device *                         device;
377 	pthread_mutex_t                              mutex;
378 	VkPipelineCacheCreateFlags                   flags;
379 
380 	uint32_t                                     total_size;
381 	uint32_t                                     table_size;
382 	uint32_t                                     kernel_count;
383 	struct cache_entry **                        hash_table;
384 	bool                                         modified;
385 
386 	VkAllocationCallbacks                        alloc;
387 };
388 
389 struct radv_pipeline_key {
390 	uint32_t instance_rate_inputs;
391 	uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
392 	uint8_t vertex_attribute_formats[MAX_VERTEX_ATTRIBS];
393 	uint32_t vertex_attribute_bindings[MAX_VERTEX_ATTRIBS];
394 	uint32_t vertex_attribute_offsets[MAX_VERTEX_ATTRIBS];
395 	uint32_t vertex_attribute_strides[MAX_VERTEX_ATTRIBS];
396 	enum ac_fetch_format vertex_alpha_adjust[MAX_VERTEX_ATTRIBS];
397 	uint32_t vertex_post_shuffle;
398 	unsigned tess_input_vertices;
399 	uint32_t col_format;
400 	uint32_t is_int8;
401 	uint32_t is_int10;
402 	uint8_t log2_ps_iter_samples;
403 	uint8_t num_samples;
404 	bool is_dual_src;
405 	uint32_t has_multiview_view_index : 1;
406 	uint32_t optimisations_disabled : 1;
407 	uint8_t topology;
408 
409 	/* Non-zero if a required subgroup size is specified via
410 	 * VK_EXT_subgroup_size_control.
411 	 */
412 	uint8_t compute_subgroup_size;
413 };
414 
415 struct radv_shader_binary;
416 struct radv_shader_variant;
417 
418 void
419 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
420 			 struct radv_device *device);
421 void
422 radv_pipeline_cache_finish(struct radv_pipeline_cache *cache);
423 bool
424 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
425 			 const void *data, size_t size);
426 
427 bool
428 radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
429 					        struct radv_pipeline_cache *cache,
430 					        const unsigned char *sha1,
431 					        struct radv_shader_variant **variants,
432 						bool *found_in_application_cache);
433 
434 void
435 radv_pipeline_cache_insert_shaders(struct radv_device *device,
436 				   struct radv_pipeline_cache *cache,
437 				   const unsigned char *sha1,
438 				   struct radv_shader_variant **variants,
439 				   struct radv_shader_binary *const *binaries);
440 
441 enum radv_blit_ds_layout {
442 	RADV_BLIT_DS_LAYOUT_TILE_ENABLE,
443 	RADV_BLIT_DS_LAYOUT_TILE_DISABLE,
444 	RADV_BLIT_DS_LAYOUT_COUNT,
445 };
446 
radv_meta_blit_ds_to_type(VkImageLayout layout)447 static inline enum radv_blit_ds_layout radv_meta_blit_ds_to_type(VkImageLayout layout)
448 {
449 	return (layout == VK_IMAGE_LAYOUT_GENERAL) ? RADV_BLIT_DS_LAYOUT_TILE_DISABLE : RADV_BLIT_DS_LAYOUT_TILE_ENABLE;
450 }
451 
radv_meta_blit_ds_to_layout(enum radv_blit_ds_layout ds_layout)452 static inline VkImageLayout radv_meta_blit_ds_to_layout(enum radv_blit_ds_layout ds_layout)
453 {
454 	return ds_layout == RADV_BLIT_DS_LAYOUT_TILE_ENABLE ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL : VK_IMAGE_LAYOUT_GENERAL;
455 }
456 
457 enum radv_meta_dst_layout {
458 	RADV_META_DST_LAYOUT_GENERAL,
459 	RADV_META_DST_LAYOUT_OPTIMAL,
460 	RADV_META_DST_LAYOUT_COUNT,
461 };
462 
radv_meta_dst_layout_from_layout(VkImageLayout layout)463 static inline enum radv_meta_dst_layout radv_meta_dst_layout_from_layout(VkImageLayout layout)
464 {
465 	return (layout == VK_IMAGE_LAYOUT_GENERAL) ? RADV_META_DST_LAYOUT_GENERAL : RADV_META_DST_LAYOUT_OPTIMAL;
466 }
467 
radv_meta_dst_layout_to_layout(enum radv_meta_dst_layout layout)468 static inline VkImageLayout radv_meta_dst_layout_to_layout(enum radv_meta_dst_layout layout)
469 {
470 	return layout == RADV_META_DST_LAYOUT_OPTIMAL ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL : VK_IMAGE_LAYOUT_GENERAL;
471 }
472 
473 struct radv_meta_state {
474 	VkAllocationCallbacks alloc;
475 
476 	struct radv_pipeline_cache cache;
477 
478 	/*
479 	 * For on-demand pipeline creation, makes sure that
480 	 * only one thread tries to build a pipeline at the same time.
481 	 */
482 	mtx_t mtx;
483 
484 	/**
485 	 * Use array element `i` for images with `2^i` samples.
486 	 */
487 	struct {
488 		VkRenderPass render_pass[NUM_META_FS_KEYS];
489 		VkPipeline color_pipelines[NUM_META_FS_KEYS];
490 
491 		VkRenderPass depthstencil_rp;
492 		VkPipeline depth_only_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
493 		VkPipeline stencil_only_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
494 		VkPipeline depthstencil_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
495 
496 		VkPipeline depth_only_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
497 		VkPipeline stencil_only_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
498 		VkPipeline depthstencil_unrestricted_pipeline[NUM_DEPTH_CLEAR_PIPELINES];
499 	} clear[MAX_SAMPLES_LOG2];
500 
501 	VkPipelineLayout                          clear_color_p_layout;
502 	VkPipelineLayout                          clear_depth_p_layout;
503 	VkPipelineLayout                          clear_depth_unrestricted_p_layout;
504 
505 	/* Optimized compute fast HTILE clear for stencil or depth only. */
506 	VkPipeline clear_htile_mask_pipeline;
507 	VkPipelineLayout clear_htile_mask_p_layout;
508 	VkDescriptorSetLayout clear_htile_mask_ds_layout;
509 
510 	struct {
511 		VkRenderPass render_pass[NUM_META_FS_KEYS][RADV_META_DST_LAYOUT_COUNT];
512 
513 		/** Pipeline that blits from a 1D image. */
514 		VkPipeline pipeline_1d_src[NUM_META_FS_KEYS];
515 
516 		/** Pipeline that blits from a 2D image. */
517 		VkPipeline pipeline_2d_src[NUM_META_FS_KEYS];
518 
519 		/** Pipeline that blits from a 3D image. */
520 		VkPipeline pipeline_3d_src[NUM_META_FS_KEYS];
521 
522 		VkRenderPass depth_only_rp[RADV_BLIT_DS_LAYOUT_COUNT];
523 		VkPipeline depth_only_1d_pipeline;
524 		VkPipeline depth_only_2d_pipeline;
525 		VkPipeline depth_only_3d_pipeline;
526 
527 		VkRenderPass stencil_only_rp[RADV_BLIT_DS_LAYOUT_COUNT];
528 		VkPipeline stencil_only_1d_pipeline;
529 		VkPipeline stencil_only_2d_pipeline;
530 		VkPipeline stencil_only_3d_pipeline;
531 		VkPipelineLayout                          pipeline_layout;
532 		VkDescriptorSetLayout                     ds_layout;
533 	} blit;
534 
535 	struct {
536 		VkPipelineLayout p_layouts[5];
537 		VkDescriptorSetLayout ds_layouts[5];
538 		VkPipeline pipelines[5][NUM_META_FS_KEYS];
539 
540 		VkPipeline depth_only_pipeline[5];
541 
542 		VkPipeline stencil_only_pipeline[5];
543 	} blit2d[MAX_SAMPLES_LOG2];
544 
545 	VkRenderPass blit2d_render_passes[NUM_META_FS_KEYS][RADV_META_DST_LAYOUT_COUNT];
546 	VkRenderPass blit2d_depth_only_rp[RADV_BLIT_DS_LAYOUT_COUNT];
547 	VkRenderPass blit2d_stencil_only_rp[RADV_BLIT_DS_LAYOUT_COUNT];
548 
549 	struct {
550 		VkPipelineLayout                          img_p_layout;
551 		VkDescriptorSetLayout                     img_ds_layout;
552 		VkPipeline pipeline;
553 		VkPipeline pipeline_3d;
554 	} itob;
555 	struct {
556 		VkPipelineLayout                          img_p_layout;
557 		VkDescriptorSetLayout                     img_ds_layout;
558 		VkPipeline pipeline;
559 		VkPipeline pipeline_3d;
560 	} btoi;
561 	struct {
562 		VkPipelineLayout                          img_p_layout;
563 		VkDescriptorSetLayout                     img_ds_layout;
564 		VkPipeline pipeline;
565 	} btoi_r32g32b32;
566 	struct {
567 		VkPipelineLayout                          img_p_layout;
568 		VkDescriptorSetLayout                     img_ds_layout;
569 		VkPipeline pipeline;
570 		VkPipeline pipeline_3d;
571 	} itoi;
572 	struct {
573 		VkPipelineLayout                          img_p_layout;
574 		VkDescriptorSetLayout                     img_ds_layout;
575 		VkPipeline pipeline;
576 	} itoi_r32g32b32;
577 	struct {
578 		VkPipelineLayout                          img_p_layout;
579 		VkDescriptorSetLayout                     img_ds_layout;
580 		VkPipeline pipeline;
581 		VkPipeline pipeline_3d;
582 	} cleari;
583 	struct {
584 		VkPipelineLayout                          img_p_layout;
585 		VkDescriptorSetLayout                     img_ds_layout;
586 		VkPipeline pipeline;
587 	} cleari_r32g32b32;
588 
589 	struct {
590 		VkPipelineLayout                          p_layout;
591 		VkPipeline                                pipeline[NUM_META_FS_KEYS];
592 		VkRenderPass                              pass[NUM_META_FS_KEYS];
593 	} resolve;
594 
595 	struct {
596 		VkDescriptorSetLayout                     ds_layout;
597 		VkPipelineLayout                          p_layout;
598 		struct {
599 			VkPipeline                                pipeline;
600 			VkPipeline                                i_pipeline;
601 			VkPipeline                                srgb_pipeline;
602 		} rc[MAX_SAMPLES_LOG2];
603 
604 		VkPipeline depth_zero_pipeline;
605 		struct {
606 			VkPipeline average_pipeline;
607 			VkPipeline max_pipeline;
608 			VkPipeline min_pipeline;
609 		} depth[MAX_SAMPLES_LOG2];
610 
611 		VkPipeline stencil_zero_pipeline;
612 		struct {
613 			VkPipeline max_pipeline;
614 			VkPipeline min_pipeline;
615 		} stencil[MAX_SAMPLES_LOG2];
616 	} resolve_compute;
617 
618 	struct {
619 		VkDescriptorSetLayout                     ds_layout;
620 		VkPipelineLayout                          p_layout;
621 
622 		struct {
623 			VkRenderPass render_pass[NUM_META_FS_KEYS][RADV_META_DST_LAYOUT_COUNT];
624 			VkPipeline   pipeline[NUM_META_FS_KEYS];
625 		} rc[MAX_SAMPLES_LOG2];
626 
627 		VkRenderPass depth_render_pass;
628 		VkPipeline depth_zero_pipeline;
629 		struct {
630 			VkPipeline average_pipeline;
631 			VkPipeline max_pipeline;
632 			VkPipeline min_pipeline;
633 		} depth[MAX_SAMPLES_LOG2];
634 
635 		VkRenderPass stencil_render_pass;
636 		VkPipeline stencil_zero_pipeline;
637 		struct {
638 			VkPipeline max_pipeline;
639 			VkPipeline min_pipeline;
640 		} stencil[MAX_SAMPLES_LOG2];
641 	} resolve_fragment;
642 
643 	struct {
644 		VkPipelineLayout                          p_layout;
645 		VkPipeline                                decompress_pipeline[NUM_DEPTH_DECOMPRESS_PIPELINES];
646 		VkPipeline                                resummarize_pipeline;
647 		VkRenderPass                              pass;
648 	} depth_decomp[MAX_SAMPLES_LOG2];
649 
650 	struct {
651 		VkPipelineLayout                          p_layout;
652 		VkPipeline                                cmask_eliminate_pipeline;
653 		VkPipeline                                fmask_decompress_pipeline;
654 		VkPipeline                                dcc_decompress_pipeline;
655 		VkRenderPass                              pass;
656 
657 		VkDescriptorSetLayout                     dcc_decompress_compute_ds_layout;
658 		VkPipelineLayout                          dcc_decompress_compute_p_layout;
659 		VkPipeline                                dcc_decompress_compute_pipeline;
660 	} fast_clear_flush;
661 
662 	struct {
663 		VkPipelineLayout fill_p_layout;
664 		VkPipelineLayout copy_p_layout;
665 		VkDescriptorSetLayout fill_ds_layout;
666 		VkDescriptorSetLayout copy_ds_layout;
667 		VkPipeline fill_pipeline;
668 		VkPipeline copy_pipeline;
669 	} buffer;
670 
671 	struct {
672 		VkDescriptorSetLayout ds_layout;
673 		VkPipelineLayout p_layout;
674 		VkPipeline occlusion_query_pipeline;
675 		VkPipeline pipeline_statistics_query_pipeline;
676 		VkPipeline tfb_query_pipeline;
677 		VkPipeline timestamp_query_pipeline;
678 	} query;
679 
680 	struct {
681 		VkDescriptorSetLayout ds_layout;
682 		VkPipelineLayout p_layout;
683 		VkPipeline pipeline[MAX_SAMPLES_LOG2];
684 	} fmask_expand;
685 };
686 
687 /* queue types */
688 #define RADV_QUEUE_GENERAL 0
689 #define RADV_QUEUE_COMPUTE 1
690 #define RADV_QUEUE_TRANSFER 2
691 
692 #define RADV_MAX_QUEUE_FAMILIES 3
693 
694 struct radv_deferred_queue_submission;
695 
696 enum ring_type radv_queue_family_to_ring(int f);
697 
698 struct radv_queue {
699 	VK_LOADER_DATA                              _loader_data;
700 	struct radv_device *                         device;
701 	struct radeon_winsys_ctx                    *hw_ctx;
702 	enum radeon_ctx_priority                     priority;
703 	uint32_t queue_family_index;
704 	int queue_idx;
705 	VkDeviceQueueCreateFlags flags;
706 
707 	uint32_t scratch_size_per_wave;
708 	uint32_t scratch_waves;
709 	uint32_t compute_scratch_size_per_wave;
710 	uint32_t compute_scratch_waves;
711 	uint32_t esgs_ring_size;
712 	uint32_t gsvs_ring_size;
713 	bool has_tess_rings;
714 	bool has_gds;
715 	bool has_gds_oa;
716 	bool has_sample_positions;
717 
718 	struct radeon_winsys_bo *scratch_bo;
719 	struct radeon_winsys_bo *descriptor_bo;
720 	struct radeon_winsys_bo *compute_scratch_bo;
721 	struct radeon_winsys_bo *esgs_ring_bo;
722 	struct radeon_winsys_bo *gsvs_ring_bo;
723 	struct radeon_winsys_bo *tess_rings_bo;
724 	struct radeon_winsys_bo *gds_bo;
725 	struct radeon_winsys_bo *gds_oa_bo;
726 	struct radeon_cmdbuf *initial_preamble_cs;
727 	struct radeon_cmdbuf *initial_full_flush_preamble_cs;
728 	struct radeon_cmdbuf *continue_preamble_cs;
729 
730 	struct list_head pending_submissions;
731 	pthread_mutex_t pending_mutex;
732 
733 	pthread_mutex_t thread_mutex;
734 	pthread_cond_t thread_cond;
735 	struct radv_deferred_queue_submission *thread_submission;
736 	pthread_t submission_thread;
737 	bool thread_exit;
738 	bool thread_running;
739 };
740 
741 struct radv_bo_list {
742 	struct radv_winsys_bo_list list;
743 	unsigned capacity;
744 	struct u_rwlock rwlock;
745 };
746 
747 VkResult radv_bo_list_add(struct radv_device *device,
748 			  struct radeon_winsys_bo *bo);
749 void radv_bo_list_remove(struct radv_device *device,
750 			 struct radeon_winsys_bo *bo);
751 
752 #define RADV_BORDER_COLOR_COUNT       4096
753 #define RADV_BORDER_COLOR_BUFFER_SIZE (sizeof(VkClearColorValue) * RADV_BORDER_COLOR_COUNT)
754 
755 struct radv_device_border_color_data {
756 	bool 			 used[RADV_BORDER_COLOR_COUNT];
757 
758 	struct radeon_winsys_bo *bo;
759 	VkClearColorValue       *colors_gpu_ptr;
760 
761 	/* Mutex is required to guarantee vkCreateSampler thread safety
762 	 * given that we are writing to a buffer and checking color occupation */
763 	pthread_mutex_t          mutex;
764 };
765 
766 struct radv_device {
767 	struct vk_device vk;
768 
769 	struct radv_instance *                       instance;
770 	struct radeon_winsys *ws;
771 
772 	struct radv_meta_state                       meta_state;
773 
774 	struct radv_queue *queues[RADV_MAX_QUEUE_FAMILIES];
775 	int queue_count[RADV_MAX_QUEUE_FAMILIES];
776 	struct radeon_cmdbuf *empty_cs[RADV_MAX_QUEUE_FAMILIES];
777 
778 	bool always_use_syncobj;
779 	bool pbb_allowed;
780 	bool dfsm_allowed;
781 	uint32_t tess_offchip_block_dw_size;
782 	uint32_t scratch_waves;
783 	uint32_t dispatch_initiator;
784 
785 	uint32_t gs_table_depth;
786 
787 	/* MSAA sample locations.
788 	 * The first index is the sample index.
789 	 * The second index is the coordinate: X, Y. */
790 	float sample_locations_1x[1][2];
791 	float sample_locations_2x[2][2];
792 	float sample_locations_4x[4][2];
793 	float sample_locations_8x[8][2];
794 
795 	/* GFX7 and later */
796 	uint32_t gfx_init_size_dw;
797 	struct radeon_winsys_bo                      *gfx_init;
798 
799 	struct radeon_winsys_bo                      *trace_bo;
800 	uint32_t                                     *trace_id_ptr;
801 
802 	/* Whether to keep shader debug info, for tracing or VK_AMD_shader_info */
803 	bool                                         keep_shader_info;
804 
805 	struct radv_physical_device                  *physical_device;
806 
807 	/* Backup in-memory cache to be used if the app doesn't provide one */
808 	struct radv_pipeline_cache *                mem_cache;
809 
810 	/*
811 	 * use different counters so MSAA MRTs get consecutive surface indices,
812 	 * even if MASK is allocated in between.
813 	 */
814 	uint32_t image_mrt_offset_counter;
815 	uint32_t fmask_mrt_offset_counter;
816 	struct list_head shader_slabs;
817 	mtx_t shader_slab_mutex;
818 
819 	/* For detecting VM faults reported by dmesg. */
820 	uint64_t dmesg_timestamp;
821 
822 	struct radv_device_extension_table enabled_extensions;
823 	struct radv_device_dispatch_table dispatch;
824 
825 	/* Whether the app has enabled the robustBufferAccess feature. */
826 	bool robust_buffer_access;
827 
828 	/* Whether the driver uses a global BO list. */
829 	bool use_global_bo_list;
830 
831 	struct radv_bo_list bo_list;
832 
833 	/* Whether anisotropy is forced with RADV_TEX_ANISO (-1 is disabled). */
834 	int force_aniso;
835 
836 	struct radv_device_border_color_data border_color_data;
837 
838 	/* Condition variable for legacy timelines, to notify waiters when a
839 	 * new point gets submitted. */
840 	pthread_cond_t timeline_cond;
841 
842 	/* Thread trace. */
843 	struct radeon_cmdbuf *thread_trace_start_cs[2];
844 	struct radeon_cmdbuf *thread_trace_stop_cs[2];
845 	struct radeon_winsys_bo *thread_trace_bo;
846 	void *thread_trace_ptr;
847 	uint32_t thread_trace_buffer_size;
848 	int thread_trace_start_frame;
849 	char *thread_trace_trigger_file;
850 
851 	/* Trap handler. */
852 	struct radv_shader_variant *trap_handler_shader;
853 	struct radeon_winsys_bo *tma_bo; /* Trap Memory Address */
854 	uint32_t *tma_ptr;
855 
856 	/* Overallocation. */
857 	bool overallocation_disallowed;
858 	uint64_t allocated_memory_size[VK_MAX_MEMORY_HEAPS];
859 	mtx_t overallocation_mutex;
860 
861 	/* Track the number of device loss occurs. */
862 	int lost;
863 };
864 
865 VkResult _radv_device_set_lost(struct radv_device *device,
866                               const char *file, int line,
867                               const char *msg, ...)
868 	radv_printflike(4, 5);
869 
870 #define radv_device_set_lost(dev, ...) \
871 	_radv_device_set_lost(dev, __FILE__, __LINE__, __VA_ARGS__)
872 
873 static inline bool
radv_device_is_lost(const struct radv_device * device)874 radv_device_is_lost(const struct radv_device *device)
875 {
876 	return unlikely(p_atomic_read(&device->lost));
877 }
878 
879 struct radv_device_memory {
880 	struct vk_object_base                        base;
881 	struct radeon_winsys_bo                      *bo;
882 	/* for dedicated allocations */
883 	struct radv_image                            *image;
884 	struct radv_buffer                           *buffer;
885 	uint32_t                                     heap_index;
886 	uint64_t                                     alloc_size;
887 	void *                                       map;
888 	void *                                       user_ptr;
889 
890 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
891 	struct AHardwareBuffer *                    android_hardware_buffer;
892 #endif
893 };
894 
895 
896 struct radv_descriptor_range {
897 	uint64_t va;
898 	uint32_t size;
899 };
900 
901 struct radv_descriptor_set {
902 	struct vk_object_base base;
903 	const struct radv_descriptor_set_layout *layout;
904 	uint32_t size;
905 	uint32_t buffer_count;
906 
907 	struct radeon_winsys_bo *bo;
908 	uint64_t va;
909 	uint32_t *mapped_ptr;
910 	struct radv_descriptor_range *dynamic_descriptors;
911 
912 	struct radeon_winsys_bo *descriptors[0];
913 };
914 
915 struct radv_push_descriptor_set
916 {
917 	struct radv_descriptor_set set;
918 	uint32_t capacity;
919 };
920 
921 struct radv_descriptor_pool_entry {
922 	uint32_t offset;
923 	uint32_t size;
924 	struct radv_descriptor_set *set;
925 };
926 
927 struct radv_descriptor_pool {
928 	struct vk_object_base base;
929 	struct radeon_winsys_bo *bo;
930 	uint8_t *mapped_ptr;
931 	uint64_t current_offset;
932 	uint64_t size;
933 
934 	uint8_t *host_memory_base;
935 	uint8_t *host_memory_ptr;
936 	uint8_t *host_memory_end;
937 
938 	uint32_t entry_count;
939 	uint32_t max_entry_count;
940 	struct radv_descriptor_pool_entry entries[0];
941 };
942 
943 struct radv_descriptor_update_template_entry {
944 	VkDescriptorType descriptor_type;
945 
946 	/* The number of descriptors to update */
947 	uint32_t descriptor_count;
948 
949 	/* Into mapped_ptr or dynamic_descriptors, in units of the respective array */
950 	uint32_t dst_offset;
951 
952 	/* In dwords. Not valid/used for dynamic descriptors */
953 	uint32_t dst_stride;
954 
955 	uint32_t buffer_offset;
956 
957 	/* Only valid for combined image samplers and samplers */
958 	uint8_t has_sampler;
959 	uint8_t sampler_offset;
960 
961 	/* In bytes */
962 	size_t src_offset;
963 	size_t src_stride;
964 
965 	/* For push descriptors */
966 	const uint32_t *immutable_samplers;
967 };
968 
969 struct radv_descriptor_update_template {
970 	struct vk_object_base base;
971 	uint32_t entry_count;
972 	VkPipelineBindPoint bind_point;
973 	struct radv_descriptor_update_template_entry entry[0];
974 };
975 
976 struct radv_buffer {
977 	struct vk_object_base                        base;
978 	VkDeviceSize                                 size;
979 
980 	VkBufferUsageFlags                           usage;
981 	VkBufferCreateFlags                          flags;
982 
983 	/* Set when bound */
984 	struct radeon_winsys_bo *                      bo;
985 	VkDeviceSize                                 offset;
986 
987 	bool shareable;
988 };
989 
990 enum radv_dynamic_state_bits {
991 	RADV_DYNAMIC_VIEWPORT				= 1 << 0,
992 	RADV_DYNAMIC_SCISSOR				= 1 << 1,
993 	RADV_DYNAMIC_LINE_WIDTH				= 1 << 2,
994 	RADV_DYNAMIC_DEPTH_BIAS				= 1 << 3,
995 	RADV_DYNAMIC_BLEND_CONSTANTS			= 1 << 4,
996 	RADV_DYNAMIC_DEPTH_BOUNDS			= 1 << 5,
997 	RADV_DYNAMIC_STENCIL_COMPARE_MASK		= 1 << 6,
998 	RADV_DYNAMIC_STENCIL_WRITE_MASK			= 1 << 7,
999 	RADV_DYNAMIC_STENCIL_REFERENCE			= 1 << 8,
1000 	RADV_DYNAMIC_DISCARD_RECTANGLE			= 1 << 9,
1001 	RADV_DYNAMIC_SAMPLE_LOCATIONS			= 1 << 10,
1002 	RADV_DYNAMIC_LINE_STIPPLE			= 1 << 11,
1003 	RADV_DYNAMIC_CULL_MODE				= 1 << 12,
1004 	RADV_DYNAMIC_FRONT_FACE				= 1 << 13,
1005 	RADV_DYNAMIC_PRIMITIVE_TOPOLOGY			= 1 << 14,
1006 	RADV_DYNAMIC_DEPTH_TEST_ENABLE			= 1 << 15,
1007 	RADV_DYNAMIC_DEPTH_WRITE_ENABLE			= 1 << 16,
1008 	RADV_DYNAMIC_DEPTH_COMPARE_OP			= 1 << 17,
1009 	RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE		= 1 << 18,
1010 	RADV_DYNAMIC_STENCIL_TEST_ENABLE		= 1 << 19,
1011 	RADV_DYNAMIC_STENCIL_OP				= 1 << 20,
1012 	RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE        = 1 << 21,
1013 	RADV_DYNAMIC_ALL				= (1 << 22) - 1,
1014 };
1015 
1016 enum radv_cmd_dirty_bits {
1017 	/* Keep the dynamic state dirty bits in sync with
1018 	 * enum radv_dynamic_state_bits */
1019 	RADV_CMD_DIRTY_DYNAMIC_VIEWPORT				= 1 << 0,
1020 	RADV_CMD_DIRTY_DYNAMIC_SCISSOR				= 1 << 1,
1021 	RADV_CMD_DIRTY_DYNAMIC_LINE_WIDTH			= 1 << 2,
1022 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS			= 1 << 3,
1023 	RADV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS			= 1 << 4,
1024 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS			= 1 << 5,
1025 	RADV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK		= 1 << 6,
1026 	RADV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK		= 1 << 7,
1027 	RADV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE		= 1 << 8,
1028 	RADV_CMD_DIRTY_DYNAMIC_DISCARD_RECTANGLE		= 1 << 9,
1029 	RADV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS			= 1 << 10,
1030 	RADV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE			= 1 << 11,
1031 	RADV_CMD_DIRTY_DYNAMIC_CULL_MODE			= 1 << 12,
1032 	RADV_CMD_DIRTY_DYNAMIC_FRONT_FACE			= 1 << 13,
1033 	RADV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY		= 1 << 14,
1034 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE		= 1 << 15,
1035 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE		= 1 << 16,
1036 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP			= 1 << 17,
1037 	RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE		= 1 << 18,
1038 	RADV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE		= 1 << 19,
1039 	RADV_CMD_DIRTY_DYNAMIC_STENCIL_OP			= 1 << 20,
1040 	RADV_CMD_DIRTY_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE      = 1 << 21,
1041 	RADV_CMD_DIRTY_DYNAMIC_ALL				= (1 << 22) - 1,
1042 	RADV_CMD_DIRTY_PIPELINE					= 1 << 22,
1043 	RADV_CMD_DIRTY_INDEX_BUFFER				= 1 << 23,
1044 	RADV_CMD_DIRTY_FRAMEBUFFER				= 1 << 24,
1045 	RADV_CMD_DIRTY_VERTEX_BUFFER				= 1 << 25,
1046 	RADV_CMD_DIRTY_STREAMOUT_BUFFER				= 1 << 26,
1047 };
1048 
1049 enum radv_cmd_flush_bits {
1050 	/* Instruction cache. */
1051 	RADV_CMD_FLAG_INV_ICACHE			 = 1 << 0,
1052 	/* Scalar L1 cache. */
1053 	RADV_CMD_FLAG_INV_SCACHE			 = 1 << 1,
1054 	/* Vector L1 cache. */
1055 	RADV_CMD_FLAG_INV_VCACHE			 = 1 << 2,
1056 	/* L2 cache + L2 metadata cache writeback & invalidate.
1057 	 * GFX6-8: Used by shaders only. GFX9-10: Used by everything. */
1058 	RADV_CMD_FLAG_INV_L2				 = 1 << 3,
1059 	/* L2 writeback (write dirty L2 lines to memory for non-L2 clients).
1060 	 * Only used for coherency with non-L2 clients like CB, DB, CP on GFX6-8.
1061 	 * GFX6-7 will do complete invalidation, because the writeback is unsupported. */
1062 	RADV_CMD_FLAG_WB_L2				 = 1 << 4,
1063 	/* Framebuffer caches */
1064 	RADV_CMD_FLAG_FLUSH_AND_INV_CB_META		 = 1 << 5,
1065 	RADV_CMD_FLAG_FLUSH_AND_INV_DB_META		 = 1 << 6,
1066 	RADV_CMD_FLAG_FLUSH_AND_INV_DB			 = 1 << 7,
1067 	RADV_CMD_FLAG_FLUSH_AND_INV_CB			 = 1 << 8,
1068 	/* Engine synchronization. */
1069 	RADV_CMD_FLAG_VS_PARTIAL_FLUSH			 = 1 << 9,
1070 	RADV_CMD_FLAG_PS_PARTIAL_FLUSH			 = 1 << 10,
1071 	RADV_CMD_FLAG_CS_PARTIAL_FLUSH			 = 1 << 11,
1072 	RADV_CMD_FLAG_VGT_FLUSH				 = 1 << 12,
1073 	/* Pipeline query controls. */
1074 	RADV_CMD_FLAG_START_PIPELINE_STATS		 = 1 << 13,
1075 	RADV_CMD_FLAG_STOP_PIPELINE_STATS		 = 1 << 14,
1076 	RADV_CMD_FLAG_VGT_STREAMOUT_SYNC		 = 1 << 15,
1077 
1078 	RADV_CMD_FLUSH_AND_INV_FRAMEBUFFER = (RADV_CMD_FLAG_FLUSH_AND_INV_CB |
1079 					      RADV_CMD_FLAG_FLUSH_AND_INV_CB_META |
1080 					      RADV_CMD_FLAG_FLUSH_AND_INV_DB |
1081 					      RADV_CMD_FLAG_FLUSH_AND_INV_DB_META)
1082 };
1083 
1084 struct radv_vertex_binding {
1085 	struct radv_buffer *                          buffer;
1086 	VkDeviceSize                                 offset;
1087 	VkDeviceSize size;
1088 	VkDeviceSize stride;
1089 };
1090 
1091 struct radv_streamout_binding {
1092 	struct radv_buffer *buffer;
1093 	VkDeviceSize offset;
1094 	VkDeviceSize size;
1095 };
1096 
1097 struct radv_streamout_state {
1098 	/* Mask of bound streamout buffers. */
1099 	uint8_t enabled_mask;
1100 
1101 	/* External state that comes from the last vertex stage, it must be
1102 	 * set explicitely when binding a new graphics pipeline.
1103 	 */
1104 	uint16_t stride_in_dw[MAX_SO_BUFFERS];
1105 	uint32_t enabled_stream_buffers_mask; /* stream0 buffers0-3 in 4 LSB */
1106 
1107 	/* State of VGT_STRMOUT_BUFFER_(CONFIG|END) */
1108 	uint32_t hw_enabled_mask;
1109 
1110 	/* State of VGT_STRMOUT_(CONFIG|EN) */
1111 	bool streamout_enabled;
1112 };
1113 
1114 struct radv_viewport_state {
1115 	uint32_t                                          count;
1116 	VkViewport                                        viewports[MAX_VIEWPORTS];
1117 };
1118 
1119 struct radv_scissor_state {
1120 	uint32_t                                          count;
1121 	VkRect2D                                          scissors[MAX_SCISSORS];
1122 };
1123 
1124 struct radv_discard_rectangle_state {
1125 	uint32_t                                          count;
1126 	VkRect2D                                          rectangles[MAX_DISCARD_RECTANGLES];
1127 };
1128 
1129 struct radv_sample_locations_state {
1130 	VkSampleCountFlagBits per_pixel;
1131 	VkExtent2D grid_size;
1132 	uint32_t count;
1133 	VkSampleLocationEXT locations[MAX_SAMPLE_LOCATIONS];
1134 };
1135 
1136 struct radv_dynamic_state {
1137 	/**
1138 	 * Bitmask of (1 << VK_DYNAMIC_STATE_*).
1139 	 * Defines the set of saved dynamic state.
1140 	 */
1141 	uint32_t mask;
1142 
1143 	struct radv_viewport_state                        viewport;
1144 
1145 	struct radv_scissor_state                         scissor;
1146 
1147 	float                                        line_width;
1148 
1149 	struct {
1150 		float                                     bias;
1151 		float                                     clamp;
1152 		float                                     slope;
1153 	} depth_bias;
1154 
1155 	float                                        blend_constants[4];
1156 
1157 	struct {
1158 		float                                     min;
1159 		float                                     max;
1160 	} depth_bounds;
1161 
1162 	struct {
1163 		uint32_t                                  front;
1164 		uint32_t                                  back;
1165 	} stencil_compare_mask;
1166 
1167 	struct {
1168 		uint32_t                                  front;
1169 		uint32_t                                  back;
1170 	} stencil_write_mask;
1171 
1172 	struct {
1173 		struct {
1174 			VkStencilOp fail_op;
1175 			VkStencilOp pass_op;
1176 			VkStencilOp depth_fail_op;
1177 			VkCompareOp compare_op;
1178 		} front;
1179 
1180 		struct {
1181 			VkStencilOp fail_op;
1182 			VkStencilOp pass_op;
1183 			VkStencilOp depth_fail_op;
1184 			VkCompareOp compare_op;
1185 		} back;
1186 	} stencil_op;
1187 
1188 	struct {
1189 		uint32_t                                  front;
1190 		uint32_t                                  back;
1191 	} stencil_reference;
1192 
1193 	struct radv_discard_rectangle_state               discard_rectangle;
1194 
1195 	struct radv_sample_locations_state                sample_location;
1196 
1197 	struct {
1198 		uint32_t factor;
1199 		uint16_t pattern;
1200 	} line_stipple;
1201 
1202 	VkCullModeFlags cull_mode;
1203 	VkFrontFace front_face;
1204 	unsigned primitive_topology;
1205 
1206 	bool depth_test_enable;
1207 	bool depth_write_enable;
1208 	VkCompareOp depth_compare_op;
1209 	bool depth_bounds_test_enable;
1210 	bool stencil_test_enable;
1211 };
1212 
1213 extern const struct radv_dynamic_state default_dynamic_state;
1214 
1215 const char *
1216 radv_get_debug_option_name(int id);
1217 
1218 const char *
1219 radv_get_perftest_option_name(int id);
1220 
1221 struct radv_color_buffer_info {
1222 	uint64_t cb_color_base;
1223 	uint64_t cb_color_cmask;
1224 	uint64_t cb_color_fmask;
1225 	uint64_t cb_dcc_base;
1226 	uint32_t cb_color_slice;
1227 	uint32_t cb_color_view;
1228 	uint32_t cb_color_info;
1229 	uint32_t cb_color_attrib;
1230 	uint32_t cb_color_attrib2; /* GFX9 and later */
1231 	uint32_t cb_color_attrib3; /* GFX10 and later */
1232 	uint32_t cb_dcc_control;
1233 	uint32_t cb_color_cmask_slice;
1234 	uint32_t cb_color_fmask_slice;
1235 	union {
1236 		uint32_t cb_color_pitch; // GFX6-GFX8
1237 		uint32_t cb_mrt_epitch; // GFX9+
1238 	};
1239 };
1240 
1241 struct radv_ds_buffer_info {
1242 	uint64_t db_z_read_base;
1243 	uint64_t db_stencil_read_base;
1244 	uint64_t db_z_write_base;
1245 	uint64_t db_stencil_write_base;
1246 	uint64_t db_htile_data_base;
1247 	uint32_t db_depth_info;
1248 	uint32_t db_z_info;
1249 	uint32_t db_stencil_info;
1250 	uint32_t db_depth_view;
1251 	uint32_t db_depth_size;
1252 	uint32_t db_depth_slice;
1253 	uint32_t db_htile_surface;
1254 	uint32_t pa_su_poly_offset_db_fmt_cntl;
1255 	uint32_t db_z_info2; /* GFX9 only */
1256 	uint32_t db_stencil_info2; /* GFX9 only */
1257 	float offset_scale;
1258 };
1259 
1260 void
1261 radv_initialise_color_surface(struct radv_device *device,
1262 			      struct radv_color_buffer_info *cb,
1263 			      struct radv_image_view *iview);
1264 void
1265 radv_initialise_ds_surface(struct radv_device *device,
1266 			   struct radv_ds_buffer_info *ds,
1267 			   struct radv_image_view *iview);
1268 
1269 /**
1270  * Attachment state when recording a renderpass instance.
1271  *
1272  * The clear value is valid only if there exists a pending clear.
1273  */
1274 struct radv_attachment_state {
1275 	VkImageAspectFlags                           pending_clear_aspects;
1276 	uint32_t                                     cleared_views;
1277 	VkClearValue                                 clear_value;
1278 	VkImageLayout                                current_layout;
1279 	VkImageLayout                                current_stencil_layout;
1280 	bool                                         current_in_render_loop;
1281 	struct radv_sample_locations_state	     sample_location;
1282 
1283 	union {
1284 		struct radv_color_buffer_info cb;
1285 		struct radv_ds_buffer_info ds;
1286 	};
1287 	struct radv_image_view *iview;
1288 };
1289 
1290 struct radv_descriptor_state {
1291 	struct radv_descriptor_set *sets[MAX_SETS];
1292 	uint32_t dirty;
1293 	uint32_t valid;
1294 	struct radv_push_descriptor_set push_set;
1295 	bool push_dirty;
1296 	uint32_t dynamic_buffers[4 * MAX_DYNAMIC_BUFFERS];
1297 };
1298 
1299 struct radv_subpass_sample_locs_state {
1300 	uint32_t subpass_idx;
1301 	struct radv_sample_locations_state sample_location;
1302 };
1303 
1304 enum rgp_flush_bits {
1305 	RGP_FLUSH_WAIT_ON_EOP_TS   = 0x1,
1306 	RGP_FLUSH_VS_PARTIAL_FLUSH = 0x2,
1307 	RGP_FLUSH_PS_PARTIAL_FLUSH = 0x4,
1308 	RGP_FLUSH_CS_PARTIAL_FLUSH = 0x8,
1309 	RGP_FLUSH_PFP_SYNC_ME      = 0x10,
1310 	RGP_FLUSH_SYNC_CP_DMA      = 0x20,
1311 	RGP_FLUSH_INVAL_VMEM_L0    = 0x40,
1312 	RGP_FLUSH_INVAL_ICACHE     = 0x80,
1313 	RGP_FLUSH_INVAL_SMEM_L0    = 0x100,
1314 	RGP_FLUSH_FLUSH_L2         = 0x200,
1315 	RGP_FLUSH_INVAL_L2         = 0x400,
1316 	RGP_FLUSH_FLUSH_CB         = 0x800,
1317 	RGP_FLUSH_INVAL_CB         = 0x1000,
1318 	RGP_FLUSH_FLUSH_DB         = 0x2000,
1319 	RGP_FLUSH_INVAL_DB         = 0x4000,
1320 	RGP_FLUSH_INVAL_L1         = 0x8000,
1321 };
1322 
1323 struct radv_cmd_state {
1324 	/* Vertex descriptors */
1325 	uint64_t                                      vb_va;
1326 	unsigned                                      vb_size;
1327 
1328 	bool predicating;
1329 	uint32_t                                      dirty;
1330 
1331 	uint32_t                                      prefetch_L2_mask;
1332 
1333 	struct radv_pipeline *                        pipeline;
1334 	struct radv_pipeline *                        emitted_pipeline;
1335 	struct radv_pipeline *                        compute_pipeline;
1336 	struct radv_pipeline *                        emitted_compute_pipeline;
1337 	struct radv_framebuffer *                     framebuffer;
1338 	struct radv_render_pass *                     pass;
1339 	const struct radv_subpass *                         subpass;
1340 	struct radv_dynamic_state                     dynamic;
1341 	struct radv_attachment_state *                attachments;
1342 	struct radv_streamout_state                  streamout;
1343 	VkRect2D                                     render_area;
1344 
1345 	uint32_t                                     num_subpass_sample_locs;
1346 	struct radv_subpass_sample_locs_state *      subpass_sample_locs;
1347 
1348 	/* Index buffer */
1349 	struct radv_buffer                           *index_buffer;
1350 	uint64_t                                     index_offset;
1351 	uint32_t                                     index_type;
1352 	uint32_t                                     max_index_count;
1353 	uint64_t                                     index_va;
1354 	int32_t                                      last_index_type;
1355 
1356 	int32_t                                      last_primitive_reset_en;
1357 	uint32_t                                     last_primitive_reset_index;
1358 	enum radv_cmd_flush_bits                     flush_bits;
1359 	unsigned                                     active_occlusion_queries;
1360 	bool                                         perfect_occlusion_queries_enabled;
1361 	unsigned                                     active_pipeline_queries;
1362 	unsigned                                     active_pipeline_gds_queries;
1363 	float					     offset_scale;
1364 	uint32_t                                      trace_id;
1365 	uint32_t                                      last_ia_multi_vgt_param;
1366 
1367 	uint32_t last_num_instances;
1368 	uint32_t last_first_instance;
1369 	uint32_t last_vertex_offset;
1370 
1371 	uint32_t last_sx_ps_downconvert;
1372 	uint32_t last_sx_blend_opt_epsilon;
1373 	uint32_t last_sx_blend_opt_control;
1374 
1375 	/* Whether CP DMA is busy/idle. */
1376 	bool dma_is_busy;
1377 
1378 	/* Conditional rendering info. */
1379 	int predication_type; /* -1: disabled, 0: normal, 1: inverted */
1380 	uint64_t predication_va;
1381 
1382 	/* Inheritance info. */
1383 	VkQueryPipelineStatisticFlags inherited_pipeline_statistics;
1384 
1385 	bool context_roll_without_scissor_emitted;
1386 
1387 	/* SQTT related state. */
1388 	uint32_t current_event_type;
1389 	uint32_t num_events;
1390 	uint32_t num_layout_transitions;
1391 	bool pending_sqtt_barrier_end;
1392 	enum rgp_flush_bits sqtt_flush_bits;
1393 
1394 	uint8_t cb_mip[MAX_RTS];
1395 };
1396 
1397 struct radv_cmd_pool {
1398 	struct vk_object_base                        base;
1399 	VkAllocationCallbacks                        alloc;
1400 	struct list_head                             cmd_buffers;
1401 	struct list_head                             free_cmd_buffers;
1402 	uint32_t queue_family_index;
1403 };
1404 
1405 struct radv_cmd_buffer_upload {
1406 	uint8_t *map;
1407 	unsigned offset;
1408 	uint64_t size;
1409 	struct radeon_winsys_bo *upload_bo;
1410 	struct list_head list;
1411 };
1412 
1413 enum radv_cmd_buffer_status {
1414 	RADV_CMD_BUFFER_STATUS_INVALID,
1415 	RADV_CMD_BUFFER_STATUS_INITIAL,
1416 	RADV_CMD_BUFFER_STATUS_RECORDING,
1417 	RADV_CMD_BUFFER_STATUS_EXECUTABLE,
1418 	RADV_CMD_BUFFER_STATUS_PENDING,
1419 };
1420 
1421 struct radv_cmd_buffer {
1422 	struct vk_object_base                         base;
1423 
1424 	struct radv_device *                          device;
1425 
1426 	struct radv_cmd_pool *                        pool;
1427 	struct list_head                             pool_link;
1428 
1429 	VkCommandBufferUsageFlags                    usage_flags;
1430 	VkCommandBufferLevel                         level;
1431 	enum radv_cmd_buffer_status status;
1432 	struct radeon_cmdbuf *cs;
1433 	struct radv_cmd_state state;
1434 	struct radv_vertex_binding                   vertex_bindings[MAX_VBS];
1435 	struct radv_streamout_binding                streamout_bindings[MAX_SO_BUFFERS];
1436 	uint32_t queue_family_index;
1437 
1438 	uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
1439 	VkShaderStageFlags push_constant_stages;
1440 	struct radv_descriptor_set meta_push_descriptors;
1441 
1442 	struct radv_descriptor_state descriptors[MAX_BIND_POINTS];
1443 
1444 	struct radv_cmd_buffer_upload upload;
1445 
1446 	uint32_t scratch_size_per_wave_needed;
1447 	uint32_t scratch_waves_wanted;
1448 	uint32_t compute_scratch_size_per_wave_needed;
1449 	uint32_t compute_scratch_waves_wanted;
1450 	uint32_t esgs_ring_size_needed;
1451 	uint32_t gsvs_ring_size_needed;
1452 	bool tess_rings_needed;
1453 	bool gds_needed; /* for GFX10 streamout and NGG GS queries */
1454 	bool gds_oa_needed; /* for GFX10 streamout */
1455 	bool sample_positions_needed;
1456 
1457 	VkResult record_result;
1458 
1459 	uint64_t gfx9_fence_va;
1460 	uint32_t gfx9_fence_idx;
1461 	uint64_t gfx9_eop_bug_va;
1462 
1463 	/**
1464 	 * Whether a query pool has been resetted and we have to flush caches.
1465 	 */
1466 	bool pending_reset_query;
1467 
1468 	/**
1469 	 * Bitmask of pending active query flushes.
1470 	 */
1471 	enum radv_cmd_flush_bits active_query_flush_bits;
1472 };
1473 
1474 struct radv_image;
1475 struct radv_image_view;
1476 
1477 bool radv_cmd_buffer_uses_mec(struct radv_cmd_buffer *cmd_buffer);
1478 
1479 void si_emit_graphics(struct radv_device *device,
1480 		      struct radeon_cmdbuf *cs);
1481 void si_emit_compute(struct radv_device *device,
1482 		      struct radeon_cmdbuf *cs);
1483 
1484 void cik_create_gfx_config(struct radv_device *device);
1485 
1486 void si_write_viewport(struct radeon_cmdbuf *cs, int first_vp,
1487 		       int count, const VkViewport *viewports);
1488 void si_write_scissors(struct radeon_cmdbuf *cs, int first,
1489 		       int count, const VkRect2D *scissors,
1490 		       const VkViewport *viewports, bool can_use_guardband);
1491 uint32_t si_get_ia_multi_vgt_param(struct radv_cmd_buffer *cmd_buffer,
1492 				   bool instanced_draw, bool indirect_draw,
1493 				   bool count_from_stream_output,
1494 				   uint32_t draw_vertex_count,
1495 				   unsigned topology);
1496 void si_cs_emit_write_event_eop(struct radeon_cmdbuf *cs,
1497 				enum chip_class chip_class,
1498 				bool is_mec,
1499 				unsigned event, unsigned event_flags,
1500 				unsigned dst_sel, unsigned data_sel,
1501 				uint64_t va,
1502 				uint32_t new_fence,
1503 				uint64_t gfx9_eop_bug_va);
1504 
1505 void radv_cp_wait_mem(struct radeon_cmdbuf *cs, uint32_t op, uint64_t va,
1506 		      uint32_t ref, uint32_t mask);
1507 void si_cs_emit_cache_flush(struct radeon_cmdbuf *cs,
1508 			    enum chip_class chip_class,
1509 			    uint32_t *fence_ptr, uint64_t va,
1510 			    bool is_mec,
1511 			    enum radv_cmd_flush_bits flush_bits,
1512 			    enum rgp_flush_bits *sqtt_flush_bits,
1513 			    uint64_t gfx9_eop_bug_va);
1514 void si_emit_cache_flush(struct radv_cmd_buffer *cmd_buffer);
1515 void si_emit_set_predication_state(struct radv_cmd_buffer *cmd_buffer,
1516 				   bool inverted, uint64_t va);
1517 void si_cp_dma_buffer_copy(struct radv_cmd_buffer *cmd_buffer,
1518 			   uint64_t src_va, uint64_t dest_va,
1519 			   uint64_t size);
1520 void si_cp_dma_prefetch(struct radv_cmd_buffer *cmd_buffer, uint64_t va,
1521                         unsigned size);
1522 void si_cp_dma_clear_buffer(struct radv_cmd_buffer *cmd_buffer, uint64_t va,
1523 			    uint64_t size, unsigned value);
1524 void si_cp_dma_wait_for_idle(struct radv_cmd_buffer *cmd_buffer);
1525 
1526 void radv_set_db_count_control(struct radv_cmd_buffer *cmd_buffer);
1527 bool
1528 radv_cmd_buffer_upload_alloc(struct radv_cmd_buffer *cmd_buffer,
1529 			     unsigned size,
1530 			     unsigned alignment,
1531 			     unsigned *out_offset,
1532 			     void **ptr);
1533 void
1534 radv_cmd_buffer_set_subpass(struct radv_cmd_buffer *cmd_buffer,
1535 			    const struct radv_subpass *subpass);
1536 bool
1537 radv_cmd_buffer_upload_data(struct radv_cmd_buffer *cmd_buffer,
1538 			    unsigned size, unsigned alignmnet,
1539 			    const void *data, unsigned *out_offset);
1540 
1541 void radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer *cmd_buffer);
1542 void radv_cmd_buffer_resolve_subpass(struct radv_cmd_buffer *cmd_buffer);
1543 void radv_cmd_buffer_resolve_subpass_cs(struct radv_cmd_buffer *cmd_buffer);
1544 void radv_depth_stencil_resolve_subpass_cs(struct radv_cmd_buffer *cmd_buffer,
1545 					   VkImageAspectFlags aspects,
1546 					   VkResolveModeFlagBits resolve_mode);
1547 void radv_cmd_buffer_resolve_subpass_fs(struct radv_cmd_buffer *cmd_buffer);
1548 void radv_depth_stencil_resolve_subpass_fs(struct radv_cmd_buffer *cmd_buffer,
1549 					   VkImageAspectFlags aspects,
1550 					   VkResolveModeFlagBits resolve_mode);
1551 void radv_emit_default_sample_locations(struct radeon_cmdbuf *cs, int nr_samples);
1552 unsigned radv_get_default_max_sample_dist(int log_samples);
1553 void radv_device_init_msaa(struct radv_device *device);
1554 
1555 void radv_update_ds_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
1556 				   const struct radv_image_view *iview,
1557 				   VkClearDepthStencilValue ds_clear_value,
1558 				   VkImageAspectFlags aspects);
1559 
1560 void radv_update_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
1561 				      const struct radv_image_view *iview,
1562 				      int cb_idx,
1563 				      uint32_t color_values[2]);
1564 
1565 void radv_update_fce_metadata(struct radv_cmd_buffer *cmd_buffer,
1566 			      struct radv_image *image,
1567 			      const VkImageSubresourceRange *range, bool value);
1568 
1569 void radv_update_dcc_metadata(struct radv_cmd_buffer *cmd_buffer,
1570 			      struct radv_image *image,
1571 			      const VkImageSubresourceRange *range, bool value);
1572 
1573 uint32_t radv_fill_buffer(struct radv_cmd_buffer *cmd_buffer,
1574 			  struct radeon_winsys_bo *bo,
1575 			  uint64_t offset, uint64_t size, uint32_t value);
1576 void radv_cmd_buffer_trace_emit(struct radv_cmd_buffer *cmd_buffer);
1577 bool radv_get_memory_fd(struct radv_device *device,
1578 			struct radv_device_memory *memory,
1579 			int *pFD);
1580 void radv_free_memory(struct radv_device *device,
1581 		      const VkAllocationCallbacks* pAllocator,
1582 		      struct radv_device_memory *mem);
1583 
1584 static inline void
radv_emit_shader_pointer_head(struct radeon_cmdbuf * cs,unsigned sh_offset,unsigned pointer_count,bool use_32bit_pointers)1585 radv_emit_shader_pointer_head(struct radeon_cmdbuf *cs,
1586 			      unsigned sh_offset, unsigned pointer_count,
1587 			      bool use_32bit_pointers)
1588 {
1589 	radeon_emit(cs, PKT3(PKT3_SET_SH_REG, pointer_count * (use_32bit_pointers ? 1 : 2), 0));
1590 	radeon_emit(cs, (sh_offset - SI_SH_REG_OFFSET) >> 2);
1591 }
1592 
1593 static inline void
radv_emit_shader_pointer_body(struct radv_device * device,struct radeon_cmdbuf * cs,uint64_t va,bool use_32bit_pointers)1594 radv_emit_shader_pointer_body(struct radv_device *device,
1595 			      struct radeon_cmdbuf *cs,
1596 			      uint64_t va, bool use_32bit_pointers)
1597 {
1598 	radeon_emit(cs, va);
1599 
1600 	if (use_32bit_pointers) {
1601 		assert(va == 0 ||
1602 		       (va >> 32) == device->physical_device->rad_info.address32_hi);
1603 	} else {
1604 		radeon_emit(cs, va >> 32);
1605 	}
1606 }
1607 
1608 static inline void
radv_emit_shader_pointer(struct radv_device * device,struct radeon_cmdbuf * cs,uint32_t sh_offset,uint64_t va,bool global)1609 radv_emit_shader_pointer(struct radv_device *device,
1610 			 struct radeon_cmdbuf *cs,
1611 			 uint32_t sh_offset, uint64_t va, bool global)
1612 {
1613 	bool use_32bit_pointers = !global;
1614 
1615 	radv_emit_shader_pointer_head(cs, sh_offset, 1, use_32bit_pointers);
1616 	radv_emit_shader_pointer_body(device, cs, va, use_32bit_pointers);
1617 }
1618 
1619 static inline struct radv_descriptor_state *
radv_get_descriptors_state(struct radv_cmd_buffer * cmd_buffer,VkPipelineBindPoint bind_point)1620 radv_get_descriptors_state(struct radv_cmd_buffer *cmd_buffer,
1621 			   VkPipelineBindPoint bind_point)
1622 {
1623 	assert(bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS ||
1624 	       bind_point == VK_PIPELINE_BIND_POINT_COMPUTE);
1625 	return &cmd_buffer->descriptors[bind_point];
1626 }
1627 
1628 /*
1629  * Takes x,y,z as exact numbers of invocations, instead of blocks.
1630  *
1631  * Limitations: Can't call normal dispatch functions without binding or rebinding
1632  *              the compute pipeline.
1633  */
1634 void radv_unaligned_dispatch(
1635 	struct radv_cmd_buffer                      *cmd_buffer,
1636 	uint32_t                                    x,
1637 	uint32_t                                    y,
1638 	uint32_t                                    z);
1639 
1640 struct radv_event {
1641 	struct vk_object_base base;
1642 	struct radeon_winsys_bo *bo;
1643 	uint64_t *map;
1644 };
1645 
1646 struct radv_shader_module;
1647 
1648 #define RADV_HASH_SHADER_NO_NGG              (1 << 0)
1649 #define RADV_HASH_SHADER_CS_WAVE32           (1 << 1)
1650 #define RADV_HASH_SHADER_PS_WAVE32           (1 << 2)
1651 #define RADV_HASH_SHADER_GE_WAVE32           (1 << 3)
1652 #define RADV_HASH_SHADER_LLVM                (1 << 4)
1653 #define RADV_HASH_SHADER_DISCARD_TO_DEMOTE   (1 << 5)
1654 #define RADV_HASH_SHADER_MRT_NAN_FIXUP       (1 << 6)
1655 #define RADV_HASH_SHADER_INVARIANT_GEOM      (1 << 7)
1656 
1657 void
1658 radv_hash_shaders(unsigned char *hash,
1659 		  const VkPipelineShaderStageCreateInfo **stages,
1660 		  const struct radv_pipeline_layout *layout,
1661 		  const struct radv_pipeline_key *key,
1662 		  uint32_t flags);
1663 
1664 static inline gl_shader_stage
vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)1665 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1666 {
1667 	assert(__builtin_popcount(vk_stage) == 1);
1668 	return ffs(vk_stage) - 1;
1669 }
1670 
1671 static inline VkShaderStageFlagBits
mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)1672 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1673 {
1674 	return (1 << mesa_stage);
1675 }
1676 
1677 #define RADV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1678 
1679 #define radv_foreach_stage(stage, stage_bits)				\
1680 	for (gl_shader_stage stage,					\
1681 		     __tmp = (gl_shader_stage)((stage_bits) & RADV_STAGE_MASK);	\
1682 	     stage = __builtin_ffs(__tmp) - 1, __tmp;			\
1683 	     __tmp &= ~(1 << (stage)))
1684 
1685 extern const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS];
1686 unsigned radv_format_meta_fs_key(VkFormat format);
1687 
1688 struct radv_multisample_state {
1689 	uint32_t db_eqaa;
1690 	uint32_t pa_sc_mode_cntl_0;
1691 	uint32_t pa_sc_mode_cntl_1;
1692 	uint32_t pa_sc_aa_config;
1693 	uint32_t pa_sc_aa_mask[2];
1694 	unsigned num_samples;
1695 };
1696 
1697 struct radv_prim_vertex_count {
1698 	uint8_t min;
1699 	uint8_t incr;
1700 };
1701 
1702 struct radv_ia_multi_vgt_param_helpers {
1703 	uint32_t base;
1704 	bool partial_es_wave;
1705 	uint8_t primgroup_size;
1706 	bool ia_switch_on_eoi;
1707 	bool partial_vs_wave;
1708 };
1709 
1710 struct radv_binning_state {
1711 	uint32_t pa_sc_binner_cntl_0;
1712 	uint32_t db_dfsm_control;
1713 };
1714 
1715 #define SI_GS_PER_ES 128
1716 
1717 struct radv_pipeline {
1718 	struct vk_object_base                         base;
1719 	struct radv_device *                          device;
1720 	struct radv_dynamic_state                     dynamic_state;
1721 
1722 	struct radv_pipeline_layout *                 layout;
1723 
1724 	bool					     need_indirect_descriptor_sets;
1725 	struct radv_shader_variant *                 shaders[MESA_SHADER_STAGES];
1726 	struct radv_shader_variant *gs_copy_shader;
1727 	VkShaderStageFlags                           active_stages;
1728 
1729 	struct radeon_cmdbuf                      cs;
1730 	uint32_t                                  ctx_cs_hash;
1731 	struct radeon_cmdbuf                      ctx_cs;
1732 
1733 	uint32_t                                     binding_stride[MAX_VBS];
1734 	uint8_t                                      num_vertex_bindings;
1735 
1736 	uint32_t user_data_0[MESA_SHADER_STAGES];
1737 	union {
1738 		struct {
1739 			struct radv_multisample_state ms;
1740 			struct radv_binning_state binning;
1741 			uint32_t spi_baryc_cntl;
1742 			bool prim_restart_enable;
1743 			unsigned esgs_ring_size;
1744 			unsigned gsvs_ring_size;
1745 			uint32_t vtx_base_sgpr;
1746 			struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param;
1747 			uint8_t vtx_emit_num;
1748  			bool can_use_guardband;
1749 			uint32_t needed_dynamic_state;
1750 			bool disable_out_of_order_rast_for_occlusion;
1751 			unsigned tess_patch_control_points;
1752 			unsigned pa_su_sc_mode_cntl;
1753 			unsigned db_depth_control;
1754 			bool uses_dynamic_stride;
1755 
1756 			/* Used for rbplus */
1757 			uint32_t col_format;
1758 			uint32_t cb_target_mask;
1759 		} graphics;
1760 	};
1761 
1762 	unsigned max_waves;
1763 	unsigned scratch_bytes_per_wave;
1764 
1765 	/* Not NULL if graphics pipeline uses streamout. */
1766 	struct radv_shader_variant *streamout_shader;
1767 };
1768 
radv_pipeline_has_gs(const struct radv_pipeline * pipeline)1769 static inline bool radv_pipeline_has_gs(const struct radv_pipeline *pipeline)
1770 {
1771 	return pipeline->shaders[MESA_SHADER_GEOMETRY] ? true : false;
1772 }
1773 
radv_pipeline_has_tess(const struct radv_pipeline * pipeline)1774 static inline bool radv_pipeline_has_tess(const struct radv_pipeline *pipeline)
1775 {
1776 	return pipeline->shaders[MESA_SHADER_TESS_CTRL] ? true : false;
1777 }
1778 
1779 bool radv_pipeline_has_ngg(const struct radv_pipeline *pipeline);
1780 
1781 bool radv_pipeline_has_ngg_passthrough(const struct radv_pipeline *pipeline);
1782 
1783 bool radv_pipeline_has_gs_copy_shader(const struct radv_pipeline *pipeline);
1784 
1785 struct radv_userdata_info *radv_lookup_user_sgpr(struct radv_pipeline *pipeline,
1786 						 gl_shader_stage stage,
1787 						 int idx);
1788 
1789 struct radv_shader_variant *radv_get_shader(const struct radv_pipeline *pipeline,
1790 					    gl_shader_stage stage);
1791 
1792 struct radv_graphics_pipeline_create_info {
1793 	bool use_rectlist;
1794 	bool db_depth_clear;
1795 	bool db_stencil_clear;
1796 	bool db_depth_disable_expclear;
1797 	bool db_stencil_disable_expclear;
1798 	bool depth_compress_disable;
1799 	bool stencil_compress_disable;
1800 	bool resummarize_enable;
1801 	uint32_t custom_blend_mode;
1802 };
1803 
1804 VkResult
1805 radv_graphics_pipeline_create(VkDevice device,
1806 			      VkPipelineCache cache,
1807 			      const VkGraphicsPipelineCreateInfo *pCreateInfo,
1808 			      const struct radv_graphics_pipeline_create_info *extra,
1809 			      const VkAllocationCallbacks *alloc,
1810 			      VkPipeline *pPipeline);
1811 
1812 struct radv_binning_settings {
1813 	unsigned context_states_per_bin; /* allowed range: [1, 6] */
1814 	unsigned persistent_states_per_bin; /* allowed range: [1, 32] */
1815 	unsigned fpovs_per_batch; /* allowed range: [0, 255], 0 = unlimited */
1816 };
1817 
1818 struct radv_binning_settings
1819 radv_get_binning_settings(const struct radv_physical_device *pdev);
1820 
1821 struct vk_format_description;
1822 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc,
1823 					  int first_non_void);
1824 uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
1825 					 int first_non_void);
1826 bool radv_is_buffer_format_supported(VkFormat format, bool *scaled);
1827 uint32_t radv_translate_colorformat(VkFormat format);
1828 uint32_t radv_translate_color_numformat(VkFormat format,
1829 					const struct vk_format_description *desc,
1830 					int first_non_void);
1831 uint32_t radv_colorformat_endian_swap(uint32_t colorformat);
1832 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap);
1833 uint32_t radv_translate_dbformat(VkFormat format);
1834 uint32_t radv_translate_tex_dataformat(VkFormat format,
1835 				       const struct vk_format_description *desc,
1836 				       int first_non_void);
1837 uint32_t radv_translate_tex_numformat(VkFormat format,
1838 				      const struct vk_format_description *desc,
1839 				      int first_non_void);
1840 bool radv_format_pack_clear_color(VkFormat format,
1841 				  uint32_t clear_vals[2],
1842 				  VkClearColorValue *value);
1843 bool radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdevice,
1844                                           VkFormat format, bool *blendable);
1845 bool radv_dcc_formats_compatible(VkFormat format1,
1846                                  VkFormat format2);
1847 bool radv_device_supports_etc(struct radv_physical_device *physical_device);
1848 
1849 struct radv_image_plane {
1850 	VkFormat format;
1851 	struct radeon_surf surface;
1852 	uint64_t offset;
1853 };
1854 
1855 struct radv_image {
1856 	struct vk_object_base base;
1857 	VkImageType type;
1858 	/* The original VkFormat provided by the client.  This may not match any
1859 	 * of the actual surface formats.
1860 	 */
1861 	VkFormat vk_format;
1862 	VkImageAspectFlags aspects;
1863 	VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1864 	struct ac_surf_info info;
1865 	VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1866 	VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1867 
1868 	VkDeviceSize size;
1869 	uint32_t alignment;
1870 
1871 	unsigned queue_family_mask;
1872 	bool exclusive;
1873 	bool shareable;
1874 
1875 	/* Set when bound */
1876 	struct radeon_winsys_bo *bo;
1877 	VkDeviceSize offset;
1878 	bool tc_compatible_htile;
1879 	bool tc_compatible_cmask;
1880 
1881 	uint64_t clear_value_offset;
1882 	uint64_t fce_pred_offset;
1883 	uint64_t dcc_pred_offset;
1884 
1885 	/*
1886 	 * Metadata for the TC-compat zrange workaround. If the 32-bit value
1887 	 * stored at this offset is UINT_MAX, the driver will emit
1888 	 * DB_Z_INFO.ZRANGE_PRECISION=0, otherwise it will skip the
1889 	 * SET_CONTEXT_REG packet.
1890 	 */
1891 	uint64_t tc_compat_zrange_offset;
1892 
1893 	/* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1894 	VkDeviceMemory owned_memory;
1895 
1896 	unsigned plane_count;
1897 	struct radv_image_plane planes[0];
1898 };
1899 
1900 /* Whether the image has a htile  that is known consistent with the contents of
1901  * the image and is allowed to be in compressed form.
1902  *
1903  * If this is false reads that don't use the htile should be able to return
1904  * correct results.
1905  */
1906 bool radv_layout_is_htile_compressed(const struct radv_device *device,
1907 				     const struct radv_image *image,
1908                                      VkImageLayout layout,
1909                                      bool in_render_loop,
1910                                      unsigned queue_mask);
1911 
1912 bool radv_layout_can_fast_clear(const struct radv_image *image,
1913 			        VkImageLayout layout,
1914 			        bool in_render_loop,
1915 			        unsigned queue_mask);
1916 
1917 bool radv_layout_dcc_compressed(const struct radv_device *device,
1918 				const struct radv_image *image,
1919 			        VkImageLayout layout,
1920 			        bool in_render_loop,
1921 			        unsigned queue_mask);
1922 
1923 /**
1924  * Return whether the image has CMASK metadata for color surfaces.
1925  */
1926 static inline bool
radv_image_has_cmask(const struct radv_image * image)1927 radv_image_has_cmask(const struct radv_image *image)
1928 {
1929 	return image->planes[0].surface.cmask_offset;
1930 }
1931 
1932 /**
1933  * Return whether the image has FMASK metadata for color surfaces.
1934  */
1935 static inline bool
radv_image_has_fmask(const struct radv_image * image)1936 radv_image_has_fmask(const struct radv_image *image)
1937 {
1938 	return image->planes[0].surface.fmask_offset;
1939 }
1940 
1941 /**
1942  * Return whether the image has DCC metadata for color surfaces.
1943  */
1944 static inline bool
radv_image_has_dcc(const struct radv_image * image)1945 radv_image_has_dcc(const struct radv_image *image)
1946 {
1947 	return image->planes[0].surface.dcc_size;
1948 }
1949 
1950 /**
1951  * Return whether the image is TC-compatible CMASK.
1952  */
1953 static inline bool
radv_image_is_tc_compat_cmask(const struct radv_image * image)1954 radv_image_is_tc_compat_cmask(const struct radv_image *image)
1955 {
1956 	return radv_image_has_fmask(image) && image->tc_compatible_cmask;
1957 }
1958 
1959 /**
1960  * Return whether DCC metadata is enabled for a level.
1961  */
1962 static inline bool
radv_dcc_enabled(const struct radv_image * image,unsigned level)1963 radv_dcc_enabled(const struct radv_image *image, unsigned level)
1964 {
1965 	return radv_image_has_dcc(image) &&
1966 	       level < image->planes[0].surface.num_dcc_levels;
1967 }
1968 
1969 /**
1970  * Return whether the image has CB metadata.
1971  */
1972 static inline bool
radv_image_has_CB_metadata(const struct radv_image * image)1973 radv_image_has_CB_metadata(const struct radv_image *image)
1974 {
1975 	return radv_image_has_cmask(image) ||
1976 	       radv_image_has_fmask(image) ||
1977 	       radv_image_has_dcc(image);
1978 }
1979 
1980 /**
1981  * Return whether the image has HTILE metadata for depth surfaces.
1982  */
1983 static inline bool
radv_image_has_htile(const struct radv_image * image)1984 radv_image_has_htile(const struct radv_image *image)
1985 {
1986 	return image->planes[0].surface.htile_size;
1987 }
1988 
1989 /**
1990  * Return whether HTILE metadata is enabled for a level.
1991  */
1992 static inline bool
radv_htile_enabled(const struct radv_image * image,unsigned level)1993 radv_htile_enabled(const struct radv_image *image, unsigned level)
1994 {
1995 	return radv_image_has_htile(image) && level == 0;
1996 }
1997 
1998 /**
1999  * Return whether the image is TC-compatible HTILE.
2000  */
2001 static inline bool
radv_image_is_tc_compat_htile(const struct radv_image * image)2002 radv_image_is_tc_compat_htile(const struct radv_image *image)
2003 {
2004 	return radv_image_has_htile(image) && image->tc_compatible_htile;
2005 }
2006 
2007 static inline uint64_t
radv_image_get_fast_clear_va(const struct radv_image * image,uint32_t base_level)2008 radv_image_get_fast_clear_va(const struct radv_image *image,
2009 			     uint32_t base_level)
2010 {
2011 	uint64_t va = radv_buffer_get_va(image->bo);
2012 	va += image->offset + image->clear_value_offset + base_level * 8;
2013 	return va;
2014 }
2015 
2016 static inline uint64_t
radv_image_get_fce_pred_va(const struct radv_image * image,uint32_t base_level)2017 radv_image_get_fce_pred_va(const struct radv_image *image,
2018 			   uint32_t base_level)
2019 {
2020 	uint64_t va = radv_buffer_get_va(image->bo);
2021 	va += image->offset + image->fce_pred_offset + base_level * 8;
2022 	return va;
2023 }
2024 
2025 static inline uint64_t
radv_image_get_dcc_pred_va(const struct radv_image * image,uint32_t base_level)2026 radv_image_get_dcc_pred_va(const struct radv_image *image,
2027 			   uint32_t base_level)
2028 {
2029 	uint64_t va = radv_buffer_get_va(image->bo);
2030 	va += image->offset + image->dcc_pred_offset + base_level * 8;
2031 	return va;
2032 }
2033 
2034 static inline uint64_t
radv_get_tc_compat_zrange_va(const struct radv_image * image,uint32_t base_level)2035 radv_get_tc_compat_zrange_va(const struct radv_image *image,
2036 			     uint32_t base_level)
2037 {
2038 	uint64_t va = radv_buffer_get_va(image->bo);
2039 	va += image->offset + image->tc_compat_zrange_offset + base_level * 4;
2040 	return va;
2041 }
2042 
2043 static inline uint64_t
radv_get_ds_clear_value_va(const struct radv_image * image,uint32_t base_level)2044 radv_get_ds_clear_value_va(const struct radv_image *image,
2045 			   uint32_t base_level)
2046 {
2047 	uint64_t va = radv_buffer_get_va(image->bo);
2048 	va += image->offset + image->clear_value_offset + base_level * 8;
2049 	return va;
2050 }
2051 
2052 unsigned radv_image_queue_family_mask(const struct radv_image *image, uint32_t family, uint32_t queue_family);
2053 
2054 static inline uint32_t
radv_get_layerCount(const struct radv_image * image,const VkImageSubresourceRange * range)2055 radv_get_layerCount(const struct radv_image *image,
2056 		    const VkImageSubresourceRange *range)
2057 {
2058 	return range->layerCount == VK_REMAINING_ARRAY_LAYERS ?
2059 		image->info.array_size - range->baseArrayLayer : range->layerCount;
2060 }
2061 
2062 static inline uint32_t
radv_get_levelCount(const struct radv_image * image,const VkImageSubresourceRange * range)2063 radv_get_levelCount(const struct radv_image *image,
2064 		    const VkImageSubresourceRange *range)
2065 {
2066 	return range->levelCount == VK_REMAINING_MIP_LEVELS ?
2067 		image->info.levels - range->baseMipLevel : range->levelCount;
2068 }
2069 
2070 struct radeon_bo_metadata;
2071 void
2072 radv_init_metadata(struct radv_device *device,
2073 		   struct radv_image *image,
2074 		   struct radeon_bo_metadata *metadata);
2075 
2076 void
2077 radv_image_override_offset_stride(struct radv_device *device,
2078                                   struct radv_image *image,
2079                                   uint64_t offset, uint32_t stride);
2080 
2081 union radv_descriptor {
2082 	struct {
2083 		uint32_t plane0_descriptor[8];
2084 		uint32_t fmask_descriptor[8];
2085 	};
2086 	struct {
2087 		uint32_t plane_descriptors[3][8];
2088 	};
2089 };
2090 
2091 struct radv_image_view {
2092 	struct vk_object_base base;
2093 	struct radv_image *image; /**< VkImageViewCreateInfo::image */
2094 	struct radeon_winsys_bo *bo;
2095 
2096 	VkImageViewType type;
2097 	VkImageAspectFlags aspect_mask;
2098 	VkFormat vk_format;
2099 	unsigned plane_id;
2100 	bool multiple_planes;
2101 	uint32_t base_layer;
2102 	uint32_t layer_count;
2103 	uint32_t base_mip;
2104 	uint32_t level_count;
2105 	VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
2106 
2107 	union radv_descriptor descriptor;
2108 
2109 	/* Descriptor for use as a storage image as opposed to a sampled image.
2110 	 * This has a few differences for cube maps (e.g. type).
2111 	 */
2112 	union radv_descriptor storage_descriptor;
2113 };
2114 
2115 struct radv_image_create_info {
2116 	const VkImageCreateInfo *vk_info;
2117 	bool scanout;
2118 	bool no_metadata_planes;
2119 	const struct radeon_bo_metadata *bo_metadata;
2120 };
2121 
2122 VkResult
2123 radv_image_create_layout(struct radv_device *device,
2124                          struct radv_image_create_info create_info,
2125                          struct radv_image *image);
2126 
2127 VkResult radv_image_create(VkDevice _device,
2128 			   const struct radv_image_create_info *info,
2129 			   const VkAllocationCallbacks* alloc,
2130 			   VkImage *pImage);
2131 
2132 bool vi_alpha_is_on_msb(struct radv_device *device, VkFormat format);
2133 
2134 VkResult
2135 radv_image_from_gralloc(VkDevice device_h,
2136                        const VkImageCreateInfo *base_info,
2137                        const VkNativeBufferANDROID *gralloc_info,
2138                        const VkAllocationCallbacks *alloc,
2139                        VkImage *out_image_h);
2140 uint64_t
2141 radv_ahb_usage_from_vk_usage(const VkImageCreateFlags vk_create,
2142                              const VkImageUsageFlags vk_usage);
2143 VkResult
2144 radv_import_ahb_memory(struct radv_device *device,
2145                        struct radv_device_memory *mem,
2146                        unsigned priority,
2147                        const VkImportAndroidHardwareBufferInfoANDROID *info);
2148 VkResult
2149 radv_create_ahb_memory(struct radv_device *device,
2150                        struct radv_device_memory *mem,
2151                        unsigned priority,
2152                        const VkMemoryAllocateInfo *pAllocateInfo);
2153 
2154 VkFormat
2155 radv_select_android_external_format(const void *next, VkFormat default_format);
2156 
2157 bool radv_android_gralloc_supports_format(VkFormat format, VkImageUsageFlagBits usage);
2158 
2159 struct radv_image_view_extra_create_info {
2160 	bool disable_compression;
2161 };
2162 
2163 void radv_image_view_init(struct radv_image_view *view,
2164 			  struct radv_device *device,
2165 			  const VkImageViewCreateInfo *pCreateInfo,
2166 			  const struct radv_image_view_extra_create_info* extra_create_info);
2167 
2168 VkFormat radv_get_aspect_format(struct radv_image *image, VkImageAspectFlags mask);
2169 
2170 struct radv_sampler_ycbcr_conversion {
2171 	struct vk_object_base base;
2172 	VkFormat format;
2173 	VkSamplerYcbcrModelConversion ycbcr_model;
2174 	VkSamplerYcbcrRange ycbcr_range;
2175 	VkComponentMapping components;
2176 	VkChromaLocation chroma_offsets[2];
2177 	VkFilter chroma_filter;
2178 };
2179 
2180 struct radv_buffer_view {
2181 	struct vk_object_base base;
2182 	struct radeon_winsys_bo *bo;
2183 	VkFormat vk_format;
2184 	uint64_t range; /**< VkBufferViewCreateInfo::range */
2185 	uint32_t state[4];
2186 };
2187 void radv_buffer_view_init(struct radv_buffer_view *view,
2188 			   struct radv_device *device,
2189 			   const VkBufferViewCreateInfo* pCreateInfo);
2190 
2191 static inline struct VkExtent3D
radv_sanitize_image_extent(const VkImageType imageType,const struct VkExtent3D imageExtent)2192 radv_sanitize_image_extent(const VkImageType imageType,
2193 			   const struct VkExtent3D imageExtent)
2194 {
2195 	switch (imageType) {
2196 	case VK_IMAGE_TYPE_1D:
2197 		return (VkExtent3D) { imageExtent.width, 1, 1 };
2198 	case VK_IMAGE_TYPE_2D:
2199 		return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
2200 	case VK_IMAGE_TYPE_3D:
2201 		return imageExtent;
2202 	default:
2203 		unreachable("invalid image type");
2204 	}
2205 }
2206 
2207 static inline struct VkOffset3D
radv_sanitize_image_offset(const VkImageType imageType,const struct VkOffset3D imageOffset)2208 radv_sanitize_image_offset(const VkImageType imageType,
2209 			   const struct VkOffset3D imageOffset)
2210 {
2211 	switch (imageType) {
2212 	case VK_IMAGE_TYPE_1D:
2213 		return (VkOffset3D) { imageOffset.x, 0, 0 };
2214 	case VK_IMAGE_TYPE_2D:
2215 		return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
2216 	case VK_IMAGE_TYPE_3D:
2217 		return imageOffset;
2218 	default:
2219 		unreachable("invalid image type");
2220 	}
2221 }
2222 
2223 static inline bool
radv_image_extent_compare(const struct radv_image * image,const VkExtent3D * extent)2224 radv_image_extent_compare(const struct radv_image *image,
2225 			  const VkExtent3D *extent)
2226 {
2227 	if (extent->width != image->info.width ||
2228 	    extent->height != image->info.height ||
2229 	    extent->depth != image->info.depth)
2230 		return false;
2231 	return true;
2232 }
2233 
2234 struct radv_sampler {
2235 	struct vk_object_base base;
2236 	uint32_t state[4];
2237 	struct radv_sampler_ycbcr_conversion *ycbcr_sampler;
2238 	uint32_t border_color_slot;
2239 };
2240 
2241 struct radv_framebuffer {
2242 	struct vk_object_base                        base;
2243 	uint32_t                                     width;
2244 	uint32_t                                     height;
2245 	uint32_t                                     layers;
2246 
2247 	uint32_t                                     attachment_count;
2248 	struct radv_image_view                       *attachments[0];
2249 };
2250 
2251 struct radv_subpass_barrier {
2252 	VkPipelineStageFlags src_stage_mask;
2253 	VkAccessFlags        src_access_mask;
2254 	VkAccessFlags        dst_access_mask;
2255 };
2256 
2257 void radv_subpass_barrier(struct radv_cmd_buffer *cmd_buffer,
2258 			  const struct radv_subpass_barrier *barrier);
2259 
2260 struct radv_subpass_attachment {
2261 	uint32_t         attachment;
2262 	VkImageLayout    layout;
2263 	VkImageLayout    stencil_layout;
2264 	bool             in_render_loop;
2265 };
2266 
2267 struct radv_subpass {
2268 	uint32_t                                     attachment_count;
2269 	struct radv_subpass_attachment *             attachments;
2270 
2271 	uint32_t                                     input_count;
2272 	uint32_t                                     color_count;
2273 	struct radv_subpass_attachment *             input_attachments;
2274 	struct radv_subpass_attachment *             color_attachments;
2275 	struct radv_subpass_attachment *             resolve_attachments;
2276 	struct radv_subpass_attachment *             depth_stencil_attachment;
2277 	struct radv_subpass_attachment *             ds_resolve_attachment;
2278 	VkResolveModeFlagBits                        depth_resolve_mode;
2279 	VkResolveModeFlagBits                        stencil_resolve_mode;
2280 
2281 	/** Subpass has at least one color resolve attachment */
2282 	bool                                         has_color_resolve;
2283 
2284 	/** Subpass has at least one color attachment */
2285 	bool                                         has_color_att;
2286 
2287 	struct radv_subpass_barrier                  start_barrier;
2288 
2289 	uint32_t                                     view_mask;
2290 
2291 	VkSampleCountFlagBits                        color_sample_count;
2292 	VkSampleCountFlagBits                        depth_sample_count;
2293 	VkSampleCountFlagBits                        max_sample_count;
2294 };
2295 
2296 uint32_t
2297 radv_get_subpass_id(struct radv_cmd_buffer *cmd_buffer);
2298 
2299 struct radv_render_pass_attachment {
2300 	VkFormat                                     format;
2301 	uint32_t                                     samples;
2302 	VkAttachmentLoadOp                           load_op;
2303 	VkAttachmentLoadOp                           stencil_load_op;
2304 	VkImageLayout                                initial_layout;
2305 	VkImageLayout                                final_layout;
2306 	VkImageLayout                                stencil_initial_layout;
2307 	VkImageLayout                                stencil_final_layout;
2308 
2309 	/* The subpass id in which the attachment will be used first/last. */
2310 	uint32_t				     first_subpass_idx;
2311 	uint32_t                                     last_subpass_idx;
2312 };
2313 
2314 struct radv_render_pass {
2315 	struct vk_object_base                        base;
2316 	uint32_t                                     attachment_count;
2317 	uint32_t                                     subpass_count;
2318 	struct radv_subpass_attachment *             subpass_attachments;
2319 	struct radv_render_pass_attachment *         attachments;
2320 	struct radv_subpass_barrier                  end_barrier;
2321 	struct radv_subpass                          subpasses[0];
2322 };
2323 
2324 VkResult radv_device_init_meta(struct radv_device *device);
2325 void radv_device_finish_meta(struct radv_device *device);
2326 
2327 struct radv_query_pool {
2328 	struct vk_object_base base;
2329 	struct radeon_winsys_bo *bo;
2330 	uint32_t stride;
2331 	uint32_t availability_offset;
2332 	uint64_t size;
2333 	char *ptr;
2334 	VkQueryType type;
2335 	uint32_t pipeline_stats_mask;
2336 };
2337 
2338 typedef enum {
2339 	RADV_SEMAPHORE_NONE,
2340 	RADV_SEMAPHORE_WINSYS,
2341 	RADV_SEMAPHORE_SYNCOBJ,
2342 	RADV_SEMAPHORE_TIMELINE_SYNCOBJ,
2343 	RADV_SEMAPHORE_TIMELINE,
2344 } radv_semaphore_kind;
2345 
2346 struct radv_deferred_queue_submission;
2347 
2348 struct radv_timeline_waiter {
2349 	struct list_head list;
2350 	struct radv_deferred_queue_submission *submission;
2351 	uint64_t value;
2352 };
2353 
2354 struct radv_timeline_point {
2355 	struct list_head list;
2356 
2357 	uint64_t value;
2358 	uint32_t syncobj;
2359 
2360 	/* Separate from the list to accomodate CPU wait being async, as well
2361 	 * as prevent point deletion during submission. */
2362 	unsigned wait_count;
2363 };
2364 
2365 struct radv_timeline {
2366 	/* Using a pthread mutex to be compatible with condition variables. */
2367 	pthread_mutex_t mutex;
2368 
2369 	uint64_t highest_signaled;
2370 	uint64_t highest_submitted;
2371 
2372 	struct list_head points;
2373 
2374 	/* Keep free points on hand so we do not have to recreate syncobjs all
2375 	 * the time. */
2376 	struct list_head free_points;
2377 
2378 	/* Submissions that are deferred waiting for a specific value to be
2379 	 * submitted. */
2380 	struct list_head waiters;
2381 };
2382 
2383 struct radv_timeline_syncobj {
2384 	/* Keep syncobj first, so common-code can just handle this as
2385 	 * non-timeline syncobj. */
2386 	uint32_t syncobj;
2387 	uint64_t max_point; /* max submitted point. */
2388 };
2389 
2390 struct radv_semaphore_part {
2391 	radv_semaphore_kind kind;
2392 	union {
2393 		uint32_t syncobj;
2394 		struct radeon_winsys_sem *ws_sem;
2395 		struct radv_timeline timeline;
2396 		struct radv_timeline_syncobj timeline_syncobj;
2397 	};
2398 };
2399 
2400 struct radv_semaphore {
2401 	struct vk_object_base base;
2402 	struct radv_semaphore_part permanent;
2403 	struct radv_semaphore_part temporary;
2404 };
2405 
2406 bool radv_queue_internal_submit(struct radv_queue *queue,
2407 				struct radeon_cmdbuf *cs);
2408 
2409 void radv_set_descriptor_set(struct radv_cmd_buffer *cmd_buffer,
2410 			     VkPipelineBindPoint bind_point,
2411 			     struct radv_descriptor_set *set,
2412 			     unsigned idx);
2413 
2414 void
2415 radv_update_descriptor_sets(struct radv_device *device,
2416                             struct radv_cmd_buffer *cmd_buffer,
2417                             VkDescriptorSet overrideSet,
2418                             uint32_t descriptorWriteCount,
2419                             const VkWriteDescriptorSet *pDescriptorWrites,
2420                             uint32_t descriptorCopyCount,
2421                             const VkCopyDescriptorSet *pDescriptorCopies);
2422 
2423 void
2424 radv_update_descriptor_set_with_template(struct radv_device *device,
2425                                          struct radv_cmd_buffer *cmd_buffer,
2426                                          struct radv_descriptor_set *set,
2427                                          VkDescriptorUpdateTemplate descriptorUpdateTemplate,
2428                                          const void *pData);
2429 
2430 void radv_meta_push_descriptor_set(struct radv_cmd_buffer *cmd_buffer,
2431                                    VkPipelineBindPoint pipelineBindPoint,
2432                                    VkPipelineLayout _layout,
2433                                    uint32_t set,
2434                                    uint32_t descriptorWriteCount,
2435                                    const VkWriteDescriptorSet *pDescriptorWrites);
2436 
2437 void radv_initialize_dcc(struct radv_cmd_buffer *cmd_buffer,
2438 			 struct radv_image *image,
2439 			 const VkImageSubresourceRange *range, uint32_t value);
2440 
2441 void radv_initialize_fmask(struct radv_cmd_buffer *cmd_buffer,
2442 			   struct radv_image *image,
2443 			   const VkImageSubresourceRange *range);
2444 
2445 typedef enum {
2446 	RADV_FENCE_NONE,
2447 	RADV_FENCE_WINSYS,
2448 	RADV_FENCE_SYNCOBJ,
2449 } radv_fence_kind;
2450 
2451 struct radv_fence_part {
2452 	radv_fence_kind kind;
2453 
2454 	union {
2455 		/* AMDGPU winsys fence. */
2456 		struct radeon_winsys_fence *fence;
2457 
2458 		/* DRM syncobj handle for syncobj-based fences. */
2459 		uint32_t syncobj;
2460 	};
2461 };
2462 
2463 struct radv_fence {
2464 	struct vk_object_base base;
2465 	struct radv_fence_part permanent;
2466 	struct radv_fence_part temporary;
2467 };
2468 
2469 /* radv_nir_to_llvm.c */
2470 struct radv_shader_args;
2471 
2472 void llvm_compile_shader(struct radv_device *device,
2473 			 unsigned shader_count,
2474 			 struct nir_shader *const *shaders,
2475 			 struct radv_shader_binary **binary,
2476 			 struct radv_shader_args *args);
2477 
2478 unsigned radv_nir_get_max_workgroup_size(enum chip_class chip_class,
2479 					 gl_shader_stage stage,
2480 					 const struct nir_shader *nir);
2481 
2482 /* radv_shader_info.h */
2483 struct radv_shader_info;
2484 struct radv_shader_variant_key;
2485 
2486 void radv_nir_shader_info_pass(const struct nir_shader *nir,
2487 			       const struct radv_pipeline_layout *layout,
2488 			       const struct radv_shader_variant_key *key,
2489 			       struct radv_shader_info *info);
2490 
2491 void radv_nir_shader_info_init(struct radv_shader_info *info);
2492 
2493 /* radv_sqtt.c */
2494 struct radv_thread_trace_info {
2495 	uint32_t cur_offset;
2496 	uint32_t trace_status;
2497 	union {
2498 		uint32_t gfx9_write_counter;
2499 		uint32_t gfx10_dropped_cntr;
2500 	};
2501 };
2502 
2503 struct radv_thread_trace_se {
2504 	struct radv_thread_trace_info info;
2505 	void *data_ptr;
2506 	uint32_t shader_engine;
2507 	uint32_t compute_unit;
2508 };
2509 
2510 struct radv_thread_trace {
2511 	uint32_t num_traces;
2512 	struct radv_thread_trace_se traces[4];
2513 };
2514 
2515 bool radv_thread_trace_init(struct radv_device *device);
2516 void radv_thread_trace_finish(struct radv_device *device);
2517 bool radv_begin_thread_trace(struct radv_queue *queue);
2518 bool radv_end_thread_trace(struct radv_queue *queue);
2519 bool radv_get_thread_trace(struct radv_queue *queue,
2520 			   struct radv_thread_trace *thread_trace);
2521 void radv_emit_thread_trace_userdata(const struct radv_device *device,
2522 				     struct radeon_cmdbuf *cs,
2523 				     const void *data, uint32_t num_dwords);
2524 
2525 /* radv_rgp.c */
2526 int radv_dump_thread_trace(struct radv_device *device,
2527 			   const struct radv_thread_trace *trace);
2528 
2529 /* radv_sqtt_layer_.c */
2530 struct radv_barrier_data {
2531 	union {
2532 		struct {
2533 			uint16_t depth_stencil_expand : 1;
2534 			uint16_t htile_hiz_range_expand : 1;
2535 			uint16_t depth_stencil_resummarize : 1;
2536 			uint16_t dcc_decompress : 1;
2537 			uint16_t fmask_decompress : 1;
2538 			uint16_t fast_clear_eliminate : 1;
2539 			uint16_t fmask_color_expand : 1;
2540 			uint16_t init_mask_ram : 1;
2541 			uint16_t reserved : 8;
2542 		};
2543 		uint16_t all;
2544 	} layout_transitions;
2545 };
2546 
2547 /**
2548  * Value for the reason field of an RGP barrier start marker originating from
2549  * the Vulkan client (does not include PAL-defined values). (Table 15)
2550  */
2551 enum rgp_barrier_reason {
2552 	RGP_BARRIER_UNKNOWN_REASON = 0xFFFFFFFF,
2553 
2554 	/* External app-generated barrier reasons, i.e. API synchronization
2555 	 * commands Range of valid values: [0x00000001 ... 0x7FFFFFFF].
2556 	 */
2557 	RGP_BARRIER_EXTERNAL_CMD_PIPELINE_BARRIER = 0x00000001,
2558 	RGP_BARRIER_EXTERNAL_RENDER_PASS_SYNC	  = 0x00000002,
2559 	RGP_BARRIER_EXTERNAL_CMD_WAIT_EVENTS	  = 0x00000003,
2560 
2561 	/* Internal barrier reasons, i.e. implicit synchronization inserted by
2562 	 * the Vulkan driver Range of valid values: [0xC0000000 ... 0xFFFFFFFE].
2563 	 */
2564 	RGP_BARRIER_INTERNAL_BASE                             = 0xC0000000,
2565 	RGP_BARRIER_INTERNAL_PRE_RESET_QUERY_POOL_SYNC        = RGP_BARRIER_INTERNAL_BASE + 0,
2566 	RGP_BARRIER_INTERNAL_POST_RESET_QUERY_POOL_SYNC       = RGP_BARRIER_INTERNAL_BASE + 1,
2567 	RGP_BARRIER_INTERNAL_GPU_EVENT_RECYCLE_STALL	      = RGP_BARRIER_INTERNAL_BASE + 2,
2568 	RGP_BARRIER_INTERNAL_PRE_COPY_QUERY_POOL_RESULTS_SYNC = RGP_BARRIER_INTERNAL_BASE + 3
2569 };
2570 
2571 void radv_describe_begin_cmd_buffer(struct radv_cmd_buffer *cmd_buffer);
2572 void radv_describe_end_cmd_buffer(struct radv_cmd_buffer *cmd_buffer);
2573 void radv_describe_draw(struct radv_cmd_buffer *cmd_buffer);
2574 void radv_describe_dispatch(struct radv_cmd_buffer *cmd_buffer, int x, int y, int z);
2575 void radv_describe_begin_render_pass_clear(struct radv_cmd_buffer *cmd_buffer,
2576 					   VkImageAspectFlagBits aspects);
2577 void radv_describe_end_render_pass_clear(struct radv_cmd_buffer *cmd_buffer);
2578 void radv_describe_barrier_start(struct radv_cmd_buffer *cmd_buffer,
2579 				 enum rgp_barrier_reason reason);
2580 void radv_describe_barrier_end(struct radv_cmd_buffer *cmd_buffer);
2581 void radv_describe_barrier_end_delayed(struct radv_cmd_buffer *cmd_buffer);
2582 void radv_describe_layout_transition(struct radv_cmd_buffer *cmd_buffer,
2583 				     const struct radv_barrier_data *barrier);
2584 
2585 struct radeon_winsys_sem;
2586 
2587 uint64_t radv_get_current_time(void);
2588 
2589 static inline uint32_t
si_conv_gl_prim_to_vertices(unsigned gl_prim)2590 si_conv_gl_prim_to_vertices(unsigned gl_prim)
2591 {
2592 	switch (gl_prim) {
2593 	case 0: /* GL_POINTS */
2594 		return 1;
2595 	case 1: /* GL_LINES */
2596 	case 3: /* GL_LINE_STRIP */
2597 		return 2;
2598 	case 4: /* GL_TRIANGLES */
2599 	case 5: /* GL_TRIANGLE_STRIP */
2600 		return 3;
2601 	case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
2602 		return 4;
2603 	case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
2604 		return 6;
2605 	case 7: /* GL_QUADS */
2606 		return V_028A6C_TRISTRIP;
2607 	default:
2608 		assert(0);
2609 		return 0;
2610 	}
2611 }
2612 
2613 void radv_cmd_buffer_begin_render_pass(struct radv_cmd_buffer *cmd_buffer,
2614 				       const VkRenderPassBeginInfo *pRenderPassBegin);
2615 void radv_cmd_buffer_end_render_pass(struct radv_cmd_buffer *cmd_buffer);
2616 
si_translate_prim(unsigned topology)2617 static inline uint32_t si_translate_prim(unsigned topology)
2618 {
2619 	switch (topology) {
2620 	case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
2621 		return V_008958_DI_PT_POINTLIST;
2622 	case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
2623 		return V_008958_DI_PT_LINELIST;
2624 	case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
2625 		return V_008958_DI_PT_LINESTRIP;
2626 	case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
2627 		return V_008958_DI_PT_TRILIST;
2628 	case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
2629 		return V_008958_DI_PT_TRISTRIP;
2630 	case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
2631 		return V_008958_DI_PT_TRIFAN;
2632 	case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
2633 		return V_008958_DI_PT_LINELIST_ADJ;
2634 	case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
2635 		return V_008958_DI_PT_LINESTRIP_ADJ;
2636 	case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
2637 		return V_008958_DI_PT_TRILIST_ADJ;
2638 	case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
2639 		return V_008958_DI_PT_TRISTRIP_ADJ;
2640 	case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
2641 		return V_008958_DI_PT_PATCH;
2642 	default:
2643 		assert(0);
2644 		return 0;
2645 	}
2646 }
2647 
si_translate_stencil_op(enum VkStencilOp op)2648 static inline uint32_t si_translate_stencil_op(enum VkStencilOp op)
2649 {
2650 	switch (op) {
2651 	case VK_STENCIL_OP_KEEP:
2652 		return V_02842C_STENCIL_KEEP;
2653 	case VK_STENCIL_OP_ZERO:
2654 		return V_02842C_STENCIL_ZERO;
2655 	case VK_STENCIL_OP_REPLACE:
2656 		return V_02842C_STENCIL_REPLACE_TEST;
2657 	case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
2658 		return V_02842C_STENCIL_ADD_CLAMP;
2659 	case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
2660 		return V_02842C_STENCIL_SUB_CLAMP;
2661 	case VK_STENCIL_OP_INVERT:
2662 		return V_02842C_STENCIL_INVERT;
2663 	case VK_STENCIL_OP_INCREMENT_AND_WRAP:
2664 		return V_02842C_STENCIL_ADD_WRAP;
2665 	case VK_STENCIL_OP_DECREMENT_AND_WRAP:
2666 		return V_02842C_STENCIL_SUB_WRAP;
2667 	default:
2668 		return 0;
2669 	}
2670 }
2671 
2672 /**
2673  * Helper used for debugging compiler issues by enabling/disabling LLVM for a
2674  * specific shader stage (developers only).
2675  */
2676 static inline bool
radv_use_llvm_for_stage(struct radv_device * device,UNUSED gl_shader_stage stage)2677 radv_use_llvm_for_stage(struct radv_device *device, UNUSED gl_shader_stage stage)
2678 {
2679 	return device->physical_device->use_llvm;
2680 }
2681 
2682 #define RADV_DEFINE_HANDLE_CASTS(__radv_type, __VkType)		\
2683 								\
2684 	static inline struct __radv_type *			\
2685 	__radv_type ## _from_handle(__VkType _handle)		\
2686 	{							\
2687 		return (struct __radv_type *) _handle;		\
2688 	}							\
2689 								\
2690 	static inline __VkType					\
2691 	__radv_type ## _to_handle(struct __radv_type *_obj)	\
2692 	{							\
2693 		return (__VkType) _obj;				\
2694 	}
2695 
2696 #define RADV_DEFINE_NONDISP_HANDLE_CASTS(__radv_type, __VkType)		\
2697 									\
2698 	static inline struct __radv_type *				\
2699 	__radv_type ## _from_handle(__VkType _handle)			\
2700 	{								\
2701 		return (struct __radv_type *)(uintptr_t) _handle;	\
2702 	}								\
2703 									\
2704 	static inline __VkType						\
2705 	__radv_type ## _to_handle(struct __radv_type *_obj)		\
2706 	{								\
2707 		return (__VkType)(uintptr_t) _obj;			\
2708 	}
2709 
2710 #define RADV_FROM_HANDLE(__radv_type, __name, __handle)			\
2711 	struct __radv_type *__name = __radv_type ## _from_handle(__handle)
2712 
2713 RADV_DEFINE_HANDLE_CASTS(radv_cmd_buffer, VkCommandBuffer)
2714 RADV_DEFINE_HANDLE_CASTS(radv_device, VkDevice)
2715 RADV_DEFINE_HANDLE_CASTS(radv_instance, VkInstance)
2716 RADV_DEFINE_HANDLE_CASTS(radv_physical_device, VkPhysicalDevice)
2717 RADV_DEFINE_HANDLE_CASTS(radv_queue, VkQueue)
2718 
2719 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_cmd_pool, VkCommandPool)
2720 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_buffer, VkBuffer)
2721 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_buffer_view, VkBufferView)
2722 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_pool, VkDescriptorPool)
2723 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_set, VkDescriptorSet)
2724 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_set_layout, VkDescriptorSetLayout)
2725 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_descriptor_update_template, VkDescriptorUpdateTemplate)
2726 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_device_memory, VkDeviceMemory)
2727 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_fence, VkFence)
2728 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_event, VkEvent)
2729 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_framebuffer, VkFramebuffer)
2730 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_image, VkImage)
2731 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_image_view, VkImageView);
2732 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline_cache, VkPipelineCache)
2733 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline, VkPipeline)
2734 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_pipeline_layout, VkPipelineLayout)
2735 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_query_pool, VkQueryPool)
2736 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_render_pass, VkRenderPass)
2737 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_sampler, VkSampler)
2738 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_sampler_ycbcr_conversion, VkSamplerYcbcrConversion)
2739 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_shader_module, VkShaderModule)
2740 RADV_DEFINE_NONDISP_HANDLE_CASTS(radv_semaphore, VkSemaphore)
2741 
2742 #endif /* RADV_PRIVATE_H */
2743