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