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