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