anv: add anv_batch_set_error() and anv_batch_has_error() helpers
[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_device_info.h"
45 #include "blorp/blorp.h"
46 #include "compiler/brw_compiler.h"
47 #include "util/macros.h"
48 #include "util/list.h"
49 #include "util/u_vector.h"
50 #include "util/vk_alloc.h"
51
52 /* Pre-declarations needed for WSI entrypoints */
53 struct wl_surface;
54 struct wl_display;
55 typedef struct xcb_connection_t xcb_connection_t;
56 typedef uint32_t xcb_visualid_t;
57 typedef uint32_t xcb_window_t;
58
59 struct anv_buffer;
60 struct anv_buffer_view;
61 struct anv_image_view;
62
63 struct gen_l3_config;
64
65 #include <vulkan/vulkan.h>
66 #include <vulkan/vulkan_intel.h>
67 #include <vulkan/vk_icd.h>
68
69 #include "anv_entrypoints.h"
70 #include "isl/isl.h"
71
72 #include "common/gen_debug.h"
73 #include "wsi_common.h"
74
75 /* Allowing different clear colors requires us to perform a depth resolve at
76 * the end of certain render passes. This is because while slow clears store
77 * the clear color in the HiZ buffer, fast clears (without a resolve) don't.
78 * See the PRMs for examples describing when additional resolves would be
79 * necessary. To enable fast clears without requiring extra resolves, we set
80 * the clear value to a globally-defined one. We could allow different values
81 * if the user doesn't expect coherent data during or after a render passes
82 * (VK_ATTACHMENT_STORE_OP_DONT_CARE), but such users (aside from the CTS)
83 * don't seem to exist yet. In almost all Vulkan applications tested thus far,
84 * 1.0f seems to be the only value used. The only application that doesn't set
85 * this value does so through the usage of an seemingly uninitialized clear
86 * value.
87 */
88 #define ANV_HZ_FC_VAL 1.0f
89
90 #define MAX_VBS 31
91 #define MAX_SETS 8
92 #define MAX_RTS 8
93 #define MAX_VIEWPORTS 16
94 #define MAX_SCISSORS 16
95 #define MAX_PUSH_CONSTANTS_SIZE 128
96 #define MAX_DYNAMIC_BUFFERS 16
97 #define MAX_IMAGES 8
98 #define MAX_PUSH_DESCRIPTORS 32 /* Minimum requirement */
99
100 #define ANV_SVGS_VB_INDEX MAX_VBS
101 #define ANV_DRAWID_VB_INDEX (MAX_VBS + 1)
102
103 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
104
105 static inline uint32_t
106 align_down_npot_u32(uint32_t v, uint32_t a)
107 {
108 return v - (v % a);
109 }
110
111 static inline uint32_t
112 align_u32(uint32_t v, uint32_t a)
113 {
114 assert(a != 0 && a == (a & -a));
115 return (v + a - 1) & ~(a - 1);
116 }
117
118 static inline uint64_t
119 align_u64(uint64_t v, uint64_t a)
120 {
121 assert(a != 0 && a == (a & -a));
122 return (v + a - 1) & ~(a - 1);
123 }
124
125 static inline int32_t
126 align_i32(int32_t v, int32_t a)
127 {
128 assert(a != 0 && a == (a & -a));
129 return (v + a - 1) & ~(a - 1);
130 }
131
132 /** Alignment must be a power of 2. */
133 static inline bool
134 anv_is_aligned(uintmax_t n, uintmax_t a)
135 {
136 assert(a == (a & -a));
137 return (n & (a - 1)) == 0;
138 }
139
140 static inline uint32_t
141 anv_minify(uint32_t n, uint32_t levels)
142 {
143 if (unlikely(n == 0))
144 return 0;
145 else
146 return MAX2(n >> levels, 1);
147 }
148
149 static inline float
150 anv_clamp_f(float f, float min, float max)
151 {
152 assert(min < max);
153
154 if (f > max)
155 return max;
156 else if (f < min)
157 return min;
158 else
159 return f;
160 }
161
162 static inline bool
163 anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
164 {
165 if (*inout_mask & clear_mask) {
166 *inout_mask &= ~clear_mask;
167 return true;
168 } else {
169 return false;
170 }
171 }
172
173 static inline union isl_color_value
174 vk_to_isl_color(VkClearColorValue color)
175 {
176 return (union isl_color_value) {
177 .u32 = {
178 color.uint32[0],
179 color.uint32[1],
180 color.uint32[2],
181 color.uint32[3],
182 },
183 };
184 }
185
186 #define for_each_bit(b, dword) \
187 for (uint32_t __dword = (dword); \
188 (b) = __builtin_ffs(__dword) - 1, __dword; \
189 __dword &= ~(1 << (b)))
190
191 #define typed_memcpy(dest, src, count) ({ \
192 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
193 memcpy((dest), (src), (count) * sizeof(*(src))); \
194 })
195
196 /* Whenever we generate an error, pass it through this function. Useful for
197 * debugging, where we can break on it. Only call at error site, not when
198 * propagating errors. Might be useful to plug in a stack trace here.
199 */
200
201 VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
202
203 #ifdef DEBUG
204 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
205 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
206 #define anv_debug(format, ...) fprintf(stderr, "debug: " format, ##__VA_ARGS__)
207 #else
208 #define vk_error(error) error
209 #define vk_errorf(error, format, ...) error
210 #define anv_debug(format, ...)
211 #endif
212
213 /**
214 * Warn on ignored extension structs.
215 *
216 * The Vulkan spec requires us to ignore unsupported or unknown structs in
217 * a pNext chain. In debug mode, emitting warnings for ignored structs may
218 * help us discover structs that we should not have ignored.
219 *
220 *
221 * From the Vulkan 1.0.38 spec:
222 *
223 * Any component of the implementation (the loader, any enabled layers,
224 * and drivers) must skip over, without processing (other than reading the
225 * sType and pNext members) any chained structures with sType values not
226 * defined by extensions supported by that component.
227 */
228 #define anv_debug_ignored_stype(sType) \
229 anv_debug("debug: %s: ignored VkStructureType %u\n", __func__, (sType))
230
231 void __anv_finishme(const char *file, int line, const char *format, ...)
232 anv_printflike(3, 4);
233 void __anv_perf_warn(const char *file, int line, const char *format, ...)
234 anv_printflike(3, 4);
235 void anv_loge(const char *format, ...) anv_printflike(1, 2);
236 void anv_loge_v(const char *format, va_list va);
237
238 /**
239 * Print a FINISHME message, including its source location.
240 */
241 #define anv_finishme(format, ...) \
242 do { \
243 static bool reported = false; \
244 if (!reported) { \
245 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
246 reported = true; \
247 } \
248 } while (0)
249
250 /**
251 * Print a perf warning message. Set INTEL_DEBUG=perf to see these.
252 */
253 #define anv_perf_warn(format, ...) \
254 do { \
255 static bool reported = false; \
256 if (!reported && unlikely(INTEL_DEBUG & DEBUG_PERF)) { \
257 __anv_perf_warn(__FILE__, __LINE__, format, ##__VA_ARGS__); \
258 reported = true; \
259 } \
260 } while (0)
261
262 /* A non-fatal assert. Useful for debugging. */
263 #ifdef DEBUG
264 #define anv_assert(x) ({ \
265 if (unlikely(!(x))) \
266 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
267 })
268 #else
269 #define anv_assert(x)
270 #endif
271
272 /**
273 * A dynamically growable, circular buffer. Elements are added at head and
274 * removed from tail. head and tail are free-running uint32_t indices and we
275 * only compute the modulo with size when accessing the array. This way,
276 * number of bytes in the queue is always head - tail, even in case of
277 * wraparound.
278 */
279
280 struct anv_bo {
281 uint32_t gem_handle;
282
283 /* Index into the current validation list. This is used by the
284 * validation list building alrogithm to track which buffers are already
285 * in the validation list so that we can ensure uniqueness.
286 */
287 uint32_t index;
288
289 /* Last known offset. This value is provided by the kernel when we
290 * execbuf and is used as the presumed offset for the next bunch of
291 * relocations.
292 */
293 uint64_t offset;
294
295 uint64_t size;
296 void *map;
297
298 /* We need to set the WRITE flag on winsys bos so GEM will know we're
299 * writing to them and synchronize uses on other rings (eg if the display
300 * server uses the blitter ring).
301 */
302 bool is_winsys_bo;
303 };
304
305 static inline void
306 anv_bo_init(struct anv_bo *bo, uint32_t gem_handle, uint64_t size)
307 {
308 bo->gem_handle = gem_handle;
309 bo->index = 0;
310 bo->offset = -1;
311 bo->size = size;
312 bo->map = NULL;
313 bo->is_winsys_bo = false;
314 }
315
316 /* Represents a lock-free linked list of "free" things. This is used by
317 * both the block pool and the state pools. Unfortunately, in order to
318 * solve the ABA problem, we can't use a single uint32_t head.
319 */
320 union anv_free_list {
321 struct {
322 int32_t offset;
323
324 /* A simple count that is incremented every time the head changes. */
325 uint32_t count;
326 };
327 uint64_t u64;
328 };
329
330 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
331
332 struct anv_block_state {
333 union {
334 struct {
335 uint32_t next;
336 uint32_t end;
337 };
338 uint64_t u64;
339 };
340 };
341
342 struct anv_block_pool {
343 struct anv_device *device;
344
345 struct anv_bo bo;
346
347 /* The offset from the start of the bo to the "center" of the block
348 * pool. Pointers to allocated blocks are given by
349 * bo.map + center_bo_offset + offsets.
350 */
351 uint32_t center_bo_offset;
352
353 /* Current memory map of the block pool. This pointer may or may not
354 * point to the actual beginning of the block pool memory. If
355 * anv_block_pool_alloc_back has ever been called, then this pointer
356 * will point to the "center" position of the buffer and all offsets
357 * (negative or positive) given out by the block pool alloc functions
358 * will be valid relative to this pointer.
359 *
360 * In particular, map == bo.map + center_offset
361 */
362 void *map;
363 int fd;
364
365 /**
366 * Array of mmaps and gem handles owned by the block pool, reclaimed when
367 * the block pool is destroyed.
368 */
369 struct u_vector mmap_cleanups;
370
371 uint32_t block_size;
372
373 union anv_free_list free_list;
374 struct anv_block_state state;
375
376 union anv_free_list back_free_list;
377 struct anv_block_state back_state;
378 };
379
380 /* Block pools are backed by a fixed-size 2GB memfd */
381 #define BLOCK_POOL_MEMFD_SIZE (1ul << 31)
382
383 /* The center of the block pool is also the middle of the memfd. This may
384 * change in the future if we decide differently for some reason.
385 */
386 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
387
388 static inline uint32_t
389 anv_block_pool_size(struct anv_block_pool *pool)
390 {
391 return pool->state.end + pool->back_state.end;
392 }
393
394 struct anv_state {
395 int32_t offset;
396 uint32_t alloc_size;
397 void *map;
398 };
399
400 struct anv_fixed_size_state_pool {
401 size_t state_size;
402 union anv_free_list free_list;
403 struct anv_block_state block;
404 };
405
406 #define ANV_MIN_STATE_SIZE_LOG2 6
407 #define ANV_MAX_STATE_SIZE_LOG2 20
408
409 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2 + 1)
410
411 struct anv_state_pool {
412 struct anv_block_pool *block_pool;
413 struct anv_fixed_size_state_pool buckets[ANV_STATE_BUCKETS];
414 };
415
416 struct anv_state_stream_block;
417
418 struct anv_state_stream {
419 struct anv_block_pool *block_pool;
420
421 /* The current working block */
422 struct anv_state_stream_block *block;
423
424 /* Offset at which the current block starts */
425 uint32_t start;
426 /* Offset at which to allocate the next state */
427 uint32_t next;
428 /* Offset at which the current block ends */
429 uint32_t end;
430 };
431
432 #define CACHELINE_SIZE 64
433 #define CACHELINE_MASK 63
434
435 static inline void
436 anv_clflush_range(void *start, size_t size)
437 {
438 void *p = (void *) (((uintptr_t) start) & ~CACHELINE_MASK);
439 void *end = start + size;
440
441 while (p < end) {
442 __builtin_ia32_clflush(p);
443 p += CACHELINE_SIZE;
444 }
445 }
446
447 static inline void
448 anv_flush_range(void *start, size_t size)
449 {
450 __builtin_ia32_mfence();
451 anv_clflush_range(start, size);
452 }
453
454 static inline void
455 anv_invalidate_range(void *start, size_t size)
456 {
457 anv_clflush_range(start, size);
458 __builtin_ia32_mfence();
459 }
460
461 VkResult anv_block_pool_init(struct anv_block_pool *pool,
462 struct anv_device *device, uint32_t block_size);
463 void anv_block_pool_finish(struct anv_block_pool *pool);
464 int32_t anv_block_pool_alloc(struct anv_block_pool *pool);
465 int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool);
466 void anv_block_pool_free(struct anv_block_pool *pool, int32_t offset);
467 void anv_state_pool_init(struct anv_state_pool *pool,
468 struct anv_block_pool *block_pool);
469 void anv_state_pool_finish(struct anv_state_pool *pool);
470 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
471 size_t state_size, size_t alignment);
472 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
473 void anv_state_stream_init(struct anv_state_stream *stream,
474 struct anv_block_pool *block_pool);
475 void anv_state_stream_finish(struct anv_state_stream *stream);
476 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
477 uint32_t size, uint32_t alignment);
478
479 /**
480 * Implements a pool of re-usable BOs. The interface is identical to that
481 * of block_pool except that each block is its own BO.
482 */
483 struct anv_bo_pool {
484 struct anv_device *device;
485
486 void *free_list[16];
487 };
488
489 void anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device);
490 void anv_bo_pool_finish(struct anv_bo_pool *pool);
491 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo,
492 uint32_t size);
493 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
494
495 struct anv_scratch_bo {
496 bool exists;
497 struct anv_bo bo;
498 };
499
500 struct anv_scratch_pool {
501 /* Indexed by Per-Thread Scratch Space number (the hardware value) and stage */
502 struct anv_scratch_bo bos[16][MESA_SHADER_STAGES];
503 };
504
505 void anv_scratch_pool_init(struct anv_device *device,
506 struct anv_scratch_pool *pool);
507 void anv_scratch_pool_finish(struct anv_device *device,
508 struct anv_scratch_pool *pool);
509 struct anv_bo *anv_scratch_pool_alloc(struct anv_device *device,
510 struct anv_scratch_pool *pool,
511 gl_shader_stage stage,
512 unsigned per_thread_scratch);
513
514 struct anv_physical_device {
515 VK_LOADER_DATA _loader_data;
516
517 struct anv_instance * instance;
518 uint32_t chipset_id;
519 char path[20];
520 const char * name;
521 struct gen_device_info info;
522 uint64_t aperture_size;
523 struct brw_compiler * compiler;
524 struct isl_device isl_dev;
525 int cmd_parser_version;
526
527 uint32_t eu_total;
528 uint32_t subslice_total;
529
530 uint8_t uuid[VK_UUID_SIZE];
531
532 struct wsi_device wsi_device;
533 int local_fd;
534 };
535
536 struct anv_instance {
537 VK_LOADER_DATA _loader_data;
538
539 VkAllocationCallbacks alloc;
540
541 uint32_t apiVersion;
542 int physicalDeviceCount;
543 struct anv_physical_device physicalDevice;
544 };
545
546 VkResult anv_init_wsi(struct anv_physical_device *physical_device);
547 void anv_finish_wsi(struct anv_physical_device *physical_device);
548
549 struct anv_queue {
550 VK_LOADER_DATA _loader_data;
551
552 struct anv_device * device;
553
554 struct anv_state_pool * pool;
555 };
556
557 struct anv_pipeline_cache {
558 struct anv_device * device;
559 pthread_mutex_t mutex;
560
561 struct hash_table * cache;
562 };
563
564 struct anv_pipeline_bind_map;
565
566 void anv_pipeline_cache_init(struct anv_pipeline_cache *cache,
567 struct anv_device *device,
568 bool cache_enabled);
569 void anv_pipeline_cache_finish(struct anv_pipeline_cache *cache);
570
571 struct anv_shader_bin *
572 anv_pipeline_cache_search(struct anv_pipeline_cache *cache,
573 const void *key, uint32_t key_size);
574 struct anv_shader_bin *
575 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache *cache,
576 const void *key_data, uint32_t key_size,
577 const void *kernel_data, uint32_t kernel_size,
578 const struct brw_stage_prog_data *prog_data,
579 uint32_t prog_data_size,
580 const struct anv_pipeline_bind_map *bind_map);
581
582 struct anv_device {
583 VK_LOADER_DATA _loader_data;
584
585 VkAllocationCallbacks alloc;
586
587 struct anv_instance * instance;
588 uint32_t chipset_id;
589 struct gen_device_info info;
590 struct isl_device isl_dev;
591 int context_id;
592 int fd;
593 bool can_chain_batches;
594 bool robust_buffer_access;
595
596 struct anv_bo_pool batch_bo_pool;
597
598 struct anv_block_pool dynamic_state_block_pool;
599 struct anv_state_pool dynamic_state_pool;
600
601 struct anv_block_pool instruction_block_pool;
602 struct anv_state_pool instruction_state_pool;
603
604 struct anv_block_pool surface_state_block_pool;
605 struct anv_state_pool surface_state_pool;
606
607 struct anv_bo workaround_bo;
608
609 struct anv_pipeline_cache blorp_shader_cache;
610 struct blorp_context blorp;
611
612 struct anv_state border_colors;
613
614 struct anv_queue queue;
615
616 struct anv_scratch_pool scratch_pool;
617
618 uint32_t default_mocs;
619
620 pthread_mutex_t mutex;
621 pthread_cond_t queue_submit;
622 };
623
624 static void inline
625 anv_state_flush(struct anv_device *device, struct anv_state state)
626 {
627 if (device->info.has_llc)
628 return;
629
630 anv_flush_range(state.map, state.alloc_size);
631 }
632
633 void anv_device_init_blorp(struct anv_device *device);
634 void anv_device_finish_blorp(struct anv_device *device);
635
636 VkResult anv_device_execbuf(struct anv_device *device,
637 struct drm_i915_gem_execbuffer2 *execbuf,
638 struct anv_bo **execbuf_bos);
639
640 void* anv_gem_mmap(struct anv_device *device,
641 uint32_t gem_handle, uint64_t offset, uint64_t size, uint32_t flags);
642 void anv_gem_munmap(void *p, uint64_t size);
643 uint32_t anv_gem_create(struct anv_device *device, size_t size);
644 void anv_gem_close(struct anv_device *device, uint32_t gem_handle);
645 uint32_t anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
646 int anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns);
647 int anv_gem_execbuffer(struct anv_device *device,
648 struct drm_i915_gem_execbuffer2 *execbuf);
649 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
650 uint32_t stride, uint32_t tiling);
651 int anv_gem_create_context(struct anv_device *device);
652 int anv_gem_destroy_context(struct anv_device *device, int context);
653 int anv_gem_get_param(int fd, uint32_t param);
654 bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling);
655 int anv_gem_get_aperture(int fd, uint64_t *size);
656 int anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle);
657 uint32_t anv_gem_fd_to_handle(struct anv_device *device, int fd);
658 int anv_gem_set_caching(struct anv_device *device, uint32_t gem_handle, uint32_t caching);
659 int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
660 uint32_t read_domains, uint32_t write_domain);
661
662 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
663
664 struct anv_reloc_list {
665 size_t num_relocs;
666 size_t array_length;
667 struct drm_i915_gem_relocation_entry * relocs;
668 struct anv_bo ** reloc_bos;
669 };
670
671 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
672 const VkAllocationCallbacks *alloc);
673 void anv_reloc_list_finish(struct anv_reloc_list *list,
674 const VkAllocationCallbacks *alloc);
675
676 uint64_t anv_reloc_list_add(struct anv_reloc_list *list,
677 const VkAllocationCallbacks *alloc,
678 uint32_t offset, struct anv_bo *target_bo,
679 uint32_t delta);
680
681 struct anv_batch_bo {
682 /* Link in the anv_cmd_buffer.owned_batch_bos list */
683 struct list_head link;
684
685 struct anv_bo bo;
686
687 /* Bytes actually consumed in this batch BO */
688 size_t length;
689
690 struct anv_reloc_list relocs;
691 };
692
693 struct anv_batch {
694 const VkAllocationCallbacks * alloc;
695
696 void * start;
697 void * end;
698 void * next;
699
700 struct anv_reloc_list * relocs;
701
702 /* This callback is called (with the associated user data) in the event
703 * that the batch runs out of space.
704 */
705 VkResult (*extend_cb)(struct anv_batch *, void *);
706 void * user_data;
707
708 /**
709 * Current error status of the command buffer. Used to track inconsistent
710 * or incomplete command buffer states that are the consequence of run-time
711 * errors such as out of memory scenarios. We want to track this in the
712 * batch because the command buffer object is not visible to some parts
713 * of the driver.
714 */
715 VkResult status;
716 };
717
718 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
719 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
720 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
721 void *location, struct anv_bo *bo, uint32_t offset);
722 VkResult anv_device_submit_simple_batch(struct anv_device *device,
723 struct anv_batch *batch);
724
725 static inline VkResult
726 anv_batch_set_error(struct anv_batch *batch, VkResult error)
727 {
728 assert(error != VK_SUCCESS);
729 if (batch->status == VK_SUCCESS)
730 batch->status = error;
731 return batch->status;
732 }
733
734 static inline bool
735 anv_batch_has_error(struct anv_batch *batch)
736 {
737 return batch->status != VK_SUCCESS;
738 }
739
740 struct anv_address {
741 struct anv_bo *bo;
742 uint32_t offset;
743 };
744
745 static inline uint64_t
746 _anv_combine_address(struct anv_batch *batch, void *location,
747 const struct anv_address address, uint32_t delta)
748 {
749 if (address.bo == NULL) {
750 return address.offset + delta;
751 } else {
752 assert(batch->start <= location && location < batch->end);
753
754 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
755 }
756 }
757
758 #define __gen_address_type struct anv_address
759 #define __gen_user_data struct anv_batch
760 #define __gen_combine_address _anv_combine_address
761
762 /* Wrapper macros needed to work around preprocessor argument issues. In
763 * particular, arguments don't get pre-evaluated if they are concatenated.
764 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
765 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
766 * We can work around this easily enough with these helpers.
767 */
768 #define __anv_cmd_length(cmd) cmd ## _length
769 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
770 #define __anv_cmd_header(cmd) cmd ## _header
771 #define __anv_cmd_pack(cmd) cmd ## _pack
772 #define __anv_reg_num(reg) reg ## _num
773
774 #define anv_pack_struct(dst, struc, ...) do { \
775 struct struc __template = { \
776 __VA_ARGS__ \
777 }; \
778 __anv_cmd_pack(struc)(NULL, dst, &__template); \
779 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dst, __anv_cmd_length(struc) * 4)); \
780 } while (0)
781
782 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
783 void *__dst = anv_batch_emit_dwords(batch, n); \
784 struct cmd __template = { \
785 __anv_cmd_header(cmd), \
786 .DWordLength = n - __anv_cmd_length_bias(cmd), \
787 __VA_ARGS__ \
788 }; \
789 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
790 __dst; \
791 })
792
793 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
794 do { \
795 uint32_t *dw; \
796 \
797 STATIC_ASSERT(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
798 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
799 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
800 dw[i] = (dwords0)[i] | (dwords1)[i]; \
801 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
802 } while (0)
803
804 #define anv_batch_emit(batch, cmd, name) \
805 for (struct cmd name = { __anv_cmd_header(cmd) }, \
806 *_dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
807 __builtin_expect(_dst != NULL, 1); \
808 ({ __anv_cmd_pack(cmd)(batch, _dst, &name); \
809 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, __anv_cmd_length(cmd) * 4)); \
810 _dst = NULL; \
811 }))
812
813 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
814 .GraphicsDataTypeGFDT = 0, \
815 .LLCCacheabilityControlLLCCC = 0, \
816 .L3CacheabilityControlL3CC = 1, \
817 }
818
819 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
820 .LLCeLLCCacheabilityControlLLCCC = 0, \
821 .L3CacheabilityControlL3CC = 1, \
822 }
823
824 #define GEN8_MOCS (struct GEN8_MEMORY_OBJECT_CONTROL_STATE) { \
825 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
826 .TargetCache = L3DefertoPATforLLCeLLCselection, \
827 .AgeforQUADLRU = 0 \
828 }
829
830 /* Skylake: MOCS is now an index into an array of 62 different caching
831 * configurations programmed by the kernel.
832 */
833
834 #define GEN9_MOCS (struct GEN9_MEMORY_OBJECT_CONTROL_STATE) { \
835 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
836 .IndextoMOCSTables = 2 \
837 }
838
839 #define GEN9_MOCS_PTE { \
840 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
841 .IndextoMOCSTables = 1 \
842 }
843
844 struct anv_device_memory {
845 struct anv_bo bo;
846 uint32_t type_index;
847 VkDeviceSize map_size;
848 void * map;
849 };
850
851 /**
852 * Header for Vertex URB Entry (VUE)
853 */
854 struct anv_vue_header {
855 uint32_t Reserved;
856 uint32_t RTAIndex; /* RenderTargetArrayIndex */
857 uint32_t ViewportIndex;
858 float PointWidth;
859 };
860
861 struct anv_descriptor_set_binding_layout {
862 #ifndef NDEBUG
863 /* The type of the descriptors in this binding */
864 VkDescriptorType type;
865 #endif
866
867 /* Number of array elements in this binding */
868 uint16_t array_size;
869
870 /* Index into the flattend descriptor set */
871 uint16_t descriptor_index;
872
873 /* Index into the dynamic state array for a dynamic buffer */
874 int16_t dynamic_offset_index;
875
876 /* Index into the descriptor set buffer views */
877 int16_t buffer_index;
878
879 struct {
880 /* Index into the binding table for the associated surface */
881 int16_t surface_index;
882
883 /* Index into the sampler table for the associated sampler */
884 int16_t sampler_index;
885
886 /* Index into the image table for the associated image */
887 int16_t image_index;
888 } stage[MESA_SHADER_STAGES];
889
890 /* Immutable samplers (or NULL if no immutable samplers) */
891 struct anv_sampler **immutable_samplers;
892 };
893
894 struct anv_descriptor_set_layout {
895 /* Number of bindings in this descriptor set */
896 uint16_t binding_count;
897
898 /* Total size of the descriptor set with room for all array entries */
899 uint16_t size;
900
901 /* Shader stages affected by this descriptor set */
902 uint16_t shader_stages;
903
904 /* Number of buffers in this descriptor set */
905 uint16_t buffer_count;
906
907 /* Number of dynamic offsets used by this descriptor set */
908 uint16_t dynamic_offset_count;
909
910 /* Bindings in this descriptor set */
911 struct anv_descriptor_set_binding_layout binding[0];
912 };
913
914 struct anv_descriptor {
915 VkDescriptorType type;
916
917 union {
918 struct {
919 struct anv_image_view *image_view;
920 struct anv_sampler *sampler;
921
922 /* Used to determine whether or not we need the surface state to have
923 * the auxiliary buffer enabled.
924 */
925 enum isl_aux_usage aux_usage;
926 };
927
928 struct {
929 struct anv_buffer *buffer;
930 uint64_t offset;
931 uint64_t range;
932 };
933
934 struct anv_buffer_view *buffer_view;
935 };
936 };
937
938 struct anv_descriptor_set {
939 const struct anv_descriptor_set_layout *layout;
940 uint32_t size;
941 uint32_t buffer_count;
942 struct anv_buffer_view *buffer_views;
943 struct anv_descriptor descriptors[0];
944 };
945
946 struct anv_buffer_view {
947 enum isl_format format; /**< VkBufferViewCreateInfo::format */
948 struct anv_bo *bo;
949 uint32_t offset; /**< Offset into bo. */
950 uint64_t range; /**< VkBufferViewCreateInfo::range */
951
952 struct anv_state surface_state;
953 struct anv_state storage_surface_state;
954 struct anv_state writeonly_storage_surface_state;
955
956 struct brw_image_param storage_image_param;
957 };
958
959 struct anv_push_descriptor_set {
960 struct anv_descriptor_set set;
961
962 /* Put this field right behind anv_descriptor_set so it fills up the
963 * descriptors[0] field. */
964 struct anv_descriptor descriptors[MAX_PUSH_DESCRIPTORS];
965
966 struct anv_buffer_view buffer_views[MAX_PUSH_DESCRIPTORS];
967 };
968
969 struct anv_descriptor_pool {
970 uint32_t size;
971 uint32_t next;
972 uint32_t free_list;
973
974 struct anv_state_stream surface_state_stream;
975 void *surface_state_free_list;
976
977 char data[0];
978 };
979
980 enum anv_descriptor_template_entry_type {
981 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_IMAGE,
982 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER,
983 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER_VIEW
984 };
985
986 struct anv_descriptor_template_entry {
987 /* The type of descriptor in this entry */
988 VkDescriptorType type;
989
990 /* Binding in the descriptor set */
991 uint32_t binding;
992
993 /* Offset at which to write into the descriptor set binding */
994 uint32_t array_element;
995
996 /* Number of elements to write into the descriptor set binding */
997 uint32_t array_count;
998
999 /* Offset into the user provided data */
1000 size_t offset;
1001
1002 /* Stride between elements into the user provided data */
1003 size_t stride;
1004 };
1005
1006 struct anv_descriptor_update_template {
1007 /* The descriptor set this template corresponds to. This value is only
1008 * valid if the template was created with the templateType
1009 * VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR.
1010 */
1011 uint8_t set;
1012
1013 /* Number of entries in this template */
1014 uint32_t entry_count;
1015
1016 /* Entries of the template */
1017 struct anv_descriptor_template_entry entries[0];
1018 };
1019
1020 size_t
1021 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout);
1022
1023 void
1024 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
1025 const struct gen_device_info * const devinfo,
1026 const VkDescriptorImageInfo * const info,
1027 VkDescriptorType type,
1028 uint32_t binding,
1029 uint32_t element);
1030
1031 void
1032 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
1033 VkDescriptorType type,
1034 struct anv_buffer_view *buffer_view,
1035 uint32_t binding,
1036 uint32_t element);
1037
1038 void
1039 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
1040 struct anv_device *device,
1041 struct anv_state_stream *alloc_stream,
1042 VkDescriptorType type,
1043 struct anv_buffer *buffer,
1044 uint32_t binding,
1045 uint32_t element,
1046 VkDeviceSize offset,
1047 VkDeviceSize range);
1048
1049 void
1050 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
1051 struct anv_device *device,
1052 struct anv_state_stream *alloc_stream,
1053 const struct anv_descriptor_update_template *template,
1054 const void *data);
1055
1056 VkResult
1057 anv_descriptor_set_create(struct anv_device *device,
1058 struct anv_descriptor_pool *pool,
1059 const struct anv_descriptor_set_layout *layout,
1060 struct anv_descriptor_set **out_set);
1061
1062 void
1063 anv_descriptor_set_destroy(struct anv_device *device,
1064 struct anv_descriptor_pool *pool,
1065 struct anv_descriptor_set *set);
1066
1067 #define ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS UINT8_MAX
1068
1069 struct anv_pipeline_binding {
1070 /* The descriptor set this surface corresponds to. The special value of
1071 * ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS indicates that the offset refers
1072 * to a color attachment and not a regular descriptor.
1073 */
1074 uint8_t set;
1075
1076 /* Binding in the descriptor set */
1077 uint8_t binding;
1078
1079 /* Index in the binding */
1080 uint8_t index;
1081
1082 /* Input attachment index (relative to the subpass) */
1083 uint8_t input_attachment_index;
1084
1085 /* For a storage image, whether it is write-only */
1086 bool write_only;
1087 };
1088
1089 struct anv_pipeline_layout {
1090 struct {
1091 struct anv_descriptor_set_layout *layout;
1092 uint32_t dynamic_offset_start;
1093 } set[MAX_SETS];
1094
1095 uint32_t num_sets;
1096
1097 struct {
1098 bool has_dynamic_offsets;
1099 } stage[MESA_SHADER_STAGES];
1100
1101 unsigned char sha1[20];
1102 };
1103
1104 struct anv_buffer {
1105 struct anv_device * device;
1106 VkDeviceSize size;
1107
1108 VkBufferUsageFlags usage;
1109
1110 /* Set when bound */
1111 struct anv_bo * bo;
1112 VkDeviceSize offset;
1113 };
1114
1115 static inline uint64_t
1116 anv_buffer_get_range(struct anv_buffer *buffer, uint64_t offset, uint64_t range)
1117 {
1118 assert(offset <= buffer->size);
1119 if (range == VK_WHOLE_SIZE) {
1120 return buffer->size - offset;
1121 } else {
1122 assert(range <= buffer->size);
1123 return range;
1124 }
1125 }
1126
1127 enum anv_cmd_dirty_bits {
1128 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
1129 ANV_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
1130 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
1131 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
1132 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
1133 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
1134 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
1135 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
1136 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
1137 ANV_CMD_DIRTY_DYNAMIC_ALL = (1 << 9) - 1,
1138 ANV_CMD_DIRTY_PIPELINE = 1 << 9,
1139 ANV_CMD_DIRTY_INDEX_BUFFER = 1 << 10,
1140 ANV_CMD_DIRTY_RENDER_TARGETS = 1 << 11,
1141 };
1142 typedef uint32_t anv_cmd_dirty_mask_t;
1143
1144 enum anv_pipe_bits {
1145 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT = (1 << 0),
1146 ANV_PIPE_STALL_AT_SCOREBOARD_BIT = (1 << 1),
1147 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT = (1 << 2),
1148 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT = (1 << 3),
1149 ANV_PIPE_VF_CACHE_INVALIDATE_BIT = (1 << 4),
1150 ANV_PIPE_DATA_CACHE_FLUSH_BIT = (1 << 5),
1151 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT = (1 << 10),
1152 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT = (1 << 11),
1153 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT = (1 << 12),
1154 ANV_PIPE_DEPTH_STALL_BIT = (1 << 13),
1155 ANV_PIPE_CS_STALL_BIT = (1 << 20),
1156
1157 /* This bit does not exist directly in PIPE_CONTROL. Instead it means that
1158 * a flush has happened but not a CS stall. The next time we do any sort
1159 * of invalidation we need to insert a CS stall at that time. Otherwise,
1160 * we would have to CS stall on every flush which could be bad.
1161 */
1162 ANV_PIPE_NEEDS_CS_STALL_BIT = (1 << 21),
1163 };
1164
1165 #define ANV_PIPE_FLUSH_BITS ( \
1166 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT | \
1167 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1168 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1169
1170 #define ANV_PIPE_STALL_BITS ( \
1171 ANV_PIPE_STALL_AT_SCOREBOARD_BIT | \
1172 ANV_PIPE_DEPTH_STALL_BIT | \
1173 ANV_PIPE_CS_STALL_BIT)
1174
1175 #define ANV_PIPE_INVALIDATE_BITS ( \
1176 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT | \
1177 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT | \
1178 ANV_PIPE_VF_CACHE_INVALIDATE_BIT | \
1179 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1180 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
1181 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
1182
1183 struct anv_vertex_binding {
1184 struct anv_buffer * buffer;
1185 VkDeviceSize offset;
1186 };
1187
1188 struct anv_push_constants {
1189 /* Current allocated size of this push constants data structure.
1190 * Because a decent chunk of it may not be used (images on SKL, for
1191 * instance), we won't actually allocate the entire structure up-front.
1192 */
1193 uint32_t size;
1194
1195 /* Push constant data provided by the client through vkPushConstants */
1196 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
1197
1198 /* Our hardware only provides zero-based vertex and instance id so, in
1199 * order to satisfy the vulkan requirements, we may have to push one or
1200 * both of these into the shader.
1201 */
1202 uint32_t base_vertex;
1203 uint32_t base_instance;
1204
1205 /* Image data for image_load_store on pre-SKL */
1206 struct brw_image_param images[MAX_IMAGES];
1207 };
1208
1209 struct anv_dynamic_state {
1210 struct {
1211 uint32_t count;
1212 VkViewport viewports[MAX_VIEWPORTS];
1213 } viewport;
1214
1215 struct {
1216 uint32_t count;
1217 VkRect2D scissors[MAX_SCISSORS];
1218 } scissor;
1219
1220 float line_width;
1221
1222 struct {
1223 float bias;
1224 float clamp;
1225 float slope;
1226 } depth_bias;
1227
1228 float blend_constants[4];
1229
1230 struct {
1231 float min;
1232 float max;
1233 } depth_bounds;
1234
1235 struct {
1236 uint32_t front;
1237 uint32_t back;
1238 } stencil_compare_mask;
1239
1240 struct {
1241 uint32_t front;
1242 uint32_t back;
1243 } stencil_write_mask;
1244
1245 struct {
1246 uint32_t front;
1247 uint32_t back;
1248 } stencil_reference;
1249 };
1250
1251 extern const struct anv_dynamic_state default_dynamic_state;
1252
1253 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
1254 const struct anv_dynamic_state *src,
1255 uint32_t copy_mask);
1256
1257 /**
1258 * Attachment state when recording a renderpass instance.
1259 *
1260 * The clear value is valid only if there exists a pending clear.
1261 */
1262 struct anv_attachment_state {
1263 enum isl_aux_usage aux_usage;
1264 enum isl_aux_usage input_aux_usage;
1265 struct anv_state color_rt_state;
1266 struct anv_state input_att_state;
1267
1268 VkImageLayout current_layout;
1269 VkImageAspectFlags pending_clear_aspects;
1270 bool fast_clear;
1271 VkClearValue clear_value;
1272 bool clear_color_is_zero_one;
1273 };
1274
1275 /** State required while building cmd buffer */
1276 struct anv_cmd_state {
1277 /* PIPELINE_SELECT.PipelineSelection */
1278 uint32_t current_pipeline;
1279 const struct gen_l3_config * current_l3_config;
1280 uint32_t vb_dirty;
1281 anv_cmd_dirty_mask_t dirty;
1282 anv_cmd_dirty_mask_t compute_dirty;
1283 enum anv_pipe_bits pending_pipe_bits;
1284 uint32_t num_workgroups_offset;
1285 struct anv_bo *num_workgroups_bo;
1286 VkShaderStageFlags descriptors_dirty;
1287 VkShaderStageFlags push_constants_dirty;
1288 uint32_t scratch_size;
1289 struct anv_pipeline * pipeline;
1290 struct anv_pipeline * compute_pipeline;
1291 struct anv_framebuffer * framebuffer;
1292 struct anv_render_pass * pass;
1293 struct anv_subpass * subpass;
1294 VkRect2D render_area;
1295 uint32_t restart_index;
1296 struct anv_vertex_binding vertex_bindings[MAX_VBS];
1297 struct anv_descriptor_set * descriptors[MAX_SETS];
1298 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
1299 VkShaderStageFlags push_constant_stages;
1300 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
1301 struct anv_state binding_tables[MESA_SHADER_STAGES];
1302 struct anv_state samplers[MESA_SHADER_STAGES];
1303 struct anv_dynamic_state dynamic;
1304 bool need_query_wa;
1305
1306 struct anv_push_descriptor_set push_descriptor;
1307
1308 /**
1309 * Whether or not the gen8 PMA fix is enabled. We ensure that, at the top
1310 * of any command buffer it is disabled by disabling it in EndCommandBuffer
1311 * and before invoking the secondary in ExecuteCommands.
1312 */
1313 bool pma_fix_enabled;
1314
1315 /**
1316 * Whether or not we know for certain that HiZ is enabled for the current
1317 * subpass. If, for whatever reason, we are unsure as to whether HiZ is
1318 * enabled or not, this will be false.
1319 */
1320 bool hiz_enabled;
1321
1322 /**
1323 * Array length is anv_cmd_state::pass::attachment_count. Array content is
1324 * valid only when recording a render pass instance.
1325 */
1326 struct anv_attachment_state * attachments;
1327
1328 /**
1329 * Surface states for color render targets. These are stored in a single
1330 * flat array. For depth-stencil attachments, the surface state is simply
1331 * left blank.
1332 */
1333 struct anv_state render_pass_states;
1334
1335 /**
1336 * A null surface state of the right size to match the framebuffer. This
1337 * is one of the states in render_pass_states.
1338 */
1339 struct anv_state null_surface_state;
1340
1341 struct {
1342 struct anv_buffer * index_buffer;
1343 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1344 uint32_t index_offset;
1345 } gen7;
1346 };
1347
1348 struct anv_cmd_pool {
1349 VkAllocationCallbacks alloc;
1350 struct list_head cmd_buffers;
1351 };
1352
1353 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1354
1355 enum anv_cmd_buffer_exec_mode {
1356 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
1357 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
1358 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT,
1359 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
1360 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
1361 };
1362
1363 struct anv_cmd_buffer {
1364 VK_LOADER_DATA _loader_data;
1365
1366 struct anv_device * device;
1367
1368 struct anv_cmd_pool * pool;
1369 struct list_head pool_link;
1370
1371 struct anv_batch batch;
1372
1373 /* Fields required for the actual chain of anv_batch_bo's.
1374 *
1375 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1376 */
1377 struct list_head batch_bos;
1378 enum anv_cmd_buffer_exec_mode exec_mode;
1379
1380 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1381 * referenced by this command buffer
1382 *
1383 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1384 */
1385 struct u_vector seen_bbos;
1386
1387 /* A vector of int32_t's for every block of binding tables.
1388 *
1389 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1390 */
1391 struct u_vector bt_blocks;
1392 uint32_t bt_next;
1393
1394 struct anv_reloc_list surface_relocs;
1395 /** Last seen surface state block pool center bo offset */
1396 uint32_t last_ss_pool_center;
1397
1398 /* Serial for tracking buffer completion */
1399 uint32_t serial;
1400
1401 /* Stream objects for storing temporary data */
1402 struct anv_state_stream surface_state_stream;
1403 struct anv_state_stream dynamic_state_stream;
1404
1405 VkCommandBufferUsageFlags usage_flags;
1406 VkCommandBufferLevel level;
1407
1408 struct anv_cmd_state state;
1409 };
1410
1411 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1412 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1413 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1414 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
1415 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
1416 struct anv_cmd_buffer *secondary);
1417 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
1418 VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
1419 struct anv_cmd_buffer *cmd_buffer);
1420
1421 VkResult anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer);
1422
1423 VkResult
1424 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
1425 gl_shader_stage stage, uint32_t size);
1426 #define anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, field) \
1427 anv_cmd_buffer_ensure_push_constants_size(cmd_buffer, stage, \
1428 (offsetof(struct anv_push_constants, field) + \
1429 sizeof(cmd_buffer->state.push_constants[0]->field)))
1430
1431 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
1432 const void *data, uint32_t size, uint32_t alignment);
1433 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
1434 uint32_t *a, uint32_t *b,
1435 uint32_t dwords, uint32_t alignment);
1436
1437 struct anv_address
1438 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1439 struct anv_state
1440 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1441 uint32_t entries, uint32_t *state_offset);
1442 struct anv_state
1443 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1444 struct anv_state
1445 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1446 uint32_t size, uint32_t alignment);
1447
1448 VkResult
1449 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1450
1451 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1452 void gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer *cmd_buffer,
1453 bool depth_clamp_enable);
1454 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1455
1456 void anv_cmd_buffer_setup_attachments(struct anv_cmd_buffer *cmd_buffer,
1457 struct anv_render_pass *pass,
1458 struct anv_framebuffer *framebuffer,
1459 const VkClearValue *clear_values);
1460
1461 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1462
1463 struct anv_state
1464 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1465 gl_shader_stage stage);
1466 struct anv_state
1467 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer);
1468
1469 void anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer *cmd_buffer);
1470 void anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer);
1471
1472 const struct anv_image_view *
1473 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1474
1475 struct anv_state
1476 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
1477 uint32_t num_entries,
1478 uint32_t *state_offset);
1479
1480 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1481
1482 enum anv_fence_state {
1483 /** Indicates that this is a new (or newly reset fence) */
1484 ANV_FENCE_STATE_RESET,
1485
1486 /** Indicates that this fence has been submitted to the GPU but is still
1487 * (as far as we know) in use by the GPU.
1488 */
1489 ANV_FENCE_STATE_SUBMITTED,
1490
1491 ANV_FENCE_STATE_SIGNALED,
1492 };
1493
1494 struct anv_fence {
1495 struct anv_bo bo;
1496 struct drm_i915_gem_execbuffer2 execbuf;
1497 struct drm_i915_gem_exec_object2 exec2_objects[1];
1498 enum anv_fence_state state;
1499 };
1500
1501 struct anv_event {
1502 uint64_t semaphore;
1503 struct anv_state state;
1504 };
1505
1506 struct anv_shader_module {
1507 unsigned char sha1[20];
1508 uint32_t size;
1509 char data[0];
1510 };
1511
1512 void anv_hash_shader(unsigned char *hash, const void *key, size_t key_size,
1513 struct anv_shader_module *module,
1514 const char *entrypoint,
1515 const struct anv_pipeline_layout *pipeline_layout,
1516 const VkSpecializationInfo *spec_info);
1517
1518 static inline gl_shader_stage
1519 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1520 {
1521 assert(__builtin_popcount(vk_stage) == 1);
1522 return ffs(vk_stage) - 1;
1523 }
1524
1525 static inline VkShaderStageFlagBits
1526 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1527 {
1528 return (1 << mesa_stage);
1529 }
1530
1531 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1532
1533 #define anv_foreach_stage(stage, stage_bits) \
1534 for (gl_shader_stage stage, \
1535 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
1536 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1537 __tmp &= ~(1 << (stage)))
1538
1539 struct anv_pipeline_bind_map {
1540 uint32_t surface_count;
1541 uint32_t sampler_count;
1542 uint32_t image_count;
1543
1544 struct anv_pipeline_binding * surface_to_descriptor;
1545 struct anv_pipeline_binding * sampler_to_descriptor;
1546 };
1547
1548 struct anv_shader_bin_key {
1549 uint32_t size;
1550 uint8_t data[0];
1551 };
1552
1553 struct anv_shader_bin {
1554 uint32_t ref_cnt;
1555
1556 const struct anv_shader_bin_key *key;
1557
1558 struct anv_state kernel;
1559 uint32_t kernel_size;
1560
1561 const struct brw_stage_prog_data *prog_data;
1562 uint32_t prog_data_size;
1563
1564 struct anv_pipeline_bind_map bind_map;
1565
1566 /* Prog data follows, then params, then the key, all aligned to 8-bytes */
1567 };
1568
1569 struct anv_shader_bin *
1570 anv_shader_bin_create(struct anv_device *device,
1571 const void *key, uint32_t key_size,
1572 const void *kernel, uint32_t kernel_size,
1573 const struct brw_stage_prog_data *prog_data,
1574 uint32_t prog_data_size, const void *prog_data_param,
1575 const struct anv_pipeline_bind_map *bind_map);
1576
1577 void
1578 anv_shader_bin_destroy(struct anv_device *device, struct anv_shader_bin *shader);
1579
1580 static inline void
1581 anv_shader_bin_ref(struct anv_shader_bin *shader)
1582 {
1583 assert(shader && shader->ref_cnt >= 1);
1584 __sync_fetch_and_add(&shader->ref_cnt, 1);
1585 }
1586
1587 static inline void
1588 anv_shader_bin_unref(struct anv_device *device, struct anv_shader_bin *shader)
1589 {
1590 assert(shader && shader->ref_cnt >= 1);
1591 if (__sync_fetch_and_add(&shader->ref_cnt, -1) == 1)
1592 anv_shader_bin_destroy(device, shader);
1593 }
1594
1595 struct anv_pipeline {
1596 struct anv_device * device;
1597 struct anv_batch batch;
1598 uint32_t batch_data[512];
1599 struct anv_reloc_list batch_relocs;
1600 uint32_t dynamic_state_mask;
1601 struct anv_dynamic_state dynamic_state;
1602
1603 struct anv_pipeline_layout * layout;
1604
1605 bool needs_data_cache;
1606
1607 struct anv_shader_bin * shaders[MESA_SHADER_STAGES];
1608
1609 struct {
1610 const struct gen_l3_config * l3_config;
1611 uint32_t total_size;
1612 } urb;
1613
1614 VkShaderStageFlags active_stages;
1615 struct anv_state blend_state;
1616
1617 uint32_t vb_used;
1618 uint32_t binding_stride[MAX_VBS];
1619 bool instancing_enable[MAX_VBS];
1620 bool primitive_restart;
1621 uint32_t topology;
1622
1623 uint32_t cs_right_mask;
1624
1625 bool writes_depth;
1626 bool depth_test_enable;
1627 bool writes_stencil;
1628 bool stencil_test_enable;
1629 bool depth_clamp_enable;
1630 bool kill_pixel;
1631
1632 struct {
1633 uint32_t sf[7];
1634 uint32_t depth_stencil_state[3];
1635 } gen7;
1636
1637 struct {
1638 uint32_t sf[4];
1639 uint32_t raster[5];
1640 uint32_t wm_depth_stencil[3];
1641 } gen8;
1642
1643 struct {
1644 uint32_t wm_depth_stencil[4];
1645 } gen9;
1646
1647 uint32_t interface_descriptor_data[8];
1648 };
1649
1650 static inline bool
1651 anv_pipeline_has_stage(const struct anv_pipeline *pipeline,
1652 gl_shader_stage stage)
1653 {
1654 return (pipeline->active_stages & mesa_to_vk_shader_stage(stage)) != 0;
1655 }
1656
1657 #define ANV_DECL_GET_PROG_DATA_FUNC(prefix, stage) \
1658 static inline const struct brw_##prefix##_prog_data * \
1659 get_##prefix##_prog_data(const struct anv_pipeline *pipeline) \
1660 { \
1661 if (anv_pipeline_has_stage(pipeline, stage)) { \
1662 return (const struct brw_##prefix##_prog_data *) \
1663 pipeline->shaders[stage]->prog_data; \
1664 } else { \
1665 return NULL; \
1666 } \
1667 }
1668
1669 ANV_DECL_GET_PROG_DATA_FUNC(vs, MESA_SHADER_VERTEX)
1670 ANV_DECL_GET_PROG_DATA_FUNC(tcs, MESA_SHADER_TESS_CTRL)
1671 ANV_DECL_GET_PROG_DATA_FUNC(tes, MESA_SHADER_TESS_EVAL)
1672 ANV_DECL_GET_PROG_DATA_FUNC(gs, MESA_SHADER_GEOMETRY)
1673 ANV_DECL_GET_PROG_DATA_FUNC(wm, MESA_SHADER_FRAGMENT)
1674 ANV_DECL_GET_PROG_DATA_FUNC(cs, MESA_SHADER_COMPUTE)
1675
1676 static inline const struct brw_vue_prog_data *
1677 anv_pipeline_get_last_vue_prog_data(const struct anv_pipeline *pipeline)
1678 {
1679 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
1680 return &get_gs_prog_data(pipeline)->base;
1681 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1682 return &get_tes_prog_data(pipeline)->base;
1683 else
1684 return &get_vs_prog_data(pipeline)->base;
1685 }
1686
1687 VkResult
1688 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1689 struct anv_pipeline_cache *cache,
1690 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1691 const VkAllocationCallbacks *alloc);
1692
1693 VkResult
1694 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1695 struct anv_pipeline_cache *cache,
1696 const VkComputePipelineCreateInfo *info,
1697 struct anv_shader_module *module,
1698 const char *entrypoint,
1699 const VkSpecializationInfo *spec_info);
1700
1701 struct anv_format {
1702 enum isl_format isl_format:16;
1703 struct isl_swizzle swizzle;
1704 };
1705
1706 struct anv_format
1707 anv_get_format(const struct gen_device_info *devinfo, VkFormat format,
1708 VkImageAspectFlags aspect, VkImageTiling tiling);
1709
1710 static inline enum isl_format
1711 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
1712 VkImageAspectFlags aspect, VkImageTiling tiling)
1713 {
1714 return anv_get_format(devinfo, vk_format, aspect, tiling).isl_format;
1715 }
1716
1717 static inline struct isl_swizzle
1718 anv_swizzle_for_render(struct isl_swizzle swizzle)
1719 {
1720 /* Sometimes the swizzle will have alpha map to one. We do this to fake
1721 * RGB as RGBA for texturing
1722 */
1723 assert(swizzle.a == ISL_CHANNEL_SELECT_ONE ||
1724 swizzle.a == ISL_CHANNEL_SELECT_ALPHA);
1725
1726 /* But it doesn't matter what we render to that channel */
1727 swizzle.a = ISL_CHANNEL_SELECT_ALPHA;
1728
1729 return swizzle;
1730 }
1731
1732 void
1733 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm);
1734
1735 /**
1736 * Subsurface of an anv_image.
1737 */
1738 struct anv_surface {
1739 /** Valid only if isl_surf::size > 0. */
1740 struct isl_surf isl;
1741
1742 /**
1743 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1744 */
1745 uint32_t offset;
1746 };
1747
1748 struct anv_image {
1749 VkImageType type;
1750 /* The original VkFormat provided by the client. This may not match any
1751 * of the actual surface formats.
1752 */
1753 VkFormat vk_format;
1754 VkImageAspectFlags aspects;
1755 VkExtent3D extent;
1756 uint32_t levels;
1757 uint32_t array_size;
1758 uint32_t samples; /**< VkImageCreateInfo::samples */
1759 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1760 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1761
1762 VkDeviceSize size;
1763 uint32_t alignment;
1764
1765 /* Set when bound */
1766 struct anv_bo *bo;
1767 VkDeviceSize offset;
1768
1769 /**
1770 * Image subsurfaces
1771 *
1772 * For each foo, anv_image::foo_surface is valid if and only if
1773 * anv_image::aspects has a foo aspect.
1774 *
1775 * The hardware requires that the depth buffer and stencil buffer be
1776 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1777 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1778 * allocate the depth and stencil buffers as separate surfaces in the same
1779 * bo.
1780 */
1781 union {
1782 struct anv_surface color_surface;
1783
1784 struct {
1785 struct anv_surface depth_surface;
1786 struct anv_surface stencil_surface;
1787 };
1788 };
1789
1790 /**
1791 * For color images, this is the aux usage for this image when not used as a
1792 * color attachment.
1793 *
1794 * For depth/stencil images, this is set to ISL_AUX_USAGE_HIZ if the image
1795 * has a HiZ buffer.
1796 */
1797 enum isl_aux_usage aux_usage;
1798
1799 struct anv_surface aux_surface;
1800 };
1801
1802 /* Returns true if a HiZ-enabled depth buffer can be sampled from. */
1803 static inline bool
1804 anv_can_sample_with_hiz(const struct gen_device_info * const devinfo,
1805 const VkImageAspectFlags aspect_mask,
1806 const uint32_t samples)
1807 {
1808 /* Validate the inputs. */
1809 assert(devinfo && aspect_mask && samples);
1810 return devinfo->gen >= 8 && (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) &&
1811 samples == 1;
1812 }
1813
1814 void
1815 anv_gen8_hiz_op_resolve(struct anv_cmd_buffer *cmd_buffer,
1816 const struct anv_image *image,
1817 enum blorp_hiz_op op);
1818
1819 enum isl_aux_usage
1820 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
1821 const struct anv_image *image,
1822 const VkImageAspectFlags aspects,
1823 const VkImageLayout layout);
1824 static inline uint32_t
1825 anv_get_layerCount(const struct anv_image *image,
1826 const VkImageSubresourceRange *range)
1827 {
1828 return range->layerCount == VK_REMAINING_ARRAY_LAYERS ?
1829 image->array_size - range->baseArrayLayer : range->layerCount;
1830 }
1831
1832 static inline uint32_t
1833 anv_get_levelCount(const struct anv_image *image,
1834 const VkImageSubresourceRange *range)
1835 {
1836 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
1837 image->levels - range->baseMipLevel : range->levelCount;
1838 }
1839
1840
1841 struct anv_image_view {
1842 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
1843 struct anv_bo *bo;
1844 uint32_t offset; /**< Offset into bo. */
1845
1846 struct isl_view isl;
1847
1848 VkImageAspectFlags aspect_mask;
1849 VkFormat vk_format;
1850 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1851
1852 /** RENDER_SURFACE_STATE when using image as a sampler surface. */
1853 struct anv_state sampler_surface_state;
1854
1855 /**
1856 * RENDER_SURFACE_STATE when using image as a sampler surface with the
1857 * auxiliary buffer disabled.
1858 */
1859 struct anv_state no_aux_sampler_surface_state;
1860
1861 /**
1862 * RENDER_SURFACE_STATE when using image as a storage image. Separate states
1863 * for write-only and readable, using the real format for write-only and the
1864 * lowered format for readable.
1865 */
1866 struct anv_state storage_surface_state;
1867 struct anv_state writeonly_storage_surface_state;
1868
1869 struct brw_image_param storage_image_param;
1870 };
1871
1872 struct anv_image_create_info {
1873 const VkImageCreateInfo *vk_info;
1874
1875 /** An opt-in bitmask which filters an ISL-mapping of the Vulkan tiling. */
1876 isl_tiling_flags_t isl_tiling_flags;
1877
1878 uint32_t stride;
1879 };
1880
1881 VkResult anv_image_create(VkDevice _device,
1882 const struct anv_image_create_info *info,
1883 const VkAllocationCallbacks* alloc,
1884 VkImage *pImage);
1885
1886 const struct anv_surface *
1887 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1888 VkImageAspectFlags aspect_mask);
1889
1890 enum isl_format
1891 anv_isl_format_for_descriptor_type(VkDescriptorType type);
1892
1893 static inline struct VkExtent3D
1894 anv_sanitize_image_extent(const VkImageType imageType,
1895 const struct VkExtent3D imageExtent)
1896 {
1897 switch (imageType) {
1898 case VK_IMAGE_TYPE_1D:
1899 return (VkExtent3D) { imageExtent.width, 1, 1 };
1900 case VK_IMAGE_TYPE_2D:
1901 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1902 case VK_IMAGE_TYPE_3D:
1903 return imageExtent;
1904 default:
1905 unreachable("invalid image type");
1906 }
1907 }
1908
1909 static inline struct VkOffset3D
1910 anv_sanitize_image_offset(const VkImageType imageType,
1911 const struct VkOffset3D imageOffset)
1912 {
1913 switch (imageType) {
1914 case VK_IMAGE_TYPE_1D:
1915 return (VkOffset3D) { imageOffset.x, 0, 0 };
1916 case VK_IMAGE_TYPE_2D:
1917 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
1918 case VK_IMAGE_TYPE_3D:
1919 return imageOffset;
1920 default:
1921 unreachable("invalid image type");
1922 }
1923 }
1924
1925
1926 void anv_fill_buffer_surface_state(struct anv_device *device,
1927 struct anv_state state,
1928 enum isl_format format,
1929 uint32_t offset, uint32_t range,
1930 uint32_t stride);
1931
1932 void anv_image_view_fill_image_param(struct anv_device *device,
1933 struct anv_image_view *view,
1934 struct brw_image_param *param);
1935 void anv_buffer_view_fill_image_param(struct anv_device *device,
1936 struct anv_buffer_view *view,
1937 struct brw_image_param *param);
1938
1939 struct anv_sampler {
1940 uint32_t state[4];
1941 };
1942
1943 struct anv_framebuffer {
1944 uint32_t width;
1945 uint32_t height;
1946 uint32_t layers;
1947
1948 uint32_t attachment_count;
1949 struct anv_image_view * attachments[0];
1950 };
1951
1952 struct anv_subpass {
1953 uint32_t attachment_count;
1954
1955 /**
1956 * A pointer to all attachment references used in this subpass.
1957 * Only valid if ::attachment_count > 0.
1958 */
1959 VkAttachmentReference * attachments;
1960 uint32_t input_count;
1961 VkAttachmentReference * input_attachments;
1962 uint32_t color_count;
1963 VkAttachmentReference * color_attachments;
1964 VkAttachmentReference * resolve_attachments;
1965
1966 VkAttachmentReference depth_stencil_attachment;
1967
1968 /** Subpass has a depth/stencil self-dependency */
1969 bool has_ds_self_dep;
1970
1971 /** Subpass has at least one resolve attachment */
1972 bool has_resolve;
1973 };
1974
1975 enum anv_subpass_usage {
1976 ANV_SUBPASS_USAGE_DRAW = (1 << 0),
1977 ANV_SUBPASS_USAGE_INPUT = (1 << 1),
1978 ANV_SUBPASS_USAGE_RESOLVE_SRC = (1 << 2),
1979 ANV_SUBPASS_USAGE_RESOLVE_DST = (1 << 3),
1980 };
1981
1982 struct anv_render_pass_attachment {
1983 /* TODO: Consider using VkAttachmentDescription instead of storing each of
1984 * its members individually.
1985 */
1986 VkFormat format;
1987 uint32_t samples;
1988 VkImageUsageFlags usage;
1989 VkAttachmentLoadOp load_op;
1990 VkAttachmentStoreOp store_op;
1991 VkAttachmentLoadOp stencil_load_op;
1992 VkImageLayout initial_layout;
1993 VkImageLayout final_layout;
1994
1995 /* An array, indexed by subpass id, of how the attachment will be used. */
1996 enum anv_subpass_usage * subpass_usage;
1997
1998 /* The subpass id in which the attachment will be used last. */
1999 uint32_t last_subpass_idx;
2000 };
2001
2002 struct anv_render_pass {
2003 uint32_t attachment_count;
2004 uint32_t subpass_count;
2005 VkAttachmentReference * subpass_attachments;
2006 enum anv_subpass_usage * subpass_usages;
2007 struct anv_render_pass_attachment * attachments;
2008 struct anv_subpass subpasses[0];
2009 };
2010
2011 struct anv_query_pool_slot {
2012 uint64_t begin;
2013 uint64_t end;
2014 uint64_t available;
2015 };
2016
2017 struct anv_query_pool {
2018 VkQueryType type;
2019 uint32_t slots;
2020 struct anv_bo bo;
2021 };
2022
2023 void *anv_lookup_entrypoint(const struct gen_device_info *devinfo,
2024 const char *name);
2025
2026 void anv_dump_image_to_ppm(struct anv_device *device,
2027 struct anv_image *image, unsigned miplevel,
2028 unsigned array_layer, VkImageAspectFlagBits aspect,
2029 const char *filename);
2030
2031 enum anv_dump_action {
2032 ANV_DUMP_FRAMEBUFFERS_BIT = 0x1,
2033 };
2034
2035 void anv_dump_start(struct anv_device *device, enum anv_dump_action actions);
2036 void anv_dump_finish(void);
2037
2038 void anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
2039 struct anv_framebuffer *fb);
2040
2041 static inline uint32_t
2042 anv_get_subpass_id(const struct anv_cmd_state * const cmd_state)
2043 {
2044 /* This function must be called from within a subpass. */
2045 assert(cmd_state->pass && cmd_state->subpass);
2046
2047 const uint32_t subpass_id = cmd_state->subpass - cmd_state->pass->subpasses;
2048
2049 /* The id of this subpass shouldn't exceed the number of subpasses in this
2050 * render pass minus 1.
2051 */
2052 assert(subpass_id < cmd_state->pass->subpass_count);
2053 return subpass_id;
2054 }
2055
2056 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
2057 \
2058 static inline struct __anv_type * \
2059 __anv_type ## _from_handle(__VkType _handle) \
2060 { \
2061 return (struct __anv_type *) _handle; \
2062 } \
2063 \
2064 static inline __VkType \
2065 __anv_type ## _to_handle(struct __anv_type *_obj) \
2066 { \
2067 return (__VkType) _obj; \
2068 }
2069
2070 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
2071 \
2072 static inline struct __anv_type * \
2073 __anv_type ## _from_handle(__VkType _handle) \
2074 { \
2075 return (struct __anv_type *)(uintptr_t) _handle; \
2076 } \
2077 \
2078 static inline __VkType \
2079 __anv_type ## _to_handle(struct __anv_type *_obj) \
2080 { \
2081 return (__VkType)(uintptr_t) _obj; \
2082 }
2083
2084 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
2085 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
2086
2087 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
2088 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
2089 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
2090 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
2091 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
2092
2093 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
2094 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
2095 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView)
2096 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool, VkDescriptorPool)
2097 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
2098 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
2099 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_update_template, VkDescriptorUpdateTemplateKHR)
2100 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
2101 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
2102 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event, VkEvent)
2103 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
2104 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
2105 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
2106 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache, VkPipelineCache)
2107 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
2108 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
2109 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
2110 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
2111 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
2112 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
2113
2114 /* Gen-specific function declarations */
2115 #ifdef genX
2116 # include "anv_genX.h"
2117 #else
2118 # define genX(x) gen7_##x
2119 # include "anv_genX.h"
2120 # undef genX
2121 # define genX(x) gen75_##x
2122 # include "anv_genX.h"
2123 # undef genX
2124 # define genX(x) gen8_##x
2125 # include "anv_genX.h"
2126 # undef genX
2127 # define genX(x) gen9_##x
2128 # include "anv_genX.h"
2129 # undef genX
2130 #endif
2131
2132 #endif /* ANV_PRIVATE_H */