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