OSDN Git Service

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