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