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