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