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