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