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