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