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