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