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