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