anv: use Mesa's u_atomic.h header
[mesa.git] / src / intel / vulkan / anv_private.h
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef ANV_PRIVATE_H
25 #define ANV_PRIVATE_H
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <pthread.h>
31 #include <assert.h>
32 #include <stdint.h>
33 #include <i915_drm.h>
34
35 #ifdef HAVE_VALGRIND
36 #include <valgrind.h>
37 #include <memcheck.h>
38 #define VG(x) x
39 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
40 #else
41 #define VG(x)
42 #endif
43
44 #include "common/gen_device_info.h"
45 #include "blorp/blorp.h"
46 #include "compiler/brw_compiler.h"
47 #include "util/macros.h"
48 #include "util/list.h"
49 #include "util/u_atomic.h"
50 #include "util/u_vector.h"
51 #include "vk_alloc.h"
52
53 /* Pre-declarations needed for WSI entrypoints */
54 struct wl_surface;
55 struct wl_display;
56 typedef struct xcb_connection_t xcb_connection_t;
57 typedef uint32_t xcb_visualid_t;
58 typedef uint32_t xcb_window_t;
59
60 struct anv_buffer;
61 struct anv_buffer_view;
62 struct anv_image_view;
63
64 struct gen_l3_config;
65
66 #include <vulkan/vulkan.h>
67 #include <vulkan/vulkan_intel.h>
68 #include <vulkan/vk_icd.h>
69
70 #include "anv_entrypoints.h"
71 #include "isl/isl.h"
72
73 #include "common/gen_debug.h"
74 #include "wsi_common.h"
75
76 /* Allowing different clear colors requires us to perform a depth resolve at
77 * the end of certain render passes. This is because while slow clears store
78 * the clear color in the HiZ buffer, fast clears (without a resolve) don't.
79 * See the PRMs for examples describing when additional resolves would be
80 * necessary. To enable fast clears without requiring extra resolves, we set
81 * the clear value to a globally-defined one. We could allow different values
82 * if the user doesn't expect coherent data during or after a render passes
83 * (VK_ATTACHMENT_STORE_OP_DONT_CARE), but such users (aside from the CTS)
84 * don't seem to exist yet. In almost all Vulkan applications tested thus far,
85 * 1.0f seems to be the only value used. The only application that doesn't set
86 * this value does so through the usage of an seemingly uninitialized clear
87 * value.
88 */
89 #define ANV_HZ_FC_VAL 1.0f
90
91 #define MAX_VBS 31
92 #define MAX_SETS 8
93 #define MAX_RTS 8
94 #define MAX_VIEWPORTS 16
95 #define MAX_SCISSORS 16
96 #define MAX_PUSH_CONSTANTS_SIZE 128
97 #define MAX_DYNAMIC_BUFFERS 16
98 #define MAX_IMAGES 8
99 #define MAX_PUSH_DESCRIPTORS 32 /* Minimum requirement */
100
101 #define ANV_SVGS_VB_INDEX MAX_VBS
102 #define ANV_DRAWID_VB_INDEX (MAX_VBS + 1)
103
104 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
105
106 static inline uint32_t
107 align_down_npot_u32(uint32_t v, uint32_t a)
108 {
109 return v - (v % a);
110 }
111
112 static inline uint32_t
113 align_u32(uint32_t v, uint32_t a)
114 {
115 assert(a != 0 && a == (a & -a));
116 return (v + a - 1) & ~(a - 1);
117 }
118
119 static inline uint64_t
120 align_u64(uint64_t v, uint64_t a)
121 {
122 assert(a != 0 && a == (a & -a));
123 return (v + a - 1) & ~(a - 1);
124 }
125
126 static inline int32_t
127 align_i32(int32_t v, int32_t a)
128 {
129 assert(a != 0 && a == (a & -a));
130 return (v + a - 1) & ~(a - 1);
131 }
132
133 /** Alignment must be a power of 2. */
134 static inline bool
135 anv_is_aligned(uintmax_t n, uintmax_t a)
136 {
137 assert(a == (a & -a));
138 return (n & (a - 1)) == 0;
139 }
140
141 static inline uint32_t
142 anv_minify(uint32_t n, uint32_t levels)
143 {
144 if (unlikely(n == 0))
145 return 0;
146 else
147 return MAX2(n >> levels, 1);
148 }
149
150 static inline float
151 anv_clamp_f(float f, float min, float max)
152 {
153 assert(min < max);
154
155 if (f > max)
156 return max;
157 else if (f < min)
158 return min;
159 else
160 return f;
161 }
162
163 static inline bool
164 anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
165 {
166 if (*inout_mask & clear_mask) {
167 *inout_mask &= ~clear_mask;
168 return true;
169 } else {
170 return false;
171 }
172 }
173
174 static inline union isl_color_value
175 vk_to_isl_color(VkClearColorValue color)
176 {
177 return (union isl_color_value) {
178 .u32 = {
179 color.uint32[0],
180 color.uint32[1],
181 color.uint32[2],
182 color.uint32[3],
183 },
184 };
185 }
186
187 #define for_each_bit(b, dword) \
188 for (uint32_t __dword = (dword); \
189 (b) = __builtin_ffs(__dword) - 1, __dword; \
190 __dword &= ~(1 << (b)))
191
192 #define typed_memcpy(dest, src, count) ({ \
193 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
194 memcpy((dest), (src), (count) * sizeof(*(src))); \
195 })
196
197 /* Whenever we generate an error, pass it through this function. Useful for
198 * debugging, where we can break on it. Only call at error site, not when
199 * propagating errors. Might be useful to plug in a stack trace here.
200 */
201
202 VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
203
204 #ifdef DEBUG
205 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
206 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
207 #define anv_debug(format, ...) fprintf(stderr, "debug: " format, ##__VA_ARGS__)
208 #else
209 #define vk_error(error) error
210 #define vk_errorf(error, format, ...) error
211 #define anv_debug(format, ...)
212 #endif
213
214 /**
215 * Warn on ignored extension structs.
216 *
217 * The Vulkan spec requires us to ignore unsupported or unknown structs in
218 * a pNext chain. In debug mode, emitting warnings for ignored structs may
219 * help us discover structs that we should not have ignored.
220 *
221 *
222 * From the Vulkan 1.0.38 spec:
223 *
224 * Any component of the implementation (the loader, any enabled layers,
225 * and drivers) must skip over, without processing (other than reading the
226 * sType and pNext members) any chained structures with sType values not
227 * defined by extensions supported by that component.
228 */
229 #define anv_debug_ignored_stype(sType) \
230 anv_debug("debug: %s: ignored VkStructureType %u\n", __func__, (sType))
231
232 void __anv_finishme(const char *file, int line, const char *format, ...)
233 anv_printflike(3, 4);
234 void __anv_perf_warn(const char *file, int line, const char *format, ...)
235 anv_printflike(3, 4);
236 void anv_loge(const char *format, ...) anv_printflike(1, 2);
237 void anv_loge_v(const char *format, va_list va);
238
239 /**
240 * Print a FINISHME message, including its source location.
241 */
242 #define anv_finishme(format, ...) \
243 do { \
244 static bool reported = false; \
245 if (!reported) { \
246 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
247 reported = true; \
248 } \
249 } while (0)
250
251 /**
252 * Print a perf warning message. Set INTEL_DEBUG=perf to see these.
253 */
254 #define anv_perf_warn(format, ...) \
255 do { \
256 static bool reported = false; \
257 if (!reported && unlikely(INTEL_DEBUG & DEBUG_PERF)) { \
258 __anv_perf_warn(__FILE__, __LINE__, format, ##__VA_ARGS__); \
259 reported = true; \
260 } \
261 } while (0)
262
263 /* A non-fatal assert. Useful for debugging. */
264 #ifdef DEBUG
265 #define anv_assert(x) ({ \
266 if (unlikely(!(x))) \
267 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
268 })
269 #else
270 #define anv_assert(x)
271 #endif
272
273 /* A multi-pointer allocator
274 *
275 * When copying data structures from the user (such as a render pass), it's
276 * common to need to allocate data for a bunch of different things. Instead
277 * of doing several allocations and having to handle all of the error checking
278 * that entails, it can be easier to do a single allocation. This struct
279 * helps facilitate that. The intended usage looks like this:
280 *
281 * ANV_MULTIALLOC(ma)
282 * anv_multialloc_add(&ma, &main_ptr, 1);
283 * anv_multialloc_add(&ma, &substruct1, substruct1Count);
284 * anv_multialloc_add(&ma, &substruct2, substruct2Count);
285 *
286 * if (!anv_multialloc_alloc(&ma, pAllocator, VK_ALLOCATION_SCOPE_FOO))
287 * return vk_error(VK_ERROR_OUT_OF_HOST_MEORY);
288 */
289 struct anv_multialloc {
290 size_t size;
291 size_t align;
292
293 uint32_t ptr_count;
294 void **ptrs[8];
295 };
296
297 #define ANV_MULTIALLOC_INIT \
298 ((struct anv_multialloc) { 0, })
299
300 #define ANV_MULTIALLOC(_name) \
301 struct anv_multialloc _name = ANV_MULTIALLOC_INIT
302
303 __attribute__((always_inline))
304 static inline void
305 _anv_multialloc_add(struct anv_multialloc *ma,
306 void **ptr, size_t size, size_t align)
307 {
308 size_t offset = align_u64(ma->size, align);
309 ma->size = offset + size;
310 ma->align = MAX2(ma->align, align);
311
312 /* Store the offset in the pointer. */
313 *ptr = (void *)(uintptr_t)offset;
314
315 assert(ma->ptr_count < ARRAY_SIZE(ma->ptrs));
316 ma->ptrs[ma->ptr_count++] = ptr;
317 }
318
319 #define anv_multialloc_add(_ma, _ptr, _count) \
320 _anv_multialloc_add((_ma), (void **)(_ptr), \
321 (_count) * sizeof(**(_ptr)), __alignof__(**(_ptr)))
322
323 __attribute__((always_inline))
324 static inline void *
325 anv_multialloc_alloc(struct anv_multialloc *ma,
326 const VkAllocationCallbacks *alloc,
327 VkSystemAllocationScope scope)
328 {
329 void *ptr = vk_alloc(alloc, ma->size, ma->align, scope);
330 if (!ptr)
331 return NULL;
332
333 /* Fill out each of the pointers with their final value.
334 *
335 * for (uint32_t i = 0; i < ma->ptr_count; i++)
336 * *ma->ptrs[i] = ptr + (uintptr_t)*ma->ptrs[i];
337 *
338 * Unfortunately, even though ma->ptr_count is basically guaranteed to be a
339 * constant, GCC is incapable of figuring this out and unrolling the loop
340 * so we have to give it a little help.
341 */
342 STATIC_ASSERT(ARRAY_SIZE(ma->ptrs) == 8);
343 #define _ANV_MULTIALLOC_UPDATE_POINTER(_i) \
344 if ((_i) < ma->ptr_count) \
345 *ma->ptrs[_i] = ptr + (uintptr_t)*ma->ptrs[_i]
346 _ANV_MULTIALLOC_UPDATE_POINTER(0);
347 _ANV_MULTIALLOC_UPDATE_POINTER(1);
348 _ANV_MULTIALLOC_UPDATE_POINTER(2);
349 _ANV_MULTIALLOC_UPDATE_POINTER(3);
350 _ANV_MULTIALLOC_UPDATE_POINTER(4);
351 _ANV_MULTIALLOC_UPDATE_POINTER(5);
352 _ANV_MULTIALLOC_UPDATE_POINTER(6);
353 _ANV_MULTIALLOC_UPDATE_POINTER(7);
354 #undef _ANV_MULTIALLOC_UPDATE_POINTER
355
356 return ptr;
357 }
358
359 __attribute__((always_inline))
360 static inline void *
361 anv_multialloc_alloc2(struct anv_multialloc *ma,
362 const VkAllocationCallbacks *parent_alloc,
363 const VkAllocationCallbacks *alloc,
364 VkSystemAllocationScope scope)
365 {
366 return anv_multialloc_alloc(ma, alloc ? alloc : parent_alloc, scope);
367 }
368
369 /**
370 * A dynamically growable, circular buffer. Elements are added at head and
371 * removed from tail. head and tail are free-running uint32_t indices and we
372 * only compute the modulo with size when accessing the array. This way,
373 * number of bytes in the queue is always head - tail, even in case of
374 * wraparound.
375 */
376
377 struct anv_bo {
378 uint32_t gem_handle;
379
380 /* Index into the current validation list. This is used by the
381 * validation list building alrogithm to track which buffers are already
382 * in the validation list so that we can ensure uniqueness.
383 */
384 uint32_t index;
385
386 /* Last known offset. This value is provided by the kernel when we
387 * execbuf and is used as the presumed offset for the next bunch of
388 * relocations.
389 */
390 uint64_t offset;
391
392 uint64_t size;
393 void *map;
394
395 /** Flags to pass to the kernel through drm_i915_exec_object2::flags */
396 uint32_t flags;
397 };
398
399 static inline void
400 anv_bo_init(struct anv_bo *bo, uint32_t gem_handle, uint64_t size)
401 {
402 bo->gem_handle = gem_handle;
403 bo->index = 0;
404 bo->offset = -1;
405 bo->size = size;
406 bo->map = NULL;
407 bo->flags = 0;
408 }
409
410 /* Represents a lock-free linked list of "free" things. This is used by
411 * both the block pool and the state pools. Unfortunately, in order to
412 * solve the ABA problem, we can't use a single uint32_t head.
413 */
414 union anv_free_list {
415 struct {
416 int32_t offset;
417
418 /* A simple count that is incremented every time the head changes. */
419 uint32_t count;
420 };
421 uint64_t u64;
422 };
423
424 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
425
426 struct anv_block_state {
427 union {
428 struct {
429 uint32_t next;
430 uint32_t end;
431 };
432 uint64_t u64;
433 };
434 };
435
436 struct anv_block_pool {
437 struct anv_device *device;
438
439 struct anv_bo bo;
440
441 /* The offset from the start of the bo to the "center" of the block
442 * pool. Pointers to allocated blocks are given by
443 * bo.map + center_bo_offset + offsets.
444 */
445 uint32_t center_bo_offset;
446
447 /* Current memory map of the block pool. This pointer may or may not
448 * point to the actual beginning of the block pool memory. If
449 * anv_block_pool_alloc_back has ever been called, then this pointer
450 * will point to the "center" position of the buffer and all offsets
451 * (negative or positive) given out by the block pool alloc functions
452 * will be valid relative to this pointer.
453 *
454 * In particular, map == bo.map + center_offset
455 */
456 void *map;
457 int fd;
458
459 /**
460 * Array of mmaps and gem handles owned by the block pool, reclaimed when
461 * the block pool is destroyed.
462 */
463 struct u_vector mmap_cleanups;
464
465 struct anv_block_state state;
466
467 struct anv_block_state back_state;
468 };
469
470 /* Block pools are backed by a fixed-size 1GB memfd */
471 #define BLOCK_POOL_MEMFD_SIZE (1ul << 30)
472
473 /* The center of the block pool is also the middle of the memfd. This may
474 * change in the future if we decide differently for some reason.
475 */
476 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
477
478 static inline uint32_t
479 anv_block_pool_size(struct anv_block_pool *pool)
480 {
481 return pool->state.end + pool->back_state.end;
482 }
483
484 struct anv_state {
485 int32_t offset;
486 uint32_t alloc_size;
487 void *map;
488 };
489
490 #define ANV_STATE_NULL ((struct anv_state) { .alloc_size = 0 })
491
492 struct anv_fixed_size_state_pool {
493 union anv_free_list free_list;
494 struct anv_block_state block;
495 };
496
497 #define ANV_MIN_STATE_SIZE_LOG2 6
498 #define ANV_MAX_STATE_SIZE_LOG2 20
499
500 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2 + 1)
501
502 struct anv_state_pool {
503 struct anv_block_pool block_pool;
504
505 /* The size of blocks which will be allocated from the block pool */
506 uint32_t block_size;
507
508 /** Free list for "back" allocations */
509 union anv_free_list back_alloc_free_list;
510
511 struct anv_fixed_size_state_pool buckets[ANV_STATE_BUCKETS];
512 };
513
514 struct anv_state_stream_block;
515
516 struct anv_state_stream {
517 struct anv_state_pool *state_pool;
518
519 /* The size of blocks to allocate from the state pool */
520 uint32_t block_size;
521
522 /* Current block we're allocating from */
523 struct anv_state block;
524
525 /* Offset into the current block at which to allocate the next state */
526 uint32_t next;
527
528 /* List of all blocks allocated from this pool */
529 struct anv_state_stream_block *block_list;
530 };
531
532 #define CACHELINE_SIZE 64
533 #define CACHELINE_MASK 63
534
535 static inline void
536 anv_clflush_range(void *start, size_t size)
537 {
538 void *p = (void *) (((uintptr_t) start) & ~CACHELINE_MASK);
539 void *end = start + size;
540
541 while (p < end) {
542 __builtin_ia32_clflush(p);
543 p += CACHELINE_SIZE;
544 }
545 }
546
547 static inline void
548 anv_flush_range(void *start, size_t size)
549 {
550 __builtin_ia32_mfence();
551 anv_clflush_range(start, size);
552 }
553
554 static inline void
555 anv_invalidate_range(void *start, size_t size)
556 {
557 anv_clflush_range(start, size);
558 __builtin_ia32_mfence();
559 }
560
561 /* The block_pool functions exported for testing only. The block pool should
562 * only be used via a state pool (see below).
563 */
564 VkResult anv_block_pool_init(struct anv_block_pool *pool,
565 struct anv_device *device,
566 uint32_t initial_size);
567 void anv_block_pool_finish(struct anv_block_pool *pool);
568 int32_t anv_block_pool_alloc(struct anv_block_pool *pool,
569 uint32_t block_size);
570 int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool,
571 uint32_t block_size);
572
573 VkResult anv_state_pool_init(struct anv_state_pool *pool,
574 struct anv_device *device,
575 uint32_t block_size);
576 void anv_state_pool_finish(struct anv_state_pool *pool);
577 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
578 uint32_t state_size, uint32_t alignment);
579 struct anv_state anv_state_pool_alloc_back(struct anv_state_pool *pool);
580 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
581 void anv_state_stream_init(struct anv_state_stream *stream,
582 struct anv_state_pool *state_pool,
583 uint32_t block_size);
584 void anv_state_stream_finish(struct anv_state_stream *stream);
585 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
586 uint32_t size, uint32_t alignment);
587
588 /**
589 * Implements a pool of re-usable BOs. The interface is identical to that
590 * of block_pool except that each block is its own BO.
591 */
592 struct anv_bo_pool {
593 struct anv_device *device;
594
595 void *free_list[16];
596 };
597
598 void anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device);
599 void anv_bo_pool_finish(struct anv_bo_pool *pool);
600 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo,
601 uint32_t size);
602 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
603
604 struct anv_scratch_bo {
605 bool exists;
606 struct anv_bo bo;
607 };
608
609 struct anv_scratch_pool {
610 /* Indexed by Per-Thread Scratch Space number (the hardware value) and stage */
611 struct anv_scratch_bo bos[16][MESA_SHADER_STAGES];
612 };
613
614 void anv_scratch_pool_init(struct anv_device *device,
615 struct anv_scratch_pool *pool);
616 void anv_scratch_pool_finish(struct anv_device *device,
617 struct anv_scratch_pool *pool);
618 struct anv_bo *anv_scratch_pool_alloc(struct anv_device *device,
619 struct anv_scratch_pool *pool,
620 gl_shader_stage stage,
621 unsigned per_thread_scratch);
622
623 /** Implements a BO cache that ensures a 1-1 mapping of GEM BOs to anv_bos */
624 struct anv_bo_cache {
625 struct hash_table *bo_map;
626 pthread_mutex_t mutex;
627 };
628
629 VkResult anv_bo_cache_init(struct anv_bo_cache *cache);
630 void anv_bo_cache_finish(struct anv_bo_cache *cache);
631 VkResult anv_bo_cache_alloc(struct anv_device *device,
632 struct anv_bo_cache *cache,
633 uint64_t size, struct anv_bo **bo);
634 VkResult anv_bo_cache_import(struct anv_device *device,
635 struct anv_bo_cache *cache,
636 int fd, uint64_t size, struct anv_bo **bo);
637 VkResult anv_bo_cache_export(struct anv_device *device,
638 struct anv_bo_cache *cache,
639 struct anv_bo *bo_in, int *fd_out);
640 void anv_bo_cache_release(struct anv_device *device,
641 struct anv_bo_cache *cache,
642 struct anv_bo *bo);
643
644 struct anv_memory_type {
645 /* Standard bits passed on to the client */
646 VkMemoryPropertyFlags propertyFlags;
647 uint32_t heapIndex;
648
649 /* Driver-internal book-keeping */
650 VkBufferUsageFlags valid_buffer_usage;
651 };
652
653 struct anv_memory_heap {
654 /* Standard bits passed on to the client */
655 VkDeviceSize size;
656 VkMemoryHeapFlags flags;
657
658 /* Driver-internal book-keeping */
659 bool supports_48bit_addresses;
660 };
661
662 struct anv_physical_device {
663 VK_LOADER_DATA _loader_data;
664
665 struct anv_instance * instance;
666 uint32_t chipset_id;
667 char path[20];
668 const char * name;
669 struct gen_device_info info;
670 /** Amount of "GPU memory" we want to advertise
671 *
672 * Clearly, this value is bogus since Intel is a UMA architecture. On
673 * gen7 platforms, we are limited by GTT size unless we want to implement
674 * fine-grained tracking and GTT splitting. On Broadwell and above we are
675 * practically unlimited. However, we will never report more than 3/4 of
676 * the total system ram to try and avoid running out of RAM.
677 */
678 bool supports_48bit_addresses;
679 struct brw_compiler * compiler;
680 struct isl_device isl_dev;
681 int cmd_parser_version;
682 bool has_exec_async;
683
684 uint32_t eu_total;
685 uint32_t subslice_total;
686
687 struct {
688 uint32_t type_count;
689 struct anv_memory_type types[VK_MAX_MEMORY_TYPES];
690 uint32_t heap_count;
691 struct anv_memory_heap heaps[VK_MAX_MEMORY_HEAPS];
692 } memory;
693
694 uint8_t pipeline_cache_uuid[VK_UUID_SIZE];
695 uint8_t driver_uuid[VK_UUID_SIZE];
696 uint8_t device_uuid[VK_UUID_SIZE];
697
698 struct wsi_device wsi_device;
699 int local_fd;
700 };
701
702 struct anv_instance {
703 VK_LOADER_DATA _loader_data;
704
705 VkAllocationCallbacks alloc;
706
707 uint32_t apiVersion;
708 int physicalDeviceCount;
709 struct anv_physical_device physicalDevice;
710 };
711
712 VkResult anv_init_wsi(struct anv_physical_device *physical_device);
713 void anv_finish_wsi(struct anv_physical_device *physical_device);
714
715 struct anv_queue {
716 VK_LOADER_DATA _loader_data;
717
718 struct anv_device * device;
719
720 struct anv_state_pool * pool;
721 };
722
723 struct anv_pipeline_cache {
724 struct anv_device * device;
725 pthread_mutex_t mutex;
726
727 struct hash_table * cache;
728 };
729
730 struct anv_pipeline_bind_map;
731
732 void anv_pipeline_cache_init(struct anv_pipeline_cache *cache,
733 struct anv_device *device,
734 bool cache_enabled);
735 void anv_pipeline_cache_finish(struct anv_pipeline_cache *cache);
736
737 struct anv_shader_bin *
738 anv_pipeline_cache_search(struct anv_pipeline_cache *cache,
739 const void *key, uint32_t key_size);
740 struct anv_shader_bin *
741 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache *cache,
742 const void *key_data, uint32_t key_size,
743 const void *kernel_data, uint32_t kernel_size,
744 const struct brw_stage_prog_data *prog_data,
745 uint32_t prog_data_size,
746 const struct anv_pipeline_bind_map *bind_map);
747
748 struct anv_device {
749 VK_LOADER_DATA _loader_data;
750
751 VkAllocationCallbacks alloc;
752
753 struct anv_instance * instance;
754 uint32_t chipset_id;
755 struct gen_device_info info;
756 struct isl_device isl_dev;
757 int context_id;
758 int fd;
759 bool can_chain_batches;
760 bool robust_buffer_access;
761
762 struct anv_bo_pool batch_bo_pool;
763
764 struct anv_bo_cache bo_cache;
765
766 struct anv_state_pool dynamic_state_pool;
767 struct anv_state_pool instruction_state_pool;
768 struct anv_state_pool surface_state_pool;
769
770 struct anv_bo workaround_bo;
771
772 struct anv_pipeline_cache blorp_shader_cache;
773 struct blorp_context blorp;
774
775 struct anv_state border_colors;
776
777 struct anv_queue queue;
778
779 struct anv_scratch_pool scratch_pool;
780
781 uint32_t default_mocs;
782
783 pthread_mutex_t mutex;
784 pthread_cond_t queue_submit;
785 bool lost;
786 };
787
788 static void inline
789 anv_state_flush(struct anv_device *device, struct anv_state state)
790 {
791 if (device->info.has_llc)
792 return;
793
794 anv_flush_range(state.map, state.alloc_size);
795 }
796
797 void anv_device_init_blorp(struct anv_device *device);
798 void anv_device_finish_blorp(struct anv_device *device);
799
800 VkResult anv_device_execbuf(struct anv_device *device,
801 struct drm_i915_gem_execbuffer2 *execbuf,
802 struct anv_bo **execbuf_bos);
803 VkResult anv_device_query_status(struct anv_device *device);
804 VkResult anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo);
805 VkResult anv_device_wait(struct anv_device *device, struct anv_bo *bo,
806 int64_t timeout);
807
808 void* anv_gem_mmap(struct anv_device *device,
809 uint32_t gem_handle, uint64_t offset, uint64_t size, uint32_t flags);
810 void anv_gem_munmap(void *p, uint64_t size);
811 uint32_t anv_gem_create(struct anv_device *device, uint64_t size);
812 void anv_gem_close(struct anv_device *device, uint32_t gem_handle);
813 uint32_t anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
814 int anv_gem_busy(struct anv_device *device, uint32_t gem_handle);
815 int anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns);
816 int anv_gem_execbuffer(struct anv_device *device,
817 struct drm_i915_gem_execbuffer2 *execbuf);
818 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
819 uint32_t stride, uint32_t tiling);
820 int anv_gem_create_context(struct anv_device *device);
821 int anv_gem_destroy_context(struct anv_device *device, int context);
822 int anv_gem_get_context_param(int fd, int context, uint32_t param,
823 uint64_t *value);
824 int anv_gem_get_param(int fd, uint32_t param);
825 bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling);
826 int anv_gem_get_aperture(int fd, uint64_t *size);
827 bool anv_gem_supports_48b_addresses(int fd);
828 int anv_gem_gpu_get_reset_stats(struct anv_device *device,
829 uint32_t *active, uint32_t *pending);
830 int anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle);
831 uint32_t anv_gem_fd_to_handle(struct anv_device *device, int fd);
832 int anv_gem_set_caching(struct anv_device *device, uint32_t gem_handle, uint32_t caching);
833 int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
834 uint32_t read_domains, uint32_t write_domain);
835
836 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
837
838 struct anv_reloc_list {
839 uint32_t num_relocs;
840 uint32_t array_length;
841 struct drm_i915_gem_relocation_entry * relocs;
842 struct anv_bo ** reloc_bos;
843 };
844
845 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
846 const VkAllocationCallbacks *alloc);
847 void anv_reloc_list_finish(struct anv_reloc_list *list,
848 const VkAllocationCallbacks *alloc);
849
850 VkResult anv_reloc_list_add(struct anv_reloc_list *list,
851 const VkAllocationCallbacks *alloc,
852 uint32_t offset, struct anv_bo *target_bo,
853 uint32_t delta);
854
855 struct anv_batch_bo {
856 /* Link in the anv_cmd_buffer.owned_batch_bos list */
857 struct list_head link;
858
859 struct anv_bo bo;
860
861 /* Bytes actually consumed in this batch BO */
862 uint32_t length;
863
864 struct anv_reloc_list relocs;
865 };
866
867 struct anv_batch {
868 const VkAllocationCallbacks * alloc;
869
870 void * start;
871 void * end;
872 void * next;
873
874 struct anv_reloc_list * relocs;
875
876 /* This callback is called (with the associated user data) in the event
877 * that the batch runs out of space.
878 */
879 VkResult (*extend_cb)(struct anv_batch *, void *);
880 void * user_data;
881
882 /**
883 * Current error status of the command buffer. Used to track inconsistent
884 * or incomplete command buffer states that are the consequence of run-time
885 * errors such as out of memory scenarios. We want to track this in the
886 * batch because the command buffer object is not visible to some parts
887 * of the driver.
888 */
889 VkResult status;
890 };
891
892 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
893 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
894 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
895 void *location, struct anv_bo *bo, uint32_t offset);
896 VkResult anv_device_submit_simple_batch(struct anv_device *device,
897 struct anv_batch *batch);
898
899 static inline VkResult
900 anv_batch_set_error(struct anv_batch *batch, VkResult error)
901 {
902 assert(error != VK_SUCCESS);
903 if (batch->status == VK_SUCCESS)
904 batch->status = error;
905 return batch->status;
906 }
907
908 static inline bool
909 anv_batch_has_error(struct anv_batch *batch)
910 {
911 return batch->status != VK_SUCCESS;
912 }
913
914 struct anv_address {
915 struct anv_bo *bo;
916 uint32_t offset;
917 };
918
919 static inline uint64_t
920 _anv_combine_address(struct anv_batch *batch, void *location,
921 const struct anv_address address, uint32_t delta)
922 {
923 if (address.bo == NULL) {
924 return address.offset + delta;
925 } else {
926 assert(batch->start <= location && location < batch->end);
927
928 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
929 }
930 }
931
932 #define __gen_address_type struct anv_address
933 #define __gen_user_data struct anv_batch
934 #define __gen_combine_address _anv_combine_address
935
936 /* Wrapper macros needed to work around preprocessor argument issues. In
937 * particular, arguments don't get pre-evaluated if they are concatenated.
938 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
939 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
940 * We can work around this easily enough with these helpers.
941 */
942 #define __anv_cmd_length(cmd) cmd ## _length
943 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
944 #define __anv_cmd_header(cmd) cmd ## _header
945 #define __anv_cmd_pack(cmd) cmd ## _pack
946 #define __anv_reg_num(reg) reg ## _num
947
948 #define anv_pack_struct(dst, struc, ...) do { \
949 struct struc __template = { \
950 __VA_ARGS__ \
951 }; \
952 __anv_cmd_pack(struc)(NULL, dst, &__template); \
953 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dst, __anv_cmd_length(struc) * 4)); \
954 } while (0)
955
956 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
957 void *__dst = anv_batch_emit_dwords(batch, n); \
958 if (__dst) { \
959 struct cmd __template = { \
960 __anv_cmd_header(cmd), \
961 .DWordLength = n - __anv_cmd_length_bias(cmd), \
962 __VA_ARGS__ \
963 }; \
964 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
965 } \
966 __dst; \
967 })
968
969 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
970 do { \
971 uint32_t *dw; \
972 \
973 STATIC_ASSERT(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
974 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
975 if (!dw) \
976 break; \
977 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
978 dw[i] = (dwords0)[i] | (dwords1)[i]; \
979 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
980 } while (0)
981
982 #define anv_batch_emit(batch, cmd, name) \
983 for (struct cmd name = { __anv_cmd_header(cmd) }, \
984 *_dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
985 __builtin_expect(_dst != NULL, 1); \
986 ({ __anv_cmd_pack(cmd)(batch, _dst, &name); \
987 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, __anv_cmd_length(cmd) * 4)); \
988 _dst = NULL; \
989 }))
990
991 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
992 .GraphicsDataTypeGFDT = 0, \
993 .LLCCacheabilityControlLLCCC = 0, \
994 .L3CacheabilityControlL3CC = 1, \
995 }
996
997 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
998 .LLCeLLCCacheabilityControlLLCCC = 0, \
999 .L3CacheabilityControlL3CC = 1, \
1000 }
1001
1002 #define GEN8_MOCS (struct GEN8_MEMORY_OBJECT_CONTROL_STATE) { \
1003 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
1004 .TargetCache = L3DefertoPATforLLCeLLCselection, \
1005 .AgeforQUADLRU = 0 \
1006 }
1007
1008 /* Skylake: MOCS is now an index into an array of 62 different caching
1009 * configurations programmed by the kernel.
1010 */
1011
1012 #define GEN9_MOCS (struct GEN9_MEMORY_OBJECT_CONTROL_STATE) { \
1013 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1014 .IndextoMOCSTables = 2 \
1015 }
1016
1017 #define GEN9_MOCS_PTE { \
1018 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1019 .IndextoMOCSTables = 1 \
1020 }
1021
1022 /* Cannonlake MOCS defines are duplicates of Skylake MOCS defines. */
1023 #define GEN10_MOCS (struct GEN10_MEMORY_OBJECT_CONTROL_STATE) { \
1024 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1025 .IndextoMOCSTables = 2 \
1026 }
1027
1028 #define GEN10_MOCS_PTE { \
1029 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1030 .IndextoMOCSTables = 1 \
1031 }
1032
1033 struct anv_device_memory {
1034 struct anv_bo * bo;
1035 struct anv_memory_type * type;
1036 VkDeviceSize map_size;
1037 void * map;
1038 };
1039
1040 /**
1041 * Header for Vertex URB Entry (VUE)
1042 */
1043 struct anv_vue_header {
1044 uint32_t Reserved;
1045 uint32_t RTAIndex; /* RenderTargetArrayIndex */
1046 uint32_t ViewportIndex;
1047 float PointWidth;
1048 };
1049
1050 struct anv_descriptor_set_binding_layout {
1051 #ifndef NDEBUG
1052 /* The type of the descriptors in this binding */
1053 VkDescriptorType type;
1054 #endif
1055
1056 /* Number of array elements in this binding */
1057 uint16_t array_size;
1058
1059 /* Index into the flattend descriptor set */
1060 uint16_t descriptor_index;
1061
1062 /* Index into the dynamic state array for a dynamic buffer */
1063 int16_t dynamic_offset_index;
1064
1065 /* Index into the descriptor set buffer views */
1066 int16_t buffer_index;
1067
1068 struct {
1069 /* Index into the binding table for the associated surface */
1070 int16_t surface_index;
1071
1072 /* Index into the sampler table for the associated sampler */
1073 int16_t sampler_index;
1074
1075 /* Index into the image table for the associated image */
1076 int16_t image_index;
1077 } stage[MESA_SHADER_STAGES];
1078
1079 /* Immutable samplers (or NULL if no immutable samplers) */
1080 struct anv_sampler **immutable_samplers;
1081 };
1082
1083 struct anv_descriptor_set_layout {
1084 /* Number of bindings in this descriptor set */
1085 uint16_t binding_count;
1086
1087 /* Total size of the descriptor set with room for all array entries */
1088 uint16_t size;
1089
1090 /* Shader stages affected by this descriptor set */
1091 uint16_t shader_stages;
1092
1093 /* Number of buffers in this descriptor set */
1094 uint16_t buffer_count;
1095
1096 /* Number of dynamic offsets used by this descriptor set */
1097 uint16_t dynamic_offset_count;
1098
1099 /* Bindings in this descriptor set */
1100 struct anv_descriptor_set_binding_layout binding[0];
1101 };
1102
1103 struct anv_descriptor {
1104 VkDescriptorType type;
1105
1106 union {
1107 struct {
1108 struct anv_image_view *image_view;
1109 struct anv_sampler *sampler;
1110
1111 /* Used to determine whether or not we need the surface state to have
1112 * the auxiliary buffer enabled.
1113 */
1114 enum isl_aux_usage aux_usage;
1115 };
1116
1117 struct {
1118 struct anv_buffer *buffer;
1119 uint64_t offset;
1120 uint64_t range;
1121 };
1122
1123 struct anv_buffer_view *buffer_view;
1124 };
1125 };
1126
1127 struct anv_descriptor_set {
1128 const struct anv_descriptor_set_layout *layout;
1129 uint32_t size;
1130 uint32_t buffer_count;
1131 struct anv_buffer_view *buffer_views;
1132 struct anv_descriptor descriptors[0];
1133 };
1134
1135 struct anv_buffer_view {
1136 enum isl_format format; /**< VkBufferViewCreateInfo::format */
1137 struct anv_bo *bo;
1138 uint32_t offset; /**< Offset into bo. */
1139 uint64_t range; /**< VkBufferViewCreateInfo::range */
1140
1141 struct anv_state surface_state;
1142 struct anv_state storage_surface_state;
1143 struct anv_state writeonly_storage_surface_state;
1144
1145 struct brw_image_param storage_image_param;
1146 };
1147
1148 struct anv_push_descriptor_set {
1149 struct anv_descriptor_set set;
1150
1151 /* Put this field right behind anv_descriptor_set so it fills up the
1152 * descriptors[0] field. */
1153 struct anv_descriptor descriptors[MAX_PUSH_DESCRIPTORS];
1154
1155 struct anv_buffer_view buffer_views[MAX_PUSH_DESCRIPTORS];
1156 };
1157
1158 struct anv_descriptor_pool {
1159 uint32_t size;
1160 uint32_t next;
1161 uint32_t free_list;
1162
1163 struct anv_state_stream surface_state_stream;
1164 void *surface_state_free_list;
1165
1166 char data[0];
1167 };
1168
1169 enum anv_descriptor_template_entry_type {
1170 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_IMAGE,
1171 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER,
1172 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER_VIEW
1173 };
1174
1175 struct anv_descriptor_template_entry {
1176 /* The type of descriptor in this entry */
1177 VkDescriptorType type;
1178
1179 /* Binding in the descriptor set */
1180 uint32_t binding;
1181
1182 /* Offset at which to write into the descriptor set binding */
1183 uint32_t array_element;
1184
1185 /* Number of elements to write into the descriptor set binding */
1186 uint32_t array_count;
1187
1188 /* Offset into the user provided data */
1189 size_t offset;
1190
1191 /* Stride between elements into the user provided data */
1192 size_t stride;
1193 };
1194
1195 struct anv_descriptor_update_template {
1196 /* The descriptor set this template corresponds to. This value is only
1197 * valid if the template was created with the templateType
1198 * VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR.
1199 */
1200 uint8_t set;
1201
1202 /* Number of entries in this template */
1203 uint32_t entry_count;
1204
1205 /* Entries of the template */
1206 struct anv_descriptor_template_entry entries[0];
1207 };
1208
1209 size_t
1210 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout);
1211
1212 void
1213 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
1214 const struct gen_device_info * const devinfo,
1215 const VkDescriptorImageInfo * const info,
1216 VkDescriptorType type,
1217 uint32_t binding,
1218 uint32_t element);
1219
1220 void
1221 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
1222 VkDescriptorType type,
1223 struct anv_buffer_view *buffer_view,
1224 uint32_t binding,
1225 uint32_t element);
1226
1227 void
1228 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
1229 struct anv_device *device,
1230 struct anv_state_stream *alloc_stream,
1231 VkDescriptorType type,
1232 struct anv_buffer *buffer,
1233 uint32_t binding,
1234 uint32_t element,
1235 VkDeviceSize offset,
1236 VkDeviceSize range);
1237
1238 void
1239 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
1240 struct anv_device *device,
1241 struct anv_state_stream *alloc_stream,
1242 const struct anv_descriptor_update_template *template,
1243 const void *data);
1244
1245 VkResult
1246 anv_descriptor_set_create(struct anv_device *device,
1247 struct anv_descriptor_pool *pool,
1248 const struct anv_descriptor_set_layout *layout,
1249 struct anv_descriptor_set **out_set);
1250
1251 void
1252 anv_descriptor_set_destroy(struct anv_device *device,
1253 struct anv_descriptor_pool *pool,
1254 struct anv_descriptor_set *set);
1255
1256 #define ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS UINT8_MAX
1257
1258 struct anv_pipeline_binding {
1259 /* The descriptor set this surface corresponds to. The special value of
1260 * ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS indicates that the offset refers
1261 * to a color attachment and not a regular descriptor.
1262 */
1263 uint8_t set;
1264
1265 /* Binding in the descriptor set */
1266 uint8_t binding;
1267
1268 /* Index in the binding */
1269 uint8_t index;
1270
1271 /* Input attachment index (relative to the subpass) */
1272 uint8_t input_attachment_index;
1273
1274 /* For a storage image, whether it is write-only */
1275 bool write_only;
1276 };
1277
1278 struct anv_pipeline_layout {
1279 struct {
1280 struct anv_descriptor_set_layout *layout;
1281 uint32_t dynamic_offset_start;
1282 } set[MAX_SETS];
1283
1284 uint32_t num_sets;
1285
1286 struct {
1287 bool has_dynamic_offsets;
1288 } stage[MESA_SHADER_STAGES];
1289
1290 unsigned char sha1[20];
1291 };
1292
1293 struct anv_buffer {
1294 struct anv_device * device;
1295 VkDeviceSize size;
1296
1297 VkBufferUsageFlags usage;
1298
1299 /* Set when bound */
1300 struct anv_bo * bo;
1301 VkDeviceSize offset;
1302 };
1303
1304 static inline uint64_t
1305 anv_buffer_get_range(struct anv_buffer *buffer, uint64_t offset, uint64_t range)
1306 {
1307 assert(offset <= buffer->size);
1308 if (range == VK_WHOLE_SIZE) {
1309 return buffer->size - offset;
1310 } else {
1311 assert(range <= buffer->size);
1312 return range;
1313 }
1314 }
1315
1316 enum anv_cmd_dirty_bits {
1317 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
1318 ANV_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
1319 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
1320 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
1321 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
1322 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
1323 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
1324 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
1325 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
1326 ANV_CMD_DIRTY_DYNAMIC_ALL = (1 << 9) - 1,
1327 ANV_CMD_DIRTY_PIPELINE = 1 << 9,
1328 ANV_CMD_DIRTY_INDEX_BUFFER = 1 << 10,
1329 ANV_CMD_DIRTY_RENDER_TARGETS = 1 << 11,
1330 };
1331 typedef uint32_t anv_cmd_dirty_mask_t;
1332
1333 enum anv_pipe_bits {
1334 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT = (1 << 0),
1335 ANV_PIPE_STALL_AT_SCOREBOARD_BIT = (1 << 1),
1336 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT = (1 << 2),
1337 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT = (1 << 3),
1338 ANV_PIPE_VF_CACHE_INVALIDATE_BIT = (1 << 4),
1339 ANV_PIPE_DATA_CACHE_FLUSH_BIT = (1 << 5),
1340 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT = (1 << 10),
1341 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT = (1 << 11),
1342 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT = (1 << 12),
1343 ANV_PIPE_DEPTH_STALL_BIT = (1 << 13),
1344 ANV_PIPE_CS_STALL_BIT = (1 << 20),
1345
1346 /* This bit does not exist directly in PIPE_CONTROL. Instead it means that
1347 * a flush has happened but not a CS stall. The next time we do any sort
1348 * of invalidation we need to insert a CS stall at that time. Otherwise,
1349 * we would have to CS stall on every flush which could be bad.
1350 */
1351 ANV_PIPE_NEEDS_CS_STALL_BIT = (1 << 21),
1352 };
1353
1354 #define ANV_PIPE_FLUSH_BITS ( \
1355 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT | \
1356 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1357 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1358
1359 #define ANV_PIPE_STALL_BITS ( \
1360 ANV_PIPE_STALL_AT_SCOREBOARD_BIT | \
1361 ANV_PIPE_DEPTH_STALL_BIT | \
1362 ANV_PIPE_CS_STALL_BIT)
1363
1364 #define ANV_PIPE_INVALIDATE_BITS ( \
1365 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT | \
1366 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT | \
1367 ANV_PIPE_VF_CACHE_INVALIDATE_BIT | \
1368 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1369 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
1370 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
1371
1372 static inline enum anv_pipe_bits
1373 anv_pipe_flush_bits_for_access_flags(VkAccessFlags flags)
1374 {
1375 enum anv_pipe_bits pipe_bits = 0;
1376
1377 unsigned b;
1378 for_each_bit(b, flags) {
1379 switch ((VkAccessFlagBits)(1 << b)) {
1380 case VK_ACCESS_SHADER_WRITE_BIT:
1381 pipe_bits |= ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1382 break;
1383 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT:
1384 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1385 break;
1386 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT:
1387 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1388 break;
1389 case VK_ACCESS_TRANSFER_WRITE_BIT:
1390 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1391 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1392 break;
1393 default:
1394 break; /* Nothing to do */
1395 }
1396 }
1397
1398 return pipe_bits;
1399 }
1400
1401 static inline enum anv_pipe_bits
1402 anv_pipe_invalidate_bits_for_access_flags(VkAccessFlags flags)
1403 {
1404 enum anv_pipe_bits pipe_bits = 0;
1405
1406 unsigned b;
1407 for_each_bit(b, flags) {
1408 switch ((VkAccessFlagBits)(1 << b)) {
1409 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT:
1410 case VK_ACCESS_INDEX_READ_BIT:
1411 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT:
1412 pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1413 break;
1414 case VK_ACCESS_UNIFORM_READ_BIT:
1415 pipe_bits |= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1416 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1417 break;
1418 case VK_ACCESS_SHADER_READ_BIT:
1419 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT:
1420 case VK_ACCESS_TRANSFER_READ_BIT:
1421 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1422 break;
1423 default:
1424 break; /* Nothing to do */
1425 }
1426 }
1427
1428 return pipe_bits;
1429 }
1430
1431 struct anv_vertex_binding {
1432 struct anv_buffer * buffer;
1433 VkDeviceSize offset;
1434 };
1435
1436 struct anv_push_constants {
1437 /* Current allocated size of this push constants data structure.
1438 * Because a decent chunk of it may not be used (images on SKL, for
1439 * instance), we won't actually allocate the entire structure up-front.
1440 */
1441 uint32_t size;
1442
1443 /* Push constant data provided by the client through vkPushConstants */
1444 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
1445
1446 /* Our hardware only provides zero-based vertex and instance id so, in
1447 * order to satisfy the vulkan requirements, we may have to push one or
1448 * both of these into the shader.
1449 */
1450 uint32_t base_vertex;
1451 uint32_t base_instance;
1452
1453 /* Image data for image_load_store on pre-SKL */
1454 struct brw_image_param images[MAX_IMAGES];
1455 };
1456
1457 struct anv_dynamic_state {
1458 struct {
1459 uint32_t count;
1460 VkViewport viewports[MAX_VIEWPORTS];
1461 } viewport;
1462
1463 struct {
1464 uint32_t count;
1465 VkRect2D scissors[MAX_SCISSORS];
1466 } scissor;
1467
1468 float line_width;
1469
1470 struct {
1471 float bias;
1472 float clamp;
1473 float slope;
1474 } depth_bias;
1475
1476 float blend_constants[4];
1477
1478 struct {
1479 float min;
1480 float max;
1481 } depth_bounds;
1482
1483 struct {
1484 uint32_t front;
1485 uint32_t back;
1486 } stencil_compare_mask;
1487
1488 struct {
1489 uint32_t front;
1490 uint32_t back;
1491 } stencil_write_mask;
1492
1493 struct {
1494 uint32_t front;
1495 uint32_t back;
1496 } stencil_reference;
1497 };
1498
1499 extern const struct anv_dynamic_state default_dynamic_state;
1500
1501 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
1502 const struct anv_dynamic_state *src,
1503 uint32_t copy_mask);
1504
1505 /**
1506 * Attachment state when recording a renderpass instance.
1507 *
1508 * The clear value is valid only if there exists a pending clear.
1509 */
1510 struct anv_attachment_state {
1511 enum isl_aux_usage aux_usage;
1512 enum isl_aux_usage input_aux_usage;
1513 struct anv_state color_rt_state;
1514 struct anv_state input_att_state;
1515
1516 VkImageLayout current_layout;
1517 VkImageAspectFlags pending_clear_aspects;
1518 bool fast_clear;
1519 VkClearValue clear_value;
1520 bool clear_color_is_zero_one;
1521 };
1522
1523 /** State required while building cmd buffer */
1524 struct anv_cmd_state {
1525 /* PIPELINE_SELECT.PipelineSelection */
1526 uint32_t current_pipeline;
1527 const struct gen_l3_config * current_l3_config;
1528 uint32_t vb_dirty;
1529 anv_cmd_dirty_mask_t dirty;
1530 anv_cmd_dirty_mask_t compute_dirty;
1531 enum anv_pipe_bits pending_pipe_bits;
1532 uint32_t num_workgroups_offset;
1533 struct anv_bo *num_workgroups_bo;
1534 VkShaderStageFlags descriptors_dirty;
1535 VkShaderStageFlags push_constants_dirty;
1536 uint32_t scratch_size;
1537 struct anv_pipeline * pipeline;
1538 struct anv_pipeline * compute_pipeline;
1539 struct anv_framebuffer * framebuffer;
1540 struct anv_render_pass * pass;
1541 struct anv_subpass * subpass;
1542 VkRect2D render_area;
1543 uint32_t restart_index;
1544 struct anv_vertex_binding vertex_bindings[MAX_VBS];
1545 struct anv_descriptor_set * descriptors[MAX_SETS];
1546 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
1547 VkShaderStageFlags push_constant_stages;
1548 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
1549 struct anv_state binding_tables[MESA_SHADER_STAGES];
1550 struct anv_state samplers[MESA_SHADER_STAGES];
1551 struct anv_dynamic_state dynamic;
1552 bool need_query_wa;
1553
1554 struct anv_push_descriptor_set push_descriptor;
1555
1556 /**
1557 * Whether or not the gen8 PMA fix is enabled. We ensure that, at the top
1558 * of any command buffer it is disabled by disabling it in EndCommandBuffer
1559 * and before invoking the secondary in ExecuteCommands.
1560 */
1561 bool pma_fix_enabled;
1562
1563 /**
1564 * Whether or not we know for certain that HiZ is enabled for the current
1565 * subpass. If, for whatever reason, we are unsure as to whether HiZ is
1566 * enabled or not, this will be false.
1567 */
1568 bool hiz_enabled;
1569
1570 /**
1571 * Array length is anv_cmd_state::pass::attachment_count. Array content is
1572 * valid only when recording a render pass instance.
1573 */
1574 struct anv_attachment_state * attachments;
1575
1576 /**
1577 * Surface states for color render targets. These are stored in a single
1578 * flat array. For depth-stencil attachments, the surface state is simply
1579 * left blank.
1580 */
1581 struct anv_state render_pass_states;
1582
1583 /**
1584 * A null surface state of the right size to match the framebuffer. This
1585 * is one of the states in render_pass_states.
1586 */
1587 struct anv_state null_surface_state;
1588
1589 struct {
1590 struct anv_buffer * index_buffer;
1591 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1592 uint32_t index_offset;
1593 } gen7;
1594 };
1595
1596 struct anv_cmd_pool {
1597 VkAllocationCallbacks alloc;
1598 struct list_head cmd_buffers;
1599 };
1600
1601 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1602
1603 enum anv_cmd_buffer_exec_mode {
1604 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
1605 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
1606 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT,
1607 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
1608 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
1609 };
1610
1611 struct anv_cmd_buffer {
1612 VK_LOADER_DATA _loader_data;
1613
1614 struct anv_device * device;
1615
1616 struct anv_cmd_pool * pool;
1617 struct list_head pool_link;
1618
1619 struct anv_batch batch;
1620
1621 /* Fields required for the actual chain of anv_batch_bo's.
1622 *
1623 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1624 */
1625 struct list_head batch_bos;
1626 enum anv_cmd_buffer_exec_mode exec_mode;
1627
1628 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1629 * referenced by this command buffer
1630 *
1631 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1632 */
1633 struct u_vector seen_bbos;
1634
1635 /* A vector of int32_t's for every block of binding tables.
1636 *
1637 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1638 */
1639 struct u_vector bt_block_states;
1640 uint32_t bt_next;
1641
1642 struct anv_reloc_list surface_relocs;
1643 /** Last seen surface state block pool center bo offset */
1644 uint32_t last_ss_pool_center;
1645
1646 /* Serial for tracking buffer completion */
1647 uint32_t serial;
1648
1649 /* Stream objects for storing temporary data */
1650 struct anv_state_stream surface_state_stream;
1651 struct anv_state_stream dynamic_state_stream;
1652
1653 VkCommandBufferUsageFlags usage_flags;
1654 VkCommandBufferLevel level;
1655
1656 struct anv_cmd_state state;
1657 };
1658
1659 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1660 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1661 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1662 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
1663 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
1664 struct anv_cmd_buffer *secondary);
1665 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
1666 VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
1667 struct anv_cmd_buffer *cmd_buffer,
1668 const VkSemaphore *in_semaphores,
1669 uint32_t num_in_semaphores,
1670 const VkSemaphore *out_semaphores,
1671 uint32_t num_out_semaphores);
1672
1673 VkResult anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer);
1674
1675 VkResult
1676 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
1677 gl_shader_stage stage, uint32_t size);
1678 #define anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, field) \
1679 anv_cmd_buffer_ensure_push_constants_size(cmd_buffer, stage, \
1680 (offsetof(struct anv_push_constants, field) + \
1681 sizeof(cmd_buffer->state.push_constants[0]->field)))
1682
1683 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
1684 const void *data, uint32_t size, uint32_t alignment);
1685 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
1686 uint32_t *a, uint32_t *b,
1687 uint32_t dwords, uint32_t alignment);
1688
1689 struct anv_address
1690 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1691 struct anv_state
1692 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1693 uint32_t entries, uint32_t *state_offset);
1694 struct anv_state
1695 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1696 struct anv_state
1697 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1698 uint32_t size, uint32_t alignment);
1699
1700 VkResult
1701 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1702
1703 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1704 void gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer *cmd_buffer,
1705 bool depth_clamp_enable);
1706 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1707
1708 void anv_cmd_buffer_setup_attachments(struct anv_cmd_buffer *cmd_buffer,
1709 struct anv_render_pass *pass,
1710 struct anv_framebuffer *framebuffer,
1711 const VkClearValue *clear_values);
1712
1713 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1714
1715 struct anv_state
1716 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1717 gl_shader_stage stage);
1718 struct anv_state
1719 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer);
1720
1721 void anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer *cmd_buffer);
1722 void anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer);
1723
1724 const struct anv_image_view *
1725 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1726
1727 VkResult
1728 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
1729 uint32_t num_entries,
1730 uint32_t *state_offset,
1731 struct anv_state *bt_state);
1732
1733 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1734
1735 enum anv_fence_state {
1736 /** Indicates that this is a new (or newly reset fence) */
1737 ANV_FENCE_STATE_RESET,
1738
1739 /** Indicates that this fence has been submitted to the GPU but is still
1740 * (as far as we know) in use by the GPU.
1741 */
1742 ANV_FENCE_STATE_SUBMITTED,
1743
1744 ANV_FENCE_STATE_SIGNALED,
1745 };
1746
1747 struct anv_fence {
1748 struct anv_bo bo;
1749 struct drm_i915_gem_execbuffer2 execbuf;
1750 struct drm_i915_gem_exec_object2 exec2_objects[1];
1751 enum anv_fence_state state;
1752 };
1753
1754 struct anv_event {
1755 uint64_t semaphore;
1756 struct anv_state state;
1757 };
1758
1759 enum anv_semaphore_type {
1760 ANV_SEMAPHORE_TYPE_NONE = 0,
1761 ANV_SEMAPHORE_TYPE_DUMMY,
1762 ANV_SEMAPHORE_TYPE_BO,
1763 };
1764
1765 struct anv_semaphore_impl {
1766 enum anv_semaphore_type type;
1767
1768 /* A BO representing this semaphore when type == ANV_SEMAPHORE_TYPE_BO.
1769 * This BO will be added to the object list on any execbuf2 calls for
1770 * which this semaphore is used as a wait or signal fence. When used as
1771 * a signal fence, the EXEC_OBJECT_WRITE flag will be set.
1772 */
1773 struct anv_bo *bo;
1774 };
1775
1776 struct anv_semaphore {
1777 /* Permanent semaphore state. Every semaphore has some form of permanent
1778 * state (type != ANV_SEMAPHORE_TYPE_NONE). This may be a BO to fence on
1779 * (for cross-process semaphores0 or it could just be a dummy for use
1780 * internally.
1781 */
1782 struct anv_semaphore_impl permanent;
1783
1784 /* Temporary semaphore state. A semaphore *may* have temporary state.
1785 * That state is added to the semaphore by an import operation and is reset
1786 * back to ANV_SEMAPHORE_TYPE_NONE when the semaphore is waited on. A
1787 * semaphore with temporary state cannot be signaled because the semaphore
1788 * must already be signaled before the temporary state can be exported from
1789 * the semaphore in the other process and imported here.
1790 */
1791 struct anv_semaphore_impl temporary;
1792 };
1793
1794 struct anv_shader_module {
1795 unsigned char sha1[20];
1796 uint32_t size;
1797 char data[0];
1798 };
1799
1800 static inline gl_shader_stage
1801 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1802 {
1803 assert(__builtin_popcount(vk_stage) == 1);
1804 return ffs(vk_stage) - 1;
1805 }
1806
1807 static inline VkShaderStageFlagBits
1808 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1809 {
1810 return (1 << mesa_stage);
1811 }
1812
1813 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1814
1815 #define anv_foreach_stage(stage, stage_bits) \
1816 for (gl_shader_stage stage, \
1817 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
1818 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1819 __tmp &= ~(1 << (stage)))
1820
1821 struct anv_pipeline_bind_map {
1822 uint32_t surface_count;
1823 uint32_t sampler_count;
1824 uint32_t image_count;
1825
1826 struct anv_pipeline_binding * surface_to_descriptor;
1827 struct anv_pipeline_binding * sampler_to_descriptor;
1828 };
1829
1830 struct anv_shader_bin_key {
1831 uint32_t size;
1832 uint8_t data[0];
1833 };
1834
1835 struct anv_shader_bin {
1836 uint32_t ref_cnt;
1837
1838 const struct anv_shader_bin_key *key;
1839
1840 struct anv_state kernel;
1841 uint32_t kernel_size;
1842
1843 const struct brw_stage_prog_data *prog_data;
1844 uint32_t prog_data_size;
1845
1846 struct anv_pipeline_bind_map bind_map;
1847
1848 /* Prog data follows, then params, then the key, all aligned to 8-bytes */
1849 };
1850
1851 struct anv_shader_bin *
1852 anv_shader_bin_create(struct anv_device *device,
1853 const void *key, uint32_t key_size,
1854 const void *kernel, uint32_t kernel_size,
1855 const struct brw_stage_prog_data *prog_data,
1856 uint32_t prog_data_size, const void *prog_data_param,
1857 const struct anv_pipeline_bind_map *bind_map);
1858
1859 void
1860 anv_shader_bin_destroy(struct anv_device *device, struct anv_shader_bin *shader);
1861
1862 static inline void
1863 anv_shader_bin_ref(struct anv_shader_bin *shader)
1864 {
1865 assert(shader && shader->ref_cnt >= 1);
1866 p_atomic_inc(&shader->ref_cnt);
1867 }
1868
1869 static inline void
1870 anv_shader_bin_unref(struct anv_device *device, struct anv_shader_bin *shader)
1871 {
1872 assert(shader && shader->ref_cnt >= 1);
1873 if (p_atomic_dec_zero(&shader->ref_cnt))
1874 anv_shader_bin_destroy(device, shader);
1875 }
1876
1877 struct anv_pipeline {
1878 struct anv_device * device;
1879 struct anv_batch batch;
1880 uint32_t batch_data[512];
1881 struct anv_reloc_list batch_relocs;
1882 uint32_t dynamic_state_mask;
1883 struct anv_dynamic_state dynamic_state;
1884
1885 struct anv_subpass * subpass;
1886 struct anv_pipeline_layout * layout;
1887
1888 bool needs_data_cache;
1889
1890 struct anv_shader_bin * shaders[MESA_SHADER_STAGES];
1891
1892 struct {
1893 const struct gen_l3_config * l3_config;
1894 uint32_t total_size;
1895 } urb;
1896
1897 VkShaderStageFlags active_stages;
1898 struct anv_state blend_state;
1899
1900 uint32_t vb_used;
1901 uint32_t binding_stride[MAX_VBS];
1902 bool instancing_enable[MAX_VBS];
1903 bool primitive_restart;
1904 uint32_t topology;
1905
1906 uint32_t cs_right_mask;
1907
1908 bool writes_depth;
1909 bool depth_test_enable;
1910 bool writes_stencil;
1911 bool stencil_test_enable;
1912 bool depth_clamp_enable;
1913 bool sample_shading_enable;
1914 bool kill_pixel;
1915
1916 struct {
1917 uint32_t sf[7];
1918 uint32_t depth_stencil_state[3];
1919 } gen7;
1920
1921 struct {
1922 uint32_t sf[4];
1923 uint32_t raster[5];
1924 uint32_t wm_depth_stencil[3];
1925 } gen8;
1926
1927 struct {
1928 uint32_t wm_depth_stencil[4];
1929 } gen9;
1930
1931 uint32_t interface_descriptor_data[8];
1932 };
1933
1934 static inline bool
1935 anv_pipeline_has_stage(const struct anv_pipeline *pipeline,
1936 gl_shader_stage stage)
1937 {
1938 return (pipeline->active_stages & mesa_to_vk_shader_stage(stage)) != 0;
1939 }
1940
1941 #define ANV_DECL_GET_PROG_DATA_FUNC(prefix, stage) \
1942 static inline const struct brw_##prefix##_prog_data * \
1943 get_##prefix##_prog_data(const struct anv_pipeline *pipeline) \
1944 { \
1945 if (anv_pipeline_has_stage(pipeline, stage)) { \
1946 return (const struct brw_##prefix##_prog_data *) \
1947 pipeline->shaders[stage]->prog_data; \
1948 } else { \
1949 return NULL; \
1950 } \
1951 }
1952
1953 ANV_DECL_GET_PROG_DATA_FUNC(vs, MESA_SHADER_VERTEX)
1954 ANV_DECL_GET_PROG_DATA_FUNC(tcs, MESA_SHADER_TESS_CTRL)
1955 ANV_DECL_GET_PROG_DATA_FUNC(tes, MESA_SHADER_TESS_EVAL)
1956 ANV_DECL_GET_PROG_DATA_FUNC(gs, MESA_SHADER_GEOMETRY)
1957 ANV_DECL_GET_PROG_DATA_FUNC(wm, MESA_SHADER_FRAGMENT)
1958 ANV_DECL_GET_PROG_DATA_FUNC(cs, MESA_SHADER_COMPUTE)
1959
1960 static inline const struct brw_vue_prog_data *
1961 anv_pipeline_get_last_vue_prog_data(const struct anv_pipeline *pipeline)
1962 {
1963 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
1964 return &get_gs_prog_data(pipeline)->base;
1965 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1966 return &get_tes_prog_data(pipeline)->base;
1967 else
1968 return &get_vs_prog_data(pipeline)->base;
1969 }
1970
1971 VkResult
1972 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1973 struct anv_pipeline_cache *cache,
1974 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1975 const VkAllocationCallbacks *alloc);
1976
1977 VkResult
1978 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1979 struct anv_pipeline_cache *cache,
1980 const VkComputePipelineCreateInfo *info,
1981 struct anv_shader_module *module,
1982 const char *entrypoint,
1983 const VkSpecializationInfo *spec_info);
1984
1985 struct anv_format {
1986 enum isl_format isl_format:16;
1987 struct isl_swizzle swizzle;
1988 };
1989
1990 struct anv_format
1991 anv_get_format(const struct gen_device_info *devinfo, VkFormat format,
1992 VkImageAspectFlags aspect, VkImageTiling tiling);
1993
1994 static inline enum isl_format
1995 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
1996 VkImageAspectFlags aspect, VkImageTiling tiling)
1997 {
1998 return anv_get_format(devinfo, vk_format, aspect, tiling).isl_format;
1999 }
2000
2001 static inline struct isl_swizzle
2002 anv_swizzle_for_render(struct isl_swizzle swizzle)
2003 {
2004 /* Sometimes the swizzle will have alpha map to one. We do this to fake
2005 * RGB as RGBA for texturing
2006 */
2007 assert(swizzle.a == ISL_CHANNEL_SELECT_ONE ||
2008 swizzle.a == ISL_CHANNEL_SELECT_ALPHA);
2009
2010 /* But it doesn't matter what we render to that channel */
2011 swizzle.a = ISL_CHANNEL_SELECT_ALPHA;
2012
2013 return swizzle;
2014 }
2015
2016 void
2017 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm);
2018
2019 /**
2020 * Subsurface of an anv_image.
2021 */
2022 struct anv_surface {
2023 /** Valid only if isl_surf::size > 0. */
2024 struct isl_surf isl;
2025
2026 /**
2027 * Offset from VkImage's base address, as bound by vkBindImageMemory().
2028 */
2029 uint32_t offset;
2030 };
2031
2032 struct anv_image {
2033 VkImageType type;
2034 /* The original VkFormat provided by the client. This may not match any
2035 * of the actual surface formats.
2036 */
2037 VkFormat vk_format;
2038 VkImageAspectFlags aspects;
2039 VkExtent3D extent;
2040 uint32_t levels;
2041 uint32_t array_size;
2042 uint32_t samples; /**< VkImageCreateInfo::samples */
2043 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
2044 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
2045
2046 VkDeviceSize size;
2047 uint32_t alignment;
2048
2049 /* Set when bound */
2050 struct anv_bo *bo;
2051 VkDeviceSize offset;
2052
2053 /**
2054 * Image subsurfaces
2055 *
2056 * For each foo, anv_image::foo_surface is valid if and only if
2057 * anv_image::aspects has a foo aspect.
2058 *
2059 * The hardware requires that the depth buffer and stencil buffer be
2060 * separate surfaces. From Vulkan's perspective, though, depth and stencil
2061 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
2062 * allocate the depth and stencil buffers as separate surfaces in the same
2063 * bo.
2064 */
2065 union {
2066 struct anv_surface color_surface;
2067
2068 struct {
2069 struct anv_surface depth_surface;
2070 struct anv_surface stencil_surface;
2071 };
2072 };
2073
2074 /**
2075 * For color images, this is the aux usage for this image when not used as a
2076 * color attachment.
2077 *
2078 * For depth/stencil images, this is set to ISL_AUX_USAGE_HIZ if the image
2079 * has a HiZ buffer.
2080 */
2081 enum isl_aux_usage aux_usage;
2082
2083 struct anv_surface aux_surface;
2084 };
2085
2086 /* Returns true if a HiZ-enabled depth buffer can be sampled from. */
2087 static inline bool
2088 anv_can_sample_with_hiz(const struct gen_device_info * const devinfo,
2089 const VkImageAspectFlags aspect_mask,
2090 const uint32_t samples)
2091 {
2092 /* Validate the inputs. */
2093 assert(devinfo && aspect_mask && samples);
2094 return devinfo->gen >= 8 && (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) &&
2095 samples == 1;
2096 }
2097
2098 void
2099 anv_gen8_hiz_op_resolve(struct anv_cmd_buffer *cmd_buffer,
2100 const struct anv_image *image,
2101 enum blorp_hiz_op op);
2102
2103 void
2104 anv_image_ccs_clear(struct anv_cmd_buffer *cmd_buffer,
2105 const struct anv_image *image,
2106 const struct isl_view *view,
2107 const VkImageSubresourceRange *subresourceRange);
2108
2109 enum isl_aux_usage
2110 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
2111 const struct anv_image *image,
2112 const VkImageAspectFlags aspects,
2113 const VkImageLayout layout);
2114
2115 /* This is defined as a macro so that it works for both
2116 * VkImageSubresourceRange and VkImageSubresourceLayers
2117 */
2118 #define anv_get_layerCount(_image, _range) \
2119 ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
2120 (_image)->array_size - (_range)->baseArrayLayer : (_range)->layerCount)
2121
2122 static inline uint32_t
2123 anv_get_levelCount(const struct anv_image *image,
2124 const VkImageSubresourceRange *range)
2125 {
2126 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
2127 image->levels - range->baseMipLevel : range->levelCount;
2128 }
2129
2130
2131 struct anv_image_view {
2132 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
2133 struct anv_bo *bo;
2134 uint32_t offset; /**< Offset into bo. */
2135
2136 struct isl_view isl;
2137
2138 VkImageAspectFlags aspect_mask;
2139 VkFormat vk_format;
2140 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
2141
2142 /** RENDER_SURFACE_STATE when using image as a sampler surface. */
2143 struct anv_state sampler_surface_state;
2144
2145 /**
2146 * RENDER_SURFACE_STATE when using image as a sampler surface with the
2147 * auxiliary buffer disabled.
2148 */
2149 struct anv_state no_aux_sampler_surface_state;
2150
2151 /**
2152 * RENDER_SURFACE_STATE when using image as a storage image. Separate states
2153 * for write-only and readable, using the real format for write-only and the
2154 * lowered format for readable.
2155 */
2156 struct anv_state storage_surface_state;
2157 struct anv_state writeonly_storage_surface_state;
2158
2159 struct brw_image_param storage_image_param;
2160 };
2161
2162 struct anv_image_create_info {
2163 const VkImageCreateInfo *vk_info;
2164
2165 /** An opt-in bitmask which filters an ISL-mapping of the Vulkan tiling. */
2166 isl_tiling_flags_t isl_tiling_flags;
2167
2168 uint32_t stride;
2169 };
2170
2171 VkResult anv_image_create(VkDevice _device,
2172 const struct anv_image_create_info *info,
2173 const VkAllocationCallbacks* alloc,
2174 VkImage *pImage);
2175
2176 const struct anv_surface *
2177 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
2178 VkImageAspectFlags aspect_mask);
2179
2180 enum isl_format
2181 anv_isl_format_for_descriptor_type(VkDescriptorType type);
2182
2183 static inline struct VkExtent3D
2184 anv_sanitize_image_extent(const VkImageType imageType,
2185 const struct VkExtent3D imageExtent)
2186 {
2187 switch (imageType) {
2188 case VK_IMAGE_TYPE_1D:
2189 return (VkExtent3D) { imageExtent.width, 1, 1 };
2190 case VK_IMAGE_TYPE_2D:
2191 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
2192 case VK_IMAGE_TYPE_3D:
2193 return imageExtent;
2194 default:
2195 unreachable("invalid image type");
2196 }
2197 }
2198
2199 static inline struct VkOffset3D
2200 anv_sanitize_image_offset(const VkImageType imageType,
2201 const struct VkOffset3D imageOffset)
2202 {
2203 switch (imageType) {
2204 case VK_IMAGE_TYPE_1D:
2205 return (VkOffset3D) { imageOffset.x, 0, 0 };
2206 case VK_IMAGE_TYPE_2D:
2207 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
2208 case VK_IMAGE_TYPE_3D:
2209 return imageOffset;
2210 default:
2211 unreachable("invalid image type");
2212 }
2213 }
2214
2215
2216 void anv_fill_buffer_surface_state(struct anv_device *device,
2217 struct anv_state state,
2218 enum isl_format format,
2219 uint32_t offset, uint32_t range,
2220 uint32_t stride);
2221
2222 void anv_image_view_fill_image_param(struct anv_device *device,
2223 struct anv_image_view *view,
2224 struct brw_image_param *param);
2225 void anv_buffer_view_fill_image_param(struct anv_device *device,
2226 struct anv_buffer_view *view,
2227 struct brw_image_param *param);
2228
2229 struct anv_sampler {
2230 uint32_t state[4];
2231 };
2232
2233 struct anv_framebuffer {
2234 uint32_t width;
2235 uint32_t height;
2236 uint32_t layers;
2237
2238 uint32_t attachment_count;
2239 struct anv_image_view * attachments[0];
2240 };
2241
2242 struct anv_subpass {
2243 uint32_t attachment_count;
2244
2245 /**
2246 * A pointer to all attachment references used in this subpass.
2247 * Only valid if ::attachment_count > 0.
2248 */
2249 VkAttachmentReference * attachments;
2250 uint32_t input_count;
2251 VkAttachmentReference * input_attachments;
2252 uint32_t color_count;
2253 VkAttachmentReference * color_attachments;
2254 VkAttachmentReference * resolve_attachments;
2255
2256 VkAttachmentReference depth_stencil_attachment;
2257
2258 uint32_t view_mask;
2259
2260 /** Subpass has a depth/stencil self-dependency */
2261 bool has_ds_self_dep;
2262
2263 /** Subpass has at least one resolve attachment */
2264 bool has_resolve;
2265 };
2266
2267 static inline unsigned
2268 anv_subpass_view_count(const struct anv_subpass *subpass)
2269 {
2270 return MAX2(1, _mesa_bitcount(subpass->view_mask));
2271 }
2272
2273 enum anv_subpass_usage {
2274 ANV_SUBPASS_USAGE_DRAW = (1 << 0),
2275 ANV_SUBPASS_USAGE_INPUT = (1 << 1),
2276 ANV_SUBPASS_USAGE_RESOLVE_SRC = (1 << 2),
2277 ANV_SUBPASS_USAGE_RESOLVE_DST = (1 << 3),
2278 };
2279
2280 struct anv_render_pass_attachment {
2281 /* TODO: Consider using VkAttachmentDescription instead of storing each of
2282 * its members individually.
2283 */
2284 VkFormat format;
2285 uint32_t samples;
2286 VkImageUsageFlags usage;
2287 VkAttachmentLoadOp load_op;
2288 VkAttachmentStoreOp store_op;
2289 VkAttachmentLoadOp stencil_load_op;
2290 VkImageLayout initial_layout;
2291 VkImageLayout final_layout;
2292
2293 /* An array, indexed by subpass id, of how the attachment will be used. */
2294 enum anv_subpass_usage * subpass_usage;
2295
2296 /* The subpass id in which the attachment will be used last. */
2297 uint32_t last_subpass_idx;
2298 };
2299
2300 struct anv_render_pass {
2301 uint32_t attachment_count;
2302 uint32_t subpass_count;
2303 /* An array of subpass_count+1 flushes, one per subpass boundary */
2304 enum anv_pipe_bits * subpass_flushes;
2305 struct anv_render_pass_attachment * attachments;
2306 struct anv_subpass subpasses[0];
2307 };
2308
2309 #define ANV_PIPELINE_STATISTICS_MASK 0x000007ff
2310
2311 struct anv_query_pool {
2312 VkQueryType type;
2313 VkQueryPipelineStatisticFlags pipeline_statistics;
2314 /** Stride between slots, in bytes */
2315 uint32_t stride;
2316 /** Number of slots in this query pool */
2317 uint32_t slots;
2318 struct anv_bo bo;
2319 };
2320
2321 void *anv_lookup_entrypoint(const struct gen_device_info *devinfo,
2322 const char *name);
2323
2324 void anv_dump_image_to_ppm(struct anv_device *device,
2325 struct anv_image *image, unsigned miplevel,
2326 unsigned array_layer, VkImageAspectFlagBits aspect,
2327 const char *filename);
2328
2329 enum anv_dump_action {
2330 ANV_DUMP_FRAMEBUFFERS_BIT = 0x1,
2331 };
2332
2333 void anv_dump_start(struct anv_device *device, enum anv_dump_action actions);
2334 void anv_dump_finish(void);
2335
2336 void anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
2337 struct anv_framebuffer *fb);
2338
2339 static inline uint32_t
2340 anv_get_subpass_id(const struct anv_cmd_state * const cmd_state)
2341 {
2342 /* This function must be called from within a subpass. */
2343 assert(cmd_state->pass && cmd_state->subpass);
2344
2345 const uint32_t subpass_id = cmd_state->subpass - cmd_state->pass->subpasses;
2346
2347 /* The id of this subpass shouldn't exceed the number of subpasses in this
2348 * render pass minus 1.
2349 */
2350 assert(subpass_id < cmd_state->pass->subpass_count);
2351 return subpass_id;
2352 }
2353
2354 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
2355 \
2356 static inline struct __anv_type * \
2357 __anv_type ## _from_handle(__VkType _handle) \
2358 { \
2359 return (struct __anv_type *) _handle; \
2360 } \
2361 \
2362 static inline __VkType \
2363 __anv_type ## _to_handle(struct __anv_type *_obj) \
2364 { \
2365 return (__VkType) _obj; \
2366 }
2367
2368 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
2369 \
2370 static inline struct __anv_type * \
2371 __anv_type ## _from_handle(__VkType _handle) \
2372 { \
2373 return (struct __anv_type *)(uintptr_t) _handle; \
2374 } \
2375 \
2376 static inline __VkType \
2377 __anv_type ## _to_handle(struct __anv_type *_obj) \
2378 { \
2379 return (__VkType)(uintptr_t) _obj; \
2380 }
2381
2382 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
2383 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
2384
2385 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
2386 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
2387 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
2388 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
2389 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
2390
2391 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
2392 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
2393 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView)
2394 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool, VkDescriptorPool)
2395 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
2396 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
2397 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_update_template, VkDescriptorUpdateTemplateKHR)
2398 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
2399 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
2400 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event, VkEvent)
2401 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
2402 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
2403 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
2404 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache, VkPipelineCache)
2405 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
2406 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
2407 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
2408 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
2409 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
2410 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_semaphore, VkSemaphore)
2411 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
2412
2413 /* Gen-specific function declarations */
2414 #ifdef genX
2415 # include "anv_genX.h"
2416 #else
2417 # define genX(x) gen7_##x
2418 # include "anv_genX.h"
2419 # undef genX
2420 # define genX(x) gen75_##x
2421 # include "anv_genX.h"
2422 # undef genX
2423 # define genX(x) gen8_##x
2424 # include "anv_genX.h"
2425 # undef genX
2426 # define genX(x) gen9_##x
2427 # include "anv_genX.h"
2428 # undef genX
2429 # define genX(x) gen10_##x
2430 # include "anv_genX.h"
2431 # undef genX
2432 #endif
2433
2434 #endif /* ANV_PRIVATE_H */