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