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