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