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