b62f5c6c88b73d3a24454321b5f3b85a564ebbbb
[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) ((void)0)
44 #endif
45
46 #include "c11/threads.h"
47 #include "main/macros.h"
48 #include "util/list.h"
49 #include "util/macros.h"
50 #include "util/u_atomic.h"
51 #include "vk_alloc.h"
52 #include "vk_object.h"
53 #include "vk_debug_report.h"
54 #include "wsi_common.h"
55
56 #include "ir3/ir3_compiler.h"
57 #include "ir3/ir3_shader.h"
58
59 #include "adreno_common.xml.h"
60 #include "adreno_pm4.xml.h"
61 #include "a6xx.xml.h"
62 #include "fdl/freedreno_layout.h"
63
64 #include "tu_descriptor_set.h"
65 #include "tu_extensions.h"
66 #include "tu_util.h"
67
68 /* Pre-declarations needed for WSI entrypoints */
69 struct wl_surface;
70 struct wl_display;
71 typedef struct xcb_connection_t xcb_connection_t;
72 typedef uint32_t xcb_visualid_t;
73 typedef uint32_t xcb_window_t;
74
75 #include <vulkan/vk_android_native_buffer.h>
76 #include <vulkan/vk_icd.h>
77 #include <vulkan/vulkan.h>
78 #include <vulkan/vulkan_intel.h>
79
80 #include "tu_entrypoints.h"
81
82 #include "vk_format.h"
83
84 #define MAX_VBS 32
85 #define MAX_VERTEX_ATTRIBS 32
86 #define MAX_RTS 8
87 #define MAX_VSC_PIPES 32
88 #define MAX_VIEWPORTS 1
89 #define MAX_SCISSORS 16
90 #define MAX_DISCARD_RECTANGLES 4
91 #define MAX_PUSH_CONSTANTS_SIZE 128
92 #define MAX_PUSH_DESCRIPTORS 32
93 #define MAX_DYNAMIC_UNIFORM_BUFFERS 16
94 #define MAX_DYNAMIC_STORAGE_BUFFERS 8
95 #define MAX_DYNAMIC_BUFFERS \
96 (MAX_DYNAMIC_UNIFORM_BUFFERS + MAX_DYNAMIC_STORAGE_BUFFERS)
97 #define TU_MAX_DRM_DEVICES 8
98 #define MAX_VIEWS 8
99 #define MAX_BIND_POINTS 2 /* compute + graphics */
100 /* The Qualcomm driver exposes 0x20000058 */
101 #define MAX_STORAGE_BUFFER_RANGE 0x20000000
102 /* We use ldc for uniform buffer loads, just like the Qualcomm driver, so
103 * expose the same maximum range.
104 * TODO: The SIZE bitfield is 15 bits, and in 4-dword units, so the actual
105 * range might be higher.
106 */
107 #define MAX_UNIFORM_BUFFER_RANGE 0x10000
108
109 #define A6XX_TEX_CONST_DWORDS 16
110 #define A6XX_TEX_SAMP_DWORDS 4
111
112 #define tu_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
113
114 #define for_each_bit(b, dword) \
115 for (uint32_t __dword = (dword); \
116 (b) = __builtin_ffs(__dword) - 1, __dword; __dword &= ~(1 << (b)))
117
118 #define COND(bool, val) ((bool) ? (val) : 0)
119 #define BIT(bit) (1u << (bit))
120
121 /* Whenever we generate an error, pass it through this function. Useful for
122 * debugging, where we can break on it. Only call at error site, not when
123 * propagating errors. Might be useful to plug in a stack trace here.
124 */
125
126 struct tu_instance;
127
128 VkResult
129 __vk_errorf(struct tu_instance *instance,
130 VkResult error,
131 const char *file,
132 int line,
133 const char *format,
134 ...);
135
136 #define vk_error(instance, error) \
137 __vk_errorf(instance, error, __FILE__, __LINE__, NULL);
138 #define vk_errorf(instance, error, format, ...) \
139 __vk_errorf(instance, error, __FILE__, __LINE__, format, ##__VA_ARGS__);
140
141 void
142 __tu_finishme(const char *file, int line, const char *format, ...)
143 tu_printflike(3, 4);
144 void
145 tu_loge(const char *format, ...) tu_printflike(1, 2);
146 void
147 tu_logi(const char *format, ...) tu_printflike(1, 2);
148
149 /**
150 * Print a FINISHME message, including its source location.
151 */
152 #define tu_finishme(format, ...) \
153 do { \
154 static bool reported = false; \
155 if (!reported) { \
156 __tu_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
157 reported = true; \
158 } \
159 } while (0)
160
161 #define tu_stub() \
162 do { \
163 tu_finishme("stub %s", __func__); \
164 } while (0)
165
166 void *
167 tu_lookup_entrypoint_unchecked(const char *name);
168 void *
169 tu_lookup_entrypoint_checked(
170 const char *name,
171 uint32_t core_version,
172 const struct tu_instance_extension_table *instance,
173 const struct tu_device_extension_table *device);
174
175 struct tu_physical_device
176 {
177 struct vk_object_base base;
178
179 struct tu_instance *instance;
180
181 char path[20];
182 char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
183 uint8_t driver_uuid[VK_UUID_SIZE];
184 uint8_t device_uuid[VK_UUID_SIZE];
185 uint8_t cache_uuid[VK_UUID_SIZE];
186
187 struct wsi_device wsi_device;
188
189 int local_fd;
190 int master_fd;
191
192 unsigned gpu_id;
193 uint32_t gmem_size;
194 uint64_t gmem_base;
195 uint32_t ccu_offset_gmem;
196 uint32_t ccu_offset_bypass;
197 /* alignment for size of tiles */
198 uint32_t tile_align_w;
199 #define TILE_ALIGN_H 16
200 /* gmem store/load granularity */
201 #define GMEM_ALIGN_W 16
202 #define GMEM_ALIGN_H 4
203 bool supports_multiview_mask;
204
205 struct {
206 uint32_t PC_UNKNOWN_9805;
207 uint32_t SP_UNKNOWN_A0F8;
208 } magic;
209
210 int msm_major_version;
211 int msm_minor_version;
212
213 bool limited_z24s8;
214
215 /* This is the drivers on-disk cache used as a fallback as opposed to
216 * the pipeline cache defined by apps.
217 */
218 struct disk_cache *disk_cache;
219
220 struct tu_device_extension_table supported_extensions;
221 };
222
223 enum tu_debug_flags
224 {
225 TU_DEBUG_STARTUP = 1 << 0,
226 TU_DEBUG_NIR = 1 << 1,
227 TU_DEBUG_IR3 = 1 << 2,
228 TU_DEBUG_NOBIN = 1 << 3,
229 TU_DEBUG_SYSMEM = 1 << 4,
230 TU_DEBUG_FORCEBIN = 1 << 5,
231 TU_DEBUG_NOUBWC = 1 << 6,
232 };
233
234 struct tu_instance
235 {
236 struct vk_object_base base;
237
238 VkAllocationCallbacks alloc;
239
240 uint32_t api_version;
241 int physical_device_count;
242 struct tu_physical_device physical_devices[TU_MAX_DRM_DEVICES];
243
244 enum tu_debug_flags debug_flags;
245
246 struct vk_debug_report_instance debug_report_callbacks;
247
248 struct tu_instance_extension_table enabled_extensions;
249 };
250
251 VkResult
252 tu_wsi_init(struct tu_physical_device *physical_device);
253 void
254 tu_wsi_finish(struct tu_physical_device *physical_device);
255
256 bool
257 tu_instance_extension_supported(const char *name);
258 uint32_t
259 tu_physical_device_api_version(struct tu_physical_device *dev);
260 bool
261 tu_physical_device_extension_supported(struct tu_physical_device *dev,
262 const char *name);
263
264 struct cache_entry;
265
266 struct tu_pipeline_cache
267 {
268 struct vk_object_base base;
269
270 struct tu_device *device;
271 pthread_mutex_t mutex;
272
273 uint32_t total_size;
274 uint32_t table_size;
275 uint32_t kernel_count;
276 struct cache_entry **hash_table;
277 bool modified;
278
279 VkAllocationCallbacks alloc;
280 };
281
282 struct tu_pipeline_key
283 {
284 };
285
286
287 /* queue types */
288 #define TU_QUEUE_GENERAL 0
289
290 #define TU_MAX_QUEUE_FAMILIES 1
291
292 struct tu_fence
293 {
294 struct vk_object_base base;
295 struct wsi_fence *fence_wsi;
296 bool signaled;
297 int fd;
298 };
299
300 void
301 tu_fence_init(struct tu_fence *fence, bool signaled);
302 void
303 tu_fence_finish(struct tu_fence *fence);
304 void
305 tu_fence_update_fd(struct tu_fence *fence, int fd);
306 void
307 tu_fence_copy(struct tu_fence *fence, const struct tu_fence *src);
308 void
309 tu_fence_signal(struct tu_fence *fence);
310 void
311 tu_fence_wait_idle(struct tu_fence *fence);
312
313 struct tu_queue
314 {
315 struct vk_object_base base;
316
317 struct tu_device *device;
318 uint32_t queue_family_index;
319 int queue_idx;
320 VkDeviceQueueCreateFlags flags;
321
322 uint32_t msm_queue_id;
323 struct tu_fence submit_fence;
324 };
325
326 struct tu_bo
327 {
328 uint32_t gem_handle;
329 uint64_t size;
330 uint64_t iova;
331 void *map;
332 };
333
334 enum global_shader {
335 GLOBAL_SH_VS,
336 GLOBAL_SH_FS_BLIT,
337 GLOBAL_SH_FS_CLEAR0,
338 GLOBAL_SH_FS_CLEAR_MAX = GLOBAL_SH_FS_CLEAR0 + MAX_RTS,
339 GLOBAL_SH_COUNT,
340 };
341
342 #define TU_BORDER_COLOR_COUNT 4096
343 #define TU_BORDER_COLOR_BUILTIN 6
344
345 /* This struct defines the layout of the global_bo */
346 struct tu6_global
347 {
348 /* clear/blit shaders, all <= 16 instrs (16 instr = 1 instrlen unit) */
349 instr_t shaders[GLOBAL_SH_COUNT][16];
350
351 uint32_t seqno_dummy; /* dummy seqno for CP_EVENT_WRITE */
352 uint32_t _pad0;
353 volatile uint32_t vsc_draw_overflow;
354 uint32_t _pad1;
355 volatile uint32_t vsc_prim_overflow;
356 uint32_t _pad2;
357 uint64_t predicate;
358
359 /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
360 struct {
361 uint32_t offset;
362 uint32_t pad[7];
363 } flush_base[4];
364
365 /* note: larger global bo will be used for customBorderColors */
366 struct bcolor_entry bcolor_builtin[TU_BORDER_COLOR_BUILTIN], bcolor[];
367 };
368 #define gb_offset(member) offsetof(struct tu6_global, member)
369 #define global_iova(cmd, member) ((cmd)->device->global_bo.iova + gb_offset(member))
370
371 void tu_init_clear_blit_shaders(struct tu6_global *global);
372
373 /* extra space in vsc draw/prim streams */
374 #define VSC_PAD 0x40
375
376 struct tu_device
377 {
378 struct vk_device vk;
379 struct tu_instance *instance;
380
381 struct tu_queue *queues[TU_MAX_QUEUE_FAMILIES];
382 int queue_count[TU_MAX_QUEUE_FAMILIES];
383
384 struct tu_physical_device *physical_device;
385 int _lost;
386
387 struct ir3_compiler *compiler;
388
389 /* Backup in-memory cache to be used if the app doesn't provide one */
390 struct tu_pipeline_cache *mem_cache;
391
392 #define MIN_SCRATCH_BO_SIZE_LOG2 12 /* A page */
393
394 /* Currently the kernel driver uses a 32-bit GPU address space, but it
395 * should be impossible to go beyond 48 bits.
396 */
397 struct {
398 struct tu_bo bo;
399 mtx_t construct_mtx;
400 bool initialized;
401 } scratch_bos[48 - MIN_SCRATCH_BO_SIZE_LOG2];
402
403 struct tu_bo global_bo;
404
405 struct tu_device_extension_table enabled_extensions;
406
407 uint32_t vsc_draw_strm_pitch;
408 uint32_t vsc_prim_strm_pitch;
409 BITSET_DECLARE(custom_border_color, TU_BORDER_COLOR_COUNT);
410 mtx_t mutex;
411 };
412
413 VkResult _tu_device_set_lost(struct tu_device *device,
414 const char *file, int line,
415 const char *msg, ...) PRINTFLIKE(4, 5);
416 #define tu_device_set_lost(dev, ...) \
417 _tu_device_set_lost(dev, __FILE__, __LINE__, __VA_ARGS__)
418
419 static inline bool
420 tu_device_is_lost(struct tu_device *device)
421 {
422 return unlikely(p_atomic_read(&device->_lost));
423 }
424
425 VkResult
426 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size);
427 VkResult
428 tu_bo_init_dmabuf(struct tu_device *dev,
429 struct tu_bo *bo,
430 uint64_t size,
431 int fd);
432 int
433 tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo);
434 void
435 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
436 VkResult
437 tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
438
439 /* Get a scratch bo for use inside a command buffer. This will always return
440 * the same bo given the same size or similar sizes, so only one scratch bo
441 * can be used at the same time. It's meant for short-lived things where we
442 * need to write to some piece of memory, read from it, and then immediately
443 * discard it.
444 */
445 VkResult
446 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo);
447
448 struct tu_cs_entry
449 {
450 /* No ownership */
451 const struct tu_bo *bo;
452
453 uint32_t size;
454 uint32_t offset;
455 };
456
457 struct tu_cs_memory {
458 uint32_t *map;
459 uint64_t iova;
460 };
461
462 struct tu_draw_state {
463 uint64_t iova : 48;
464 uint32_t size : 16;
465 };
466
467 enum tu_dynamic_state
468 {
469 /* re-use VK_DYNAMIC_STATE_ enums for non-extended dynamic states */
470 TU_DYNAMIC_STATE_SAMPLE_LOCATIONS = VK_DYNAMIC_STATE_STENCIL_REFERENCE + 1,
471 TU_DYNAMIC_STATE_COUNT,
472 };
473
474 enum tu_draw_state_group_id
475 {
476 TU_DRAW_STATE_PROGRAM,
477 TU_DRAW_STATE_PROGRAM_BINNING,
478 TU_DRAW_STATE_TESS,
479 TU_DRAW_STATE_VB,
480 TU_DRAW_STATE_VI,
481 TU_DRAW_STATE_VI_BINNING,
482 TU_DRAW_STATE_RAST,
483 TU_DRAW_STATE_DS,
484 TU_DRAW_STATE_BLEND,
485 TU_DRAW_STATE_VS_CONST,
486 TU_DRAW_STATE_HS_CONST,
487 TU_DRAW_STATE_DS_CONST,
488 TU_DRAW_STATE_GS_CONST,
489 TU_DRAW_STATE_FS_CONST,
490 TU_DRAW_STATE_DESC_SETS,
491 TU_DRAW_STATE_DESC_SETS_LOAD,
492 TU_DRAW_STATE_VS_PARAMS,
493 TU_DRAW_STATE_INPUT_ATTACHMENTS_GMEM,
494 TU_DRAW_STATE_INPUT_ATTACHMENTS_SYSMEM,
495
496 /* dynamic state related draw states */
497 TU_DRAW_STATE_DYNAMIC,
498 TU_DRAW_STATE_COUNT = TU_DRAW_STATE_DYNAMIC + TU_DYNAMIC_STATE_COUNT,
499 };
500
501 enum tu_cs_mode
502 {
503
504 /*
505 * A command stream in TU_CS_MODE_GROW mode grows automatically whenever it
506 * is full. tu_cs_begin must be called before command packet emission and
507 * tu_cs_end must be called after.
508 *
509 * This mode may create multiple entries internally. The entries must be
510 * submitted together.
511 */
512 TU_CS_MODE_GROW,
513
514 /*
515 * A command stream in TU_CS_MODE_EXTERNAL mode wraps an external,
516 * fixed-size buffer. tu_cs_begin and tu_cs_end are optional and have no
517 * effect on it.
518 *
519 * This mode does not create any entry or any BO.
520 */
521 TU_CS_MODE_EXTERNAL,
522
523 /*
524 * A command stream in TU_CS_MODE_SUB_STREAM mode does not support direct
525 * command packet emission. tu_cs_begin_sub_stream must be called to get a
526 * sub-stream to emit comamnd packets to. When done with the sub-stream,
527 * tu_cs_end_sub_stream must be called.
528 *
529 * This mode does not create any entry internally.
530 */
531 TU_CS_MODE_SUB_STREAM,
532 };
533
534 struct tu_cs
535 {
536 uint32_t *start;
537 uint32_t *cur;
538 uint32_t *reserved_end;
539 uint32_t *end;
540
541 struct tu_device *device;
542 enum tu_cs_mode mode;
543 uint32_t next_bo_size;
544
545 struct tu_cs_entry *entries;
546 uint32_t entry_count;
547 uint32_t entry_capacity;
548
549 struct tu_bo **bos;
550 uint32_t bo_count;
551 uint32_t bo_capacity;
552
553 /* state for cond_exec_start/cond_exec_end */
554 uint32_t cond_flags;
555 uint32_t *cond_dwords;
556 };
557
558 struct tu_device_memory
559 {
560 struct vk_object_base base;
561
562 struct tu_bo bo;
563 VkDeviceSize size;
564
565 /* for dedicated allocations */
566 struct tu_image *image;
567 struct tu_buffer *buffer;
568
569 uint32_t type_index;
570 void *map;
571 void *user_ptr;
572 };
573
574 struct tu_descriptor_range
575 {
576 uint64_t va;
577 uint32_t size;
578 };
579
580 struct tu_descriptor_set
581 {
582 struct vk_object_base base;
583
584 const struct tu_descriptor_set_layout *layout;
585 struct tu_descriptor_pool *pool;
586 uint32_t size;
587
588 uint64_t va;
589 uint32_t *mapped_ptr;
590
591 uint32_t *dynamic_descriptors;
592
593 struct tu_bo *buffers[0];
594 };
595
596 struct tu_push_descriptor_set
597 {
598 struct tu_descriptor_set set;
599 uint32_t capacity;
600 };
601
602 struct tu_descriptor_pool_entry
603 {
604 uint32_t offset;
605 uint32_t size;
606 struct tu_descriptor_set *set;
607 };
608
609 struct tu_descriptor_pool
610 {
611 struct vk_object_base base;
612
613 struct tu_bo bo;
614 uint64_t current_offset;
615 uint64_t size;
616
617 uint8_t *host_memory_base;
618 uint8_t *host_memory_ptr;
619 uint8_t *host_memory_end;
620
621 uint32_t entry_count;
622 uint32_t max_entry_count;
623 struct tu_descriptor_pool_entry entries[0];
624 };
625
626 struct tu_descriptor_update_template_entry
627 {
628 VkDescriptorType descriptor_type;
629
630 /* The number of descriptors to update */
631 uint32_t descriptor_count;
632
633 /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
634 */
635 uint32_t dst_offset;
636
637 /* In dwords. Not valid/used for dynamic descriptors */
638 uint32_t dst_stride;
639
640 uint32_t buffer_offset;
641
642 /* Only valid for combined image samplers and samplers */
643 uint16_t has_sampler;
644
645 /* In bytes */
646 size_t src_offset;
647 size_t src_stride;
648
649 /* For push descriptors */
650 const uint32_t *immutable_samplers;
651 };
652
653 struct tu_descriptor_update_template
654 {
655 struct vk_object_base base;
656
657 uint32_t entry_count;
658 struct tu_descriptor_update_template_entry entry[0];
659 };
660
661 struct tu_buffer
662 {
663 struct vk_object_base base;
664
665 VkDeviceSize size;
666
667 VkBufferUsageFlags usage;
668 VkBufferCreateFlags flags;
669
670 struct tu_bo *bo;
671 VkDeviceSize bo_offset;
672 };
673
674 static inline uint64_t
675 tu_buffer_iova(struct tu_buffer *buffer)
676 {
677 return buffer->bo->iova + buffer->bo_offset;
678 }
679
680 struct tu_vertex_binding
681 {
682 struct tu_buffer *buffer;
683 VkDeviceSize offset;
684 };
685
686 const char *
687 tu_get_debug_option_name(int id);
688
689 const char *
690 tu_get_perftest_option_name(int id);
691
692 struct tu_descriptor_state
693 {
694 struct tu_descriptor_set *sets[MAX_SETS];
695 uint32_t dynamic_descriptors[MAX_DYNAMIC_BUFFERS * A6XX_TEX_CONST_DWORDS];
696 };
697
698 enum tu_cmd_dirty_bits
699 {
700 TU_CMD_DIRTY_VERTEX_BUFFERS = 1 << 2,
701 TU_CMD_DIRTY_DESC_SETS_LOAD = 1 << 3,
702 TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD = 1 << 4,
703 TU_CMD_DIRTY_SHADER_CONSTS = 1 << 5,
704 /* all draw states were disabled and need to be re-enabled: */
705 TU_CMD_DIRTY_DRAW_STATE = 1 << 7,
706 };
707
708 /* There are only three cache domains we have to care about: the CCU, or
709 * color cache unit, which is used for color and depth/stencil attachments
710 * and copy/blit destinations, and is split conceptually into color and depth,
711 * and the universal cache or UCHE which is used for pretty much everything
712 * else, except for the CP (uncached) and host. We need to flush whenever data
713 * crosses these boundaries.
714 */
715
716 enum tu_cmd_access_mask {
717 TU_ACCESS_UCHE_READ = 1 << 0,
718 TU_ACCESS_UCHE_WRITE = 1 << 1,
719 TU_ACCESS_CCU_COLOR_READ = 1 << 2,
720 TU_ACCESS_CCU_COLOR_WRITE = 1 << 3,
721 TU_ACCESS_CCU_DEPTH_READ = 1 << 4,
722 TU_ACCESS_CCU_DEPTH_WRITE = 1 << 5,
723
724 /* Experiments have shown that while it's safe to avoid flushing the CCU
725 * after each blit/renderpass, it's not safe to assume that subsequent
726 * lookups with a different attachment state will hit unflushed cache
727 * entries. That is, the CCU needs to be flushed and possibly invalidated
728 * when accessing memory with a different attachment state. Writing to an
729 * attachment under the following conditions after clearing using the
730 * normal 2d engine path is known to have issues:
731 *
732 * - It isn't the 0'th layer.
733 * - There are more than one attachment, and this isn't the 0'th attachment
734 * (this seems to also depend on the cpp of the attachments).
735 *
736 * Our best guess is that the layer/MRT state is used when computing
737 * the location of a cache entry in CCU, to avoid conflicts. We assume that
738 * any access in a renderpass after or before an access by a transfer needs
739 * a flush/invalidate, and use the _INCOHERENT variants to represent access
740 * by a transfer.
741 */
742 TU_ACCESS_CCU_COLOR_INCOHERENT_READ = 1 << 6,
743 TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE = 1 << 7,
744 TU_ACCESS_CCU_DEPTH_INCOHERENT_READ = 1 << 8,
745 TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE = 1 << 9,
746
747 /* Accesses by the host */
748 TU_ACCESS_HOST_READ = 1 << 10,
749 TU_ACCESS_HOST_WRITE = 1 << 11,
750
751 /* Accesses by a GPU engine which bypasses any cache. e.g. writes via
752 * CP_EVENT_WRITE::BLIT and the CP are SYSMEM_WRITE.
753 */
754 TU_ACCESS_SYSMEM_READ = 1 << 12,
755 TU_ACCESS_SYSMEM_WRITE = 1 << 13,
756
757 /* Set if a WFI is required. This can be required for:
758 * - 2D engine which (on some models) doesn't wait for flushes to complete
759 * before starting
760 * - CP draw indirect opcodes, where we need to wait for any flushes to
761 * complete but the CP implicitly waits for WFI's to complete and
762 * therefore we only need a WFI after the flushes.
763 */
764 TU_ACCESS_WFI_READ = 1 << 14,
765
766 /* Set if a CP_WAIT_FOR_ME is required due to the data being read by the CP
767 * without it waiting for any WFI.
768 */
769 TU_ACCESS_WFM_READ = 1 << 15,
770
771 /* Memory writes from the CP start in-order with draws and event writes,
772 * but execute asynchronously and hence need a CP_WAIT_MEM_WRITES if read.
773 */
774 TU_ACCESS_CP_WRITE = 1 << 16,
775
776 TU_ACCESS_READ =
777 TU_ACCESS_UCHE_READ |
778 TU_ACCESS_CCU_COLOR_READ |
779 TU_ACCESS_CCU_DEPTH_READ |
780 TU_ACCESS_CCU_COLOR_INCOHERENT_READ |
781 TU_ACCESS_CCU_DEPTH_INCOHERENT_READ |
782 TU_ACCESS_HOST_READ |
783 TU_ACCESS_SYSMEM_READ |
784 TU_ACCESS_WFI_READ |
785 TU_ACCESS_WFM_READ,
786
787 TU_ACCESS_WRITE =
788 TU_ACCESS_UCHE_WRITE |
789 TU_ACCESS_CCU_COLOR_WRITE |
790 TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE |
791 TU_ACCESS_CCU_DEPTH_WRITE |
792 TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE |
793 TU_ACCESS_HOST_WRITE |
794 TU_ACCESS_SYSMEM_WRITE |
795 TU_ACCESS_CP_WRITE,
796
797 TU_ACCESS_ALL =
798 TU_ACCESS_READ |
799 TU_ACCESS_WRITE,
800 };
801
802 enum tu_cmd_flush_bits {
803 TU_CMD_FLAG_CCU_FLUSH_DEPTH = 1 << 0,
804 TU_CMD_FLAG_CCU_FLUSH_COLOR = 1 << 1,
805 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH = 1 << 2,
806 TU_CMD_FLAG_CCU_INVALIDATE_COLOR = 1 << 3,
807 TU_CMD_FLAG_CACHE_FLUSH = 1 << 4,
808 TU_CMD_FLAG_CACHE_INVALIDATE = 1 << 5,
809 TU_CMD_FLAG_WAIT_MEM_WRITES = 1 << 6,
810 TU_CMD_FLAG_WAIT_FOR_IDLE = 1 << 7,
811 TU_CMD_FLAG_WAIT_FOR_ME = 1 << 8,
812
813 TU_CMD_FLAG_ALL_FLUSH =
814 TU_CMD_FLAG_CCU_FLUSH_DEPTH |
815 TU_CMD_FLAG_CCU_FLUSH_COLOR |
816 TU_CMD_FLAG_CACHE_FLUSH |
817 /* Treat the CP as a sort of "cache" which may need to be "flushed" via
818 * waiting for writes to land with WAIT_FOR_MEM_WRITES.
819 */
820 TU_CMD_FLAG_WAIT_MEM_WRITES,
821
822 TU_CMD_FLAG_GPU_INVALIDATE =
823 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH |
824 TU_CMD_FLAG_CCU_INVALIDATE_COLOR |
825 TU_CMD_FLAG_CACHE_INVALIDATE,
826
827 TU_CMD_FLAG_ALL_INVALIDATE =
828 TU_CMD_FLAG_GPU_INVALIDATE |
829 /* Treat the CP as a sort of "cache" which may need to be "invalidated"
830 * via waiting for UCHE/CCU flushes to land with WFI/WFM.
831 */
832 TU_CMD_FLAG_WAIT_FOR_IDLE |
833 TU_CMD_FLAG_WAIT_FOR_ME,
834 };
835
836 /* Changing the CCU from sysmem mode to gmem mode or vice-versa is pretty
837 * heavy, involving a CCU cache flush/invalidate and a WFI in order to change
838 * which part of the gmem is used by the CCU. Here we keep track of what the
839 * state of the CCU.
840 */
841 enum tu_cmd_ccu_state {
842 TU_CMD_CCU_SYSMEM,
843 TU_CMD_CCU_GMEM,
844 TU_CMD_CCU_UNKNOWN,
845 };
846
847 struct tu_cache_state {
848 /* Caches which must be made available (flushed) eventually if there are
849 * any users outside that cache domain, and caches which must be
850 * invalidated eventually if there are any reads.
851 */
852 enum tu_cmd_flush_bits pending_flush_bits;
853 /* Pending flushes */
854 enum tu_cmd_flush_bits flush_bits;
855 };
856
857 struct tu_cmd_state
858 {
859 uint32_t dirty;
860
861 struct tu_pipeline *pipeline;
862 struct tu_pipeline *compute_pipeline;
863
864 /* Vertex buffers */
865 struct
866 {
867 struct tu_buffer *buffers[MAX_VBS];
868 VkDeviceSize offsets[MAX_VBS];
869 } vb;
870
871 /* for dynamic states that can't be emitted directly */
872 uint32_t dynamic_stencil_mask;
873 uint32_t dynamic_stencil_wrmask;
874 uint32_t dynamic_stencil_ref;
875 uint32_t dynamic_gras_su_cntl;
876
877 /* saved states to re-emit in TU_CMD_DIRTY_DRAW_STATE case */
878 struct tu_draw_state dynamic_state[TU_DYNAMIC_STATE_COUNT];
879 struct tu_draw_state vertex_buffers;
880 struct tu_draw_state shader_const[MESA_SHADER_STAGES];
881 struct tu_draw_state desc_sets;
882
883 struct tu_draw_state vs_params;
884
885 /* Index buffer */
886 uint64_t index_va;
887 uint32_t max_index_count;
888 uint8_t index_size;
889
890 /* because streamout base has to be 32-byte aligned
891 * there is an extra offset to deal with when it is
892 * unaligned
893 */
894 uint8_t streamout_offset[IR3_MAX_SO_BUFFERS];
895
896 /* Renderpasses are tricky, because we may need to flush differently if
897 * using sysmem vs. gmem and therefore we have to delay any flushing that
898 * happens before a renderpass. So we have to have two copies of the flush
899 * state, one for intra-renderpass flushes (i.e. renderpass dependencies)
900 * and one for outside a renderpass.
901 */
902 struct tu_cache_state cache;
903 struct tu_cache_state renderpass_cache;
904
905 enum tu_cmd_ccu_state ccu_state;
906
907 const struct tu_render_pass *pass;
908 const struct tu_subpass *subpass;
909 const struct tu_framebuffer *framebuffer;
910 VkRect2D render_area;
911
912 struct tu_cs_entry tile_store_ib;
913
914 bool xfb_used;
915 bool has_tess;
916 bool has_subpass_predication;
917 bool predication_active;
918 };
919
920 struct tu_cmd_pool
921 {
922 struct vk_object_base base;
923
924 VkAllocationCallbacks alloc;
925 struct list_head cmd_buffers;
926 struct list_head free_cmd_buffers;
927 uint32_t queue_family_index;
928 };
929
930 struct tu_cmd_buffer_upload
931 {
932 uint8_t *map;
933 unsigned offset;
934 uint64_t size;
935 struct list_head list;
936 };
937
938 enum tu_cmd_buffer_status
939 {
940 TU_CMD_BUFFER_STATUS_INVALID,
941 TU_CMD_BUFFER_STATUS_INITIAL,
942 TU_CMD_BUFFER_STATUS_RECORDING,
943 TU_CMD_BUFFER_STATUS_EXECUTABLE,
944 TU_CMD_BUFFER_STATUS_PENDING,
945 };
946
947 #ifndef MSM_SUBMIT_BO_READ
948 #define MSM_SUBMIT_BO_READ 0x0001
949 #define MSM_SUBMIT_BO_WRITE 0x0002
950 #define MSM_SUBMIT_BO_DUMP 0x0004
951
952 struct drm_msm_gem_submit_bo {
953 uint32_t flags; /* in, mask of MSM_SUBMIT_BO_x */
954 uint32_t handle; /* in, GEM handle */
955 uint64_t presumed; /* in/out, presumed buffer address */
956 };
957 #endif
958
959 struct tu_bo_list
960 {
961 uint32_t count;
962 uint32_t capacity;
963 struct drm_msm_gem_submit_bo *bo_infos;
964 };
965
966 #define TU_BO_LIST_FAILED (~0)
967
968 void
969 tu_bo_list_init(struct tu_bo_list *list);
970 void
971 tu_bo_list_destroy(struct tu_bo_list *list);
972 void
973 tu_bo_list_reset(struct tu_bo_list *list);
974 uint32_t
975 tu_bo_list_add(struct tu_bo_list *list,
976 const struct tu_bo *bo,
977 uint32_t flags);
978 VkResult
979 tu_bo_list_merge(struct tu_bo_list *list, const struct tu_bo_list *other);
980
981 struct tu_cmd_buffer
982 {
983 struct vk_object_base base;
984
985 struct tu_device *device;
986
987 struct tu_cmd_pool *pool;
988 struct list_head pool_link;
989
990 VkCommandBufferUsageFlags usage_flags;
991 VkCommandBufferLevel level;
992 enum tu_cmd_buffer_status status;
993
994 struct tu_cmd_state state;
995 struct tu_vertex_binding vertex_bindings[MAX_VBS];
996 uint32_t vertex_bindings_set;
997 uint32_t queue_family_index;
998
999 uint32_t push_constants[MAX_PUSH_CONSTANTS_SIZE / 4];
1000 VkShaderStageFlags push_constant_stages;
1001 struct tu_descriptor_set meta_push_descriptors;
1002
1003 struct tu_descriptor_state descriptors[MAX_BIND_POINTS];
1004
1005 struct tu_cmd_buffer_upload upload;
1006
1007 VkResult record_result;
1008
1009 struct tu_bo_list bo_list;
1010 struct tu_cs cs;
1011 struct tu_cs draw_cs;
1012 struct tu_cs draw_epilogue_cs;
1013 struct tu_cs sub_cs;
1014
1015 uint32_t vsc_draw_strm_pitch;
1016 uint32_t vsc_prim_strm_pitch;
1017 };
1018
1019 /* Temporary struct for tracking a register state to be written, used by
1020 * a6xx-pack.h and tu_cs_emit_regs()
1021 */
1022 struct tu_reg_value {
1023 uint32_t reg;
1024 uint64_t value;
1025 bool is_address;
1026 struct tu_bo *bo;
1027 bool bo_write;
1028 uint32_t bo_offset;
1029 uint32_t bo_shift;
1030 };
1031
1032
1033 void tu_emit_cache_flush_renderpass(struct tu_cmd_buffer *cmd_buffer,
1034 struct tu_cs *cs);
1035
1036 void tu_emit_cache_flush_ccu(struct tu_cmd_buffer *cmd_buffer,
1037 struct tu_cs *cs,
1038 enum tu_cmd_ccu_state ccu_state);
1039
1040 void
1041 tu6_emit_event_write(struct tu_cmd_buffer *cmd,
1042 struct tu_cs *cs,
1043 enum vgt_event_type event);
1044
1045 static inline struct tu_descriptor_state *
1046 tu_get_descriptors_state(struct tu_cmd_buffer *cmd_buffer,
1047 VkPipelineBindPoint bind_point)
1048 {
1049 return &cmd_buffer->descriptors[bind_point];
1050 }
1051
1052 struct tu_event
1053 {
1054 struct vk_object_base base;
1055 struct tu_bo bo;
1056 };
1057
1058 struct tu_shader_module
1059 {
1060 struct vk_object_base base;
1061
1062 unsigned char sha1[20];
1063
1064 uint32_t code_size;
1065 const uint32_t *code[0];
1066 };
1067
1068 struct tu_push_constant_range
1069 {
1070 uint32_t lo;
1071 uint32_t count;
1072 };
1073
1074 struct tu_shader
1075 {
1076 struct ir3_shader *ir3_shader;
1077
1078 struct tu_push_constant_range push_consts;
1079 uint8_t active_desc_sets;
1080 };
1081
1082 bool
1083 tu_nir_lower_multiview(nir_shader *nir, uint32_t mask, struct tu_device *dev);
1084
1085 struct tu_shader *
1086 tu_shader_create(struct tu_device *dev,
1087 gl_shader_stage stage,
1088 const VkPipelineShaderStageCreateInfo *stage_info,
1089 unsigned multiview_mask,
1090 struct tu_pipeline_layout *layout,
1091 const VkAllocationCallbacks *alloc);
1092
1093 void
1094 tu_shader_destroy(struct tu_device *dev,
1095 struct tu_shader *shader,
1096 const VkAllocationCallbacks *alloc);
1097
1098 struct tu_program_descriptor_linkage
1099 {
1100 struct ir3_const_state const_state;
1101
1102 uint32_t constlen;
1103
1104 struct tu_push_constant_range push_consts;
1105 };
1106
1107 struct tu_pipeline
1108 {
1109 struct vk_object_base base;
1110
1111 struct tu_cs cs;
1112
1113 struct tu_pipeline_layout *layout;
1114
1115 bool need_indirect_descriptor_sets;
1116 VkShaderStageFlags active_stages;
1117 uint32_t active_desc_sets;
1118
1119 /* mask of enabled dynamic states
1120 * if BIT(i) is set, pipeline->dynamic_state[i] is *NOT* used
1121 */
1122 uint32_t dynamic_state_mask;
1123 struct tu_draw_state dynamic_state[TU_DYNAMIC_STATE_COUNT];
1124
1125 /* gras_su_cntl without line width, used for dynamic line width state */
1126 uint32_t gras_su_cntl;
1127
1128 /* draw states for the pipeline */
1129 struct tu_draw_state load_state, rast_state, ds_state, blend_state;
1130
1131 struct
1132 {
1133 struct tu_draw_state state;
1134 struct tu_draw_state binning_state;
1135
1136 struct tu_program_descriptor_linkage link[MESA_SHADER_STAGES];
1137 } program;
1138
1139 struct
1140 {
1141 struct tu_draw_state state;
1142 struct tu_draw_state binning_state;
1143 uint32_t bindings_used;
1144 } vi;
1145
1146 struct
1147 {
1148 enum pc_di_primtype primtype;
1149 bool primitive_restart;
1150 } ia;
1151
1152 struct
1153 {
1154 uint32_t patch_type;
1155 uint32_t param_stride;
1156 uint32_t hs_bo_regid;
1157 uint32_t ds_bo_regid;
1158 bool upper_left_domain_origin;
1159 } tess;
1160
1161 struct
1162 {
1163 uint32_t local_size[3];
1164 } compute;
1165 };
1166
1167 void
1168 tu6_emit_viewport(struct tu_cs *cs, const VkViewport *viewport);
1169
1170 void
1171 tu6_emit_scissor(struct tu_cs *cs, const VkRect2D *scissor);
1172
1173 void
1174 tu6_emit_sample_locations(struct tu_cs *cs, const VkSampleLocationsInfoEXT *samp_loc);
1175
1176 void
1177 tu6_emit_depth_bias(struct tu_cs *cs,
1178 float constant_factor,
1179 float clamp,
1180 float slope_factor);
1181
1182 void tu6_emit_msaa(struct tu_cs *cs, VkSampleCountFlagBits samples);
1183
1184 void tu6_emit_window_scissor(struct tu_cs *cs, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
1185
1186 void tu6_emit_window_offset(struct tu_cs *cs, uint32_t x1, uint32_t y1);
1187
1188 void
1189 tu6_emit_xs_config(struct tu_cs *cs,
1190 gl_shader_stage stage,
1191 const struct ir3_shader_variant *xs,
1192 uint64_t binary_iova);
1193
1194 void
1195 tu6_emit_vpc(struct tu_cs *cs,
1196 const struct ir3_shader_variant *vs,
1197 const struct ir3_shader_variant *hs,
1198 const struct ir3_shader_variant *ds,
1199 const struct ir3_shader_variant *gs,
1200 const struct ir3_shader_variant *fs,
1201 uint32_t patch_control_points,
1202 bool vshs_workgroup);
1203
1204 void
1205 tu6_emit_fs_inputs(struct tu_cs *cs, const struct ir3_shader_variant *fs);
1206
1207 struct tu_image_view;
1208
1209 void
1210 tu_resolve_sysmem(struct tu_cmd_buffer *cmd,
1211 struct tu_cs *cs,
1212 struct tu_image_view *src,
1213 struct tu_image_view *dst,
1214 uint32_t layer_mask,
1215 uint32_t layers,
1216 const VkRect2D *rect);
1217
1218 void
1219 tu_clear_sysmem_attachment(struct tu_cmd_buffer *cmd,
1220 struct tu_cs *cs,
1221 uint32_t a,
1222 const VkRenderPassBeginInfo *info);
1223
1224 void
1225 tu_clear_gmem_attachment(struct tu_cmd_buffer *cmd,
1226 struct tu_cs *cs,
1227 uint32_t a,
1228 const VkRenderPassBeginInfo *info);
1229
1230 void
1231 tu_load_gmem_attachment(struct tu_cmd_buffer *cmd,
1232 struct tu_cs *cs,
1233 uint32_t a,
1234 bool force_load);
1235
1236 /* expose this function to be able to emit load without checking LOAD_OP */
1237 void
1238 tu_emit_load_gmem_attachment(struct tu_cmd_buffer *cmd, struct tu_cs *cs, uint32_t a);
1239
1240 /* note: gmem store can also resolve */
1241 void
1242 tu_store_gmem_attachment(struct tu_cmd_buffer *cmd,
1243 struct tu_cs *cs,
1244 uint32_t a,
1245 uint32_t gmem_a);
1246
1247 enum tu_supported_formats {
1248 FMT_VERTEX = 1,
1249 FMT_TEXTURE = 2,
1250 FMT_COLOR = 4,
1251 };
1252
1253 struct tu_native_format
1254 {
1255 enum a6xx_format fmt : 8;
1256 enum a3xx_color_swap swap : 8;
1257 enum a6xx_tile_mode tile_mode : 8;
1258 enum tu_supported_formats supported : 8;
1259 };
1260
1261 struct tu_native_format tu6_format_vtx(VkFormat format);
1262 struct tu_native_format tu6_format_color(VkFormat format, enum a6xx_tile_mode tile_mode);
1263 struct tu_native_format tu6_format_texture(VkFormat format, enum a6xx_tile_mode tile_mode);
1264
1265 static inline enum a6xx_format
1266 tu6_base_format(VkFormat format)
1267 {
1268 /* note: tu6_format_color doesn't care about tiling for .fmt field */
1269 return tu6_format_color(format, TILE6_LINEAR).fmt;
1270 }
1271
1272 struct tu_image
1273 {
1274 struct vk_object_base base;
1275
1276 VkImageType type;
1277 /* The original VkFormat provided by the client. This may not match any
1278 * of the actual surface formats.
1279 */
1280 VkFormat vk_format;
1281 VkImageAspectFlags aspects;
1282 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1283 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1284 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1285 VkExtent3D extent;
1286 uint32_t level_count;
1287 uint32_t layer_count;
1288 VkSampleCountFlagBits samples;
1289
1290 struct fdl_layout layout[3];
1291 uint32_t total_size;
1292
1293 unsigned queue_family_mask;
1294 bool exclusive;
1295 bool shareable;
1296
1297 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1298 VkDeviceMemory owned_memory;
1299
1300 /* Set when bound */
1301 struct tu_bo *bo;
1302 VkDeviceSize bo_offset;
1303 };
1304
1305 static inline uint32_t
1306 tu_get_layerCount(const struct tu_image *image,
1307 const VkImageSubresourceRange *range)
1308 {
1309 return range->layerCount == VK_REMAINING_ARRAY_LAYERS
1310 ? image->layer_count - range->baseArrayLayer
1311 : range->layerCount;
1312 }
1313
1314 static inline uint32_t
1315 tu_get_levelCount(const struct tu_image *image,
1316 const VkImageSubresourceRange *range)
1317 {
1318 return range->levelCount == VK_REMAINING_MIP_LEVELS
1319 ? image->level_count - range->baseMipLevel
1320 : range->levelCount;
1321 }
1322
1323 struct tu_image_view
1324 {
1325 struct vk_object_base base;
1326
1327 struct tu_image *image; /**< VkImageViewCreateInfo::image */
1328
1329 uint64_t base_addr;
1330 uint64_t ubwc_addr;
1331 uint32_t layer_size;
1332 uint32_t ubwc_layer_size;
1333
1334 /* used to determine if fast gmem store path can be used */
1335 VkExtent2D extent;
1336 bool need_y2_align;
1337
1338 bool ubwc_enabled;
1339
1340 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1341
1342 /* Descriptor for use as a storage image as opposed to a sampled image.
1343 * This has a few differences for cube maps (e.g. type).
1344 */
1345 uint32_t storage_descriptor[A6XX_TEX_CONST_DWORDS];
1346
1347 /* pre-filled register values */
1348 uint32_t PITCH;
1349 uint32_t FLAG_BUFFER_PITCH;
1350
1351 uint32_t RB_MRT_BUF_INFO;
1352 uint32_t SP_FS_MRT_REG;
1353
1354 uint32_t SP_PS_2D_SRC_INFO;
1355 uint32_t SP_PS_2D_SRC_SIZE;
1356
1357 uint32_t RB_2D_DST_INFO;
1358
1359 uint32_t RB_BLIT_DST_INFO;
1360
1361 /* for d32s8 separate stencil */
1362 uint64_t stencil_base_addr;
1363 uint32_t stencil_layer_size;
1364 uint32_t stencil_PITCH;
1365 };
1366
1367 struct tu_sampler_ycbcr_conversion {
1368 struct vk_object_base base;
1369
1370 VkFormat format;
1371 VkSamplerYcbcrModelConversion ycbcr_model;
1372 VkSamplerYcbcrRange ycbcr_range;
1373 VkComponentMapping components;
1374 VkChromaLocation chroma_offsets[2];
1375 VkFilter chroma_filter;
1376 };
1377
1378 struct tu_sampler {
1379 struct vk_object_base base;
1380
1381 uint32_t descriptor[A6XX_TEX_SAMP_DWORDS];
1382 struct tu_sampler_ycbcr_conversion *ycbcr_sampler;
1383 };
1384
1385 void
1386 tu_cs_image_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1387
1388 void
1389 tu_cs_image_ref_2d(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer, bool src);
1390
1391 void
1392 tu_cs_image_flag_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1393
1394 void
1395 tu_cs_image_stencil_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1396
1397 #define tu_image_view_stencil(iview, x) \
1398 ((iview->x & ~A6XX_##x##_COLOR_FORMAT__MASK) | A6XX_##x##_COLOR_FORMAT(FMT6_8_UINT))
1399
1400 VkResult
1401 tu_image_create(VkDevice _device,
1402 const VkImageCreateInfo *pCreateInfo,
1403 const VkAllocationCallbacks *alloc,
1404 VkImage *pImage,
1405 uint64_t modifier,
1406 const VkSubresourceLayout *plane_layouts);
1407
1408 VkResult
1409 tu_image_from_gralloc(VkDevice device_h,
1410 const VkImageCreateInfo *base_info,
1411 const VkNativeBufferANDROID *gralloc_info,
1412 const VkAllocationCallbacks *alloc,
1413 VkImage *out_image_h);
1414
1415 void
1416 tu_image_view_init(struct tu_image_view *iview,
1417 const VkImageViewCreateInfo *pCreateInfo,
1418 bool limited_z24s8);
1419
1420 struct tu_buffer_view
1421 {
1422 struct vk_object_base base;
1423
1424 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1425
1426 struct tu_buffer *buffer;
1427 };
1428 void
1429 tu_buffer_view_init(struct tu_buffer_view *view,
1430 struct tu_device *device,
1431 const VkBufferViewCreateInfo *pCreateInfo);
1432
1433 struct tu_attachment_info
1434 {
1435 struct tu_image_view *attachment;
1436 };
1437
1438 struct tu_framebuffer
1439 {
1440 struct vk_object_base base;
1441
1442 uint32_t width;
1443 uint32_t height;
1444 uint32_t layers;
1445
1446 /* size of the first tile */
1447 VkExtent2D tile0;
1448 /* number of tiles */
1449 VkExtent2D tile_count;
1450
1451 /* size of the first VSC pipe */
1452 VkExtent2D pipe0;
1453 /* number of VSC pipes */
1454 VkExtent2D pipe_count;
1455
1456 /* pipe register values */
1457 uint32_t pipe_config[MAX_VSC_PIPES];
1458 uint32_t pipe_sizes[MAX_VSC_PIPES];
1459
1460 uint32_t attachment_count;
1461 struct tu_attachment_info attachments[0];
1462 };
1463
1464 void
1465 tu_framebuffer_tiling_config(struct tu_framebuffer *fb,
1466 const struct tu_device *device,
1467 const struct tu_render_pass *pass);
1468
1469 struct tu_subpass_barrier {
1470 VkPipelineStageFlags src_stage_mask;
1471 VkAccessFlags src_access_mask;
1472 VkAccessFlags dst_access_mask;
1473 bool incoherent_ccu_color, incoherent_ccu_depth;
1474 };
1475
1476 struct tu_subpass_attachment
1477 {
1478 uint32_t attachment;
1479 };
1480
1481 struct tu_subpass
1482 {
1483 uint32_t input_count;
1484 uint32_t color_count;
1485 struct tu_subpass_attachment *input_attachments;
1486 struct tu_subpass_attachment *color_attachments;
1487 struct tu_subpass_attachment *resolve_attachments;
1488 struct tu_subpass_attachment depth_stencil_attachment;
1489
1490 VkSampleCountFlagBits samples;
1491
1492 uint32_t srgb_cntl;
1493 uint32_t multiview_mask;
1494
1495 struct tu_subpass_barrier start_barrier;
1496 };
1497
1498 struct tu_render_pass_attachment
1499 {
1500 VkFormat format;
1501 uint32_t samples;
1502 uint32_t cpp;
1503 VkImageAspectFlags clear_mask;
1504 uint32_t clear_views;
1505 bool load;
1506 bool store;
1507 int32_t gmem_offset;
1508 /* for D32S8 separate stencil: */
1509 bool load_stencil;
1510 bool store_stencil;
1511 int32_t gmem_offset_stencil;
1512 };
1513
1514 struct tu_render_pass
1515 {
1516 struct vk_object_base base;
1517
1518 uint32_t attachment_count;
1519 uint32_t subpass_count;
1520 uint32_t gmem_pixels;
1521 uint32_t tile_align_w;
1522 struct tu_subpass_attachment *subpass_attachments;
1523 struct tu_render_pass_attachment *attachments;
1524 struct tu_subpass_barrier end_barrier;
1525 struct tu_subpass subpasses[0];
1526 };
1527
1528 struct tu_query_pool
1529 {
1530 struct vk_object_base base;
1531
1532 VkQueryType type;
1533 uint32_t stride;
1534 uint64_t size;
1535 uint32_t pipeline_statistics;
1536 struct tu_bo bo;
1537 };
1538
1539 enum tu_semaphore_kind
1540 {
1541 TU_SEMAPHORE_NONE,
1542 TU_SEMAPHORE_SYNCOBJ,
1543 };
1544
1545 struct tu_semaphore_part
1546 {
1547 enum tu_semaphore_kind kind;
1548 union {
1549 uint32_t syncobj;
1550 };
1551 };
1552
1553 struct tu_semaphore
1554 {
1555 struct vk_object_base base;
1556
1557 struct tu_semaphore_part permanent;
1558 struct tu_semaphore_part temporary;
1559 };
1560
1561 void
1562 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1563 VkPipelineBindPoint bind_point,
1564 struct tu_descriptor_set *set,
1565 unsigned idx);
1566
1567 void
1568 tu_update_descriptor_sets(struct tu_device *device,
1569 struct tu_cmd_buffer *cmd_buffer,
1570 VkDescriptorSet overrideSet,
1571 uint32_t descriptorWriteCount,
1572 const VkWriteDescriptorSet *pDescriptorWrites,
1573 uint32_t descriptorCopyCount,
1574 const VkCopyDescriptorSet *pDescriptorCopies);
1575
1576 void
1577 tu_update_descriptor_set_with_template(
1578 struct tu_device *device,
1579 struct tu_cmd_buffer *cmd_buffer,
1580 struct tu_descriptor_set *set,
1581 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1582 const void *pData);
1583
1584 VkResult
1585 tu_physical_device_init(struct tu_physical_device *device,
1586 struct tu_instance *instance);
1587 VkResult
1588 tu_enumerate_devices(struct tu_instance *instance);
1589
1590 int
1591 tu_drm_submitqueue_new(const struct tu_device *dev,
1592 int priority,
1593 uint32_t *queue_id);
1594
1595 void
1596 tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id);
1597
1598 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1599 \
1600 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1601 { \
1602 return (struct __tu_type *) _handle; \
1603 } \
1604 \
1605 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1606 { \
1607 return (__VkType) _obj; \
1608 }
1609
1610 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1611 \
1612 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1613 { \
1614 return (struct __tu_type *) (uintptr_t) _handle; \
1615 } \
1616 \
1617 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1618 { \
1619 return (__VkType)(uintptr_t) _obj; \
1620 }
1621
1622 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1623 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1624
1625 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1626 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1627 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1628 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1629 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1630
1631 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1632 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1633 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1634 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1635 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1636 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1637 VkDescriptorSetLayout)
1638 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1639 VkDescriptorUpdateTemplate)
1640 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1641 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1642 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1643 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1644 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1645 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1646 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1647 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1648 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1649 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1650 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1651 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1652 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler_ycbcr_conversion, VkSamplerYcbcrConversion)
1653 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1654 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1655
1656 #endif /* TU_PRIVATE_H */