anv: Implement VK_EXT_conditional_rendering for gen 7.5+
[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 are ongoing. Some operations like copies on the
1794 * command streamer might need to be aware of this to trigger the
1795 * appropriate stall before they can proceed with the copy.
1796 */
1797 ANV_PIPE_RENDER_TARGET_WRITES = (1 << 22),
1798 };
1799
1800 #define ANV_PIPE_FLUSH_BITS ( \
1801 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT | \
1802 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1803 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1804
1805 #define ANV_PIPE_STALL_BITS ( \
1806 ANV_PIPE_STALL_AT_SCOREBOARD_BIT | \
1807 ANV_PIPE_DEPTH_STALL_BIT | \
1808 ANV_PIPE_CS_STALL_BIT)
1809
1810 #define ANV_PIPE_INVALIDATE_BITS ( \
1811 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT | \
1812 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT | \
1813 ANV_PIPE_VF_CACHE_INVALIDATE_BIT | \
1814 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1815 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
1816 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
1817
1818 static inline enum anv_pipe_bits
1819 anv_pipe_flush_bits_for_access_flags(VkAccessFlags flags)
1820 {
1821 enum anv_pipe_bits pipe_bits = 0;
1822
1823 unsigned b;
1824 for_each_bit(b, flags) {
1825 switch ((VkAccessFlagBits)(1 << b)) {
1826 case VK_ACCESS_SHADER_WRITE_BIT:
1827 pipe_bits |= ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1828 break;
1829 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT:
1830 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1831 break;
1832 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT:
1833 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1834 break;
1835 case VK_ACCESS_TRANSFER_WRITE_BIT:
1836 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1837 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1838 break;
1839 case VK_ACCESS_MEMORY_WRITE_BIT:
1840 pipe_bits |= ANV_PIPE_FLUSH_BITS;
1841 break;
1842 default:
1843 break; /* Nothing to do */
1844 }
1845 }
1846
1847 return pipe_bits;
1848 }
1849
1850 static inline enum anv_pipe_bits
1851 anv_pipe_invalidate_bits_for_access_flags(VkAccessFlags flags)
1852 {
1853 enum anv_pipe_bits pipe_bits = 0;
1854
1855 unsigned b;
1856 for_each_bit(b, flags) {
1857 switch ((VkAccessFlagBits)(1 << b)) {
1858 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT:
1859 case VK_ACCESS_INDEX_READ_BIT:
1860 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT:
1861 pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1862 break;
1863 case VK_ACCESS_UNIFORM_READ_BIT:
1864 pipe_bits |= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1865 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1866 break;
1867 case VK_ACCESS_SHADER_READ_BIT:
1868 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT:
1869 case VK_ACCESS_TRANSFER_READ_BIT:
1870 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1871 break;
1872 case VK_ACCESS_MEMORY_READ_BIT:
1873 pipe_bits |= ANV_PIPE_INVALIDATE_BITS;
1874 break;
1875 case VK_ACCESS_MEMORY_WRITE_BIT:
1876 pipe_bits |= ANV_PIPE_FLUSH_BITS;
1877 break;
1878 case VK_ACCESS_CONDITIONAL_RENDERING_READ_BIT_EXT:
1879 pipe_bits |= ANV_PIPE_CS_STALL_BIT;
1880 break;
1881 default:
1882 break; /* Nothing to do */
1883 }
1884 }
1885
1886 return pipe_bits;
1887 }
1888
1889 #define VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV ( \
1890 VK_IMAGE_ASPECT_COLOR_BIT | \
1891 VK_IMAGE_ASPECT_PLANE_0_BIT | \
1892 VK_IMAGE_ASPECT_PLANE_1_BIT | \
1893 VK_IMAGE_ASPECT_PLANE_2_BIT)
1894 #define VK_IMAGE_ASPECT_PLANES_BITS_ANV ( \
1895 VK_IMAGE_ASPECT_PLANE_0_BIT | \
1896 VK_IMAGE_ASPECT_PLANE_1_BIT | \
1897 VK_IMAGE_ASPECT_PLANE_2_BIT)
1898
1899 struct anv_vertex_binding {
1900 struct anv_buffer * buffer;
1901 VkDeviceSize offset;
1902 };
1903
1904 #define ANV_PARAM_PUSH(offset) ((1 << 16) | (uint32_t)(offset))
1905 #define ANV_PARAM_PUSH_OFFSET(param) ((param) & 0xffff)
1906
1907 struct anv_push_constants {
1908 /* Current allocated size of this push constants data structure.
1909 * Because a decent chunk of it may not be used (images on SKL, for
1910 * instance), we won't actually allocate the entire structure up-front.
1911 */
1912 uint32_t size;
1913
1914 /* Push constant data provided by the client through vkPushConstants */
1915 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
1916
1917 /* Used for vkCmdDispatchBase */
1918 uint32_t base_work_group_id[3];
1919
1920 /* Image data for image_load_store on pre-SKL */
1921 struct brw_image_param images[MAX_GEN8_IMAGES];
1922 };
1923
1924 struct anv_dynamic_state {
1925 struct {
1926 uint32_t count;
1927 VkViewport viewports[MAX_VIEWPORTS];
1928 } viewport;
1929
1930 struct {
1931 uint32_t count;
1932 VkRect2D scissors[MAX_SCISSORS];
1933 } scissor;
1934
1935 float line_width;
1936
1937 struct {
1938 float bias;
1939 float clamp;
1940 float slope;
1941 } depth_bias;
1942
1943 float blend_constants[4];
1944
1945 struct {
1946 float min;
1947 float max;
1948 } depth_bounds;
1949
1950 struct {
1951 uint32_t front;
1952 uint32_t back;
1953 } stencil_compare_mask;
1954
1955 struct {
1956 uint32_t front;
1957 uint32_t back;
1958 } stencil_write_mask;
1959
1960 struct {
1961 uint32_t front;
1962 uint32_t back;
1963 } stencil_reference;
1964 };
1965
1966 extern const struct anv_dynamic_state default_dynamic_state;
1967
1968 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
1969 const struct anv_dynamic_state *src,
1970 uint32_t copy_mask);
1971
1972 struct anv_surface_state {
1973 struct anv_state state;
1974 /** Address of the surface referred to by this state
1975 *
1976 * This address is relative to the start of the BO.
1977 */
1978 struct anv_address address;
1979 /* Address of the aux surface, if any
1980 *
1981 * This field is ANV_NULL_ADDRESS if and only if no aux surface exists.
1982 *
1983 * With the exception of gen8, the bottom 12 bits of this address' offset
1984 * include extra aux information.
1985 */
1986 struct anv_address aux_address;
1987 /* Address of the clear color, if any
1988 *
1989 * This address is relative to the start of the BO.
1990 */
1991 struct anv_address clear_address;
1992 };
1993
1994 /**
1995 * Attachment state when recording a renderpass instance.
1996 *
1997 * The clear value is valid only if there exists a pending clear.
1998 */
1999 struct anv_attachment_state {
2000 enum isl_aux_usage aux_usage;
2001 enum isl_aux_usage input_aux_usage;
2002 struct anv_surface_state color;
2003 struct anv_surface_state input;
2004
2005 VkImageLayout current_layout;
2006 VkImageAspectFlags pending_clear_aspects;
2007 VkImageAspectFlags pending_load_aspects;
2008 bool fast_clear;
2009 VkClearValue clear_value;
2010 bool clear_color_is_zero_one;
2011 bool clear_color_is_zero;
2012
2013 /* When multiview is active, attachments with a renderpass clear
2014 * operation have their respective layers cleared on the first
2015 * subpass that uses them, and only in that subpass. We keep track
2016 * of this using a bitfield to indicate which layers of an attachment
2017 * have not been cleared yet when multiview is active.
2018 */
2019 uint32_t pending_clear_views;
2020 };
2021
2022 /** State tracking for particular pipeline bind point
2023 *
2024 * This struct is the base struct for anv_cmd_graphics_state and
2025 * anv_cmd_compute_state. These are used to track state which is bound to a
2026 * particular type of pipeline. Generic state that applies per-stage such as
2027 * binding table offsets and push constants is tracked generically with a
2028 * per-stage array in anv_cmd_state.
2029 */
2030 struct anv_cmd_pipeline_state {
2031 struct anv_pipeline *pipeline;
2032 struct anv_pipeline_layout *layout;
2033
2034 struct anv_descriptor_set *descriptors[MAX_SETS];
2035 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
2036
2037 struct anv_push_descriptor_set *push_descriptors[MAX_SETS];
2038 };
2039
2040 /** State tracking for graphics pipeline
2041 *
2042 * This has anv_cmd_pipeline_state as a base struct to track things which get
2043 * bound to a graphics pipeline. Along with general pipeline bind point state
2044 * which is in the anv_cmd_pipeline_state base struct, it also contains other
2045 * state which is graphics-specific.
2046 */
2047 struct anv_cmd_graphics_state {
2048 struct anv_cmd_pipeline_state base;
2049
2050 anv_cmd_dirty_mask_t dirty;
2051 uint32_t vb_dirty;
2052
2053 struct anv_dynamic_state dynamic;
2054
2055 struct {
2056 struct anv_buffer *index_buffer;
2057 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
2058 uint32_t index_offset;
2059 } gen7;
2060 };
2061
2062 /** State tracking for compute pipeline
2063 *
2064 * This has anv_cmd_pipeline_state as a base struct to track things which get
2065 * bound to a compute pipeline. Along with general pipeline bind point state
2066 * which is in the anv_cmd_pipeline_state base struct, it also contains other
2067 * state which is compute-specific.
2068 */
2069 struct anv_cmd_compute_state {
2070 struct anv_cmd_pipeline_state base;
2071
2072 bool pipeline_dirty;
2073
2074 struct anv_address num_workgroups;
2075 };
2076
2077 /** State required while building cmd buffer */
2078 struct anv_cmd_state {
2079 /* PIPELINE_SELECT.PipelineSelection */
2080 uint32_t current_pipeline;
2081 const struct gen_l3_config * current_l3_config;
2082
2083 struct anv_cmd_graphics_state gfx;
2084 struct anv_cmd_compute_state compute;
2085
2086 enum anv_pipe_bits pending_pipe_bits;
2087 VkShaderStageFlags descriptors_dirty;
2088 VkShaderStageFlags push_constants_dirty;
2089
2090 struct anv_framebuffer * framebuffer;
2091 struct anv_render_pass * pass;
2092 struct anv_subpass * subpass;
2093 VkRect2D render_area;
2094 uint32_t restart_index;
2095 struct anv_vertex_binding vertex_bindings[MAX_VBS];
2096 VkShaderStageFlags push_constant_stages;
2097 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
2098 struct anv_state binding_tables[MESA_SHADER_STAGES];
2099 struct anv_state samplers[MESA_SHADER_STAGES];
2100
2101 /**
2102 * Whether or not the gen8 PMA fix is enabled. We ensure that, at the top
2103 * of any command buffer it is disabled by disabling it in EndCommandBuffer
2104 * and before invoking the secondary in ExecuteCommands.
2105 */
2106 bool pma_fix_enabled;
2107
2108 /**
2109 * Whether or not we know for certain that HiZ is enabled for the current
2110 * subpass. If, for whatever reason, we are unsure as to whether HiZ is
2111 * enabled or not, this will be false.
2112 */
2113 bool hiz_enabled;
2114
2115 bool conditional_render_enabled;
2116
2117 /**
2118 * Array length is anv_cmd_state::pass::attachment_count. Array content is
2119 * valid only when recording a render pass instance.
2120 */
2121 struct anv_attachment_state * attachments;
2122
2123 /**
2124 * Surface states for color render targets. These are stored in a single
2125 * flat array. For depth-stencil attachments, the surface state is simply
2126 * left blank.
2127 */
2128 struct anv_state render_pass_states;
2129
2130 /**
2131 * A null surface state of the right size to match the framebuffer. This
2132 * is one of the states in render_pass_states.
2133 */
2134 struct anv_state null_surface_state;
2135 };
2136
2137 struct anv_cmd_pool {
2138 VkAllocationCallbacks alloc;
2139 struct list_head cmd_buffers;
2140 };
2141
2142 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
2143
2144 enum anv_cmd_buffer_exec_mode {
2145 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
2146 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
2147 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT,
2148 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
2149 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
2150 };
2151
2152 struct anv_cmd_buffer {
2153 VK_LOADER_DATA _loader_data;
2154
2155 struct anv_device * device;
2156
2157 struct anv_cmd_pool * pool;
2158 struct list_head pool_link;
2159
2160 struct anv_batch batch;
2161
2162 /* Fields required for the actual chain of anv_batch_bo's.
2163 *
2164 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
2165 */
2166 struct list_head batch_bos;
2167 enum anv_cmd_buffer_exec_mode exec_mode;
2168
2169 /* A vector of anv_batch_bo pointers for every batch or surface buffer
2170 * referenced by this command buffer
2171 *
2172 * initialized by anv_cmd_buffer_init_batch_bo_chain()
2173 */
2174 struct u_vector seen_bbos;
2175
2176 /* A vector of int32_t's for every block of binding tables.
2177 *
2178 * initialized by anv_cmd_buffer_init_batch_bo_chain()
2179 */
2180 struct u_vector bt_block_states;
2181 uint32_t bt_next;
2182
2183 struct anv_reloc_list surface_relocs;
2184 /** Last seen surface state block pool center bo offset */
2185 uint32_t last_ss_pool_center;
2186
2187 /* Serial for tracking buffer completion */
2188 uint32_t serial;
2189
2190 /* Stream objects for storing temporary data */
2191 struct anv_state_stream surface_state_stream;
2192 struct anv_state_stream dynamic_state_stream;
2193
2194 VkCommandBufferUsageFlags usage_flags;
2195 VkCommandBufferLevel level;
2196
2197 struct anv_cmd_state state;
2198 };
2199
2200 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
2201 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
2202 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
2203 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
2204 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
2205 struct anv_cmd_buffer *secondary);
2206 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
2207 VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
2208 struct anv_cmd_buffer *cmd_buffer,
2209 const VkSemaphore *in_semaphores,
2210 uint32_t num_in_semaphores,
2211 const VkSemaphore *out_semaphores,
2212 uint32_t num_out_semaphores,
2213 VkFence fence);
2214
2215 VkResult anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer);
2216
2217 VkResult
2218 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
2219 gl_shader_stage stage, uint32_t size);
2220 #define anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, field) \
2221 anv_cmd_buffer_ensure_push_constants_size(cmd_buffer, stage, \
2222 (offsetof(struct anv_push_constants, field) + \
2223 sizeof(cmd_buffer->state.push_constants[0]->field)))
2224
2225 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
2226 const void *data, uint32_t size, uint32_t alignment);
2227 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
2228 uint32_t *a, uint32_t *b,
2229 uint32_t dwords, uint32_t alignment);
2230
2231 struct anv_address
2232 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
2233 struct anv_state
2234 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
2235 uint32_t entries, uint32_t *state_offset);
2236 struct anv_state
2237 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
2238 struct anv_state
2239 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
2240 uint32_t size, uint32_t alignment);
2241
2242 VkResult
2243 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
2244
2245 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
2246 void gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer *cmd_buffer,
2247 bool depth_clamp_enable);
2248 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
2249
2250 void anv_cmd_buffer_setup_attachments(struct anv_cmd_buffer *cmd_buffer,
2251 struct anv_render_pass *pass,
2252 struct anv_framebuffer *framebuffer,
2253 const VkClearValue *clear_values);
2254
2255 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
2256
2257 struct anv_state
2258 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
2259 gl_shader_stage stage);
2260 struct anv_state
2261 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer);
2262
2263 const struct anv_image_view *
2264 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
2265
2266 VkResult
2267 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
2268 uint32_t num_entries,
2269 uint32_t *state_offset,
2270 struct anv_state *bt_state);
2271
2272 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
2273
2274 void anv_cmd_emit_conditional_render_predicate(struct anv_cmd_buffer *cmd_buffer);
2275
2276 enum anv_fence_type {
2277 ANV_FENCE_TYPE_NONE = 0,
2278 ANV_FENCE_TYPE_BO,
2279 ANV_FENCE_TYPE_SYNCOBJ,
2280 ANV_FENCE_TYPE_WSI,
2281 };
2282
2283 enum anv_bo_fence_state {
2284 /** Indicates that this is a new (or newly reset fence) */
2285 ANV_BO_FENCE_STATE_RESET,
2286
2287 /** Indicates that this fence has been submitted to the GPU but is still
2288 * (as far as we know) in use by the GPU.
2289 */
2290 ANV_BO_FENCE_STATE_SUBMITTED,
2291
2292 ANV_BO_FENCE_STATE_SIGNALED,
2293 };
2294
2295 struct anv_fence_impl {
2296 enum anv_fence_type type;
2297
2298 union {
2299 /** Fence implementation for BO fences
2300 *
2301 * These fences use a BO and a set of CPU-tracked state flags. The BO
2302 * is added to the object list of the last execbuf call in a QueueSubmit
2303 * and is marked EXEC_WRITE. The state flags track when the BO has been
2304 * submitted to the kernel. We need to do this because Vulkan lets you
2305 * wait on a fence that has not yet been submitted and I915_GEM_BUSY
2306 * will say it's idle in this case.
2307 */
2308 struct {
2309 struct anv_bo bo;
2310 enum anv_bo_fence_state state;
2311 } bo;
2312
2313 /** DRM syncobj handle for syncobj-based fences */
2314 uint32_t syncobj;
2315
2316 /** WSI fence */
2317 struct wsi_fence *fence_wsi;
2318 };
2319 };
2320
2321 struct anv_fence {
2322 /* Permanent fence state. Every fence has some form of permanent state
2323 * (type != ANV_SEMAPHORE_TYPE_NONE). This may be a BO to fence on (for
2324 * cross-process fences) or it could just be a dummy for use internally.
2325 */
2326 struct anv_fence_impl permanent;
2327
2328 /* Temporary fence state. A fence *may* have temporary state. That state
2329 * is added to the fence by an import operation and is reset back to
2330 * ANV_SEMAPHORE_TYPE_NONE when the fence is reset. A fence with temporary
2331 * state cannot be signaled because the fence must already be signaled
2332 * before the temporary state can be exported from the fence in the other
2333 * process and imported here.
2334 */
2335 struct anv_fence_impl temporary;
2336 };
2337
2338 struct anv_event {
2339 uint64_t semaphore;
2340 struct anv_state state;
2341 };
2342
2343 enum anv_semaphore_type {
2344 ANV_SEMAPHORE_TYPE_NONE = 0,
2345 ANV_SEMAPHORE_TYPE_DUMMY,
2346 ANV_SEMAPHORE_TYPE_BO,
2347 ANV_SEMAPHORE_TYPE_SYNC_FILE,
2348 ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ,
2349 };
2350
2351 struct anv_semaphore_impl {
2352 enum anv_semaphore_type type;
2353
2354 union {
2355 /* A BO representing this semaphore when type == ANV_SEMAPHORE_TYPE_BO.
2356 * This BO will be added to the object list on any execbuf2 calls for
2357 * which this semaphore is used as a wait or signal fence. When used as
2358 * a signal fence, the EXEC_OBJECT_WRITE flag will be set.
2359 */
2360 struct anv_bo *bo;
2361
2362 /* The sync file descriptor when type == ANV_SEMAPHORE_TYPE_SYNC_FILE.
2363 * If the semaphore is in the unsignaled state due to either just being
2364 * created or because it has been used for a wait, fd will be -1.
2365 */
2366 int fd;
2367
2368 /* Sync object handle when type == ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ.
2369 * Unlike GEM BOs, DRM sync objects aren't deduplicated by the kernel on
2370 * import so we don't need to bother with a userspace cache.
2371 */
2372 uint32_t syncobj;
2373 };
2374 };
2375
2376 struct anv_semaphore {
2377 /* Permanent semaphore state. Every semaphore has some form of permanent
2378 * state (type != ANV_SEMAPHORE_TYPE_NONE). This may be a BO to fence on
2379 * (for cross-process semaphores0 or it could just be a dummy for use
2380 * internally.
2381 */
2382 struct anv_semaphore_impl permanent;
2383
2384 /* Temporary semaphore state. A semaphore *may* have temporary state.
2385 * That state is added to the semaphore by an import operation and is reset
2386 * back to ANV_SEMAPHORE_TYPE_NONE when the semaphore is waited on. A
2387 * semaphore with temporary state cannot be signaled because the semaphore
2388 * must already be signaled before the temporary state can be exported from
2389 * the semaphore in the other process and imported here.
2390 */
2391 struct anv_semaphore_impl temporary;
2392 };
2393
2394 void anv_semaphore_reset_temporary(struct anv_device *device,
2395 struct anv_semaphore *semaphore);
2396
2397 struct anv_shader_module {
2398 unsigned char sha1[20];
2399 uint32_t size;
2400 char data[0];
2401 };
2402
2403 static inline gl_shader_stage
2404 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
2405 {
2406 assert(__builtin_popcount(vk_stage) == 1);
2407 return ffs(vk_stage) - 1;
2408 }
2409
2410 static inline VkShaderStageFlagBits
2411 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
2412 {
2413 return (1 << mesa_stage);
2414 }
2415
2416 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
2417
2418 #define anv_foreach_stage(stage, stage_bits) \
2419 for (gl_shader_stage stage, \
2420 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
2421 stage = __builtin_ffs(__tmp) - 1, __tmp; \
2422 __tmp &= ~(1 << (stage)))
2423
2424 struct anv_pipeline_bind_map {
2425 uint32_t surface_count;
2426 uint32_t sampler_count;
2427 uint32_t image_count;
2428
2429 struct anv_pipeline_binding * surface_to_descriptor;
2430 struct anv_pipeline_binding * sampler_to_descriptor;
2431 };
2432
2433 struct anv_shader_bin_key {
2434 uint32_t size;
2435 uint8_t data[0];
2436 };
2437
2438 struct anv_shader_bin {
2439 uint32_t ref_cnt;
2440
2441 const struct anv_shader_bin_key *key;
2442
2443 struct anv_state kernel;
2444 uint32_t kernel_size;
2445
2446 struct anv_state constant_data;
2447 uint32_t constant_data_size;
2448
2449 const struct brw_stage_prog_data *prog_data;
2450 uint32_t prog_data_size;
2451
2452 struct anv_pipeline_bind_map bind_map;
2453 };
2454
2455 struct anv_shader_bin *
2456 anv_shader_bin_create(struct anv_device *device,
2457 const void *key, uint32_t key_size,
2458 const void *kernel, uint32_t kernel_size,
2459 const void *constant_data, uint32_t constant_data_size,
2460 const struct brw_stage_prog_data *prog_data,
2461 uint32_t prog_data_size, const void *prog_data_param,
2462 const struct anv_pipeline_bind_map *bind_map);
2463
2464 void
2465 anv_shader_bin_destroy(struct anv_device *device, struct anv_shader_bin *shader);
2466
2467 static inline void
2468 anv_shader_bin_ref(struct anv_shader_bin *shader)
2469 {
2470 assert(shader && shader->ref_cnt >= 1);
2471 p_atomic_inc(&shader->ref_cnt);
2472 }
2473
2474 static inline void
2475 anv_shader_bin_unref(struct anv_device *device, struct anv_shader_bin *shader)
2476 {
2477 assert(shader && shader->ref_cnt >= 1);
2478 if (p_atomic_dec_zero(&shader->ref_cnt))
2479 anv_shader_bin_destroy(device, shader);
2480 }
2481
2482 struct anv_pipeline {
2483 struct anv_device * device;
2484 struct anv_batch batch;
2485 uint32_t batch_data[512];
2486 struct anv_reloc_list batch_relocs;
2487 uint32_t dynamic_state_mask;
2488 struct anv_dynamic_state dynamic_state;
2489
2490 struct anv_subpass * subpass;
2491
2492 bool needs_data_cache;
2493
2494 struct anv_shader_bin * shaders[MESA_SHADER_STAGES];
2495
2496 struct {
2497 const struct gen_l3_config * l3_config;
2498 uint32_t total_size;
2499 } urb;
2500
2501 VkShaderStageFlags active_stages;
2502 struct anv_state blend_state;
2503
2504 uint32_t vb_used;
2505 struct anv_pipeline_vertex_binding {
2506 uint32_t stride;
2507 bool instanced;
2508 uint32_t instance_divisor;
2509 } vb[MAX_VBS];
2510
2511 bool primitive_restart;
2512 uint32_t topology;
2513
2514 uint32_t cs_right_mask;
2515
2516 bool writes_depth;
2517 bool depth_test_enable;
2518 bool writes_stencil;
2519 bool stencil_test_enable;
2520 bool depth_clamp_enable;
2521 bool sample_shading_enable;
2522 bool kill_pixel;
2523
2524 struct {
2525 uint32_t sf[7];
2526 uint32_t depth_stencil_state[3];
2527 } gen7;
2528
2529 struct {
2530 uint32_t sf[4];
2531 uint32_t raster[5];
2532 uint32_t wm_depth_stencil[3];
2533 } gen8;
2534
2535 struct {
2536 uint32_t wm_depth_stencil[4];
2537 } gen9;
2538
2539 uint32_t interface_descriptor_data[8];
2540 };
2541
2542 static inline bool
2543 anv_pipeline_has_stage(const struct anv_pipeline *pipeline,
2544 gl_shader_stage stage)
2545 {
2546 return (pipeline->active_stages & mesa_to_vk_shader_stage(stage)) != 0;
2547 }
2548
2549 #define ANV_DECL_GET_PROG_DATA_FUNC(prefix, stage) \
2550 static inline const struct brw_##prefix##_prog_data * \
2551 get_##prefix##_prog_data(const struct anv_pipeline *pipeline) \
2552 { \
2553 if (anv_pipeline_has_stage(pipeline, stage)) { \
2554 return (const struct brw_##prefix##_prog_data *) \
2555 pipeline->shaders[stage]->prog_data; \
2556 } else { \
2557 return NULL; \
2558 } \
2559 }
2560
2561 ANV_DECL_GET_PROG_DATA_FUNC(vs, MESA_SHADER_VERTEX)
2562 ANV_DECL_GET_PROG_DATA_FUNC(tcs, MESA_SHADER_TESS_CTRL)
2563 ANV_DECL_GET_PROG_DATA_FUNC(tes, MESA_SHADER_TESS_EVAL)
2564 ANV_DECL_GET_PROG_DATA_FUNC(gs, MESA_SHADER_GEOMETRY)
2565 ANV_DECL_GET_PROG_DATA_FUNC(wm, MESA_SHADER_FRAGMENT)
2566 ANV_DECL_GET_PROG_DATA_FUNC(cs, MESA_SHADER_COMPUTE)
2567
2568 static inline const struct brw_vue_prog_data *
2569 anv_pipeline_get_last_vue_prog_data(const struct anv_pipeline *pipeline)
2570 {
2571 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
2572 return &get_gs_prog_data(pipeline)->base;
2573 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
2574 return &get_tes_prog_data(pipeline)->base;
2575 else
2576 return &get_vs_prog_data(pipeline)->base;
2577 }
2578
2579 VkResult
2580 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
2581 struct anv_pipeline_cache *cache,
2582 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2583 const VkAllocationCallbacks *alloc);
2584
2585 VkResult
2586 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
2587 struct anv_pipeline_cache *cache,
2588 const VkComputePipelineCreateInfo *info,
2589 const struct anv_shader_module *module,
2590 const char *entrypoint,
2591 const VkSpecializationInfo *spec_info);
2592
2593 struct anv_format_plane {
2594 enum isl_format isl_format:16;
2595 struct isl_swizzle swizzle;
2596
2597 /* Whether this plane contains chroma channels */
2598 bool has_chroma;
2599
2600 /* For downscaling of YUV planes */
2601 uint8_t denominator_scales[2];
2602
2603 /* How to map sampled ycbcr planes to a single 4 component element. */
2604 struct isl_swizzle ycbcr_swizzle;
2605
2606 /* What aspect is associated to this plane */
2607 VkImageAspectFlags aspect;
2608 };
2609
2610
2611 struct anv_format {
2612 struct anv_format_plane planes[3];
2613 VkFormat vk_format;
2614 uint8_t n_planes;
2615 bool can_ycbcr;
2616 };
2617
2618 static inline uint32_t
2619 anv_image_aspect_to_plane(VkImageAspectFlags image_aspects,
2620 VkImageAspectFlags aspect_mask)
2621 {
2622 switch (aspect_mask) {
2623 case VK_IMAGE_ASPECT_COLOR_BIT:
2624 case VK_IMAGE_ASPECT_DEPTH_BIT:
2625 case VK_IMAGE_ASPECT_PLANE_0_BIT:
2626 return 0;
2627 case VK_IMAGE_ASPECT_STENCIL_BIT:
2628 if ((image_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) == 0)
2629 return 0;
2630 /* Fall-through */
2631 case VK_IMAGE_ASPECT_PLANE_1_BIT:
2632 return 1;
2633 case VK_IMAGE_ASPECT_PLANE_2_BIT:
2634 return 2;
2635 default:
2636 /* Purposefully assert with depth/stencil aspects. */
2637 unreachable("invalid image aspect");
2638 }
2639 }
2640
2641 static inline VkImageAspectFlags
2642 anv_plane_to_aspect(VkImageAspectFlags image_aspects,
2643 uint32_t plane)
2644 {
2645 if (image_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
2646 if (util_bitcount(image_aspects) > 1)
2647 return VK_IMAGE_ASPECT_PLANE_0_BIT << plane;
2648 return VK_IMAGE_ASPECT_COLOR_BIT;
2649 }
2650 if (image_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)
2651 return VK_IMAGE_ASPECT_DEPTH_BIT << plane;
2652 assert(image_aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
2653 return VK_IMAGE_ASPECT_STENCIL_BIT;
2654 }
2655
2656 #define anv_foreach_image_aspect_bit(b, image, aspects) \
2657 for_each_bit(b, anv_image_expand_aspects(image, aspects))
2658
2659 const struct anv_format *
2660 anv_get_format(VkFormat format);
2661
2662 static inline uint32_t
2663 anv_get_format_planes(VkFormat vk_format)
2664 {
2665 const struct anv_format *format = anv_get_format(vk_format);
2666
2667 return format != NULL ? format->n_planes : 0;
2668 }
2669
2670 struct anv_format_plane
2671 anv_get_format_plane(const struct gen_device_info *devinfo, VkFormat vk_format,
2672 VkImageAspectFlagBits aspect, VkImageTiling tiling);
2673
2674 static inline enum isl_format
2675 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
2676 VkImageAspectFlags aspect, VkImageTiling tiling)
2677 {
2678 return anv_get_format_plane(devinfo, vk_format, aspect, tiling).isl_format;
2679 }
2680
2681 static inline struct isl_swizzle
2682 anv_swizzle_for_render(struct isl_swizzle swizzle)
2683 {
2684 /* Sometimes the swizzle will have alpha map to one. We do this to fake
2685 * RGB as RGBA for texturing
2686 */
2687 assert(swizzle.a == ISL_CHANNEL_SELECT_ONE ||
2688 swizzle.a == ISL_CHANNEL_SELECT_ALPHA);
2689
2690 /* But it doesn't matter what we render to that channel */
2691 swizzle.a = ISL_CHANNEL_SELECT_ALPHA;
2692
2693 return swizzle;
2694 }
2695
2696 void
2697 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm);
2698
2699 /**
2700 * Subsurface of an anv_image.
2701 */
2702 struct anv_surface {
2703 /** Valid only if isl_surf::size_B > 0. */
2704 struct isl_surf isl;
2705
2706 /**
2707 * Offset from VkImage's base address, as bound by vkBindImageMemory().
2708 */
2709 uint32_t offset;
2710 };
2711
2712 struct anv_image {
2713 VkImageType type;
2714 /* The original VkFormat provided by the client. This may not match any
2715 * of the actual surface formats.
2716 */
2717 VkFormat vk_format;
2718 const struct anv_format *format;
2719
2720 VkImageAspectFlags aspects;
2721 VkExtent3D extent;
2722 uint32_t levels;
2723 uint32_t array_size;
2724 uint32_t samples; /**< VkImageCreateInfo::samples */
2725 uint32_t n_planes;
2726 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
2727 VkImageCreateFlags create_flags; /* Flags used when creating image. */
2728 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
2729
2730 /** True if this is needs to be bound to an appropriately tiled BO.
2731 *
2732 * When not using modifiers, consumers such as X11, Wayland, and KMS need
2733 * the tiling passed via I915_GEM_SET_TILING. When exporting these buffers
2734 * we require a dedicated allocation so that we can know to allocate a
2735 * tiled buffer.
2736 */
2737 bool needs_set_tiling;
2738
2739 /**
2740 * Must be DRM_FORMAT_MOD_INVALID unless tiling is
2741 * VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT.
2742 */
2743 uint64_t drm_format_mod;
2744
2745 VkDeviceSize size;
2746 uint32_t alignment;
2747
2748 /* Whether the image is made of several underlying buffer objects rather a
2749 * single one with different offsets.
2750 */
2751 bool disjoint;
2752
2753 /* All the formats that can be used when creating views of this image
2754 * are CCS_E compatible.
2755 */
2756 bool ccs_e_compatible;
2757
2758 /* Image was created with external format. */
2759 bool external_format;
2760
2761 /**
2762 * Image subsurfaces
2763 *
2764 * For each foo, anv_image::planes[x].surface is valid if and only if
2765 * anv_image::aspects has a x aspect. Refer to anv_image_aspect_to_plane()
2766 * to figure the number associated with a given aspect.
2767 *
2768 * The hardware requires that the depth buffer and stencil buffer be
2769 * separate surfaces. From Vulkan's perspective, though, depth and stencil
2770 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
2771 * allocate the depth and stencil buffers as separate surfaces in the same
2772 * bo.
2773 *
2774 * Memory layout :
2775 *
2776 * -----------------------
2777 * | surface0 | /|\
2778 * ----------------------- |
2779 * | shadow surface0 | |
2780 * ----------------------- | Plane 0
2781 * | aux surface0 | |
2782 * ----------------------- |
2783 * | fast clear colors0 | \|/
2784 * -----------------------
2785 * | surface1 | /|\
2786 * ----------------------- |
2787 * | shadow surface1 | |
2788 * ----------------------- | Plane 1
2789 * | aux surface1 | |
2790 * ----------------------- |
2791 * | fast clear colors1 | \|/
2792 * -----------------------
2793 * | ... |
2794 * | |
2795 * -----------------------
2796 */
2797 struct {
2798 /**
2799 * Offset of the entire plane (whenever the image is disjoint this is
2800 * set to 0).
2801 */
2802 uint32_t offset;
2803
2804 VkDeviceSize size;
2805 uint32_t alignment;
2806
2807 struct anv_surface surface;
2808
2809 /**
2810 * A surface which shadows the main surface and may have different
2811 * tiling. This is used for sampling using a tiling that isn't supported
2812 * for other operations.
2813 */
2814 struct anv_surface shadow_surface;
2815
2816 /**
2817 * For color images, this is the aux usage for this image when not used
2818 * as a color attachment.
2819 *
2820 * For depth/stencil images, this is set to ISL_AUX_USAGE_HIZ if the
2821 * image has a HiZ buffer.
2822 */
2823 enum isl_aux_usage aux_usage;
2824
2825 struct anv_surface aux_surface;
2826
2827 /**
2828 * Offset of the fast clear state (used to compute the
2829 * fast_clear_state_offset of the following planes).
2830 */
2831 uint32_t fast_clear_state_offset;
2832
2833 /**
2834 * BO associated with this plane, set when bound.
2835 */
2836 struct anv_address address;
2837
2838 /**
2839 * When destroying the image, also free the bo.
2840 * */
2841 bool bo_is_owned;
2842 } planes[3];
2843 };
2844
2845 /* The ordering of this enum is important */
2846 enum anv_fast_clear_type {
2847 /** Image does not have/support any fast-clear blocks */
2848 ANV_FAST_CLEAR_NONE = 0,
2849 /** Image has/supports fast-clear but only to the default value */
2850 ANV_FAST_CLEAR_DEFAULT_VALUE = 1,
2851 /** Image has/supports fast-clear with an arbitrary fast-clear value */
2852 ANV_FAST_CLEAR_ANY = 2,
2853 };
2854
2855 /* Returns the number of auxiliary buffer levels attached to an image. */
2856 static inline uint8_t
2857 anv_image_aux_levels(const struct anv_image * const image,
2858 VkImageAspectFlagBits aspect)
2859 {
2860 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
2861 return image->planes[plane].aux_surface.isl.size_B > 0 ?
2862 image->planes[plane].aux_surface.isl.levels : 0;
2863 }
2864
2865 /* Returns the number of auxiliary buffer layers attached to an image. */
2866 static inline uint32_t
2867 anv_image_aux_layers(const struct anv_image * const image,
2868 VkImageAspectFlagBits aspect,
2869 const uint8_t miplevel)
2870 {
2871 assert(image);
2872
2873 /* The miplevel must exist in the main buffer. */
2874 assert(miplevel < image->levels);
2875
2876 if (miplevel >= anv_image_aux_levels(image, aspect)) {
2877 /* There are no layers with auxiliary data because the miplevel has no
2878 * auxiliary data.
2879 */
2880 return 0;
2881 } else {
2882 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
2883 return MAX2(image->planes[plane].aux_surface.isl.logical_level0_px.array_len,
2884 image->planes[plane].aux_surface.isl.logical_level0_px.depth >> miplevel);
2885 }
2886 }
2887
2888 static inline struct anv_address
2889 anv_image_get_clear_color_addr(const struct anv_device *device,
2890 const struct anv_image *image,
2891 VkImageAspectFlagBits aspect)
2892 {
2893 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
2894
2895 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
2896 return anv_address_add(image->planes[plane].address,
2897 image->planes[plane].fast_clear_state_offset);
2898 }
2899
2900 static inline struct anv_address
2901 anv_image_get_fast_clear_type_addr(const struct anv_device *device,
2902 const struct anv_image *image,
2903 VkImageAspectFlagBits aspect)
2904 {
2905 struct anv_address addr =
2906 anv_image_get_clear_color_addr(device, image, aspect);
2907
2908 const unsigned clear_color_state_size = device->info.gen >= 10 ?
2909 device->isl_dev.ss.clear_color_state_size :
2910 device->isl_dev.ss.clear_value_size;
2911 return anv_address_add(addr, clear_color_state_size);
2912 }
2913
2914 static inline struct anv_address
2915 anv_image_get_compression_state_addr(const struct anv_device *device,
2916 const struct anv_image *image,
2917 VkImageAspectFlagBits aspect,
2918 uint32_t level, uint32_t array_layer)
2919 {
2920 assert(level < anv_image_aux_levels(image, aspect));
2921 assert(array_layer < anv_image_aux_layers(image, aspect, level));
2922 UNUSED uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
2923 assert(image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E);
2924
2925 struct anv_address addr =
2926 anv_image_get_fast_clear_type_addr(device, image, aspect);
2927 addr.offset += 4; /* Go past the fast clear type */
2928
2929 if (image->type == VK_IMAGE_TYPE_3D) {
2930 for (uint32_t l = 0; l < level; l++)
2931 addr.offset += anv_minify(image->extent.depth, l) * 4;
2932 } else {
2933 addr.offset += level * image->array_size * 4;
2934 }
2935 addr.offset += array_layer * 4;
2936
2937 return addr;
2938 }
2939
2940 /* Returns true if a HiZ-enabled depth buffer can be sampled from. */
2941 static inline bool
2942 anv_can_sample_with_hiz(const struct gen_device_info * const devinfo,
2943 const struct anv_image *image)
2944 {
2945 if (!(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
2946 return false;
2947
2948 if (devinfo->gen < 8)
2949 return false;
2950
2951 return image->samples == 1;
2952 }
2953
2954 void
2955 anv_cmd_buffer_mark_image_written(struct anv_cmd_buffer *cmd_buffer,
2956 const struct anv_image *image,
2957 VkImageAspectFlagBits aspect,
2958 enum isl_aux_usage aux_usage,
2959 uint32_t level,
2960 uint32_t base_layer,
2961 uint32_t layer_count);
2962
2963 void
2964 anv_image_clear_color(struct anv_cmd_buffer *cmd_buffer,
2965 const struct anv_image *image,
2966 VkImageAspectFlagBits aspect,
2967 enum isl_aux_usage aux_usage,
2968 enum isl_format format, struct isl_swizzle swizzle,
2969 uint32_t level, uint32_t base_layer, uint32_t layer_count,
2970 VkRect2D area, union isl_color_value clear_color);
2971 void
2972 anv_image_clear_depth_stencil(struct anv_cmd_buffer *cmd_buffer,
2973 const struct anv_image *image,
2974 VkImageAspectFlags aspects,
2975 enum isl_aux_usage depth_aux_usage,
2976 uint32_t level,
2977 uint32_t base_layer, uint32_t layer_count,
2978 VkRect2D area,
2979 float depth_value, uint8_t stencil_value);
2980 void
2981 anv_image_msaa_resolve(struct anv_cmd_buffer *cmd_buffer,
2982 const struct anv_image *src_image,
2983 enum isl_aux_usage src_aux_usage,
2984 uint32_t src_level, uint32_t src_base_layer,
2985 const struct anv_image *dst_image,
2986 enum isl_aux_usage dst_aux_usage,
2987 uint32_t dst_level, uint32_t dst_base_layer,
2988 VkImageAspectFlagBits aspect,
2989 uint32_t src_x, uint32_t src_y,
2990 uint32_t dst_x, uint32_t dst_y,
2991 uint32_t width, uint32_t height,
2992 uint32_t layer_count,
2993 enum blorp_filter filter);
2994 void
2995 anv_image_hiz_op(struct anv_cmd_buffer *cmd_buffer,
2996 const struct anv_image *image,
2997 VkImageAspectFlagBits aspect, uint32_t level,
2998 uint32_t base_layer, uint32_t layer_count,
2999 enum isl_aux_op hiz_op);
3000 void
3001 anv_image_hiz_clear(struct anv_cmd_buffer *cmd_buffer,
3002 const struct anv_image *image,
3003 VkImageAspectFlags aspects,
3004 uint32_t level,
3005 uint32_t base_layer, uint32_t layer_count,
3006 VkRect2D area, uint8_t stencil_value);
3007 void
3008 anv_image_mcs_op(struct anv_cmd_buffer *cmd_buffer,
3009 const struct anv_image *image,
3010 enum isl_format format,
3011 VkImageAspectFlagBits aspect,
3012 uint32_t base_layer, uint32_t layer_count,
3013 enum isl_aux_op mcs_op, union isl_color_value *clear_value,
3014 bool predicate);
3015 void
3016 anv_image_ccs_op(struct anv_cmd_buffer *cmd_buffer,
3017 const struct anv_image *image,
3018 enum isl_format format,
3019 VkImageAspectFlagBits aspect, uint32_t level,
3020 uint32_t base_layer, uint32_t layer_count,
3021 enum isl_aux_op ccs_op, union isl_color_value *clear_value,
3022 bool predicate);
3023
3024 void
3025 anv_image_copy_to_shadow(struct anv_cmd_buffer *cmd_buffer,
3026 const struct anv_image *image,
3027 uint32_t base_level, uint32_t level_count,
3028 uint32_t base_layer, uint32_t layer_count);
3029
3030 enum isl_aux_usage
3031 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
3032 const struct anv_image *image,
3033 const VkImageAspectFlagBits aspect,
3034 const VkImageLayout layout);
3035
3036 enum anv_fast_clear_type
3037 anv_layout_to_fast_clear_type(const struct gen_device_info * const devinfo,
3038 const struct anv_image * const image,
3039 const VkImageAspectFlagBits aspect,
3040 const VkImageLayout layout);
3041
3042 /* This is defined as a macro so that it works for both
3043 * VkImageSubresourceRange and VkImageSubresourceLayers
3044 */
3045 #define anv_get_layerCount(_image, _range) \
3046 ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
3047 (_image)->array_size - (_range)->baseArrayLayer : (_range)->layerCount)
3048
3049 static inline uint32_t
3050 anv_get_levelCount(const struct anv_image *image,
3051 const VkImageSubresourceRange *range)
3052 {
3053 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
3054 image->levels - range->baseMipLevel : range->levelCount;
3055 }
3056
3057 static inline VkImageAspectFlags
3058 anv_image_expand_aspects(const struct anv_image *image,
3059 VkImageAspectFlags aspects)
3060 {
3061 /* If the underlying image has color plane aspects and
3062 * VK_IMAGE_ASPECT_COLOR_BIT has been requested, then return the aspects of
3063 * the underlying image. */
3064 if ((image->aspects & VK_IMAGE_ASPECT_PLANES_BITS_ANV) != 0 &&
3065 aspects == VK_IMAGE_ASPECT_COLOR_BIT)
3066 return image->aspects;
3067
3068 return aspects;
3069 }
3070
3071 static inline bool
3072 anv_image_aspects_compatible(VkImageAspectFlags aspects1,
3073 VkImageAspectFlags aspects2)
3074 {
3075 if (aspects1 == aspects2)
3076 return true;
3077
3078 /* Only 1 color aspects are compatibles. */
3079 if ((aspects1 & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) != 0 &&
3080 (aspects2 & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) != 0 &&
3081 util_bitcount(aspects1) == util_bitcount(aspects2))
3082 return true;
3083
3084 return false;
3085 }
3086
3087 struct anv_image_view {
3088 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
3089
3090 VkImageAspectFlags aspect_mask;
3091 VkFormat vk_format;
3092 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
3093
3094 unsigned n_planes;
3095 struct {
3096 uint32_t image_plane;
3097
3098 struct isl_view isl;
3099
3100 /**
3101 * RENDER_SURFACE_STATE when using image as a sampler surface with an
3102 * image layout of SHADER_READ_ONLY_OPTIMAL or
3103 * DEPTH_STENCIL_READ_ONLY_OPTIMAL.
3104 */
3105 struct anv_surface_state optimal_sampler_surface_state;
3106
3107 /**
3108 * RENDER_SURFACE_STATE when using image as a sampler surface with an
3109 * image layout of GENERAL.
3110 */
3111 struct anv_surface_state general_sampler_surface_state;
3112
3113 /**
3114 * RENDER_SURFACE_STATE when using image as a storage image. Separate
3115 * states for write-only and readable, using the real format for
3116 * write-only and the lowered format for readable.
3117 */
3118 struct anv_surface_state storage_surface_state;
3119 struct anv_surface_state writeonly_storage_surface_state;
3120
3121 struct brw_image_param storage_image_param;
3122 } planes[3];
3123 };
3124
3125 enum anv_image_view_state_flags {
3126 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY = (1 << 0),
3127 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL = (1 << 1),
3128 };
3129
3130 void anv_image_fill_surface_state(struct anv_device *device,
3131 const struct anv_image *image,
3132 VkImageAspectFlagBits aspect,
3133 const struct isl_view *view,
3134 isl_surf_usage_flags_t view_usage,
3135 enum isl_aux_usage aux_usage,
3136 const union isl_color_value *clear_color,
3137 enum anv_image_view_state_flags flags,
3138 struct anv_surface_state *state_inout,
3139 struct brw_image_param *image_param_out);
3140
3141 struct anv_image_create_info {
3142 const VkImageCreateInfo *vk_info;
3143
3144 /** An opt-in bitmask which filters an ISL-mapping of the Vulkan tiling. */
3145 isl_tiling_flags_t isl_tiling_flags;
3146
3147 /** These flags will be added to any derived from VkImageCreateInfo. */
3148 isl_surf_usage_flags_t isl_extra_usage_flags;
3149
3150 uint32_t stride;
3151 bool external_format;
3152 };
3153
3154 VkResult anv_image_create(VkDevice _device,
3155 const struct anv_image_create_info *info,
3156 const VkAllocationCallbacks* alloc,
3157 VkImage *pImage);
3158
3159 const struct anv_surface *
3160 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
3161 VkImageAspectFlags aspect_mask);
3162
3163 enum isl_format
3164 anv_isl_format_for_descriptor_type(VkDescriptorType type);
3165
3166 static inline struct VkExtent3D
3167 anv_sanitize_image_extent(const VkImageType imageType,
3168 const struct VkExtent3D imageExtent)
3169 {
3170 switch (imageType) {
3171 case VK_IMAGE_TYPE_1D:
3172 return (VkExtent3D) { imageExtent.width, 1, 1 };
3173 case VK_IMAGE_TYPE_2D:
3174 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
3175 case VK_IMAGE_TYPE_3D:
3176 return imageExtent;
3177 default:
3178 unreachable("invalid image type");
3179 }
3180 }
3181
3182 static inline struct VkOffset3D
3183 anv_sanitize_image_offset(const VkImageType imageType,
3184 const struct VkOffset3D imageOffset)
3185 {
3186 switch (imageType) {
3187 case VK_IMAGE_TYPE_1D:
3188 return (VkOffset3D) { imageOffset.x, 0, 0 };
3189 case VK_IMAGE_TYPE_2D:
3190 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
3191 case VK_IMAGE_TYPE_3D:
3192 return imageOffset;
3193 default:
3194 unreachable("invalid image type");
3195 }
3196 }
3197
3198 VkFormatFeatureFlags
3199 anv_get_image_format_features(const struct gen_device_info *devinfo,
3200 VkFormat vk_format,
3201 const struct anv_format *anv_format,
3202 VkImageTiling vk_tiling);
3203
3204 void anv_fill_buffer_surface_state(struct anv_device *device,
3205 struct anv_state state,
3206 enum isl_format format,
3207 struct anv_address address,
3208 uint32_t range, uint32_t stride);
3209
3210 static inline void
3211 anv_clear_color_from_att_state(union isl_color_value *clear_color,
3212 const struct anv_attachment_state *att_state,
3213 const struct anv_image_view *iview)
3214 {
3215 const struct isl_format_layout *view_fmtl =
3216 isl_format_get_layout(iview->planes[0].isl.format);
3217
3218 #define COPY_CLEAR_COLOR_CHANNEL(c, i) \
3219 if (view_fmtl->channels.c.bits) \
3220 clear_color->u32[i] = att_state->clear_value.color.uint32[i]
3221
3222 COPY_CLEAR_COLOR_CHANNEL(r, 0);
3223 COPY_CLEAR_COLOR_CHANNEL(g, 1);
3224 COPY_CLEAR_COLOR_CHANNEL(b, 2);
3225 COPY_CLEAR_COLOR_CHANNEL(a, 3);
3226
3227 #undef COPY_CLEAR_COLOR_CHANNEL
3228 }
3229
3230
3231 struct anv_ycbcr_conversion {
3232 const struct anv_format * format;
3233 VkSamplerYcbcrModelConversion ycbcr_model;
3234 VkSamplerYcbcrRange ycbcr_range;
3235 VkComponentSwizzle mapping[4];
3236 VkChromaLocation chroma_offsets[2];
3237 VkFilter chroma_filter;
3238 bool chroma_reconstruction;
3239 };
3240
3241 struct anv_sampler {
3242 uint32_t state[3][4];
3243 uint32_t n_planes;
3244 struct anv_ycbcr_conversion *conversion;
3245 };
3246
3247 struct anv_framebuffer {
3248 uint32_t width;
3249 uint32_t height;
3250 uint32_t layers;
3251
3252 uint32_t attachment_count;
3253 struct anv_image_view * attachments[0];
3254 };
3255
3256 struct anv_subpass_attachment {
3257 VkImageUsageFlagBits usage;
3258 uint32_t attachment;
3259 VkImageLayout layout;
3260 };
3261
3262 struct anv_subpass {
3263 uint32_t attachment_count;
3264
3265 /**
3266 * A pointer to all attachment references used in this subpass.
3267 * Only valid if ::attachment_count > 0.
3268 */
3269 struct anv_subpass_attachment * attachments;
3270 uint32_t input_count;
3271 struct anv_subpass_attachment * input_attachments;
3272 uint32_t color_count;
3273 struct anv_subpass_attachment * color_attachments;
3274 struct anv_subpass_attachment * resolve_attachments;
3275
3276 struct anv_subpass_attachment * depth_stencil_attachment;
3277 struct anv_subpass_attachment * ds_resolve_attachment;
3278 VkResolveModeFlagBitsKHR depth_resolve_mode;
3279 VkResolveModeFlagBitsKHR stencil_resolve_mode;
3280
3281 uint32_t view_mask;
3282
3283 /** Subpass has a depth/stencil self-dependency */
3284 bool has_ds_self_dep;
3285
3286 /** Subpass has at least one color resolve attachment */
3287 bool has_color_resolve;
3288 };
3289
3290 static inline unsigned
3291 anv_subpass_view_count(const struct anv_subpass *subpass)
3292 {
3293 return MAX2(1, util_bitcount(subpass->view_mask));
3294 }
3295
3296 struct anv_render_pass_attachment {
3297 /* TODO: Consider using VkAttachmentDescription instead of storing each of
3298 * its members individually.
3299 */
3300 VkFormat format;
3301 uint32_t samples;
3302 VkImageUsageFlags usage;
3303 VkAttachmentLoadOp load_op;
3304 VkAttachmentStoreOp store_op;
3305 VkAttachmentLoadOp stencil_load_op;
3306 VkImageLayout initial_layout;
3307 VkImageLayout final_layout;
3308 VkImageLayout first_subpass_layout;
3309
3310 /* The subpass id in which the attachment will be used last. */
3311 uint32_t last_subpass_idx;
3312 };
3313
3314 struct anv_render_pass {
3315 uint32_t attachment_count;
3316 uint32_t subpass_count;
3317 /* An array of subpass_count+1 flushes, one per subpass boundary */
3318 enum anv_pipe_bits * subpass_flushes;
3319 struct anv_render_pass_attachment * attachments;
3320 struct anv_subpass subpasses[0];
3321 };
3322
3323 #define ANV_PIPELINE_STATISTICS_MASK 0x000007ff
3324
3325 struct anv_query_pool {
3326 VkQueryType type;
3327 VkQueryPipelineStatisticFlags pipeline_statistics;
3328 /** Stride between slots, in bytes */
3329 uint32_t stride;
3330 /** Number of slots in this query pool */
3331 uint32_t slots;
3332 struct anv_bo bo;
3333 };
3334
3335 int anv_get_instance_entrypoint_index(const char *name);
3336 int anv_get_device_entrypoint_index(const char *name);
3337
3338 bool
3339 anv_instance_entrypoint_is_enabled(int index, uint32_t core_version,
3340 const struct anv_instance_extension_table *instance);
3341
3342 bool
3343 anv_device_entrypoint_is_enabled(int index, uint32_t core_version,
3344 const struct anv_instance_extension_table *instance,
3345 const struct anv_device_extension_table *device);
3346
3347 void *anv_lookup_entrypoint(const struct gen_device_info *devinfo,
3348 const char *name);
3349
3350 void anv_dump_image_to_ppm(struct anv_device *device,
3351 struct anv_image *image, unsigned miplevel,
3352 unsigned array_layer, VkImageAspectFlagBits aspect,
3353 const char *filename);
3354
3355 enum anv_dump_action {
3356 ANV_DUMP_FRAMEBUFFERS_BIT = 0x1,
3357 };
3358
3359 void anv_dump_start(struct anv_device *device, enum anv_dump_action actions);
3360 void anv_dump_finish(void);
3361
3362 void anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
3363 struct anv_framebuffer *fb);
3364
3365 static inline uint32_t
3366 anv_get_subpass_id(const struct anv_cmd_state * const cmd_state)
3367 {
3368 /* This function must be called from within a subpass. */
3369 assert(cmd_state->pass && cmd_state->subpass);
3370
3371 const uint32_t subpass_id = cmd_state->subpass - cmd_state->pass->subpasses;
3372
3373 /* The id of this subpass shouldn't exceed the number of subpasses in this
3374 * render pass minus 1.
3375 */
3376 assert(subpass_id < cmd_state->pass->subpass_count);
3377 return subpass_id;
3378 }
3379
3380 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
3381 \
3382 static inline struct __anv_type * \
3383 __anv_type ## _from_handle(__VkType _handle) \
3384 { \
3385 return (struct __anv_type *) _handle; \
3386 } \
3387 \
3388 static inline __VkType \
3389 __anv_type ## _to_handle(struct __anv_type *_obj) \
3390 { \
3391 return (__VkType) _obj; \
3392 }
3393
3394 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
3395 \
3396 static inline struct __anv_type * \
3397 __anv_type ## _from_handle(__VkType _handle) \
3398 { \
3399 return (struct __anv_type *)(uintptr_t) _handle; \
3400 } \
3401 \
3402 static inline __VkType \
3403 __anv_type ## _to_handle(struct __anv_type *_obj) \
3404 { \
3405 return (__VkType)(uintptr_t) _obj; \
3406 }
3407
3408 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
3409 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
3410
3411 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
3412 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
3413 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
3414 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
3415 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
3416
3417 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
3418 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
3419 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView)
3420 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool, VkDescriptorPool)
3421 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
3422 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
3423 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_update_template, VkDescriptorUpdateTemplate)
3424 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
3425 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
3426 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event, VkEvent)
3427 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
3428 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
3429 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
3430 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache, VkPipelineCache)
3431 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
3432 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
3433 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
3434 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
3435 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
3436 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_semaphore, VkSemaphore)
3437 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
3438 ANV_DEFINE_NONDISP_HANDLE_CASTS(vk_debug_report_callback, VkDebugReportCallbackEXT)
3439 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_ycbcr_conversion, VkSamplerYcbcrConversion)
3440
3441 /* Gen-specific function declarations */
3442 #ifdef genX
3443 # include "anv_genX.h"
3444 #else
3445 # define genX(x) gen7_##x
3446 # include "anv_genX.h"
3447 # undef genX
3448 # define genX(x) gen75_##x
3449 # include "anv_genX.h"
3450 # undef genX
3451 # define genX(x) gen8_##x
3452 # include "anv_genX.h"
3453 # undef genX
3454 # define genX(x) gen9_##x
3455 # include "anv_genX.h"
3456 # undef genX
3457 # define genX(x) gen10_##x
3458 # include "anv_genX.h"
3459 # undef genX
3460 # define genX(x) gen11_##x
3461 # include "anv_genX.h"
3462 # undef genX
3463 #endif
3464
3465 #endif /* ANV_PRIVATE_H */