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