anv: Add facilities for dumping an image to a file
[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 <i915_drm.h>
32
33 #ifdef HAVE_VALGRIND
34 #include <valgrind.h>
35 #include <memcheck.h>
36 #define VG(x) x
37 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
38 #else
39 #define VG(x)
40 #endif
41
42 #include "brw_device_info.h"
43 #include "util/macros.h"
44 #include "util/list.h"
45
46 #define VK_PROTOTYPES
47 #include <vulkan/vulkan.h>
48 #include <vulkan/vulkan_intel.h>
49 #include <vulkan/vk_ext_khr_swapchain.h>
50 #include <vulkan/vk_ext_khr_device_swapchain.h>
51
52 #include "anv_entrypoints.h"
53
54 #include "brw_context.h"
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 #define ICD_LOADER_MAGIC 0x01CDC0DE
61
62 typedef union _VK_LOADER_DATA {
63 uintptr_t loaderMagic;
64 void *loaderData;
65 } VK_LOADER_DATA;
66
67 #define anv_noreturn __attribute__((__noreturn__))
68 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
69
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
71 #define MAX(a, b) ((a) > (b) ? (a) : (b))
72
73 static inline uint32_t
74 align_u32(uint32_t v, uint32_t a)
75 {
76 return (v + a - 1) & ~(a - 1);
77 }
78
79 static inline int32_t
80 align_i32(int32_t v, int32_t a)
81 {
82 return (v + a - 1) & ~(a - 1);
83 }
84
85 /** Alignment must be a power of 2. */
86 static inline bool
87 anv_is_aligned(uintmax_t n, uintmax_t a)
88 {
89 assert(a == (a & -a));
90 return (n & (a - 1)) == 0;
91 }
92
93 static inline uint32_t
94 anv_minify(uint32_t n, uint32_t levels)
95 {
96 if (unlikely(n == 0))
97 return 0;
98 else
99 return MAX(n >> levels, 1);
100 }
101
102 static inline bool
103 anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
104 {
105 if (*inout_mask & clear_mask) {
106 *inout_mask &= ~clear_mask;
107 return true;
108 } else {
109 return false;
110 }
111 }
112
113 #define for_each_bit(b, dword) \
114 for (uint32_t __dword = (dword); \
115 (b) = __builtin_ffs(__dword) - 1, __dword; \
116 __dword &= ~(1 << (b)))
117
118 #define typed_memcpy(dest, src, count) ({ \
119 static_assert(sizeof(*src) == sizeof(*dest), ""); \
120 memcpy((dest), (src), (count) * sizeof(*(src))); \
121 })
122
123 /* Define no kernel as 1, since that's an illegal offset for a kernel */
124 #define NO_KERNEL 1
125
126 struct anv_common {
127 VkStructureType sType;
128 const void* pNext;
129 };
130
131 /* Whenever we generate an error, pass it through this function. Useful for
132 * debugging, where we can break on it. Only call at error site, not when
133 * propagating errors. Might be useful to plug in a stack trace here.
134 */
135
136 VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
137
138 #ifdef DEBUG
139 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
140 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
141 #else
142 #define vk_error(error) error
143 #define vk_errorf(error, format, ...) error
144 #endif
145
146 void __anv_finishme(const char *file, int line, const char *format, ...)
147 anv_printflike(3, 4);
148 void anv_loge(const char *format, ...) anv_printflike(1, 2);
149 void anv_loge_v(const char *format, va_list va);
150
151 /**
152 * Print a FINISHME message, including its source location.
153 */
154 #define anv_finishme(format, ...) \
155 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);
156
157 /* A non-fatal assert. Useful for debugging. */
158 #ifdef DEBUG
159 #define anv_assert(x) ({ \
160 if (unlikely(!(x))) \
161 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
162 })
163 #else
164 #define anv_assert(x)
165 #endif
166
167 void anv_abortf(const char *format, ...) anv_noreturn anv_printflike(1, 2);
168 void anv_abortfv(const char *format, va_list va) anv_noreturn;
169
170 #define stub_return(v) \
171 do { \
172 anv_finishme("stub %s", __func__); \
173 return (v); \
174 } while (0)
175
176 #define stub() \
177 do { \
178 anv_finishme("stub %s", __func__); \
179 return; \
180 } while (0)
181
182 /**
183 * A dynamically growable, circular buffer. Elements are added at head and
184 * removed from tail. head and tail are free-running uint32_t indices and we
185 * only compute the modulo with size when accessing the array. This way,
186 * number of bytes in the queue is always head - tail, even in case of
187 * wraparound.
188 */
189
190 struct anv_vector {
191 uint32_t head;
192 uint32_t tail;
193 uint32_t element_size;
194 uint32_t size;
195 void *data;
196 };
197
198 int anv_vector_init(struct anv_vector *queue, uint32_t element_size, uint32_t size);
199 void *anv_vector_add(struct anv_vector *queue);
200 void *anv_vector_remove(struct anv_vector *queue);
201
202 static inline int
203 anv_vector_length(struct anv_vector *queue)
204 {
205 return (queue->head - queue->tail) / queue->element_size;
206 }
207
208 static inline void *
209 anv_vector_head(struct anv_vector *vector)
210 {
211 assert(vector->tail < vector->head);
212 return (void *)((char *)vector->data +
213 ((vector->head - vector->element_size) &
214 (vector->size - 1)));
215 }
216
217 static inline void *
218 anv_vector_tail(struct anv_vector *vector)
219 {
220 return (void *)((char *)vector->data + (vector->tail & (vector->size - 1)));
221 }
222
223 static inline void
224 anv_vector_finish(struct anv_vector *queue)
225 {
226 free(queue->data);
227 }
228
229 #define anv_vector_foreach(elem, queue) \
230 static_assert(__builtin_types_compatible_p(__typeof__(queue), struct anv_vector *), ""); \
231 for (uint32_t __anv_vector_offset = (queue)->tail; \
232 elem = (queue)->data + (__anv_vector_offset & ((queue)->size - 1)), __anv_vector_offset < (queue)->head; \
233 __anv_vector_offset += (queue)->element_size)
234
235 struct anv_bo {
236 int gem_handle;
237
238 /* Index into the current validation list. This is used by the
239 * validation list building alrogithm to track which buffers are already
240 * in the validation list so that we can ensure uniqueness.
241 */
242 uint32_t index;
243
244 /* Last known offset. This value is provided by the kernel when we
245 * execbuf and is used as the presumed offset for the next bunch of
246 * relocations.
247 */
248 uint64_t offset;
249
250 uint64_t size;
251 void *map;
252 };
253
254 /* Represents a lock-free linked list of "free" things. This is used by
255 * both the block pool and the state pools. Unfortunately, in order to
256 * solve the ABA problem, we can't use a single uint32_t head.
257 */
258 union anv_free_list {
259 struct {
260 int32_t offset;
261
262 /* A simple count that is incremented every time the head changes. */
263 uint32_t count;
264 };
265 uint64_t u64;
266 };
267
268 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
269
270 struct anv_block_state {
271 union {
272 struct {
273 uint32_t next;
274 uint32_t end;
275 };
276 uint64_t u64;
277 };
278 };
279
280 struct anv_block_pool {
281 struct anv_device *device;
282
283 struct anv_bo bo;
284
285 /* The offset from the start of the bo to the "center" of the block
286 * pool. Pointers to allocated blocks are given by
287 * bo.map + center_bo_offset + offsets.
288 */
289 uint32_t center_bo_offset;
290
291 /* Current memory map of the block pool. This pointer may or may not
292 * point to the actual beginning of the block pool memory. If
293 * anv_block_pool_alloc_back has ever been called, then this pointer
294 * will point to the "center" position of the buffer and all offsets
295 * (negative or positive) given out by the block pool alloc functions
296 * will be valid relative to this pointer.
297 *
298 * In particular, map == bo.map + center_offset
299 */
300 void *map;
301 int fd;
302
303 /**
304 * Array of mmaps and gem handles owned by the block pool, reclaimed when
305 * the block pool is destroyed.
306 */
307 struct anv_vector mmap_cleanups;
308
309 uint32_t block_size;
310
311 union anv_free_list free_list;
312 struct anv_block_state state;
313
314 union anv_free_list back_free_list;
315 struct anv_block_state back_state;
316 };
317
318 /* Block pools are backed by a fixed-size 2GB memfd */
319 #define BLOCK_POOL_MEMFD_SIZE (1ull << 32)
320
321 /* The center of the block pool is also the middle of the memfd. This may
322 * change in the future if we decide differently for some reason.
323 */
324 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
325
326 static inline uint32_t
327 anv_block_pool_size(struct anv_block_pool *pool)
328 {
329 return pool->state.end + pool->back_state.end;
330 }
331
332 struct anv_state {
333 int32_t offset;
334 uint32_t alloc_size;
335 void *map;
336 };
337
338 struct anv_fixed_size_state_pool {
339 size_t state_size;
340 union anv_free_list free_list;
341 struct anv_block_state block;
342 };
343
344 #define ANV_MIN_STATE_SIZE_LOG2 6
345 #define ANV_MAX_STATE_SIZE_LOG2 10
346
347 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2)
348
349 struct anv_state_pool {
350 struct anv_block_pool *block_pool;
351 struct anv_fixed_size_state_pool buckets[ANV_STATE_BUCKETS];
352 };
353
354 struct anv_state_stream {
355 struct anv_block_pool *block_pool;
356 uint32_t next;
357 uint32_t current_block;
358 uint32_t end;
359 };
360
361 void anv_block_pool_init(struct anv_block_pool *pool,
362 struct anv_device *device, uint32_t block_size);
363 void anv_block_pool_finish(struct anv_block_pool *pool);
364 int32_t anv_block_pool_alloc(struct anv_block_pool *pool);
365 int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool);
366 void anv_block_pool_free(struct anv_block_pool *pool, int32_t offset);
367 void anv_state_pool_init(struct anv_state_pool *pool,
368 struct anv_block_pool *block_pool);
369 void anv_state_pool_finish(struct anv_state_pool *pool);
370 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
371 size_t state_size, size_t alignment);
372 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
373 void anv_state_stream_init(struct anv_state_stream *stream,
374 struct anv_block_pool *block_pool);
375 void anv_state_stream_finish(struct anv_state_stream *stream);
376 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
377 uint32_t size, uint32_t alignment);
378
379 /**
380 * Implements a pool of re-usable BOs. The interface is identical to that
381 * of block_pool except that each block is its own BO.
382 */
383 struct anv_bo_pool {
384 struct anv_device *device;
385
386 uint32_t bo_size;
387
388 void *free_list;
389 };
390
391 void anv_bo_pool_init(struct anv_bo_pool *pool,
392 struct anv_device *device, uint32_t block_size);
393 void anv_bo_pool_finish(struct anv_bo_pool *pool);
394 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo);
395 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
396
397
398 void *anv_resolve_entrypoint(uint32_t index);
399
400 extern struct anv_dispatch_table dtable;
401
402 #define ANV_CALL(func) ({ \
403 if (dtable.func == NULL) { \
404 size_t idx = offsetof(struct anv_dispatch_table, func) / sizeof(void *); \
405 dtable.entrypoints[idx] = anv_resolve_entrypoint(idx); \
406 } \
407 dtable.func; \
408 })
409
410
411 struct anv_physical_device {
412 VK_LOADER_DATA _loader_data;
413
414 struct anv_instance * instance;
415 uint32_t chipset_id;
416 const char * path;
417 const char * name;
418 const struct brw_device_info * info;
419 uint64_t aperture_size;
420 };
421
422 struct anv_instance {
423 VK_LOADER_DATA _loader_data;
424
425 void * pAllocUserData;
426 PFN_vkAllocFunction pfnAlloc;
427 PFN_vkFreeFunction pfnFree;
428 uint32_t apiVersion;
429 uint32_t physicalDeviceCount;
430 struct anv_physical_device physicalDevice;
431
432 struct anv_wsi_implementation * wsi_impl[VK_PLATFORM_NUM_KHR];
433 };
434
435 VkResult anv_init_wsi(struct anv_instance *instance);
436 void anv_finish_wsi(struct anv_instance *instance);
437
438 struct anv_meta_state {
439 struct {
440 VkPipeline pipeline;
441 } clear;
442
443 struct {
444 /** Pipeline that blits from a 2D image. */
445 VkPipeline pipeline_2d_src;
446
447 /** Pipeline that blits from a 3D image. */
448 VkPipeline pipeline_3d_src;
449
450 VkPipelineLayout pipeline_layout;
451 VkDescriptorSetLayout ds_layout;
452 } blit;
453 };
454
455 struct anv_queue {
456 VK_LOADER_DATA _loader_data;
457
458 struct anv_device * device;
459
460 struct anv_state_pool * pool;
461 };
462
463 struct anv_device {
464 VK_LOADER_DATA _loader_data;
465
466 struct anv_instance * instance;
467 uint32_t chipset_id;
468 struct brw_device_info info;
469 int context_id;
470 int fd;
471
472 struct anv_bo_pool batch_bo_pool;
473
474 struct anv_block_pool dynamic_state_block_pool;
475 struct anv_state_pool dynamic_state_pool;
476
477 struct anv_block_pool instruction_block_pool;
478 struct anv_block_pool surface_state_block_pool;
479 struct anv_state_pool surface_state_pool;
480
481 struct anv_meta_state meta_state;
482
483 struct anv_state border_colors;
484
485 struct anv_queue queue;
486
487 struct anv_block_pool scratch_block_pool;
488
489 struct anv_compiler * compiler;
490 pthread_mutex_t mutex;
491 };
492
493 void *
494 anv_instance_alloc(struct anv_instance * instance,
495 size_t size,
496 size_t alignment,
497 VkSystemAllocType allocType);
498
499 void
500 anv_instance_free(struct anv_instance * instance,
501 void * mem);
502
503 void *
504 anv_device_alloc(struct anv_device * device,
505 size_t size,
506 size_t alignment,
507 VkSystemAllocType allocType);
508
509 void
510 anv_device_free(struct anv_device * device,
511 void * mem);
512
513 void* anv_gem_mmap(struct anv_device *device,
514 uint32_t gem_handle, uint64_t offset, uint64_t size);
515 void anv_gem_munmap(void *p, uint64_t size);
516 uint32_t anv_gem_create(struct anv_device *device, size_t size);
517 void anv_gem_close(struct anv_device *device, int gem_handle);
518 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
519 int anv_gem_wait(struct anv_device *device, int gem_handle, int64_t *timeout_ns);
520 int anv_gem_execbuffer(struct anv_device *device,
521 struct drm_i915_gem_execbuffer2 *execbuf);
522 int anv_gem_set_tiling(struct anv_device *device, int gem_handle,
523 uint32_t stride, uint32_t tiling);
524 int anv_gem_create_context(struct anv_device *device);
525 int anv_gem_destroy_context(struct anv_device *device, int context);
526 int anv_gem_get_param(int fd, uint32_t param);
527 int anv_gem_get_aperture(int fd, uint64_t *size);
528 int anv_gem_handle_to_fd(struct anv_device *device, int gem_handle);
529 int anv_gem_fd_to_handle(struct anv_device *device, int fd);
530 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
531
532 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
533
534 struct anv_reloc_list {
535 size_t num_relocs;
536 size_t array_length;
537 struct drm_i915_gem_relocation_entry * relocs;
538 struct anv_bo ** reloc_bos;
539 };
540
541 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
542 struct anv_device *device);
543 void anv_reloc_list_finish(struct anv_reloc_list *list,
544 struct anv_device *device);
545
546 uint64_t anv_reloc_list_add(struct anv_reloc_list *list,
547 struct anv_device *device,
548 uint32_t offset, struct anv_bo *target_bo,
549 uint32_t delta);
550
551 struct anv_batch_bo {
552 /* Link in the anv_cmd_buffer.owned_batch_bos list */
553 struct list_head link;
554
555 struct anv_bo bo;
556
557 /* Bytes actually consumed in this batch BO */
558 size_t length;
559
560 /* Last seen surface state block pool bo offset */
561 uint32_t last_ss_pool_bo_offset;
562
563 struct anv_reloc_list relocs;
564 };
565
566 struct anv_batch {
567 struct anv_device * device;
568
569 void * start;
570 void * end;
571 void * next;
572
573 struct anv_reloc_list * relocs;
574
575 /* This callback is called (with the associated user data) in the event
576 * that the batch runs out of space.
577 */
578 VkResult (*extend_cb)(struct anv_batch *, void *);
579 void * user_data;
580 };
581
582 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
583 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
584 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
585 void *location, struct anv_bo *bo, uint32_t offset);
586
587 struct anv_address {
588 struct anv_bo *bo;
589 uint32_t offset;
590 };
591
592 #define __gen_address_type struct anv_address
593 #define __gen_user_data struct anv_batch
594
595 static inline uint64_t
596 __gen_combine_address(struct anv_batch *batch, void *location,
597 const struct anv_address address, uint32_t delta)
598 {
599 if (address.bo == NULL) {
600 return address.offset + delta;
601 } else {
602 assert(batch->start <= location && location < batch->end);
603
604 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
605 }
606 }
607
608 #include "gen7_pack.h"
609 #include "gen75_pack.h"
610 #undef GEN8_3DSTATE_MULTISAMPLE
611 #include "gen8_pack.h"
612
613 #define anv_batch_emit(batch, cmd, ...) do { \
614 void *__dst = anv_batch_emit_dwords(batch, cmd ## _length); \
615 struct cmd __template = { \
616 cmd ## _header, \
617 __VA_ARGS__ \
618 }; \
619 cmd ## _pack(batch, __dst, &__template); \
620 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, cmd ## _length * 4)); \
621 } while (0)
622
623 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
624 void *__dst = anv_batch_emit_dwords(batch, n); \
625 struct cmd __template = { \
626 cmd ## _header, \
627 .DwordLength = n - cmd ## _length_bias, \
628 __VA_ARGS__ \
629 }; \
630 cmd ## _pack(batch, __dst, &__template); \
631 __dst; \
632 })
633
634 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
635 do { \
636 uint32_t *dw; \
637 \
638 assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
639 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
640 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
641 dw[i] = (dwords0)[i] | (dwords1)[i]; \
642 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
643 } while (0)
644
645 static const struct GEN7_MEMORY_OBJECT_CONTROL_STATE GEN7_MOCS = {
646 .GraphicsDataTypeGFDT = 0,
647 .LLCCacheabilityControlLLCCC = 0,
648 .L3CacheabilityControlL3CC = 1
649 };
650
651 #define GEN8_MOCS { \
652 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
653 .TargetCache = L3DefertoPATforLLCeLLCselection, \
654 .AgeforQUADLRU = 0 \
655 }
656
657 struct anv_device_memory {
658 struct anv_bo bo;
659 VkDeviceSize map_size;
660 void * map;
661 };
662
663 struct anv_descriptor_set_binding_layout {
664 /* Number of array elements in this binding */
665 uint16_t array_size;
666
667 /* Index into the dynamic state array for a dynamic buffer */
668 int16_t dynamic_offset_index;
669
670 struct {
671 /* Index into the binding table for the associated surface */
672 int16_t surface_index;
673
674 /* Index into the sampler table for the associated sampler */
675 int16_t sampler_index;
676 } stage[VK_SHADER_STAGE_NUM];
677 };
678
679 struct anv_descriptor_set_layout {
680 /* Number of bindings in this descriptor set */
681 uint16_t binding_count;
682
683 /* Total size of the descriptor set with room for all array entries */
684 uint16_t size;
685
686 /* Shader stages affected by this descriptor set */
687 uint16_t shader_stages;
688
689 /* Number of dynamic offsets used by this descriptor set */
690 uint16_t dynamic_offset_count;
691
692 /* Don't use this directly */
693 struct anv_descriptor_set_binding_layout binding[0];
694 };
695
696 enum anv_descriptor_type {
697 ANV_DESCRIPTOR_TYPE_EMPTY = 0,
698 ANV_DESCRIPTOR_TYPE_BUFFER_VIEW,
699 ANV_DESCRIPTOR_TYPE_BUFFER_AND_OFFSET,
700 ANV_DESCRIPTOR_TYPE_IMAGE_VIEW,
701 ANV_DESCRIPTOR_TYPE_SAMPLER,
702 ANV_DESCRIPTOR_TYPE_IMAGE_VIEW_AND_SAMPLER,
703 };
704
705 struct anv_descriptor {
706 enum anv_descriptor_type type;
707
708 union {
709 struct {
710 union {
711 struct anv_buffer_view *buffer_view;
712 struct anv_image_view *image_view;
713 };
714 struct anv_sampler *sampler;
715 };
716
717 struct {
718 struct anv_buffer *buffer;
719 uint64_t offset;
720 uint64_t range;
721 };
722 };
723 };
724
725 struct anv_descriptor_set {
726 struct anv_descriptor descriptors[0];
727 };
728
729 VkResult
730 anv_descriptor_set_create(struct anv_device *device,
731 const struct anv_descriptor_set_layout *layout,
732 struct anv_descriptor_set **out_set);
733
734 void
735 anv_descriptor_set_destroy(struct anv_device *device,
736 struct anv_descriptor_set *set);
737
738 #define MAX_VBS 32
739 #define MAX_SETS 8
740 #define MAX_RTS 8
741 #define MAX_VIEWPORTS 16
742 #define MAX_SCISSORS 16
743 #define MAX_PUSH_CONSTANTS_SIZE 128
744 #define MAX_DYNAMIC_BUFFERS 16
745 #define MAX_IMAGES 8
746
747 struct anv_pipeline_binding {
748 /* The descriptor set this surface corresponds to */
749 uint16_t set;
750
751 /* Offset into the descriptor set */
752 uint16_t offset;
753 };
754
755 struct anv_pipeline_layout {
756 struct {
757 struct anv_descriptor_set_layout *layout;
758 uint32_t dynamic_offset_start;
759 struct {
760 uint32_t surface_start;
761 uint32_t sampler_start;
762 } stage[VK_SHADER_STAGE_NUM];
763 } set[MAX_SETS];
764
765 uint32_t num_sets;
766
767 struct {
768 bool has_dynamic_offsets;
769 uint32_t surface_count;
770 struct anv_pipeline_binding *surface_to_descriptor;
771 uint32_t sampler_count;
772 struct anv_pipeline_binding *sampler_to_descriptor;
773 } stage[VK_SHADER_STAGE_NUM];
774
775 struct anv_pipeline_binding entries[0];
776 };
777
778 struct anv_buffer {
779 struct anv_device * device;
780 VkDeviceSize size;
781
782 /* Set when bound */
783 struct anv_bo * bo;
784 VkDeviceSize offset;
785 };
786
787 /* The first 9 correspond to 1 << VK_DYNAMIC_STATE_FOO */
788 #define ANV_DYNAMIC_VIEWPORT_DIRTY (1 << 0)
789 #define ANV_DYNAMIC_SCISSOR_DIRTY (1 << 1)
790 #define ANV_DYNAMIC_LINE_WIDTH_DIRTY (1 << 2)
791 #define ANV_DYNAMIC_DEPTH_BIAS_DIRTY (1 << 3)
792 #define ANV_DYNAMIC_BLEND_CONSTANTS_DIRTY (1 << 4)
793 #define ANV_DYNAMIC_DEPTH_BOUNDS_DIRTY (1 << 5)
794 #define ANV_DYNAMIC_STENCIL_COMPARE_MASK_DIRTY (1 << 6)
795 #define ANV_DYNAMIC_STENCIL_WRITE_MASK_DIRTY (1 << 7)
796 #define ANV_DYNAMIC_STENCIL_REFERENCE_DIRTY (1 << 8)
797 #define ANV_DYNAMIC_STATE_DIRTY_MASK ((1 << 9) - 1)
798 #define ANV_CMD_BUFFER_PIPELINE_DIRTY (1 << 9)
799 #define ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY (1 << 10)
800
801 struct anv_vertex_binding {
802 struct anv_buffer * buffer;
803 VkDeviceSize offset;
804 };
805
806 struct anv_push_constants {
807 /* Current allocated size of this push constants data structure.
808 * Because a decent chunk of it may not be used (images on SKL, for
809 * instance), we won't actually allocate the entire structure up-front.
810 */
811 uint32_t size;
812
813 /* Push constant data provided by the client through vkPushConstants */
814 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
815
816 /* Our hardware only provides zero-based vertex and instance id so, in
817 * order to satisfy the vulkan requirements, we may have to push one or
818 * both of these into the shader.
819 */
820 uint32_t base_vertex;
821 uint32_t base_instance;
822
823 /* Offsets for dynamically bound buffers */
824 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
825
826 /* Image data for image_load_store on pre-SKL */
827 struct brw_image_param images[MAX_IMAGES];
828 };
829
830 struct anv_dynamic_state {
831 struct {
832 uint32_t count;
833 VkViewport viewports[MAX_VIEWPORTS];
834 } viewport;
835
836 struct {
837 uint32_t count;
838 VkRect2D scissors[MAX_SCISSORS];
839 } scissor;
840
841 float line_width;
842
843 struct {
844 float bias;
845 float clamp;
846 float slope_scaled;
847 } depth_bias;
848
849 float blend_constants[4];
850
851 struct {
852 float min;
853 float max;
854 } depth_bounds;
855
856 struct {
857 uint32_t front;
858 uint32_t back;
859 } stencil_compare_mask;
860
861 struct {
862 uint32_t front;
863 uint32_t back;
864 } stencil_write_mask;
865
866 struct {
867 uint32_t front;
868 uint32_t back;
869 } stencil_reference;
870 };
871
872 extern const struct anv_dynamic_state default_dynamic_state;
873
874 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
875 const struct anv_dynamic_state *src,
876 uint32_t copy_mask);
877
878 /** State required while building cmd buffer */
879 struct anv_cmd_state {
880 uint32_t current_pipeline;
881 uint32_t vb_dirty;
882 uint32_t dirty;
883 uint32_t compute_dirty;
884 VkShaderStageFlags descriptors_dirty;
885 VkShaderStageFlags push_constants_dirty;
886 uint32_t scratch_size;
887 struct anv_pipeline * pipeline;
888 struct anv_pipeline * compute_pipeline;
889 struct anv_framebuffer * framebuffer;
890 struct anv_render_pass * pass;
891 struct anv_subpass * subpass;
892 uint32_t state_vf[GEN8_3DSTATE_VF_length];
893 struct anv_vertex_binding vertex_bindings[MAX_VBS];
894 struct anv_descriptor_set * descriptors[MAX_SETS];
895 struct anv_push_constants * push_constants[VK_SHADER_STAGE_NUM];
896 struct anv_dynamic_state dynamic;
897
898 struct {
899 struct anv_buffer * index_buffer;
900 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
901 uint32_t index_offset;
902 } gen7;
903 };
904
905 struct anv_cmd_pool {
906 struct list_head cmd_buffers;
907 };
908
909 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
910
911 enum anv_cmd_buffer_exec_mode {
912 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
913 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
914 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
915 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
916 };
917
918 struct anv_cmd_buffer {
919 VK_LOADER_DATA _loader_data;
920
921 struct anv_device * device;
922
923 struct list_head pool_link;
924
925 struct anv_batch batch;
926
927 /* Fields required for the actual chain of anv_batch_bo's.
928 *
929 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
930 */
931 struct list_head batch_bos;
932 enum anv_cmd_buffer_exec_mode exec_mode;
933
934 /* A vector of anv_batch_bo pointers for every batch or surface buffer
935 * referenced by this command buffer
936 *
937 * initialized by anv_cmd_buffer_init_batch_bo_chain()
938 */
939 struct anv_vector seen_bbos;
940
941 /* A vector of int32_t's for every block of binding tables.
942 *
943 * initialized by anv_cmd_buffer_init_batch_bo_chain()
944 */
945 struct anv_vector bt_blocks;
946 uint32_t bt_next;
947 struct anv_reloc_list surface_relocs;
948
949 /* Information needed for execbuf
950 *
951 * These fields are generated by anv_cmd_buffer_prepare_execbuf().
952 */
953 struct {
954 struct drm_i915_gem_execbuffer2 execbuf;
955
956 struct drm_i915_gem_exec_object2 * objects;
957 uint32_t bo_count;
958 struct anv_bo ** bos;
959
960 /* Allocated length of the 'objects' and 'bos' arrays */
961 uint32_t array_length;
962
963 bool need_reloc;
964 } execbuf2;
965
966 /* Serial for tracking buffer completion */
967 uint32_t serial;
968
969 /* Stream objects for storing temporary data */
970 struct anv_state_stream surface_state_stream;
971 struct anv_state_stream dynamic_state_stream;
972
973 VkCmdBufferOptimizeFlags opt_flags;
974 VkCmdBufferLevel level;
975
976 struct anv_cmd_state state;
977 };
978
979 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
980 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
981 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
982 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
983 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
984 struct anv_cmd_buffer *secondary);
985 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
986
987 VkResult anv_cmd_buffer_emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
988 unsigned stage, struct anv_state *bt_state);
989 VkResult anv_cmd_buffer_emit_samplers(struct anv_cmd_buffer *cmd_buffer,
990 unsigned stage, struct anv_state *state);
991 void anv_flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer);
992
993 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
994 uint32_t *a, uint32_t dwords,
995 uint32_t alignment);
996 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
997 uint32_t *a, uint32_t *b,
998 uint32_t dwords, uint32_t alignment);
999 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1000 struct anv_subpass *subpass);
1001
1002 struct anv_address
1003 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1004 struct anv_state
1005 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1006 uint32_t entries, uint32_t *state_offset);
1007 struct anv_state
1008 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1009 struct anv_state
1010 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1011 uint32_t size, uint32_t alignment);
1012
1013 VkResult
1014 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1015
1016 void anv_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1017 void anv_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1018
1019 void gen7_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1020 void gen8_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1021
1022 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1023
1024 void gen7_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1025 struct anv_subpass *subpass);
1026
1027 void gen8_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1028 struct anv_subpass *subpass);
1029
1030 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
1031 struct anv_subpass *subpass);
1032
1033 struct anv_state
1034 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1035 VkShaderStage stage);
1036
1037 void anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
1038 struct anv_render_pass *pass,
1039 const VkClearValue *clear_values);
1040 const struct anv_image_view *
1041 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1042
1043 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1044
1045 struct anv_fence {
1046 struct anv_bo bo;
1047 struct drm_i915_gem_execbuffer2 execbuf;
1048 struct drm_i915_gem_exec_object2 exec2_objects[1];
1049 bool ready;
1050 };
1051
1052 struct nir_shader;
1053
1054 struct anv_shader_module {
1055 struct nir_shader * nir;
1056
1057 uint32_t size;
1058 char data[0];
1059 };
1060
1061 struct anv_shader {
1062 struct anv_shader_module * module;
1063 char entrypoint[0];
1064 };
1065
1066 struct anv_pipeline {
1067 struct anv_device * device;
1068 struct anv_batch batch;
1069 uint32_t batch_data[256];
1070 struct anv_reloc_list batch_relocs;
1071 uint32_t dynamic_state_mask;
1072 struct anv_dynamic_state dynamic_state;
1073
1074 struct anv_shader * shaders[VK_SHADER_STAGE_NUM];
1075 struct anv_pipeline_layout * layout;
1076 bool use_repclear;
1077
1078 struct brw_vs_prog_data vs_prog_data;
1079 struct brw_wm_prog_data wm_prog_data;
1080 struct brw_gs_prog_data gs_prog_data;
1081 struct brw_cs_prog_data cs_prog_data;
1082 bool writes_point_size;
1083 struct brw_stage_prog_data * prog_data[VK_SHADER_STAGE_NUM];
1084 uint32_t scratch_start[VK_SHADER_STAGE_NUM];
1085 uint32_t total_scratch;
1086 struct {
1087 uint32_t vs_start;
1088 uint32_t vs_size;
1089 uint32_t nr_vs_entries;
1090 uint32_t gs_start;
1091 uint32_t gs_size;
1092 uint32_t nr_gs_entries;
1093 } urb;
1094
1095 VkShaderStageFlags active_stages;
1096 struct anv_state_stream program_stream;
1097 struct anv_state blend_state;
1098 uint32_t vs_simd8;
1099 uint32_t vs_vec4;
1100 uint32_t ps_simd8;
1101 uint32_t ps_simd16;
1102 uint32_t ps_ksp0;
1103 uint32_t ps_ksp2;
1104 uint32_t ps_grf_start0;
1105 uint32_t ps_grf_start2;
1106 uint32_t gs_vec4;
1107 uint32_t gs_vertex_count;
1108 uint32_t cs_simd;
1109
1110 uint32_t vb_used;
1111 uint32_t binding_stride[MAX_VBS];
1112 bool instancing_enable[MAX_VBS];
1113 bool primitive_restart;
1114 uint32_t topology;
1115
1116 uint32_t cs_thread_width_max;
1117 uint32_t cs_right_mask;
1118
1119 struct {
1120 uint32_t sf[GEN7_3DSTATE_SF_length];
1121 uint32_t depth_stencil_state[GEN7_DEPTH_STENCIL_STATE_length];
1122 } gen7;
1123
1124 struct {
1125 uint32_t sf[GEN8_3DSTATE_SF_length];
1126 uint32_t vf[GEN8_3DSTATE_VF_length];
1127 uint32_t raster[GEN8_3DSTATE_RASTER_length];
1128 uint32_t wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
1129 } gen8;
1130 };
1131
1132 struct anv_graphics_pipeline_create_info {
1133 bool use_repclear;
1134 bool disable_viewport;
1135 bool disable_scissor;
1136 bool disable_vs;
1137 bool use_rectlist;
1138 };
1139
1140 VkResult
1141 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1142 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1143 const struct anv_graphics_pipeline_create_info *extra);
1144
1145 VkResult
1146 anv_graphics_pipeline_create(VkDevice device,
1147 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1148 const struct anv_graphics_pipeline_create_info *extra,
1149 VkPipeline *pPipeline);
1150
1151 VkResult
1152 gen7_graphics_pipeline_create(VkDevice _device,
1153 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1154 const struct anv_graphics_pipeline_create_info *extra,
1155 VkPipeline *pPipeline);
1156
1157 VkResult
1158 gen8_graphics_pipeline_create(VkDevice _device,
1159 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1160 const struct anv_graphics_pipeline_create_info *extra,
1161 VkPipeline *pPipeline);
1162 VkResult
1163 gen7_compute_pipeline_create(VkDevice _device,
1164 const VkComputePipelineCreateInfo *pCreateInfo,
1165 VkPipeline *pPipeline);
1166
1167 VkResult
1168 gen8_compute_pipeline_create(VkDevice _device,
1169 const VkComputePipelineCreateInfo *pCreateInfo,
1170 VkPipeline *pPipeline);
1171
1172 struct anv_compiler *anv_compiler_create(struct anv_device *device);
1173 void anv_compiler_destroy(struct anv_compiler *compiler);
1174 int anv_compiler_run(struct anv_compiler *compiler, struct anv_pipeline *pipeline);
1175 void anv_compiler_free(struct anv_pipeline *pipeline);
1176
1177 struct anv_format {
1178 const VkFormat vk_format;
1179 const char *name;
1180 uint16_t surface_format; /**< RENDER_SURFACE_STATE.SurfaceFormat */
1181 uint8_t cpp; /**< Bytes-per-pixel of anv_format::surface_format. */
1182 uint8_t num_channels;
1183 uint16_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
1184 bool has_stencil;
1185 };
1186
1187 /**
1188 * Stencil formats are often a special case. To reduce the number of lookups
1189 * into the VkFormat-to-anv_format translation table when working with
1190 * stencil, here is the handle to the table's entry for VK_FORMAT_S8_UINT.
1191 */
1192 extern const struct anv_format *const anv_format_s8_uint;
1193
1194 const struct anv_format *
1195 anv_format_for_vk_format(VkFormat format);
1196
1197 static inline bool
1198 anv_format_is_color(const struct anv_format *format)
1199 {
1200 return !format->depth_format && !format->has_stencil;
1201 }
1202
1203 static inline bool
1204 anv_format_is_depth_or_stencil(const struct anv_format *format)
1205 {
1206 return format->depth_format || format->has_stencil;
1207 }
1208
1209 struct anv_image_view_info {
1210 uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
1211 bool is_array:1; /**< RENDER_SURFACE_STATE.SurfaceArray */
1212 bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
1213 };
1214
1215 struct anv_image_view_info
1216 anv_image_view_info_for_vk_image_view_type(VkImageViewType type);
1217
1218 /**
1219 * A proxy for the color surfaces, depth surfaces, and stencil surfaces.
1220 */
1221 struct anv_surface {
1222 /**
1223 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1224 */
1225 uint32_t offset;
1226
1227 uint32_t stride; /**< RENDER_SURFACE_STATE.SurfacePitch */
1228 uint16_t qpitch; /**< RENDER_SURFACE_STATE.QPitch */
1229
1230 /**
1231 * \name Alignment of miptree images, in units of pixels.
1232 *
1233 * These fields contain the real alignment values, not the values to be
1234 * given to the GPU. For example, if h_align is 4, then program the GPU
1235 * with HALIGN_4.
1236 * \{
1237 */
1238 uint8_t h_align; /**< RENDER_SURFACE_STATE.SurfaceHorizontalAlignment */
1239 uint8_t v_align; /**< RENDER_SURFACE_STATE.SurfaceVerticalAlignment */
1240 /** \} */
1241
1242 uint8_t tile_mode; /**< RENDER_SURFACE_STATE.TileMode */
1243 };
1244
1245 struct anv_image {
1246 VkImageType type;
1247 const struct anv_format *format;
1248 VkExtent3D extent;
1249 uint32_t levels;
1250 uint32_t array_size;
1251 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1252
1253 VkDeviceSize size;
1254 uint32_t alignment;
1255
1256 /* Set when bound */
1257 struct anv_bo *bo;
1258 VkDeviceSize offset;
1259
1260 uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
1261
1262 bool needs_nonrt_surface_state:1;
1263 bool needs_color_rt_surface_state:1;
1264
1265 /**
1266 * Image subsurfaces
1267 *
1268 * For each foo, anv_image::foo_surface is valid if and only if
1269 * anv_image::format has a foo aspect.
1270 *
1271 * The hardware requires that the depth buffer and stencil buffer be
1272 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1273 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1274 * allocate the depth and stencil buffers as separate surfaces in the same
1275 * bo.
1276 */
1277 union {
1278 struct anv_surface color_surface;
1279
1280 struct {
1281 struct anv_surface depth_surface;
1282 struct anv_surface stencil_surface;
1283 };
1284 };
1285 };
1286
1287 struct anv_buffer_view {
1288 struct anv_state surface_state; /**< RENDER_SURFACE_STATE */
1289 struct anv_bo *bo;
1290 uint32_t offset; /**< Offset into bo. */
1291 uint32_t range; /**< VkBufferViewCreateInfo::range */
1292 const struct anv_format *format; /**< VkBufferViewCreateInfo::format */
1293 };
1294
1295 struct anv_image_view {
1296 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
1297 const struct anv_format *format; /**< VkImageViewCreateInfo::format */
1298 struct anv_bo *bo;
1299 uint32_t offset; /**< Offset into bo. */
1300 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1301
1302 /** RENDER_SURFACE_STATE when using image as a color render target. */
1303 struct anv_state color_rt_surface_state;
1304
1305 /** RENDER_SURFACE_STATE when using image as a non render target. */
1306 struct anv_state nonrt_surface_state;
1307 };
1308
1309 struct anv_image_create_info {
1310 const VkImageCreateInfo *vk_info;
1311 bool force_tile_mode;
1312 uint8_t tile_mode;
1313 uint32_t stride;
1314 };
1315
1316 VkResult anv_image_create(VkDevice _device,
1317 const struct anv_image_create_info *info,
1318 VkImage *pImage);
1319
1320 struct anv_surface *
1321 anv_image_get_surface_for_aspect_mask(struct anv_image *image,
1322 VkImageAspectFlags aspect_mask);
1323
1324 void anv_image_view_init(struct anv_image_view *view,
1325 struct anv_device *device,
1326 const VkImageViewCreateInfo* pCreateInfo,
1327 struct anv_cmd_buffer *cmd_buffer);
1328
1329 void
1330 gen7_image_view_init(struct anv_image_view *iview,
1331 struct anv_device *device,
1332 const VkImageViewCreateInfo* pCreateInfo,
1333 struct anv_cmd_buffer *cmd_buffer);
1334
1335 void
1336 gen8_image_view_init(struct anv_image_view *iview,
1337 struct anv_device *device,
1338 const VkImageViewCreateInfo* pCreateInfo,
1339 struct anv_cmd_buffer *cmd_buffer);
1340
1341 VkResult anv_buffer_view_create(struct anv_device *device,
1342 const VkBufferViewCreateInfo *pCreateInfo,
1343 struct anv_buffer_view **bview_out);
1344
1345 void anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1346 const struct anv_format *format,
1347 uint32_t offset, uint32_t range);
1348
1349 void gen7_fill_buffer_surface_state(void *state, const struct anv_format *format,
1350 uint32_t offset, uint32_t range);
1351 void gen8_fill_buffer_surface_state(void *state, const struct anv_format *format,
1352 uint32_t offset, uint32_t range);
1353
1354 struct anv_sampler {
1355 uint32_t state[4];
1356 };
1357
1358 struct anv_framebuffer {
1359 uint32_t width;
1360 uint32_t height;
1361 uint32_t layers;
1362
1363 uint32_t attachment_count;
1364 const struct anv_image_view * attachments[0];
1365 };
1366
1367 struct anv_subpass {
1368 uint32_t input_count;
1369 uint32_t * input_attachments;
1370 uint32_t color_count;
1371 uint32_t * color_attachments;
1372 uint32_t * resolve_attachments;
1373 uint32_t depth_stencil_attachment;
1374 };
1375
1376 struct anv_render_pass_attachment {
1377 const struct anv_format *format;
1378 uint32_t samples;
1379 VkAttachmentLoadOp load_op;
1380 VkAttachmentLoadOp stencil_load_op;
1381 };
1382
1383 struct anv_render_pass {
1384 uint32_t attachment_count;
1385 uint32_t subpass_count;
1386
1387 uint32_t num_color_clear_attachments;
1388 bool has_depth_clear_attachment;
1389 bool has_stencil_clear_attachment;
1390
1391 struct anv_render_pass_attachment * attachments;
1392 struct anv_subpass subpasses[0];
1393 };
1394
1395 struct anv_query_pool_slot {
1396 uint64_t begin;
1397 uint64_t end;
1398 uint64_t available;
1399 };
1400
1401 struct anv_query_pool {
1402 VkQueryType type;
1403 uint32_t slots;
1404 struct anv_bo bo;
1405 };
1406
1407 void anv_device_init_meta(struct anv_device *device);
1408 void anv_device_finish_meta(struct anv_device *device);
1409
1410 void *anv_lookup_entrypoint(const char *name);
1411
1412 void anv_dump_image_to_ppm(struct anv_device *device,
1413 struct anv_image *image, unsigned miplevel,
1414 unsigned array_layer, const char *filename);
1415
1416 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
1417 \
1418 static inline struct __anv_type * \
1419 __anv_type ## _from_handle(__VkType _handle) \
1420 { \
1421 return (struct __anv_type *) _handle; \
1422 } \
1423 \
1424 static inline __VkType \
1425 __anv_type ## _to_handle(struct __anv_type *_obj) \
1426 { \
1427 return (__VkType) _obj; \
1428 }
1429
1430 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
1431 \
1432 static inline struct __anv_type * \
1433 __anv_type ## _from_handle(__VkType _handle) \
1434 { \
1435 return (struct __anv_type *) _handle.handle; \
1436 } \
1437 \
1438 static inline __VkType \
1439 __anv_type ## _to_handle(struct __anv_type *_obj) \
1440 { \
1441 return (__VkType) { .handle = (uint64_t) _obj }; \
1442 }
1443
1444 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
1445 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
1446
1447 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCmdBuffer)
1448 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
1449 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
1450 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
1451 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
1452
1453 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCmdPool)
1454 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
1455 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView);
1456 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
1457 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
1458 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
1459 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
1460 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
1461 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
1462 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
1463 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
1464 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
1465 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
1466 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
1467 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
1468 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader, VkShader)
1469 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
1470
1471 #define ANV_DEFINE_STRUCT_CASTS(__anv_type, __VkType) \
1472 \
1473 static inline const __VkType * \
1474 __anv_type ## _to_ ## __VkType(const struct __anv_type *__anv_obj) \
1475 { \
1476 return (const __VkType *) __anv_obj; \
1477 }
1478
1479 #define ANV_COMMON_TO_STRUCT(__VkType, __vk_name, __common_name) \
1480 const __VkType *__vk_name = anv_common_to_ ## __VkType(__common_name)
1481
1482 ANV_DEFINE_STRUCT_CASTS(anv_common, VkMemoryBarrier)
1483 ANV_DEFINE_STRUCT_CASTS(anv_common, VkBufferMemoryBarrier)
1484 ANV_DEFINE_STRUCT_CASTS(anv_common, VkImageMemoryBarrier)
1485
1486 #ifdef __cplusplus
1487 }
1488 #endif