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