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