turnip: improve GMEM load/store logic
[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 struct tu_descriptor_update_template_entry entry[0];
694 };
695
696 struct tu_buffer
697 {
698 VkDeviceSize size;
699
700 VkBufferUsageFlags usage;
701 VkBufferCreateFlags flags;
702
703 struct tu_bo *bo;
704 VkDeviceSize bo_offset;
705 };
706
707 static inline uint64_t
708 tu_buffer_iova(struct tu_buffer *buffer)
709 {
710 return buffer->bo->iova + buffer->bo_offset;
711 }
712
713 enum tu_dynamic_state_bits
714 {
715 TU_DYNAMIC_VIEWPORT = 1 << 0,
716 TU_DYNAMIC_SCISSOR = 1 << 1,
717 TU_DYNAMIC_LINE_WIDTH = 1 << 2,
718 TU_DYNAMIC_DEPTH_BIAS = 1 << 3,
719 TU_DYNAMIC_BLEND_CONSTANTS = 1 << 4,
720 TU_DYNAMIC_DEPTH_BOUNDS = 1 << 5,
721 TU_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6,
722 TU_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7,
723 TU_DYNAMIC_STENCIL_REFERENCE = 1 << 8,
724 TU_DYNAMIC_DISCARD_RECTANGLE = 1 << 9,
725 TU_DYNAMIC_ALL = (1 << 10) - 1,
726 };
727
728 struct tu_vertex_binding
729 {
730 struct tu_buffer *buffer;
731 VkDeviceSize offset;
732 };
733
734 struct tu_viewport_state
735 {
736 uint32_t count;
737 VkViewport viewports[MAX_VIEWPORTS];
738 };
739
740 struct tu_scissor_state
741 {
742 uint32_t count;
743 VkRect2D scissors[MAX_SCISSORS];
744 };
745
746 struct tu_discard_rectangle_state
747 {
748 uint32_t count;
749 VkRect2D rectangles[MAX_DISCARD_RECTANGLES];
750 };
751
752 struct tu_dynamic_state
753 {
754 /**
755 * Bitmask of (1 << VK_DYNAMIC_STATE_*).
756 * Defines the set of saved dynamic state.
757 */
758 uint32_t mask;
759
760 struct tu_viewport_state viewport;
761
762 struct tu_scissor_state scissor;
763
764 float line_width;
765
766 struct
767 {
768 float bias;
769 float clamp;
770 float slope;
771 } depth_bias;
772
773 float blend_constants[4];
774
775 struct
776 {
777 float min;
778 float max;
779 } depth_bounds;
780
781 struct
782 {
783 uint32_t front;
784 uint32_t back;
785 } stencil_compare_mask;
786
787 struct
788 {
789 uint32_t front;
790 uint32_t back;
791 } stencil_write_mask;
792
793 struct
794 {
795 uint32_t front;
796 uint32_t back;
797 } stencil_reference;
798
799 struct tu_discard_rectangle_state discard_rectangle;
800 };
801
802 extern const struct tu_dynamic_state default_dynamic_state;
803
804 const char *
805 tu_get_debug_option_name(int id);
806
807 const char *
808 tu_get_perftest_option_name(int id);
809
810 struct tu_descriptor_state
811 {
812 struct tu_descriptor_set *sets[MAX_SETS];
813 uint32_t valid;
814 struct tu_push_descriptor_set push_set;
815 bool push_dirty;
816 uint32_t dynamic_descriptors[MAX_DYNAMIC_BUFFERS * A6XX_TEX_CONST_DWORDS];
817 uint32_t input_attachments[MAX_RTS * A6XX_TEX_CONST_DWORDS];
818 };
819
820 struct tu_tile
821 {
822 uint8_t pipe;
823 uint8_t slot;
824 VkOffset2D begin;
825 VkOffset2D end;
826 };
827
828 struct tu_tiling_config
829 {
830 VkRect2D render_area;
831
832 /* position and size of the first tile */
833 VkRect2D tile0;
834 /* number of tiles */
835 VkExtent2D tile_count;
836
837 /* size of the first VSC pipe */
838 VkExtent2D pipe0;
839 /* number of VSC pipes */
840 VkExtent2D pipe_count;
841
842 /* pipe register values */
843 uint32_t pipe_config[MAX_VSC_PIPES];
844 uint32_t pipe_sizes[MAX_VSC_PIPES];
845
846 /* Whether sysmem rendering must be used */
847 bool force_sysmem;
848 };
849
850 enum tu_cmd_dirty_bits
851 {
852 TU_CMD_DIRTY_PIPELINE = 1 << 0,
853 TU_CMD_DIRTY_COMPUTE_PIPELINE = 1 << 1,
854 TU_CMD_DIRTY_VERTEX_BUFFERS = 1 << 2,
855 TU_CMD_DIRTY_DESCRIPTOR_SETS = 1 << 3,
856 TU_CMD_DIRTY_COMPUTE_DESCRIPTOR_SETS = 1 << 4,
857 TU_CMD_DIRTY_PUSH_CONSTANTS = 1 << 5,
858 TU_CMD_DIRTY_STREAMOUT_BUFFERS = 1 << 6,
859 TU_CMD_DIRTY_INPUT_ATTACHMENTS = 1 << 7,
860
861 TU_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 16,
862 TU_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 17,
863 TU_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 18,
864 TU_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 19,
865 TU_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 20,
866 TU_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 21,
867 };
868
869 struct tu_streamout_state {
870 uint16_t stride[IR3_MAX_SO_BUFFERS];
871 uint32_t ncomp[IR3_MAX_SO_BUFFERS];
872 uint32_t prog[IR3_MAX_SO_OUTPUTS * 2];
873 uint32_t prog_count;
874 uint32_t vpc_so_buf_cntl;
875 };
876
877 struct tu_cmd_state
878 {
879 uint32_t dirty;
880
881 struct tu_pipeline *pipeline;
882 struct tu_pipeline *compute_pipeline;
883
884 /* Vertex buffers */
885 struct
886 {
887 struct tu_buffer *buffers[MAX_VBS];
888 VkDeviceSize offsets[MAX_VBS];
889 } vb;
890
891 struct tu_dynamic_state dynamic;
892
893 /* Stream output buffers */
894 struct
895 {
896 struct tu_buffer *buffers[IR3_MAX_SO_BUFFERS];
897 VkDeviceSize offsets[IR3_MAX_SO_BUFFERS];
898 VkDeviceSize sizes[IR3_MAX_SO_BUFFERS];
899 } streamout_buf;
900
901 uint8_t streamout_reset;
902 uint8_t streamout_enabled;
903
904 /* Index buffer */
905 struct tu_buffer *index_buffer;
906 uint64_t index_offset;
907 uint32_t index_type;
908 uint32_t max_index_count;
909 uint64_t index_va;
910
911 const struct tu_render_pass *pass;
912 const struct tu_subpass *subpass;
913 const struct tu_framebuffer *framebuffer;
914
915 struct tu_tiling_config tiling_config;
916
917 struct tu_cs_entry tile_store_ib;
918 };
919
920 struct tu_cmd_pool
921 {
922 VkAllocationCallbacks alloc;
923 struct list_head cmd_buffers;
924 struct list_head free_cmd_buffers;
925 uint32_t queue_family_index;
926 };
927
928 struct tu_cmd_buffer_upload
929 {
930 uint8_t *map;
931 unsigned offset;
932 uint64_t size;
933 struct list_head list;
934 };
935
936 enum tu_cmd_buffer_status
937 {
938 TU_CMD_BUFFER_STATUS_INVALID,
939 TU_CMD_BUFFER_STATUS_INITIAL,
940 TU_CMD_BUFFER_STATUS_RECORDING,
941 TU_CMD_BUFFER_STATUS_EXECUTABLE,
942 TU_CMD_BUFFER_STATUS_PENDING,
943 };
944
945 struct tu_bo_list
946 {
947 uint32_t count;
948 uint32_t capacity;
949 struct drm_msm_gem_submit_bo *bo_infos;
950 };
951
952 #define TU_BO_LIST_FAILED (~0)
953
954 void
955 tu_bo_list_init(struct tu_bo_list *list);
956 void
957 tu_bo_list_destroy(struct tu_bo_list *list);
958 void
959 tu_bo_list_reset(struct tu_bo_list *list);
960 uint32_t
961 tu_bo_list_add(struct tu_bo_list *list,
962 const struct tu_bo *bo,
963 uint32_t flags);
964 VkResult
965 tu_bo_list_merge(struct tu_bo_list *list, const struct tu_bo_list *other);
966
967 /* This struct defines the layout of the scratch_bo */
968 struct tu6_control
969 {
970 uint32_t seqno; /* seqno for async CP_EVENT_WRITE, etc */
971 uint32_t _pad0;
972 volatile uint32_t vsc_overflow;
973 uint32_t _pad1;
974 /* flag set from cmdstream when VSC overflow detected: */
975 uint32_t vsc_scratch;
976 uint32_t _pad2;
977 uint32_t _pad3;
978 uint32_t _pad4;
979
980 /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
981 struct {
982 uint32_t offset;
983 uint32_t pad[7];
984 } flush_base[4];
985 };
986
987 #define ctrl_offset(member) offsetof(struct tu6_control, member)
988
989 struct tu_cmd_buffer
990 {
991 VK_LOADER_DATA _loader_data;
992
993 struct tu_device *device;
994
995 struct tu_cmd_pool *pool;
996 struct list_head pool_link;
997
998 VkCommandBufferUsageFlags usage_flags;
999 VkCommandBufferLevel level;
1000 enum tu_cmd_buffer_status status;
1001
1002 struct tu_cmd_state state;
1003 struct tu_vertex_binding vertex_bindings[MAX_VBS];
1004 uint32_t queue_family_index;
1005
1006 uint32_t push_constants[MAX_PUSH_CONSTANTS_SIZE / 4];
1007 VkShaderStageFlags push_constant_stages;
1008 struct tu_descriptor_set meta_push_descriptors;
1009
1010 struct tu_descriptor_state descriptors[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
1011
1012 struct tu_cmd_buffer_upload upload;
1013
1014 VkResult record_result;
1015
1016 struct tu_bo_list bo_list;
1017 struct tu_cs cs;
1018 struct tu_cs draw_cs;
1019 struct tu_cs draw_epilogue_cs;
1020 struct tu_cs sub_cs;
1021
1022 struct tu_bo scratch_bo;
1023 uint32_t scratch_seqno;
1024
1025 struct tu_bo vsc_data;
1026 struct tu_bo vsc_data2;
1027 uint32_t vsc_data_pitch;
1028 uint32_t vsc_data2_pitch;
1029 bool use_vsc_data;
1030
1031 bool wait_for_idle;
1032 };
1033
1034 /* Temporary struct for tracking a register state to be written, used by
1035 * a6xx-pack.h and tu_cs_emit_regs()
1036 */
1037 struct tu_reg_value {
1038 uint32_t reg;
1039 uint64_t value;
1040 bool is_address;
1041 struct tu_bo *bo;
1042 bool bo_write;
1043 uint32_t bo_offset;
1044 uint32_t bo_shift;
1045 };
1046
1047 unsigned
1048 tu6_emit_event_write(struct tu_cmd_buffer *cmd,
1049 struct tu_cs *cs,
1050 enum vgt_event_type event,
1051 bool need_seqno);
1052
1053 bool
1054 tu_get_memory_fd(struct tu_device *device,
1055 struct tu_device_memory *memory,
1056 int *pFD);
1057
1058 static inline struct tu_descriptor_state *
1059 tu_get_descriptors_state(struct tu_cmd_buffer *cmd_buffer,
1060 VkPipelineBindPoint bind_point)
1061 {
1062 return &cmd_buffer->descriptors[bind_point];
1063 }
1064
1065 /*
1066 * Takes x,y,z as exact numbers of invocations, instead of blocks.
1067 *
1068 * Limitations: Can't call normal dispatch functions without binding or
1069 * rebinding
1070 * the compute pipeline.
1071 */
1072 void
1073 tu_unaligned_dispatch(struct tu_cmd_buffer *cmd_buffer,
1074 uint32_t x,
1075 uint32_t y,
1076 uint32_t z);
1077
1078 struct tu_event
1079 {
1080 struct tu_bo bo;
1081 };
1082
1083 struct tu_shader_module;
1084
1085 #define TU_HASH_SHADER_IS_GEOM_COPY_SHADER (1 << 0)
1086 #define TU_HASH_SHADER_SISCHED (1 << 1)
1087 #define TU_HASH_SHADER_UNSAFE_MATH (1 << 2)
1088 void
1089 tu_hash_shaders(unsigned char *hash,
1090 const VkPipelineShaderStageCreateInfo **stages,
1091 const struct tu_pipeline_layout *layout,
1092 const struct tu_pipeline_key *key,
1093 uint32_t flags);
1094
1095 static inline gl_shader_stage
1096 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1097 {
1098 assert(__builtin_popcount(vk_stage) == 1);
1099 return ffs(vk_stage) - 1;
1100 }
1101
1102 static inline VkShaderStageFlagBits
1103 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1104 {
1105 return (1 << mesa_stage);
1106 }
1107
1108 #define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1109
1110 #define tu_foreach_stage(stage, stage_bits) \
1111 for (gl_shader_stage stage, \
1112 __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK); \
1113 stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
1114
1115 struct tu_shader_module
1116 {
1117 unsigned char sha1[20];
1118
1119 uint32_t code_size;
1120 const uint32_t *code[0];
1121 };
1122
1123 struct tu_shader_compile_options
1124 {
1125 struct ir3_shader_key key;
1126
1127 bool optimize;
1128 bool include_binning_pass;
1129 };
1130
1131 struct tu_push_constant_range
1132 {
1133 uint32_t lo;
1134 uint32_t count;
1135 };
1136
1137 struct tu_shader
1138 {
1139 struct ir3_shader ir3_shader;
1140
1141 struct tu_push_constant_range push_consts;
1142 unsigned attachment_idx[MAX_RTS];
1143
1144 /* This may be true for vertex shaders. When true, variants[1] is the
1145 * binning variant and binning_binary is non-NULL.
1146 */
1147 bool has_binning_pass;
1148
1149 void *binary;
1150 void *binning_binary;
1151
1152 struct ir3_shader_variant variants[0];
1153 };
1154
1155 struct tu_shader *
1156 tu_shader_create(struct tu_device *dev,
1157 gl_shader_stage stage,
1158 const VkPipelineShaderStageCreateInfo *stage_info,
1159 struct tu_pipeline_layout *layout,
1160 const VkAllocationCallbacks *alloc);
1161
1162 void
1163 tu_shader_destroy(struct tu_device *dev,
1164 struct tu_shader *shader,
1165 const VkAllocationCallbacks *alloc);
1166
1167 void
1168 tu_shader_compile_options_init(
1169 struct tu_shader_compile_options *options,
1170 const VkGraphicsPipelineCreateInfo *pipeline_info);
1171
1172 VkResult
1173 tu_shader_compile(struct tu_device *dev,
1174 struct tu_shader *shader,
1175 const struct tu_shader *next_stage,
1176 const struct tu_shader_compile_options *options,
1177 const VkAllocationCallbacks *alloc);
1178
1179 struct tu_program_descriptor_linkage
1180 {
1181 struct ir3_ubo_analysis_state ubo_state;
1182 struct ir3_const_state const_state;
1183
1184 uint32_t constlen;
1185
1186 struct tu_push_constant_range push_consts;
1187 };
1188
1189 struct tu_pipeline
1190 {
1191 struct tu_cs cs;
1192
1193 struct tu_dynamic_state dynamic_state;
1194
1195 struct tu_pipeline_layout *layout;
1196
1197 bool need_indirect_descriptor_sets;
1198 VkShaderStageFlags active_stages;
1199
1200 struct tu_streamout_state streamout;
1201
1202 struct
1203 {
1204 struct tu_bo binary_bo;
1205 struct tu_cs_entry state_ib;
1206 struct tu_cs_entry binning_state_ib;
1207
1208 struct tu_program_descriptor_linkage link[MESA_SHADER_STAGES];
1209 unsigned input_attachment_idx[MAX_RTS];
1210 } program;
1211
1212 struct
1213 {
1214 struct tu_cs_entry state_ib;
1215 } load_state;
1216
1217 struct
1218 {
1219 uint8_t bindings[MAX_VERTEX_ATTRIBS];
1220 uint32_t count;
1221
1222 uint8_t binning_bindings[MAX_VERTEX_ATTRIBS];
1223 uint32_t binning_count;
1224
1225 struct tu_cs_entry state_ib;
1226 struct tu_cs_entry binning_state_ib;
1227 } vi;
1228
1229 struct
1230 {
1231 enum pc_di_primtype primtype;
1232 bool primitive_restart;
1233 } ia;
1234
1235 struct
1236 {
1237 struct tu_cs_entry state_ib;
1238 } vp;
1239
1240 struct
1241 {
1242 uint32_t gras_su_cntl;
1243 struct tu_cs_entry state_ib;
1244 } rast;
1245
1246 struct
1247 {
1248 struct tu_cs_entry state_ib;
1249 } ds;
1250
1251 struct
1252 {
1253 struct tu_cs_entry state_ib;
1254 } blend;
1255
1256 struct
1257 {
1258 uint32_t local_size[3];
1259 } compute;
1260 };
1261
1262 void
1263 tu6_emit_viewport(struct tu_cs *cs, const VkViewport *viewport);
1264
1265 void
1266 tu6_emit_scissor(struct tu_cs *cs, const VkRect2D *scissor);
1267
1268 void
1269 tu6_emit_gras_su_cntl(struct tu_cs *cs,
1270 uint32_t gras_su_cntl,
1271 float line_width);
1272
1273 void
1274 tu6_emit_depth_bias(struct tu_cs *cs,
1275 float constant_factor,
1276 float clamp,
1277 float slope_factor);
1278
1279 void
1280 tu6_emit_stencil_compare_mask(struct tu_cs *cs,
1281 uint32_t front,
1282 uint32_t back);
1283
1284 void
1285 tu6_emit_stencil_write_mask(struct tu_cs *cs, uint32_t front, uint32_t back);
1286
1287 void
1288 tu6_emit_stencil_reference(struct tu_cs *cs, uint32_t front, uint32_t back);
1289
1290 void
1291 tu6_emit_blend_constants(struct tu_cs *cs, const float constants[4]);
1292
1293 void tu6_emit_msaa(struct tu_cs *cs, VkSampleCountFlagBits samples);
1294
1295 void tu6_emit_window_scissor(struct tu_cs *cs, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
1296
1297 void tu6_emit_window_offset(struct tu_cs *cs, uint32_t x1, uint32_t y1);
1298
1299 struct tu_image_view;
1300
1301 void
1302 tu_resolve_sysmem(struct tu_cmd_buffer *cmd,
1303 struct tu_cs *cs,
1304 struct tu_image_view *src,
1305 struct tu_image_view *dst,
1306 uint32_t layers,
1307 const VkRect2D *rect);
1308
1309 void
1310 tu_clear_sysmem_attachment(struct tu_cmd_buffer *cmd,
1311 struct tu_cs *cs,
1312 uint32_t a,
1313 const VkRenderPassBeginInfo *info);
1314
1315 void
1316 tu_clear_gmem_attachment(struct tu_cmd_buffer *cmd,
1317 struct tu_cs *cs,
1318 uint32_t a,
1319 const VkRenderPassBeginInfo *info);
1320
1321 void
1322 tu_load_gmem_attachment(struct tu_cmd_buffer *cmd,
1323 struct tu_cs *cs,
1324 uint32_t a,
1325 bool force_load);
1326
1327 /* expose this function to be able to emit load without checking LOAD_OP */
1328 void
1329 tu_emit_load_gmem_attachment(struct tu_cmd_buffer *cmd, struct tu_cs *cs, uint32_t a);
1330
1331 /* note: gmem store can also resolve */
1332 void
1333 tu_store_gmem_attachment(struct tu_cmd_buffer *cmd,
1334 struct tu_cs *cs,
1335 uint32_t a,
1336 uint32_t gmem_a);
1337
1338 struct tu_userdata_info *
1339 tu_lookup_user_sgpr(struct tu_pipeline *pipeline,
1340 gl_shader_stage stage,
1341 int idx);
1342
1343 struct tu_shader_variant *
1344 tu_get_shader(struct tu_pipeline *pipeline, gl_shader_stage stage);
1345
1346 struct tu_graphics_pipeline_create_info
1347 {
1348 bool use_rectlist;
1349 bool db_depth_clear;
1350 bool db_stencil_clear;
1351 bool db_depth_disable_expclear;
1352 bool db_stencil_disable_expclear;
1353 bool db_flush_depth_inplace;
1354 bool db_flush_stencil_inplace;
1355 bool db_resummarize;
1356 uint32_t custom_blend_mode;
1357 };
1358
1359 enum tu_supported_formats {
1360 FMT_VERTEX = 1,
1361 FMT_TEXTURE = 2,
1362 FMT_COLOR = 4,
1363 };
1364
1365 struct tu_native_format
1366 {
1367 enum a6xx_format fmt : 8;
1368 enum a3xx_color_swap swap : 8;
1369 enum a6xx_tile_mode tile_mode : 8;
1370 enum tu_supported_formats supported : 8;
1371 };
1372
1373 struct tu_native_format tu6_format_vtx(VkFormat format);
1374 struct tu_native_format tu6_format_color(VkFormat format, enum a6xx_tile_mode tile_mode);
1375 struct tu_native_format tu6_format_texture(VkFormat format, enum a6xx_tile_mode tile_mode);
1376
1377 static inline enum a6xx_format
1378 tu6_base_format(VkFormat format)
1379 {
1380 /* note: tu6_format_color doesn't care about tiling for .fmt field */
1381 return tu6_format_color(format, TILE6_LINEAR).fmt;
1382 }
1383
1384 enum a6xx_depth_format tu6_pipe2depth(VkFormat format);
1385
1386 struct tu_image
1387 {
1388 VkImageType type;
1389 /* The original VkFormat provided by the client. This may not match any
1390 * of the actual surface formats.
1391 */
1392 VkFormat vk_format;
1393 VkImageAspectFlags aspects;
1394 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1395 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1396 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1397 VkExtent3D extent;
1398 uint32_t level_count;
1399 uint32_t layer_count;
1400 VkSampleCountFlagBits samples;
1401
1402 struct fdl_layout layout;
1403
1404 unsigned queue_family_mask;
1405 bool exclusive;
1406 bool shareable;
1407
1408 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1409 VkDeviceMemory owned_memory;
1410
1411 /* Set when bound */
1412 struct tu_bo *bo;
1413 VkDeviceSize bo_offset;
1414 };
1415
1416 unsigned
1417 tu_image_queue_family_mask(const struct tu_image *image,
1418 uint32_t family,
1419 uint32_t queue_family);
1420
1421 static inline uint32_t
1422 tu_get_layerCount(const struct tu_image *image,
1423 const VkImageSubresourceRange *range)
1424 {
1425 return range->layerCount == VK_REMAINING_ARRAY_LAYERS
1426 ? image->layer_count - range->baseArrayLayer
1427 : range->layerCount;
1428 }
1429
1430 static inline uint32_t
1431 tu_get_levelCount(const struct tu_image *image,
1432 const VkImageSubresourceRange *range)
1433 {
1434 return range->levelCount == VK_REMAINING_MIP_LEVELS
1435 ? image->level_count - range->baseMipLevel
1436 : range->levelCount;
1437 }
1438
1439 enum a3xx_msaa_samples
1440 tu_msaa_samples(uint32_t samples);
1441 enum a6xx_tex_fetchsize
1442 tu6_fetchsize(VkFormat format);
1443
1444 struct tu_image_view
1445 {
1446 struct tu_image *image; /**< VkImageViewCreateInfo::image */
1447
1448 uint64_t base_addr;
1449 uint64_t ubwc_addr;
1450 uint32_t layer_size;
1451 uint32_t ubwc_layer_size;
1452
1453 /* used to determine if fast gmem store path can be used */
1454 VkExtent2D extent;
1455 bool need_y2_align;
1456
1457 bool ubwc_enabled;
1458
1459 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1460
1461 /* Descriptor for use as a storage image as opposed to a sampled image.
1462 * This has a few differences for cube maps (e.g. type).
1463 */
1464 uint32_t storage_descriptor[A6XX_TEX_CONST_DWORDS];
1465
1466 /* pre-filled register values */
1467 uint32_t PITCH;
1468 uint32_t FLAG_BUFFER_PITCH;
1469
1470 uint32_t RB_MRT_BUF_INFO;
1471 uint32_t SP_FS_MRT_REG;
1472
1473 uint32_t SP_PS_2D_SRC_INFO;
1474 uint32_t SP_PS_2D_SRC_SIZE;
1475
1476 uint32_t RB_2D_DST_INFO;
1477
1478 uint32_t RB_BLIT_DST_INFO;
1479 };
1480
1481 struct tu_sampler {
1482 uint32_t descriptor[A6XX_TEX_SAMP_DWORDS];
1483 };
1484
1485 void
1486 tu_cs_image_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1487
1488 void
1489 tu_cs_image_ref_2d(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer, bool src);
1490
1491 void
1492 tu_cs_image_flag_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1493
1494 VkResult
1495 tu_image_create(VkDevice _device,
1496 const VkImageCreateInfo *pCreateInfo,
1497 const VkAllocationCallbacks *alloc,
1498 VkImage *pImage,
1499 uint64_t modifier);
1500
1501 VkResult
1502 tu_image_from_gralloc(VkDevice device_h,
1503 const VkImageCreateInfo *base_info,
1504 const VkNativeBufferANDROID *gralloc_info,
1505 const VkAllocationCallbacks *alloc,
1506 VkImage *out_image_h);
1507
1508 void
1509 tu_image_view_init(struct tu_image_view *view,
1510 const VkImageViewCreateInfo *pCreateInfo);
1511
1512 struct tu_buffer_view
1513 {
1514 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1515
1516 struct tu_buffer *buffer;
1517 };
1518 void
1519 tu_buffer_view_init(struct tu_buffer_view *view,
1520 struct tu_device *device,
1521 const VkBufferViewCreateInfo *pCreateInfo);
1522
1523 static inline struct VkExtent3D
1524 tu_sanitize_image_extent(const VkImageType imageType,
1525 const struct VkExtent3D imageExtent)
1526 {
1527 switch (imageType) {
1528 case VK_IMAGE_TYPE_1D:
1529 return (VkExtent3D) { imageExtent.width, 1, 1 };
1530 case VK_IMAGE_TYPE_2D:
1531 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1532 case VK_IMAGE_TYPE_3D:
1533 return imageExtent;
1534 default:
1535 unreachable("invalid image type");
1536 }
1537 }
1538
1539 static inline struct VkOffset3D
1540 tu_sanitize_image_offset(const VkImageType imageType,
1541 const struct VkOffset3D imageOffset)
1542 {
1543 switch (imageType) {
1544 case VK_IMAGE_TYPE_1D:
1545 return (VkOffset3D) { imageOffset.x, 0, 0 };
1546 case VK_IMAGE_TYPE_2D:
1547 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
1548 case VK_IMAGE_TYPE_3D:
1549 return imageOffset;
1550 default:
1551 unreachable("invalid image type");
1552 }
1553 }
1554
1555 struct tu_attachment_info
1556 {
1557 struct tu_image_view *attachment;
1558 };
1559
1560 struct tu_framebuffer
1561 {
1562 uint32_t width;
1563 uint32_t height;
1564 uint32_t layers;
1565
1566 uint32_t attachment_count;
1567 struct tu_attachment_info attachments[0];
1568 };
1569
1570 struct tu_subpass_attachment
1571 {
1572 uint32_t attachment;
1573 };
1574
1575 struct tu_subpass
1576 {
1577 uint32_t input_count;
1578 uint32_t color_count;
1579 struct tu_subpass_attachment *input_attachments;
1580 struct tu_subpass_attachment *color_attachments;
1581 struct tu_subpass_attachment *resolve_attachments;
1582 struct tu_subpass_attachment depth_stencil_attachment;
1583
1584 VkSampleCountFlagBits samples;
1585
1586 /* pre-filled register values */
1587 uint32_t render_components;
1588 uint32_t srgb_cntl;
1589 };
1590
1591 struct tu_render_pass_attachment
1592 {
1593 VkFormat format;
1594 uint32_t samples;
1595 uint32_t cpp;
1596 VkImageAspectFlags clear_mask;
1597 bool load;
1598 bool store;
1599 int32_t gmem_offset;
1600 };
1601
1602 struct tu_render_pass
1603 {
1604 uint32_t attachment_count;
1605 uint32_t subpass_count;
1606 uint32_t gmem_pixels;
1607 struct tu_subpass_attachment *subpass_attachments;
1608 struct tu_render_pass_attachment *attachments;
1609 struct tu_subpass subpasses[0];
1610 };
1611
1612 VkResult
1613 tu_device_init_meta(struct tu_device *device);
1614 void
1615 tu_device_finish_meta(struct tu_device *device);
1616
1617 struct tu_query_pool
1618 {
1619 VkQueryType type;
1620 uint32_t stride;
1621 uint64_t size;
1622 uint32_t pipeline_statistics;
1623 struct tu_bo bo;
1624 };
1625
1626 struct tu_semaphore
1627 {
1628 uint32_t syncobj;
1629 uint32_t temp_syncobj;
1630 };
1631
1632 void
1633 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1634 VkPipelineBindPoint bind_point,
1635 struct tu_descriptor_set *set,
1636 unsigned idx);
1637
1638 void
1639 tu_update_descriptor_sets(struct tu_device *device,
1640 struct tu_cmd_buffer *cmd_buffer,
1641 VkDescriptorSet overrideSet,
1642 uint32_t descriptorWriteCount,
1643 const VkWriteDescriptorSet *pDescriptorWrites,
1644 uint32_t descriptorCopyCount,
1645 const VkCopyDescriptorSet *pDescriptorCopies);
1646
1647 void
1648 tu_update_descriptor_set_with_template(
1649 struct tu_device *device,
1650 struct tu_cmd_buffer *cmd_buffer,
1651 struct tu_descriptor_set *set,
1652 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1653 const void *pData);
1654
1655 void
1656 tu_meta_push_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1657 VkPipelineBindPoint pipelineBindPoint,
1658 VkPipelineLayout _layout,
1659 uint32_t set,
1660 uint32_t descriptorWriteCount,
1661 const VkWriteDescriptorSet *pDescriptorWrites);
1662
1663 int
1664 tu_drm_get_gpu_id(const struct tu_physical_device *dev, uint32_t *id);
1665
1666 int
1667 tu_drm_get_gmem_size(const struct tu_physical_device *dev, uint32_t *size);
1668
1669 int
1670 tu_drm_get_gmem_base(const struct tu_physical_device *dev, uint64_t *base);
1671
1672 int
1673 tu_drm_submitqueue_new(const struct tu_device *dev,
1674 int priority,
1675 uint32_t *queue_id);
1676
1677 void
1678 tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id);
1679
1680 uint32_t
1681 tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags);
1682 uint32_t
1683 tu_gem_import_dmabuf(const struct tu_device *dev,
1684 int prime_fd,
1685 uint64_t size);
1686 int
1687 tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle);
1688 void
1689 tu_gem_close(const struct tu_device *dev, uint32_t gem_handle);
1690 uint64_t
1691 tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle);
1692 uint64_t
1693 tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle);
1694
1695 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1696 \
1697 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1698 { \
1699 return (struct __tu_type *) _handle; \
1700 } \
1701 \
1702 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1703 { \
1704 return (__VkType) _obj; \
1705 }
1706
1707 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1708 \
1709 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1710 { \
1711 return (struct __tu_type *) (uintptr_t) _handle; \
1712 } \
1713 \
1714 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1715 { \
1716 return (__VkType)(uintptr_t) _obj; \
1717 }
1718
1719 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1720 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1721
1722 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1723 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1724 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1725 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1726 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1727
1728 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1729 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1730 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1731 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1732 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1733 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1734 VkDescriptorSetLayout)
1735 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1736 VkDescriptorUpdateTemplate)
1737 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1738 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1739 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1740 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1741 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1742 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1743 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1744 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1745 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1746 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1747 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1748 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1749 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1750 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1751
1752 #endif /* TU_PRIVATE_H */