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