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