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