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