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