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