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