vk: Set I915_CACHING_NONE for userptr BOs when !llc
[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 int 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 void anv_block_pool_init(struct anv_block_pool *pool,
398 struct anv_device *device, uint32_t block_size);
399 void anv_block_pool_finish(struct anv_block_pool *pool);
400 int32_t anv_block_pool_alloc(struct anv_block_pool *pool);
401 int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool);
402 void anv_block_pool_free(struct anv_block_pool *pool, int32_t offset);
403 void anv_state_pool_init(struct anv_state_pool *pool,
404 struct anv_block_pool *block_pool);
405 void anv_state_pool_finish(struct anv_state_pool *pool);
406 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
407 size_t state_size, size_t alignment);
408 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
409 void anv_state_stream_init(struct anv_state_stream *stream,
410 struct anv_block_pool *block_pool);
411 void anv_state_stream_finish(struct anv_state_stream *stream);
412 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
413 uint32_t size, uint32_t alignment);
414
415 /**
416 * Implements a pool of re-usable BOs. The interface is identical to that
417 * of block_pool except that each block is its own BO.
418 */
419 struct anv_bo_pool {
420 struct anv_device *device;
421
422 uint32_t bo_size;
423
424 void *free_list;
425 };
426
427 void anv_bo_pool_init(struct anv_bo_pool *pool,
428 struct anv_device *device, uint32_t block_size);
429 void anv_bo_pool_finish(struct anv_bo_pool *pool);
430 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo);
431 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
432
433
434 void *anv_resolve_entrypoint(uint32_t index);
435
436 extern struct anv_dispatch_table dtable;
437
438 #define ANV_CALL(func) ({ \
439 if (dtable.func == NULL) { \
440 size_t idx = offsetof(struct anv_dispatch_table, func) / sizeof(void *); \
441 dtable.entrypoints[idx] = anv_resolve_entrypoint(idx); \
442 } \
443 dtable.func; \
444 })
445
446 static inline void *
447 anv_alloc(const VkAllocationCallbacks *alloc,
448 size_t size, size_t align,
449 VkSystemAllocationScope scope)
450 {
451 return alloc->pfnAllocation(alloc->pUserData, size, align, scope);
452 }
453
454 static inline void *
455 anv_realloc(const VkAllocationCallbacks *alloc,
456 void *ptr, size_t size, size_t align,
457 VkSystemAllocationScope scope)
458 {
459 return alloc->pfnReallocation(alloc->pUserData, ptr, size, align, scope);
460 }
461
462 static inline void
463 anv_free(const VkAllocationCallbacks *alloc, void *data)
464 {
465 alloc->pfnFree(alloc->pUserData, data);
466 }
467
468 static inline void *
469 anv_alloc2(const VkAllocationCallbacks *parent_alloc,
470 const VkAllocationCallbacks *alloc,
471 size_t size, size_t align,
472 VkSystemAllocationScope scope)
473 {
474 if (alloc)
475 return anv_alloc(alloc, size, align, scope);
476 else
477 return anv_alloc(parent_alloc, size, align, scope);
478 }
479
480 static inline void
481 anv_free2(const VkAllocationCallbacks *parent_alloc,
482 const VkAllocationCallbacks *alloc,
483 void *data)
484 {
485 if (alloc)
486 anv_free(alloc, data);
487 else
488 anv_free(parent_alloc, data);
489 }
490
491 struct anv_physical_device {
492 VK_LOADER_DATA _loader_data;
493
494 struct anv_instance * instance;
495 uint32_t chipset_id;
496 const char * path;
497 const char * name;
498 const struct brw_device_info * info;
499 uint64_t aperture_size;
500 struct brw_compiler * compiler;
501 struct isl_device isl_dev;
502 };
503
504 struct anv_instance {
505 VK_LOADER_DATA _loader_data;
506
507 VkAllocationCallbacks alloc;
508
509 uint32_t apiVersion;
510 int physicalDeviceCount;
511 struct anv_physical_device physicalDevice;
512
513 void * wayland_wsi;
514 };
515
516 VkResult anv_init_wsi(struct anv_instance *instance);
517 void anv_finish_wsi(struct anv_instance *instance);
518
519 struct anv_meta_state {
520 struct {
521 struct anv_pipeline *color_pipeline;
522 struct anv_pipeline *depth_only_pipeline;
523 struct anv_pipeline *stencil_only_pipeline;
524 struct anv_pipeline *depthstencil_pipeline;
525 } clear;
526
527 struct {
528 VkRenderPass render_pass;
529
530 /** Pipeline that blits from a 2D image. */
531 VkPipeline pipeline_2d_src;
532
533 /** Pipeline that blits from a 3D image. */
534 VkPipeline pipeline_3d_src;
535
536 VkPipelineLayout pipeline_layout;
537 VkDescriptorSetLayout ds_layout;
538 } blit;
539 };
540
541 struct anv_queue {
542 VK_LOADER_DATA _loader_data;
543
544 struct anv_device * device;
545
546 struct anv_state_pool * pool;
547 };
548
549 struct anv_device {
550 VK_LOADER_DATA _loader_data;
551
552 VkAllocationCallbacks alloc;
553
554 struct anv_instance * instance;
555 uint32_t chipset_id;
556 struct brw_device_info info;
557 struct isl_device isl_dev;
558 int context_id;
559 int fd;
560
561 struct anv_bo_pool batch_bo_pool;
562
563 struct anv_block_pool dynamic_state_block_pool;
564 struct anv_state_pool dynamic_state_pool;
565
566 struct anv_block_pool instruction_block_pool;
567 struct anv_block_pool surface_state_block_pool;
568 struct anv_state_pool surface_state_pool;
569
570 struct anv_bo workaround_bo;
571
572 struct anv_meta_state meta_state;
573
574 struct anv_state border_colors;
575
576 struct anv_queue queue;
577
578 struct anv_block_pool scratch_block_pool;
579
580 pthread_mutex_t mutex;
581 };
582
583 void* anv_gem_mmap(struct anv_device *device,
584 uint32_t gem_handle, uint64_t offset, uint64_t size);
585 void anv_gem_munmap(void *p, uint64_t size);
586 uint32_t anv_gem_create(struct anv_device *device, size_t size);
587 void anv_gem_close(struct anv_device *device, int gem_handle);
588 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
589 int anv_gem_wait(struct anv_device *device, int gem_handle, int64_t *timeout_ns);
590 int anv_gem_execbuffer(struct anv_device *device,
591 struct drm_i915_gem_execbuffer2 *execbuf);
592 int anv_gem_set_tiling(struct anv_device *device, int gem_handle,
593 uint32_t stride, uint32_t tiling);
594 int anv_gem_create_context(struct anv_device *device);
595 int anv_gem_destroy_context(struct anv_device *device, int context);
596 int anv_gem_get_param(int fd, uint32_t param);
597 int anv_gem_get_aperture(int fd, uint64_t *size);
598 int anv_gem_handle_to_fd(struct anv_device *device, int gem_handle);
599 int anv_gem_fd_to_handle(struct anv_device *device, int fd);
600 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
601 int anv_gem_set_caching(struct anv_device *device, int gem_handle, uint32_t caching);
602 int anv_gem_set_domain(struct anv_device *device, int gem_handle,
603 uint32_t read_domains, uint32_t write_domain);
604
605 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
606
607 struct anv_reloc_list {
608 size_t num_relocs;
609 size_t array_length;
610 struct drm_i915_gem_relocation_entry * relocs;
611 struct anv_bo ** reloc_bos;
612 };
613
614 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
615 const VkAllocationCallbacks *alloc);
616 void anv_reloc_list_finish(struct anv_reloc_list *list,
617 const VkAllocationCallbacks *alloc);
618
619 uint64_t anv_reloc_list_add(struct anv_reloc_list *list,
620 const VkAllocationCallbacks *alloc,
621 uint32_t offset, struct anv_bo *target_bo,
622 uint32_t delta);
623
624 struct anv_batch_bo {
625 /* Link in the anv_cmd_buffer.owned_batch_bos list */
626 struct list_head link;
627
628 struct anv_bo bo;
629
630 /* Bytes actually consumed in this batch BO */
631 size_t length;
632
633 /* Last seen surface state block pool bo offset */
634 uint32_t last_ss_pool_bo_offset;
635
636 struct anv_reloc_list relocs;
637 };
638
639 struct anv_batch {
640 const VkAllocationCallbacks * alloc;
641
642 void * start;
643 void * end;
644 void * next;
645
646 struct anv_reloc_list * relocs;
647
648 /* This callback is called (with the associated user data) in the event
649 * that the batch runs out of space.
650 */
651 VkResult (*extend_cb)(struct anv_batch *, void *);
652 void * user_data;
653 };
654
655 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
656 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
657 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
658 void *location, struct anv_bo *bo, uint32_t offset);
659
660 struct anv_address {
661 struct anv_bo *bo;
662 uint32_t offset;
663 };
664
665 #define __gen_address_type struct anv_address
666 #define __gen_user_data struct anv_batch
667
668 static inline uint64_t
669 __gen_combine_address(struct anv_batch *batch, void *location,
670 const struct anv_address address, uint32_t delta)
671 {
672 if (address.bo == NULL) {
673 return address.offset + delta;
674 } else {
675 assert(batch->start <= location && location < batch->end);
676
677 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
678 }
679 }
680
681 /* Wrapper macros needed to work around preprocessor argument issues. In
682 * particular, arguments don't get pre-evaluated if they are concatenated.
683 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
684 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
685 * We can work around this easily enough with these helpers.
686 */
687 #define __anv_cmd_length(cmd) cmd ## _length
688 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
689 #define __anv_cmd_header(cmd) cmd ## _header
690 #define __anv_cmd_pack(cmd) cmd ## _pack
691
692 #define anv_batch_emit(batch, cmd, ...) do { \
693 void *__dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
694 struct cmd __template = { \
695 __anv_cmd_header(cmd), \
696 __VA_ARGS__ \
697 }; \
698 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
699 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, __anv_cmd_length(cmd) * 4)); \
700 } while (0)
701
702 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
703 void *__dst = anv_batch_emit_dwords(batch, n); \
704 struct cmd __template = { \
705 __anv_cmd_header(cmd), \
706 .DwordLength = n - __anv_cmd_length_bias(cmd), \
707 __VA_ARGS__ \
708 }; \
709 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
710 __dst; \
711 })
712
713 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
714 do { \
715 uint32_t *dw; \
716 \
717 static_assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1), "mismatch merge"); \
718 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
719 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
720 dw[i] = (dwords0)[i] | (dwords1)[i]; \
721 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
722 } while (0)
723
724 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
725 .GraphicsDataTypeGFDT = 0, \
726 .LLCCacheabilityControlLLCCC = 0, \
727 .L3CacheabilityControlL3CC = 1, \
728 }
729
730 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
731 .LLCeLLCCacheabilityControlLLCCC = 0, \
732 .L3CacheabilityControlL3CC = 1, \
733 }
734
735 #define GEN8_MOCS { \
736 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
737 .TargetCache = L3DefertoPATforLLCeLLCselection, \
738 .AgeforQUADLRU = 0 \
739 }
740
741 /* Skylake: MOCS is now an index into an array of 62 different caching
742 * configurations programmed by the kernel.
743 */
744
745 #define GEN9_MOCS { \
746 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
747 .IndextoMOCSTables = 2 \
748 }
749
750 #define GEN9_MOCS_PTE { \
751 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
752 .IndextoMOCSTables = 1 \
753 }
754
755 struct anv_device_memory {
756 struct anv_bo bo;
757 VkDeviceSize map_size;
758 void * map;
759 };
760
761 /**
762 * Header for Vertex URB Entry (VUE)
763 */
764 struct anv_vue_header {
765 uint32_t Reserved;
766 uint32_t RTAIndex; /* RenderTargetArrayIndex */
767 uint32_t ViewportIndex;
768 float PointWidth;
769 };
770
771 struct anv_descriptor_set_binding_layout {
772 /* Number of array elements in this binding */
773 uint16_t array_size;
774
775 /* Index into the flattend descriptor set */
776 uint16_t descriptor_index;
777
778 /* Index into the dynamic state array for a dynamic buffer */
779 int16_t dynamic_offset_index;
780
781 struct {
782 /* Index into the binding table for the associated surface */
783 int16_t surface_index;
784
785 /* Index into the sampler table for the associated sampler */
786 int16_t sampler_index;
787 } stage[MESA_SHADER_STAGES];
788
789 /* Immutable samplers (or NULL if no immutable samplers) */
790 struct anv_sampler **immutable_samplers;
791 };
792
793 struct anv_descriptor_set_layout {
794 /* Number of bindings in this descriptor set */
795 uint16_t binding_count;
796
797 /* Total size of the descriptor set with room for all array entries */
798 uint16_t size;
799
800 /* Shader stages affected by this descriptor set */
801 uint16_t shader_stages;
802
803 /* Number of dynamic offsets used by this descriptor set */
804 uint16_t dynamic_offset_count;
805
806 /* Bindings in this descriptor set */
807 struct anv_descriptor_set_binding_layout binding[0];
808 };
809
810 struct anv_descriptor {
811 VkDescriptorType type;
812
813 union {
814 struct {
815 union {
816 struct anv_image_view *image_view;
817 };
818 struct anv_sampler *sampler;
819 };
820
821 struct {
822 struct anv_buffer *buffer;
823 uint64_t offset;
824 uint64_t range;
825 };
826 };
827 };
828
829 struct anv_descriptor_set {
830 const struct anv_descriptor_set_layout *layout;
831 struct anv_descriptor descriptors[0];
832 };
833
834 VkResult
835 anv_descriptor_set_create(struct anv_device *device,
836 const struct anv_descriptor_set_layout *layout,
837 struct anv_descriptor_set **out_set);
838
839 void
840 anv_descriptor_set_destroy(struct anv_device *device,
841 struct anv_descriptor_set *set);
842
843 #define MAX_VBS 32
844 #define MAX_SETS 8
845 #define MAX_RTS 8
846 #define MAX_VIEWPORTS 16
847 #define MAX_SCISSORS 16
848 #define MAX_PUSH_CONSTANTS_SIZE 128
849 #define MAX_DYNAMIC_BUFFERS 16
850 #define MAX_IMAGES 8
851
852 struct anv_pipeline_binding {
853 /* The descriptor set this surface corresponds to */
854 uint16_t set;
855
856 /* Offset into the descriptor set */
857 uint16_t offset;
858 };
859
860 struct anv_pipeline_layout {
861 struct {
862 struct anv_descriptor_set_layout *layout;
863 uint32_t dynamic_offset_start;
864 struct {
865 uint32_t surface_start;
866 uint32_t sampler_start;
867 } stage[MESA_SHADER_STAGES];
868 } set[MAX_SETS];
869
870 uint32_t num_sets;
871
872 struct {
873 bool has_dynamic_offsets;
874 uint32_t surface_count;
875 struct anv_pipeline_binding *surface_to_descriptor;
876 uint32_t sampler_count;
877 struct anv_pipeline_binding *sampler_to_descriptor;
878 } stage[MESA_SHADER_STAGES];
879
880 struct anv_pipeline_binding entries[0];
881 };
882
883 struct anv_buffer {
884 struct anv_device * device;
885 VkDeviceSize size;
886
887 /* Set when bound */
888 struct anv_bo * bo;
889 VkDeviceSize offset;
890 };
891
892 enum anv_cmd_dirty_bits {
893 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
894 ANV_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
895 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
896 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
897 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
898 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
899 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
900 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
901 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
902 ANV_CMD_DIRTY_DYNAMIC_ALL = (1 << 9) - 1,
903 ANV_CMD_DIRTY_PIPELINE = 1 << 9,
904 ANV_CMD_DIRTY_INDEX_BUFFER = 1 << 10,
905 ANV_CMD_DIRTY_RENDER_TARGETS = 1 << 11,
906 };
907 typedef uint32_t anv_cmd_dirty_mask_t;
908
909 struct anv_vertex_binding {
910 struct anv_buffer * buffer;
911 VkDeviceSize offset;
912 };
913
914 struct anv_push_constants {
915 /* Current allocated size of this push constants data structure.
916 * Because a decent chunk of it may not be used (images on SKL, for
917 * instance), we won't actually allocate the entire structure up-front.
918 */
919 uint32_t size;
920
921 /* Push constant data provided by the client through vkPushConstants */
922 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
923
924 /* Our hardware only provides zero-based vertex and instance id so, in
925 * order to satisfy the vulkan requirements, we may have to push one or
926 * both of these into the shader.
927 */
928 uint32_t base_vertex;
929 uint32_t base_instance;
930
931 /* Offsets and ranges for dynamically bound buffers */
932 struct {
933 uint32_t offset;
934 uint32_t range;
935 } dynamic[MAX_DYNAMIC_BUFFERS];
936
937 /* Image data for image_load_store on pre-SKL */
938 struct brw_image_param images[MAX_IMAGES];
939 };
940
941 struct anv_dynamic_state {
942 struct {
943 uint32_t count;
944 VkViewport viewports[MAX_VIEWPORTS];
945 } viewport;
946
947 struct {
948 uint32_t count;
949 VkRect2D scissors[MAX_SCISSORS];
950 } scissor;
951
952 float line_width;
953
954 struct {
955 float bias;
956 float clamp;
957 float slope;
958 } depth_bias;
959
960 float blend_constants[4];
961
962 struct {
963 float min;
964 float max;
965 } depth_bounds;
966
967 struct {
968 uint32_t front;
969 uint32_t back;
970 } stencil_compare_mask;
971
972 struct {
973 uint32_t front;
974 uint32_t back;
975 } stencil_write_mask;
976
977 struct {
978 uint32_t front;
979 uint32_t back;
980 } stencil_reference;
981 };
982
983 extern const struct anv_dynamic_state default_dynamic_state;
984
985 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
986 const struct anv_dynamic_state *src,
987 uint32_t copy_mask);
988
989 /** State required while building cmd buffer */
990 struct anv_cmd_state {
991 uint32_t current_pipeline;
992 uint32_t vb_dirty;
993 anv_cmd_dirty_mask_t dirty;
994 anv_cmd_dirty_mask_t compute_dirty;
995 VkShaderStageFlags descriptors_dirty;
996 VkShaderStageFlags push_constants_dirty;
997 uint32_t scratch_size;
998 struct anv_pipeline * pipeline;
999 struct anv_pipeline * compute_pipeline;
1000 struct anv_framebuffer * framebuffer;
1001 struct anv_render_pass * pass;
1002 struct anv_subpass * subpass;
1003 uint32_t restart_index;
1004 struct anv_vertex_binding vertex_bindings[MAX_VBS];
1005 struct anv_descriptor_set * descriptors[MAX_SETS];
1006 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
1007 struct anv_dynamic_state dynamic;
1008
1009 struct {
1010 struct anv_buffer * index_buffer;
1011 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1012 uint32_t index_offset;
1013 } gen7;
1014 };
1015
1016 struct anv_cmd_pool {
1017 VkAllocationCallbacks alloc;
1018 struct list_head cmd_buffers;
1019 };
1020
1021 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1022
1023 enum anv_cmd_buffer_exec_mode {
1024 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
1025 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
1026 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
1027 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
1028 };
1029
1030 struct anv_cmd_buffer {
1031 VK_LOADER_DATA _loader_data;
1032
1033 struct anv_device * device;
1034
1035 struct anv_cmd_pool * pool;
1036 struct list_head pool_link;
1037
1038 struct anv_batch batch;
1039
1040 /* Fields required for the actual chain of anv_batch_bo's.
1041 *
1042 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1043 */
1044 struct list_head batch_bos;
1045 enum anv_cmd_buffer_exec_mode exec_mode;
1046
1047 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1048 * referenced by this command buffer
1049 *
1050 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1051 */
1052 struct anv_vector seen_bbos;
1053
1054 /* A vector of int32_t's for every block of binding tables.
1055 *
1056 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1057 */
1058 struct anv_vector bt_blocks;
1059 uint32_t bt_next;
1060 struct anv_reloc_list surface_relocs;
1061
1062 /* Information needed for execbuf
1063 *
1064 * These fields are generated by anv_cmd_buffer_prepare_execbuf().
1065 */
1066 struct {
1067 struct drm_i915_gem_execbuffer2 execbuf;
1068
1069 struct drm_i915_gem_exec_object2 * objects;
1070 uint32_t bo_count;
1071 struct anv_bo ** bos;
1072
1073 /* Allocated length of the 'objects' and 'bos' arrays */
1074 uint32_t array_length;
1075
1076 bool need_reloc;
1077 } execbuf2;
1078
1079 /* Serial for tracking buffer completion */
1080 uint32_t serial;
1081
1082 /* Stream objects for storing temporary data */
1083 struct anv_state_stream surface_state_stream;
1084 struct anv_state_stream dynamic_state_stream;
1085
1086 VkCommandBufferUsageFlags usage_flags;
1087 VkCommandBufferLevel level;
1088
1089 struct anv_cmd_state state;
1090 };
1091
1092 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1093 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1094 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1095 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
1096 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
1097 struct anv_cmd_buffer *secondary);
1098 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
1099
1100 VkResult anv_cmd_buffer_emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
1101 unsigned stage, struct anv_state *bt_state);
1102 VkResult anv_cmd_buffer_emit_samplers(struct anv_cmd_buffer *cmd_buffer,
1103 unsigned stage, struct anv_state *state);
1104 void gen7_cmd_buffer_flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer);
1105
1106 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
1107 uint32_t *a, uint32_t dwords,
1108 uint32_t alignment);
1109 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
1110 uint32_t *a, uint32_t *b,
1111 uint32_t dwords, uint32_t alignment);
1112 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1113 struct anv_subpass *subpass);
1114
1115 struct anv_address
1116 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1117 struct anv_state
1118 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1119 uint32_t entries, uint32_t *state_offset);
1120 struct anv_state
1121 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1122 struct anv_state
1123 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1124 uint32_t size, uint32_t alignment);
1125
1126 VkResult
1127 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1128
1129 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1130 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1131
1132 void gen7_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1133 void gen75_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1134 void gen8_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1135 void gen9_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1136
1137 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1138
1139 void gen7_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1140 struct anv_subpass *subpass);
1141
1142 void gen8_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1143 struct anv_subpass *subpass);
1144 void gen9_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1145 struct anv_subpass *subpass);
1146
1147 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1148 struct anv_subpass *subpass);
1149
1150 struct anv_state
1151 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1152 gl_shader_stage stage);
1153
1154 void anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
1155 struct anv_render_pass *pass,
1156 const VkClearValue *clear_values);
1157 const struct anv_image_view *
1158 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1159
1160 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1161
1162 struct anv_fence {
1163 struct anv_bo bo;
1164 struct drm_i915_gem_execbuffer2 execbuf;
1165 struct drm_i915_gem_exec_object2 exec2_objects[1];
1166 bool ready;
1167 };
1168
1169 struct nir_shader;
1170
1171 struct anv_shader_module {
1172 struct nir_shader * nir;
1173
1174 uint32_t size;
1175 char data[0];
1176 };
1177
1178 static inline gl_shader_stage
1179 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1180 {
1181 assert(__builtin_popcount(vk_stage) == 1);
1182 return ffs(vk_stage) - 1;
1183 }
1184
1185 static inline VkShaderStageFlagBits
1186 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1187 {
1188 return (1 << mesa_stage);
1189 }
1190
1191 #define anv_foreach_stage(stage, stage_bits) \
1192 for (gl_shader_stage stage, __tmp = (gl_shader_stage)(stage_bits);\
1193 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1194 __tmp &= ~(1 << (stage)))
1195
1196 struct anv_pipeline {
1197 struct anv_device * device;
1198 struct anv_batch batch;
1199 uint32_t batch_data[512];
1200 struct anv_reloc_list batch_relocs;
1201 uint32_t dynamic_state_mask;
1202 struct anv_dynamic_state dynamic_state;
1203
1204 struct anv_pipeline_layout * layout;
1205 bool use_repclear;
1206
1207 struct brw_vs_prog_data vs_prog_data;
1208 struct brw_wm_prog_data wm_prog_data;
1209 struct brw_gs_prog_data gs_prog_data;
1210 struct brw_cs_prog_data cs_prog_data;
1211 bool writes_point_size;
1212 struct brw_stage_prog_data * prog_data[MESA_SHADER_STAGES];
1213 uint32_t scratch_start[MESA_SHADER_STAGES];
1214 uint32_t total_scratch;
1215 struct {
1216 uint32_t vs_start;
1217 uint32_t vs_size;
1218 uint32_t nr_vs_entries;
1219 uint32_t gs_start;
1220 uint32_t gs_size;
1221 uint32_t nr_gs_entries;
1222 } urb;
1223
1224 VkShaderStageFlags active_stages;
1225 struct anv_state_stream program_stream;
1226 struct anv_state blend_state;
1227 uint32_t vs_simd8;
1228 uint32_t vs_vec4;
1229 uint32_t ps_simd8;
1230 uint32_t ps_simd16;
1231 uint32_t ps_ksp0;
1232 uint32_t ps_ksp2;
1233 uint32_t ps_grf_start0;
1234 uint32_t ps_grf_start2;
1235 uint32_t gs_vec4;
1236 uint32_t gs_vertex_count;
1237 uint32_t cs_simd;
1238
1239 uint32_t vb_used;
1240 uint32_t binding_stride[MAX_VBS];
1241 bool instancing_enable[MAX_VBS];
1242 bool primitive_restart;
1243 uint32_t topology;
1244
1245 uint32_t cs_thread_width_max;
1246 uint32_t cs_right_mask;
1247
1248 struct {
1249 uint32_t sf[7];
1250 uint32_t depth_stencil_state[3];
1251 } gen7;
1252
1253 struct {
1254 uint32_t sf[4];
1255 uint32_t raster[5];
1256 uint32_t wm_depth_stencil[3];
1257 } gen8;
1258
1259 struct {
1260 uint32_t wm_depth_stencil[4];
1261 } gen9;
1262 };
1263
1264 struct anv_graphics_pipeline_create_info {
1265 bool use_repclear;
1266 bool disable_viewport;
1267 bool disable_scissor;
1268 bool disable_vs;
1269 bool use_rectlist;
1270 };
1271
1272 VkResult
1273 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1274 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1275 const struct anv_graphics_pipeline_create_info *extra,
1276 const VkAllocationCallbacks *alloc);
1277
1278 VkResult
1279 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1280 const VkComputePipelineCreateInfo *info,
1281 struct anv_shader_module *module,
1282 const char *entrypoint_name);
1283
1284 VkResult
1285 anv_graphics_pipeline_create(VkDevice device,
1286 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1287 const struct anv_graphics_pipeline_create_info *extra,
1288 const VkAllocationCallbacks *alloc,
1289 VkPipeline *pPipeline);
1290
1291 VkResult
1292 gen7_graphics_pipeline_create(VkDevice _device,
1293 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1294 const struct anv_graphics_pipeline_create_info *extra,
1295 const VkAllocationCallbacks *alloc,
1296 VkPipeline *pPipeline);
1297
1298 VkResult
1299 gen75_graphics_pipeline_create(VkDevice _device,
1300 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1301 const struct anv_graphics_pipeline_create_info *extra,
1302 const VkAllocationCallbacks *alloc,
1303 VkPipeline *pPipeline);
1304
1305 VkResult
1306 gen8_graphics_pipeline_create(VkDevice _device,
1307 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1308 const struct anv_graphics_pipeline_create_info *extra,
1309 const VkAllocationCallbacks *alloc,
1310 VkPipeline *pPipeline);
1311 VkResult
1312 gen9_graphics_pipeline_create(VkDevice _device,
1313 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1314 const struct anv_graphics_pipeline_create_info *extra,
1315 const VkAllocationCallbacks *alloc,
1316 VkPipeline *pPipeline);
1317 VkResult
1318 gen7_compute_pipeline_create(VkDevice _device,
1319 const VkComputePipelineCreateInfo *pCreateInfo,
1320 const VkAllocationCallbacks *alloc,
1321 VkPipeline *pPipeline);
1322 VkResult
1323 gen75_compute_pipeline_create(VkDevice _device,
1324 const VkComputePipelineCreateInfo *pCreateInfo,
1325 const VkAllocationCallbacks *alloc,
1326 VkPipeline *pPipeline);
1327
1328 VkResult
1329 gen8_compute_pipeline_create(VkDevice _device,
1330 const VkComputePipelineCreateInfo *pCreateInfo,
1331 const VkAllocationCallbacks *alloc,
1332 VkPipeline *pPipeline);
1333 VkResult
1334 gen9_compute_pipeline_create(VkDevice _device,
1335 const VkComputePipelineCreateInfo *pCreateInfo,
1336 const VkAllocationCallbacks *alloc,
1337 VkPipeline *pPipeline);
1338
1339 struct anv_format {
1340 const VkFormat vk_format;
1341 const char *name;
1342 enum isl_format surface_format; /**< RENDER_SURFACE_STATE.SurfaceFormat */
1343 const struct isl_format_layout *isl_layout;
1344 uint8_t num_channels;
1345 uint16_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
1346 bool has_stencil;
1347 };
1348
1349 /**
1350 * Stencil formats are often a special case. To reduce the number of lookups
1351 * into the VkFormat-to-anv_format translation table when working with
1352 * stencil, here is the handle to the table's entry for VK_FORMAT_S8_UINT.
1353 */
1354 extern const struct anv_format *const anv_format_s8_uint;
1355
1356 const struct anv_format *
1357 anv_format_for_vk_format(VkFormat format);
1358
1359 static inline bool
1360 anv_format_is_color(const struct anv_format *format)
1361 {
1362 return !format->depth_format && !format->has_stencil;
1363 }
1364
1365 static inline bool
1366 anv_format_is_depth_or_stencil(const struct anv_format *format)
1367 {
1368 return format->depth_format || format->has_stencil;
1369 }
1370
1371 struct anv_image_view_info {
1372 uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
1373 bool is_array:1; /**< RENDER_SURFACE_STATE.SurfaceArray */
1374 bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
1375 };
1376
1377 struct anv_image_view_info
1378 anv_image_view_info_for_vk_image_view_type(VkImageViewType type);
1379
1380 /**
1381 * Subsurface of an anv_image.
1382 */
1383 struct anv_surface {
1384 struct isl_surf isl;
1385
1386 /**
1387 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1388 */
1389 uint32_t offset;
1390 };
1391
1392 struct anv_image {
1393 VkImageType type;
1394 const struct anv_format *format;
1395 VkExtent3D extent;
1396 uint32_t levels;
1397 uint32_t array_size;
1398 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1399
1400 VkDeviceSize size;
1401 uint32_t alignment;
1402
1403 /* Set when bound */
1404 struct anv_bo *bo;
1405 VkDeviceSize offset;
1406
1407 uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
1408
1409 bool needs_nonrt_surface_state:1;
1410 bool needs_color_rt_surface_state:1;
1411
1412 /**
1413 * Image subsurfaces
1414 *
1415 * For each foo, anv_image::foo_surface is valid if and only if
1416 * anv_image::format has a foo aspect.
1417 *
1418 * The hardware requires that the depth buffer and stencil buffer be
1419 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1420 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1421 * allocate the depth and stencil buffers as separate surfaces in the same
1422 * bo.
1423 */
1424 union {
1425 struct anv_surface color_surface;
1426
1427 struct {
1428 struct anv_surface depth_surface;
1429 struct anv_surface stencil_surface;
1430 };
1431 };
1432 };
1433
1434 struct anv_image_view {
1435 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
1436 const struct anv_format *format; /**< VkImageViewCreateInfo::format */
1437 struct anv_bo *bo;
1438 uint32_t offset; /**< Offset into bo. */
1439 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1440
1441 /** RENDER_SURFACE_STATE when using image as a color render target. */
1442 struct anv_state color_rt_surface_state;
1443
1444 /** RENDER_SURFACE_STATE when using image as a non render target. */
1445 struct anv_state nonrt_surface_state;
1446 };
1447
1448 struct anv_image_create_info {
1449 const VkImageCreateInfo *vk_info;
1450 bool force_tiling;
1451 enum isl_tiling tiling;
1452 uint32_t stride;
1453 };
1454
1455 VkResult anv_image_create(VkDevice _device,
1456 const struct anv_image_create_info *info,
1457 const VkAllocationCallbacks* alloc,
1458 VkImage *pImage);
1459
1460 struct anv_surface *
1461 anv_image_get_surface_for_aspect_mask(struct anv_image *image,
1462 VkImageAspectFlags aspect_mask);
1463
1464 void anv_image_view_init(struct anv_image_view *view,
1465 struct anv_device *device,
1466 const VkImageViewCreateInfo* pCreateInfo,
1467 struct anv_cmd_buffer *cmd_buffer);
1468
1469 void
1470 gen7_image_view_init(struct anv_image_view *iview,
1471 struct anv_device *device,
1472 const VkImageViewCreateInfo* pCreateInfo,
1473 struct anv_cmd_buffer *cmd_buffer);
1474
1475 void
1476 gen75_image_view_init(struct anv_image_view *iview,
1477 struct anv_device *device,
1478 const VkImageViewCreateInfo* pCreateInfo,
1479 struct anv_cmd_buffer *cmd_buffer);
1480
1481 void
1482 gen8_image_view_init(struct anv_image_view *iview,
1483 struct anv_device *device,
1484 const VkImageViewCreateInfo* pCreateInfo,
1485 struct anv_cmd_buffer *cmd_buffer);
1486
1487 void
1488 gen9_image_view_init(struct anv_image_view *iview,
1489 struct anv_device *device,
1490 const VkImageViewCreateInfo* pCreateInfo,
1491 struct anv_cmd_buffer *cmd_buffer);
1492
1493 void anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1494 const struct anv_format *format,
1495 uint32_t offset, uint32_t range,
1496 uint32_t stride);
1497
1498 void gen7_fill_buffer_surface_state(void *state, const struct anv_format *format,
1499 uint32_t offset, uint32_t range,
1500 uint32_t stride);
1501 void gen75_fill_buffer_surface_state(void *state, const struct anv_format *format,
1502 uint32_t offset, uint32_t range,
1503 uint32_t stride);
1504 void gen8_fill_buffer_surface_state(void *state, const struct anv_format *format,
1505 uint32_t offset, uint32_t range,
1506 uint32_t stride);
1507 void gen9_fill_buffer_surface_state(void *state, const struct anv_format *format,
1508 uint32_t offset, uint32_t range,
1509 uint32_t stride);
1510
1511 struct anv_sampler {
1512 uint32_t state[4];
1513 };
1514
1515 struct anv_framebuffer {
1516 uint32_t width;
1517 uint32_t height;
1518 uint32_t layers;
1519
1520 uint32_t attachment_count;
1521 const struct anv_image_view * attachments[0];
1522 };
1523
1524 struct anv_subpass {
1525 uint32_t input_count;
1526 uint32_t * input_attachments;
1527 uint32_t color_count;
1528 uint32_t * color_attachments;
1529 uint32_t * resolve_attachments;
1530 uint32_t depth_stencil_attachment;
1531 };
1532
1533 struct anv_render_pass_attachment {
1534 const struct anv_format *format;
1535 uint32_t samples;
1536 VkAttachmentLoadOp load_op;
1537 VkAttachmentLoadOp stencil_load_op;
1538 };
1539
1540 struct anv_render_pass {
1541 uint32_t attachment_count;
1542 uint32_t subpass_count;
1543 struct anv_render_pass_attachment * attachments;
1544 struct anv_subpass subpasses[0];
1545 };
1546
1547 extern struct anv_render_pass anv_meta_dummy_renderpass;
1548
1549 struct anv_query_pool_slot {
1550 uint64_t begin;
1551 uint64_t end;
1552 uint64_t available;
1553 };
1554
1555 struct anv_query_pool {
1556 VkQueryType type;
1557 uint32_t slots;
1558 struct anv_bo bo;
1559 };
1560
1561 void anv_device_init_meta(struct anv_device *device);
1562 void anv_device_finish_meta(struct anv_device *device);
1563
1564 void *anv_lookup_entrypoint(const char *name);
1565
1566 void anv_dump_image_to_ppm(struct anv_device *device,
1567 struct anv_image *image, unsigned miplevel,
1568 unsigned array_layer, const char *filename);
1569
1570 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
1571 \
1572 static inline struct __anv_type * \
1573 __anv_type ## _from_handle(__VkType _handle) \
1574 { \
1575 return (struct __anv_type *) _handle; \
1576 } \
1577 \
1578 static inline __VkType \
1579 __anv_type ## _to_handle(struct __anv_type *_obj) \
1580 { \
1581 return (__VkType) _obj; \
1582 }
1583
1584 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
1585 \
1586 static inline struct __anv_type * \
1587 __anv_type ## _from_handle(__VkType _handle) \
1588 { \
1589 return (struct __anv_type *)(uintptr_t) _handle; \
1590 } \
1591 \
1592 static inline __VkType \
1593 __anv_type ## _to_handle(struct __anv_type *_obj) \
1594 { \
1595 return (__VkType)(uintptr_t) _obj; \
1596 }
1597
1598 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
1599 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
1600
1601 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
1602 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
1603 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
1604 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
1605 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
1606
1607 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
1608 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
1609 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
1610 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
1611 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
1612 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
1613 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
1614 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
1615 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
1616 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
1617 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
1618 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
1619 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
1620 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
1621 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
1622
1623 #define ANV_DEFINE_STRUCT_CASTS(__anv_type, __VkType) \
1624 \
1625 static inline const __VkType * \
1626 __anv_type ## _to_ ## __VkType(const struct __anv_type *__anv_obj) \
1627 { \
1628 return (const __VkType *) __anv_obj; \
1629 }
1630
1631 #define ANV_COMMON_TO_STRUCT(__VkType, __vk_name, __common_name) \
1632 const __VkType *__vk_name = anv_common_to_ ## __VkType(__common_name)
1633
1634 ANV_DEFINE_STRUCT_CASTS(anv_common, VkMemoryBarrier)
1635 ANV_DEFINE_STRUCT_CASTS(anv_common, VkBufferMemoryBarrier)
1636 ANV_DEFINE_STRUCT_CASTS(anv_common, VkImageMemoryBarrier)
1637
1638 #ifdef __cplusplus
1639 }
1640 #endif