2 * Copyright © 2015 Intel Corporation
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:
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
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
37 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
42 #include "brw_device_info.h"
43 #include "util/macros.h"
44 #include "util/list.h"
47 #include <vulkan/vulkan.h>
48 #include <vulkan/vulkan_intel.h>
49 #include <vulkan/vk_ext_khr_swapchain.h>
50 #include <vulkan/vk_ext_khr_device_swapchain.h>
52 #include "anv_entrypoints.h"
54 #include "brw_context.h"
60 #define ICD_LOADER_MAGIC 0x01CDC0DE
62 typedef union _VK_LOADER_DATA
{
63 uintptr_t loaderMagic
;
67 #define anv_noreturn __attribute__((__noreturn__))
68 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
71 #define MAX(a, b) ((a) > (b) ? (a) : (b))
73 static inline uint32_t
74 align_u32(uint32_t v
, uint32_t a
)
76 return (v
+ a
- 1) & ~(a
- 1);
80 align_i32(int32_t v
, int32_t a
)
82 return (v
+ a
- 1) & ~(a
- 1);
85 /** Alignment must be a power of 2. */
87 anv_is_aligned(uintmax_t n
, uintmax_t a
)
89 assert(a
== (a
& -a
));
90 return (n
& (a
- 1)) == 0;
93 static inline uint32_t
94 anv_minify(uint32_t n
, uint32_t levels
)
99 return MAX(n
>> levels
, 1);
103 anv_clear_mask(uint32_t *inout_mask
, uint32_t clear_mask
)
105 if (*inout_mask
& clear_mask
) {
106 *inout_mask
&= ~clear_mask
;
113 #define for_each_bit(b, dword) \
114 for (uint32_t __dword = (dword); \
115 (b) = __builtin_ffs(__dword) - 1, __dword; \
116 __dword &= ~(1 << (b)))
118 #define typed_memcpy(dest, src, count) ({ \
119 static_assert(sizeof(*src) == sizeof(*dest), ""); \
120 memcpy((dest), (src), (count) * sizeof(*(src))); \
123 /* Define no kernel as 1, since that's an illegal offset for a kernel */
127 VkStructureType sType
;
131 /* Whenever we generate an error, pass it through this function. Useful for
132 * debugging, where we can break on it. Only call at error site, not when
133 * propagating errors. Might be useful to plug in a stack trace here.
136 VkResult
__vk_errorf(VkResult error
, const char *file
, int line
, const char *format
, ...);
139 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
140 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
142 #define vk_error(error) error
143 #define vk_errorf(error, format, ...) error
146 void __anv_finishme(const char *file
, int line
, const char *format
, ...)
147 anv_printflike(3, 4);
148 void anv_loge(const char *format
, ...) anv_printflike(1, 2);
149 void anv_loge_v(const char *format
, va_list va
);
152 * Print a FINISHME message, including its source location.
154 #define anv_finishme(format, ...) \
155 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);
157 /* A non-fatal assert. Useful for debugging. */
159 #define anv_assert(x) ({ \
160 if (unlikely(!(x))) \
161 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
164 #define anv_assert(x)
167 void anv_abortf(const char *format
, ...) anv_noreturn
anv_printflike(1, 2);
168 void anv_abortfv(const char *format
, va_list va
) anv_noreturn
;
170 #define stub_return(v) \
172 anv_finishme("stub %s", __func__); \
178 anv_finishme("stub %s", __func__); \
183 * A dynamically growable, circular buffer. Elements are added at head and
184 * removed from tail. head and tail are free-running uint32_t indices and we
185 * only compute the modulo with size when accessing the array. This way,
186 * number of bytes in the queue is always head - tail, even in case of
193 uint32_t element_size
;
198 int anv_vector_init(struct anv_vector
*queue
, uint32_t element_size
, uint32_t size
);
199 void *anv_vector_add(struct anv_vector
*queue
);
200 void *anv_vector_remove(struct anv_vector
*queue
);
203 anv_vector_length(struct anv_vector
*queue
)
205 return (queue
->head
- queue
->tail
) / queue
->element_size
;
209 anv_vector_head(struct anv_vector
*vector
)
211 assert(vector
->tail
< vector
->head
);
212 return (void *)((char *)vector
->data
+
213 ((vector
->head
- vector
->element_size
) &
214 (vector
->size
- 1)));
218 anv_vector_tail(struct anv_vector
*vector
)
220 return (void *)((char *)vector
->data
+ (vector
->tail
& (vector
->size
- 1)));
224 anv_vector_finish(struct anv_vector
*queue
)
229 #define anv_vector_foreach(elem, queue) \
230 static_assert(__builtin_types_compatible_p(__typeof__(queue), struct anv_vector *), ""); \
231 for (uint32_t __anv_vector_offset = (queue)->tail; \
232 elem = (queue)->data + (__anv_vector_offset & ((queue)->size - 1)), __anv_vector_offset < (queue)->head; \
233 __anv_vector_offset += (queue)->element_size)
238 /* Index into the current validation list. This is used by the
239 * validation list building alrogithm to track which buffers are already
240 * in the validation list so that we can ensure uniqueness.
244 /* Last known offset. This value is provided by the kernel when we
245 * execbuf and is used as the presumed offset for the next bunch of
254 /* Represents a lock-free linked list of "free" things. This is used by
255 * both the block pool and the state pools. Unfortunately, in order to
256 * solve the ABA problem, we can't use a single uint32_t head.
258 union anv_free_list
{
262 /* A simple count that is incremented every time the head changes. */
268 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
270 struct anv_block_state
{
280 struct anv_block_pool
{
281 struct anv_device
*device
;
285 /* The offset from the start of the bo to the "center" of the block
286 * pool. Pointers to allocated blocks are given by
287 * bo.map + center_bo_offset + offsets.
289 uint32_t center_bo_offset
;
291 /* Current memory map of the block pool. This pointer may or may not
292 * point to the actual beginning of the block pool memory. If
293 * anv_block_pool_alloc_back has ever been called, then this pointer
294 * will point to the "center" position of the buffer and all offsets
295 * (negative or positive) given out by the block pool alloc functions
296 * will be valid relative to this pointer.
298 * In particular, map == bo.map + center_offset
304 * Array of mmaps and gem handles owned by the block pool, reclaimed when
305 * the block pool is destroyed.
307 struct anv_vector mmap_cleanups
;
311 union anv_free_list free_list
;
312 struct anv_block_state state
;
314 union anv_free_list back_free_list
;
315 struct anv_block_state back_state
;
318 /* Block pools are backed by a fixed-size 2GB memfd */
319 #define BLOCK_POOL_MEMFD_SIZE (1ull << 32)
321 /* The center of the block pool is also the middle of the memfd. This may
322 * change in the future if we decide differently for some reason.
324 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
326 static inline uint32_t
327 anv_block_pool_size(struct anv_block_pool
*pool
)
329 return pool
->state
.end
+ pool
->back_state
.end
;
338 struct anv_fixed_size_state_pool
{
340 union anv_free_list free_list
;
341 struct anv_block_state block
;
344 #define ANV_MIN_STATE_SIZE_LOG2 6
345 #define ANV_MAX_STATE_SIZE_LOG2 10
347 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2)
349 struct anv_state_pool
{
350 struct anv_block_pool
*block_pool
;
351 struct anv_fixed_size_state_pool buckets
[ANV_STATE_BUCKETS
];
354 struct anv_state_stream
{
355 struct anv_block_pool
*block_pool
;
357 uint32_t current_block
;
361 void anv_block_pool_init(struct anv_block_pool
*pool
,
362 struct anv_device
*device
, uint32_t block_size
);
363 void anv_block_pool_finish(struct anv_block_pool
*pool
);
364 int32_t anv_block_pool_alloc(struct anv_block_pool
*pool
);
365 int32_t anv_block_pool_alloc_back(struct anv_block_pool
*pool
);
366 void anv_block_pool_free(struct anv_block_pool
*pool
, int32_t offset
);
367 void anv_state_pool_init(struct anv_state_pool
*pool
,
368 struct anv_block_pool
*block_pool
);
369 void anv_state_pool_finish(struct anv_state_pool
*pool
);
370 struct anv_state
anv_state_pool_alloc(struct anv_state_pool
*pool
,
371 size_t state_size
, size_t alignment
);
372 void anv_state_pool_free(struct anv_state_pool
*pool
, struct anv_state state
);
373 void anv_state_stream_init(struct anv_state_stream
*stream
,
374 struct anv_block_pool
*block_pool
);
375 void anv_state_stream_finish(struct anv_state_stream
*stream
);
376 struct anv_state
anv_state_stream_alloc(struct anv_state_stream
*stream
,
377 uint32_t size
, uint32_t alignment
);
380 * Implements a pool of re-usable BOs. The interface is identical to that
381 * of block_pool except that each block is its own BO.
384 struct anv_device
*device
;
391 void anv_bo_pool_init(struct anv_bo_pool
*pool
,
392 struct anv_device
*device
, uint32_t block_size
);
393 void anv_bo_pool_finish(struct anv_bo_pool
*pool
);
394 VkResult
anv_bo_pool_alloc(struct anv_bo_pool
*pool
, struct anv_bo
*bo
);
395 void anv_bo_pool_free(struct anv_bo_pool
*pool
, const struct anv_bo
*bo
);
398 void *anv_resolve_entrypoint(uint32_t index
);
400 extern struct anv_dispatch_table dtable
;
402 #define ANV_CALL(func) ({ \
403 if (dtable.func == NULL) { \
404 size_t idx = offsetof(struct anv_dispatch_table, func) / sizeof(void *); \
405 dtable.entrypoints[idx] = anv_resolve_entrypoint(idx); \
411 struct anv_physical_device
{
412 VK_LOADER_DATA _loader_data
;
414 struct anv_instance
* instance
;
418 const struct brw_device_info
* info
;
419 uint64_t aperture_size
;
422 struct anv_instance
{
423 VK_LOADER_DATA _loader_data
;
425 void * pAllocUserData
;
426 PFN_vkAllocFunction pfnAlloc
;
427 PFN_vkFreeFunction pfnFree
;
429 uint32_t physicalDeviceCount
;
430 struct anv_physical_device physicalDevice
;
432 struct anv_wsi_implementation
* wsi_impl
[VK_PLATFORM_NUM_KHR
];
435 VkResult
anv_init_wsi(struct anv_instance
*instance
);
436 void anv_finish_wsi(struct anv_instance
*instance
);
438 struct anv_meta_state
{
444 /** Pipeline that blits from a 2D image. */
445 VkPipeline pipeline_2d_src
;
447 /** Pipeline that blits from a 3D image. */
448 VkPipeline pipeline_3d_src
;
450 VkPipelineLayout pipeline_layout
;
451 VkDescriptorSetLayout ds_layout
;
456 VK_LOADER_DATA _loader_data
;
458 struct anv_device
* device
;
460 struct anv_state_pool
* pool
;
464 VK_LOADER_DATA _loader_data
;
466 struct anv_instance
* instance
;
468 struct brw_device_info info
;
472 struct anv_bo_pool batch_bo_pool
;
474 struct anv_block_pool dynamic_state_block_pool
;
475 struct anv_state_pool dynamic_state_pool
;
477 struct anv_block_pool instruction_block_pool
;
478 struct anv_block_pool surface_state_block_pool
;
479 struct anv_state_pool surface_state_pool
;
481 struct anv_meta_state meta_state
;
483 struct anv_state border_colors
;
485 struct anv_queue queue
;
487 struct anv_block_pool scratch_block_pool
;
489 struct anv_compiler
* compiler
;
490 pthread_mutex_t mutex
;
494 anv_instance_alloc(struct anv_instance
* instance
,
497 VkSystemAllocType allocType
);
500 anv_instance_free(struct anv_instance
* instance
,
504 anv_device_alloc(struct anv_device
* device
,
507 VkSystemAllocType allocType
);
510 anv_device_free(struct anv_device
* device
,
513 void* anv_gem_mmap(struct anv_device
*device
,
514 uint32_t gem_handle
, uint64_t offset
, uint64_t size
);
515 void anv_gem_munmap(void *p
, uint64_t size
);
516 uint32_t anv_gem_create(struct anv_device
*device
, size_t size
);
517 void anv_gem_close(struct anv_device
*device
, int gem_handle
);
518 int anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
);
519 int anv_gem_wait(struct anv_device
*device
, int gem_handle
, int64_t *timeout_ns
);
520 int anv_gem_execbuffer(struct anv_device
*device
,
521 struct drm_i915_gem_execbuffer2
*execbuf
);
522 int anv_gem_set_tiling(struct anv_device
*device
, int gem_handle
,
523 uint32_t stride
, uint32_t tiling
);
524 int anv_gem_create_context(struct anv_device
*device
);
525 int anv_gem_destroy_context(struct anv_device
*device
, int context
);
526 int anv_gem_get_param(int fd
, uint32_t param
);
527 int anv_gem_get_aperture(int fd
, uint64_t *size
);
528 int anv_gem_handle_to_fd(struct anv_device
*device
, int gem_handle
);
529 int anv_gem_fd_to_handle(struct anv_device
*device
, int fd
);
530 int anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
);
532 VkResult
anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
);
534 struct anv_reloc_list
{
537 struct drm_i915_gem_relocation_entry
* relocs
;
538 struct anv_bo
** reloc_bos
;
541 VkResult
anv_reloc_list_init(struct anv_reloc_list
*list
,
542 struct anv_device
*device
);
543 void anv_reloc_list_finish(struct anv_reloc_list
*list
,
544 struct anv_device
*device
);
546 uint64_t anv_reloc_list_add(struct anv_reloc_list
*list
,
547 struct anv_device
*device
,
548 uint32_t offset
, struct anv_bo
*target_bo
,
551 struct anv_batch_bo
{
552 /* Link in the anv_cmd_buffer.owned_batch_bos list */
553 struct list_head link
;
557 /* Bytes actually consumed in this batch BO */
560 /* Last seen surface state block pool bo offset */
561 uint32_t last_ss_pool_bo_offset
;
563 struct anv_reloc_list relocs
;
567 struct anv_device
* device
;
573 struct anv_reloc_list
* relocs
;
575 /* This callback is called (with the associated user data) in the event
576 * that the batch runs out of space.
578 VkResult (*extend_cb
)(struct anv_batch
*, void *);
582 void *anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
);
583 void anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
);
584 uint64_t anv_batch_emit_reloc(struct anv_batch
*batch
,
585 void *location
, struct anv_bo
*bo
, uint32_t offset
);
592 #define __gen_address_type struct anv_address
593 #define __gen_user_data struct anv_batch
595 static inline uint64_t
596 __gen_combine_address(struct anv_batch
*batch
, void *location
,
597 const struct anv_address address
, uint32_t delta
)
599 if (address
.bo
== NULL
) {
600 return address
.offset
+ delta
;
602 assert(batch
->start
<= location
&& location
< batch
->end
);
604 return anv_batch_emit_reloc(batch
, location
, address
.bo
, address
.offset
+ delta
);
608 #include "gen7_pack.h"
609 #include "gen75_pack.h"
610 #undef GEN8_3DSTATE_MULTISAMPLE
611 #include "gen8_pack.h"
613 #define anv_batch_emit(batch, cmd, ...) do { \
614 void *__dst = anv_batch_emit_dwords(batch, cmd ## _length); \
615 struct cmd __template = { \
619 cmd ## _pack(batch, __dst, &__template); \
620 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, cmd ## _length * 4)); \
623 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
624 void *__dst = anv_batch_emit_dwords(batch, n); \
625 struct cmd __template = { \
627 .DwordLength = n - cmd ## _length_bias, \
630 cmd ## _pack(batch, __dst, &__template); \
634 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
638 assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
639 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
640 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
641 dw[i] = (dwords0)[i] | (dwords1)[i]; \
642 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
645 static const struct GEN7_MEMORY_OBJECT_CONTROL_STATE GEN7_MOCS
= {
646 .GraphicsDataTypeGFDT
= 0,
647 .LLCCacheabilityControlLLCCC
= 0,
648 .L3CacheabilityControlL3CC
= 1
651 #define GEN8_MOCS { \
652 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
653 .TargetCache = L3DefertoPATforLLCeLLCselection, \
657 struct anv_device_memory
{
659 VkDeviceSize map_size
;
663 struct anv_descriptor_set_binding_layout
{
664 /* Number of array elements in this binding */
667 /* Index into the dynamic state array for a dynamic buffer */
668 int16_t dynamic_offset_index
;
671 /* Index into the binding table for the associated surface */
672 int16_t surface_index
;
674 /* Index into the sampler table for the associated sampler */
675 int16_t sampler_index
;
676 } stage
[VK_SHADER_STAGE_NUM
];
679 struct anv_descriptor_set_layout
{
680 /* Number of bindings in this descriptor set */
681 uint16_t binding_count
;
683 /* Total size of the descriptor set with room for all array entries */
686 /* Shader stages affected by this descriptor set */
687 uint16_t shader_stages
;
689 /* Number of dynamic offsets used by this descriptor set */
690 uint16_t dynamic_offset_count
;
692 /* Don't use this directly */
693 struct anv_descriptor_set_binding_layout binding
[0];
696 enum anv_descriptor_type
{
697 ANV_DESCRIPTOR_TYPE_EMPTY
= 0,
698 ANV_DESCRIPTOR_TYPE_BUFFER_VIEW
,
699 ANV_DESCRIPTOR_TYPE_BUFFER_AND_OFFSET
,
700 ANV_DESCRIPTOR_TYPE_IMAGE_VIEW
,
701 ANV_DESCRIPTOR_TYPE_SAMPLER
,
702 ANV_DESCRIPTOR_TYPE_IMAGE_VIEW_AND_SAMPLER
,
705 struct anv_descriptor
{
706 enum anv_descriptor_type type
;
711 struct anv_buffer_view
*buffer_view
;
712 struct anv_image_view
*image_view
;
714 struct anv_sampler
*sampler
;
718 struct anv_buffer
*buffer
;
725 struct anv_descriptor_set
{
726 struct anv_descriptor descriptors
[0];
730 anv_descriptor_set_create(struct anv_device
*device
,
731 const struct anv_descriptor_set_layout
*layout
,
732 struct anv_descriptor_set
**out_set
);
735 anv_descriptor_set_destroy(struct anv_device
*device
,
736 struct anv_descriptor_set
*set
);
741 #define MAX_VIEWPORTS 16
742 #define MAX_SCISSORS 16
743 #define MAX_PUSH_CONSTANTS_SIZE 128
744 #define MAX_DYNAMIC_BUFFERS 16
747 struct anv_pipeline_binding
{
748 /* The descriptor set this surface corresponds to */
751 /* Offset into the descriptor set */
755 struct anv_pipeline_layout
{
757 struct anv_descriptor_set_layout
*layout
;
758 uint32_t dynamic_offset_start
;
760 uint32_t surface_start
;
761 uint32_t sampler_start
;
762 } stage
[VK_SHADER_STAGE_NUM
];
768 bool has_dynamic_offsets
;
769 uint32_t surface_count
;
770 struct anv_pipeline_binding
*surface_to_descriptor
;
771 uint32_t sampler_count
;
772 struct anv_pipeline_binding
*sampler_to_descriptor
;
773 } stage
[VK_SHADER_STAGE_NUM
];
775 struct anv_pipeline_binding entries
[0];
779 struct anv_device
* device
;
787 /* The first 9 correspond to 1 << VK_DYNAMIC_STATE_FOO */
788 #define ANV_DYNAMIC_VIEWPORT_DIRTY (1 << 0)
789 #define ANV_DYNAMIC_SCISSOR_DIRTY (1 << 1)
790 #define ANV_DYNAMIC_LINE_WIDTH_DIRTY (1 << 2)
791 #define ANV_DYNAMIC_DEPTH_BIAS_DIRTY (1 << 3)
792 #define ANV_DYNAMIC_BLEND_CONSTANTS_DIRTY (1 << 4)
793 #define ANV_DYNAMIC_DEPTH_BOUNDS_DIRTY (1 << 5)
794 #define ANV_DYNAMIC_STENCIL_COMPARE_MASK_DIRTY (1 << 6)
795 #define ANV_DYNAMIC_STENCIL_WRITE_MASK_DIRTY (1 << 7)
796 #define ANV_DYNAMIC_STENCIL_REFERENCE_DIRTY (1 << 8)
797 #define ANV_DYNAMIC_STATE_DIRTY_MASK ((1 << 9) - 1)
798 #define ANV_CMD_BUFFER_PIPELINE_DIRTY (1 << 9)
799 #define ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY (1 << 10)
801 struct anv_vertex_binding
{
802 struct anv_buffer
* buffer
;
806 struct anv_push_constants
{
807 /* Current allocated size of this push constants data structure.
808 * Because a decent chunk of it may not be used (images on SKL, for
809 * instance), we won't actually allocate the entire structure up-front.
813 /* Push constant data provided by the client through vkPushConstants */
814 uint8_t client_data
[MAX_PUSH_CONSTANTS_SIZE
];
816 /* Our hardware only provides zero-based vertex and instance id so, in
817 * order to satisfy the vulkan requirements, we may have to push one or
818 * both of these into the shader.
820 uint32_t base_vertex
;
821 uint32_t base_instance
;
823 /* Offsets for dynamically bound buffers */
824 uint32_t dynamic_offsets
[MAX_DYNAMIC_BUFFERS
];
826 /* Image data for image_load_store on pre-SKL */
827 struct brw_image_param images
[MAX_IMAGES
];
830 struct anv_dynamic_state
{
833 VkViewport viewports
[MAX_VIEWPORTS
];
838 VkRect2D scissors
[MAX_SCISSORS
];
849 float blend_constants
[4];
859 } stencil_compare_mask
;
864 } stencil_write_mask
;
872 extern const struct anv_dynamic_state default_dynamic_state
;
874 void anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
875 const struct anv_dynamic_state
*src
,
878 /** State required while building cmd buffer */
879 struct anv_cmd_state
{
880 uint32_t current_pipeline
;
883 uint32_t compute_dirty
;
884 VkShaderStageFlags descriptors_dirty
;
885 VkShaderStageFlags push_constants_dirty
;
886 uint32_t scratch_size
;
887 struct anv_pipeline
* pipeline
;
888 struct anv_pipeline
* compute_pipeline
;
889 struct anv_framebuffer
* framebuffer
;
890 struct anv_render_pass
* pass
;
891 struct anv_subpass
* subpass
;
892 uint32_t state_vf
[GEN8_3DSTATE_VF_length
];
893 struct anv_vertex_binding vertex_bindings
[MAX_VBS
];
894 struct anv_descriptor_set
* descriptors
[MAX_SETS
];
895 struct anv_push_constants
* push_constants
[VK_SHADER_STAGE_NUM
];
896 struct anv_dynamic_state dynamic
;
899 struct anv_buffer
* index_buffer
;
900 uint32_t index_type
; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
901 uint32_t index_offset
;
905 struct anv_cmd_pool
{
906 struct list_head cmd_buffers
;
909 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
911 enum anv_cmd_buffer_exec_mode
{
912 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
,
913 ANV_CMD_BUFFER_EXEC_MODE_EMIT
,
914 ANV_CMD_BUFFER_EXEC_MODE_CHAIN
,
915 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
,
918 struct anv_cmd_buffer
{
919 VK_LOADER_DATA _loader_data
;
921 struct anv_device
* device
;
923 struct list_head pool_link
;
925 struct anv_batch batch
;
927 /* Fields required for the actual chain of anv_batch_bo's.
929 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
931 struct list_head batch_bos
;
932 enum anv_cmd_buffer_exec_mode exec_mode
;
934 /* A vector of anv_batch_bo pointers for every batch or surface buffer
935 * referenced by this command buffer
937 * initialized by anv_cmd_buffer_init_batch_bo_chain()
939 struct anv_vector seen_bbos
;
941 /* A vector of int32_t's for every block of binding tables.
943 * initialized by anv_cmd_buffer_init_batch_bo_chain()
945 struct anv_vector bt_blocks
;
947 struct anv_reloc_list surface_relocs
;
949 /* Information needed for execbuf
951 * These fields are generated by anv_cmd_buffer_prepare_execbuf().
954 struct drm_i915_gem_execbuffer2 execbuf
;
956 struct drm_i915_gem_exec_object2
* objects
;
958 struct anv_bo
** bos
;
960 /* Allocated length of the 'objects' and 'bos' arrays */
961 uint32_t array_length
;
966 /* Serial for tracking buffer completion */
969 /* Stream objects for storing temporary data */
970 struct anv_state_stream surface_state_stream
;
971 struct anv_state_stream dynamic_state_stream
;
973 VkCmdBufferOptimizeFlags opt_flags
;
974 VkCmdBufferLevel level
;
976 struct anv_cmd_state state
;
979 VkResult
anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
980 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
981 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
982 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
);
983 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
984 struct anv_cmd_buffer
*secondary
);
985 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer
*cmd_buffer
);
987 VkResult
anv_cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
988 unsigned stage
, struct anv_state
*bt_state
);
989 VkResult
anv_cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
990 unsigned stage
, struct anv_state
*state
);
991 void anv_flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
);
993 struct anv_state
anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
994 uint32_t *a
, uint32_t dwords
,
996 struct anv_state
anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
997 uint32_t *a
, uint32_t *b
,
998 uint32_t dwords
, uint32_t alignment
);
999 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
1000 struct anv_subpass
*subpass
);
1003 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1005 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1006 uint32_t entries
, uint32_t *state_offset
);
1008 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
);
1010 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
1011 uint32_t size
, uint32_t alignment
);
1014 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
);
1016 void anv_cmd_buffer_emit_viewport(struct anv_cmd_buffer
*cmd_buffer
);
1017 void anv_cmd_buffer_emit_scissor(struct anv_cmd_buffer
*cmd_buffer
);
1019 void gen7_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1020 void gen8_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1022 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1024 void gen7_cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
1025 struct anv_subpass
*subpass
);
1027 void gen8_cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
1028 struct anv_subpass
*subpass
);
1030 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
1031 struct anv_subpass
*subpass
);
1034 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
1035 VkShaderStage stage
);
1037 void anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer
*cmd_buffer
,
1038 struct anv_render_pass
*pass
,
1039 const VkClearValue
*clear_values
);
1040 const struct anv_image_view
*
1041 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
);
1043 void anv_cmd_buffer_dump(struct anv_cmd_buffer
*cmd_buffer
);
1047 struct drm_i915_gem_execbuffer2 execbuf
;
1048 struct drm_i915_gem_exec_object2 exec2_objects
[1];
1054 struct anv_shader_module
{
1055 struct nir_shader
* nir
;
1062 struct anv_shader_module
* module
;
1066 struct anv_pipeline
{
1067 struct anv_device
* device
;
1068 struct anv_batch batch
;
1069 uint32_t batch_data
[256];
1070 struct anv_reloc_list batch_relocs
;
1071 uint32_t dynamic_state_mask
;
1072 struct anv_dynamic_state dynamic_state
;
1074 struct anv_shader
* shaders
[VK_SHADER_STAGE_NUM
];
1075 struct anv_pipeline_layout
* layout
;
1078 struct brw_vs_prog_data vs_prog_data
;
1079 struct brw_wm_prog_data wm_prog_data
;
1080 struct brw_gs_prog_data gs_prog_data
;
1081 struct brw_cs_prog_data cs_prog_data
;
1082 bool writes_point_size
;
1083 struct brw_stage_prog_data
* prog_data
[VK_SHADER_STAGE_NUM
];
1084 uint32_t scratch_start
[VK_SHADER_STAGE_NUM
];
1085 uint32_t total_scratch
;
1089 uint32_t nr_vs_entries
;
1092 uint32_t nr_gs_entries
;
1095 VkShaderStageFlags active_stages
;
1096 struct anv_state_stream program_stream
;
1097 struct anv_state blend_state
;
1104 uint32_t ps_grf_start0
;
1105 uint32_t ps_grf_start2
;
1107 uint32_t gs_vertex_count
;
1111 uint32_t binding_stride
[MAX_VBS
];
1112 bool instancing_enable
[MAX_VBS
];
1113 bool primitive_restart
;
1116 uint32_t cs_thread_width_max
;
1117 uint32_t cs_right_mask
;
1120 uint32_t sf
[GEN7_3DSTATE_SF_length
];
1121 uint32_t depth_stencil_state
[GEN7_DEPTH_STENCIL_STATE_length
];
1125 uint32_t sf
[GEN8_3DSTATE_SF_length
];
1126 uint32_t vf
[GEN8_3DSTATE_VF_length
];
1127 uint32_t raster
[GEN8_3DSTATE_RASTER_length
];
1128 uint32_t wm_depth_stencil
[GEN8_3DSTATE_WM_DEPTH_STENCIL_length
];
1132 struct anv_graphics_pipeline_create_info
{
1134 bool disable_viewport
;
1135 bool disable_scissor
;
1141 anv_pipeline_init(struct anv_pipeline
*pipeline
, struct anv_device
*device
,
1142 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1143 const struct anv_graphics_pipeline_create_info
*extra
);
1146 anv_graphics_pipeline_create(VkDevice device
,
1147 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1148 const struct anv_graphics_pipeline_create_info
*extra
,
1149 VkPipeline
*pPipeline
);
1152 gen7_graphics_pipeline_create(VkDevice _device
,
1153 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1154 const struct anv_graphics_pipeline_create_info
*extra
,
1155 VkPipeline
*pPipeline
);
1158 gen8_graphics_pipeline_create(VkDevice _device
,
1159 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1160 const struct anv_graphics_pipeline_create_info
*extra
,
1161 VkPipeline
*pPipeline
);
1163 gen7_compute_pipeline_create(VkDevice _device
,
1164 const VkComputePipelineCreateInfo
*pCreateInfo
,
1165 VkPipeline
*pPipeline
);
1168 gen8_compute_pipeline_create(VkDevice _device
,
1169 const VkComputePipelineCreateInfo
*pCreateInfo
,
1170 VkPipeline
*pPipeline
);
1172 struct anv_compiler
*anv_compiler_create(struct anv_device
*device
);
1173 void anv_compiler_destroy(struct anv_compiler
*compiler
);
1174 int anv_compiler_run(struct anv_compiler
*compiler
, struct anv_pipeline
*pipeline
);
1175 void anv_compiler_free(struct anv_pipeline
*pipeline
);
1178 const VkFormat vk_format
;
1180 uint16_t surface_format
; /**< RENDER_SURFACE_STATE.SurfaceFormat */
1181 uint8_t cpp
; /**< Bytes-per-pixel of anv_format::surface_format. */
1182 uint8_t num_channels
;
1183 uint16_t depth_format
; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
1188 * Stencil formats are often a special case. To reduce the number of lookups
1189 * into the VkFormat-to-anv_format translation table when working with
1190 * stencil, here is the handle to the table's entry for VK_FORMAT_S8_UINT.
1192 extern const struct anv_format
*const anv_format_s8_uint
;
1194 const struct anv_format
*
1195 anv_format_for_vk_format(VkFormat format
);
1198 anv_format_is_color(const struct anv_format
*format
)
1200 return !format
->depth_format
&& !format
->has_stencil
;
1204 anv_format_is_depth_or_stencil(const struct anv_format
*format
)
1206 return format
->depth_format
|| format
->has_stencil
;
1209 struct anv_image_view_info
{
1210 uint8_t surface_type
; /**< RENDER_SURFACE_STATE.SurfaceType */
1211 bool is_array
:1; /**< RENDER_SURFACE_STATE.SurfaceArray */
1212 bool is_cube
:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
1215 struct anv_image_view_info
1216 anv_image_view_info_for_vk_image_view_type(VkImageViewType type
);
1219 * A proxy for the color surfaces, depth surfaces, and stencil surfaces.
1221 struct anv_surface
{
1223 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1227 uint32_t stride
; /**< RENDER_SURFACE_STATE.SurfacePitch */
1228 uint16_t qpitch
; /**< RENDER_SURFACE_STATE.QPitch */
1231 * \name Alignment of miptree images, in units of pixels.
1233 * These fields contain the real alignment values, not the values to be
1234 * given to the GPU. For example, if h_align is 4, then program the GPU
1238 uint8_t h_align
; /**< RENDER_SURFACE_STATE.SurfaceHorizontalAlignment */
1239 uint8_t v_align
; /**< RENDER_SURFACE_STATE.SurfaceVerticalAlignment */
1242 uint8_t tile_mode
; /**< RENDER_SURFACE_STATE.TileMode */
1247 const struct anv_format
*format
;
1250 uint32_t array_size
;
1251 VkImageUsageFlags usage
; /**< Superset of VkImageCreateInfo::usage. */
1256 /* Set when bound */
1258 VkDeviceSize offset
;
1260 uint8_t surface_type
; /**< RENDER_SURFACE_STATE.SurfaceType */
1262 bool needs_nonrt_surface_state
:1;
1263 bool needs_color_rt_surface_state
:1;
1268 * For each foo, anv_image::foo_surface is valid if and only if
1269 * anv_image::format has a foo aspect.
1271 * The hardware requires that the depth buffer and stencil buffer be
1272 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1273 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1274 * allocate the depth and stencil buffers as separate surfaces in the same
1278 struct anv_surface color_surface
;
1281 struct anv_surface depth_surface
;
1282 struct anv_surface stencil_surface
;
1287 struct anv_buffer_view
{
1288 struct anv_state surface_state
; /**< RENDER_SURFACE_STATE */
1290 uint32_t offset
; /**< Offset into bo. */
1291 uint32_t range
; /**< VkBufferViewCreateInfo::range */
1292 const struct anv_format
*format
; /**< VkBufferViewCreateInfo::format */
1295 struct anv_image_view
{
1296 const struct anv_image
*image
; /**< VkImageViewCreateInfo::image */
1297 const struct anv_format
*format
; /**< VkImageViewCreateInfo::format */
1299 uint32_t offset
; /**< Offset into bo. */
1300 VkExtent3D extent
; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1302 /** RENDER_SURFACE_STATE when using image as a color render target. */
1303 struct anv_state color_rt_surface_state
;
1305 /** RENDER_SURFACE_STATE when using image as a non render target. */
1306 struct anv_state nonrt_surface_state
;
1309 struct anv_image_create_info
{
1310 const VkImageCreateInfo
*vk_info
;
1311 bool force_tile_mode
;
1316 VkResult
anv_image_create(VkDevice _device
,
1317 const struct anv_image_create_info
*info
,
1320 struct anv_surface
*
1321 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
,
1322 VkImageAspectFlags aspect_mask
);
1324 void anv_image_view_init(struct anv_image_view
*view
,
1325 struct anv_device
*device
,
1326 const VkImageViewCreateInfo
* pCreateInfo
,
1327 struct anv_cmd_buffer
*cmd_buffer
);
1330 gen7_image_view_init(struct anv_image_view
*iview
,
1331 struct anv_device
*device
,
1332 const VkImageViewCreateInfo
* pCreateInfo
,
1333 struct anv_cmd_buffer
*cmd_buffer
);
1336 gen8_image_view_init(struct anv_image_view
*iview
,
1337 struct anv_device
*device
,
1338 const VkImageViewCreateInfo
* pCreateInfo
,
1339 struct anv_cmd_buffer
*cmd_buffer
);
1341 VkResult
anv_buffer_view_create(struct anv_device
*device
,
1342 const VkBufferViewCreateInfo
*pCreateInfo
,
1343 struct anv_buffer_view
**bview_out
);
1345 void anv_fill_buffer_surface_state(struct anv_device
*device
, void *state
,
1346 const struct anv_format
*format
,
1347 uint32_t offset
, uint32_t range
);
1349 void gen7_fill_buffer_surface_state(void *state
, const struct anv_format
*format
,
1350 uint32_t offset
, uint32_t range
);
1351 void gen8_fill_buffer_surface_state(void *state
, const struct anv_format
*format
,
1352 uint32_t offset
, uint32_t range
);
1354 struct anv_sampler
{
1358 struct anv_framebuffer
{
1363 uint32_t attachment_count
;
1364 const struct anv_image_view
* attachments
[0];
1367 struct anv_subpass
{
1368 uint32_t input_count
;
1369 uint32_t * input_attachments
;
1370 uint32_t color_count
;
1371 uint32_t * color_attachments
;
1372 uint32_t * resolve_attachments
;
1373 uint32_t depth_stencil_attachment
;
1376 struct anv_render_pass_attachment
{
1377 const struct anv_format
*format
;
1379 VkAttachmentLoadOp load_op
;
1380 VkAttachmentLoadOp stencil_load_op
;
1383 struct anv_render_pass
{
1384 uint32_t attachment_count
;
1385 uint32_t subpass_count
;
1387 uint32_t num_color_clear_attachments
;
1388 bool has_depth_clear_attachment
;
1389 bool has_stencil_clear_attachment
;
1391 struct anv_render_pass_attachment
* attachments
;
1392 struct anv_subpass subpasses
[0];
1395 struct anv_query_pool_slot
{
1401 struct anv_query_pool
{
1407 void anv_device_init_meta(struct anv_device
*device
);
1408 void anv_device_finish_meta(struct anv_device
*device
);
1410 void *anv_lookup_entrypoint(const char *name
);
1412 void anv_dump_image_to_ppm(struct anv_device
*device
,
1413 struct anv_image
*image
, unsigned miplevel
,
1414 unsigned array_layer
, const char *filename
);
1416 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
1418 static inline struct __anv_type * \
1419 __anv_type ## _from_handle(__VkType _handle) \
1421 return (struct __anv_type *) _handle; \
1424 static inline __VkType \
1425 __anv_type ## _to_handle(struct __anv_type *_obj) \
1427 return (__VkType) _obj; \
1430 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
1432 static inline struct __anv_type * \
1433 __anv_type ## _from_handle(__VkType _handle) \
1435 return (struct __anv_type *) _handle.handle; \
1438 static inline __VkType \
1439 __anv_type ## _to_handle(struct __anv_type *_obj) \
1441 return (__VkType) { .handle = (uint64_t) _obj }; \
1444 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
1445 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
1447 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer
, VkCmdBuffer
)
1448 ANV_DEFINE_HANDLE_CASTS(anv_device
, VkDevice
)
1449 ANV_DEFINE_HANDLE_CASTS(anv_instance
, VkInstance
)
1450 ANV_DEFINE_HANDLE_CASTS(anv_physical_device
, VkPhysicalDevice
)
1451 ANV_DEFINE_HANDLE_CASTS(anv_queue
, VkQueue
)
1453 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool
, VkCmdPool
)
1454 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer
, VkBuffer
)
1455 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view
, VkBufferView
);
1456 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set
, VkDescriptorSet
)
1457 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout
, VkDescriptorSetLayout
)
1458 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory
, VkDeviceMemory
)
1459 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence
, VkFence
)
1460 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer
, VkFramebuffer
)
1461 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image
, VkImage
)
1462 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view
, VkImageView
);
1463 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline
, VkPipeline
)
1464 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout
, VkPipelineLayout
)
1465 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool
, VkQueryPool
)
1466 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass
, VkRenderPass
)
1467 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler
, VkSampler
)
1468 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader
, VkShader
)
1469 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module
, VkShaderModule
)
1471 #define ANV_DEFINE_STRUCT_CASTS(__anv_type, __VkType) \
1473 static inline const __VkType * \
1474 __anv_type ## _to_ ## __VkType(const struct __anv_type *__anv_obj) \
1476 return (const __VkType *) __anv_obj; \
1479 #define ANV_COMMON_TO_STRUCT(__VkType, __vk_name, __common_name) \
1480 const __VkType *__vk_name = anv_common_to_ ## __VkType(__common_name)
1482 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkMemoryBarrier
)
1483 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkBufferMemoryBarrier
)
1484 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkImageMemoryBarrier
)