ee2700254cd6abc2b896d706e5ffd1b72a32f0e3
[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_wsi_lunarg.h>
50
51 #include "anv_entrypoints.h"
52
53 #include "brw_context.h"
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 #define anv_noreturn __attribute__((__noreturn__))
60 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
61
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #define MAX(a, b) ((a) > (b) ? (a) : (b))
64
65 static inline uint32_t
66 align_u32(uint32_t v, uint32_t a)
67 {
68 return (v + a - 1) & ~(a - 1);
69 }
70
71 static inline int32_t
72 align_i32(int32_t v, int32_t a)
73 {
74 return (v + a - 1) & ~(a - 1);
75 }
76
77 /** Alignment must be a power of 2. */
78 static inline bool
79 anv_is_aligned(uintmax_t n, uintmax_t a)
80 {
81 assert(a == (a & -a));
82 return (n & (a - 1)) == 0;
83 }
84
85 static inline uint32_t
86 anv_minify(uint32_t n, uint32_t levels)
87 {
88 if (unlikely(n == 0))
89 return 0;
90 else
91 return MAX(n >> levels, 1);
92 }
93
94 static inline bool
95 anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
96 {
97 if (*inout_mask & clear_mask) {
98 *inout_mask &= ~clear_mask;
99 return true;
100 } else {
101 return false;
102 }
103 }
104
105 #define for_each_bit(b, dword) \
106 for (uint32_t __dword = (dword); \
107 (b) = __builtin_ffs(__dword) - 1, __dword; \
108 __dword &= ~(1 << (b)))
109
110 /* Define no kernel as 1, since that's an illegal offset for a kernel */
111 #define NO_KERNEL 1
112
113 struct anv_common {
114 VkStructureType sType;
115 const void* pNext;
116 };
117
118 /* Whenever we generate an error, pass it through this function. Useful for
119 * debugging, where we can break on it. Only call at error site, not when
120 * propagating errors. Might be useful to plug in a stack trace here.
121 */
122
123 static inline VkResult
124 vk_error(VkResult error)
125 {
126 #ifdef DEBUG
127 fprintf(stderr, "vk_error: %x\n", error);
128 #endif
129
130 return error;
131 }
132
133 void __anv_finishme(const char *file, int line, const char *format, ...)
134 anv_printflike(3, 4);
135 void anv_loge(const char *format, ...) anv_printflike(1, 2);
136 void anv_loge_v(const char *format, va_list va);
137
138 /**
139 * Print a FINISHME message, including its source location.
140 */
141 #define anv_finishme(format, ...) \
142 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);
143
144 /* A non-fatal assert. Useful for debugging. */
145 #ifdef DEBUG
146 #define anv_assert(x) ({ \
147 if (unlikely(!(x))) \
148 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
149 })
150 #else
151 #define anv_assert(x)
152 #endif
153
154 void anv_abortf(const char *format, ...) anv_noreturn anv_printflike(1, 2);
155 void anv_abortfv(const char *format, va_list va) anv_noreturn;
156
157 #define stub_return(v) \
158 do { \
159 anv_finishme("stub %s", __func__); \
160 return (v); \
161 } while (0)
162
163 #define stub() \
164 do { \
165 anv_finishme("stub %s", __func__); \
166 return; \
167 } while (0)
168
169 /**
170 * A dynamically growable, circular buffer. Elements are added at head and
171 * removed from tail. head and tail are free-running uint32_t indices and we
172 * only compute the modulo with size when accessing the array. This way,
173 * number of bytes in the queue is always head - tail, even in case of
174 * wraparound.
175 */
176
177 struct anv_vector {
178 uint32_t head;
179 uint32_t tail;
180 uint32_t element_size;
181 uint32_t size;
182 void *data;
183 };
184
185 int anv_vector_init(struct anv_vector *queue, uint32_t element_size, uint32_t size);
186 void *anv_vector_add(struct anv_vector *queue);
187 void *anv_vector_remove(struct anv_vector *queue);
188
189 static inline int
190 anv_vector_length(struct anv_vector *queue)
191 {
192 return (queue->head - queue->tail) / queue->element_size;
193 }
194
195 static inline void
196 anv_vector_finish(struct anv_vector *queue)
197 {
198 free(queue->data);
199 }
200
201 #define anv_vector_foreach(elem, queue) \
202 static_assert(__builtin_types_compatible_p(__typeof__(queue), struct anv_vector *), ""); \
203 for (uint32_t __anv_vector_offset = (queue)->tail; \
204 elem = (queue)->data + (__anv_vector_offset & ((queue)->size - 1)), __anv_vector_offset < (queue)->head; \
205 __anv_vector_offset += (queue)->element_size)
206
207 struct anv_bo {
208 int gem_handle;
209 uint32_t index;
210 uint64_t offset;
211 uint64_t size;
212 void *map;
213 };
214
215 /* Represents a lock-free linked list of "free" things. This is used by
216 * both the block pool and the state pools. Unfortunately, in order to
217 * solve the ABA problem, we can't use a single uint32_t head.
218 */
219 union anv_free_list {
220 struct {
221 uint32_t offset;
222
223 /* A simple count that is incremented every time the head changes. */
224 uint32_t count;
225 };
226 uint64_t u64;
227 };
228
229 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
230
231 struct anv_block_state {
232 union {
233 struct {
234 uint32_t next;
235 uint32_t end;
236 };
237 uint64_t u64;
238 };
239 };
240
241 struct anv_block_pool {
242 struct anv_device *device;
243
244 struct anv_bo bo;
245 void *map;
246 int fd;
247
248 /**
249 * Array of mmaps and gem handles owned by the block pool, reclaimed when
250 * the block pool is destroyed.
251 */
252 struct anv_vector mmap_cleanups;
253
254 uint32_t block_size;
255
256 union anv_free_list free_list;
257 struct anv_block_state state;
258 };
259
260 static inline uint32_t
261 anv_block_pool_size(struct anv_block_pool *pool)
262 {
263 return pool->state.end;
264 }
265
266 struct anv_state {
267 uint32_t offset;
268 uint32_t alloc_size;
269 void *map;
270 };
271
272 struct anv_fixed_size_state_pool {
273 size_t state_size;
274 union anv_free_list free_list;
275 struct anv_block_state block;
276 };
277
278 #define ANV_MIN_STATE_SIZE_LOG2 6
279 #define ANV_MAX_STATE_SIZE_LOG2 10
280
281 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2)
282
283 struct anv_state_pool {
284 struct anv_block_pool *block_pool;
285 struct anv_fixed_size_state_pool buckets[ANV_STATE_BUCKETS];
286 };
287
288 struct anv_state_stream {
289 struct anv_block_pool *block_pool;
290 uint32_t next;
291 uint32_t current_block;
292 uint32_t end;
293 };
294
295 void anv_block_pool_init(struct anv_block_pool *pool,
296 struct anv_device *device, uint32_t block_size);
297 void anv_block_pool_finish(struct anv_block_pool *pool);
298 uint32_t anv_block_pool_alloc(struct anv_block_pool *pool);
299 void anv_block_pool_free(struct anv_block_pool *pool, uint32_t offset);
300 void anv_state_pool_init(struct anv_state_pool *pool,
301 struct anv_block_pool *block_pool);
302 void anv_state_pool_finish(struct anv_state_pool *pool);
303 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
304 size_t state_size, size_t alignment);
305 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
306 void anv_state_stream_init(struct anv_state_stream *stream,
307 struct anv_block_pool *block_pool);
308 void anv_state_stream_finish(struct anv_state_stream *stream);
309 struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
310 uint32_t size, uint32_t alignment);
311
312 /**
313 * Implements a pool of re-usable BOs. The interface is identical to that
314 * of block_pool except that each block is its own BO.
315 */
316 struct anv_bo_pool {
317 struct anv_device *device;
318
319 uint32_t bo_size;
320
321 void *free_list;
322 };
323
324 void anv_bo_pool_init(struct anv_bo_pool *pool,
325 struct anv_device *device, uint32_t block_size);
326 void anv_bo_pool_finish(struct anv_bo_pool *pool);
327 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo);
328 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
329
330 struct anv_physical_device {
331 struct anv_instance * instance;
332 uint32_t chipset_id;
333 const char * path;
334 const char * name;
335 const struct brw_device_info * info;
336 uint64_t aperture_size;
337 };
338
339 struct anv_instance {
340 void * pAllocUserData;
341 PFN_vkAllocFunction pfnAlloc;
342 PFN_vkFreeFunction pfnFree;
343 uint32_t apiVersion;
344 uint32_t physicalDeviceCount;
345 struct anv_physical_device physicalDevice;
346 };
347
348 struct anv_meta_state {
349 struct {
350 VkPipeline pipeline;
351 } clear;
352
353 struct {
354 VkPipeline pipeline;
355 VkPipelineLayout pipeline_layout;
356 VkDescriptorSetLayout ds_layout;
357 } blit;
358
359 struct {
360 VkDynamicRasterState rs_state;
361 VkDynamicColorBlendState cb_state;
362 VkDynamicDepthStencilState ds_state;
363 } shared;
364 };
365
366 struct anv_queue {
367 struct anv_device * device;
368
369 struct anv_state_pool * pool;
370
371 /**
372 * Serial number of the most recently completed batch executed on the
373 * engine.
374 */
375 struct anv_state completed_serial;
376
377 /**
378 * The next batch submitted to the engine will be assigned this serial
379 * number.
380 */
381 uint32_t next_serial;
382
383 uint32_t last_collected_serial;
384 };
385
386 struct anv_device {
387 struct anv_instance * instance;
388 uint32_t chipset_id;
389 struct brw_device_info info;
390 int context_id;
391 int fd;
392
393 struct anv_bo_pool batch_bo_pool;
394
395 struct anv_block_pool dynamic_state_block_pool;
396 struct anv_state_pool dynamic_state_pool;
397
398 struct anv_block_pool instruction_block_pool;
399 struct anv_block_pool surface_state_block_pool;
400 struct anv_state_pool surface_state_pool;
401
402 struct anv_meta_state meta_state;
403
404 struct anv_state border_colors;
405
406 struct anv_queue queue;
407
408 struct anv_block_pool scratch_block_pool;
409
410 struct anv_compiler * compiler;
411 pthread_mutex_t mutex;
412 };
413
414 void *
415 anv_device_alloc(struct anv_device * device,
416 size_t size,
417 size_t alignment,
418 VkSystemAllocType allocType);
419
420 void
421 anv_device_free(struct anv_device * device,
422 void * mem);
423
424 void* anv_gem_mmap(struct anv_device *device,
425 uint32_t gem_handle, uint64_t offset, uint64_t size);
426 void anv_gem_munmap(void *p, uint64_t size);
427 uint32_t anv_gem_create(struct anv_device *device, size_t size);
428 void anv_gem_close(struct anv_device *device, int gem_handle);
429 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
430 int anv_gem_wait(struct anv_device *device, int gem_handle, int64_t *timeout_ns);
431 int anv_gem_execbuffer(struct anv_device *device,
432 struct drm_i915_gem_execbuffer2 *execbuf);
433 int anv_gem_set_tiling(struct anv_device *device, int gem_handle,
434 uint32_t stride, uint32_t tiling);
435 int anv_gem_create_context(struct anv_device *device);
436 int anv_gem_destroy_context(struct anv_device *device, int context);
437 int anv_gem_get_param(int fd, uint32_t param);
438 int anv_gem_get_aperture(int fd, uint64_t *size);
439 int anv_gem_handle_to_fd(struct anv_device *device, int gem_handle);
440 int anv_gem_fd_to_handle(struct anv_device *device, int fd);
441 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
442
443 VkResult anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size);
444
445 struct anv_reloc_list {
446 size_t num_relocs;
447 size_t array_length;
448 struct drm_i915_gem_relocation_entry * relocs;
449 struct anv_bo ** reloc_bos;
450 };
451
452 VkResult anv_reloc_list_init(struct anv_reloc_list *list,
453 struct anv_device *device);
454 void anv_reloc_list_finish(struct anv_reloc_list *list,
455 struct anv_device *device);
456
457 uint64_t anv_reloc_list_add(struct anv_reloc_list *list,
458 struct anv_device *device,
459 uint32_t offset, struct anv_bo *target_bo,
460 uint32_t delta);
461
462 struct anv_batch_bo {
463 /* Link in the anv_cmd_buffer.owned_batch_bos list */
464 struct list_head link;
465
466 struct anv_bo bo;
467
468 /* Bytes actually consumed in this batch BO */
469 size_t length;
470
471 struct anv_reloc_list relocs;
472 };
473
474 struct anv_batch {
475 struct anv_device * device;
476
477 void * start;
478 void * end;
479 void * next;
480
481 struct anv_reloc_list * relocs;
482
483 /* This callback is called (with the associated user data) in the event
484 * that the batch runs out of space.
485 */
486 VkResult (*extend_cb)(struct anv_batch *, void *);
487 void * user_data;
488 };
489
490 void *anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords);
491 void anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other);
492 uint64_t anv_batch_emit_reloc(struct anv_batch *batch,
493 void *location, struct anv_bo *bo, uint32_t offset);
494
495 struct anv_address {
496 struct anv_bo *bo;
497 uint32_t offset;
498 };
499
500 #define __gen_address_type struct anv_address
501 #define __gen_user_data struct anv_batch
502
503 static inline uint64_t
504 __gen_combine_address(struct anv_batch *batch, void *location,
505 const struct anv_address address, uint32_t delta)
506 {
507 if (address.bo == NULL) {
508 return delta;
509 } else {
510 assert(batch->start <= location && location < batch->end);
511
512 return anv_batch_emit_reloc(batch, location, address.bo, address.offset + delta);
513 }
514 }
515
516 #include "gen7_pack.h"
517 #include "gen75_pack.h"
518 #undef GEN8_3DSTATE_MULTISAMPLE
519 #include "gen8_pack.h"
520
521 #define anv_batch_emit(batch, cmd, ...) do { \
522 void *__dst = anv_batch_emit_dwords(batch, cmd ## _length); \
523 struct cmd __template = { \
524 cmd ## _header, \
525 __VA_ARGS__ \
526 }; \
527 cmd ## _pack(batch, __dst, &__template); \
528 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, cmd ## _length * 4)); \
529 } while (0)
530
531 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
532 void *__dst = anv_batch_emit_dwords(batch, n); \
533 struct cmd __template = { \
534 cmd ## _header, \
535 .DwordLength = n - cmd ## _length_bias, \
536 __VA_ARGS__ \
537 }; \
538 cmd ## _pack(batch, __dst, &__template); \
539 __dst; \
540 })
541
542 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
543 do { \
544 uint32_t *dw; \
545 \
546 assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
547 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
548 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
549 dw[i] = (dwords0)[i] | (dwords1)[i]; \
550 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
551 } while (0)
552
553 #define GEN8_MOCS { \
554 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
555 .TargetCache = L3DefertoPATforLLCeLLCselection, \
556 .AgeforQUADLRU = 0 \
557 }
558
559 struct anv_device_memory {
560 struct anv_bo bo;
561 VkDeviceSize map_size;
562 void * map;
563 };
564
565 struct anv_dynamic_vp_state {
566 struct anv_state sf_clip_vp;
567 struct anv_state cc_vp;
568 struct anv_state scissor;
569 };
570
571 struct anv_dynamic_rs_state {
572 uint32_t state_sf[GEN8_3DSTATE_SF_length];
573 uint32_t state_raster[GEN8_3DSTATE_RASTER_length];
574 };
575
576 struct anv_dynamic_ds_state {
577 uint32_t state_wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
578 uint32_t state_color_calc[GEN8_COLOR_CALC_STATE_length];
579 };
580
581 struct anv_dynamic_cb_state {
582 uint32_t state_color_calc[GEN8_COLOR_CALC_STATE_length];
583
584 };
585
586 struct anv_descriptor_slot {
587 int8_t dynamic_slot;
588 uint8_t index;
589 };
590
591 struct anv_descriptor_set_layout {
592 struct {
593 uint32_t surface_count;
594 struct anv_descriptor_slot *surface_start;
595 uint32_t sampler_count;
596 struct anv_descriptor_slot *sampler_start;
597 } stage[VK_SHADER_STAGE_NUM];
598
599 uint32_t count;
600 uint32_t num_dynamic_buffers;
601 uint32_t shader_stages;
602 struct anv_descriptor_slot entries[0];
603 };
604
605 struct anv_descriptor {
606 struct anv_sampler *sampler;
607 struct anv_surface_view *view;
608 };
609
610 struct anv_descriptor_set {
611 struct anv_descriptor descriptors[0];
612 };
613
614 VkResult
615 anv_descriptor_set_create(struct anv_device *device,
616 const struct anv_descriptor_set_layout *layout,
617 struct anv_descriptor_set **out_set);
618
619 void
620 anv_descriptor_set_destroy(struct anv_device *device,
621 struct anv_descriptor_set *set);
622
623 #define MAX_VBS 32
624 #define MAX_SETS 8
625 #define MAX_RTS 8
626
627 struct anv_pipeline_layout {
628 struct {
629 struct anv_descriptor_set_layout *layout;
630 uint32_t surface_start[VK_SHADER_STAGE_NUM];
631 uint32_t sampler_start[VK_SHADER_STAGE_NUM];
632 } set[MAX_SETS];
633
634 uint32_t num_sets;
635
636 struct {
637 uint32_t surface_count;
638 uint32_t sampler_count;
639 } stage[VK_SHADER_STAGE_NUM];
640 };
641
642 struct anv_buffer {
643 struct anv_device * device;
644 VkDeviceSize size;
645
646 /* Set when bound */
647 struct anv_bo * bo;
648 VkDeviceSize offset;
649 };
650
651 #define ANV_CMD_BUFFER_PIPELINE_DIRTY (1 << 0)
652 #define ANV_CMD_BUFFER_RS_DIRTY (1 << 2)
653 #define ANV_CMD_BUFFER_DS_DIRTY (1 << 3)
654 #define ANV_CMD_BUFFER_CB_DIRTY (1 << 4)
655 #define ANV_CMD_BUFFER_VP_DIRTY (1 << 5)
656 #define ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY (1 << 6)
657
658 struct anv_vertex_binding {
659 struct anv_buffer * buffer;
660 VkDeviceSize offset;
661 };
662
663 struct anv_descriptor_set_binding {
664 struct anv_descriptor_set * set;
665 uint32_t dynamic_offsets[128];
666 };
667
668 /** State required while building cmd buffer */
669 struct anv_cmd_state {
670 uint32_t current_pipeline;
671 uint32_t vb_dirty;
672 uint32_t dirty;
673 uint32_t compute_dirty;
674 uint32_t descriptors_dirty;
675 uint32_t scratch_size;
676 struct anv_pipeline * pipeline;
677 struct anv_pipeline * compute_pipeline;
678 struct anv_framebuffer * framebuffer;
679 struct anv_render_pass * pass;
680 struct anv_subpass * subpass;
681 struct anv_dynamic_rs_state * rs_state;
682 struct anv_dynamic_ds_state * ds_state;
683 struct anv_dynamic_vp_state * vp_state;
684 struct anv_dynamic_cb_state * cb_state;
685 uint32_t state_vf[GEN8_3DSTATE_VF_length];
686 struct anv_vertex_binding vertex_bindings[MAX_VBS];
687 struct anv_descriptor_set_binding descriptors[MAX_SETS];
688 };
689
690 struct anv_cmd_pool {
691 struct list_head cmd_buffers;
692 };
693
694 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
695
696 enum anv_cmd_buffer_exec_mode {
697 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
698 ANV_CMD_BUFFER_EXEC_MODE_EMIT,
699 ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
700 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
701 };
702
703 struct anv_cmd_buffer {
704 struct anv_device * device;
705
706 struct list_head pool_link;
707
708 struct anv_batch batch;
709
710 /* Fields required for the actual chain of anv_batch_bo's.
711 *
712 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
713 */
714 struct list_head batch_bos;
715 struct list_head surface_bos;
716 uint32_t surface_next;
717 enum anv_cmd_buffer_exec_mode exec_mode;
718
719 /* A vector of anv_batch_bo pointers for every batch or surface buffer
720 * referenced by this command buffer
721 *
722 * initialized by anv_cmd_buffer_init_batch_bo_chain()
723 */
724 struct anv_vector seen_bbos;
725
726 /* Information needed for execbuf
727 *
728 * These fields are generated by anv_cmd_buffer_prepare_execbuf().
729 */
730 struct {
731 struct drm_i915_gem_execbuffer2 execbuf;
732
733 struct drm_i915_gem_exec_object2 * objects;
734 uint32_t bo_count;
735 struct anv_bo ** bos;
736
737 /* Allocated length of the 'objects' and 'bos' arrays */
738 uint32_t array_length;
739
740 bool need_reloc;
741 } execbuf2;
742
743 /* Serial for tracking buffer completion */
744 uint32_t serial;
745
746 /* Stream objects for storing temporary data */
747 struct anv_state_stream surface_state_stream;
748 struct anv_state_stream dynamic_state_stream;
749
750 VkCmdBufferOptimizeFlags opt_flags;
751 VkCmdBufferLevel level;
752
753 struct anv_cmd_state state;
754 };
755
756 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
757 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
758 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
759 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
760 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
761 struct anv_cmd_buffer *secondary);
762 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
763
764 struct anv_bo *
765 anv_cmd_buffer_current_surface_bo(struct anv_cmd_buffer *cmd_buffer);
766 struct anv_reloc_list *
767 anv_cmd_buffer_current_surface_relocs(struct anv_cmd_buffer *cmd_buffer);
768 struct anv_state
769 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer,
770 uint32_t size, uint32_t alignment);
771 struct anv_state
772 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
773 uint32_t size, uint32_t alignment);
774
775 VkResult anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer *cmd_buffer);
776
777 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
778
779 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
780 struct anv_subpass *subpass);
781
782 void anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
783 struct anv_render_pass *pass,
784 const VkClearValue *clear_values);
785
786 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
787
788 struct anv_fence {
789 struct anv_bo bo;
790 struct drm_i915_gem_execbuffer2 execbuf;
791 struct drm_i915_gem_exec_object2 exec2_objects[1];
792 bool ready;
793 };
794
795 struct anv_shader_module {
796 uint32_t size;
797 char data[0];
798 };
799
800 struct anv_shader {
801 struct anv_shader_module * module;
802 char entrypoint[0];
803 };
804
805 struct anv_pipeline {
806 struct anv_device * device;
807 struct anv_batch batch;
808 uint32_t batch_data[256];
809 struct anv_reloc_list batch_relocs;
810 struct anv_shader * shaders[VK_SHADER_STAGE_NUM];
811 struct anv_pipeline_layout * layout;
812 bool use_repclear;
813
814 struct brw_vs_prog_data vs_prog_data;
815 struct brw_wm_prog_data wm_prog_data;
816 struct brw_gs_prog_data gs_prog_data;
817 struct brw_cs_prog_data cs_prog_data;
818 bool writes_point_size;
819 struct brw_stage_prog_data * prog_data[VK_SHADER_STAGE_NUM];
820 uint32_t scratch_start[VK_SHADER_STAGE_NUM];
821 uint32_t total_scratch;
822 struct {
823 uint32_t vs_start;
824 uint32_t vs_size;
825 uint32_t nr_vs_entries;
826 uint32_t gs_start;
827 uint32_t gs_size;
828 uint32_t nr_gs_entries;
829 } urb;
830
831 uint32_t active_stages;
832 struct anv_state_stream program_stream;
833 struct anv_state blend_state;
834 uint32_t vs_simd8;
835 uint32_t ps_simd8;
836 uint32_t ps_simd16;
837 uint32_t gs_vec4;
838 uint32_t gs_vertex_count;
839 uint32_t cs_simd;
840
841 uint32_t vb_used;
842 uint32_t binding_stride[MAX_VBS];
843
844 uint32_t state_sf[GEN8_3DSTATE_SF_length];
845 uint32_t state_vf[GEN8_3DSTATE_VF_length];
846 uint32_t state_raster[GEN8_3DSTATE_RASTER_length];
847 uint32_t state_wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
848
849 uint32_t cs_thread_width_max;
850 uint32_t cs_right_mask;
851 };
852
853 struct anv_pipeline_create_info {
854 bool use_repclear;
855 bool disable_viewport;
856 bool disable_scissor;
857 bool disable_vs;
858 bool use_rectlist;
859 };
860
861 VkResult
862 anv_pipeline_create(VkDevice device,
863 const VkGraphicsPipelineCreateInfo *pCreateInfo,
864 const struct anv_pipeline_create_info *extra,
865 VkPipeline *pPipeline);
866
867 struct anv_compiler *anv_compiler_create(struct anv_device *device);
868 void anv_compiler_destroy(struct anv_compiler *compiler);
869 int anv_compiler_run(struct anv_compiler *compiler, struct anv_pipeline *pipeline);
870 void anv_compiler_free(struct anv_pipeline *pipeline);
871
872 struct anv_format {
873 const VkFormat vk_format;
874 const char *name;
875 uint16_t surface_format; /**< RENDER_SURFACE_STATE.SurfaceFormat */
876 uint8_t cpp; /**< Bytes-per-pixel of anv_format::surface_format. */
877 uint8_t num_channels;
878 uint16_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
879 bool has_stencil;
880 };
881
882 /**
883 * Stencil formats are often a special case. To reduce the number of lookups
884 * into the VkFormat-to-anv_format translation table when working with
885 * stencil, here is the handle to the table's entry for VK_FORMAT_S8_UINT.
886 */
887 extern const struct anv_format *const anv_format_s8_uint;
888
889 const struct anv_format *
890 anv_format_for_vk_format(VkFormat format);
891 bool anv_is_vk_format_depth_or_stencil(VkFormat format);
892
893 /**
894 * A proxy for the color surfaces, depth surfaces, and stencil surfaces.
895 */
896 struct anv_surface {
897 /**
898 * Offset from VkImage's base address, as bound by vkBindImageMemory().
899 */
900 uint32_t offset;
901
902 uint32_t stride; /**< RENDER_SURFACE_STATE.SurfacePitch */
903 uint16_t qpitch; /**< RENDER_SURFACE_STATE.QPitch */
904
905 /**
906 * \name Alignment of miptree images, in units of pixels.
907 *
908 * These fields contain the real alignment values, not the values to be
909 * given to the GPU. For example, if h_align is 4, then program the GPU
910 * with HALIGN_4.
911 * \{
912 */
913 uint8_t h_align; /**< RENDER_SURFACE_STATE.SurfaceHorizontalAlignment */
914 uint8_t v_align; /**< RENDER_SURFACE_STATE.SurfaceVerticalAlignment */
915 /** \} */
916
917 uint8_t tile_mode; /**< RENDER_SURFACE_STATE.TileMode */
918 };
919
920 struct anv_image {
921 VkImageType type;
922 const struct anv_format *format;
923 VkExtent3D extent;
924 uint32_t levels;
925 uint32_t array_size;
926
927 VkDeviceSize size;
928 uint32_t alignment;
929
930 /* Set when bound */
931 struct anv_bo *bo;
932 VkDeviceSize offset;
933
934 struct anv_swap_chain *swap_chain;
935
936 /** RENDER_SURFACE_STATE.SurfaceType */
937 uint8_t surf_type;
938
939 /** Primary surface is either color or depth. */
940 struct anv_surface primary_surface;
941
942 /** Stencil surface is optional. */
943 struct anv_surface stencil_surface;
944 };
945
946 struct anv_surface_view {
947 struct anv_state surface_state; /**< RENDER_SURFACE_STATE */
948 struct anv_bo *bo;
949 uint32_t offset; /**< VkBufferCreateInfo::offset */
950 uint32_t range; /**< VkBufferCreateInfo::range */
951 const struct anv_format *format; /**< VkBufferCreateInfo::format */
952 };
953
954 struct anv_buffer_view {
955 struct anv_surface_view view;
956 };
957
958 struct anv_image_view {
959 struct anv_surface_view view;
960 VkExtent3D extent;
961 };
962
963 enum anv_attachment_view_type {
964 ANV_ATTACHMENT_VIEW_TYPE_COLOR,
965 ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL,
966 };
967
968 struct anv_attachment_view {
969 enum anv_attachment_view_type attachment_type;
970 VkExtent3D extent;
971 };
972
973 struct anv_color_attachment_view {
974 struct anv_attachment_view base;
975 struct anv_surface_view view;
976 };
977
978 struct anv_depth_stencil_view {
979 struct anv_attachment_view base;
980
981 struct anv_bo *bo;
982
983 uint32_t depth_offset; /**< Offset into bo. */
984 uint32_t depth_stride; /**< 3DSTATE_DEPTH_BUFFER.SurfacePitch */
985 uint32_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
986 uint16_t depth_qpitch; /**< 3DSTATE_DEPTH_BUFFER.SurfaceQPitch */
987
988 uint32_t stencil_offset; /**< Offset into bo. */
989 uint32_t stencil_stride; /**< 3DSTATE_STENCIL_BUFFER.SurfacePitch */
990 uint16_t stencil_qpitch; /**< 3DSTATE_STENCIL_BUFFER.SurfaceQPitch */
991 };
992
993 struct anv_image_create_info {
994 const VkImageCreateInfo *vk_info;
995 bool force_tile_mode;
996 uint8_t tile_mode;
997 uint32_t stride;
998 };
999
1000 VkResult anv_image_create(VkDevice _device,
1001 const struct anv_image_create_info *info,
1002 VkImage *pImage);
1003
1004 void anv_image_view_init(struct anv_image_view *view,
1005 struct anv_device *device,
1006 const VkImageViewCreateInfo* pCreateInfo,
1007 struct anv_cmd_buffer *cmd_buffer);
1008
1009 void anv_color_attachment_view_init(struct anv_color_attachment_view *view,
1010 struct anv_device *device,
1011 const VkAttachmentViewCreateInfo* pCreateInfo,
1012 struct anv_cmd_buffer *cmd_buffer);
1013 void anv_fill_buffer_surface_state(void *state, const struct anv_format *format,
1014 uint32_t offset, uint32_t range);
1015
1016 void anv_surface_view_fini(struct anv_device *device,
1017 struct anv_surface_view *view);
1018
1019 struct anv_sampler {
1020 uint32_t state[4];
1021 };
1022
1023 struct anv_framebuffer {
1024 uint32_t width;
1025 uint32_t height;
1026 uint32_t layers;
1027
1028 /* Viewport for clears */
1029 VkDynamicViewportState vp_state;
1030
1031 uint32_t attachment_count;
1032 const struct anv_attachment_view * attachments[0];
1033 };
1034
1035 struct anv_subpass {
1036 uint32_t input_count;
1037 uint32_t * input_attachments;
1038 uint32_t color_count;
1039 uint32_t * color_attachments;
1040 uint32_t * resolve_attachments;
1041 uint32_t depth_stencil_attachment;
1042 };
1043
1044 struct anv_render_pass_attachment {
1045 VkFormat format;
1046 uint32_t samples;
1047 VkAttachmentLoadOp load_op;
1048 VkAttachmentLoadOp stencil_load_op;
1049 };
1050
1051 struct anv_render_pass {
1052 uint32_t attachment_count;
1053 uint32_t subpass_count;
1054
1055 struct anv_render_pass_attachment * attachments;
1056 struct anv_subpass subpasses[0];
1057 };
1058
1059 void anv_device_init_meta(struct anv_device *device);
1060 void anv_device_finish_meta(struct anv_device *device);
1061
1062 void *anv_lookup_entrypoint(const char *name);
1063
1064 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
1065 \
1066 static inline struct __anv_type * \
1067 __anv_type ## _from_handle(__VkType _handle) \
1068 { \
1069 return (struct __anv_type *) _handle; \
1070 } \
1071 \
1072 static inline __VkType \
1073 __anv_type ## _to_handle(struct __anv_type *_obj) \
1074 { \
1075 return (__VkType) _obj; \
1076 }
1077
1078 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
1079 \
1080 static inline struct __anv_type * \
1081 __anv_type ## _from_handle(__VkType _handle) \
1082 { \
1083 return (struct __anv_type *) _handle.handle; \
1084 } \
1085 \
1086 static inline __VkType \
1087 __anv_type ## _to_handle(struct __anv_type *_obj) \
1088 { \
1089 return (__VkType) { .handle = (uint64_t) _obj }; \
1090 }
1091
1092 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
1093 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
1094
1095 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer, VkCmdBuffer)
1096 ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
1097 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
1098 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
1099 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
1100 ANV_DEFINE_HANDLE_CASTS(anv_swap_chain, VkSwapChainWSI);
1101
1102 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCmdPool)
1103 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_attachment_view, VkAttachmentView)
1104 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
1105 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView);
1106 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
1107 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
1108 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
1109 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_cb_state, VkDynamicColorBlendState)
1110 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_ds_state, VkDynamicDepthStencilState)
1111 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_rs_state, VkDynamicRasterState)
1112 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_vp_state, VkDynamicViewportState)
1113 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
1114 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
1115 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)
1116 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view, VkImageView);
1117 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline, VkPipeline)
1118 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout, VkPipelineLayout)
1119 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool, VkQueryPool)
1120 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass, VkRenderPass)
1121 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler, VkSampler)
1122 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader, VkShader)
1123 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module, VkShaderModule)
1124
1125 #define ANV_DEFINE_STRUCT_CASTS(__anv_type, __VkType) \
1126 \
1127 static inline const __VkType * \
1128 __anv_type ## _to_ ## __VkType(const struct __anv_type *__anv_obj) \
1129 { \
1130 return (const __VkType *) __anv_obj; \
1131 }
1132
1133 #define ANV_COMMON_TO_STRUCT(__VkType, __vk_name, __common_name) \
1134 const __VkType *__vk_name = anv_common_to_ ## __VkType(__common_name)
1135
1136 ANV_DEFINE_STRUCT_CASTS(anv_common, VkMemoryBarrier)
1137 ANV_DEFINE_STRUCT_CASTS(anv_common, VkBufferMemoryBarrier)
1138 ANV_DEFINE_STRUCT_CASTS(anv_common, VkImageMemoryBarrier)
1139
1140 #ifdef __cplusplus
1141 }
1142 #endif