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