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