turnip: Remove some radv leftovers.
[mesa.git] / src / freedreno / vulkan / tu_private.h
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifndef TU_PRIVATE_H
29 #define TU_PRIVATE_H
30
31 #include <assert.h>
32 #include <pthread.h>
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_VALGRIND
39 #include <memcheck.h>
40 #include <valgrind.h>
41 #define VG(x) x
42 #else
43 #define VG(x)
44 #endif
45
46 #include "c11/threads.h"
47 #include "compiler/shader_enums.h"
48 #include "main/macros.h"
49 #include "util/list.h"
50 #include "util/macros.h"
51 #include "vk_alloc.h"
52 #include "vk_debug_report.h"
53
54 #include "tu_descriptor_set.h"
55 #include "tu_extensions.h"
56
57 /* Pre-declarations needed for WSI entrypoints */
58 struct wl_surface;
59 struct wl_display;
60 typedef struct xcb_connection_t xcb_connection_t;
61 typedef uint32_t xcb_visualid_t;
62 typedef uint32_t xcb_window_t;
63
64 #include <vulkan/vk_android_native_buffer.h>
65 #include <vulkan/vk_icd.h>
66 #include <vulkan/vulkan.h>
67 #include <vulkan/vulkan_intel.h>
68
69 #include "drm/freedreno_ringbuffer.h"
70 #include "tu_entrypoints.h"
71
72 #define MAX_VBS 32
73 #define MAX_VERTEX_ATTRIBS 32
74 #define MAX_RTS 8
75 #define MAX_VIEWPORTS 16
76 #define MAX_SCISSORS 16
77 #define MAX_DISCARD_RECTANGLES 4
78 #define MAX_PUSH_CONSTANTS_SIZE 128
79 #define MAX_PUSH_DESCRIPTORS 32
80 #define MAX_DYNAMIC_UNIFORM_BUFFERS 16
81 #define MAX_DYNAMIC_STORAGE_BUFFERS 8
82 #define MAX_DYNAMIC_BUFFERS \
83 (MAX_DYNAMIC_UNIFORM_BUFFERS + MAX_DYNAMIC_STORAGE_BUFFERS)
84 #define MAX_SAMPLES_LOG2 4
85 #define NUM_META_FS_KEYS 13
86 #define TU_MAX_DRM_DEVICES 8
87 #define MAX_VIEWS 8
88
89 #define NUM_DEPTH_CLEAR_PIPELINES 3
90
91 /*
92 * This is the point we switch from using CP to compute shader
93 * for certain buffer operations.
94 */
95 #define TU_BUFFER_OPS_CS_THRESHOLD 4096
96
97 enum tu_mem_heap
98 {
99 TU_MEM_HEAP_VRAM,
100 TU_MEM_HEAP_VRAM_CPU_ACCESS,
101 TU_MEM_HEAP_GTT,
102 TU_MEM_HEAP_COUNT
103 };
104
105 enum tu_mem_type
106 {
107 TU_MEM_TYPE_VRAM,
108 TU_MEM_TYPE_GTT_WRITE_COMBINE,
109 TU_MEM_TYPE_VRAM_CPU_ACCESS,
110 TU_MEM_TYPE_GTT_CACHED,
111 TU_MEM_TYPE_COUNT
112 };
113
114 #define tu_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
115
116 static inline uint32_t
117 align_u32(uint32_t v, uint32_t a)
118 {
119 assert(a != 0 && a == (a & -a));
120 return (v + a - 1) & ~(a - 1);
121 }
122
123 static inline uint32_t
124 align_u32_npot(uint32_t v, uint32_t a)
125 {
126 return (v + a - 1) / a * a;
127 }
128
129 static inline uint64_t
130 align_u64(uint64_t v, uint64_t a)
131 {
132 assert(a != 0 && a == (a & -a));
133 return (v + a - 1) & ~(a - 1);
134 }
135
136 static inline int32_t
137 align_i32(int32_t v, int32_t a)
138 {
139 assert(a != 0 && a == (a & -a));
140 return (v + a - 1) & ~(a - 1);
141 }
142
143 /** Alignment must be a power of 2. */
144 static inline bool
145 tu_is_aligned(uintmax_t n, uintmax_t a)
146 {
147 assert(a == (a & -a));
148 return (n & (a - 1)) == 0;
149 }
150
151 static inline uint32_t
152 round_up_u32(uint32_t v, uint32_t a)
153 {
154 return (v + a - 1) / a;
155 }
156
157 static inline uint64_t
158 round_up_u64(uint64_t v, uint64_t a)
159 {
160 return (v + a - 1) / a;
161 }
162
163 static inline uint32_t
164 tu_minify(uint32_t n, uint32_t levels)
165 {
166 if (unlikely(n == 0))
167 return 0;
168 else
169 return MAX2(n >> levels, 1);
170 }
171 static inline float
172 tu_clamp_f(float f, float min, float max)
173 {
174 assert(min < max);
175
176 if (f > max)
177 return max;
178 else if (f < min)
179 return min;
180 else
181 return f;
182 }
183
184 static inline bool
185 tu_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
186 {
187 if (*inout_mask & clear_mask) {
188 *inout_mask &= ~clear_mask;
189 return true;
190 } else {
191 return false;
192 }
193 }
194
195 #define for_each_bit(b, dword) \
196 for (uint32_t __dword = (dword); \
197 (b) = __builtin_ffs(__dword) - 1, __dword; __dword &= ~(1 << (b)))
198
199 #define typed_memcpy(dest, src, count) \
200 ({ \
201 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
202 memcpy((dest), (src), (count) * sizeof(*(src))); \
203 })
204
205 /* Whenever we generate an error, pass it through this function. Useful for
206 * debugging, where we can break on it. Only call at error site, not when
207 * propagating errors. Might be useful to plug in a stack trace here.
208 */
209
210 struct tu_instance;
211
212 VkResult
213 __vk_errorf(struct tu_instance *instance,
214 VkResult error,
215 const char *file,
216 int line,
217 const char *format,
218 ...);
219
220 #define vk_error(instance, error) \
221 __vk_errorf(instance, error, __FILE__, __LINE__, NULL);
222 #define vk_errorf(instance, error, format, ...) \
223 __vk_errorf(instance, error, __FILE__, __LINE__, format, ##__VA_ARGS__);
224
225 void
226 __tu_finishme(const char *file, int line, const char *format, ...)
227 tu_printflike(3, 4);
228 void
229 tu_loge(const char *format, ...) tu_printflike(1, 2);
230 void
231 tu_loge_v(const char *format, va_list va);
232 void
233 tu_logi(const char *format, ...) tu_printflike(1, 2);
234 void
235 tu_logi_v(const char *format, va_list va);
236
237 /**
238 * Print a FINISHME message, including its source location.
239 */
240 #define tu_finishme(format, ...) \
241 do { \
242 static bool reported = false; \
243 if (!reported) { \
244 __tu_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
245 reported = true; \
246 } \
247 } while (0)
248
249 /* A non-fatal assert. Useful for debugging. */
250 #ifdef DEBUG
251 #define tu_assert(x) \
252 ({ \
253 if (unlikely(!(x))) \
254 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
255 })
256 #else
257 #define tu_assert(x)
258 #endif
259
260 /* Suppress -Wunused in stub functions */
261 #define tu_use_args(...) __tu_use_args(0, ##__VA_ARGS__)
262 static inline void
263 __tu_use_args(int ignore, ...)
264 {
265 }
266
267 #define tu_stub() \
268 do { \
269 tu_finishme("stub %s", __func__); \
270 } while (0)
271
272 void *
273 tu_lookup_entrypoint_unchecked(const char *name);
274 void *
275 tu_lookup_entrypoint_checked(
276 const char *name,
277 uint32_t core_version,
278 const struct tu_instance_extension_table *instance,
279 const struct tu_device_extension_table *device);
280
281 struct tu_physical_device
282 {
283 VK_LOADER_DATA _loader_data;
284
285 struct tu_instance *instance;
286
287 char path[20];
288 char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
289 uint8_t driver_uuid[VK_UUID_SIZE];
290 uint8_t device_uuid[VK_UUID_SIZE];
291 uint8_t cache_uuid[VK_UUID_SIZE];
292
293 int local_fd;
294 int master_fd;
295
296 struct fd_device *drm_device;
297 unsigned gpu_id;
298 uint32_t gmem_size;
299
300 /* This is the drivers on-disk cache used as a fallback as opposed to
301 * the pipeline cache defined by apps.
302 */
303 struct disk_cache *disk_cache;
304
305 struct tu_device_extension_table supported_extensions;
306 };
307
308 enum tu_debug_flags
309 {
310 TU_DEBUG_STARTUP = 1 << 0,
311 };
312
313 struct tu_instance
314 {
315 VK_LOADER_DATA _loader_data;
316
317 VkAllocationCallbacks alloc;
318
319 uint32_t api_version;
320 int physical_device_count;
321 struct tu_physical_device physical_devices[TU_MAX_DRM_DEVICES];
322
323 enum tu_debug_flags debug_flags;
324
325 struct vk_debug_report_instance debug_report_callbacks;
326
327 struct tu_instance_extension_table enabled_extensions;
328 };
329
330 bool
331 tu_instance_extension_supported(const char *name);
332 uint32_t
333 tu_physical_device_api_version(struct tu_physical_device *dev);
334 bool
335 tu_physical_device_extension_supported(struct tu_physical_device *dev,
336 const char *name);
337
338 struct cache_entry;
339
340 struct tu_pipeline_cache
341 {
342 struct tu_device *device;
343 pthread_mutex_t mutex;
344
345 uint32_t total_size;
346 uint32_t table_size;
347 uint32_t kernel_count;
348 struct cache_entry **hash_table;
349 bool modified;
350
351 VkAllocationCallbacks alloc;
352 };
353
354 struct tu_pipeline_key
355 {
356 };
357
358 void
359 tu_pipeline_cache_init(struct tu_pipeline_cache *cache,
360 struct tu_device *device);
361 void
362 tu_pipeline_cache_finish(struct tu_pipeline_cache *cache);
363 void
364 tu_pipeline_cache_load(struct tu_pipeline_cache *cache,
365 const void *data,
366 size_t size);
367
368 struct tu_shader_variant;
369
370 bool
371 tu_create_shader_variants_from_pipeline_cache(
372 struct tu_device *device,
373 struct tu_pipeline_cache *cache,
374 const unsigned char *sha1,
375 struct tu_shader_variant **variants);
376
377 void
378 tu_pipeline_cache_insert_shaders(struct tu_device *device,
379 struct tu_pipeline_cache *cache,
380 const unsigned char *sha1,
381 struct tu_shader_variant **variants,
382 const void *const *codes,
383 const unsigned *code_sizes);
384
385 struct tu_meta_state
386 {
387 VkAllocationCallbacks alloc;
388
389 struct tu_pipeline_cache cache;
390 };
391
392 /* queue types */
393 #define TU_QUEUE_GENERAL 0
394
395 #define TU_MAX_QUEUE_FAMILIES 1
396
397 struct tu_queue
398 {
399 VK_LOADER_DATA _loader_data;
400 struct tu_device *device;
401 uint32_t queue_family_index;
402 int queue_idx;
403 VkDeviceQueueCreateFlags flags;
404 };
405
406 struct tu_bo_list
407 {
408 unsigned capacity;
409 pthread_mutex_t mutex;
410 };
411
412 struct tu_device
413 {
414 VK_LOADER_DATA _loader_data;
415
416 VkAllocationCallbacks alloc;
417
418 struct tu_instance *instance;
419
420 struct tu_meta_state meta_state;
421
422 struct tu_queue *queues[TU_MAX_QUEUE_FAMILIES];
423 int queue_count[TU_MAX_QUEUE_FAMILIES];
424
425 struct tu_physical_device *physical_device;
426
427 /* Backup in-memory cache to be used if the app doesn't provide one */
428 struct tu_pipeline_cache *mem_cache;
429
430 struct list_head shader_slabs;
431 mtx_t shader_slab_mutex;
432
433 struct tu_device_extension_table enabled_extensions;
434
435 /* Whether the driver uses a global BO list. */
436 bool use_global_bo_list;
437
438 struct tu_bo_list bo_list;
439 };
440
441 struct tu_bo
442 {
443 uint32_t gem_handle;
444 uint64_t size;
445 uint64_t offset;
446 uint64_t iova;
447 void *map;
448 };
449
450 VkResult
451 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size);
452 void
453 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
454 VkResult
455 tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
456
457 struct tu_device_memory
458 {
459 struct tu_bo bo;
460 VkDeviceSize size;
461
462 /* for dedicated allocations */
463 struct tu_image *image;
464 struct tu_buffer *buffer;
465
466 uint32_t type_index;
467 void *map;
468 void *user_ptr;
469 };
470
471 struct tu_descriptor_range
472 {
473 uint64_t va;
474 uint32_t size;
475 };
476
477 struct tu_descriptor_set
478 {
479 const struct tu_descriptor_set_layout *layout;
480 uint32_t size;
481
482 uint64_t va;
483 uint32_t *mapped_ptr;
484 struct tu_descriptor_range *dynamic_descriptors;
485 };
486
487 struct tu_push_descriptor_set
488 {
489 struct tu_descriptor_set set;
490 uint32_t capacity;
491 };
492
493 struct tu_descriptor_pool_entry
494 {
495 uint32_t offset;
496 uint32_t size;
497 struct tu_descriptor_set *set;
498 };
499
500 struct tu_descriptor_pool
501 {
502 uint8_t *mapped_ptr;
503 uint64_t current_offset;
504 uint64_t size;
505
506 uint8_t *host_memory_base;
507 uint8_t *host_memory_ptr;
508 uint8_t *host_memory_end;
509
510 uint32_t entry_count;
511 uint32_t max_entry_count;
512 struct tu_descriptor_pool_entry entries[0];
513 };
514
515 struct tu_descriptor_update_template_entry
516 {
517 VkDescriptorType descriptor_type;
518
519 /* The number of descriptors to update */
520 uint32_t descriptor_count;
521
522 /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
523 */
524 uint32_t dst_offset;
525
526 /* In dwords. Not valid/used for dynamic descriptors */
527 uint32_t dst_stride;
528
529 uint32_t buffer_offset;
530
531 /* Only valid for combined image samplers and samplers */
532 uint16_t has_sampler;
533
534 /* In bytes */
535 size_t src_offset;
536 size_t src_stride;
537
538 /* For push descriptors */
539 const uint32_t *immutable_samplers;
540 };
541
542 struct tu_descriptor_update_template
543 {
544 uint32_t entry_count;
545 VkPipelineBindPoint bind_point;
546 struct tu_descriptor_update_template_entry entry[0];
547 };
548
549 struct tu_buffer
550 {
551 VkDeviceSize size;
552
553 VkBufferUsageFlags usage;
554 VkBufferCreateFlags flags;
555 };
556
557 enum tu_dynamic_state_bits
558 {
559 TU_DYNAMIC_VIEWPORT = 1 << 0,
560 TU_DYNAMIC_SCISSOR = 1 << 1,
561 TU_DYNAMIC_LINE_WIDTH = 1 << 2,
562 TU_DYNAMIC_DEPTH_BIAS = 1 << 3,
563 TU_DYNAMIC_BLEND_CONSTANTS = 1 << 4,
564 TU_DYNAMIC_DEPTH_BOUNDS = 1 << 5,
565 TU_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6,
566 TU_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7,
567 TU_DYNAMIC_STENCIL_REFERENCE = 1 << 8,
568 TU_DYNAMIC_DISCARD_RECTANGLE = 1 << 9,
569 TU_DYNAMIC_ALL = (1 << 10) - 1,
570 };
571
572 struct tu_vertex_binding
573 {
574 struct tu_buffer *buffer;
575 VkDeviceSize offset;
576 };
577
578 struct tu_viewport_state
579 {
580 uint32_t count;
581 VkViewport viewports[MAX_VIEWPORTS];
582 };
583
584 struct tu_scissor_state
585 {
586 uint32_t count;
587 VkRect2D scissors[MAX_SCISSORS];
588 };
589
590 struct tu_discard_rectangle_state
591 {
592 uint32_t count;
593 VkRect2D rectangles[MAX_DISCARD_RECTANGLES];
594 };
595
596 struct tu_dynamic_state
597 {
598 /**
599 * Bitmask of (1 << VK_DYNAMIC_STATE_*).
600 * Defines the set of saved dynamic state.
601 */
602 uint32_t mask;
603
604 struct tu_viewport_state viewport;
605
606 struct tu_scissor_state scissor;
607
608 float line_width;
609
610 struct
611 {
612 float bias;
613 float clamp;
614 float slope;
615 } depth_bias;
616
617 float blend_constants[4];
618
619 struct
620 {
621 float min;
622 float max;
623 } depth_bounds;
624
625 struct
626 {
627 uint32_t front;
628 uint32_t back;
629 } stencil_compare_mask;
630
631 struct
632 {
633 uint32_t front;
634 uint32_t back;
635 } stencil_write_mask;
636
637 struct
638 {
639 uint32_t front;
640 uint32_t back;
641 } stencil_reference;
642
643 struct tu_discard_rectangle_state discard_rectangle;
644 };
645
646 extern const struct tu_dynamic_state default_dynamic_state;
647
648 const char *
649 tu_get_debug_option_name(int id);
650
651 const char *
652 tu_get_perftest_option_name(int id);
653
654 /**
655 * Attachment state when recording a renderpass instance.
656 *
657 * The clear value is valid only if there exists a pending clear.
658 */
659 struct tu_attachment_state
660 {
661 VkImageAspectFlags pending_clear_aspects;
662 uint32_t cleared_views;
663 VkClearValue clear_value;
664 VkImageLayout current_layout;
665 };
666
667 struct tu_descriptor_state
668 {
669 struct tu_descriptor_set *sets[MAX_SETS];
670 uint32_t dirty;
671 uint32_t valid;
672 struct tu_push_descriptor_set push_set;
673 bool push_dirty;
674 uint32_t dynamic_buffers[4 * MAX_DYNAMIC_BUFFERS];
675 };
676
677 struct tu_cmd_state
678 {
679 /* Vertex descriptors */
680 uint64_t vb_va;
681 unsigned vb_size;
682
683 struct tu_dynamic_state dynamic;
684
685 /* Index buffer */
686 struct tu_buffer *index_buffer;
687 uint64_t index_offset;
688 uint32_t index_type;
689 uint32_t max_index_count;
690 uint64_t index_va;
691 };
692
693 struct tu_cmd_pool
694 {
695 VkAllocationCallbacks alloc;
696 struct list_head cmd_buffers;
697 struct list_head free_cmd_buffers;
698 uint32_t queue_family_index;
699 };
700
701 struct tu_cmd_buffer_upload
702 {
703 uint8_t *map;
704 unsigned offset;
705 uint64_t size;
706 struct list_head list;
707 };
708
709 enum tu_cmd_buffer_status
710 {
711 TU_CMD_BUFFER_STATUS_INVALID,
712 TU_CMD_BUFFER_STATUS_INITIAL,
713 TU_CMD_BUFFER_STATUS_RECORDING,
714 TU_CMD_BUFFER_STATUS_EXECUTABLE,
715 TU_CMD_BUFFER_STATUS_PENDING,
716 };
717
718 struct tu_cmd_buffer
719 {
720 VK_LOADER_DATA _loader_data;
721
722 struct tu_device *device;
723
724 struct tu_cmd_pool *pool;
725 struct list_head pool_link;
726
727 VkCommandBufferUsageFlags usage_flags;
728 VkCommandBufferLevel level;
729 enum tu_cmd_buffer_status status;
730
731 struct tu_cmd_state state;
732 struct tu_vertex_binding vertex_bindings[MAX_VBS];
733 uint32_t queue_family_index;
734
735 uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
736 VkShaderStageFlags push_constant_stages;
737 struct tu_descriptor_set meta_push_descriptors;
738
739 struct tu_descriptor_state descriptors[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
740
741 struct tu_cmd_buffer_upload upload;
742
743 uint32_t scratch_size_needed;
744 uint32_t compute_scratch_size_needed;
745 uint32_t esgs_ring_size_needed;
746 uint32_t gsvs_ring_size_needed;
747 bool tess_rings_needed;
748 bool sample_positions_needed;
749
750 VkResult record_result;
751
752 /**
753 * Whether a query pool has been resetted and we have to flush caches.
754 */
755 bool pending_reset_query;
756 };
757
758 bool
759 tu_get_memory_fd(struct tu_device *device,
760 struct tu_device_memory *memory,
761 int *pFD);
762
763 /*
764 * Takes x,y,z as exact numbers of invocations, instead of blocks.
765 *
766 * Limitations: Can't call normal dispatch functions without binding or
767 * rebinding
768 * the compute pipeline.
769 */
770 void
771 tu_unaligned_dispatch(struct tu_cmd_buffer *cmd_buffer,
772 uint32_t x,
773 uint32_t y,
774 uint32_t z);
775
776 struct tu_event
777 {
778 uint64_t *map;
779 };
780
781 struct tu_shader_module;
782
783 #define TU_HASH_SHADER_IS_GEOM_COPY_SHADER (1 << 0)
784 #define TU_HASH_SHADER_SISCHED (1 << 1)
785 #define TU_HASH_SHADER_UNSAFE_MATH (1 << 2)
786 void
787 tu_hash_shaders(unsigned char *hash,
788 const VkPipelineShaderStageCreateInfo **stages,
789 const struct tu_pipeline_layout *layout,
790 const struct tu_pipeline_key *key,
791 uint32_t flags);
792
793 static inline gl_shader_stage
794 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
795 {
796 assert(__builtin_popcount(vk_stage) == 1);
797 return ffs(vk_stage) - 1;
798 }
799
800 static inline VkShaderStageFlagBits
801 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
802 {
803 return (1 << mesa_stage);
804 }
805
806 #define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
807
808 #define tu_foreach_stage(stage, stage_bits) \
809 for (gl_shader_stage stage, \
810 __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK); \
811 stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
812
813 struct tu_shader_module
814 {
815 struct nir_shader *nir;
816 unsigned char sha1[20];
817 uint32_t size;
818 char data[0];
819 };
820
821 struct tu_pipeline
822 {
823 struct tu_device *device;
824 struct tu_dynamic_state dynamic_state;
825
826 struct tu_pipeline_layout *layout;
827
828 bool need_indirect_descriptor_sets;
829 VkShaderStageFlags active_stages;
830 };
831
832 struct tu_userdata_info *
833 tu_lookup_user_sgpr(struct tu_pipeline *pipeline,
834 gl_shader_stage stage,
835 int idx);
836
837 struct tu_shader_variant *
838 tu_get_shader(struct tu_pipeline *pipeline, gl_shader_stage stage);
839
840 struct tu_graphics_pipeline_create_info
841 {
842 bool use_rectlist;
843 bool db_depth_clear;
844 bool db_stencil_clear;
845 bool db_depth_disable_expclear;
846 bool db_stencil_disable_expclear;
847 bool db_flush_depth_inplace;
848 bool db_flush_stencil_inplace;
849 bool db_resummarize;
850 uint32_t custom_blend_mode;
851 };
852
853 VkResult
854 tu_graphics_pipeline_create(
855 VkDevice device,
856 VkPipelineCache cache,
857 const VkGraphicsPipelineCreateInfo *pCreateInfo,
858 const struct tu_graphics_pipeline_create_info *extra,
859 const VkAllocationCallbacks *alloc,
860 VkPipeline *pPipeline);
861
862 struct vk_format_description;
863 uint32_t
864 tu_translate_buffer_dataformat(const struct vk_format_description *desc,
865 int first_non_void);
866 uint32_t
867 tu_translate_buffer_numformat(const struct vk_format_description *desc,
868 int first_non_void);
869 uint32_t
870 tu_translate_colorformat(VkFormat format);
871 uint32_t
872 tu_translate_color_numformat(VkFormat format,
873 const struct vk_format_description *desc,
874 int first_non_void);
875 uint32_t
876 tu_colorformat_endian_swap(uint32_t colorformat);
877 unsigned
878 tu_translate_colorswap(VkFormat format, bool do_endian_swap);
879 uint32_t
880 tu_translate_dbformat(VkFormat format);
881 uint32_t
882 tu_translate_tex_dataformat(VkFormat format,
883 const struct vk_format_description *desc,
884 int first_non_void);
885 uint32_t
886 tu_translate_tex_numformat(VkFormat format,
887 const struct vk_format_description *desc,
888 int first_non_void);
889 bool
890 tu_format_pack_clear_color(VkFormat format,
891 uint32_t clear_vals[2],
892 VkClearColorValue *value);
893 bool
894 tu_is_colorbuffer_format_supported(VkFormat format, bool *blendable);
895 bool
896 tu_dcc_formats_compatible(VkFormat format1, VkFormat format2);
897
898 struct tu_image_level
899 {
900 VkDeviceSize offset;
901 VkDeviceSize size;
902 uint32_t pitch;
903 };
904
905 struct tu_image
906 {
907 VkImageType type;
908 /* The original VkFormat provided by the client. This may not match any
909 * of the actual surface formats.
910 */
911 VkFormat vk_format;
912 VkImageAspectFlags aspects;
913 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
914 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
915 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
916 VkExtent3D extent;
917
918 VkDeviceSize size;
919 uint32_t alignment;
920
921 /* memory layout */
922 VkDeviceSize layer_size;
923 struct tu_image_level levels[15];
924 unsigned tile_mode;
925
926 unsigned queue_family_mask;
927 bool exclusive;
928 bool shareable;
929
930 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
931 VkDeviceMemory owned_memory;
932 };
933
934 unsigned
935 tu_image_queue_family_mask(const struct tu_image *image,
936 uint32_t family,
937 uint32_t queue_family);
938
939 static inline uint32_t
940 tu_get_layerCount(const struct tu_image *image,
941 const VkImageSubresourceRange *range)
942 {
943 abort();
944 }
945
946 static inline uint32_t
947 tu_get_levelCount(const struct tu_image *image,
948 const VkImageSubresourceRange *range)
949 {
950 abort();
951 }
952
953 struct tu_image_view
954 {
955 struct tu_image *image; /**< VkImageViewCreateInfo::image */
956
957 VkImageViewType type;
958 VkImageAspectFlags aspect_mask;
959 VkFormat vk_format;
960 uint32_t base_layer;
961 uint32_t layer_count;
962 uint32_t base_mip;
963 uint32_t level_count;
964 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
965
966 uint32_t descriptor[16];
967
968 /* Descriptor for use as a storage image as opposed to a sampled image.
969 * This has a few differences for cube maps (e.g. type).
970 */
971 uint32_t storage_descriptor[16];
972 };
973
974 struct tu_sampler
975 {
976 };
977
978 struct tu_image_create_info
979 {
980 const VkImageCreateInfo *vk_info;
981 bool scanout;
982 bool no_metadata_planes;
983 };
984
985 VkResult
986 tu_image_create(VkDevice _device,
987 const struct tu_image_create_info *info,
988 const VkAllocationCallbacks *alloc,
989 VkImage *pImage);
990
991 VkResult
992 tu_image_from_gralloc(VkDevice device_h,
993 const VkImageCreateInfo *base_info,
994 const VkNativeBufferANDROID *gralloc_info,
995 const VkAllocationCallbacks *alloc,
996 VkImage *out_image_h);
997
998 void
999 tu_image_view_init(struct tu_image_view *view,
1000 struct tu_device *device,
1001 const VkImageViewCreateInfo *pCreateInfo);
1002
1003 struct tu_buffer_view
1004 {
1005 VkFormat vk_format;
1006 uint64_t range; /**< VkBufferViewCreateInfo::range */
1007 uint32_t state[4];
1008 };
1009 void
1010 tu_buffer_view_init(struct tu_buffer_view *view,
1011 struct tu_device *device,
1012 const VkBufferViewCreateInfo *pCreateInfo);
1013
1014 static inline struct VkExtent3D
1015 tu_sanitize_image_extent(const VkImageType imageType,
1016 const struct VkExtent3D imageExtent)
1017 {
1018 switch (imageType) {
1019 case VK_IMAGE_TYPE_1D:
1020 return (VkExtent3D) { imageExtent.width, 1, 1 };
1021 case VK_IMAGE_TYPE_2D:
1022 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1023 case VK_IMAGE_TYPE_3D:
1024 return imageExtent;
1025 default:
1026 unreachable("invalid image type");
1027 }
1028 }
1029
1030 static inline struct VkOffset3D
1031 tu_sanitize_image_offset(const VkImageType imageType,
1032 const struct VkOffset3D imageOffset)
1033 {
1034 switch (imageType) {
1035 case VK_IMAGE_TYPE_1D:
1036 return (VkOffset3D) { imageOffset.x, 0, 0 };
1037 case VK_IMAGE_TYPE_2D:
1038 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
1039 case VK_IMAGE_TYPE_3D:
1040 return imageOffset;
1041 default:
1042 unreachable("invalid image type");
1043 }
1044 }
1045
1046 struct tu_attachment_info
1047 {
1048 struct tu_image_view *attachment;
1049 };
1050
1051 struct tu_framebuffer
1052 {
1053 uint32_t width;
1054 uint32_t height;
1055 uint32_t layers;
1056
1057 uint32_t attachment_count;
1058 struct tu_attachment_info attachments[0];
1059 };
1060
1061 struct tu_subpass_barrier
1062 {
1063 VkPipelineStageFlags src_stage_mask;
1064 VkAccessFlags src_access_mask;
1065 VkAccessFlags dst_access_mask;
1066 };
1067
1068 void
1069 tu_subpass_barrier(struct tu_cmd_buffer *cmd_buffer,
1070 const struct tu_subpass_barrier *barrier);
1071
1072 struct tu_subpass_attachment
1073 {
1074 uint32_t attachment;
1075 VkImageLayout layout;
1076 };
1077
1078 struct tu_subpass
1079 {
1080 uint32_t input_count;
1081 uint32_t color_count;
1082 struct tu_subpass_attachment *input_attachments;
1083 struct tu_subpass_attachment *color_attachments;
1084 struct tu_subpass_attachment *resolve_attachments;
1085 struct tu_subpass_attachment depth_stencil_attachment;
1086
1087 /** Subpass has at least one resolve attachment */
1088 bool has_resolve;
1089
1090 struct tu_subpass_barrier start_barrier;
1091
1092 uint32_t view_mask;
1093 VkSampleCountFlagBits max_sample_count;
1094 };
1095
1096 struct tu_render_pass_attachment
1097 {
1098 VkFormat format;
1099 uint32_t samples;
1100 VkAttachmentLoadOp load_op;
1101 VkAttachmentLoadOp stencil_load_op;
1102 VkImageLayout initial_layout;
1103 VkImageLayout final_layout;
1104 uint32_t view_mask;
1105 };
1106
1107 struct tu_render_pass
1108 {
1109 uint32_t attachment_count;
1110 uint32_t subpass_count;
1111 struct tu_subpass_attachment *subpass_attachments;
1112 struct tu_render_pass_attachment *attachments;
1113 struct tu_subpass_barrier end_barrier;
1114 struct tu_subpass subpasses[0];
1115 };
1116
1117 VkResult
1118 tu_device_init_meta(struct tu_device *device);
1119 void
1120 tu_device_finish_meta(struct tu_device *device);
1121
1122 struct tu_query_pool
1123 {
1124 uint32_t stride;
1125 uint32_t availability_offset;
1126 uint64_t size;
1127 char *ptr;
1128 VkQueryType type;
1129 uint32_t pipeline_stats_mask;
1130 };
1131
1132 struct tu_semaphore
1133 {
1134 uint32_t syncobj;
1135 uint32_t temp_syncobj;
1136 };
1137
1138 void
1139 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1140 VkPipelineBindPoint bind_point,
1141 struct tu_descriptor_set *set,
1142 unsigned idx);
1143
1144 void
1145 tu_update_descriptor_sets(struct tu_device *device,
1146 struct tu_cmd_buffer *cmd_buffer,
1147 VkDescriptorSet overrideSet,
1148 uint32_t descriptorWriteCount,
1149 const VkWriteDescriptorSet *pDescriptorWrites,
1150 uint32_t descriptorCopyCount,
1151 const VkCopyDescriptorSet *pDescriptorCopies);
1152
1153 void
1154 tu_update_descriptor_set_with_template(
1155 struct tu_device *device,
1156 struct tu_cmd_buffer *cmd_buffer,
1157 struct tu_descriptor_set *set,
1158 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
1159 const void *pData);
1160
1161 void
1162 tu_meta_push_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1163 VkPipelineBindPoint pipelineBindPoint,
1164 VkPipelineLayout _layout,
1165 uint32_t set,
1166 uint32_t descriptorWriteCount,
1167 const VkWriteDescriptorSet *pDescriptorWrites);
1168
1169 struct tu_fence
1170 {
1171 uint32_t syncobj;
1172 uint32_t temp_syncobj;
1173 };
1174
1175 uint32_t
1176 tu_gem_new(struct tu_device *dev, uint64_t size, uint32_t flags);
1177 void
1178 tu_gem_close(struct tu_device *dev, uint32_t gem_handle);
1179 uint64_t
1180 tu_gem_info_offset(struct tu_device *dev, uint32_t gem_handle);
1181 uint64_t
1182 tu_gem_info_iova(struct tu_device *dev, uint32_t gem_handle);
1183 int
1184 tu_drm_query_param(struct tu_physical_device *dev,
1185 uint32_t param,
1186 uint64_t *value);
1187
1188 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1189 \
1190 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1191 { \
1192 return (struct __tu_type *) _handle; \
1193 } \
1194 \
1195 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1196 { \
1197 return (__VkType) _obj; \
1198 }
1199
1200 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1201 \
1202 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1203 { \
1204 return (struct __tu_type *) (uintptr_t) _handle; \
1205 } \
1206 \
1207 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1208 { \
1209 return (__VkType)(uintptr_t) _obj; \
1210 }
1211
1212 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1213 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1214
1215 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1216 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1217 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1218 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1219 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1220
1221 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1222 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1223 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1224 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1225 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1226 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1227 VkDescriptorSetLayout)
1228 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1229 VkDescriptorUpdateTemplateKHR)
1230 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1231 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1232 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1233 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1234 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1235 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1236 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1237 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1238 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1239 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1240 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1241 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1242 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1243 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1244
1245 #endif /* TU_PRIVATE_H */