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