anv: Add helpers for converting access flags to pipe bits
[mesa.git] / src / intel / 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 #ifndef ANV_PRIVATE_H
25 #define ANV_PRIVATE_H
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <pthread.h>
31 #include <assert.h>
32 #include <stdint.h>
33 #include <i915_drm.h>
34
35 #ifdef HAVE_VALGRIND
36 #include <valgrind.h>
37 #include <memcheck.h>
38 #define VG(x) x
39 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
40 #else
41 #define VG(x)
42 #endif
43
44 #include "common/gen_device_info.h"
45 #include "blorp/blorp.h"
46 #include "compiler/brw_compiler.h"
47 #include "util/macros.h"
48 #include "util/list.h"
49 #include "util/u_vector.h"
50 #include "util/vk_alloc.h"
51
52 /* Pre-declarations needed for WSI entrypoints */
53 struct wl_surface;
54 struct wl_display;
55 typedef struct xcb_connection_t xcb_connection_t;
56 typedef uint32_t xcb_visualid_t;
57 typedef uint32_t xcb_window_t;
58
59 struct anv_buffer;
60 struct anv_buffer_view;
61 struct anv_image_view;
62
63 struct gen_l3_config;
64
65 #include <vulkan/vulkan.h>
66 #include <vulkan/vulkan_intel.h>
67 #include <vulkan/vk_icd.h>
68
69 #include "anv_entrypoints.h"
70 #include "isl/isl.h"
71
72 #include "common/gen_debug.h"
73 #include "wsi_common.h"
74
75 /* Allowing different clear colors requires us to perform a depth resolve at
76 * the end of certain render passes. This is because while slow clears store
77 * the clear color in the HiZ buffer, fast clears (without a resolve) don't.
78 * See the PRMs for examples describing when additional resolves would be
79 * necessary. To enable fast clears without requiring extra resolves, we set
80 * the clear value to a globally-defined one. We could allow different values
81 * if the user doesn't expect coherent data during or after a render passes
82 * (VK_ATTACHMENT_STORE_OP_DONT_CARE), but such users (aside from the CTS)
83 * don't seem to exist yet. In almost all Vulkan applications tested thus far,
84 * 1.0f seems to be the only value used. The only application that doesn't set
85 * this value does so through the usage of an seemingly uninitialized clear
86 * value.
87 */
88 #define ANV_HZ_FC_VAL 1.0f
89
90 #define MAX_VBS 31
91 #define MAX_SETS 8
92 #define MAX_RTS 8
93 #define MAX_VIEWPORTS 16
94 #define MAX_SCISSORS 16
95 #define MAX_PUSH_CONSTANTS_SIZE 128
96 #define MAX_DYNAMIC_BUFFERS 16
97 #define MAX_IMAGES 8
98 #define MAX_PUSH_DESCRIPTORS 32 /* Minimum requirement */
99
100 #define ANV_SVGS_VB_INDEX MAX_VBS
101 #define ANV_DRAWID_VB_INDEX (MAX_VBS + 1)
102
103 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
104
105 static inline uint32_t
106 align_down_npot_u32(uint32_t v, uint32_t a)
107 {
108 return v - (v % a);
109 }
110
111 static inline uint32_t
112 align_u32(uint32_t v, uint32_t a)
113 {
114 assert(a != 0 && a == (a & -a));
115 return (v + a - 1) & ~(a - 1);
116 }
117
118 static inline uint64_t
119 align_u64(uint64_t v, uint64_t a)
120 {
121 assert(a != 0 && a == (a & -a));
122 return (v + a - 1) & ~(a - 1);
123 }
124
125 static inline int32_t
126 align_i32(int32_t v, int32_t a)
127 {
128 assert(a != 0 && a == (a & -a));
129 return (v + a - 1) & ~(a - 1);
130 }
131
132 /** Alignment must be a power of 2. */
133 static inline bool
134 anv_is_aligned(uintmax_t n, uintmax_t a)
135 {
136 assert(a == (a & -a));
137 return (n & (a - 1)) == 0;
138 }
139
140 static inline uint32_t
141 anv_minify(uint32_t n, uint32_t levels)
142 {
143 if (unlikely(n == 0))
144 return 0;
145 else
146 return MAX2(n >> levels, 1);
147 }
148
149 static inline float
150 anv_clamp_f(float f, float min, float max)
151 {
152 assert(min < max);
153
154 if (f > max)
155 return max;
156 else if (f < min)
157 return min;
158 else
159 return f;
160 }
161
162 static inline bool
163 anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
164 {
165 if (*inout_mask & clear_mask) {
166 *inout_mask &= ~clear_mask;
167 return true;
168 } else {
169 return false;
170 }
171 }
172
173 static inline union isl_color_value
174 vk_to_isl_color(VkClearColorValue color)
175 {
176 return (union isl_color_value) {
177 .u32 = {
178 color.uint32[0],
179 color.uint32[1],
180 color.uint32[2],
181 color.uint32[3],
182 },
183 };
184 }
185
186 #define for_each_bit(b, dword) \
187 for (uint32_t __dword = (dword); \
188 (b) = __builtin_ffs(__dword) - 1, __dword; \
189 __dword &= ~(1 << (b)))
190
191 #define typed_memcpy(dest, src, count) ({ \
192 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
193 memcpy((dest), (src), (count) * sizeof(*(src))); \
194 })
195
196 /* Whenever we generate an error, pass it through this function. Useful for
197 * debugging, where we can break on it. Only call at error site, not when
198 * propagating errors. Might be useful to plug in a stack trace here.
199 */
200
201 VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
202
203 #ifdef DEBUG
204 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
205 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
206 #define anv_debug(format, ...) fprintf(stderr, "debug: " format, ##__VA_ARGS__)
207 #else
208 #define vk_error(error) error
209 #define vk_errorf(error, format, ...) error
210 #define anv_debug(format, ...)
211 #endif
212
213 /**
214 * Warn on ignored extension structs.
215 *
216 * The Vulkan spec requires us to ignore unsupported or unknown structs in
217 * a pNext chain. In debug mode, emitting warnings for ignored structs may
218 * help us discover structs that we should not have ignored.
219 *
220 *
221 * From the Vulkan 1.0.38 spec:
222 *
223 * Any component of the implementation (the loader, any enabled layers,
224 * and drivers) must skip over, without processing (other than reading the
225 * sType and pNext members) any chained structures with sType values not
226 * defined by extensions supported by that component.
227 */
228 #define anv_debug_ignored_stype(sType) \
229 anv_debug("debug: %s: ignored VkStructureType %u\n", __func__, (sType))
230
231 void __anv_finishme(const char *file, int line, const char *format, ...)
232 anv_printflike(3, 4);
233 void __anv_perf_warn(const char *file, int line, const char *format, ...)
234 anv_printflike(3, 4);
235 void anv_loge(const char *format, ...) anv_printflike(1, 2);
236 void anv_loge_v(const char *format, va_list va);
237
238 /**
239 * Print a FINISHME message, including its source location.
240 */
241 #define anv_finishme(format, ...) \
242 do { \
243 static bool reported = false; \
244 if (!reported) { \
245 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
246 reported = true; \
247 } \
248 } while (0)
249
250 /**
251 * Print a perf warning message. Set INTEL_DEBUG=perf to see these.
252 */
253 #define anv_perf_warn(format, ...) \
254 do { \
255 static bool reported = false; \
256 if (!reported && unlikely(INTEL_DEBUG & DEBUG_PERF)) { \
257 __anv_perf_warn(__FILE__, __LINE__, format, ##__VA_ARGS__); \
258 reported = true; \
259 } \
260 } while (0)
261
262 /* A non-fatal assert. Useful for debugging. */
263 #ifdef DEBUG
264 #define anv_assert(x) ({ \
265 if (unlikely(!(x))) \
266 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
267 })
268 #else
269 #define anv_assert(x)
270 #endif
271
272 /**
273 * A dynamically growable, circular buffer. Elements are added at head and
274 * removed from tail. head and tail are free-running uint32_t indices and we
275 * only compute the modulo with size when accessing the array. This way,
276 * number of bytes in the queue is always head - tail, even in case of
277 * wraparound.
278 */
279
280 struct anv_bo {
281 uint32_t gem_handle;
282
283 /* Index into the current validation list. This is used by the
284 * validation list building alrogithm to track which buffers are already
285 * in the validation list so that we can ensure uniqueness.
286 */
287 uint32_t index;
288
289 /* Last known offset. This value is provided by the kernel when we
290 * execbuf and is used as the presumed offset for the next bunch of
291 * relocations.
292 */
293 uint64_t offset;
294
295 uint64_t size;
296 void *map;
297
298 /** Flags to pass to the kernel through drm_i915_exec_object2::flags */
299 uint32_t flags;
300 };
301
302 static inline void
303 anv_bo_init(struct anv_bo *bo, uint32_t gem_handle, uint64_t size)
304 {
305 bo->gem_handle = gem_handle;
306 bo->index = 0;
307 bo->offset = -1;
308 bo->size = size;
309 bo->map = NULL;
310 bo->flags = 0;
311 }
312
313 /* Represents a lock-free linked list of "free" things. This is used by
314 * both the block pool and the state pools. Unfortunately, in order to
315 * solve the ABA problem, we can't use a single uint32_t head.
316 */
317 union anv_free_list {
318 struct {
319 int32_t offset;
320
321 /* A simple count that is incremented every time the head changes. */
322 uint32_t count;
323 };
324 uint64_t u64;
325 };
326
327 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
328
329 struct anv_block_state {
330 union {
331 struct {
332 uint32_t next;
333 uint32_t end;
334 };
335 uint64_t u64;
336 };
337 };
338
339 struct anv_block_pool {
340 struct anv_device *device;
341
342 struct anv_bo bo;
343
344 /* The offset from the start of the bo to the "center" of the block
345 * pool. Pointers to allocated blocks are given by
346 * bo.map + center_bo_offset + offsets.
347 */
348 uint32_t center_bo_offset;
349
350 /* Current memory map of the block pool. This pointer may or may not
351 * point to the actual beginning of the block pool memory. If
352 * anv_block_pool_alloc_back has ever been called, then this pointer
353 * will point to the "center" position of the buffer and all offsets
354 * (negative or positive) given out by the block pool alloc functions
355 * will be valid relative to this pointer.
356 *
357 * In particular, map == bo.map + center_offset
358 */
359 void *map;
360 int fd;
361
362 /**
363 * Array of mmaps and gem handles owned by the block pool, reclaimed when
364 * the block pool is destroyed.
365 */
366 struct u_vector mmap_cleanups;
367
368 uint32_t block_size;
369
370 union anv_free_list free_list;
371 struct anv_block_state state;
372
373 union anv_free_list back_free_list;
374 struct anv_block_state back_state;
375 };
376
377 /* Block pools are backed by a fixed-size 1GB memfd */
378 #define BLOCK_POOL_MEMFD_SIZE (1ul << 30)
379
380 /* The center of the block pool is also the middle of the memfd. This may
381 * change in the future if we decide differently for some reason.
382 */
383 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
384
385 static inline uint32_t
386 anv_block_pool_size(struct anv_block_pool *pool)
387 {
388 return pool->state.end + pool->back_state.end;
389 }
390
391 struct anv_state {
392 int32_t offset;
393 uint32_t alloc_size;
394 void *map;
395 };
396
397 struct anv_fixed_size_state_pool {
398 size_t state_size;
399 union anv_free_list free_list;
400 struct anv_block_state block;
401 };
402
403 #define ANV_MIN_STATE_SIZE_LOG2 6
404 #define ANV_MAX_STATE_SIZE_LOG2 20
405
406 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2 + 1)
407
408 struct anv_state_pool {
409 struct anv_block_pool *block_pool;
410 struct anv_fixed_size_state_pool buckets[ANV_STATE_BUCKETS];
411 };
412
413 struct anv_state_stream_block;
414
415 struct anv_state_stream {
416 struct anv_block_pool *block_pool;
417
418 /* The current working block */
419 struct anv_state_stream_block *block;
420
421 /* Offset at which the current block starts */
422 uint32_t start;
423 /* Offset at which to allocate the next state */
424 uint32_t next;
425 /* Offset at which the current block ends */
426 uint32_t end;
427 };
428
429 #define CACHELINE_SIZE 64
430 #define CACHELINE_MASK 63
431
432 static inline void
433 anv_clflush_range(void *start, size_t size)
434 {
435 void *p = (void *) (((uintptr_t) start) & ~CACHELINE_MASK);
436 void *end = start + size;
437
438 while (p < end) {
439 __builtin_ia32_clflush(p);
440 p += CACHELINE_SIZE;
441 }
442 }
443
444 static inline void
445 anv_flush_range(void *start, size_t size)
446 {
447 __builtin_ia32_mfence();
448 anv_clflush_range(start, size);
449 }
450
451 static inline void
452 anv_invalidate_range(void *start, size_t size)
453 {
454 anv_clflush_range(start, size);
455 __builtin_ia32_mfence();
456 }
457
458 VkResult anv_block_pool_init(struct anv_block_pool *pool,
459 struct anv_device *device, uint32_t block_size);
460 void anv_block_pool_finish(struct anv_block_pool *pool);
461 int32_t anv_block_pool_alloc(struct anv_block_pool *pool);
462 int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool);
463 void anv_block_pool_free(struct anv_block_pool *pool, int32_t offset);
464 void anv_state_pool_init(struct anv_state_pool *pool,
465 struct anv_block_pool *block_pool);
466 void anv_state_pool_finish(struct anv_state_pool *pool);
467 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
468 size_t state_size, size_t alignment);
469 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
470 void anv_state_stream_init(struct anv_state_stream *stream,
471 struct anv_block_pool *block_pool);
472 void anv_state_stream_finish(struct anv_state_stream *stream);
473 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
474 uint32_t size, uint32_t alignment);
475
476 /**
477 * Implements a pool of re-usable BOs. The interface is identical to that
478 * of block_pool except that each block is its own BO.
479 */
480 struct anv_bo_pool {
481 struct anv_device *device;
482
483 void *free_list[16];
484 };
485
486 void anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device);
487 void anv_bo_pool_finish(struct anv_bo_pool *pool);
488 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo,
489 uint32_t size);
490 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
491
492 struct anv_scratch_bo {
493 bool exists;
494 struct anv_bo bo;
495 };
496
497 struct anv_scratch_pool {
498 /* Indexed by Per-Thread Scratch Space number (the hardware value) and stage */
499 struct anv_scratch_bo bos[16][MESA_SHADER_STAGES];
500 };
501
502 void anv_scratch_pool_init(struct anv_device *device,
503 struct anv_scratch_pool *pool);
504 void anv_scratch_pool_finish(struct anv_device *device,
505 struct anv_scratch_pool *pool);
506 struct anv_bo *anv_scratch_pool_alloc(struct anv_device *device,
507 struct anv_scratch_pool *pool,
508 gl_shader_stage stage,
509 unsigned per_thread_scratch);
510
511 struct anv_physical_device {
512 VK_LOADER_DATA _loader_data;
513
514 struct anv_instance * instance;
515 uint32_t chipset_id;
516 char path[20];
517 const char * name;
518 struct gen_device_info info;
519 /** Amount of "GPU memory" we want to advertise
520 *
521 * Clearly, this value is bogus since Intel is a UMA architecture. On
522 * gen7 platforms, we are limited by GTT size unless we want to implement
523 * fine-grained tracking and GTT splitting. On Broadwell and above we are
524 * practically unlimited. However, we will never report more than 3/4 of
525 * the total system ram to try and avoid running out of RAM.
526 */
527 uint64_t heap_size;
528 bool supports_48bit_addresses;
529 struct brw_compiler * compiler;
530 struct isl_device isl_dev;
531 int cmd_parser_version;
532
533 uint32_t eu_total;
534 uint32_t subslice_total;
535
536 uint8_t uuid[VK_UUID_SIZE];
537
538 struct wsi_device wsi_device;
539 int local_fd;
540 };
541
542 struct anv_instance {
543 VK_LOADER_DATA _loader_data;
544
545 VkAllocationCallbacks alloc;
546
547 uint32_t apiVersion;
548 int physicalDeviceCount;
549 struct anv_physical_device physicalDevice;
550 };
551
552 VkResult anv_init_wsi(struct anv_physical_device *physical_device);
553 void anv_finish_wsi(struct anv_physical_device *physical_device);
554
555 struct anv_queue {
556 VK_LOADER_DATA _loader_data;
557
558 struct anv_device * device;
559
560 struct anv_state_pool * pool;
561 };
562
563 struct anv_pipeline_cache {
564 struct anv_device * device;
565 pthread_mutex_t mutex;
566
567 struct hash_table * cache;
568 };
569
570 struct anv_pipeline_bind_map;
571
572 void anv_pipeline_cache_init(struct anv_pipeline_cache *cache,
573 struct anv_device *device,
574 bool cache_enabled);
575 void anv_pipeline_cache_finish(struct anv_pipeline_cache *cache);
576
577 struct anv_shader_bin *
578 anv_pipeline_cache_search(struct anv_pipeline_cache *cache,
579 const void *key, uint32_t key_size);
580 struct anv_shader_bin *
581 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache *cache,
582 const void *key_data, uint32_t key_size,
583 const void *kernel_data, uint32_t kernel_size,
584 const struct brw_stage_prog_data *prog_data,
585 uint32_t prog_data_size,
586 const struct anv_pipeline_bind_map *bind_map);
587
588 struct anv_device {
589 VK_LOADER_DATA _loader_data;
590
591 VkAllocationCallbacks alloc;
592
593 struct anv_instance * instance;
594 uint32_t chipset_id;
595 struct gen_device_info info;
596 struct isl_device isl_dev;
597 int context_id;
598 int fd;
599 bool can_chain_batches;
600 bool robust_buffer_access;
601
602 struct anv_bo_pool batch_bo_pool;
603
604 struct anv_block_pool dynamic_state_block_pool;
605 struct anv_state_pool dynamic_state_pool;
606
607 struct anv_block_pool instruction_block_pool;
608 struct anv_state_pool instruction_state_pool;
609
610 struct anv_block_pool surface_state_block_pool;
611 struct anv_state_pool surface_state_pool;
612
613 struct anv_bo workaround_bo;
614
615 struct anv_pipeline_cache blorp_shader_cache;
616 struct blorp_context blorp;
617
618 struct anv_state border_colors;
619
620 struct anv_queue queue;
621
622 struct anv_scratch_pool scratch_pool;
623
624 uint32_t default_mocs;
625
626 pthread_mutex_t mutex;
627 pthread_cond_t queue_submit;
628 bool lost;
629 };
630
631 static void inline
632 anv_state_flush(struct anv_device *device, struct anv_state state)
633 {
634 if (device->info.has_llc)
635 return;
636
637 anv_flush_range(state.map, state.alloc_size);
638 }
639
640 void anv_device_init_blorp(struct anv_device *device);
641 void anv_device_finish_blorp(struct anv_device *device);
642
643 VkResult anv_device_execbuf(struct anv_device *device,
644 struct drm_i915_gem_execbuffer2 *execbuf,
645 struct anv_bo **execbuf_bos);
646 VkResult anv_device_query_status(struct anv_device *device);
647 VkResult anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo);
648 VkResult anv_device_wait(struct anv_device *device, struct anv_bo *bo,
649 int64_t timeout);
650
651 void* anv_gem_mmap(struct anv_device *device,
652 uint32_t gem_handle, uint64_t offset, uint64_t size, uint32_t flags);
653 void anv_gem_munmap(void *p, uint64_t size);
654 uint32_t anv_gem_create(struct anv_device *device, size_t size);
655 void anv_gem_close(struct anv_device *device, uint32_t gem_handle);
656 uint32_t anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
657 int anv_gem_busy(struct anv_device *device, uint32_t gem_handle);
658 int anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns);
659 int anv_gem_execbuffer(struct anv_device *device,
660 struct drm_i915_gem_execbuffer2 *execbuf);
661 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
662 uint32_t stride, uint32_t tiling);
663 int anv_gem_create_context(struct anv_device *device);
664 int anv_gem_destroy_context(struct anv_device *device, int context);
665 int anv_gem_get_context_param(int fd, int context, uint32_t param,
666 uint64_t *value);
667 int anv_gem_get_param(int fd, uint32_t param);
668 bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling);
669 int anv_gem_get_aperture(int fd, uint64_t *size);
670 bool anv_gem_supports_48b_addresses(int fd);
671 int anv_gem_gpu_get_reset_stats(struct anv_device *device,
672 uint32_t *active, uint32_t *pending);
673 int anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle);
674 uint32_t anv_gem_fd_to_handle(struct anv_device *device, int fd);
675 int anv_gem_set_caching(struct anv_device *device, uint32_t gem_handle, uint32_t caching);
676 int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
677 uint32_t read_domains, uint32_t write_domain);
678
679 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
680
681 struct anv_reloc_list {
682 size_t num_relocs;
683 size_t array_length;
684 struct drm_i915_gem_relocation_entry * relocs;
685 struct anv_bo ** reloc_bos;
686 };
687
688 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
689 const VkAllocationCallbacks *alloc);
690 void anv_reloc_list_finish(struct anv_reloc_list *list,
691 const VkAllocationCallbacks *alloc);
692
693 VkResult anv_reloc_list_add(struct anv_reloc_list *list,
694 const VkAllocationCallbacks *alloc,
695 uint32_t offset, struct anv_bo *target_bo,
696 uint32_t delta);
697
698 struct anv_batch_bo {
699 /* Link in the anv_cmd_buffer.owned_batch_bos list */
700 struct list_head link;
701
702 struct anv_bo bo;
703
704 /* Bytes actually consumed in this batch BO */
705 size_t length;
706
707 struct anv_reloc_list relocs;
708 };
709
710 struct anv_batch {
711 const VkAllocationCallbacks * alloc;
712
713 void * start;
714 void * end;
715 void * next;
716
717 struct anv_reloc_list * relocs;
718
719 /* This callback is called (with the associated user data) in the event
720 * that the batch runs out of space.
721 */
722 VkResult (*extend_cb)(struct anv_batch *, void *);
723 void * user_data;
724
725 /**
726 * Current error status of the command buffer. Used to track inconsistent
727 * or incomplete command buffer states that are the consequence of run-time
728 * errors such as out of memory scenarios. We want to track this in the
729 * batch because the command buffer object is not visible to some parts
730 * of the driver.
731 */
732 VkResult status;
733 };
734
735 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
736 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
737 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
738 void *location, struct anv_bo *bo, uint32_t offset);
739 VkResult anv_device_submit_simple_batch(struct anv_device *device,
740 struct anv_batch *batch);
741
742 static inline VkResult
743 anv_batch_set_error(struct anv_batch *batch, VkResult error)
744 {
745 assert(error != VK_SUCCESS);
746 if (batch->status == VK_SUCCESS)
747 batch->status = error;
748 return batch->status;
749 }
750
751 static inline bool
752 anv_batch_has_error(struct anv_batch *batch)
753 {
754 return batch->status != VK_SUCCESS;
755 }
756
757 struct anv_address {
758 struct anv_bo *bo;
759 uint32_t offset;
760 };
761
762 static inline uint64_t
763 _anv_combine_address(struct anv_batch *batch, void *location,
764 const struct anv_address address, uint32_t delta)
765 {
766 if (address.bo == NULL) {
767 return address.offset + delta;
768 } else {
769 assert(batch->start <= location && location < batch->end);
770
771 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
772 }
773 }
774
775 #define __gen_address_type struct anv_address
776 #define __gen_user_data struct anv_batch
777 #define __gen_combine_address _anv_combine_address
778
779 /* Wrapper macros needed to work around preprocessor argument issues. In
780 * particular, arguments don't get pre-evaluated if they are concatenated.
781 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
782 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
783 * We can work around this easily enough with these helpers.
784 */
785 #define __anv_cmd_length(cmd) cmd ## _length
786 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
787 #define __anv_cmd_header(cmd) cmd ## _header
788 #define __anv_cmd_pack(cmd) cmd ## _pack
789 #define __anv_reg_num(reg) reg ## _num
790
791 #define anv_pack_struct(dst, struc, ...) do { \
792 struct struc __template = { \
793 __VA_ARGS__ \
794 }; \
795 __anv_cmd_pack(struc)(NULL, dst, &__template); \
796 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dst, __anv_cmd_length(struc) * 4)); \
797 } while (0)
798
799 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
800 void *__dst = anv_batch_emit_dwords(batch, n); \
801 if (__dst) { \
802 struct cmd __template = { \
803 __anv_cmd_header(cmd), \
804 .DWordLength = n - __anv_cmd_length_bias(cmd), \
805 __VA_ARGS__ \
806 }; \
807 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
808 } \
809 __dst; \
810 })
811
812 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
813 do { \
814 uint32_t *dw; \
815 \
816 STATIC_ASSERT(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
817 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
818 if (!dw) \
819 break; \
820 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
821 dw[i] = (dwords0)[i] | (dwords1)[i]; \
822 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
823 } while (0)
824
825 #define anv_batch_emit(batch, cmd, name) \
826 for (struct cmd name = { __anv_cmd_header(cmd) }, \
827 *_dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
828 __builtin_expect(_dst != NULL, 1); \
829 ({ __anv_cmd_pack(cmd)(batch, _dst, &name); \
830 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, __anv_cmd_length(cmd) * 4)); \
831 _dst = NULL; \
832 }))
833
834 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
835 .GraphicsDataTypeGFDT = 0, \
836 .LLCCacheabilityControlLLCCC = 0, \
837 .L3CacheabilityControlL3CC = 1, \
838 }
839
840 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
841 .LLCeLLCCacheabilityControlLLCCC = 0, \
842 .L3CacheabilityControlL3CC = 1, \
843 }
844
845 #define GEN8_MOCS (struct GEN8_MEMORY_OBJECT_CONTROL_STATE) { \
846 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
847 .TargetCache = L3DefertoPATforLLCeLLCselection, \
848 .AgeforQUADLRU = 0 \
849 }
850
851 /* Skylake: MOCS is now an index into an array of 62 different caching
852 * configurations programmed by the kernel.
853 */
854
855 #define GEN9_MOCS (struct GEN9_MEMORY_OBJECT_CONTROL_STATE) { \
856 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
857 .IndextoMOCSTables = 2 \
858 }
859
860 #define GEN9_MOCS_PTE { \
861 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
862 .IndextoMOCSTables = 1 \
863 }
864
865 struct anv_device_memory {
866 struct anv_bo bo;
867 uint32_t type_index;
868 VkDeviceSize map_size;
869 void * map;
870 };
871
872 /**
873 * Header for Vertex URB Entry (VUE)
874 */
875 struct anv_vue_header {
876 uint32_t Reserved;
877 uint32_t RTAIndex; /* RenderTargetArrayIndex */
878 uint32_t ViewportIndex;
879 float PointWidth;
880 };
881
882 struct anv_descriptor_set_binding_layout {
883 #ifndef NDEBUG
884 /* The type of the descriptors in this binding */
885 VkDescriptorType type;
886 #endif
887
888 /* Number of array elements in this binding */
889 uint16_t array_size;
890
891 /* Index into the flattend descriptor set */
892 uint16_t descriptor_index;
893
894 /* Index into the dynamic state array for a dynamic buffer */
895 int16_t dynamic_offset_index;
896
897 /* Index into the descriptor set buffer views */
898 int16_t buffer_index;
899
900 struct {
901 /* Index into the binding table for the associated surface */
902 int16_t surface_index;
903
904 /* Index into the sampler table for the associated sampler */
905 int16_t sampler_index;
906
907 /* Index into the image table for the associated image */
908 int16_t image_index;
909 } stage[MESA_SHADER_STAGES];
910
911 /* Immutable samplers (or NULL if no immutable samplers) */
912 struct anv_sampler **immutable_samplers;
913 };
914
915 struct anv_descriptor_set_layout {
916 /* Number of bindings in this descriptor set */
917 uint16_t binding_count;
918
919 /* Total size of the descriptor set with room for all array entries */
920 uint16_t size;
921
922 /* Shader stages affected by this descriptor set */
923 uint16_t shader_stages;
924
925 /* Number of buffers in this descriptor set */
926 uint16_t buffer_count;
927
928 /* Number of dynamic offsets used by this descriptor set */
929 uint16_t dynamic_offset_count;
930
931 /* Bindings in this descriptor set */
932 struct anv_descriptor_set_binding_layout binding[0];
933 };
934
935 struct anv_descriptor {
936 VkDescriptorType type;
937
938 union {
939 struct {
940 struct anv_image_view *image_view;
941 struct anv_sampler *sampler;
942
943 /* Used to determine whether or not we need the surface state to have
944 * the auxiliary buffer enabled.
945 */
946 enum isl_aux_usage aux_usage;
947 };
948
949 struct {
950 struct anv_buffer *buffer;
951 uint64_t offset;
952 uint64_t range;
953 };
954
955 struct anv_buffer_view *buffer_view;
956 };
957 };
958
959 struct anv_descriptor_set {
960 const struct anv_descriptor_set_layout *layout;
961 uint32_t size;
962 uint32_t buffer_count;
963 struct anv_buffer_view *buffer_views;
964 struct anv_descriptor descriptors[0];
965 };
966
967 struct anv_buffer_view {
968 enum isl_format format; /**< VkBufferViewCreateInfo::format */
969 struct anv_bo *bo;
970 uint32_t offset; /**< Offset into bo. */
971 uint64_t range; /**< VkBufferViewCreateInfo::range */
972
973 struct anv_state surface_state;
974 struct anv_state storage_surface_state;
975 struct anv_state writeonly_storage_surface_state;
976
977 struct brw_image_param storage_image_param;
978 };
979
980 struct anv_push_descriptor_set {
981 struct anv_descriptor_set set;
982
983 /* Put this field right behind anv_descriptor_set so it fills up the
984 * descriptors[0] field. */
985 struct anv_descriptor descriptors[MAX_PUSH_DESCRIPTORS];
986
987 struct anv_buffer_view buffer_views[MAX_PUSH_DESCRIPTORS];
988 };
989
990 struct anv_descriptor_pool {
991 uint32_t size;
992 uint32_t next;
993 uint32_t free_list;
994
995 struct anv_state_stream surface_state_stream;
996 void *surface_state_free_list;
997
998 char data[0];
999 };
1000
1001 enum anv_descriptor_template_entry_type {
1002 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_IMAGE,
1003 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER,
1004 ANV_DESCRIPTOR_TEMPLATE_ENTRY_TYPE_BUFFER_VIEW
1005 };
1006
1007 struct anv_descriptor_template_entry {
1008 /* The type of descriptor in this entry */
1009 VkDescriptorType type;
1010
1011 /* Binding in the descriptor set */
1012 uint32_t binding;
1013
1014 /* Offset at which to write into the descriptor set binding */
1015 uint32_t array_element;
1016
1017 /* Number of elements to write into the descriptor set binding */
1018 uint32_t array_count;
1019
1020 /* Offset into the user provided data */
1021 size_t offset;
1022
1023 /* Stride between elements into the user provided data */
1024 size_t stride;
1025 };
1026
1027 struct anv_descriptor_update_template {
1028 /* The descriptor set this template corresponds to. This value is only
1029 * valid if the template was created with the templateType
1030 * VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR.
1031 */
1032 uint8_t set;
1033
1034 /* Number of entries in this template */
1035 uint32_t entry_count;
1036
1037 /* Entries of the template */
1038 struct anv_descriptor_template_entry entries[0];
1039 };
1040
1041 size_t
1042 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout);
1043
1044 void
1045 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
1046 const struct gen_device_info * const devinfo,
1047 const VkDescriptorImageInfo * const info,
1048 VkDescriptorType type,
1049 uint32_t binding,
1050 uint32_t element);
1051
1052 void
1053 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
1054 VkDescriptorType type,
1055 struct anv_buffer_view *buffer_view,
1056 uint32_t binding,
1057 uint32_t element);
1058
1059 void
1060 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
1061 struct anv_device *device,
1062 struct anv_state_stream *alloc_stream,
1063 VkDescriptorType type,
1064 struct anv_buffer *buffer,
1065 uint32_t binding,
1066 uint32_t element,
1067 VkDeviceSize offset,
1068 VkDeviceSize range);
1069
1070 void
1071 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
1072 struct anv_device *device,
1073 struct anv_state_stream *alloc_stream,
1074 const struct anv_descriptor_update_template *template,
1075 const void *data);
1076
1077 VkResult
1078 anv_descriptor_set_create(struct anv_device *device,
1079 struct anv_descriptor_pool *pool,
1080 const struct anv_descriptor_set_layout *layout,
1081 struct anv_descriptor_set **out_set);
1082
1083 void
1084 anv_descriptor_set_destroy(struct anv_device *device,
1085 struct anv_descriptor_pool *pool,
1086 struct anv_descriptor_set *set);
1087
1088 #define ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS UINT8_MAX
1089
1090 struct anv_pipeline_binding {
1091 /* The descriptor set this surface corresponds to. The special value of
1092 * ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS indicates that the offset refers
1093 * to a color attachment and not a regular descriptor.
1094 */
1095 uint8_t set;
1096
1097 /* Binding in the descriptor set */
1098 uint8_t binding;
1099
1100 /* Index in the binding */
1101 uint8_t index;
1102
1103 /* Input attachment index (relative to the subpass) */
1104 uint8_t input_attachment_index;
1105
1106 /* For a storage image, whether it is write-only */
1107 bool write_only;
1108 };
1109
1110 struct anv_pipeline_layout {
1111 struct {
1112 struct anv_descriptor_set_layout *layout;
1113 uint32_t dynamic_offset_start;
1114 } set[MAX_SETS];
1115
1116 uint32_t num_sets;
1117
1118 struct {
1119 bool has_dynamic_offsets;
1120 } stage[MESA_SHADER_STAGES];
1121
1122 unsigned char sha1[20];
1123 };
1124
1125 struct anv_buffer {
1126 struct anv_device * device;
1127 VkDeviceSize size;
1128
1129 VkBufferUsageFlags usage;
1130
1131 /* Set when bound */
1132 struct anv_bo * bo;
1133 VkDeviceSize offset;
1134 };
1135
1136 static inline uint64_t
1137 anv_buffer_get_range(struct anv_buffer *buffer, uint64_t offset, uint64_t range)
1138 {
1139 assert(offset <= buffer->size);
1140 if (range == VK_WHOLE_SIZE) {
1141 return buffer->size - offset;
1142 } else {
1143 assert(range <= buffer->size);
1144 return range;
1145 }
1146 }
1147
1148 enum anv_cmd_dirty_bits {
1149 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
1150 ANV_CMD_DIRTY_DYNAMIC_SCISSOR = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
1151 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
1152 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
1153 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
1154 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
1155 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
1156 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
1157 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
1158 ANV_CMD_DIRTY_DYNAMIC_ALL = (1 << 9) - 1,
1159 ANV_CMD_DIRTY_PIPELINE = 1 << 9,
1160 ANV_CMD_DIRTY_INDEX_BUFFER = 1 << 10,
1161 ANV_CMD_DIRTY_RENDER_TARGETS = 1 << 11,
1162 };
1163 typedef uint32_t anv_cmd_dirty_mask_t;
1164
1165 enum anv_pipe_bits {
1166 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT = (1 << 0),
1167 ANV_PIPE_STALL_AT_SCOREBOARD_BIT = (1 << 1),
1168 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT = (1 << 2),
1169 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT = (1 << 3),
1170 ANV_PIPE_VF_CACHE_INVALIDATE_BIT = (1 << 4),
1171 ANV_PIPE_DATA_CACHE_FLUSH_BIT = (1 << 5),
1172 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT = (1 << 10),
1173 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT = (1 << 11),
1174 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT = (1 << 12),
1175 ANV_PIPE_DEPTH_STALL_BIT = (1 << 13),
1176 ANV_PIPE_CS_STALL_BIT = (1 << 20),
1177
1178 /* This bit does not exist directly in PIPE_CONTROL. Instead it means that
1179 * a flush has happened but not a CS stall. The next time we do any sort
1180 * of invalidation we need to insert a CS stall at that time. Otherwise,
1181 * we would have to CS stall on every flush which could be bad.
1182 */
1183 ANV_PIPE_NEEDS_CS_STALL_BIT = (1 << 21),
1184 };
1185
1186 #define ANV_PIPE_FLUSH_BITS ( \
1187 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT | \
1188 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1189 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1190
1191 #define ANV_PIPE_STALL_BITS ( \
1192 ANV_PIPE_STALL_AT_SCOREBOARD_BIT | \
1193 ANV_PIPE_DEPTH_STALL_BIT | \
1194 ANV_PIPE_CS_STALL_BIT)
1195
1196 #define ANV_PIPE_INVALIDATE_BITS ( \
1197 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT | \
1198 ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT | \
1199 ANV_PIPE_VF_CACHE_INVALIDATE_BIT | \
1200 ANV_PIPE_DATA_CACHE_FLUSH_BIT | \
1201 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
1202 ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
1203
1204 static inline enum anv_pipe_bits
1205 anv_pipe_flush_bits_for_access_flags(VkAccessFlags flags)
1206 {
1207 enum anv_pipe_bits pipe_bits = 0;
1208
1209 unsigned b;
1210 for_each_bit(b, flags) {
1211 switch ((VkAccessFlagBits)(1 << b)) {
1212 case VK_ACCESS_SHADER_WRITE_BIT:
1213 pipe_bits |= ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1214 break;
1215 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT:
1216 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1217 break;
1218 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT:
1219 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1220 break;
1221 case VK_ACCESS_TRANSFER_WRITE_BIT:
1222 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1223 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1224 break;
1225 default:
1226 break; /* Nothing to do */
1227 }
1228 }
1229
1230 return pipe_bits;
1231 }
1232
1233 static inline enum anv_pipe_bits
1234 anv_pipe_invalidate_bits_for_access_flags(VkAccessFlags flags)
1235 {
1236 enum anv_pipe_bits pipe_bits = 0;
1237
1238 unsigned b;
1239 for_each_bit(b, flags) {
1240 switch ((VkAccessFlagBits)(1 << b)) {
1241 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT:
1242 case VK_ACCESS_INDEX_READ_BIT:
1243 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT:
1244 pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1245 break;
1246 case VK_ACCESS_UNIFORM_READ_BIT:
1247 pipe_bits |= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1248 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1249 break;
1250 case VK_ACCESS_SHADER_READ_BIT:
1251 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT:
1252 case VK_ACCESS_TRANSFER_READ_BIT:
1253 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1254 break;
1255 default:
1256 break; /* Nothing to do */
1257 }
1258 }
1259
1260 return pipe_bits;
1261 }
1262
1263 struct anv_vertex_binding {
1264 struct anv_buffer * buffer;
1265 VkDeviceSize offset;
1266 };
1267
1268 struct anv_push_constants {
1269 /* Current allocated size of this push constants data structure.
1270 * Because a decent chunk of it may not be used (images on SKL, for
1271 * instance), we won't actually allocate the entire structure up-front.
1272 */
1273 uint32_t size;
1274
1275 /* Push constant data provided by the client through vkPushConstants */
1276 uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
1277
1278 /* Our hardware only provides zero-based vertex and instance id so, in
1279 * order to satisfy the vulkan requirements, we may have to push one or
1280 * both of these into the shader.
1281 */
1282 uint32_t base_vertex;
1283 uint32_t base_instance;
1284
1285 /* Image data for image_load_store on pre-SKL */
1286 struct brw_image_param images[MAX_IMAGES];
1287 };
1288
1289 struct anv_dynamic_state {
1290 struct {
1291 uint32_t count;
1292 VkViewport viewports[MAX_VIEWPORTS];
1293 } viewport;
1294
1295 struct {
1296 uint32_t count;
1297 VkRect2D scissors[MAX_SCISSORS];
1298 } scissor;
1299
1300 float line_width;
1301
1302 struct {
1303 float bias;
1304 float clamp;
1305 float slope;
1306 } depth_bias;
1307
1308 float blend_constants[4];
1309
1310 struct {
1311 float min;
1312 float max;
1313 } depth_bounds;
1314
1315 struct {
1316 uint32_t front;
1317 uint32_t back;
1318 } stencil_compare_mask;
1319
1320 struct {
1321 uint32_t front;
1322 uint32_t back;
1323 } stencil_write_mask;
1324
1325 struct {
1326 uint32_t front;
1327 uint32_t back;
1328 } stencil_reference;
1329 };
1330
1331 extern const struct anv_dynamic_state default_dynamic_state;
1332
1333 void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
1334 const struct anv_dynamic_state *src,
1335 uint32_t copy_mask);
1336
1337 /**
1338 * Attachment state when recording a renderpass instance.
1339 *
1340 * The clear value is valid only if there exists a pending clear.
1341 */
1342 struct anv_attachment_state {
1343 enum isl_aux_usage aux_usage;
1344 enum isl_aux_usage input_aux_usage;
1345 struct anv_state color_rt_state;
1346 struct anv_state input_att_state;
1347
1348 VkImageLayout current_layout;
1349 VkImageAspectFlags pending_clear_aspects;
1350 bool fast_clear;
1351 VkClearValue clear_value;
1352 bool clear_color_is_zero_one;
1353 };
1354
1355 /** State required while building cmd buffer */
1356 struct anv_cmd_state {
1357 /* PIPELINE_SELECT.PipelineSelection */
1358 uint32_t current_pipeline;
1359 const struct gen_l3_config * current_l3_config;
1360 uint32_t vb_dirty;
1361 anv_cmd_dirty_mask_t dirty;
1362 anv_cmd_dirty_mask_t compute_dirty;
1363 enum anv_pipe_bits pending_pipe_bits;
1364 uint32_t num_workgroups_offset;
1365 struct anv_bo *num_workgroups_bo;
1366 VkShaderStageFlags descriptors_dirty;
1367 VkShaderStageFlags push_constants_dirty;
1368 uint32_t scratch_size;
1369 struct anv_pipeline * pipeline;
1370 struct anv_pipeline * compute_pipeline;
1371 struct anv_framebuffer * framebuffer;
1372 struct anv_render_pass * pass;
1373 struct anv_subpass * subpass;
1374 VkRect2D render_area;
1375 uint32_t restart_index;
1376 struct anv_vertex_binding vertex_bindings[MAX_VBS];
1377 struct anv_descriptor_set * descriptors[MAX_SETS];
1378 uint32_t dynamic_offsets[MAX_DYNAMIC_BUFFERS];
1379 VkShaderStageFlags push_constant_stages;
1380 struct anv_push_constants * push_constants[MESA_SHADER_STAGES];
1381 struct anv_state binding_tables[MESA_SHADER_STAGES];
1382 struct anv_state samplers[MESA_SHADER_STAGES];
1383 struct anv_dynamic_state dynamic;
1384 bool need_query_wa;
1385
1386 struct anv_push_descriptor_set push_descriptor;
1387
1388 /**
1389 * Whether or not the gen8 PMA fix is enabled. We ensure that, at the top
1390 * of any command buffer it is disabled by disabling it in EndCommandBuffer
1391 * and before invoking the secondary in ExecuteCommands.
1392 */
1393 bool pma_fix_enabled;
1394
1395 /**
1396 * Whether or not we know for certain that HiZ is enabled for the current
1397 * subpass. If, for whatever reason, we are unsure as to whether HiZ is
1398 * enabled or not, this will be false.
1399 */
1400 bool hiz_enabled;
1401
1402 /**
1403 * Array length is anv_cmd_state::pass::attachment_count. Array content is
1404 * valid only when recording a render pass instance.
1405 */
1406 struct anv_attachment_state * attachments;
1407
1408 /**
1409 * Surface states for color render targets. These are stored in a single
1410 * flat array. For depth-stencil attachments, the surface state is simply
1411 * left blank.
1412 */
1413 struct anv_state render_pass_states;
1414
1415 /**
1416 * A null surface state of the right size to match the framebuffer. This
1417 * is one of the states in render_pass_states.
1418 */
1419 struct anv_state null_surface_state;
1420
1421 struct {
1422 struct anv_buffer * index_buffer;
1423 uint32_t index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1424 uint32_t index_offset;
1425 } gen7;
1426 };
1427
1428 struct anv_cmd_pool {
1429 VkAllocationCallbacks alloc;
1430 struct list_head cmd_buffers;
1431 };
1432
1433 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1434
1435 enum anv_cmd_buffer_exec_mode {
1436 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
1437 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
1438 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT,
1439 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
1440 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
1441 };
1442
1443 struct anv_cmd_buffer {
1444 VK_LOADER_DATA _loader_data;
1445
1446 struct anv_device * device;
1447
1448 struct anv_cmd_pool * pool;
1449 struct list_head pool_link;
1450
1451 struct anv_batch batch;
1452
1453 /* Fields required for the actual chain of anv_batch_bo's.
1454 *
1455 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1456 */
1457 struct list_head batch_bos;
1458 enum anv_cmd_buffer_exec_mode exec_mode;
1459
1460 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1461 * referenced by this command buffer
1462 *
1463 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1464 */
1465 struct u_vector seen_bbos;
1466
1467 /* A vector of int32_t's for every block of binding tables.
1468 *
1469 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1470 */
1471 struct u_vector bt_blocks;
1472 uint32_t bt_next;
1473
1474 struct anv_reloc_list surface_relocs;
1475 /** Last seen surface state block pool center bo offset */
1476 uint32_t last_ss_pool_center;
1477
1478 /* Serial for tracking buffer completion */
1479 uint32_t serial;
1480
1481 /* Stream objects for storing temporary data */
1482 struct anv_state_stream surface_state_stream;
1483 struct anv_state_stream dynamic_state_stream;
1484
1485 VkCommandBufferUsageFlags usage_flags;
1486 VkCommandBufferLevel level;
1487
1488 struct anv_cmd_state state;
1489 };
1490
1491 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1492 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1493 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
1494 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
1495 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
1496 struct anv_cmd_buffer *secondary);
1497 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
1498 VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
1499 struct anv_cmd_buffer *cmd_buffer);
1500
1501 VkResult anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer);
1502
1503 VkResult
1504 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
1505 gl_shader_stage stage, uint32_t size);
1506 #define anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, field) \
1507 anv_cmd_buffer_ensure_push_constants_size(cmd_buffer, stage, \
1508 (offsetof(struct anv_push_constants, field) + \
1509 sizeof(cmd_buffer->state.push_constants[0]->field)))
1510
1511 struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
1512 const void *data, uint32_t size, uint32_t alignment);
1513 struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
1514 uint32_t *a, uint32_t *b,
1515 uint32_t dwords, uint32_t alignment);
1516
1517 struct anv_address
1518 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer);
1519 struct anv_state
1520 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
1521 uint32_t entries, uint32_t *state_offset);
1522 struct anv_state
1523 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
1524 struct anv_state
1525 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
1526 uint32_t size, uint32_t alignment);
1527
1528 VkResult
1529 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
1530
1531 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
1532 void gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer *cmd_buffer,
1533 bool depth_clamp_enable);
1534 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer);
1535
1536 void anv_cmd_buffer_setup_attachments(struct anv_cmd_buffer *cmd_buffer,
1537 struct anv_render_pass *pass,
1538 struct anv_framebuffer *framebuffer,
1539 const VkClearValue *clear_values);
1540
1541 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
1542
1543 struct anv_state
1544 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
1545 gl_shader_stage stage);
1546 struct anv_state
1547 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer);
1548
1549 void anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer *cmd_buffer);
1550 void anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer);
1551
1552 const struct anv_image_view *
1553 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
1554
1555 VkResult
1556 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
1557 uint32_t num_entries,
1558 uint32_t *state_offset,
1559 struct anv_state *bt_state);
1560
1561 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
1562
1563 enum anv_fence_state {
1564 /** Indicates that this is a new (or newly reset fence) */
1565 ANV_FENCE_STATE_RESET,
1566
1567 /** Indicates that this fence has been submitted to the GPU but is still
1568 * (as far as we know) in use by the GPU.
1569 */
1570 ANV_FENCE_STATE_SUBMITTED,
1571
1572 ANV_FENCE_STATE_SIGNALED,
1573 };
1574
1575 struct anv_fence {
1576 struct anv_bo bo;
1577 struct drm_i915_gem_execbuffer2 execbuf;
1578 struct drm_i915_gem_exec_object2 exec2_objects[1];
1579 enum anv_fence_state state;
1580 };
1581
1582 struct anv_event {
1583 uint64_t semaphore;
1584 struct anv_state state;
1585 };
1586
1587 struct anv_shader_module {
1588 unsigned char sha1[20];
1589 uint32_t size;
1590 char data[0];
1591 };
1592
1593 void anv_hash_shader(unsigned char *hash, const void *key, size_t key_size,
1594 struct anv_shader_module *module,
1595 const char *entrypoint,
1596 const struct anv_pipeline_layout *pipeline_layout,
1597 const VkSpecializationInfo *spec_info);
1598
1599 static inline gl_shader_stage
1600 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
1601 {
1602 assert(__builtin_popcount(vk_stage) == 1);
1603 return ffs(vk_stage) - 1;
1604 }
1605
1606 static inline VkShaderStageFlagBits
1607 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
1608 {
1609 return (1 << mesa_stage);
1610 }
1611
1612 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1613
1614 #define anv_foreach_stage(stage, stage_bits) \
1615 for (gl_shader_stage stage, \
1616 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
1617 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1618 __tmp &= ~(1 << (stage)))
1619
1620 struct anv_pipeline_bind_map {
1621 uint32_t surface_count;
1622 uint32_t sampler_count;
1623 uint32_t image_count;
1624
1625 struct anv_pipeline_binding * surface_to_descriptor;
1626 struct anv_pipeline_binding * sampler_to_descriptor;
1627 };
1628
1629 struct anv_shader_bin_key {
1630 uint32_t size;
1631 uint8_t data[0];
1632 };
1633
1634 struct anv_shader_bin {
1635 uint32_t ref_cnt;
1636
1637 const struct anv_shader_bin_key *key;
1638
1639 struct anv_state kernel;
1640 uint32_t kernel_size;
1641
1642 const struct brw_stage_prog_data *prog_data;
1643 uint32_t prog_data_size;
1644
1645 struct anv_pipeline_bind_map bind_map;
1646
1647 /* Prog data follows, then params, then the key, all aligned to 8-bytes */
1648 };
1649
1650 struct anv_shader_bin *
1651 anv_shader_bin_create(struct anv_device *device,
1652 const void *key, uint32_t key_size,
1653 const void *kernel, uint32_t kernel_size,
1654 const struct brw_stage_prog_data *prog_data,
1655 uint32_t prog_data_size, const void *prog_data_param,
1656 const struct anv_pipeline_bind_map *bind_map);
1657
1658 void
1659 anv_shader_bin_destroy(struct anv_device *device, struct anv_shader_bin *shader);
1660
1661 static inline void
1662 anv_shader_bin_ref(struct anv_shader_bin *shader)
1663 {
1664 assert(shader && shader->ref_cnt >= 1);
1665 __sync_fetch_and_add(&shader->ref_cnt, 1);
1666 }
1667
1668 static inline void
1669 anv_shader_bin_unref(struct anv_device *device, struct anv_shader_bin *shader)
1670 {
1671 assert(shader && shader->ref_cnt >= 1);
1672 if (__sync_fetch_and_add(&shader->ref_cnt, -1) == 1)
1673 anv_shader_bin_destroy(device, shader);
1674 }
1675
1676 struct anv_pipeline {
1677 struct anv_device * device;
1678 struct anv_batch batch;
1679 uint32_t batch_data[512];
1680 struct anv_reloc_list batch_relocs;
1681 uint32_t dynamic_state_mask;
1682 struct anv_dynamic_state dynamic_state;
1683
1684 struct anv_pipeline_layout * layout;
1685
1686 bool needs_data_cache;
1687
1688 struct anv_shader_bin * shaders[MESA_SHADER_STAGES];
1689
1690 struct {
1691 const struct gen_l3_config * l3_config;
1692 uint32_t total_size;
1693 } urb;
1694
1695 VkShaderStageFlags active_stages;
1696 struct anv_state blend_state;
1697
1698 uint32_t vb_used;
1699 uint32_t binding_stride[MAX_VBS];
1700 bool instancing_enable[MAX_VBS];
1701 bool primitive_restart;
1702 uint32_t topology;
1703
1704 uint32_t cs_right_mask;
1705
1706 bool writes_depth;
1707 bool depth_test_enable;
1708 bool writes_stencil;
1709 bool stencil_test_enable;
1710 bool depth_clamp_enable;
1711 bool sample_shading_enable;
1712 bool kill_pixel;
1713
1714 struct {
1715 uint32_t sf[7];
1716 uint32_t depth_stencil_state[3];
1717 } gen7;
1718
1719 struct {
1720 uint32_t sf[4];
1721 uint32_t raster[5];
1722 uint32_t wm_depth_stencil[3];
1723 } gen8;
1724
1725 struct {
1726 uint32_t wm_depth_stencil[4];
1727 } gen9;
1728
1729 uint32_t interface_descriptor_data[8];
1730 };
1731
1732 static inline bool
1733 anv_pipeline_has_stage(const struct anv_pipeline *pipeline,
1734 gl_shader_stage stage)
1735 {
1736 return (pipeline->active_stages & mesa_to_vk_shader_stage(stage)) != 0;
1737 }
1738
1739 #define ANV_DECL_GET_PROG_DATA_FUNC(prefix, stage) \
1740 static inline const struct brw_##prefix##_prog_data * \
1741 get_##prefix##_prog_data(const struct anv_pipeline *pipeline) \
1742 { \
1743 if (anv_pipeline_has_stage(pipeline, stage)) { \
1744 return (const struct brw_##prefix##_prog_data *) \
1745 pipeline->shaders[stage]->prog_data; \
1746 } else { \
1747 return NULL; \
1748 } \
1749 }
1750
1751 ANV_DECL_GET_PROG_DATA_FUNC(vs, MESA_SHADER_VERTEX)
1752 ANV_DECL_GET_PROG_DATA_FUNC(tcs, MESA_SHADER_TESS_CTRL)
1753 ANV_DECL_GET_PROG_DATA_FUNC(tes, MESA_SHADER_TESS_EVAL)
1754 ANV_DECL_GET_PROG_DATA_FUNC(gs, MESA_SHADER_GEOMETRY)
1755 ANV_DECL_GET_PROG_DATA_FUNC(wm, MESA_SHADER_FRAGMENT)
1756 ANV_DECL_GET_PROG_DATA_FUNC(cs, MESA_SHADER_COMPUTE)
1757
1758 static inline const struct brw_vue_prog_data *
1759 anv_pipeline_get_last_vue_prog_data(const struct anv_pipeline *pipeline)
1760 {
1761 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
1762 return &get_gs_prog_data(pipeline)->base;
1763 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1764 return &get_tes_prog_data(pipeline)->base;
1765 else
1766 return &get_vs_prog_data(pipeline)->base;
1767 }
1768
1769 VkResult
1770 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
1771 struct anv_pipeline_cache *cache,
1772 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1773 const VkAllocationCallbacks *alloc);
1774
1775 VkResult
1776 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1777 struct anv_pipeline_cache *cache,
1778 const VkComputePipelineCreateInfo *info,
1779 struct anv_shader_module *module,
1780 const char *entrypoint,
1781 const VkSpecializationInfo *spec_info);
1782
1783 struct anv_format {
1784 enum isl_format isl_format:16;
1785 struct isl_swizzle swizzle;
1786 };
1787
1788 struct anv_format
1789 anv_get_format(const struct gen_device_info *devinfo, VkFormat format,
1790 VkImageAspectFlags aspect, VkImageTiling tiling);
1791
1792 static inline enum isl_format
1793 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
1794 VkImageAspectFlags aspect, VkImageTiling tiling)
1795 {
1796 return anv_get_format(devinfo, vk_format, aspect, tiling).isl_format;
1797 }
1798
1799 static inline struct isl_swizzle
1800 anv_swizzle_for_render(struct isl_swizzle swizzle)
1801 {
1802 /* Sometimes the swizzle will have alpha map to one. We do this to fake
1803 * RGB as RGBA for texturing
1804 */
1805 assert(swizzle.a == ISL_CHANNEL_SELECT_ONE ||
1806 swizzle.a == ISL_CHANNEL_SELECT_ALPHA);
1807
1808 /* But it doesn't matter what we render to that channel */
1809 swizzle.a = ISL_CHANNEL_SELECT_ALPHA;
1810
1811 return swizzle;
1812 }
1813
1814 void
1815 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm);
1816
1817 /**
1818 * Subsurface of an anv_image.
1819 */
1820 struct anv_surface {
1821 /** Valid only if isl_surf::size > 0. */
1822 struct isl_surf isl;
1823
1824 /**
1825 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1826 */
1827 uint32_t offset;
1828 };
1829
1830 struct anv_image {
1831 VkImageType type;
1832 /* The original VkFormat provided by the client. This may not match any
1833 * of the actual surface formats.
1834 */
1835 VkFormat vk_format;
1836 VkImageAspectFlags aspects;
1837 VkExtent3D extent;
1838 uint32_t levels;
1839 uint32_t array_size;
1840 uint32_t samples; /**< VkImageCreateInfo::samples */
1841 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1842 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1843
1844 VkDeviceSize size;
1845 uint32_t alignment;
1846
1847 /* Set when bound */
1848 struct anv_bo *bo;
1849 VkDeviceSize offset;
1850
1851 /**
1852 * Image subsurfaces
1853 *
1854 * For each foo, anv_image::foo_surface is valid if and only if
1855 * anv_image::aspects has a foo aspect.
1856 *
1857 * The hardware requires that the depth buffer and stencil buffer be
1858 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1859 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1860 * allocate the depth and stencil buffers as separate surfaces in the same
1861 * bo.
1862 */
1863 union {
1864 struct anv_surface color_surface;
1865
1866 struct {
1867 struct anv_surface depth_surface;
1868 struct anv_surface stencil_surface;
1869 };
1870 };
1871
1872 /**
1873 * For color images, this is the aux usage for this image when not used as a
1874 * color attachment.
1875 *
1876 * For depth/stencil images, this is set to ISL_AUX_USAGE_HIZ if the image
1877 * has a HiZ buffer.
1878 */
1879 enum isl_aux_usage aux_usage;
1880
1881 struct anv_surface aux_surface;
1882 };
1883
1884 /* Returns true if a HiZ-enabled depth buffer can be sampled from. */
1885 static inline bool
1886 anv_can_sample_with_hiz(const struct gen_device_info * const devinfo,
1887 const VkImageAspectFlags aspect_mask,
1888 const uint32_t samples)
1889 {
1890 /* Validate the inputs. */
1891 assert(devinfo && aspect_mask && samples);
1892 return devinfo->gen >= 8 && (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) &&
1893 samples == 1;
1894 }
1895
1896 void
1897 anv_gen8_hiz_op_resolve(struct anv_cmd_buffer *cmd_buffer,
1898 const struct anv_image *image,
1899 enum blorp_hiz_op op);
1900
1901 enum isl_aux_usage
1902 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
1903 const struct anv_image *image,
1904 const VkImageAspectFlags aspects,
1905 const VkImageLayout layout);
1906
1907 /* This is defined as a macro so that it works for both
1908 * VkImageSubresourceRange and VkImageSubresourceLayers
1909 */
1910 #define anv_get_layerCount(_image, _range) \
1911 ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
1912 (_image)->array_size - (_range)->baseArrayLayer : (_range)->layerCount)
1913
1914 static inline uint32_t
1915 anv_get_levelCount(const struct anv_image *image,
1916 const VkImageSubresourceRange *range)
1917 {
1918 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
1919 image->levels - range->baseMipLevel : range->levelCount;
1920 }
1921
1922
1923 struct anv_image_view {
1924 const struct anv_image *image; /**< VkImageViewCreateInfo::image */
1925 struct anv_bo *bo;
1926 uint32_t offset; /**< Offset into bo. */
1927
1928 struct isl_view isl;
1929
1930 VkImageAspectFlags aspect_mask;
1931 VkFormat vk_format;
1932 VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1933
1934 /** RENDER_SURFACE_STATE when using image as a sampler surface. */
1935 struct anv_state sampler_surface_state;
1936
1937 /**
1938 * RENDER_SURFACE_STATE when using image as a sampler surface with the
1939 * auxiliary buffer disabled.
1940 */
1941 struct anv_state no_aux_sampler_surface_state;
1942
1943 /**
1944 * RENDER_SURFACE_STATE when using image as a storage image. Separate states
1945 * for write-only and readable, using the real format for write-only and the
1946 * lowered format for readable.
1947 */
1948 struct anv_state storage_surface_state;
1949 struct anv_state writeonly_storage_surface_state;
1950
1951 struct brw_image_param storage_image_param;
1952 };
1953
1954 struct anv_image_create_info {
1955 const VkImageCreateInfo *vk_info;
1956
1957 /** An opt-in bitmask which filters an ISL-mapping of the Vulkan tiling. */
1958 isl_tiling_flags_t isl_tiling_flags;
1959
1960 uint32_t stride;
1961 };
1962
1963 VkResult anv_image_create(VkDevice _device,
1964 const struct anv_image_create_info *info,
1965 const VkAllocationCallbacks* alloc,
1966 VkImage *pImage);
1967
1968 const struct anv_surface *
1969 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1970 VkImageAspectFlags aspect_mask);
1971
1972 enum isl_format
1973 anv_isl_format_for_descriptor_type(VkDescriptorType type);
1974
1975 static inline struct VkExtent3D
1976 anv_sanitize_image_extent(const VkImageType imageType,
1977 const struct VkExtent3D imageExtent)
1978 {
1979 switch (imageType) {
1980 case VK_IMAGE_TYPE_1D:
1981 return (VkExtent3D) { imageExtent.width, 1, 1 };
1982 case VK_IMAGE_TYPE_2D:
1983 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
1984 case VK_IMAGE_TYPE_3D:
1985 return imageExtent;
1986 default:
1987 unreachable("invalid image type");
1988 }
1989 }
1990
1991 static inline struct VkOffset3D
1992 anv_sanitize_image_offset(const VkImageType imageType,
1993 const struct VkOffset3D imageOffset)
1994 {
1995 switch (imageType) {
1996 case VK_IMAGE_TYPE_1D:
1997 return (VkOffset3D) { imageOffset.x, 0, 0 };
1998 case VK_IMAGE_TYPE_2D:
1999 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
2000 case VK_IMAGE_TYPE_3D:
2001 return imageOffset;
2002 default:
2003 unreachable("invalid image type");
2004 }
2005 }
2006
2007
2008 void anv_fill_buffer_surface_state(struct anv_device *device,
2009 struct anv_state state,
2010 enum isl_format format,
2011 uint32_t offset, uint32_t range,
2012 uint32_t stride);
2013
2014 void anv_image_view_fill_image_param(struct anv_device *device,
2015 struct anv_image_view *view,
2016 struct brw_image_param *param);
2017 void anv_buffer_view_fill_image_param(struct anv_device *device,
2018 struct anv_buffer_view *view,
2019 struct brw_image_param *param);
2020
2021 struct anv_sampler {
2022 uint32_t state[4];
2023 };
2024
2025 struct anv_framebuffer {
2026 uint32_t width;
2027 uint32_t height;
2028 uint32_t layers;
2029
2030 uint32_t attachment_count;
2031 struct anv_image_view * attachments[0];
2032 };
2033
2034 struct anv_subpass {
2035 uint32_t attachment_count;
2036
2037 /**
2038 * A pointer to all attachment references used in this subpass.
2039 * Only valid if ::attachment_count > 0.
2040 */
2041 VkAttachmentReference * attachments;
2042 uint32_t input_count;
2043 VkAttachmentReference * input_attachments;
2044 uint32_t color_count;
2045 VkAttachmentReference * color_attachments;
2046 VkAttachmentReference * resolve_attachments;
2047
2048 VkAttachmentReference depth_stencil_attachment;
2049
2050 /** Subpass has a depth/stencil self-dependency */
2051 bool has_ds_self_dep;
2052
2053 /** Subpass has at least one resolve attachment */
2054 bool has_resolve;
2055 };
2056
2057 enum anv_subpass_usage {
2058 ANV_SUBPASS_USAGE_DRAW = (1 << 0),
2059 ANV_SUBPASS_USAGE_INPUT = (1 << 1),
2060 ANV_SUBPASS_USAGE_RESOLVE_SRC = (1 << 2),
2061 ANV_SUBPASS_USAGE_RESOLVE_DST = (1 << 3),
2062 };
2063
2064 struct anv_render_pass_attachment {
2065 /* TODO: Consider using VkAttachmentDescription instead of storing each of
2066 * its members individually.
2067 */
2068 VkFormat format;
2069 uint32_t samples;
2070 VkImageUsageFlags usage;
2071 VkAttachmentLoadOp load_op;
2072 VkAttachmentStoreOp store_op;
2073 VkAttachmentLoadOp stencil_load_op;
2074 VkImageLayout initial_layout;
2075 VkImageLayout final_layout;
2076
2077 /* An array, indexed by subpass id, of how the attachment will be used. */
2078 enum anv_subpass_usage * subpass_usage;
2079
2080 /* The subpass id in which the attachment will be used last. */
2081 uint32_t last_subpass_idx;
2082 };
2083
2084 struct anv_render_pass {
2085 uint32_t attachment_count;
2086 uint32_t subpass_count;
2087 VkAttachmentReference * subpass_attachments;
2088 enum anv_subpass_usage * subpass_usages;
2089 struct anv_render_pass_attachment * attachments;
2090 struct anv_subpass subpasses[0];
2091 };
2092
2093 #define ANV_PIPELINE_STATISTICS_MASK 0x000007ff
2094
2095 struct anv_query_pool {
2096 VkQueryType type;
2097 VkQueryPipelineStatisticFlags pipeline_statistics;
2098 /** Stride between slots, in bytes */
2099 uint32_t stride;
2100 /** Number of slots in this query pool */
2101 uint32_t slots;
2102 struct anv_bo bo;
2103 };
2104
2105 void *anv_lookup_entrypoint(const struct gen_device_info *devinfo,
2106 const char *name);
2107
2108 void anv_dump_image_to_ppm(struct anv_device *device,
2109 struct anv_image *image, unsigned miplevel,
2110 unsigned array_layer, VkImageAspectFlagBits aspect,
2111 const char *filename);
2112
2113 enum anv_dump_action {
2114 ANV_DUMP_FRAMEBUFFERS_BIT = 0x1,
2115 };
2116
2117 void anv_dump_start(struct anv_device *device, enum anv_dump_action actions);
2118 void anv_dump_finish(void);
2119
2120 void anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
2121 struct anv_framebuffer *fb);
2122
2123 static inline uint32_t
2124 anv_get_subpass_id(const struct anv_cmd_state * const cmd_state)
2125 {
2126 /* This function must be called from within a subpass. */
2127 assert(cmd_state->pass && cmd_state->subpass);
2128
2129 const uint32_t subpass_id = cmd_state->subpass - cmd_state->pass->subpasses;
2130
2131 /* The id of this subpass shouldn't exceed the number of subpasses in this
2132 * render pass minus 1.
2133 */
2134 assert(subpass_id < cmd_state->pass->subpass_count);
2135 return subpass_id;
2136 }
2137
2138 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
2139 \
2140 static inline struct __anv_type * \
2141 __anv_type ## _from_handle(__VkType _handle) \
2142 { \
2143 return (struct __anv_type *) _handle; \
2144 } \
2145 \
2146 static inline __VkType \
2147 __anv_type ## _to_handle(struct __anv_type *_obj) \
2148 { \
2149 return (__VkType) _obj; \
2150 }
2151
2152 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
2153 \
2154 static inline struct __anv_type * \
2155 __anv_type ## _from_handle(__VkType _handle) \
2156 { \
2157 return (struct __anv_type *)(uintptr_t) _handle; \
2158 } \
2159 \
2160 static inline __VkType \
2161 __anv_type ## _to_handle(struct __anv_type *_obj) \
2162 { \
2163 return (__VkType)(uintptr_t) _obj; \
2164 }
2165
2166 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
2167 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
2168
2169 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCommandBuffer)
2170 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
2171 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
2172 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
2173 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
2174
2175 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCommandPool)
2176 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
2177 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView)
2178 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool, VkDescriptorPool)
2179 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
2180 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
2181 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_update_template, VkDescriptorUpdateTemplateKHR)
2182 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
2183 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
2184 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event, VkEvent)
2185 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
2186 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
2187 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
2188 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache, VkPipelineCache)
2189 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
2190 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
2191 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
2192 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
2193 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
2194 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
2195
2196 /* Gen-specific function declarations */
2197 #ifdef genX
2198 # include "anv_genX.h"
2199 #else
2200 # define genX(x) gen7_##x
2201 # include "anv_genX.h"
2202 # undef genX
2203 # define genX(x) gen75_##x
2204 # include "anv_genX.h"
2205 # undef genX
2206 # define genX(x) gen8_##x
2207 # include "anv_genX.h"
2208 # undef genX
2209 # define genX(x) gen9_##x
2210 # include "anv_genX.h"
2211 # undef genX
2212 #endif
2213
2214 #endif /* ANV_PRIVATE_H */