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