turnip: add tu_cs_mode
[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_queue
400 {
401 VK_LOADER_DATA _loader_data;
402 struct tu_device *device;
403 uint32_t queue_family_index;
404 int queue_idx;
405 VkDeviceQueueCreateFlags flags;
406
407 uint32_t msm_queue_id;
408 int submit_fence_fd;
409 };
410
411 struct tu_device
412 {
413 VK_LOADER_DATA _loader_data;
414
415 VkAllocationCallbacks alloc;
416
417 struct tu_instance *instance;
418
419 struct tu_meta_state meta_state;
420
421 struct tu_queue *queues[TU_MAX_QUEUE_FAMILIES];
422 int queue_count[TU_MAX_QUEUE_FAMILIES];
423
424 struct tu_physical_device *physical_device;
425
426 /* Backup in-memory cache to be used if the app doesn't provide one */
427 struct tu_pipeline_cache *mem_cache;
428
429 struct list_head shader_slabs;
430 mtx_t shader_slab_mutex;
431
432 struct tu_device_extension_table enabled_extensions;
433 };
434
435 struct tu_bo
436 {
437 uint32_t gem_handle;
438 uint64_t size;
439 uint64_t iova;
440 void *map;
441 };
442
443 VkResult
444 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size);
445 void
446 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
447 VkResult
448 tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
449
450 struct tu_cs_entry
451 {
452 /* No ownership */
453 const struct tu_bo *bo;
454
455 uint32_t size;
456 uint64_t offset;
457 };
458
459 enum tu_cs_mode
460 {
461
462 /*
463 * A command stream in TU_CS_MODE_GROW mode grows automatically whenever it
464 * is full. tu_cs_begin must be called before command packet emission and
465 * tu_cs_end must be called after.
466 *
467 * This mode may create multiple entries internally. The entries must be
468 * submitted together.
469 */
470 TU_CS_MODE_GROW,
471
472 /*
473 * A command stream in TU_CS_MODE_EXTERNAL mode wraps an external,
474 * fixed-size buffer. tu_cs_begin and tu_cs_end are optional and have no
475 * effect on it.
476 *
477 * This mode does not create any entry or any BO.
478 */
479 TU_CS_MODE_EXTERNAL,
480 };
481
482 struct tu_cs
483 {
484 uint32_t *start;
485 uint32_t *cur;
486 uint32_t *reserved_end;
487 uint32_t *end;
488
489 enum tu_cs_mode mode;
490 uint32_t next_bo_size;
491
492 struct tu_cs_entry *entries;
493 uint32_t entry_count;
494 uint32_t entry_capacity;
495
496 struct tu_bo **bos;
497 uint32_t bo_count;
498 uint32_t bo_capacity;
499 };
500
501 struct tu_device_memory
502 {
503 struct tu_bo bo;
504 VkDeviceSize size;
505
506 /* for dedicated allocations */
507 struct tu_image *image;
508 struct tu_buffer *buffer;
509
510 uint32_t type_index;
511 void *map;
512 void *user_ptr;
513 };
514
515 struct tu_descriptor_range
516 {
517 uint64_t va;
518 uint32_t size;
519 };
520
521 struct tu_descriptor_set
522 {
523 const struct tu_descriptor_set_layout *layout;
524 uint32_t size;
525
526 uint64_t va;
527 uint32_t *mapped_ptr;
528 struct tu_descriptor_range *dynamic_descriptors;
529 };
530
531 struct tu_push_descriptor_set
532 {
533 struct tu_descriptor_set set;
534 uint32_t capacity;
535 };
536
537 struct tu_descriptor_pool_entry
538 {
539 uint32_t offset;
540 uint32_t size;
541 struct tu_descriptor_set *set;
542 };
543
544 struct tu_descriptor_pool
545 {
546 uint8_t *mapped_ptr;
547 uint64_t current_offset;
548 uint64_t size;
549
550 uint8_t *host_memory_base;
551 uint8_t *host_memory_ptr;
552 uint8_t *host_memory_end;
553
554 uint32_t entry_count;
555 uint32_t max_entry_count;
556 struct tu_descriptor_pool_entry entries[0];
557 };
558
559 struct tu_descriptor_update_template_entry
560 {
561 VkDescriptorType descriptor_type;
562
563 /* The number of descriptors to update */
564 uint32_t descriptor_count;
565
566 /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
567 */
568 uint32_t dst_offset;
569
570 /* In dwords. Not valid/used for dynamic descriptors */
571 uint32_t dst_stride;
572
573 uint32_t buffer_offset;
574
575 /* Only valid for combined image samplers and samplers */
576 uint16_t has_sampler;
577
578 /* In bytes */
579 size_t src_offset;
580 size_t src_stride;
581
582 /* For push descriptors */
583 const uint32_t *immutable_samplers;
584 };
585
586 struct tu_descriptor_update_template
587 {
588 uint32_t entry_count;
589 VkPipelineBindPoint bind_point;
590 struct tu_descriptor_update_template_entry entry[0];
591 };
592
593 struct tu_buffer
594 {
595 VkDeviceSize size;
596
597 VkBufferUsageFlags usage;
598 VkBufferCreateFlags flags;
599 };
600
601 enum tu_dynamic_state_bits
602 {
603 TU_DYNAMIC_VIEWPORT = 1 << 0,
604 TU_DYNAMIC_SCISSOR = 1 << 1,
605 TU_DYNAMIC_LINE_WIDTH = 1 << 2,
606 TU_DYNAMIC_DEPTH_BIAS = 1 << 3,
607 TU_DYNAMIC_BLEND_CONSTANTS = 1 << 4,
608 TU_DYNAMIC_DEPTH_BOUNDS = 1 << 5,
609 TU_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6,
610 TU_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7,
611 TU_DYNAMIC_STENCIL_REFERENCE = 1 << 8,
612 TU_DYNAMIC_DISCARD_RECTANGLE = 1 << 9,
613 TU_DYNAMIC_ALL = (1 << 10) - 1,
614 };
615
616 struct tu_vertex_binding
617 {
618 struct tu_buffer *buffer;
619 VkDeviceSize offset;
620 };
621
622 struct tu_viewport_state
623 {
624 uint32_t count;
625 VkViewport viewports[MAX_VIEWPORTS];
626 };
627
628 struct tu_scissor_state
629 {
630 uint32_t count;
631 VkRect2D scissors[MAX_SCISSORS];
632 };
633
634 struct tu_discard_rectangle_state
635 {
636 uint32_t count;
637 VkRect2D rectangles[MAX_DISCARD_RECTANGLES];
638 };
639
640 struct tu_dynamic_state
641 {
642 /**
643 * Bitmask of (1 << VK_DYNAMIC_STATE_*).
644 * Defines the set of saved dynamic state.
645 */
646 uint32_t mask;
647
648 struct tu_viewport_state viewport;
649
650 struct tu_scissor_state scissor;
651
652 float line_width;
653
654 struct
655 {
656 float bias;
657 float clamp;
658 float slope;
659 } depth_bias;
660
661 float blend_constants[4];
662
663 struct
664 {
665 float min;
666 float max;
667 } depth_bounds;
668
669 struct
670 {
671 uint32_t front;
672 uint32_t back;
673 } stencil_compare_mask;
674
675 struct
676 {
677 uint32_t front;
678 uint32_t back;
679 } stencil_write_mask;
680
681 struct
682 {
683 uint32_t front;
684 uint32_t back;
685 } stencil_reference;
686
687 struct tu_discard_rectangle_state discard_rectangle;
688 };
689
690 extern const struct tu_dynamic_state default_dynamic_state;
691
692 const char *
693 tu_get_debug_option_name(int id);
694
695 const char *
696 tu_get_perftest_option_name(int id);
697
698 /**
699 * Attachment state when recording a renderpass instance.
700 *
701 * The clear value is valid only if there exists a pending clear.
702 */
703 struct tu_attachment_state
704 {
705 VkImageAspectFlags pending_clear_aspects;
706 uint32_t cleared_views;
707 VkClearValue clear_value;
708 VkImageLayout current_layout;
709 };
710
711 struct tu_descriptor_state
712 {
713 struct tu_descriptor_set *sets[MAX_SETS];
714 uint32_t dirty;
715 uint32_t valid;
716 struct tu_push_descriptor_set push_set;
717 bool push_dirty;
718 uint32_t dynamic_buffers[4 * MAX_DYNAMIC_BUFFERS];
719 };
720
721 struct tu_tile
722 {
723 uint8_t pipe;
724 uint8_t slot;
725 VkOffset2D begin;
726 VkOffset2D end;
727 };
728
729 struct tu_tiling_config
730 {
731 VkRect2D render_area;
732 uint32_t buffer_cpp[MAX_RTS + 2];
733 uint32_t buffer_count;
734
735 /* position and size of the first tile */
736 VkRect2D tile0;
737 /* number of tiles */
738 VkExtent2D tile_count;
739
740 uint32_t gmem_offsets[MAX_RTS + 2];
741
742 /* size of the first VSC pipe */
743 VkExtent2D pipe0;
744 /* number of VSC pipes */
745 VkExtent2D pipe_count;
746
747 /* pipe register values */
748 uint32_t pipe_config[MAX_VSC_PIPES];
749 uint32_t pipe_sizes[MAX_VSC_PIPES];
750 };
751
752 struct tu_cmd_state
753 {
754 /* Vertex descriptors */
755 uint64_t vb_va;
756 unsigned vb_size;
757
758 struct tu_dynamic_state dynamic;
759
760 /* Index buffer */
761 struct tu_buffer *index_buffer;
762 uint64_t index_offset;
763 uint32_t index_type;
764 uint32_t max_index_count;
765 uint64_t index_va;
766
767 const struct tu_render_pass *pass;
768 const struct tu_subpass *subpass;
769 const struct tu_framebuffer *framebuffer;
770 struct tu_attachment_state *attachments;
771
772 struct tu_tiling_config tiling_config;
773 };
774
775 struct tu_cmd_pool
776 {
777 VkAllocationCallbacks alloc;
778 struct list_head cmd_buffers;
779 struct list_head free_cmd_buffers;
780 uint32_t queue_family_index;
781 };
782
783 struct tu_cmd_buffer_upload
784 {
785 uint8_t *map;
786 unsigned offset;
787 uint64_t size;
788 struct list_head list;
789 };
790
791 enum tu_cmd_buffer_status
792 {
793 TU_CMD_BUFFER_STATUS_INVALID,
794 TU_CMD_BUFFER_STATUS_INITIAL,
795 TU_CMD_BUFFER_STATUS_RECORDING,
796 TU_CMD_BUFFER_STATUS_EXECUTABLE,
797 TU_CMD_BUFFER_STATUS_PENDING,
798 };
799
800 struct tu_bo_list
801 {
802 uint32_t count;
803 uint32_t capacity;
804 struct drm_msm_gem_submit_bo *bo_infos;
805 };
806
807 #define TU_BO_LIST_FAILED (~0)
808
809 void
810 tu_bo_list_init(struct tu_bo_list *list);
811 void
812 tu_bo_list_destroy(struct tu_bo_list *list);
813 void
814 tu_bo_list_reset(struct tu_bo_list *list);
815 uint32_t
816 tu_bo_list_add(struct tu_bo_list *list,
817 const struct tu_bo *bo,
818 uint32_t flags);
819 VkResult
820 tu_bo_list_merge(struct tu_bo_list *list, const struct tu_bo_list *other);
821
822 struct tu_cmd_buffer
823 {
824 VK_LOADER_DATA _loader_data;
825
826 struct tu_device *device;
827
828 struct tu_cmd_pool *pool;
829 struct list_head pool_link;
830
831 VkCommandBufferUsageFlags usage_flags;
832 VkCommandBufferLevel level;
833 enum tu_cmd_buffer_status status;
834
835 struct tu_cmd_state state;
836 struct tu_vertex_binding vertex_bindings[MAX_VBS];
837 uint32_t queue_family_index;
838
839 uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
840 VkShaderStageFlags push_constant_stages;
841 struct tu_descriptor_set meta_push_descriptors;
842
843 struct tu_descriptor_state descriptors[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
844
845 struct tu_cmd_buffer_upload upload;
846
847 VkResult record_result;
848
849 struct tu_bo_list bo_list;
850 struct tu_cs cs;
851
852 uint16_t marker_reg;
853 uint32_t marker_seqno;
854
855 struct tu_bo scratch_bo;
856 uint32_t scratch_seqno;
857
858 /* current cs; command packets are always emitted to it */
859 struct tu_cs *cur_cs;
860 };
861
862 bool
863 tu_get_memory_fd(struct tu_device *device,
864 struct tu_device_memory *memory,
865 int *pFD);
866
867 /*
868 * Takes x,y,z as exact numbers of invocations, instead of blocks.
869 *
870 * Limitations: Can't call normal dispatch functions without binding or
871 * rebinding
872 * the compute pipeline.
873 */
874 void
875 tu_unaligned_dispatch(struct tu_cmd_buffer *cmd_buffer,
876 uint32_t x,
877 uint32_t y,
878 uint32_t z);
879
880 struct tu_event
881 {
882 uint64_t *map;
883 };
884
885 struct tu_shader_module;
886
887 #define TU_HASH_SHADER_IS_GEOM_COPY_SHADER (1 << 0)
888 #define TU_HASH_SHADER_SISCHED (1 << 1)
889 #define TU_HASH_SHADER_UNSAFE_MATH (1 << 2)
890 void
891 tu_hash_shaders(unsigned char *hash,
892 const VkPipelineShaderStageCreateInfo **stages,
893 const struct tu_pipeline_layout *layout,
894 const struct tu_pipeline_key *key,
895 uint32_t flags);
896
897 static inline gl_shader_stage
898 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
899 {
900 assert(__builtin_popcount(vk_stage) == 1);
901 return ffs(vk_stage) - 1;
902 }
903
904 static inline VkShaderStageFlagBits
905 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
906 {
907 return (1 << mesa_stage);
908 }
909
910 #define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
911
912 #define tu_foreach_stage(stage, stage_bits) \
913 for (gl_shader_stage stage, \
914 __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK); \
915 stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
916
917 struct tu_shader_module
918 {
919 struct nir_shader *nir;
920 unsigned char sha1[20];
921 uint32_t size;
922 char data[0];
923 };
924
925 struct tu_pipeline
926 {
927 struct tu_device *device;
928 struct tu_dynamic_state dynamic_state;
929
930 struct tu_pipeline_layout *layout;
931
932 bool need_indirect_descriptor_sets;
933 VkShaderStageFlags active_stages;
934 };
935
936 struct tu_userdata_info *
937 tu_lookup_user_sgpr(struct tu_pipeline *pipeline,
938 gl_shader_stage stage,
939 int idx);
940
941 struct tu_shader_variant *
942 tu_get_shader(struct tu_pipeline *pipeline, gl_shader_stage stage);
943
944 struct tu_graphics_pipeline_create_info
945 {
946 bool use_rectlist;
947 bool db_depth_clear;
948 bool db_stencil_clear;
949 bool db_depth_disable_expclear;
950 bool db_stencil_disable_expclear;
951 bool db_flush_depth_inplace;
952 bool db_flush_stencil_inplace;
953 bool db_resummarize;
954 uint32_t custom_blend_mode;
955 };
956
957 VkResult
958 tu_graphics_pipeline_create(
959 VkDevice device,
960 VkPipelineCache cache,
961 const VkGraphicsPipelineCreateInfo *pCreateInfo,
962 const struct tu_graphics_pipeline_create_info *extra,
963 const VkAllocationCallbacks *alloc,
964 VkPipeline *pPipeline);
965
966 struct vk_format_description;
967 uint32_t
968 tu_translate_buffer_dataformat(const struct vk_format_description *desc,
969 int first_non_void);
970 uint32_t
971 tu_translate_buffer_numformat(const struct vk_format_description *desc,
972 int first_non_void);
973 uint32_t
974 tu_translate_colorformat(VkFormat format);
975 uint32_t
976 tu_translate_color_numformat(VkFormat format,
977 const struct vk_format_description *desc,
978 int first_non_void);
979 uint32_t
980 tu_colorformat_endian_swap(uint32_t colorformat);
981 unsigned
982 tu_translate_colorswap(VkFormat format, bool do_endian_swap);
983 uint32_t
984 tu_translate_dbformat(VkFormat format);
985 uint32_t
986 tu_translate_tex_dataformat(VkFormat format,
987 const struct vk_format_description *desc,
988 int first_non_void);
989 uint32_t
990 tu_translate_tex_numformat(VkFormat format,
991 const struct vk_format_description *desc,
992 int first_non_void);
993 bool
994 tu_format_pack_clear_color(VkFormat format,
995 uint32_t clear_vals[2],
996 VkClearColorValue *value);
997 bool
998 tu_is_colorbuffer_format_supported(VkFormat format, bool *blendable);
999 bool
1000 tu_dcc_formats_compatible(VkFormat format1, VkFormat format2);
1001
1002 struct tu_image_level
1003 {
1004 VkDeviceSize offset;
1005 VkDeviceSize size;
1006 uint32_t pitch;
1007 };
1008
1009 struct tu_image
1010 {
1011 VkImageType type;
1012 /* The original VkFormat provided by the client. This may not match any
1013 * of the actual surface formats.
1014 */
1015 VkFormat vk_format;
1016 VkImageAspectFlags aspects;
1017 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1018 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1019 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1020 VkExtent3D extent;
1021 uint32_t level_count;
1022 uint32_t layer_count;
1023
1024 VkDeviceSize size;
1025 uint32_t alignment;
1026
1027 /* memory layout */
1028 VkDeviceSize layer_size;
1029 struct tu_image_level levels[15];
1030 unsigned tile_mode;
1031
1032 unsigned queue_family_mask;
1033 bool exclusive;
1034 bool shareable;
1035
1036 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1037 VkDeviceMemory owned_memory;
1038
1039 /* Set when bound */
1040 const struct tu_bo *bo;
1041 VkDeviceSize bo_offset;
1042 };
1043
1044 unsigned
1045 tu_image_queue_family_mask(const struct tu_image *image,
1046 uint32_t family,
1047 uint32_t queue_family);
1048
1049 static inline uint32_t
1050 tu_get_layerCount(const struct tu_image *image,
1051 const VkImageSubresourceRange *range)
1052 {
1053 return range->layerCount == VK_REMAINING_ARRAY_LAYERS
1054 ? image->layer_count - range->baseArrayLayer
1055 : range->layerCount;
1056 }
1057
1058 static inline uint32_t
1059 tu_get_levelCount(const struct tu_image *image,
1060 const VkImageSubresourceRange *range)
1061 {
1062 return range->levelCount == VK_REMAINING_MIP_LEVELS
1063 ? image->level_count - range->baseMipLevel
1064 : range->levelCount;
1065 }
1066
1067 struct tu_image_view
1068 {
1069 struct tu_image *image; /**< VkImageViewCreateInfo::image */
1070
1071 VkImageViewType type;
1072 VkImageAspectFlags aspect_mask;
1073 VkFormat vk_format;
1074 uint32_t base_layer;
1075 uint32_t layer_count;
1076 uint32_t base_mip;
1077 uint32_t level_count;
1078 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1079
1080 uint32_t descriptor[16];
1081
1082 /* Descriptor for use as a storage image as opposed to a sampled image.
1083 * This has a few differences for cube maps (e.g. type).
1084 */
1085 uint32_t storage_descriptor[16];
1086 };
1087
1088 struct tu_sampler
1089 {
1090 };
1091
1092 struct tu_image_create_info
1093 {
1094 const VkImageCreateInfo *vk_info;
1095 bool scanout;
1096 bool no_metadata_planes;
1097 };
1098
1099 VkResult
1100 tu_image_create(VkDevice _device,
1101 const struct tu_image_create_info *info,
1102 const VkAllocationCallbacks *alloc,
1103 VkImage *pImage);
1104
1105 VkResult
1106 tu_image_from_gralloc(VkDevice device_h,
1107 const VkImageCreateInfo *base_info,
1108 const VkNativeBufferANDROID *gralloc_info,
1109 const VkAllocationCallbacks *alloc,
1110 VkImage *out_image_h);
1111
1112 void
1113 tu_image_view_init(struct tu_image_view *view,
1114 struct tu_device *device,
1115 const VkImageViewCreateInfo *pCreateInfo);
1116
1117 struct tu_buffer_view
1118 {
1119 VkFormat vk_format;
1120 uint64_t range; /**< VkBufferViewCreateInfo::range */
1121 uint32_t state[4];
1122 };
1123 void
1124 tu_buffer_view_init(struct tu_buffer_view *view,
1125 struct tu_device *device,
1126 const VkBufferViewCreateInfo *pCreateInfo);
1127
1128 static inline struct VkExtent3D
1129 tu_sanitize_image_extent(const VkImageType imageType,
1130 const struct VkExtent3D imageExtent)
1131 {
1132 switch (imageType) {
1133 case VK_IMAGE_TYPE_1D:
1134 return (VkExtent3D) { imageExtent.width, 1, 1 };
1135 case VK_IMAGE_TYPE_2D:
1136 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1137 case VK_IMAGE_TYPE_3D:
1138 return imageExtent;
1139 default:
1140 unreachable("invalid image type");
1141 }
1142 }
1143
1144 static inline struct VkOffset3D
1145 tu_sanitize_image_offset(const VkImageType imageType,
1146 const struct VkOffset3D imageOffset)
1147 {
1148 switch (imageType) {
1149 case VK_IMAGE_TYPE_1D:
1150 return (VkOffset3D) { imageOffset.x, 0, 0 };
1151 case VK_IMAGE_TYPE_2D:
1152 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
1153 case VK_IMAGE_TYPE_3D:
1154 return imageOffset;
1155 default:
1156 unreachable("invalid image type");
1157 }
1158 }
1159
1160 struct tu_attachment_info
1161 {
1162 struct tu_image_view *attachment;
1163 };
1164
1165 struct tu_framebuffer
1166 {
1167 uint32_t width;
1168 uint32_t height;
1169 uint32_t layers;
1170
1171 uint32_t attachment_count;
1172 struct tu_attachment_info attachments[0];
1173 };
1174
1175 struct tu_subpass_barrier
1176 {
1177 VkPipelineStageFlags src_stage_mask;
1178 VkAccessFlags src_access_mask;
1179 VkAccessFlags dst_access_mask;
1180 };
1181
1182 void
1183 tu_subpass_barrier(struct tu_cmd_buffer *cmd_buffer,
1184 const struct tu_subpass_barrier *barrier);
1185
1186 struct tu_subpass_attachment
1187 {
1188 uint32_t attachment;
1189 VkImageLayout layout;
1190 };
1191
1192 struct tu_subpass
1193 {
1194 uint32_t input_count;
1195 uint32_t color_count;
1196 struct tu_subpass_attachment *input_attachments;
1197 struct tu_subpass_attachment *color_attachments;
1198 struct tu_subpass_attachment *resolve_attachments;
1199 struct tu_subpass_attachment depth_stencil_attachment;
1200
1201 /** Subpass has at least one resolve attachment */
1202 bool has_resolve;
1203
1204 struct tu_subpass_barrier start_barrier;
1205
1206 uint32_t view_mask;
1207 VkSampleCountFlagBits max_sample_count;
1208 };
1209
1210 struct tu_render_pass_attachment
1211 {
1212 VkFormat format;
1213 uint32_t samples;
1214 VkAttachmentLoadOp load_op;
1215 VkAttachmentLoadOp stencil_load_op;
1216 VkImageLayout initial_layout;
1217 VkImageLayout final_layout;
1218 uint32_t view_mask;
1219 };
1220
1221 struct tu_render_pass
1222 {
1223 uint32_t attachment_count;
1224 uint32_t subpass_count;
1225 struct tu_subpass_attachment *subpass_attachments;
1226 struct tu_render_pass_attachment *attachments;
1227 struct tu_subpass_barrier end_barrier;
1228 struct tu_subpass subpasses[0];
1229 };
1230
1231 VkResult
1232 tu_device_init_meta(struct tu_device *device);
1233 void
1234 tu_device_finish_meta(struct tu_device *device);
1235
1236 struct tu_query_pool
1237 {
1238 uint32_t stride;
1239 uint32_t availability_offset;
1240 uint64_t size;
1241 char *ptr;
1242 VkQueryType type;
1243 uint32_t pipeline_stats_mask;
1244 };
1245
1246 struct tu_semaphore
1247 {
1248 uint32_t syncobj;
1249 uint32_t temp_syncobj;
1250 };
1251
1252 void
1253 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1254 VkPipelineBindPoint bind_point,
1255 struct tu_descriptor_set *set,
1256 unsigned idx);
1257
1258 void
1259 tu_update_descriptor_sets(struct tu_device *device,
1260 struct tu_cmd_buffer *cmd_buffer,
1261 VkDescriptorSet overrideSet,
1262 uint32_t descriptorWriteCount,
1263 const VkWriteDescriptorSet *pDescriptorWrites,
1264 uint32_t descriptorCopyCount,
1265 const VkCopyDescriptorSet *pDescriptorCopies);
1266
1267 void
1268 tu_update_descriptor_set_with_template(
1269 struct tu_device *device,
1270 struct tu_cmd_buffer *cmd_buffer,
1271 struct tu_descriptor_set *set,
1272 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
1273 const void *pData);
1274
1275 void
1276 tu_meta_push_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1277 VkPipelineBindPoint pipelineBindPoint,
1278 VkPipelineLayout _layout,
1279 uint32_t set,
1280 uint32_t descriptorWriteCount,
1281 const VkWriteDescriptorSet *pDescriptorWrites);
1282
1283 struct tu_fence
1284 {
1285 uint32_t syncobj;
1286 uint32_t temp_syncobj;
1287 };
1288
1289 int
1290 tu_drm_get_gpu_id(const struct tu_physical_device *dev, uint32_t *id);
1291
1292 int
1293 tu_drm_get_gmem_size(const struct tu_physical_device *dev, uint32_t *size);
1294
1295 int
1296 tu_drm_submitqueue_new(const struct tu_device *dev,
1297 int priority,
1298 uint32_t *queue_id);
1299
1300 void
1301 tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id);
1302
1303 uint32_t
1304 tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags);
1305 void
1306 tu_gem_close(const struct tu_device *dev, uint32_t gem_handle);
1307 uint64_t
1308 tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle);
1309 uint64_t
1310 tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle);
1311
1312 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1313 \
1314 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1315 { \
1316 return (struct __tu_type *) _handle; \
1317 } \
1318 \
1319 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1320 { \
1321 return (__VkType) _obj; \
1322 }
1323
1324 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1325 \
1326 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1327 { \
1328 return (struct __tu_type *) (uintptr_t) _handle; \
1329 } \
1330 \
1331 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1332 { \
1333 return (__VkType)(uintptr_t) _obj; \
1334 }
1335
1336 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1337 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1338
1339 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1340 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1341 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1342 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1343 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1344
1345 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1346 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1347 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1348 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1349 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1350 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1351 VkDescriptorSetLayout)
1352 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1353 VkDescriptorUpdateTemplateKHR)
1354 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1355 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1356 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1357 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1358 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1359 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1360 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1361 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1362 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1363 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1364 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1365 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1366 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1367 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1368
1369 #endif /* TU_PRIVATE_H */