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