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