anv/gem: Add a drm syncobj support
[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 28
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 bool has_exec_fence;
656
657 uint32_t eu_total;
658 uint32_t subslice_total;
659
660 struct {
661 uint32_t type_count;
662 struct anv_memory_type types[VK_MAX_MEMORY_TYPES];
663 uint32_t heap_count;
664 struct anv_memory_heap heaps[VK_MAX_MEMORY_HEAPS];
665 } memory;
666
667 uint8_t pipeline_cache_uuid[VK_UUID_SIZE];
668 uint8_t driver_uuid[VK_UUID_SIZE];
669 uint8_t device_uuid[VK_UUID_SIZE];
670
671 struct wsi_device wsi_device;
672 int local_fd;
673 };
674
675 struct anv_instance {
676 VK_LOADER_DATA _loader_data;
677
678 VkAllocationCallbacks alloc;
679
680 uint32_t apiVersion;
681 int physicalDeviceCount;
682 struct anv_physical_device physicalDevice;
683 };
684
685 VkResult anv_init_wsi(struct anv_physical_device *physical_device);
686 void anv_finish_wsi(struct anv_physical_device *physical_device);
687
688 bool anv_instance_extension_supported(const char *name);
689 uint32_t anv_physical_device_api_version(struct anv_physical_device *dev);
690 bool anv_physical_device_extension_supported(struct anv_physical_device *dev,
691 const char *name);
692
693 struct anv_queue {
694 VK_LOADER_DATA _loader_data;
695
696 struct anv_device * device;
697
698 struct anv_state_pool * pool;
699 };
700
701 struct anv_pipeline_cache {
702 struct anv_device * device;
703 pthread_mutex_t mutex;
704
705 struct hash_table * cache;
706 };
707
708 struct anv_pipeline_bind_map;
709
710 void anv_pipeline_cache_init(struct anv_pipeline_cache *cache,
711 struct anv_device *device,
712 bool cache_enabled);
713 void anv_pipeline_cache_finish(struct anv_pipeline_cache *cache);
714
715 struct anv_shader_bin *
716 anv_pipeline_cache_search(struct anv_pipeline_cache *cache,
717 const void *key, uint32_t key_size);
718 struct anv_shader_bin *
719 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache *cache,
720 const void *key_data, uint32_t key_size,
721 const void *kernel_data, uint32_t kernel_size,
722 const struct brw_stage_prog_data *prog_data,
723 uint32_t prog_data_size,
724 const struct anv_pipeline_bind_map *bind_map);
725
726 struct anv_device {
727 VK_LOADER_DATA _loader_data;
728
729 VkAllocationCallbacks alloc;
730
731 struct anv_instance * instance;
732 uint32_t chipset_id;
733 struct gen_device_info info;
734 struct isl_device isl_dev;
735 int context_id;
736 int fd;
737 bool can_chain_batches;
738 bool robust_buffer_access;
739
740 struct anv_bo_pool batch_bo_pool;
741
742 struct anv_bo_cache bo_cache;
743
744 struct anv_state_pool dynamic_state_pool;
745 struct anv_state_pool instruction_state_pool;
746 struct anv_state_pool surface_state_pool;
747
748 struct anv_bo workaround_bo;
749 struct anv_bo trivial_batch_bo;
750
751 struct anv_pipeline_cache blorp_shader_cache;
752 struct blorp_context blorp;
753
754 struct anv_state border_colors;
755
756 struct anv_queue queue;
757
758 struct anv_scratch_pool scratch_pool;
759
760 uint32_t default_mocs;
761
762 pthread_mutex_t mutex;
763 pthread_cond_t queue_submit;
764 bool lost;
765 };
766
767 static void inline
768 anv_state_flush(struct anv_device *device, struct anv_state state)
769 {
770 if (device->info.has_llc)
771 return;
772
773 gen_flush_range(state.map, state.alloc_size);
774 }
775
776 void anv_device_init_blorp(struct anv_device *device);
777 void anv_device_finish_blorp(struct anv_device *device);
778
779 VkResult anv_device_execbuf(struct anv_device *device,
780 struct drm_i915_gem_execbuffer2 *execbuf,
781 struct anv_bo **execbuf_bos);
782 VkResult anv_device_query_status(struct anv_device *device);
783 VkResult anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo);
784 VkResult anv_device_wait(struct anv_device *device, struct anv_bo *bo,
785 int64_t timeout);
786
787 void* anv_gem_mmap(struct anv_device *device,
788 uint32_t gem_handle, uint64_t offset, uint64_t size, uint32_t flags);
789 void anv_gem_munmap(void *p, uint64_t size);
790 uint32_t anv_gem_create(struct anv_device *device, uint64_t size);
791 void anv_gem_close(struct anv_device *device, uint32_t gem_handle);
792 uint32_t anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
793 int anv_gem_busy(struct anv_device *device, uint32_t gem_handle);
794 int anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns);
795 int anv_gem_execbuffer(struct anv_device *device,
796 struct drm_i915_gem_execbuffer2 *execbuf);
797 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
798 uint32_t stride, uint32_t tiling);
799 int anv_gem_create_context(struct anv_device *device);
800 int anv_gem_destroy_context(struct anv_device *device, int context);
801 int anv_gem_get_context_param(int fd, int context, uint32_t param,
802 uint64_t *value);
803 int anv_gem_get_param(int fd, uint32_t param);
804 bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling);
805 int anv_gem_get_aperture(int fd, uint64_t *size);
806 bool anv_gem_supports_48b_addresses(int fd);
807 int anv_gem_gpu_get_reset_stats(struct anv_device *device,
808 uint32_t *active, uint32_t *pending);
809 int anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle);
810 uint32_t anv_gem_fd_to_handle(struct anv_device *device, int fd);
811 int anv_gem_set_caching(struct anv_device *device, uint32_t gem_handle, uint32_t caching);
812 int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
813 uint32_t read_domains, uint32_t write_domain);
814 int anv_gem_sync_file_merge(struct anv_device *device, int fd1, int fd2);
815 uint32_t anv_gem_syncobj_create(struct anv_device *device);
816 void anv_gem_syncobj_destroy(struct anv_device *device, uint32_t handle);
817 int anv_gem_syncobj_handle_to_fd(struct anv_device *device, uint32_t handle);
818 uint32_t anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd);
819
820 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
821
822 struct anv_reloc_list {
823 uint32_t num_relocs;
824 uint32_t array_length;
825 struct drm_i915_gem_relocation_entry * relocs;
826 struct anv_bo ** reloc_bos;
827 };
828
829 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
830 const VkAllocationCallbacks *alloc);
831 void anv_reloc_list_finish(struct anv_reloc_list *list,
832 const VkAllocationCallbacks *alloc);
833
834 VkResult anv_reloc_list_add(struct anv_reloc_list *list,
835 const VkAllocationCallbacks *alloc,
836 uint32_t offset, struct anv_bo *target_bo,
837 uint32_t delta);
838
839 struct anv_batch_bo {
840 /* Link in the anv_cmd_buffer.owned_batch_bos list */
841 struct list_head link;
842
843 struct anv_bo bo;
844
845 /* Bytes actually consumed in this batch BO */
846 uint32_t length;
847
848 struct anv_reloc_list relocs;
849 };
850
851 struct anv_batch {
852 const VkAllocationCallbacks * alloc;
853
854 void * start;
855 void * end;
856 void * next;
857
858 struct anv_reloc_list * relocs;
859
860 /* This callback is called (with the associated user data) in the event
861 * that the batch runs out of space.
862 */
863 VkResult (*extend_cb)(struct anv_batch *, void *);
864 void * user_data;
865
866 /**
867 * Current error status of the command buffer. Used to track inconsistent
868 * or incomplete command buffer states that are the consequence of run-time
869 * errors such as out of memory scenarios. We want to track this in the
870 * batch because the command buffer object is not visible to some parts
871 * of the driver.
872 */
873 VkResult status;
874 };
875
876 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
877 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
878 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
879 void *location, struct anv_bo *bo, uint32_t offset);
880 VkResult anv_device_submit_simple_batch(struct anv_device *device,
881 struct anv_batch *batch);
882
883 static inline VkResult
884 anv_batch_set_error(struct anv_batch *batch, VkResult error)
885 {
886 assert(error != VK_SUCCESS);
887 if (batch->status == VK_SUCCESS)
888 batch->status = error;
889 return batch->status;
890 }
891
892 static inline bool
893 anv_batch_has_error(struct anv_batch *batch)
894 {
895 return batch->status != VK_SUCCESS;
896 }
897
898 struct anv_address {
899 struct anv_bo *bo;
900 uint32_t offset;
901 };
902
903 static inline uint64_t
904 _anv_combine_address(struct anv_batch *batch, void *location,
905 const struct anv_address address, uint32_t delta)
906 {
907 if (address.bo == NULL) {
908 return address.offset + delta;
909 } else {
910 assert(batch->start <= location && location < batch->end);
911
912 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
913 }
914 }
915
916 #define __gen_address_type struct anv_address
917 #define __gen_user_data struct anv_batch
918 #define __gen_combine_address _anv_combine_address
919
920 /* Wrapper macros needed to work around preprocessor argument issues. In
921 * particular, arguments don't get pre-evaluated if they are concatenated.
922 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
923 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
924 * We can work around this easily enough with these helpers.
925 */
926 #define __anv_cmd_length(cmd) cmd ## _length
927 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
928 #define __anv_cmd_header(cmd) cmd ## _header
929 #define __anv_cmd_pack(cmd) cmd ## _pack
930 #define __anv_reg_num(reg) reg ## _num
931
932 #define anv_pack_struct(dst, struc, ...) do { \
933 struct struc __template = { \
934 __VA_ARGS__ \
935 }; \
936 __anv_cmd_pack(struc)(NULL, dst, &__template); \
937 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dst, __anv_cmd_length(struc) * 4)); \
938 } while (0)
939
940 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
941 void *__dst = anv_batch_emit_dwords(batch, n); \
942 if (__dst) { \
943 struct cmd __template = { \
944 __anv_cmd_header(cmd), \
945 .DWordLength = n - __anv_cmd_length_bias(cmd), \
946 __VA_ARGS__ \
947 }; \
948 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
949 } \
950 __dst; \
951 })
952
953 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
954 do { \
955 uint32_t *dw; \
956 \
957 STATIC_ASSERT(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
958 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
959 if (!dw) \
960 break; \
961 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
962 dw[i] = (dwords0)[i] | (dwords1)[i]; \
963 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
964 } while (0)
965
966 #define anv_batch_emit(batch, cmd, name) \
967 for (struct cmd name = { __anv_cmd_header(cmd) }, \
968 *_dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
969 __builtin_expect(_dst != NULL, 1); \
970 ({ __anv_cmd_pack(cmd)(batch, _dst, &name); \
971 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, __anv_cmd_length(cmd) * 4)); \
972 _dst = NULL; \
973 }))
974
975 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
976 .GraphicsDataTypeGFDT = 0, \
977 .LLCCacheabilityControlLLCCC = 0, \
978 .L3CacheabilityControlL3CC = 1, \
979 }
980
981 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
982 .LLCeLLCCacheabilityControlLLCCC = 0, \
983 .L3CacheabilityControlL3CC = 1, \
984 }
985
986 #define GEN8_MOCS (struct GEN8_MEMORY_OBJECT_CONTROL_STATE) { \
987 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
988 .TargetCache = L3DefertoPATforLLCeLLCselection, \
989 .AgeforQUADLRU = 0 \
990 }
991
992 /* Skylake: MOCS is now an index into an array of 62 different caching
993 * configurations programmed by the kernel.
994 */
995
996 #define GEN9_MOCS (struct GEN9_MEMORY_OBJECT_CONTROL_STATE) { \
997 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
998 .IndextoMOCSTables = 2 \
999 }
1000
1001 #define GEN9_MOCS_PTE { \
1002 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1003 .IndextoMOCSTables = 1 \
1004 }
1005
1006 /* Cannonlake MOCS defines are duplicates of Skylake MOCS defines. */
1007 #define GEN10_MOCS (struct GEN10_MEMORY_OBJECT_CONTROL_STATE) { \
1008 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1009 .IndextoMOCSTables = 2 \
1010 }
1011
1012 #define GEN10_MOCS_PTE { \
1013 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
1014 .IndextoMOCSTables = 1 \
1015 }
1016
1017 struct anv_device_memory {
1018 struct anv_bo * bo;
1019 struct anv_memory_type * type;
1020 VkDeviceSize map_size;
1021 void * map;
1022 };
1023
1024 /**
1025 * Header for Vertex URB Entry (VUE)
1026 */
1027 struct anv_vue_header {
1028 uint32_t Reserved;
1029 uint32_t RTAIndex; /* RenderTargetArrayIndex */
1030 uint32_t ViewportIndex;
1031 float PointWidth;
1032 };
1033
1034 struct anv_descriptor_set_binding_layout {
1035 #ifndef NDEBUG
1036 /* The type of the descriptors in this binding */
1037 VkDescriptorType type;
1038 #endif
1039
1040 /* Number of array elements in this binding */
1041 uint16_t array_size;
1042
1043 /* Index into the flattend descriptor set */
1044 uint16_t descriptor_index;
1045
1046 /* Index into the dynamic state array for a dynamic buffer */
1047 int16_t dynamic_offset_index;
1048
1049 /* Index into the descriptor set buffer views */
1050 int16_t buffer_index;
1051
1052 struct {
1053 /* Index into the binding table for the associated surface */
1054 int16_t surface_index;
1055
1056 /* Index into the sampler table for the associated sampler */
1057 int16_t sampler_index;
1058
1059 /* Index into the image table for the associated image */
1060 int16_t image_index;
1061 } stage[MESA_SHADER_STAGES];
1062
1063 /* Immutable samplers (or NULL if no immutable samplers) */
1064 struct anv_sampler **immutable_samplers;
1065 };
1066
1067 struct anv_descriptor_set_layout {
1068 /* Number of bindings in this descriptor set */
1069 uint16_t binding_count;
1070
1071 /* Total size of the descriptor set with room for all array entries */
1072 uint16_t size;
1073
1074 /* Shader stages affected by this descriptor set */
1075 uint16_t shader_stages;
1076
1077 /* Number of buffers in this descriptor set */
1078 uint16_t buffer_count;
1079
1080 /* Number of dynamic offsets used by this descriptor set */
1081 uint16_t dynamic_offset_count;
1082
1083 /* Bindings in this descriptor set */
1084 struct anv_descriptor_set_binding_layout binding[0];
1085 };
1086
1087 struct anv_descriptor {
1088 VkDescriptorType type;
1089
1090 union {
1091 struct {
1092 VkImageLayout layout;
1093 struct anv_image_view *image_view;
1094 struct anv_sampler *sampler;
1095 };
1096
1097 struct {
1098 struct anv_buffer *buffer;
1099 uint64_t offset;
1100 uint64_t range;
1101 };
1102
1103 struct anv_buffer_view *buffer_view;
1104 };
1105 };
1106
1107 struct anv_descriptor_set {
1108 const struct anv_descriptor_set_layout *layout;
1109 uint32_t size;
1110 uint32_t buffer_count;
1111 struct anv_buffer_view *buffer_views;
1112 struct anv_descriptor descriptors[0];
1113 };
1114
1115 struct anv_buffer_view {
1116 enum isl_format format; /**< VkBufferViewCreateInfo::format */
1117 struct anv_bo *bo;
1118 uint32_t offset; /**< Offset into bo. */
1119 uint64_t range; /**< VkBufferViewCreateInfo::range */
1120
1121 struct anv_state surface_state;
1122 struct anv_state storage_surface_state;
1123 struct anv_state writeonly_storage_surface_state;
1124
1125 struct brw_image_param storage_image_param;
1126 };
1127
1128 struct anv_push_descriptor_set {
1129 struct anv_descriptor_set set;
1130
1131 /* Put this field right behind anv_descriptor_set so it fills up the
1132 * descriptors[0] field. */
1133 struct anv_descriptor descriptors[MAX_PUSH_DESCRIPTORS];
1134
1135 struct anv_buffer_view buffer_views[MAX_PUSH_DESCRIPTORS];
1136 };
1137
1138 struct anv_descriptor_pool {
1139 uint32_t size;
1140 uint32_t next;
1141 uint32_t free_list;
1142
1143 struct anv_state_stream surface_state_stream;
1144 void *surface_state_free_list;
1145
1146 char data[0];
1147 };
1148
1149 enum anv_descriptor_template_entry_type {
1150 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_IMAGE,
1151 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER,
1152 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER_VIEW
1153 };
1154
1155 struct anv_descriptor_template_entry {
1156 /* The type of descriptor in this entry */
1157 VkDescriptorType type;
1158
1159 /* Binding in the descriptor set */
1160 uint32_t binding;
1161
1162 /* Offset at which to write into the descriptor set binding */
1163 uint32_t array_element;
1164
1165 /* Number of elements to write into the descriptor set binding */
1166 uint32_t array_count;
1167
1168 /* Offset into the user provided data */
1169 size_t offset;
1170
1171 /* Stride between elements into the user provided data */
1172 size_t stride;
1173 };
1174
1175 struct anv_descriptor_update_template {
1176 /* The descriptor set this template corresponds to. This value is only
1177 * valid if the template was created with the templateType
1178 * VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR.
1179 */
1180 uint8_t set;
1181
1182 /* Number of entries in this template */
1183 uint32_t entry_count;
1184
1185 /* Entries of the template */
1186 struct anv_descriptor_template_entry entries[0];
1187 };
1188
1189 size_t
1190 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout);
1191
1192 void
1193 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
1194 const struct gen_device_info * const devinfo,
1195 const VkDescriptorImageInfo * const info,
1196 VkDescriptorType type,
1197 uint32_t binding,
1198 uint32_t element);
1199
1200 void
1201 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
1202 VkDescriptorType type,
1203 struct anv_buffer_view *buffer_view,
1204 uint32_t binding,
1205 uint32_t element);
1206
1207 void
1208 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
1209 struct anv_device *device,
1210 struct anv_state_stream *alloc_stream,
1211 VkDescriptorType type,
1212 struct anv_buffer *buffer,
1213 uint32_t binding,
1214 uint32_t element,
1215 VkDeviceSize offset,
1216 VkDeviceSize range);
1217
1218 void
1219 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
1220 struct anv_device *device,
1221 struct anv_state_stream *alloc_stream,
1222 const struct anv_descriptor_update_template *template,
1223 const void *data);
1224
1225 VkResult
1226 anv_descriptor_set_create(struct anv_device *device,
1227 struct anv_descriptor_pool *pool,
1228 const struct anv_descriptor_set_layout *layout,
1229 struct anv_descriptor_set **out_set);
1230
1231 void
1232 anv_descriptor_set_destroy(struct anv_device *device,
1233 struct anv_descriptor_pool *pool,
1234 struct anv_descriptor_set *set);
1235
1236 #define ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS UINT8_MAX
1237
1238 struct anv_pipeline_binding {
1239 /* The descriptor set this surface corresponds to. The special value of
1240 * ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS indicates that the offset refers
1241 * to a color attachment and not a regular descriptor.
1242 */
1243 uint8_t set;
1244
1245 /* Binding in the descriptor set */
1246 uint8_t binding;
1247
1248 /* Index in the binding */
1249 uint8_t index;
1250
1251 /* Input attachment index (relative to the subpass) */
1252 uint8_t input_attachment_index;
1253
1254 /* For a storage image, whether it is write-only */
1255 bool write_only;
1256 };
1257
1258 struct anv_pipeline_layout {
1259 struct {
1260 struct anv_descriptor_set_layout *layout;
1261 uint32_t dynamic_offset_start;
1262 } set[MAX_SETS];
1263
1264 uint32_t num_sets;
1265
1266 struct {
1267 bool has_dynamic_offsets;
1268 } stage[MESA_SHADER_STAGES];
1269
1270 unsigned char sha1[20];
1271 };
1272
1273 struct anv_buffer {
1274 struct anv_device * device;
1275 VkDeviceSize size;
1276
1277 VkBufferUsageFlags usage;
1278
1279 /* Set when bound */
1280 struct anv_bo * bo;
1281 VkDeviceSize offset;
1282 };
1283
1284 static inline uint64_t
1285 anv_buffer_get_range(struct anv_buffer *buffer, uint64_t offset, uint64_t range)
1286 {
1287 assert(offset <= buffer->size);
1288 if (range == VK_WHOLE_SIZE) {
1289 return buffer->size - offset;
1290 } else {
1291 assert(range <= buffer->size);
1292 return range;
1293 }
1294 }
1295
1296 enum anv_cmd_dirty_bits {
1297 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
1298 ANV_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
1299 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
1300 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
1301 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
1302 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
1303 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
1304 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
1305 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
1306 ANV_CMD_DIRTY_DYNAMIC_ALL = (1 << 9) - 1,
1307 ANV_CMD_DIRTY_PIPELINE = 1 << 9,
1308 ANV_CMD_DIRTY_INDEX_BUFFER = 1 << 10,
1309 ANV_CMD_DIRTY_RENDER_TARGETS = 1 << 11,
1310 };
1311 typedef uint32_t anv_cmd_dirty_mask_t;
1312
1313 enum anv_pipe_bits {
1314 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT = (1 << 0),
1315 ANV_PIPE_STALL_AT_SCOREBOARD_BIT = (1 << 1),
1316 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT = (1 << 2),
1317 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT = (1 << 3),
1318 ANV_PIPE_VF_CACHE_INVALIDATE_BIT = (1 << 4),
1319 ANV_PIPE_DATA_CACHE_FLUSH_BIT = (1 << 5),
1320 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT = (1 << 10),
1321 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT = (1 << 11),
1322 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT = (1 << 12),
1323 ANV_PIPE_DEPTH_STALL_BIT = (1 << 13),
1324 ANV_PIPE_CS_STALL_BIT = (1 << 20),
1325
1326 /* This bit does not exist directly in PIPE_CONTROL. Instead it means that
1327 * a flush has happened but not a CS stall. The next time we do any sort
1328 * of invalidation we need to insert a CS stall at that time. Otherwise,
1329 * we would have to CS stall on every flush which could be bad.
1330 */
1331 ANV_PIPE_NEEDS_CS_STALL_BIT = (1 << 21),
1332 };
1333
1334 #define ANV_PIPE_FLUSH_BITS ( \
1335 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT | \
1336 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1337 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1338
1339 #define ANV_PIPE_STALL_BITS ( \
1340 ANV_PIPE_STALL_AT_SCOREBOARD_BIT | \
1341 ANV_PIPE_DEPTH_STALL_BIT | \
1342 ANV_PIPE_CS_STALL_BIT)
1343
1344 #define ANV_PIPE_INVALIDATE_BITS ( \
1345 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT | \
1346 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT | \
1347 ANV_PIPE_VF_CACHE_INVALIDATE_BIT | \
1348 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1349 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
1350 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
1351
1352 static inline enum anv_pipe_bits
1353 anv_pipe_flush_bits_for_access_flags(VkAccessFlags flags)
1354 {
1355 enum anv_pipe_bits pipe_bits = 0;
1356
1357 unsigned b;
1358 for_each_bit(b, flags) {
1359 switch ((VkAccessFlagBits)(1 << b)) {
1360 case VK_ACCESS_SHADER_WRITE_BIT:
1361 pipe_bits |= ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1362 break;
1363 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT:
1364 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1365 break;
1366 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT:
1367 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1368 break;
1369 case VK_ACCESS_TRANSFER_WRITE_BIT:
1370 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1371 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1372 break;
1373 default:
1374 break; /* Nothing to do */
1375 }
1376 }
1377
1378 return pipe_bits;
1379 }
1380
1381 static inline enum anv_pipe_bits
1382 anv_pipe_invalidate_bits_for_access_flags(VkAccessFlags flags)
1383 {
1384 enum anv_pipe_bits pipe_bits = 0;
1385
1386 unsigned b;
1387 for_each_bit(b, flags) {
1388 switch ((VkAccessFlagBits)(1 << b)) {
1389 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT:
1390 case VK_ACCESS_INDEX_READ_BIT:
1391 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT:
1392 pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1393 break;
1394 case VK_ACCESS_UNIFORM_READ_BIT:
1395 pipe_bits |= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1396 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1397 break;
1398 case VK_ACCESS_SHADER_READ_BIT:
1399 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT:
1400 case VK_ACCESS_TRANSFER_READ_BIT:
1401 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1402 break;
1403 default:
1404 break; /* Nothing to do */
1405 }
1406 }
1407
1408 return pipe_bits;
1409 }
1410
1411 struct anv_vertex_binding {
1412 struct anv_buffer * buffer;
1413 VkDeviceSize offset;
1414 };
1415
1416 struct anv_push_constants {
1417 /* Current allocated size of this push constants data structure.
1418 * Because a decent chunk of it may not be used (images on SKL, for
1419 * instance), we won't actually allocate the entire structure up-front.
1420 */
1421 uint32_t size;
1422
1423 /* Push constant data provided by the client through vkPushConstants */
1424 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
1425
1426 /* Our hardware only provides zero-based vertex and instance id so, in
1427 * order to satisfy the vulkan requirements, we may have to push one or
1428 * both of these into the shader.
1429 */
1430 uint32_t base_vertex;
1431 uint32_t base_instance;
1432
1433 /* Image data for image_load_store on pre-SKL */
1434 struct brw_image_param images[MAX_IMAGES];
1435 };
1436
1437 struct anv_dynamic_state {
1438 struct {
1439 uint32_t count;
1440 VkViewport viewports[MAX_VIEWPORTS];
1441 } viewport;
1442
1443 struct {
1444 uint32_t count;
1445 VkRect2D scissors[MAX_SCISSORS];
1446 } scissor;
1447
1448 float line_width;
1449
1450 struct {
1451 float bias;
1452 float clamp;
1453 float slope;
1454 } depth_bias;
1455
1456 float blend_constants[4];
1457
1458 struct {
1459 float min;
1460 float max;
1461 } depth_bounds;
1462
1463 struct {
1464 uint32_t front;
1465 uint32_t back;
1466 } stencil_compare_mask;
1467
1468 struct {
1469 uint32_t front;
1470 uint32_t back;
1471 } stencil_write_mask;
1472
1473 struct {
1474 uint32_t front;
1475 uint32_t back;
1476 } stencil_reference;
1477 };
1478
1479 extern const struct anv_dynamic_state default_dynamic_state;
1480
1481 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
1482 const struct anv_dynamic_state *src,
1483 uint32_t copy_mask);
1484
1485 /**
1486 * Attachment state when recording a renderpass instance.
1487 *
1488 * The clear value is valid only if there exists a pending clear.
1489 */
1490 struct anv_attachment_state {
1491 enum isl_aux_usage aux_usage;
1492 enum isl_aux_usage input_aux_usage;
1493 struct anv_state color_rt_state;
1494 struct anv_state input_att_state;
1495
1496 VkImageLayout current_layout;
1497 VkImageAspectFlags pending_clear_aspects;
1498 bool fast_clear;
1499 VkClearValue clear_value;
1500 bool clear_color_is_zero_one;
1501 bool clear_color_is_zero;
1502 };
1503
1504 /** State required while building cmd buffer */
1505 struct anv_cmd_state {
1506 /* PIPELINE_SELECT.PipelineSelection */
1507 uint32_t current_pipeline;
1508 const struct gen_l3_config * current_l3_config;
1509 uint32_t vb_dirty;
1510 anv_cmd_dirty_mask_t dirty;
1511 anv_cmd_dirty_mask_t compute_dirty;
1512 enum anv_pipe_bits pending_pipe_bits;
1513 uint32_t num_workgroups_offset;
1514 struct anv_bo *num_workgroups_bo;
1515 VkShaderStageFlags descriptors_dirty;
1516 VkShaderStageFlags push_constants_dirty;
1517 uint32_t scratch_size;
1518 struct anv_pipeline * pipeline;
1519 struct anv_pipeline * compute_pipeline;
1520 struct anv_framebuffer * framebuffer;
1521 struct anv_render_pass * pass;
1522 struct anv_subpass * subpass;
1523 VkRect2D render_area;
1524 uint32_t restart_index;
1525 struct anv_vertex_binding vertex_bindings[MAX_VBS];
1526 struct anv_descriptor_set * descriptors[MAX_SETS];
1527 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
1528 VkShaderStageFlags push_constant_stages;
1529 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
1530 struct anv_state binding_tables[MESA_SHADER_STAGES];
1531 struct anv_state samplers[MESA_SHADER_STAGES];
1532 struct anv_dynamic_state dynamic;
1533 bool need_query_wa;
1534
1535 struct anv_push_descriptor_set push_descriptor;
1536
1537 /**
1538 * Whether or not the gen8 PMA fix is enabled. We ensure that, at the top
1539 * of any command buffer it is disabled by disabling it in EndCommandBuffer
1540 * and before invoking the secondary in ExecuteCommands.
1541 */
1542 bool pma_fix_enabled;
1543
1544 /**
1545 * Whether or not we know for certain that HiZ is enabled for the current
1546 * subpass. If, for whatever reason, we are unsure as to whether HiZ is
1547 * enabled or not, this will be false.
1548 */
1549 bool hiz_enabled;
1550
1551 /**
1552 * Array length is anv_cmd_state::pass::attachment_count. Array content is
1553 * valid only when recording a render pass instance.
1554 */
1555 struct anv_attachment_state * attachments;
1556
1557 /**
1558 * Surface states for color render targets. These are stored in a single
1559 * flat array. For depth-stencil attachments, the surface state is simply
1560 * left blank.
1561 */
1562 struct anv_state render_pass_states;
1563
1564 /**
1565 * A null surface state of the right size to match the framebuffer. This
1566 * is one of the states in render_pass_states.
1567 */
1568 struct anv_state null_surface_state;
1569
1570 struct {
1571 struct anv_buffer * index_buffer;
1572 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1573 uint32_t index_offset;
1574 } gen7;
1575 };
1576
1577 struct anv_cmd_pool {
1578 VkAllocationCallbacks alloc;
1579 struct list_head cmd_buffers;
1580 };
1581
1582 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1583
1584 enum anv_cmd_buffer_exec_mode {
1585 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
1586 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
1587 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT,
1588 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
1589 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
1590 };
1591
1592 struct anv_cmd_buffer {
1593 VK_LOADER_DATA _loader_data;
1594
1595 struct anv_device * device;
1596
1597 struct anv_cmd_pool * pool;
1598 struct list_head pool_link;
1599
1600 struct anv_batch batch;
1601
1602 /* Fields required for the actual chain of anv_batch_bo's.
1603 *
1604 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1605 */
1606 struct list_head batch_bos;
1607 enum anv_cmd_buffer_exec_mode exec_mode;
1608
1609 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1610 * referenced by this command buffer
1611 *
1612 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1613 */
1614 struct u_vector seen_bbos;
1615
1616 /* A vector of int32_t's for every block of binding tables.
1617 *
1618 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1619 */
1620 struct u_vector bt_block_states;
1621 uint32_t bt_next;
1622
1623 struct anv_reloc_list surface_relocs;
1624 /** Last seen surface state block pool center bo offset */
1625 uint32_t last_ss_pool_center;
1626
1627 /* Serial for tracking buffer completion */
1628 uint32_t serial;
1629
1630 /* Stream objects for storing temporary data */
1631 struct anv_state_stream surface_state_stream;
1632 struct anv_state_stream dynamic_state_stream;
1633
1634 VkCommandBufferUsageFlags usage_flags;
1635 VkCommandBufferLevel level;
1636
1637 struct anv_cmd_state state;
1638 };
1639
1640 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1641 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1642 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1643 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
1644 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
1645 struct anv_cmd_buffer *secondary);
1646 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
1647 VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
1648 struct anv_cmd_buffer *cmd_buffer,
1649 const VkSemaphore *in_semaphores,
1650 uint32_t num_in_semaphores,
1651 const VkSemaphore *out_semaphores,
1652 uint32_t num_out_semaphores);
1653
1654 VkResult anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer);
1655
1656 VkResult
1657 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
1658 gl_shader_stage stage, uint32_t size);
1659 #define anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, field) \
1660 anv_cmd_buffer_ensure_push_constants_size(cmd_buffer, stage, \
1661 (offsetof(struct anv_push_constants, field) + \
1662 sizeof(cmd_buffer->state.push_constants[0]->field)))
1663
1664 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
1665 const void *data, uint32_t size, uint32_t alignment);
1666 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
1667 uint32_t *a, uint32_t *b,
1668 uint32_t dwords, uint32_t alignment);
1669
1670 struct anv_address
1671 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1672 struct anv_state
1673 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1674 uint32_t entries, uint32_t *state_offset);
1675 struct anv_state
1676 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1677 struct anv_state
1678 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1679 uint32_t size, uint32_t alignment);
1680
1681 VkResult
1682 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1683
1684 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1685 void gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer *cmd_buffer,
1686 bool depth_clamp_enable);
1687 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1688
1689 void anv_cmd_buffer_setup_attachments(struct anv_cmd_buffer *cmd_buffer,
1690 struct anv_render_pass *pass,
1691 struct anv_framebuffer *framebuffer,
1692 const VkClearValue *clear_values);
1693
1694 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1695
1696 struct anv_state
1697 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1698 gl_shader_stage stage);
1699 struct anv_state
1700 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer);
1701
1702 void anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer *cmd_buffer);
1703 void anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer);
1704
1705 const struct anv_image_view *
1706 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1707
1708 VkResult
1709 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
1710 uint32_t num_entries,
1711 uint32_t *state_offset,
1712 struct anv_state *bt_state);
1713
1714 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1715
1716 enum anv_fence_state {
1717 /** Indicates that this is a new (or newly reset fence) */
1718 ANV_FENCE_STATE_RESET,
1719
1720 /** Indicates that this fence has been submitted to the GPU but is still
1721 * (as far as we know) in use by the GPU.
1722 */
1723 ANV_FENCE_STATE_SUBMITTED,
1724
1725 ANV_FENCE_STATE_SIGNALED,
1726 };
1727
1728 struct anv_fence {
1729 struct anv_bo bo;
1730 struct drm_i915_gem_execbuffer2 execbuf;
1731 struct drm_i915_gem_exec_object2 exec2_objects[1];
1732 enum anv_fence_state state;
1733 };
1734
1735 struct anv_event {
1736 uint64_t semaphore;
1737 struct anv_state state;
1738 };
1739
1740 enum anv_semaphore_type {
1741 ANV_SEMAPHORE_TYPE_NONE = 0,
1742 ANV_SEMAPHORE_TYPE_DUMMY,
1743 ANV_SEMAPHORE_TYPE_BO,
1744 ANV_SEMAPHORE_TYPE_SYNC_FILE,
1745 };
1746
1747 struct anv_semaphore_impl {
1748 enum anv_semaphore_type type;
1749
1750 union {
1751 /* A BO representing this semaphore when type == ANV_SEMAPHORE_TYPE_BO.
1752 * This BO will be added to the object list on any execbuf2 calls for
1753 * which this semaphore is used as a wait or signal fence. When used as
1754 * a signal fence, the EXEC_OBJECT_WRITE flag will be set.
1755 */
1756 struct anv_bo *bo;
1757
1758 /* The sync file descriptor when type == AKV_SEMAPHORE_TYPE_SYNC_FILE.
1759 * If the semaphore is in the unsignaled state due to either just being
1760 * created or because it has been used for a wait, fd will be -1.
1761 */
1762 int fd;
1763 };
1764 };
1765
1766 struct anv_semaphore {
1767 /* Permanent semaphore state. Every semaphore has some form of permanent
1768 * state (type != ANV_SEMAPHORE_TYPE_NONE). This may be a BO to fence on
1769 * (for cross-process semaphores0 or it could just be a dummy for use
1770 * internally.
1771 */
1772 struct anv_semaphore_impl permanent;
1773
1774 /* Temporary semaphore state. A semaphore *may* have temporary state.
1775 * That state is added to the semaphore by an import operation and is reset
1776 * back to ANV_SEMAPHORE_TYPE_NONE when the semaphore is waited on. A
1777 * semaphore with temporary state cannot be signaled because the semaphore
1778 * must already be signaled before the temporary state can be exported from
1779 * the semaphore in the other process and imported here.
1780 */
1781 struct anv_semaphore_impl temporary;
1782 };
1783
1784 void anv_semaphore_reset_temporary(struct anv_device *device,
1785 struct anv_semaphore *semaphore);
1786
1787 struct anv_shader_module {
1788 unsigned char sha1[20];
1789 uint32_t size;
1790 char data[0];
1791 };
1792
1793 static inline gl_shader_stage
1794 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1795 {
1796 assert(__builtin_popcount(vk_stage) == 1);
1797 return ffs(vk_stage) - 1;
1798 }
1799
1800 static inline VkShaderStageFlagBits
1801 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1802 {
1803 return (1 << mesa_stage);
1804 }
1805
1806 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1807
1808 #define anv_foreach_stage(stage, stage_bits) \
1809 for (gl_shader_stage stage, \
1810 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
1811 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1812 __tmp &= ~(1 << (stage)))
1813
1814 struct anv_pipeline_bind_map {
1815 uint32_t surface_count;
1816 uint32_t sampler_count;
1817 uint32_t image_count;
1818
1819 struct anv_pipeline_binding * surface_to_descriptor;
1820 struct anv_pipeline_binding * sampler_to_descriptor;
1821 };
1822
1823 struct anv_shader_bin_key {
1824 uint32_t size;
1825 uint8_t data[0];
1826 };
1827
1828 struct anv_shader_bin {
1829 uint32_t ref_cnt;
1830
1831 const struct anv_shader_bin_key *key;
1832
1833 struct anv_state kernel;
1834 uint32_t kernel_size;
1835
1836 const struct brw_stage_prog_data *prog_data;
1837 uint32_t prog_data_size;
1838
1839 struct anv_pipeline_bind_map bind_map;
1840
1841 /* Prog data follows, then params, then the key, all aligned to 8-bytes */
1842 };
1843
1844 struct anv_shader_bin *
1845 anv_shader_bin_create(struct anv_device *device,
1846 const void *key, uint32_t key_size,
1847 const void *kernel, uint32_t kernel_size,
1848 const struct brw_stage_prog_data *prog_data,
1849 uint32_t prog_data_size, const void *prog_data_param,
1850 const struct anv_pipeline_bind_map *bind_map);
1851
1852 void
1853 anv_shader_bin_destroy(struct anv_device *device, struct anv_shader_bin *shader);
1854
1855 static inline void
1856 anv_shader_bin_ref(struct anv_shader_bin *shader)
1857 {
1858 assert(shader && shader->ref_cnt >= 1);
1859 p_atomic_inc(&shader->ref_cnt);
1860 }
1861
1862 static inline void
1863 anv_shader_bin_unref(struct anv_device *device, struct anv_shader_bin *shader)
1864 {
1865 assert(shader && shader->ref_cnt >= 1);
1866 if (p_atomic_dec_zero(&shader->ref_cnt))
1867 anv_shader_bin_destroy(device, shader);
1868 }
1869
1870 struct anv_pipeline {
1871 struct anv_device * device;
1872 struct anv_batch batch;
1873 uint32_t batch_data[512];
1874 struct anv_reloc_list batch_relocs;
1875 uint32_t dynamic_state_mask;
1876 struct anv_dynamic_state dynamic_state;
1877
1878 struct anv_subpass * subpass;
1879 struct anv_pipeline_layout * layout;
1880
1881 bool needs_data_cache;
1882
1883 struct anv_shader_bin * shaders[MESA_SHADER_STAGES];
1884
1885 struct {
1886 const struct gen_l3_config * l3_config;
1887 uint32_t total_size;
1888 } urb;
1889
1890 VkShaderStageFlags active_stages;
1891 struct anv_state blend_state;
1892
1893 uint32_t vb_used;
1894 uint32_t binding_stride[MAX_VBS];
1895 bool instancing_enable[MAX_VBS];
1896 bool primitive_restart;
1897 uint32_t topology;
1898
1899 uint32_t cs_right_mask;
1900
1901 bool writes_depth;
1902 bool depth_test_enable;
1903 bool writes_stencil;
1904 bool stencil_test_enable;
1905 bool depth_clamp_enable;
1906 bool sample_shading_enable;
1907 bool kill_pixel;
1908
1909 struct {
1910 uint32_t sf[7];
1911 uint32_t depth_stencil_state[3];
1912 } gen7;
1913
1914 struct {
1915 uint32_t sf[4];
1916 uint32_t raster[5];
1917 uint32_t wm_depth_stencil[3];
1918 } gen8;
1919
1920 struct {
1921 uint32_t wm_depth_stencil[4];
1922 } gen9;
1923
1924 uint32_t interface_descriptor_data[8];
1925 };
1926
1927 static inline bool
1928 anv_pipeline_has_stage(const struct anv_pipeline *pipeline,
1929 gl_shader_stage stage)
1930 {
1931 return (pipeline->active_stages & mesa_to_vk_shader_stage(stage)) != 0;
1932 }
1933
1934 #define ANV_DECL_GET_PROG_DATA_FUNC(prefix, stage) \
1935 static inline const struct brw_##prefix##_prog_data * \
1936 get_##prefix##_prog_data(const struct anv_pipeline *pipeline) \
1937 { \
1938 if (anv_pipeline_has_stage(pipeline, stage)) { \
1939 return (const struct brw_##prefix##_prog_data *) \
1940 pipeline->shaders[stage]->prog_data; \
1941 } else { \
1942 return NULL; \
1943 } \
1944 }
1945
1946 ANV_DECL_GET_PROG_DATA_FUNC(vs, MESA_SHADER_VERTEX)
1947 ANV_DECL_GET_PROG_DATA_FUNC(tcs, MESA_SHADER_TESS_CTRL)
1948 ANV_DECL_GET_PROG_DATA_FUNC(tes, MESA_SHADER_TESS_EVAL)
1949 ANV_DECL_GET_PROG_DATA_FUNC(gs, MESA_SHADER_GEOMETRY)
1950 ANV_DECL_GET_PROG_DATA_FUNC(wm, MESA_SHADER_FRAGMENT)
1951 ANV_DECL_GET_PROG_DATA_FUNC(cs, MESA_SHADER_COMPUTE)
1952
1953 static inline const struct brw_vue_prog_data *
1954 anv_pipeline_get_last_vue_prog_data(const struct anv_pipeline *pipeline)
1955 {
1956 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
1957 return &get_gs_prog_data(pipeline)->base;
1958 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1959 return &get_tes_prog_data(pipeline)->base;
1960 else
1961 return &get_vs_prog_data(pipeline)->base;
1962 }
1963
1964 VkResult
1965 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1966 struct anv_pipeline_cache *cache,
1967 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1968 const VkAllocationCallbacks *alloc);
1969
1970 VkResult
1971 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1972 struct anv_pipeline_cache *cache,
1973 const VkComputePipelineCreateInfo *info,
1974 struct anv_shader_module *module,
1975 const char *entrypoint,
1976 const VkSpecializationInfo *spec_info);
1977
1978 struct anv_format {
1979 enum isl_format isl_format:16;
1980 struct isl_swizzle swizzle;
1981 };
1982
1983 struct anv_format
1984 anv_get_format(const struct gen_device_info *devinfo, VkFormat format,
1985 VkImageAspectFlags aspect, VkImageTiling tiling);
1986
1987 static inline enum isl_format
1988 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
1989 VkImageAspectFlags aspect, VkImageTiling tiling)
1990 {
1991 return anv_get_format(devinfo, vk_format, aspect, tiling).isl_format;
1992 }
1993
1994 static inline struct isl_swizzle
1995 anv_swizzle_for_render(struct isl_swizzle swizzle)
1996 {
1997 /* Sometimes the swizzle will have alpha map to one. We do this to fake
1998 * RGB as RGBA for texturing
1999 */
2000 assert(swizzle.a == ISL_CHANNEL_SELECT_ONE ||
2001 swizzle.a == ISL_CHANNEL_SELECT_ALPHA);
2002
2003 /* But it doesn't matter what we render to that channel */
2004 swizzle.a = ISL_CHANNEL_SELECT_ALPHA;
2005
2006 return swizzle;
2007 }
2008
2009 void
2010 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm);
2011
2012 /**
2013 * Subsurface of an anv_image.
2014 */
2015 struct anv_surface {
2016 /** Valid only if isl_surf::size > 0. */
2017 struct isl_surf isl;
2018
2019 /**
2020 * Offset from VkImage's base address, as bound by vkBindImageMemory().
2021 */
2022 uint32_t offset;
2023 };
2024
2025 struct anv_image {
2026 VkImageType type;
2027 /* The original VkFormat provided by the client. This may not match any
2028 * of the actual surface formats.
2029 */
2030 VkFormat vk_format;
2031 VkImageAspectFlags aspects;
2032 VkExtent3D extent;
2033 uint32_t levels;
2034 uint32_t array_size;
2035 uint32_t samples; /**< VkImageCreateInfo::samples */
2036 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
2037 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
2038
2039 VkDeviceSize size;
2040 uint32_t alignment;
2041
2042 /* Set when bound */
2043 struct anv_bo *bo;
2044 VkDeviceSize offset;
2045
2046 /**
2047 * Image subsurfaces
2048 *
2049 * For each foo, anv_image::foo_surface is valid if and only if
2050 * anv_image::aspects has a foo aspect.
2051 *
2052 * The hardware requires that the depth buffer and stencil buffer be
2053 * separate surfaces. From Vulkan's perspective, though, depth and stencil
2054 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
2055 * allocate the depth and stencil buffers as separate surfaces in the same
2056 * bo.
2057 */
2058 union {
2059 struct anv_surface color_surface;
2060
2061 struct {
2062 struct anv_surface depth_surface;
2063 struct anv_surface stencil_surface;
2064 };
2065 };
2066
2067 /**
2068 * For color images, this is the aux usage for this image when not used as a
2069 * color attachment.
2070 *
2071 * For depth/stencil images, this is set to ISL_AUX_USAGE_HIZ if the image
2072 * has a HiZ buffer.
2073 */
2074 enum isl_aux_usage aux_usage;
2075
2076 struct anv_surface aux_surface;
2077 };
2078
2079 /* Returns the number of auxiliary buffer levels attached to an image. */
2080 static inline uint8_t
2081 anv_image_aux_levels(const struct anv_image * const image)
2082 {
2083 assert(image);
2084 return image->aux_surface.isl.size > 0 ? image->aux_surface.isl.levels : 0;
2085 }
2086
2087 /* Returns the number of auxiliary buffer layers attached to an image. */
2088 static inline uint32_t
2089 anv_image_aux_layers(const struct anv_image * const image,
2090 const uint8_t miplevel)
2091 {
2092 assert(image);
2093
2094 /* The miplevel must exist in the main buffer. */
2095 assert(miplevel < image->levels);
2096
2097 if (miplevel >= anv_image_aux_levels(image)) {
2098 /* There are no layers with auxiliary data because the miplevel has no
2099 * auxiliary data.
2100 */
2101 return 0;
2102 } else {
2103 return MAX2(image->aux_surface.isl.logical_level0_px.array_len,
2104 image->aux_surface.isl.logical_level0_px.depth >> miplevel);
2105 }
2106 }
2107
2108 static inline unsigned
2109 anv_fast_clear_state_entry_size(const struct anv_device *device)
2110 {
2111 assert(device);
2112 /* Entry contents:
2113 * +--------------------------------------------+
2114 * | clear value dword(s) | needs resolve dword |
2115 * +--------------------------------------------+
2116 */
2117
2118 /* Ensure that the needs resolve dword is in fact dword-aligned to enable
2119 * GPU memcpy operations.
2120 */
2121 assert(device->isl_dev.ss.clear_value_size % 4 == 0);
2122 return device->isl_dev.ss.clear_value_size + 4;
2123 }
2124
2125 /* Returns true if a HiZ-enabled depth buffer can be sampled from. */
2126 static inline bool
2127 anv_can_sample_with_hiz(const struct gen_device_info * const devinfo,
2128 const VkImageAspectFlags aspect_mask,
2129 const uint32_t samples)
2130 {
2131 /* Validate the inputs. */
2132 assert(devinfo && aspect_mask && samples);
2133 return devinfo->gen >= 8 && (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) &&
2134 samples == 1;
2135 }
2136
2137 void
2138 anv_gen8_hiz_op_resolve(struct anv_cmd_buffer *cmd_buffer,
2139 const struct anv_image *image,
2140 enum blorp_hiz_op op);
2141 void
2142 anv_ccs_resolve(struct anv_cmd_buffer * const cmd_buffer,
2143 const struct anv_state surface_state,
2144 const struct anv_image * const image,
2145 const uint8_t level, const uint32_t layer_count,
2146 const enum blorp_fast_clear_op op);
2147
2148 void
2149 anv_image_fast_clear(struct anv_cmd_buffer *cmd_buffer,
2150 const struct anv_image *image,
2151 const uint32_t base_level, const uint32_t level_count,
2152 const uint32_t base_layer, uint32_t layer_count);
2153
2154 enum isl_aux_usage
2155 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
2156 const struct anv_image *image,
2157 const VkImageAspectFlags aspects,
2158 const VkImageLayout layout);
2159
2160 /* This is defined as a macro so that it works for both
2161 * VkImageSubresourceRange and VkImageSubresourceLayers
2162 */
2163 #define anv_get_layerCount(_image, _range) \
2164 ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
2165 (_image)->array_size - (_range)->baseArrayLayer : (_range)->layerCount)
2166
2167 static inline uint32_t
2168 anv_get_levelCount(const struct anv_image *image,
2169 const VkImageSubresourceRange *range)
2170 {
2171 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
2172 image->levels - range->baseMipLevel : range->levelCount;
2173 }
2174
2175
2176 struct anv_image_view {
2177 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
2178 struct anv_bo *bo;
2179 uint32_t offset; /**< Offset into bo. */
2180
2181 struct isl_view isl;
2182
2183 VkImageAspectFlags aspect_mask;
2184 VkFormat vk_format;
2185 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
2186
2187 /**
2188 * RENDER_SURFACE_STATE when using image as a sampler surface with an image
2189 * layout of SHADER_READ_ONLY_OPTIMAL or DEPTH_STENCIL_READ_ONLY_OPTIMAL.
2190 */
2191 enum isl_aux_usage optimal_sampler_aux_usage;
2192 struct anv_state optimal_sampler_surface_state;
2193
2194 /**
2195 * RENDER_SURFACE_STATE when using image as a sampler surface with an image
2196 * layout of GENERAL.
2197 */
2198 enum isl_aux_usage general_sampler_aux_usage;
2199 struct anv_state general_sampler_surface_state;
2200
2201 /**
2202 * RENDER_SURFACE_STATE when using image as a storage image. Separate states
2203 * for write-only and readable, using the real format for write-only and the
2204 * lowered format for readable.
2205 */
2206 struct anv_state storage_surface_state;
2207 struct anv_state writeonly_storage_surface_state;
2208
2209 struct brw_image_param storage_image_param;
2210 };
2211
2212 struct anv_image_create_info {
2213 const VkImageCreateInfo *vk_info;
2214
2215 /** An opt-in bitmask which filters an ISL-mapping of the Vulkan tiling. */
2216 isl_tiling_flags_t isl_tiling_flags;
2217
2218 uint32_t stride;
2219 };
2220
2221 VkResult anv_image_create(VkDevice _device,
2222 const struct anv_image_create_info *info,
2223 const VkAllocationCallbacks* alloc,
2224 VkImage *pImage);
2225
2226 const struct anv_surface *
2227 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
2228 VkImageAspectFlags aspect_mask);
2229
2230 enum isl_format
2231 anv_isl_format_for_descriptor_type(VkDescriptorType type);
2232
2233 static inline struct VkExtent3D
2234 anv_sanitize_image_extent(const VkImageType imageType,
2235 const struct VkExtent3D imageExtent)
2236 {
2237 switch (imageType) {
2238 case VK_IMAGE_TYPE_1D:
2239 return (VkExtent3D) { imageExtent.width, 1, 1 };
2240 case VK_IMAGE_TYPE_2D:
2241 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
2242 case VK_IMAGE_TYPE_3D:
2243 return imageExtent;
2244 default:
2245 unreachable("invalid image type");
2246 }
2247 }
2248
2249 static inline struct VkOffset3D
2250 anv_sanitize_image_offset(const VkImageType imageType,
2251 const struct VkOffset3D imageOffset)
2252 {
2253 switch (imageType) {
2254 case VK_IMAGE_TYPE_1D:
2255 return (VkOffset3D) { imageOffset.x, 0, 0 };
2256 case VK_IMAGE_TYPE_2D:
2257 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
2258 case VK_IMAGE_TYPE_3D:
2259 return imageOffset;
2260 default:
2261 unreachable("invalid image type");
2262 }
2263 }
2264
2265
2266 void anv_fill_buffer_surface_state(struct anv_device *device,
2267 struct anv_state state,
2268 enum isl_format format,
2269 uint32_t offset, uint32_t range,
2270 uint32_t stride);
2271
2272 struct anv_sampler {
2273 uint32_t state[4];
2274 };
2275
2276 struct anv_framebuffer {
2277 uint32_t width;
2278 uint32_t height;
2279 uint32_t layers;
2280
2281 uint32_t attachment_count;
2282 struct anv_image_view * attachments[0];
2283 };
2284
2285 struct anv_subpass {
2286 uint32_t attachment_count;
2287
2288 /**
2289 * A pointer to all attachment references used in this subpass.
2290 * Only valid if ::attachment_count > 0.
2291 */
2292 VkAttachmentReference * attachments;
2293 uint32_t input_count;
2294 VkAttachmentReference * input_attachments;
2295 uint32_t color_count;
2296 VkAttachmentReference * color_attachments;
2297 VkAttachmentReference * resolve_attachments;
2298
2299 VkAttachmentReference depth_stencil_attachment;
2300
2301 uint32_t view_mask;
2302
2303 /** Subpass has a depth/stencil self-dependency */
2304 bool has_ds_self_dep;
2305
2306 /** Subpass has at least one resolve attachment */
2307 bool has_resolve;
2308 };
2309
2310 static inline unsigned
2311 anv_subpass_view_count(const struct anv_subpass *subpass)
2312 {
2313 return MAX2(1, _mesa_bitcount(subpass->view_mask));
2314 }
2315
2316 struct anv_render_pass_attachment {
2317 /* TODO: Consider using VkAttachmentDescription instead of storing each of
2318 * its members individually.
2319 */
2320 VkFormat format;
2321 uint32_t samples;
2322 VkImageUsageFlags usage;
2323 VkAttachmentLoadOp load_op;
2324 VkAttachmentStoreOp store_op;
2325 VkAttachmentLoadOp stencil_load_op;
2326 VkImageLayout initial_layout;
2327 VkImageLayout final_layout;
2328 VkImageLayout first_subpass_layout;
2329
2330 /* The subpass id in which the attachment will be used last. */
2331 uint32_t last_subpass_idx;
2332 };
2333
2334 struct anv_render_pass {
2335 uint32_t attachment_count;
2336 uint32_t subpass_count;
2337 /* An array of subpass_count+1 flushes, one per subpass boundary */
2338 enum anv_pipe_bits * subpass_flushes;
2339 struct anv_render_pass_attachment * attachments;
2340 struct anv_subpass subpasses[0];
2341 };
2342
2343 #define ANV_PIPELINE_STATISTICS_MASK 0x000007ff
2344
2345 struct anv_query_pool {
2346 VkQueryType type;
2347 VkQueryPipelineStatisticFlags pipeline_statistics;
2348 /** Stride between slots, in bytes */
2349 uint32_t stride;
2350 /** Number of slots in this query pool */
2351 uint32_t slots;
2352 struct anv_bo bo;
2353 };
2354
2355 void *anv_lookup_entrypoint(const struct gen_device_info *devinfo,
2356 const char *name);
2357
2358 void anv_dump_image_to_ppm(struct anv_device *device,
2359 struct anv_image *image, unsigned miplevel,
2360 unsigned array_layer, VkImageAspectFlagBits aspect,
2361 const char *filename);
2362
2363 enum anv_dump_action {
2364 ANV_DUMP_FRAMEBUFFERS_BIT = 0x1,
2365 };
2366
2367 void anv_dump_start(struct anv_device *device, enum anv_dump_action actions);
2368 void anv_dump_finish(void);
2369
2370 void anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
2371 struct anv_framebuffer *fb);
2372
2373 static inline uint32_t
2374 anv_get_subpass_id(const struct anv_cmd_state * const cmd_state)
2375 {
2376 /* This function must be called from within a subpass. */
2377 assert(cmd_state->pass && cmd_state->subpass);
2378
2379 const uint32_t subpass_id = cmd_state->subpass - cmd_state->pass->subpasses;
2380
2381 /* The id of this subpass shouldn't exceed the number of subpasses in this
2382 * render pass minus 1.
2383 */
2384 assert(subpass_id < cmd_state->pass->subpass_count);
2385 return subpass_id;
2386 }
2387
2388 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
2389 \
2390 static inline struct __anv_type * \
2391 __anv_type ## _from_handle(__VkType _handle) \
2392 { \
2393 return (struct __anv_type *) _handle; \
2394 } \
2395 \
2396 static inline __VkType \
2397 __anv_type ## _to_handle(struct __anv_type *_obj) \
2398 { \
2399 return (__VkType) _obj; \
2400 }
2401
2402 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
2403 \
2404 static inline struct __anv_type * \
2405 __anv_type ## _from_handle(__VkType _handle) \
2406 { \
2407 return (struct __anv_type *)(uintptr_t) _handle; \
2408 } \
2409 \
2410 static inline __VkType \
2411 __anv_type ## _to_handle(struct __anv_type *_obj) \
2412 { \
2413 return (__VkType)(uintptr_t) _obj; \
2414 }
2415
2416 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
2417 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
2418
2419 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
2420 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
2421 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
2422 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
2423 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
2424
2425 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
2426 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
2427 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView)
2428 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool, VkDescriptorPool)
2429 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
2430 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
2431 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_update_template, VkDescriptorUpdateTemplateKHR)
2432 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
2433 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
2434 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event, VkEvent)
2435 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
2436 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
2437 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
2438 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache, VkPipelineCache)
2439 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
2440 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
2441 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
2442 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
2443 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
2444 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_semaphore, VkSemaphore)
2445 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
2446
2447 /* Gen-specific function declarations */
2448 #ifdef genX
2449 # include "anv_genX.h"
2450 #else
2451 # define genX(x) gen7_##x
2452 # include "anv_genX.h"
2453 # undef genX
2454 # define genX(x) gen75_##x
2455 # include "anv_genX.h"
2456 # undef genX
2457 # define genX(x) gen8_##x
2458 # include "anv_genX.h"
2459 # undef genX
2460 # define genX(x) gen9_##x
2461 # include "anv_genX.h"
2462 # undef genX
2463 # define genX(x) gen10_##x
2464 # include "anv_genX.h"
2465 # undef genX
2466 #endif
2467
2468 #endif /* ANV_PRIVATE_H */