tu: Switch to the bindless descriptor model
[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) ((void)0)
44 #endif
45
46 #include "c11/threads.h"
47 #include "main/macros.h"
48 #include "util/list.h"
49 #include "util/macros.h"
50 #include "vk_alloc.h"
51 #include "vk_debug_report.h"
52 #include "wsi_common.h"
53
54 #include "drm-uapi/msm_drm.h"
55 #include "ir3/ir3_compiler.h"
56 #include "ir3/ir3_shader.h"
57
58 #include "adreno_common.xml.h"
59 #include "adreno_pm4.xml.h"
60 #include "a6xx.xml.h"
61 #include "fdl/freedreno_layout.h"
62
63 #include "tu_descriptor_set.h"
64 #include "tu_extensions.h"
65
66 /* Pre-declarations needed for WSI entrypoints */
67 struct wl_surface;
68 struct wl_display;
69 typedef struct xcb_connection_t xcb_connection_t;
70 typedef uint32_t xcb_visualid_t;
71 typedef uint32_t xcb_window_t;
72
73 #include <vulkan/vk_android_native_buffer.h>
74 #include <vulkan/vk_icd.h>
75 #include <vulkan/vulkan.h>
76 #include <vulkan/vulkan_intel.h>
77
78 #include "tu_entrypoints.h"
79
80 #include "vk_format.h"
81
82 #define MAX_VBS 32
83 #define MAX_VERTEX_ATTRIBS 32
84 #define MAX_RTS 8
85 #define MAX_VSC_PIPES 32
86 #define MAX_VIEWPORTS 1
87 #define MAX_SCISSORS 16
88 #define MAX_DISCARD_RECTANGLES 4
89 #define MAX_PUSH_CONSTANTS_SIZE 128
90 #define MAX_PUSH_DESCRIPTORS 32
91 #define MAX_DYNAMIC_UNIFORM_BUFFERS 16
92 #define MAX_DYNAMIC_STORAGE_BUFFERS 8
93 #define MAX_DYNAMIC_BUFFERS \
94 (MAX_DYNAMIC_UNIFORM_BUFFERS + MAX_DYNAMIC_STORAGE_BUFFERS)
95 #define MAX_SAMPLES_LOG2 4
96 #define NUM_META_FS_KEYS 13
97 #define TU_MAX_DRM_DEVICES 8
98 #define MAX_VIEWS 8
99 /* The Qualcomm driver exposes 0x20000058 */
100 #define MAX_STORAGE_BUFFER_RANGE 0x20000000
101 /* We use ldc for uniform buffer loads, just like the Qualcomm driver, so
102 * expose the same maximum range.
103 * TODO: The SIZE bitfield is 15 bits, and in 4-dword units, so the actual
104 * range might be higher.
105 */
106 #define MAX_UNIFORM_BUFFER_RANGE 0x10000
107
108 #define NUM_DEPTH_CLEAR_PIPELINES 3
109
110 /*
111 * This is the point we switch from using CP to compute shader
112 * for certain buffer operations.
113 */
114 #define TU_BUFFER_OPS_CS_THRESHOLD 4096
115
116 #define A6XX_TEX_CONST_DWORDS 16
117 #define A6XX_TEX_SAMP_DWORDS 4
118
119 enum tu_mem_heap
120 {
121 TU_MEM_HEAP_VRAM,
122 TU_MEM_HEAP_VRAM_CPU_ACCESS,
123 TU_MEM_HEAP_GTT,
124 TU_MEM_HEAP_COUNT
125 };
126
127 enum tu_mem_type
128 {
129 TU_MEM_TYPE_VRAM,
130 TU_MEM_TYPE_GTT_WRITE_COMBINE,
131 TU_MEM_TYPE_VRAM_CPU_ACCESS,
132 TU_MEM_TYPE_GTT_CACHED,
133 TU_MEM_TYPE_COUNT
134 };
135
136 #define tu_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
137
138 static inline uint32_t
139 align_u32(uint32_t v, uint32_t a)
140 {
141 assert(a != 0 && a == (a & -a));
142 return (v + a - 1) & ~(a - 1);
143 }
144
145 static inline uint32_t
146 align_u32_npot(uint32_t v, uint32_t a)
147 {
148 return (v + a - 1) / a * a;
149 }
150
151 static inline uint64_t
152 align_u64(uint64_t v, uint64_t a)
153 {
154 assert(a != 0 && a == (a & -a));
155 return (v + a - 1) & ~(a - 1);
156 }
157
158 static inline int32_t
159 align_i32(int32_t v, int32_t a)
160 {
161 assert(a != 0 && a == (a & -a));
162 return (v + a - 1) & ~(a - 1);
163 }
164
165 /** Alignment must be a power of 2. */
166 static inline bool
167 tu_is_aligned(uintmax_t n, uintmax_t a)
168 {
169 assert(a == (a & -a));
170 return (n & (a - 1)) == 0;
171 }
172
173 static inline uint32_t
174 round_up_u32(uint32_t v, uint32_t a)
175 {
176 return (v + a - 1) / a;
177 }
178
179 static inline uint64_t
180 round_up_u64(uint64_t v, uint64_t a)
181 {
182 return (v + a - 1) / a;
183 }
184
185 static inline uint32_t
186 tu_minify(uint32_t n, uint32_t levels)
187 {
188 if (unlikely(n == 0))
189 return 0;
190 else
191 return MAX2(n >> levels, 1);
192 }
193 static inline float
194 tu_clamp_f(float f, float min, float max)
195 {
196 assert(min < max);
197
198 if (f > max)
199 return max;
200 else if (f < min)
201 return min;
202 else
203 return f;
204 }
205
206 static inline bool
207 tu_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
208 {
209 if (*inout_mask & clear_mask) {
210 *inout_mask &= ~clear_mask;
211 return true;
212 } else {
213 return false;
214 }
215 }
216
217 #define for_each_bit(b, dword) \
218 for (uint32_t __dword = (dword); \
219 (b) = __builtin_ffs(__dword) - 1, __dword; __dword &= ~(1 << (b)))
220
221 #define typed_memcpy(dest, src, count) \
222 ({ \
223 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
224 memcpy((dest), (src), (count) * sizeof(*(src))); \
225 })
226
227 #define COND(bool, val) ((bool) ? (val) : 0)
228
229 /* Whenever we generate an error, pass it through this function. Useful for
230 * debugging, where we can break on it. Only call at error site, not when
231 * propagating errors. Might be useful to plug in a stack trace here.
232 */
233
234 struct tu_instance;
235
236 VkResult
237 __vk_errorf(struct tu_instance *instance,
238 VkResult error,
239 const char *file,
240 int line,
241 const char *format,
242 ...);
243
244 #define vk_error(instance, error) \
245 __vk_errorf(instance, error, __FILE__, __LINE__, NULL);
246 #define vk_errorf(instance, error, format, ...) \
247 __vk_errorf(instance, error, __FILE__, __LINE__, format, ##__VA_ARGS__);
248
249 void
250 __tu_finishme(const char *file, int line, const char *format, ...)
251 tu_printflike(3, 4);
252 void
253 tu_loge(const char *format, ...) tu_printflike(1, 2);
254 void
255 tu_loge_v(const char *format, va_list va);
256 void
257 tu_logi(const char *format, ...) tu_printflike(1, 2);
258 void
259 tu_logi_v(const char *format, va_list va);
260
261 /**
262 * Print a FINISHME message, including its source location.
263 */
264 #define tu_finishme(format, ...) \
265 do { \
266 static bool reported = false; \
267 if (!reported) { \
268 __tu_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
269 reported = true; \
270 } \
271 } while (0)
272
273 /* A non-fatal assert. Useful for debugging. */
274 #ifdef DEBUG
275 #define tu_assert(x) \
276 ({ \
277 if (unlikely(!(x))) \
278 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
279 })
280 #else
281 #define tu_assert(x)
282 #endif
283
284 /* Suppress -Wunused in stub functions */
285 #define tu_use_args(...) __tu_use_args(0, ##__VA_ARGS__)
286 static inline void
287 __tu_use_args(int ignore, ...)
288 {
289 }
290
291 #define tu_stub() \
292 do { \
293 tu_finishme("stub %s", __func__); \
294 } while (0)
295
296 void *
297 tu_lookup_entrypoint_unchecked(const char *name);
298 void *
299 tu_lookup_entrypoint_checked(
300 const char *name,
301 uint32_t core_version,
302 const struct tu_instance_extension_table *instance,
303 const struct tu_device_extension_table *device);
304
305 struct tu_physical_device
306 {
307 VK_LOADER_DATA _loader_data;
308
309 struct tu_instance *instance;
310
311 char path[20];
312 char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
313 uint8_t driver_uuid[VK_UUID_SIZE];
314 uint8_t device_uuid[VK_UUID_SIZE];
315 uint8_t cache_uuid[VK_UUID_SIZE];
316
317 struct wsi_device wsi_device;
318
319 int local_fd;
320 int master_fd;
321
322 unsigned gpu_id;
323 uint32_t gmem_size;
324 uint64_t gmem_base;
325 uint32_t ccu_offset_gmem;
326 uint32_t ccu_offset_bypass;
327 #define GMEM_ALIGN_W 16
328 #define GMEM_ALIGN_H 4
329
330 struct {
331 uint32_t RB_UNKNOWN_8E04_blit; /* for CP_BLIT's */
332 uint32_t PC_UNKNOWN_9805;
333 uint32_t SP_UNKNOWN_A0F8;
334 } magic;
335
336 /* This is the drivers on-disk cache used as a fallback as opposed to
337 * the pipeline cache defined by apps.
338 */
339 struct disk_cache *disk_cache;
340
341 struct tu_device_extension_table supported_extensions;
342 };
343
344 enum tu_debug_flags
345 {
346 TU_DEBUG_STARTUP = 1 << 0,
347 TU_DEBUG_NIR = 1 << 1,
348 TU_DEBUG_IR3 = 1 << 2,
349 TU_DEBUG_NOBIN = 1 << 3,
350 TU_DEBUG_SYSMEM = 1 << 4,
351 TU_DEBUG_FORCEBIN = 1 << 5,
352 };
353
354 struct tu_instance
355 {
356 VK_LOADER_DATA _loader_data;
357
358 VkAllocationCallbacks alloc;
359
360 uint32_t api_version;
361 int physical_device_count;
362 struct tu_physical_device physical_devices[TU_MAX_DRM_DEVICES];
363
364 enum tu_debug_flags debug_flags;
365
366 struct vk_debug_report_instance debug_report_callbacks;
367
368 struct tu_instance_extension_table enabled_extensions;
369 };
370
371 VkResult
372 tu_wsi_init(struct tu_physical_device *physical_device);
373 void
374 tu_wsi_finish(struct tu_physical_device *physical_device);
375
376 bool
377 tu_instance_extension_supported(const char *name);
378 uint32_t
379 tu_physical_device_api_version(struct tu_physical_device *dev);
380 bool
381 tu_physical_device_extension_supported(struct tu_physical_device *dev,
382 const char *name);
383
384 struct cache_entry;
385
386 struct tu_pipeline_cache
387 {
388 struct tu_device *device;
389 pthread_mutex_t mutex;
390
391 uint32_t total_size;
392 uint32_t table_size;
393 uint32_t kernel_count;
394 struct cache_entry **hash_table;
395 bool modified;
396
397 VkAllocationCallbacks alloc;
398 };
399
400 struct tu_pipeline_key
401 {
402 };
403
404 void
405 tu_pipeline_cache_init(struct tu_pipeline_cache *cache,
406 struct tu_device *device);
407 void
408 tu_pipeline_cache_finish(struct tu_pipeline_cache *cache);
409 void
410 tu_pipeline_cache_load(struct tu_pipeline_cache *cache,
411 const void *data,
412 size_t size);
413
414 struct tu_shader_variant;
415
416 bool
417 tu_create_shader_variants_from_pipeline_cache(
418 struct tu_device *device,
419 struct tu_pipeline_cache *cache,
420 const unsigned char *sha1,
421 struct tu_shader_variant **variants);
422
423 void
424 tu_pipeline_cache_insert_shaders(struct tu_device *device,
425 struct tu_pipeline_cache *cache,
426 const unsigned char *sha1,
427 struct tu_shader_variant **variants,
428 const void *const *codes,
429 const unsigned *code_sizes);
430
431 struct tu_meta_state
432 {
433 VkAllocationCallbacks alloc;
434
435 struct tu_pipeline_cache cache;
436 };
437
438 /* queue types */
439 #define TU_QUEUE_GENERAL 0
440
441 #define TU_MAX_QUEUE_FAMILIES 1
442
443 struct tu_fence
444 {
445 struct wsi_fence *fence_wsi;
446 bool signaled;
447 int fd;
448 };
449
450 void
451 tu_fence_init(struct tu_fence *fence, bool signaled);
452 void
453 tu_fence_finish(struct tu_fence *fence);
454 void
455 tu_fence_update_fd(struct tu_fence *fence, int fd);
456 void
457 tu_fence_copy(struct tu_fence *fence, const struct tu_fence *src);
458 void
459 tu_fence_signal(struct tu_fence *fence);
460 void
461 tu_fence_wait_idle(struct tu_fence *fence);
462
463 struct tu_queue
464 {
465 VK_LOADER_DATA _loader_data;
466 struct tu_device *device;
467 uint32_t queue_family_index;
468 int queue_idx;
469 VkDeviceQueueCreateFlags flags;
470
471 uint32_t msm_queue_id;
472 struct tu_fence submit_fence;
473 };
474
475 struct tu_bo
476 {
477 uint32_t gem_handle;
478 uint64_t size;
479 uint64_t iova;
480 void *map;
481 };
482
483 struct tu_device
484 {
485 VK_LOADER_DATA _loader_data;
486
487 VkAllocationCallbacks alloc;
488
489 struct tu_instance *instance;
490
491 struct tu_meta_state meta_state;
492
493 struct tu_queue *queues[TU_MAX_QUEUE_FAMILIES];
494 int queue_count[TU_MAX_QUEUE_FAMILIES];
495
496 struct tu_physical_device *physical_device;
497
498 struct ir3_compiler *compiler;
499
500 /* Backup in-memory cache to be used if the app doesn't provide one */
501 struct tu_pipeline_cache *mem_cache;
502
503 struct tu_bo vsc_data;
504 struct tu_bo vsc_data2;
505 uint32_t vsc_data_pitch;
506 uint32_t vsc_data2_pitch;
507
508 struct tu_bo border_color;
509
510 struct list_head shader_slabs;
511 mtx_t shader_slab_mutex;
512
513 struct tu_device_extension_table enabled_extensions;
514 };
515
516 VkResult
517 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size);
518 VkResult
519 tu_bo_init_dmabuf(struct tu_device *dev,
520 struct tu_bo *bo,
521 uint64_t size,
522 int fd);
523 int
524 tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo);
525 void
526 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
527 VkResult
528 tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
529
530 struct tu_cs_entry
531 {
532 /* No ownership */
533 const struct tu_bo *bo;
534
535 uint32_t size;
536 uint32_t offset;
537 };
538
539 struct ts_cs_memory {
540 uint32_t *map;
541 uint64_t iova;
542 };
543
544 enum tu_cs_mode
545 {
546
547 /*
548 * A command stream in TU_CS_MODE_GROW mode grows automatically whenever it
549 * is full. tu_cs_begin must be called before command packet emission and
550 * tu_cs_end must be called after.
551 *
552 * This mode may create multiple entries internally. The entries must be
553 * submitted together.
554 */
555 TU_CS_MODE_GROW,
556
557 /*
558 * A command stream in TU_CS_MODE_EXTERNAL mode wraps an external,
559 * fixed-size buffer. tu_cs_begin and tu_cs_end are optional and have no
560 * effect on it.
561 *
562 * This mode does not create any entry or any BO.
563 */
564 TU_CS_MODE_EXTERNAL,
565
566 /*
567 * A command stream in TU_CS_MODE_SUB_STREAM mode does not support direct
568 * command packet emission. tu_cs_begin_sub_stream must be called to get a
569 * sub-stream to emit comamnd packets to. When done with the sub-stream,
570 * tu_cs_end_sub_stream must be called.
571 *
572 * This mode does not create any entry internally.
573 */
574 TU_CS_MODE_SUB_STREAM,
575 };
576
577 struct tu_cs
578 {
579 uint32_t *start;
580 uint32_t *cur;
581 uint32_t *reserved_end;
582 uint32_t *end;
583
584 struct tu_device *device;
585 enum tu_cs_mode mode;
586 uint32_t next_bo_size;
587
588 struct tu_cs_entry *entries;
589 uint32_t entry_count;
590 uint32_t entry_capacity;
591
592 struct tu_bo **bos;
593 uint32_t bo_count;
594 uint32_t bo_capacity;
595
596 /* state for cond_exec_start/cond_exec_end */
597 uint32_t cond_flags;
598 uint32_t *cond_dwords;
599 };
600
601 struct tu_device_memory
602 {
603 struct tu_bo bo;
604 VkDeviceSize size;
605
606 /* for dedicated allocations */
607 struct tu_image *image;
608 struct tu_buffer *buffer;
609
610 uint32_t type_index;
611 void *map;
612 void *user_ptr;
613 };
614
615 struct tu_descriptor_range
616 {
617 uint64_t va;
618 uint32_t size;
619 };
620
621 struct tu_descriptor_set
622 {
623 const struct tu_descriptor_set_layout *layout;
624 struct tu_descriptor_pool *pool;
625 uint32_t size;
626
627 uint64_t va;
628 uint32_t *mapped_ptr;
629
630 uint32_t *dynamic_descriptors;
631
632 struct tu_bo *buffers[0];
633 };
634
635 struct tu_push_descriptor_set
636 {
637 struct tu_descriptor_set set;
638 uint32_t capacity;
639 };
640
641 struct tu_descriptor_pool_entry
642 {
643 uint32_t offset;
644 uint32_t size;
645 struct tu_descriptor_set *set;
646 };
647
648 struct tu_descriptor_pool
649 {
650 struct tu_bo bo;
651 uint64_t current_offset;
652 uint64_t size;
653
654 uint8_t *host_memory_base;
655 uint8_t *host_memory_ptr;
656 uint8_t *host_memory_end;
657
658 uint32_t entry_count;
659 uint32_t max_entry_count;
660 struct tu_descriptor_pool_entry entries[0];
661 };
662
663 struct tu_descriptor_update_template_entry
664 {
665 VkDescriptorType descriptor_type;
666
667 /* The number of descriptors to update */
668 uint32_t descriptor_count;
669
670 /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
671 */
672 uint32_t dst_offset;
673
674 /* In dwords. Not valid/used for dynamic descriptors */
675 uint32_t dst_stride;
676
677 uint32_t buffer_offset;
678
679 /* Only valid for combined image samplers and samplers */
680 uint16_t has_sampler;
681
682 /* In bytes */
683 size_t src_offset;
684 size_t src_stride;
685
686 /* For push descriptors */
687 const uint32_t *immutable_samplers;
688 };
689
690 struct tu_descriptor_update_template
691 {
692 uint32_t entry_count;
693 VkPipelineBindPoint bind_point;
694 struct tu_descriptor_update_template_entry entry[0];
695 };
696
697 struct tu_buffer
698 {
699 VkDeviceSize size;
700
701 VkBufferUsageFlags usage;
702 VkBufferCreateFlags flags;
703
704 struct tu_bo *bo;
705 VkDeviceSize bo_offset;
706 };
707
708 static inline uint64_t
709 tu_buffer_iova(struct tu_buffer *buffer)
710 {
711 return buffer->bo->iova + buffer->bo_offset;
712 }
713
714 enum tu_dynamic_state_bits
715 {
716 TU_DYNAMIC_VIEWPORT = 1 << 0,
717 TU_DYNAMIC_SCISSOR = 1 << 1,
718 TU_DYNAMIC_LINE_WIDTH = 1 << 2,
719 TU_DYNAMIC_DEPTH_BIAS = 1 << 3,
720 TU_DYNAMIC_BLEND_CONSTANTS = 1 << 4,
721 TU_DYNAMIC_DEPTH_BOUNDS = 1 << 5,
722 TU_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6,
723 TU_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7,
724 TU_DYNAMIC_STENCIL_REFERENCE = 1 << 8,
725 TU_DYNAMIC_DISCARD_RECTANGLE = 1 << 9,
726 TU_DYNAMIC_ALL = (1 << 10) - 1,
727 };
728
729 struct tu_vertex_binding
730 {
731 struct tu_buffer *buffer;
732 VkDeviceSize offset;
733 };
734
735 struct tu_viewport_state
736 {
737 uint32_t count;
738 VkViewport viewports[MAX_VIEWPORTS];
739 };
740
741 struct tu_scissor_state
742 {
743 uint32_t count;
744 VkRect2D scissors[MAX_SCISSORS];
745 };
746
747 struct tu_discard_rectangle_state
748 {
749 uint32_t count;
750 VkRect2D rectangles[MAX_DISCARD_RECTANGLES];
751 };
752
753 struct tu_dynamic_state
754 {
755 /**
756 * Bitmask of (1 << VK_DYNAMIC_STATE_*).
757 * Defines the set of saved dynamic state.
758 */
759 uint32_t mask;
760
761 struct tu_viewport_state viewport;
762
763 struct tu_scissor_state scissor;
764
765 float line_width;
766
767 struct
768 {
769 float bias;
770 float clamp;
771 float slope;
772 } depth_bias;
773
774 float blend_constants[4];
775
776 struct
777 {
778 float min;
779 float max;
780 } depth_bounds;
781
782 struct
783 {
784 uint32_t front;
785 uint32_t back;
786 } stencil_compare_mask;
787
788 struct
789 {
790 uint32_t front;
791 uint32_t back;
792 } stencil_write_mask;
793
794 struct
795 {
796 uint32_t front;
797 uint32_t back;
798 } stencil_reference;
799
800 struct tu_discard_rectangle_state discard_rectangle;
801 };
802
803 extern const struct tu_dynamic_state default_dynamic_state;
804
805 const char *
806 tu_get_debug_option_name(int id);
807
808 const char *
809 tu_get_perftest_option_name(int id);
810
811 struct tu_descriptor_state
812 {
813 struct tu_descriptor_set *sets[MAX_SETS];
814 uint32_t valid;
815 struct tu_push_descriptor_set push_set;
816 bool push_dirty;
817 uint32_t dynamic_descriptors[MAX_DYNAMIC_BUFFERS * A6XX_TEX_CONST_DWORDS];
818 uint32_t input_attachments[MAX_RTS * A6XX_TEX_CONST_DWORDS];
819 };
820
821 struct tu_tile
822 {
823 uint8_t pipe;
824 uint8_t slot;
825 VkOffset2D begin;
826 VkOffset2D end;
827 };
828
829 struct tu_tiling_config
830 {
831 VkRect2D render_area;
832
833 /* position and size of the first tile */
834 VkRect2D tile0;
835 /* number of tiles */
836 VkExtent2D tile_count;
837
838 /* size of the first VSC pipe */
839 VkExtent2D pipe0;
840 /* number of VSC pipes */
841 VkExtent2D pipe_count;
842
843 /* pipe register values */
844 uint32_t pipe_config[MAX_VSC_PIPES];
845 uint32_t pipe_sizes[MAX_VSC_PIPES];
846
847 /* Whether sysmem rendering must be used */
848 bool force_sysmem;
849 };
850
851 enum tu_cmd_dirty_bits
852 {
853 TU_CMD_DIRTY_PIPELINE = 1 << 0,
854 TU_CMD_DIRTY_COMPUTE_PIPELINE = 1 << 1,
855 TU_CMD_DIRTY_VERTEX_BUFFERS = 1 << 2,
856 TU_CMD_DIRTY_DESCRIPTOR_SETS = 1 << 3,
857 TU_CMD_DIRTY_COMPUTE_DESCRIPTOR_SETS = 1 << 4,
858 TU_CMD_DIRTY_PUSH_CONSTANTS = 1 << 5,
859 TU_CMD_DIRTY_STREAMOUT_BUFFERS = 1 << 6,
860 TU_CMD_DIRTY_INPUT_ATTACHMENTS = 1 << 7,
861
862 TU_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 16,
863 TU_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 17,
864 TU_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 18,
865 TU_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 19,
866 TU_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 20,
867 TU_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 21,
868 };
869
870 struct tu_streamout_state {
871 uint16_t stride[IR3_MAX_SO_BUFFERS];
872 uint32_t ncomp[IR3_MAX_SO_BUFFERS];
873 uint32_t prog[IR3_MAX_SO_OUTPUTS * 2];
874 uint32_t prog_count;
875 uint32_t vpc_so_buf_cntl;
876 };
877
878 struct tu_cmd_state
879 {
880 uint32_t dirty;
881
882 struct tu_pipeline *pipeline;
883 struct tu_pipeline *compute_pipeline;
884
885 /* Vertex buffers */
886 struct
887 {
888 struct tu_buffer *buffers[MAX_VBS];
889 VkDeviceSize offsets[MAX_VBS];
890 } vb;
891
892 struct tu_dynamic_state dynamic;
893
894 /* Stream output buffers */
895 struct
896 {
897 struct tu_buffer *buffers[IR3_MAX_SO_BUFFERS];
898 VkDeviceSize offsets[IR3_MAX_SO_BUFFERS];
899 VkDeviceSize sizes[IR3_MAX_SO_BUFFERS];
900 } streamout_buf;
901
902 uint8_t streamout_reset;
903 uint8_t streamout_enabled;
904
905 /* Index buffer */
906 struct tu_buffer *index_buffer;
907 uint64_t index_offset;
908 uint32_t index_type;
909 uint32_t max_index_count;
910 uint64_t index_va;
911
912 const struct tu_render_pass *pass;
913 const struct tu_subpass *subpass;
914 const struct tu_framebuffer *framebuffer;
915
916 struct tu_tiling_config tiling_config;
917
918 struct tu_cs_entry tile_store_ib;
919 };
920
921 struct tu_cmd_pool
922 {
923 VkAllocationCallbacks alloc;
924 struct list_head cmd_buffers;
925 struct list_head free_cmd_buffers;
926 uint32_t queue_family_index;
927 };
928
929 struct tu_cmd_buffer_upload
930 {
931 uint8_t *map;
932 unsigned offset;
933 uint64_t size;
934 struct list_head list;
935 };
936
937 enum tu_cmd_buffer_status
938 {
939 TU_CMD_BUFFER_STATUS_INVALID,
940 TU_CMD_BUFFER_STATUS_INITIAL,
941 TU_CMD_BUFFER_STATUS_RECORDING,
942 TU_CMD_BUFFER_STATUS_EXECUTABLE,
943 TU_CMD_BUFFER_STATUS_PENDING,
944 };
945
946 struct tu_bo_list
947 {
948 uint32_t count;
949 uint32_t capacity;
950 struct drm_msm_gem_submit_bo *bo_infos;
951 };
952
953 #define TU_BO_LIST_FAILED (~0)
954
955 void
956 tu_bo_list_init(struct tu_bo_list *list);
957 void
958 tu_bo_list_destroy(struct tu_bo_list *list);
959 void
960 tu_bo_list_reset(struct tu_bo_list *list);
961 uint32_t
962 tu_bo_list_add(struct tu_bo_list *list,
963 const struct tu_bo *bo,
964 uint32_t flags);
965 VkResult
966 tu_bo_list_merge(struct tu_bo_list *list, const struct tu_bo_list *other);
967
968 /* This struct defines the layout of the scratch_bo */
969 struct tu6_control
970 {
971 uint32_t seqno; /* seqno for async CP_EVENT_WRITE, etc */
972 uint32_t _pad0;
973 volatile uint32_t vsc_overflow;
974 uint32_t _pad1;
975 /* flag set from cmdstream when VSC overflow detected: */
976 uint32_t vsc_scratch;
977 uint32_t _pad2;
978 uint32_t _pad3;
979 uint32_t _pad4;
980
981 /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
982 struct {
983 uint32_t offset;
984 uint32_t pad[7];
985 } flush_base[4];
986 };
987
988 #define ctrl_offset(member) offsetof(struct tu6_control, member)
989
990 struct tu_cmd_buffer
991 {
992 VK_LOADER_DATA _loader_data;
993
994 struct tu_device *device;
995
996 struct tu_cmd_pool *pool;
997 struct list_head pool_link;
998
999 VkCommandBufferUsageFlags usage_flags;
1000 VkCommandBufferLevel level;
1001 enum tu_cmd_buffer_status status;
1002
1003 struct tu_cmd_state state;
1004 struct tu_vertex_binding vertex_bindings[MAX_VBS];
1005 uint32_t queue_family_index;
1006
1007 uint32_t push_constants[MAX_PUSH_CONSTANTS_SIZE / 4];
1008 VkShaderStageFlags push_constant_stages;
1009 struct tu_descriptor_set meta_push_descriptors;
1010
1011 struct tu_descriptor_state descriptors[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
1012
1013 struct tu_cmd_buffer_upload upload;
1014
1015 VkResult record_result;
1016
1017 struct tu_bo_list bo_list;
1018 struct tu_cs cs;
1019 struct tu_cs draw_cs;
1020 struct tu_cs draw_epilogue_cs;
1021 struct tu_cs sub_cs;
1022
1023 struct tu_bo scratch_bo;
1024 uint32_t scratch_seqno;
1025
1026 struct tu_bo vsc_data;
1027 struct tu_bo vsc_data2;
1028 uint32_t vsc_data_pitch;
1029 uint32_t vsc_data2_pitch;
1030 bool use_vsc_data;
1031
1032 bool wait_for_idle;
1033 };
1034
1035 /* Temporary struct for tracking a register state to be written, used by
1036 * a6xx-pack.h and tu_cs_emit_regs()
1037 */
1038 struct tu_reg_value {
1039 uint32_t reg;
1040 uint64_t value;
1041 bool is_address;
1042 struct tu_bo *bo;
1043 bool bo_write;
1044 uint32_t bo_offset;
1045 uint32_t bo_shift;
1046 };
1047
1048 unsigned
1049 tu6_emit_event_write(struct tu_cmd_buffer *cmd,
1050 struct tu_cs *cs,
1051 enum vgt_event_type event,
1052 bool need_seqno);
1053
1054 bool
1055 tu_get_memory_fd(struct tu_device *device,
1056 struct tu_device_memory *memory,
1057 int *pFD);
1058
1059 static inline struct tu_descriptor_state *
1060 tu_get_descriptors_state(struct tu_cmd_buffer *cmd_buffer,
1061 VkPipelineBindPoint bind_point)
1062 {
1063 return &cmd_buffer->descriptors[bind_point];
1064 }
1065
1066 /*
1067 * Takes x,y,z as exact numbers of invocations, instead of blocks.
1068 *
1069 * Limitations: Can't call normal dispatch functions without binding or
1070 * rebinding
1071 * the compute pipeline.
1072 */
1073 void
1074 tu_unaligned_dispatch(struct tu_cmd_buffer *cmd_buffer,
1075 uint32_t x,
1076 uint32_t y,
1077 uint32_t z);
1078
1079 struct tu_event
1080 {
1081 struct tu_bo bo;
1082 };
1083
1084 struct tu_shader_module;
1085
1086 #define TU_HASH_SHADER_IS_GEOM_COPY_SHADER (1 << 0)
1087 #define TU_HASH_SHADER_SISCHED (1 << 1)
1088 #define TU_HASH_SHADER_UNSAFE_MATH (1 << 2)
1089 void
1090 tu_hash_shaders(unsigned char *hash,
1091 const VkPipelineShaderStageCreateInfo **stages,
1092 const struct tu_pipeline_layout *layout,
1093 const struct tu_pipeline_key *key,
1094 uint32_t flags);
1095
1096 static inline gl_shader_stage
1097 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1098 {
1099 assert(__builtin_popcount(vk_stage) == 1);
1100 return ffs(vk_stage) - 1;
1101 }
1102
1103 static inline VkShaderStageFlagBits
1104 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1105 {
1106 return (1 << mesa_stage);
1107 }
1108
1109 #define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1110
1111 #define tu_foreach_stage(stage, stage_bits) \
1112 for (gl_shader_stage stage, \
1113 __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK); \
1114 stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
1115
1116 struct tu_shader_module
1117 {
1118 unsigned char sha1[20];
1119
1120 uint32_t code_size;
1121 const uint32_t *code[0];
1122 };
1123
1124 struct tu_shader_compile_options
1125 {
1126 struct ir3_shader_key key;
1127
1128 bool optimize;
1129 bool include_binning_pass;
1130 };
1131
1132 struct tu_push_constant_range
1133 {
1134 uint32_t lo;
1135 uint32_t count;
1136 };
1137
1138 struct tu_shader
1139 {
1140 struct ir3_shader ir3_shader;
1141
1142 struct tu_push_constant_range push_consts;
1143 unsigned attachment_idx[MAX_RTS];
1144
1145 /* This may be true for vertex shaders. When true, variants[1] is the
1146 * binning variant and binning_binary is non-NULL.
1147 */
1148 bool has_binning_pass;
1149
1150 void *binary;
1151 void *binning_binary;
1152
1153 struct ir3_shader_variant variants[0];
1154 };
1155
1156 struct tu_shader *
1157 tu_shader_create(struct tu_device *dev,
1158 gl_shader_stage stage,
1159 const VkPipelineShaderStageCreateInfo *stage_info,
1160 struct tu_pipeline_layout *layout,
1161 const VkAllocationCallbacks *alloc);
1162
1163 void
1164 tu_shader_destroy(struct tu_device *dev,
1165 struct tu_shader *shader,
1166 const VkAllocationCallbacks *alloc);
1167
1168 void
1169 tu_shader_compile_options_init(
1170 struct tu_shader_compile_options *options,
1171 const VkGraphicsPipelineCreateInfo *pipeline_info);
1172
1173 VkResult
1174 tu_shader_compile(struct tu_device *dev,
1175 struct tu_shader *shader,
1176 const struct tu_shader *next_stage,
1177 const struct tu_shader_compile_options *options,
1178 const VkAllocationCallbacks *alloc);
1179
1180 struct tu_program_descriptor_linkage
1181 {
1182 struct ir3_ubo_analysis_state ubo_state;
1183 struct ir3_const_state const_state;
1184
1185 uint32_t constlen;
1186
1187 struct tu_push_constant_range push_consts;
1188 };
1189
1190 struct tu_pipeline
1191 {
1192 struct tu_cs cs;
1193
1194 struct tu_dynamic_state dynamic_state;
1195
1196 struct tu_pipeline_layout *layout;
1197
1198 bool need_indirect_descriptor_sets;
1199 VkShaderStageFlags active_stages;
1200
1201 struct tu_streamout_state streamout;
1202
1203 struct
1204 {
1205 struct tu_bo binary_bo;
1206 struct tu_cs_entry state_ib;
1207 struct tu_cs_entry binning_state_ib;
1208
1209 struct tu_program_descriptor_linkage link[MESA_SHADER_STAGES];
1210 unsigned input_attachment_idx[MAX_RTS];
1211 } program;
1212
1213 struct
1214 {
1215 uint8_t bindings[MAX_VERTEX_ATTRIBS];
1216 uint32_t count;
1217
1218 uint8_t binning_bindings[MAX_VERTEX_ATTRIBS];
1219 uint32_t binning_count;
1220
1221 struct tu_cs_entry state_ib;
1222 struct tu_cs_entry binning_state_ib;
1223 } vi;
1224
1225 struct
1226 {
1227 enum pc_di_primtype primtype;
1228 bool primitive_restart;
1229 } ia;
1230
1231 struct
1232 {
1233 struct tu_cs_entry state_ib;
1234 } vp;
1235
1236 struct
1237 {
1238 uint32_t gras_su_cntl;
1239 struct tu_cs_entry state_ib;
1240 } rast;
1241
1242 struct
1243 {
1244 struct tu_cs_entry state_ib;
1245 } ds;
1246
1247 struct
1248 {
1249 struct tu_cs_entry state_ib;
1250 } blend;
1251
1252 struct
1253 {
1254 uint32_t local_size[3];
1255 } compute;
1256 };
1257
1258 void
1259 tu6_emit_viewport(struct tu_cs *cs, const VkViewport *viewport);
1260
1261 void
1262 tu6_emit_scissor(struct tu_cs *cs, const VkRect2D *scissor);
1263
1264 void
1265 tu6_emit_gras_su_cntl(struct tu_cs *cs,
1266 uint32_t gras_su_cntl,
1267 float line_width);
1268
1269 void
1270 tu6_emit_depth_bias(struct tu_cs *cs,
1271 float constant_factor,
1272 float clamp,
1273 float slope_factor);
1274
1275 void
1276 tu6_emit_stencil_compare_mask(struct tu_cs *cs,
1277 uint32_t front,
1278 uint32_t back);
1279
1280 void
1281 tu6_emit_stencil_write_mask(struct tu_cs *cs, uint32_t front, uint32_t back);
1282
1283 void
1284 tu6_emit_stencil_reference(struct tu_cs *cs, uint32_t front, uint32_t back);
1285
1286 void
1287 tu6_emit_blend_constants(struct tu_cs *cs, const float constants[4]);
1288
1289 void tu6_emit_msaa(struct tu_cs *cs, VkSampleCountFlagBits samples);
1290
1291 void tu6_emit_window_scissor(struct tu_cs *cs, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
1292
1293 void tu6_emit_window_offset(struct tu_cs *cs, uint32_t x1, uint32_t y1);
1294
1295 struct tu_image_view;
1296
1297 void
1298 tu_resolve_sysmem(struct tu_cmd_buffer *cmd,
1299 struct tu_cs *cs,
1300 struct tu_image_view *src,
1301 struct tu_image_view *dst,
1302 uint32_t layers,
1303 const VkRect2D *rect);
1304
1305 void
1306 tu_clear_sysmem_attachment(struct tu_cmd_buffer *cmd,
1307 struct tu_cs *cs,
1308 uint32_t a,
1309 const VkRenderPassBeginInfo *info);
1310
1311 void
1312 tu_clear_gmem_attachment(struct tu_cmd_buffer *cmd,
1313 struct tu_cs *cs,
1314 uint32_t a,
1315 const VkRenderPassBeginInfo *info);
1316
1317 void
1318 tu_load_gmem_attachment(struct tu_cmd_buffer *cmd, struct tu_cs *cs, uint32_t a);
1319
1320 /* expose this function to be able to emit load without checking LOAD_OP */
1321 void
1322 tu_emit_load_gmem_attachment(struct tu_cmd_buffer *cmd, struct tu_cs *cs, uint32_t a);
1323
1324 /* note: gmem store can also resolve */
1325 void
1326 tu_store_gmem_attachment(struct tu_cmd_buffer *cmd,
1327 struct tu_cs *cs,
1328 uint32_t a,
1329 uint32_t gmem_a);
1330
1331 struct tu_userdata_info *
1332 tu_lookup_user_sgpr(struct tu_pipeline *pipeline,
1333 gl_shader_stage stage,
1334 int idx);
1335
1336 struct tu_shader_variant *
1337 tu_get_shader(struct tu_pipeline *pipeline, gl_shader_stage stage);
1338
1339 struct tu_graphics_pipeline_create_info
1340 {
1341 bool use_rectlist;
1342 bool db_depth_clear;
1343 bool db_stencil_clear;
1344 bool db_depth_disable_expclear;
1345 bool db_stencil_disable_expclear;
1346 bool db_flush_depth_inplace;
1347 bool db_flush_stencil_inplace;
1348 bool db_resummarize;
1349 uint32_t custom_blend_mode;
1350 };
1351
1352 enum tu_supported_formats {
1353 FMT_VERTEX = 1,
1354 FMT_TEXTURE = 2,
1355 FMT_COLOR = 4,
1356 };
1357
1358 struct tu_native_format
1359 {
1360 enum a6xx_format fmt : 8;
1361 enum a3xx_color_swap swap : 8;
1362 enum a6xx_tile_mode tile_mode : 8;
1363 enum tu_supported_formats supported : 8;
1364 };
1365
1366 struct tu_native_format tu6_format_vtx(VkFormat format);
1367 struct tu_native_format tu6_format_color(VkFormat format, enum a6xx_tile_mode tile_mode);
1368 struct tu_native_format tu6_format_texture(VkFormat format, enum a6xx_tile_mode tile_mode);
1369
1370 static inline enum a6xx_format
1371 tu6_base_format(VkFormat format)
1372 {
1373 /* note: tu6_format_color doesn't care about tiling for .fmt field */
1374 return tu6_format_color(format, TILE6_LINEAR).fmt;
1375 }
1376
1377 enum a6xx_depth_format tu6_pipe2depth(VkFormat format);
1378
1379 struct tu_image
1380 {
1381 VkImageType type;
1382 /* The original VkFormat provided by the client. This may not match any
1383 * of the actual surface formats.
1384 */
1385 VkFormat vk_format;
1386 VkImageAspectFlags aspects;
1387 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1388 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1389 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1390 VkExtent3D extent;
1391 uint32_t level_count;
1392 uint32_t layer_count;
1393 VkSampleCountFlagBits samples;
1394
1395 struct fdl_layout layout;
1396
1397 unsigned queue_family_mask;
1398 bool exclusive;
1399 bool shareable;
1400
1401 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1402 VkDeviceMemory owned_memory;
1403
1404 /* Set when bound */
1405 struct tu_bo *bo;
1406 VkDeviceSize bo_offset;
1407 };
1408
1409 unsigned
1410 tu_image_queue_family_mask(const struct tu_image *image,
1411 uint32_t family,
1412 uint32_t queue_family);
1413
1414 static inline uint32_t
1415 tu_get_layerCount(const struct tu_image *image,
1416 const VkImageSubresourceRange *range)
1417 {
1418 return range->layerCount == VK_REMAINING_ARRAY_LAYERS
1419 ? image->layer_count - range->baseArrayLayer
1420 : range->layerCount;
1421 }
1422
1423 static inline uint32_t
1424 tu_get_levelCount(const struct tu_image *image,
1425 const VkImageSubresourceRange *range)
1426 {
1427 return range->levelCount == VK_REMAINING_MIP_LEVELS
1428 ? image->level_count - range->baseMipLevel
1429 : range->levelCount;
1430 }
1431
1432 static inline VkDeviceSize
1433 tu_layer_size(struct tu_image *image, int level)
1434 {
1435 return fdl_layer_stride(&image->layout, level);
1436 }
1437
1438 static inline uint32_t
1439 tu_image_stride(struct tu_image *image, int level)
1440 {
1441 return image->layout.slices[level].pitch * image->layout.cpp;
1442 }
1443
1444 /* to get the right pitch for compressed formats */
1445 static inline uint32_t
1446 tu_image_pitch(struct tu_image *image, int level)
1447 {
1448 uint32_t stride = tu_image_stride(image, level);
1449 return stride / vk_format_get_blockwidth(image->vk_format);
1450 }
1451
1452 static inline uint64_t
1453 tu_image_base(struct tu_image *image, int level, int layer)
1454 {
1455 return image->bo->iova + image->bo_offset +
1456 fdl_surface_offset(&image->layout, level, layer);
1457 }
1458
1459 #define tu_image_base_ref(image, level, layer) \
1460 .bo = image->bo, \
1461 .bo_offset = (image->bo_offset + fdl_surface_offset(&image->layout, \
1462 level, layer))
1463
1464 #define tu_image_view_base_ref(iview) \
1465 tu_image_base_ref(iview->image, iview->base_mip, iview->base_layer)
1466
1467 static inline VkDeviceSize
1468 tu_image_ubwc_size(struct tu_image *image, int level)
1469 {
1470 return image->layout.ubwc_layer_size;
1471 }
1472
1473 static inline uint32_t
1474 tu_image_ubwc_pitch(struct tu_image *image, int level)
1475 {
1476 return image->layout.ubwc_slices[level].pitch;
1477 }
1478
1479 static inline uint64_t
1480 tu_image_ubwc_surface_offset(struct tu_image *image, int level, int layer)
1481 {
1482 return image->layout.ubwc_slices[level].offset +
1483 layer * tu_image_ubwc_size(image, level);
1484 }
1485
1486 static inline uint64_t
1487 tu_image_ubwc_base(struct tu_image *image, int level, int layer)
1488 {
1489 return image->bo->iova + image->bo_offset +
1490 tu_image_ubwc_surface_offset(image, level, layer);
1491 }
1492
1493 #define tu_image_ubwc_base_ref(image, level, layer) \
1494 .bo = image->bo, \
1495 .bo_offset = (image->bo_offset + tu_image_ubwc_surface_offset(image, \
1496 level, layer))
1497
1498 #define tu_image_view_ubwc_base_ref(iview) \
1499 tu_image_ubwc_base_ref(iview->image, iview->base_mip, iview->base_layer)
1500
1501 #define tu_image_view_ubwc_pitches(iview) \
1502 .pitch = tu_image_ubwc_pitch(iview->image, iview->base_mip), \
1503 .array_pitch = tu_image_ubwc_size(iview->image, iview->base_mip) >> 2
1504
1505 enum a6xx_tile_mode
1506 tu6_get_image_tile_mode(struct tu_image *image, int level);
1507 enum a3xx_msaa_samples
1508 tu_msaa_samples(uint32_t samples);
1509 enum a6xx_tex_fetchsize
1510 tu6_fetchsize(VkFormat format);
1511
1512 static inline struct tu_native_format
1513 tu6_format_image(struct tu_image *image, VkFormat format, uint32_t level)
1514 {
1515 struct tu_native_format fmt =
1516 tu6_format_color(format, image->layout.tile_mode);
1517 fmt.tile_mode = tu6_get_image_tile_mode(image, level);
1518 return fmt;
1519 }
1520
1521 static inline struct tu_native_format
1522 tu6_format_image_src(struct tu_image *image, VkFormat format, uint32_t level)
1523 {
1524 struct tu_native_format fmt =
1525 tu6_format_texture(format, image->layout.tile_mode);
1526 fmt.tile_mode = tu6_get_image_tile_mode(image, level);
1527 return fmt;
1528 }
1529
1530 struct tu_image_view
1531 {
1532 struct tu_image *image; /**< VkImageViewCreateInfo::image */
1533
1534 VkImageViewType type;
1535 VkImageAspectFlags aspect_mask;
1536 VkFormat vk_format;
1537 uint32_t base_layer;
1538 uint32_t layer_count;
1539 uint32_t base_mip;
1540 uint32_t level_count;
1541 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1542
1543 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1544
1545 /* Descriptor for use as a storage image as opposed to a sampled image.
1546 * This has a few differences for cube maps (e.g. type).
1547 */
1548 uint32_t storage_descriptor[A6XX_TEX_CONST_DWORDS];
1549 };
1550
1551 struct tu_sampler {
1552 uint32_t descriptor[A6XX_TEX_SAMP_DWORDS];
1553 };
1554
1555 VkResult
1556 tu_image_create(VkDevice _device,
1557 const VkImageCreateInfo *pCreateInfo,
1558 const VkAllocationCallbacks *alloc,
1559 VkImage *pImage,
1560 uint64_t modifier);
1561
1562 VkResult
1563 tu_image_from_gralloc(VkDevice device_h,
1564 const VkImageCreateInfo *base_info,
1565 const VkNativeBufferANDROID *gralloc_info,
1566 const VkAllocationCallbacks *alloc,
1567 VkImage *out_image_h);
1568
1569 void
1570 tu_image_view_init(struct tu_image_view *view,
1571 struct tu_device *device,
1572 const VkImageViewCreateInfo *pCreateInfo);
1573
1574 struct tu_buffer_view
1575 {
1576 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1577
1578 struct tu_buffer *buffer;
1579 };
1580 void
1581 tu_buffer_view_init(struct tu_buffer_view *view,
1582 struct tu_device *device,
1583 const VkBufferViewCreateInfo *pCreateInfo);
1584
1585 static inline struct VkExtent3D
1586 tu_sanitize_image_extent(const VkImageType imageType,
1587 const struct VkExtent3D imageExtent)
1588 {
1589 switch (imageType) {
1590 case VK_IMAGE_TYPE_1D:
1591 return (VkExtent3D) { imageExtent.width, 1, 1 };
1592 case VK_IMAGE_TYPE_2D:
1593 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1594 case VK_IMAGE_TYPE_3D:
1595 return imageExtent;
1596 default:
1597 unreachable("invalid image type");
1598 }
1599 }
1600
1601 static inline struct VkOffset3D
1602 tu_sanitize_image_offset(const VkImageType imageType,
1603 const struct VkOffset3D imageOffset)
1604 {
1605 switch (imageType) {
1606 case VK_IMAGE_TYPE_1D:
1607 return (VkOffset3D) { imageOffset.x, 0, 0 };
1608 case VK_IMAGE_TYPE_2D:
1609 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
1610 case VK_IMAGE_TYPE_3D:
1611 return imageOffset;
1612 default:
1613 unreachable("invalid image type");
1614 }
1615 }
1616
1617 struct tu_attachment_info
1618 {
1619 struct tu_image_view *attachment;
1620 };
1621
1622 struct tu_framebuffer
1623 {
1624 uint32_t width;
1625 uint32_t height;
1626 uint32_t layers;
1627
1628 uint32_t attachment_count;
1629 struct tu_attachment_info attachments[0];
1630 };
1631
1632 struct tu_subpass_attachment
1633 {
1634 uint32_t attachment;
1635 };
1636
1637 struct tu_subpass
1638 {
1639 uint32_t input_count;
1640 uint32_t color_count;
1641 struct tu_subpass_attachment *input_attachments;
1642 struct tu_subpass_attachment *color_attachments;
1643 struct tu_subpass_attachment *resolve_attachments;
1644 struct tu_subpass_attachment depth_stencil_attachment;
1645
1646 VkSampleCountFlagBits samples;
1647 };
1648
1649 struct tu_render_pass_attachment
1650 {
1651 VkFormat format;
1652 uint32_t samples;
1653 uint32_t cpp;
1654 VkAttachmentLoadOp load_op;
1655 VkAttachmentLoadOp stencil_load_op;
1656 VkAttachmentStoreOp store_op;
1657 VkAttachmentStoreOp stencil_store_op;
1658 int32_t gmem_offset;
1659 };
1660
1661 struct tu_render_pass
1662 {
1663 uint32_t attachment_count;
1664 uint32_t subpass_count;
1665 uint32_t gmem_pixels;
1666 struct tu_subpass_attachment *subpass_attachments;
1667 struct tu_render_pass_attachment *attachments;
1668 struct tu_subpass subpasses[0];
1669 };
1670
1671 VkResult
1672 tu_device_init_meta(struct tu_device *device);
1673 void
1674 tu_device_finish_meta(struct tu_device *device);
1675
1676 struct tu_query_pool
1677 {
1678 VkQueryType type;
1679 uint32_t stride;
1680 uint64_t size;
1681 uint32_t pipeline_statistics;
1682 struct tu_bo bo;
1683 };
1684
1685 struct tu_semaphore
1686 {
1687 uint32_t syncobj;
1688 uint32_t temp_syncobj;
1689 };
1690
1691 void
1692 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1693 VkPipelineBindPoint bind_point,
1694 struct tu_descriptor_set *set,
1695 unsigned idx);
1696
1697 void
1698 tu_update_descriptor_sets(struct tu_device *device,
1699 struct tu_cmd_buffer *cmd_buffer,
1700 VkDescriptorSet overrideSet,
1701 uint32_t descriptorWriteCount,
1702 const VkWriteDescriptorSet *pDescriptorWrites,
1703 uint32_t descriptorCopyCount,
1704 const VkCopyDescriptorSet *pDescriptorCopies);
1705
1706 void
1707 tu_update_descriptor_set_with_template(
1708 struct tu_device *device,
1709 struct tu_cmd_buffer *cmd_buffer,
1710 struct tu_descriptor_set *set,
1711 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1712 const void *pData);
1713
1714 void
1715 tu_meta_push_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1716 VkPipelineBindPoint pipelineBindPoint,
1717 VkPipelineLayout _layout,
1718 uint32_t set,
1719 uint32_t descriptorWriteCount,
1720 const VkWriteDescriptorSet *pDescriptorWrites);
1721
1722 int
1723 tu_drm_get_gpu_id(const struct tu_physical_device *dev, uint32_t *id);
1724
1725 int
1726 tu_drm_get_gmem_size(const struct tu_physical_device *dev, uint32_t *size);
1727
1728 int
1729 tu_drm_get_gmem_base(const struct tu_physical_device *dev, uint64_t *base);
1730
1731 int
1732 tu_drm_submitqueue_new(const struct tu_device *dev,
1733 int priority,
1734 uint32_t *queue_id);
1735
1736 void
1737 tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id);
1738
1739 uint32_t
1740 tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags);
1741 uint32_t
1742 tu_gem_import_dmabuf(const struct tu_device *dev,
1743 int prime_fd,
1744 uint64_t size);
1745 int
1746 tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle);
1747 void
1748 tu_gem_close(const struct tu_device *dev, uint32_t gem_handle);
1749 uint64_t
1750 tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle);
1751 uint64_t
1752 tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle);
1753
1754 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1755 \
1756 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1757 { \
1758 return (struct __tu_type *) _handle; \
1759 } \
1760 \
1761 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1762 { \
1763 return (__VkType) _obj; \
1764 }
1765
1766 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1767 \
1768 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1769 { \
1770 return (struct __tu_type *) (uintptr_t) _handle; \
1771 } \
1772 \
1773 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1774 { \
1775 return (__VkType)(uintptr_t) _obj; \
1776 }
1777
1778 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1779 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1780
1781 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1782 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1783 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1784 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1785 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1786
1787 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1788 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1789 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1790 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1791 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1792 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1793 VkDescriptorSetLayout)
1794 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1795 VkDescriptorUpdateTemplate)
1796 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1797 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1798 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1799 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1800 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1801 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1802 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1803 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1804 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1805 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1806 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1807 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1808 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1809 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1810
1811 #endif /* TU_PRIVATE_H */