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