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