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