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
38 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
43 #include "brw_device_info.h"
44 #include "brw_compiler.h"
45 #include "util/macros.h"
46 #include "util/list.h"
48 /* Pre-declarations needed for WSI entrypoints */
51 typedef struct xcb_connection_t xcb_connection_t
;
52 typedef uint32_t xcb_visualid_t
;
53 typedef uint32_t xcb_window_t
;
55 #define VK_USE_PLATFORM_XCB_KHR
56 #define VK_USE_PLATFORM_WAYLAND_KHR
59 #include <vulkan/vulkan.h>
60 #include <vulkan/vulkan_intel.h>
61 #include <vulkan/vk_icd.h>
63 #include "anv_entrypoints.h"
64 #include "brw_context.h"
74 #define MAX_VIEWPORTS 16
75 #define MAX_SCISSORS 16
76 #define MAX_PUSH_CONSTANTS_SIZE 128
77 #define MAX_DYNAMIC_BUFFERS 16
79 #define MAX_SAMPLES_LOG2 4 /* SKL supports 16 samples */
81 #define anv_noreturn __attribute__((__noreturn__))
82 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
84 #define MIN(a, b) ((a) < (b) ? (a) : (b))
85 #define MAX(a, b) ((a) > (b) ? (a) : (b))
87 static inline uint32_t
88 align_u32(uint32_t v
, uint32_t a
)
90 assert(a
!= 0 && a
== (a
& -a
));
91 return (v
+ a
- 1) & ~(a
- 1);
94 static inline uint64_t
95 align_u64(uint64_t v
, uint64_t a
)
97 assert(a
!= 0 && a
== (a
& -a
));
98 return (v
+ a
- 1) & ~(a
- 1);
101 static inline int32_t
102 align_i32(int32_t v
, int32_t a
)
104 assert(a
!= 0 && a
== (a
& -a
));
105 return (v
+ a
- 1) & ~(a
- 1);
108 /** Alignment must be a power of 2. */
110 anv_is_aligned(uintmax_t n
, uintmax_t a
)
112 assert(a
== (a
& -a
));
113 return (n
& (a
- 1)) == 0;
116 static inline uint32_t
117 anv_minify(uint32_t n
, uint32_t levels
)
119 if (unlikely(n
== 0))
122 return MAX(n
>> levels
, 1);
126 anv_clamp_f(float f
, float min
, float max
)
139 anv_clear_mask(uint32_t *inout_mask
, uint32_t clear_mask
)
141 if (*inout_mask
& clear_mask
) {
142 *inout_mask
&= ~clear_mask
;
149 #define for_each_bit(b, dword) \
150 for (uint32_t __dword = (dword); \
151 (b) = __builtin_ffs(__dword) - 1, __dword; \
152 __dword &= ~(1 << (b)))
154 #define typed_memcpy(dest, src, count) ({ \
155 static_assert(sizeof(*src) == sizeof(*dest), ""); \
156 memcpy((dest), (src), (count) * sizeof(*(src))); \
159 #define zero(x) (memset(&(x), 0, sizeof(x)))
161 /* Define no kernel as 1, since that's an illegal offset for a kernel */
165 VkStructureType sType
;
169 /* Whenever we generate an error, pass it through this function. Useful for
170 * debugging, where we can break on it. Only call at error site, not when
171 * propagating errors. Might be useful to plug in a stack trace here.
174 VkResult
__vk_errorf(VkResult error
, const char *file
, int line
, const char *format
, ...);
177 #define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
178 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
180 #define vk_error(error) error
181 #define vk_errorf(error, format, ...) error
184 void __anv_finishme(const char *file
, int line
, const char *format
, ...)
185 anv_printflike(3, 4);
186 void anv_loge(const char *format
, ...) anv_printflike(1, 2);
187 void anv_loge_v(const char *format
, va_list va
);
190 * Print a FINISHME message, including its source location.
192 #define anv_finishme(format, ...) \
193 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);
195 /* A non-fatal assert. Useful for debugging. */
197 #define anv_assert(x) ({ \
198 if (unlikely(!(x))) \
199 fprintf(stderr, "%s:%d ASSERT: %s\n", __FILE__, __LINE__, #x); \
202 #define anv_assert(x)
206 * If a block of code is annotated with anv_validate, then the block runs only
210 #define anv_validate if (1)
212 #define anv_validate if (0)
215 void anv_abortf(const char *format
, ...) anv_noreturn
anv_printflike(1, 2);
216 void anv_abortfv(const char *format
, va_list va
) anv_noreturn
;
218 #define stub_return(v) \
220 anv_finishme("stub %s", __func__); \
226 anv_finishme("stub %s", __func__); \
231 * A dynamically growable, circular buffer. Elements are added at head and
232 * removed from tail. head and tail are free-running uint32_t indices and we
233 * only compute the modulo with size when accessing the array. This way,
234 * number of bytes in the queue is always head - tail, even in case of
241 uint32_t element_size
;
246 int anv_vector_init(struct anv_vector
*queue
, uint32_t element_size
, uint32_t size
);
247 void *anv_vector_add(struct anv_vector
*queue
);
248 void *anv_vector_remove(struct anv_vector
*queue
);
251 anv_vector_length(struct anv_vector
*queue
)
253 return (queue
->head
- queue
->tail
) / queue
->element_size
;
257 anv_vector_head(struct anv_vector
*vector
)
259 assert(vector
->tail
< vector
->head
);
260 return (void *)((char *)vector
->data
+
261 ((vector
->head
- vector
->element_size
) &
262 (vector
->size
- 1)));
266 anv_vector_tail(struct anv_vector
*vector
)
268 return (void *)((char *)vector
->data
+ (vector
->tail
& (vector
->size
- 1)));
272 anv_vector_finish(struct anv_vector
*queue
)
277 #define anv_vector_foreach(elem, queue) \
278 static_assert(__builtin_types_compatible_p(__typeof__(queue), struct anv_vector *), ""); \
279 for (uint32_t __anv_vector_offset = (queue)->tail; \
280 elem = (queue)->data + (__anv_vector_offset & ((queue)->size - 1)), __anv_vector_offset < (queue)->head; \
281 __anv_vector_offset += (queue)->element_size)
286 /* Index into the current validation list. This is used by the
287 * validation list building alrogithm to track which buffers are already
288 * in the validation list so that we can ensure uniqueness.
292 /* Last known offset. This value is provided by the kernel when we
293 * execbuf and is used as the presumed offset for the next bunch of
301 /* We need to set the WRITE flag on winsys bos so GEM will know we're
302 * writing to them and synchronize uses on other rings (eg if the display
303 * server uses the blitter ring).
308 /* Represents a lock-free linked list of "free" things. This is used by
309 * both the block pool and the state pools. Unfortunately, in order to
310 * solve the ABA problem, we can't use a single uint32_t head.
312 union anv_free_list
{
316 /* A simple count that is incremented every time the head changes. */
322 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
324 struct anv_block_state
{
334 struct anv_block_pool
{
335 struct anv_device
*device
;
339 /* The offset from the start of the bo to the "center" of the block
340 * pool. Pointers to allocated blocks are given by
341 * bo.map + center_bo_offset + offsets.
343 uint32_t center_bo_offset
;
345 /* Current memory map of the block pool. This pointer may or may not
346 * point to the actual beginning of the block pool memory. If
347 * anv_block_pool_alloc_back has ever been called, then this pointer
348 * will point to the "center" position of the buffer and all offsets
349 * (negative or positive) given out by the block pool alloc functions
350 * will be valid relative to this pointer.
352 * In particular, map == bo.map + center_offset
358 * Array of mmaps and gem handles owned by the block pool, reclaimed when
359 * the block pool is destroyed.
361 struct anv_vector mmap_cleanups
;
365 union anv_free_list free_list
;
366 struct anv_block_state state
;
368 union anv_free_list back_free_list
;
369 struct anv_block_state back_state
;
372 /* Block pools are backed by a fixed-size 2GB memfd */
373 #define BLOCK_POOL_MEMFD_SIZE (1ull << 32)
375 /* The center of the block pool is also the middle of the memfd. This may
376 * change in the future if we decide differently for some reason.
378 #define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
380 static inline uint32_t
381 anv_block_pool_size(struct anv_block_pool
*pool
)
383 return pool
->state
.end
+ pool
->back_state
.end
;
392 struct anv_fixed_size_state_pool
{
394 union anv_free_list free_list
;
395 struct anv_block_state block
;
398 #define ANV_MIN_STATE_SIZE_LOG2 6
399 #define ANV_MAX_STATE_SIZE_LOG2 10
401 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2)
403 struct anv_state_pool
{
404 struct anv_block_pool
*block_pool
;
405 struct anv_fixed_size_state_pool buckets
[ANV_STATE_BUCKETS
];
408 struct anv_state_stream_block
;
410 struct anv_state_stream
{
411 struct anv_block_pool
*block_pool
;
413 /* The current working block */
414 struct anv_state_stream_block
*block
;
416 /* Offset at which the current block starts */
418 /* Offset at which to allocate the next state */
420 /* Offset at which the current block ends */
424 #define CACHELINE_SIZE 64
425 #define CACHELINE_MASK 63
428 anv_clflush_range(void *start
, size_t size
)
430 void *p
= (void *) (((uintptr_t) start
) & ~CACHELINE_MASK
);
431 void *end
= start
+ size
;
433 __builtin_ia32_mfence();
435 __builtin_ia32_clflush(p
);
441 anv_state_clflush(struct anv_state state
)
443 anv_clflush_range(state
.map
, state
.alloc_size
);
446 void anv_block_pool_init(struct anv_block_pool
*pool
,
447 struct anv_device
*device
, uint32_t block_size
);
448 void anv_block_pool_finish(struct anv_block_pool
*pool
);
449 int32_t anv_block_pool_alloc(struct anv_block_pool
*pool
);
450 int32_t anv_block_pool_alloc_back(struct anv_block_pool
*pool
);
451 void anv_block_pool_free(struct anv_block_pool
*pool
, int32_t offset
);
452 void anv_state_pool_init(struct anv_state_pool
*pool
,
453 struct anv_block_pool
*block_pool
);
454 void anv_state_pool_finish(struct anv_state_pool
*pool
);
455 struct anv_state
anv_state_pool_alloc(struct anv_state_pool
*pool
,
456 size_t state_size
, size_t alignment
);
457 void anv_state_pool_free(struct anv_state_pool
*pool
, struct anv_state state
);
458 void anv_state_stream_init(struct anv_state_stream
*stream
,
459 struct anv_block_pool
*block_pool
);
460 void anv_state_stream_finish(struct anv_state_stream
*stream
);
461 struct anv_state
anv_state_stream_alloc(struct anv_state_stream
*stream
,
462 uint32_t size
, uint32_t alignment
);
465 * Implements a pool of re-usable BOs. The interface is identical to that
466 * of block_pool except that each block is its own BO.
469 struct anv_device
*device
;
474 void anv_bo_pool_init(struct anv_bo_pool
*pool
, struct anv_device
*device
);
475 void anv_bo_pool_finish(struct anv_bo_pool
*pool
);
476 VkResult
anv_bo_pool_alloc(struct anv_bo_pool
*pool
, struct anv_bo
*bo
,
478 void anv_bo_pool_free(struct anv_bo_pool
*pool
, const struct anv_bo
*bo
);
481 void *anv_resolve_entrypoint(uint32_t index
);
483 extern struct anv_dispatch_table dtable
;
485 #define ANV_CALL(func) ({ \
486 if (dtable.func == NULL) { \
487 size_t idx = offsetof(struct anv_dispatch_table, func) / sizeof(void *); \
488 dtable.entrypoints[idx] = anv_resolve_entrypoint(idx); \
494 anv_alloc(const VkAllocationCallbacks
*alloc
,
495 size_t size
, size_t align
,
496 VkSystemAllocationScope scope
)
498 return alloc
->pfnAllocation(alloc
->pUserData
, size
, align
, scope
);
502 anv_realloc(const VkAllocationCallbacks
*alloc
,
503 void *ptr
, size_t size
, size_t align
,
504 VkSystemAllocationScope scope
)
506 return alloc
->pfnReallocation(alloc
->pUserData
, ptr
, size
, align
, scope
);
510 anv_free(const VkAllocationCallbacks
*alloc
, void *data
)
512 alloc
->pfnFree(alloc
->pUserData
, data
);
516 anv_alloc2(const VkAllocationCallbacks
*parent_alloc
,
517 const VkAllocationCallbacks
*alloc
,
518 size_t size
, size_t align
,
519 VkSystemAllocationScope scope
)
522 return anv_alloc(alloc
, size
, align
, scope
);
524 return anv_alloc(parent_alloc
, size
, align
, scope
);
528 anv_free2(const VkAllocationCallbacks
*parent_alloc
,
529 const VkAllocationCallbacks
*alloc
,
533 anv_free(alloc
, data
);
535 anv_free(parent_alloc
, data
);
538 struct anv_physical_device
{
539 VK_LOADER_DATA _loader_data
;
541 struct anv_instance
* instance
;
545 const struct brw_device_info
* info
;
546 uint64_t aperture_size
;
547 struct brw_compiler
* compiler
;
548 struct isl_device isl_dev
;
549 int cmd_parser_version
;
552 struct anv_wsi_interaface
;
554 #define VK_ICD_WSI_PLATFORM_MAX 5
556 struct anv_instance
{
557 VK_LOADER_DATA _loader_data
;
559 VkAllocationCallbacks alloc
;
562 int physicalDeviceCount
;
563 struct anv_physical_device physicalDevice
;
565 struct anv_wsi_interface
* wsi
[VK_ICD_WSI_PLATFORM_MAX
];
568 VkResult
anv_init_wsi(struct anv_instance
*instance
);
569 void anv_finish_wsi(struct anv_instance
*instance
);
571 struct anv_meta_state
{
572 VkAllocationCallbacks alloc
;
575 * Use array element `i` for images with `2^i` samples.
579 * Pipeline N is used to clear color attachment N of the current
582 * HACK: We use one pipeline per color attachment to work around the
583 * compiler's inability to dynamically set the render target index of
584 * the render target write message.
586 struct anv_pipeline
*color_pipelines
[MAX_RTS
];
588 struct anv_pipeline
*depth_only_pipeline
;
589 struct anv_pipeline
*stencil_only_pipeline
;
590 struct anv_pipeline
*depthstencil_pipeline
;
591 } clear
[1 + MAX_SAMPLES_LOG2
];
594 VkRenderPass render_pass
;
596 /** Pipeline that blits from a 1D image. */
597 VkPipeline pipeline_1d_src
;
599 /** Pipeline that blits from a 2D image. */
600 VkPipeline pipeline_2d_src
;
602 /** Pipeline that blits from a 3D image. */
603 VkPipeline pipeline_3d_src
;
605 VkPipelineLayout pipeline_layout
;
606 VkDescriptorSetLayout ds_layout
;
610 VkRenderPass render_pass
;
612 /** Pipeline that copies from a 2D image. */
613 VkPipeline pipeline_2d_src
;
615 VkPipelineLayout pipeline_layout
;
616 VkDescriptorSetLayout ds_layout
;
620 /** Pipeline [i] resolves an image with 2^(i+1) samples. */
621 VkPipeline pipelines
[MAX_SAMPLES_LOG2
];
624 VkPipelineLayout pipeline_layout
;
625 VkDescriptorSetLayout ds_layout
;
630 VK_LOADER_DATA _loader_data
;
632 struct anv_device
* device
;
634 struct anv_state_pool
* pool
;
637 struct anv_pipeline_cache
{
638 struct anv_device
* device
;
639 struct anv_state_stream program_stream
;
640 pthread_mutex_t mutex
;
644 uint32_t kernel_count
;
645 uint32_t * hash_table
;
648 struct anv_pipeline_bind_map
;
650 void anv_pipeline_cache_init(struct anv_pipeline_cache
*cache
,
651 struct anv_device
*device
);
652 void anv_pipeline_cache_finish(struct anv_pipeline_cache
*cache
);
653 uint32_t anv_pipeline_cache_search(struct anv_pipeline_cache
*cache
,
654 const unsigned char *sha1
,
655 const struct brw_stage_prog_data
**prog_data
,
656 struct anv_pipeline_bind_map
*map
);
657 uint32_t anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache
*cache
,
658 const unsigned char *sha1
,
661 const struct brw_stage_prog_data
**prog_data
,
662 size_t prog_data_size
,
663 struct anv_pipeline_bind_map
*map
);
666 VK_LOADER_DATA _loader_data
;
668 VkAllocationCallbacks alloc
;
670 struct anv_instance
* instance
;
672 struct brw_device_info info
;
673 struct isl_device isl_dev
;
676 bool can_chain_batches
;
678 struct anv_bo_pool batch_bo_pool
;
680 struct anv_block_pool dynamic_state_block_pool
;
681 struct anv_state_pool dynamic_state_pool
;
683 struct anv_block_pool instruction_block_pool
;
684 struct anv_pipeline_cache default_pipeline_cache
;
686 struct anv_block_pool surface_state_block_pool
;
687 struct anv_state_pool surface_state_pool
;
689 struct anv_bo workaround_bo
;
691 struct anv_meta_state meta_state
;
693 struct anv_state border_colors
;
695 struct anv_queue queue
;
697 struct anv_block_pool scratch_block_pool
;
699 uint32_t default_mocs
;
701 pthread_mutex_t mutex
;
704 void anv_device_get_cache_uuid(void *uuid
);
707 void* anv_gem_mmap(struct anv_device
*device
,
708 uint32_t gem_handle
, uint64_t offset
, uint64_t size
, uint32_t flags
);
709 void anv_gem_munmap(void *p
, uint64_t size
);
710 uint32_t anv_gem_create(struct anv_device
*device
, size_t size
);
711 void anv_gem_close(struct anv_device
*device
, uint32_t gem_handle
);
712 uint32_t anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
);
713 int anv_gem_wait(struct anv_device
*device
, uint32_t gem_handle
, int64_t *timeout_ns
);
714 int anv_gem_execbuffer(struct anv_device
*device
,
715 struct drm_i915_gem_execbuffer2
*execbuf
);
716 int anv_gem_set_tiling(struct anv_device
*device
, uint32_t gem_handle
,
717 uint32_t stride
, uint32_t tiling
);
718 int anv_gem_create_context(struct anv_device
*device
);
719 int anv_gem_destroy_context(struct anv_device
*device
, int context
);
720 int anv_gem_get_param(int fd
, uint32_t param
);
721 bool anv_gem_get_bit6_swizzle(int fd
, uint32_t tiling
);
722 int anv_gem_get_aperture(int fd
, uint64_t *size
);
723 int anv_gem_handle_to_fd(struct anv_device
*device
, uint32_t gem_handle
);
724 uint32_t anv_gem_fd_to_handle(struct anv_device
*device
, int fd
);
725 int anv_gem_set_caching(struct anv_device
*device
, uint32_t gem_handle
, uint32_t caching
);
726 int anv_gem_set_domain(struct anv_device
*device
, uint32_t gem_handle
,
727 uint32_t read_domains
, uint32_t write_domain
);
729 VkResult
anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
);
731 struct anv_reloc_list
{
734 struct drm_i915_gem_relocation_entry
* relocs
;
735 struct anv_bo
** reloc_bos
;
738 VkResult
anv_reloc_list_init(struct anv_reloc_list
*list
,
739 const VkAllocationCallbacks
*alloc
);
740 void anv_reloc_list_finish(struct anv_reloc_list
*list
,
741 const VkAllocationCallbacks
*alloc
);
743 uint64_t anv_reloc_list_add(struct anv_reloc_list
*list
,
744 const VkAllocationCallbacks
*alloc
,
745 uint32_t offset
, struct anv_bo
*target_bo
,
748 struct anv_batch_bo
{
749 /* Link in the anv_cmd_buffer.owned_batch_bos list */
750 struct list_head link
;
754 /* Bytes actually consumed in this batch BO */
757 /* Last seen surface state block pool bo offset */
758 uint32_t last_ss_pool_bo_offset
;
760 struct anv_reloc_list relocs
;
764 const VkAllocationCallbacks
* alloc
;
770 struct anv_reloc_list
* relocs
;
772 /* This callback is called (with the associated user data) in the event
773 * that the batch runs out of space.
775 VkResult (*extend_cb
)(struct anv_batch
*, void *);
779 void *anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
);
780 void anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
);
781 uint64_t anv_batch_emit_reloc(struct anv_batch
*batch
,
782 void *location
, struct anv_bo
*bo
, uint32_t offset
);
783 VkResult
anv_device_submit_simple_batch(struct anv_device
*device
,
784 struct anv_batch
*batch
);
791 #define __gen_address_type struct anv_address
792 #define __gen_user_data struct anv_batch
794 static inline uint64_t
795 __gen_combine_address(struct anv_batch
*batch
, void *location
,
796 const struct anv_address address
, uint32_t delta
)
798 if (address
.bo
== NULL
) {
799 return address
.offset
+ delta
;
801 assert(batch
->start
<= location
&& location
< batch
->end
);
803 return anv_batch_emit_reloc(batch
, location
, address
.bo
, address
.offset
+ delta
);
807 /* Wrapper macros needed to work around preprocessor argument issues. In
808 * particular, arguments don't get pre-evaluated if they are concatenated.
809 * This means that, if you pass GENX(3DSTATE_PS) into the emit macro, the
810 * GENX macro won't get evaluated if the emit macro contains "cmd ## foo".
811 * We can work around this easily enough with these helpers.
813 #define __anv_cmd_length(cmd) cmd ## _length
814 #define __anv_cmd_length_bias(cmd) cmd ## _length_bias
815 #define __anv_cmd_header(cmd) cmd ## _header
816 #define __anv_cmd_pack(cmd) cmd ## _pack
817 #define __anv_reg_num(reg) reg ## _num
819 #define anv_pack_struct(dst, struc, ...) do { \
820 struct struc __template = { \
823 __anv_cmd_pack(struc)(NULL, dst, &__template); \
824 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dst, __anv_cmd_length(struc) * 4)); \
827 #define anv_batch_emit(batch, cmd, ...) do { \
828 void *__dst = anv_batch_emit_dwords(batch, __anv_cmd_length(cmd)); \
829 struct cmd __template = { \
830 __anv_cmd_header(cmd), \
833 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
834 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, __anv_cmd_length(cmd) * 4)); \
837 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
838 void *__dst = anv_batch_emit_dwords(batch, n); \
839 struct cmd __template = { \
840 __anv_cmd_header(cmd), \
841 .DWordLength = n - __anv_cmd_length_bias(cmd), \
844 __anv_cmd_pack(cmd)(batch, __dst, &__template); \
848 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
852 static_assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1), "mismatch merge"); \
853 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
854 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
855 dw[i] = (dwords0)[i] | (dwords1)[i]; \
856 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
859 #define anv_state_pool_emit(pool, cmd, align, ...) ({ \
860 const uint32_t __size = __anv_cmd_length(cmd) * 4; \
861 struct anv_state __state = \
862 anv_state_pool_alloc((pool), __size, align); \
863 struct cmd __template = { \
866 __anv_cmd_pack(cmd)(NULL, __state.map, &__template); \
867 VG(VALGRIND_CHECK_MEM_IS_DEFINED(__state.map, __anv_cmd_length(cmd) * 4)); \
868 if (!(pool)->block_pool->device->info.has_llc) \
869 anv_state_clflush(__state); \
873 #define GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) { \
874 .GraphicsDataTypeGFDT = 0, \
875 .LLCCacheabilityControlLLCCC = 0, \
876 .L3CacheabilityControlL3CC = 1, \
879 #define GEN75_MOCS (struct GEN75_MEMORY_OBJECT_CONTROL_STATE) { \
880 .LLCeLLCCacheabilityControlLLCCC = 0, \
881 .L3CacheabilityControlL3CC = 1, \
884 #define GEN8_MOCS (struct GEN8_MEMORY_OBJECT_CONTROL_STATE) { \
885 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
886 .TargetCache = L3DefertoPATforLLCeLLCselection, \
890 /* Skylake: MOCS is now an index into an array of 62 different caching
891 * configurations programmed by the kernel.
894 #define GEN9_MOCS (struct GEN9_MEMORY_OBJECT_CONTROL_STATE) { \
895 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
896 .IndextoMOCSTables = 2 \
899 #define GEN9_MOCS_PTE { \
900 /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */ \
901 .IndextoMOCSTables = 1 \
904 struct anv_device_memory
{
907 VkDeviceSize map_size
;
912 * Header for Vertex URB Entry (VUE)
914 struct anv_vue_header
{
916 uint32_t RTAIndex
; /* RenderTargetArrayIndex */
917 uint32_t ViewportIndex
;
921 struct anv_descriptor_set_binding_layout
{
922 /* Number of array elements in this binding */
925 /* Index into the flattend descriptor set */
926 uint16_t descriptor_index
;
928 /* Index into the dynamic state array for a dynamic buffer */
929 int16_t dynamic_offset_index
;
931 /* Index into the descriptor set buffer views */
932 int16_t buffer_index
;
935 /* Index into the binding table for the associated surface */
936 int16_t surface_index
;
938 /* Index into the sampler table for the associated sampler */
939 int16_t sampler_index
;
941 /* Index into the image table for the associated image */
943 } stage
[MESA_SHADER_STAGES
];
945 /* Immutable samplers (or NULL if no immutable samplers) */
946 struct anv_sampler
**immutable_samplers
;
949 struct anv_descriptor_set_layout
{
950 /* Number of bindings in this descriptor set */
951 uint16_t binding_count
;
953 /* Total size of the descriptor set with room for all array entries */
956 /* Shader stages affected by this descriptor set */
957 uint16_t shader_stages
;
959 /* Number of buffers in this descriptor set */
960 uint16_t buffer_count
;
962 /* Number of dynamic offsets used by this descriptor set */
963 uint16_t dynamic_offset_count
;
965 /* Bindings in this descriptor set */
966 struct anv_descriptor_set_binding_layout binding
[0];
969 struct anv_descriptor
{
970 VkDescriptorType type
;
974 struct anv_image_view
*image_view
;
975 struct anv_sampler
*sampler
;
978 struct anv_buffer_view
*buffer_view
;
982 struct anv_descriptor_set
{
983 const struct anv_descriptor_set_layout
*layout
;
985 uint32_t buffer_count
;
986 struct anv_buffer_view
*buffer_views
;
987 struct anv_descriptor descriptors
[0];
990 struct anv_descriptor_pool
{
995 struct anv_state_stream surface_state_stream
;
996 void *surface_state_free_list
;
1002 anv_descriptor_set_create(struct anv_device
*device
,
1003 struct anv_descriptor_pool
*pool
,
1004 const struct anv_descriptor_set_layout
*layout
,
1005 struct anv_descriptor_set
**out_set
);
1008 anv_descriptor_set_destroy(struct anv_device
*device
,
1009 struct anv_descriptor_pool
*pool
,
1010 struct anv_descriptor_set
*set
);
1012 #define ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS UINT16_MAX
1014 struct anv_pipeline_binding
{
1015 /* The descriptor set this surface corresponds to. The special value of
1016 * ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS indicates that the offset refers
1017 * to a color attachment and not a regular descriptor.
1021 /* Offset into the descriptor set or attachment list. */
1025 struct anv_pipeline_layout
{
1027 struct anv_descriptor_set_layout
*layout
;
1028 uint32_t dynamic_offset_start
;
1034 bool has_dynamic_offsets
;
1035 } stage
[MESA_SHADER_STAGES
];
1039 struct anv_device
* device
;
1042 VkBufferUsageFlags usage
;
1044 /* Set when bound */
1046 VkDeviceSize offset
;
1049 enum anv_cmd_dirty_bits
{
1050 ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
= 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
1051 ANV_CMD_DIRTY_DYNAMIC_SCISSOR
= 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
1052 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
= 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
1053 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
= 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
1054 ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
= 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
1055 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
= 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
1056 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
= 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
1057 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
= 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
1058 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
= 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
1059 ANV_CMD_DIRTY_DYNAMIC_ALL
= (1 << 9) - 1,
1060 ANV_CMD_DIRTY_PIPELINE
= 1 << 9,
1061 ANV_CMD_DIRTY_INDEX_BUFFER
= 1 << 10,
1062 ANV_CMD_DIRTY_RENDER_TARGETS
= 1 << 11,
1064 typedef uint32_t anv_cmd_dirty_mask_t
;
1066 struct anv_vertex_binding
{
1067 struct anv_buffer
* buffer
;
1068 VkDeviceSize offset
;
1071 struct anv_push_constants
{
1072 /* Current allocated size of this push constants data structure.
1073 * Because a decent chunk of it may not be used (images on SKL, for
1074 * instance), we won't actually allocate the entire structure up-front.
1078 /* Push constant data provided by the client through vkPushConstants */
1079 uint8_t client_data
[MAX_PUSH_CONSTANTS_SIZE
];
1081 /* Our hardware only provides zero-based vertex and instance id so, in
1082 * order to satisfy the vulkan requirements, we may have to push one or
1083 * both of these into the shader.
1085 uint32_t base_vertex
;
1086 uint32_t base_instance
;
1088 /* Offsets and ranges for dynamically bound buffers */
1092 } dynamic
[MAX_DYNAMIC_BUFFERS
];
1094 /* Image data for image_load_store on pre-SKL */
1095 struct brw_image_param images
[MAX_IMAGES
];
1098 struct anv_dynamic_state
{
1101 VkViewport viewports
[MAX_VIEWPORTS
];
1106 VkRect2D scissors
[MAX_SCISSORS
];
1117 float blend_constants
[4];
1127 } stencil_compare_mask
;
1132 } stencil_write_mask
;
1137 } stencil_reference
;
1140 extern const struct anv_dynamic_state default_dynamic_state
;
1142 void anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
1143 const struct anv_dynamic_state
*src
,
1144 uint32_t copy_mask
);
1147 * Attachment state when recording a renderpass instance.
1149 * The clear value is valid only if there exists a pending clear.
1151 struct anv_attachment_state
{
1152 VkImageAspectFlags pending_clear_aspects
;
1153 VkClearValue clear_value
;
1156 /** State required while building cmd buffer */
1157 struct anv_cmd_state
{
1158 /* PIPELINE_SELECT.PipelineSelection */
1159 uint32_t current_pipeline
;
1160 uint32_t current_l3_config
;
1162 anv_cmd_dirty_mask_t dirty
;
1163 anv_cmd_dirty_mask_t compute_dirty
;
1164 uint32_t num_workgroups_offset
;
1165 struct anv_bo
*num_workgroups_bo
;
1166 VkShaderStageFlags descriptors_dirty
;
1167 VkShaderStageFlags push_constants_dirty
;
1168 uint32_t scratch_size
;
1169 struct anv_pipeline
* pipeline
;
1170 struct anv_pipeline
* compute_pipeline
;
1171 struct anv_framebuffer
* framebuffer
;
1172 struct anv_render_pass
* pass
;
1173 struct anv_subpass
* subpass
;
1174 uint32_t restart_index
;
1175 struct anv_vertex_binding vertex_bindings
[MAX_VBS
];
1176 struct anv_descriptor_set
* descriptors
[MAX_SETS
];
1177 struct anv_push_constants
* push_constants
[MESA_SHADER_STAGES
];
1178 struct anv_state binding_tables
[MESA_SHADER_STAGES
];
1179 struct anv_state samplers
[MESA_SHADER_STAGES
];
1180 struct anv_dynamic_state dynamic
;
1184 * Array length is anv_cmd_state::pass::attachment_count. Array content is
1185 * valid only when recording a render pass instance.
1187 struct anv_attachment_state
* attachments
;
1190 struct anv_buffer
* index_buffer
;
1191 uint32_t index_type
; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
1192 uint32_t index_offset
;
1196 struct anv_cmd_pool
{
1197 VkAllocationCallbacks alloc
;
1198 struct list_head cmd_buffers
;
1201 #define ANV_CMD_BUFFER_BATCH_SIZE 8192
1203 enum anv_cmd_buffer_exec_mode
{
1204 ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
,
1205 ANV_CMD_BUFFER_EXEC_MODE_EMIT
,
1206 ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
,
1207 ANV_CMD_BUFFER_EXEC_MODE_CHAIN
,
1208 ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
,
1211 struct anv_cmd_buffer
{
1212 VK_LOADER_DATA _loader_data
;
1214 struct anv_device
* device
;
1216 struct anv_cmd_pool
* pool
;
1217 struct list_head pool_link
;
1219 struct anv_batch batch
;
1221 /* Fields required for the actual chain of anv_batch_bo's.
1223 * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
1225 struct list_head batch_bos
;
1226 enum anv_cmd_buffer_exec_mode exec_mode
;
1228 /* A vector of anv_batch_bo pointers for every batch or surface buffer
1229 * referenced by this command buffer
1231 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1233 struct anv_vector seen_bbos
;
1235 /* A vector of int32_t's for every block of binding tables.
1237 * initialized by anv_cmd_buffer_init_batch_bo_chain()
1239 struct anv_vector bt_blocks
;
1241 struct anv_reloc_list surface_relocs
;
1243 /* Information needed for execbuf
1245 * These fields are generated by anv_cmd_buffer_prepare_execbuf().
1248 struct drm_i915_gem_execbuffer2 execbuf
;
1250 struct drm_i915_gem_exec_object2
* objects
;
1252 struct anv_bo
** bos
;
1254 /* Allocated length of the 'objects' and 'bos' arrays */
1255 uint32_t array_length
;
1260 /* Serial for tracking buffer completion */
1263 /* Stream objects for storing temporary data */
1264 struct anv_state_stream surface_state_stream
;
1265 struct anv_state_stream dynamic_state_stream
;
1267 VkCommandBufferUsageFlags usage_flags
;
1268 VkCommandBufferLevel level
;
1270 struct anv_cmd_state state
;
1273 VkResult
anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
1274 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
1275 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
);
1276 void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
);
1277 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
1278 struct anv_cmd_buffer
*secondary
);
1279 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer
*cmd_buffer
);
1281 VkResult
anv_cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1282 unsigned stage
, struct anv_state
*bt_state
);
1283 VkResult
anv_cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
1284 unsigned stage
, struct anv_state
*state
);
1285 uint32_t gen7_cmd_buffer_flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
);
1286 void gen7_cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
1289 struct anv_state
anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
1290 const void *data
, uint32_t size
, uint32_t alignment
);
1291 struct anv_state
anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
1292 uint32_t *a
, uint32_t *b
,
1293 uint32_t dwords
, uint32_t alignment
);
1296 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1298 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1299 uint32_t entries
, uint32_t *state_offset
);
1301 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
);
1303 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
1304 uint32_t size
, uint32_t alignment
);
1307 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
);
1309 void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer
*cmd_buffer
);
1310 void gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer
*cmd_buffer
);
1312 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
);
1314 void anv_cmd_state_setup_attachments(struct anv_cmd_buffer
*cmd_buffer
,
1315 const VkRenderPassBeginInfo
*info
);
1317 void anv_cmd_buffer_set_subpass(struct anv_cmd_buffer
*cmd_buffer
,
1318 struct anv_subpass
*subpass
);
1321 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
1322 gl_shader_stage stage
);
1324 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
);
1326 void anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer
*cmd_buffer
);
1327 void anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer
*cmd_buffer
);
1329 const struct anv_image_view
*
1330 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
);
1332 void anv_cmd_buffer_dump(struct anv_cmd_buffer
*cmd_buffer
);
1336 struct drm_i915_gem_execbuffer2 execbuf
;
1337 struct drm_i915_gem_exec_object2 exec2_objects
[1];
1343 struct anv_state state
;
1348 struct anv_shader_module
{
1349 struct nir_shader
* nir
;
1351 unsigned char sha1
[20];
1356 void anv_hash_shader(unsigned char *hash
, const void *key
, size_t key_size
,
1357 struct anv_shader_module
*module
,
1358 const char *entrypoint
,
1359 const VkSpecializationInfo
*spec_info
);
1361 static inline gl_shader_stage
1362 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage
)
1364 assert(__builtin_popcount(vk_stage
) == 1);
1365 return ffs(vk_stage
) - 1;
1368 static inline VkShaderStageFlagBits
1369 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage
)
1371 return (1 << mesa_stage
);
1374 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
1376 #define anv_foreach_stage(stage, stage_bits) \
1377 for (gl_shader_stage stage, \
1378 __tmp = (gl_shader_stage)((stage_bits) & ANV_STAGE_MASK); \
1379 stage = __builtin_ffs(__tmp) - 1, __tmp; \
1380 __tmp &= ~(1 << (stage)))
1382 struct anv_pipeline_bind_map
{
1383 uint32_t surface_count
;
1384 uint32_t sampler_count
;
1385 uint32_t image_count
;
1386 uint32_t attachment_count
;
1388 struct anv_pipeline_binding
* surface_to_descriptor
;
1389 struct anv_pipeline_binding
* sampler_to_descriptor
;
1390 uint32_t * surface_to_attachment
;
1393 struct anv_pipeline
{
1394 struct anv_device
* device
;
1395 struct anv_batch batch
;
1396 uint32_t batch_data
[512];
1397 struct anv_reloc_list batch_relocs
;
1398 uint32_t dynamic_state_mask
;
1399 struct anv_dynamic_state dynamic_state
;
1401 struct anv_pipeline_layout
* layout
;
1402 struct anv_pipeline_bind_map bindings
[MESA_SHADER_STAGES
];
1406 const struct brw_stage_prog_data
* prog_data
[MESA_SHADER_STAGES
];
1407 uint32_t scratch_start
[MESA_SHADER_STAGES
];
1408 uint32_t total_scratch
;
1410 uint8_t push_size
[MESA_SHADER_FRAGMENT
+ 1];
1411 uint32_t start
[MESA_SHADER_GEOMETRY
+ 1];
1412 uint32_t size
[MESA_SHADER_GEOMETRY
+ 1];
1413 uint32_t entries
[MESA_SHADER_GEOMETRY
+ 1];
1416 VkShaderStageFlags active_stages
;
1417 struct anv_state blend_state
;
1424 uint32_t ps_grf_start0
;
1425 uint32_t ps_grf_start2
;
1430 uint32_t binding_stride
[MAX_VBS
];
1431 bool instancing_enable
[MAX_VBS
];
1432 bool primitive_restart
;
1435 uint32_t cs_thread_width_max
;
1436 uint32_t cs_right_mask
;
1440 uint32_t depth_stencil_state
[3];
1446 uint32_t wm_depth_stencil
[3];
1450 uint32_t wm_depth_stencil
[4];
1454 static inline const struct brw_vs_prog_data
*
1455 get_vs_prog_data(struct anv_pipeline
*pipeline
)
1457 return (const struct brw_vs_prog_data
*) pipeline
->prog_data
[MESA_SHADER_VERTEX
];
1460 static inline const struct brw_gs_prog_data
*
1461 get_gs_prog_data(struct anv_pipeline
*pipeline
)
1463 return (const struct brw_gs_prog_data
*) pipeline
->prog_data
[MESA_SHADER_GEOMETRY
];
1466 static inline const struct brw_wm_prog_data
*
1467 get_wm_prog_data(struct anv_pipeline
*pipeline
)
1469 return (const struct brw_wm_prog_data
*) pipeline
->prog_data
[MESA_SHADER_FRAGMENT
];
1472 static inline const struct brw_cs_prog_data
*
1473 get_cs_prog_data(struct anv_pipeline
*pipeline
)
1475 return (const struct brw_cs_prog_data
*) pipeline
->prog_data
[MESA_SHADER_COMPUTE
];
1478 struct anv_graphics_pipeline_create_info
{
1480 * If non-negative, overrides the color attachment count of the pipeline's
1483 int8_t color_attachment_count
;
1486 bool disable_viewport
;
1487 bool disable_scissor
;
1493 anv_pipeline_init(struct anv_pipeline
*pipeline
, struct anv_device
*device
,
1494 struct anv_pipeline_cache
*cache
,
1495 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1496 const struct anv_graphics_pipeline_create_info
*extra
,
1497 const VkAllocationCallbacks
*alloc
);
1500 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
1501 struct anv_pipeline_cache
*cache
,
1502 const VkComputePipelineCreateInfo
*info
,
1503 struct anv_shader_module
*module
,
1504 const char *entrypoint
,
1505 const VkSpecializationInfo
*spec_info
);
1508 anv_graphics_pipeline_create(VkDevice device
,
1509 VkPipelineCache cache
,
1510 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1511 const struct anv_graphics_pipeline_create_info
*extra
,
1512 const VkAllocationCallbacks
*alloc
,
1513 VkPipeline
*pPipeline
);
1515 struct anv_format_swizzle
{
1523 const VkFormat vk_format
;
1525 enum isl_format isl_format
; /**< RENDER_SURFACE_STATE.SurfaceFormat */
1526 const struct isl_format_layout
*isl_layout
;
1527 struct anv_format_swizzle swizzle
;
1532 const struct anv_format
*
1533 anv_format_for_vk_format(VkFormat format
);
1536 anv_get_isl_format(VkFormat format
, VkImageAspectFlags aspect
,
1537 VkImageTiling tiling
, struct anv_format_swizzle
*swizzle
);
1540 anv_format_is_color(const struct anv_format
*format
)
1542 return !format
->has_depth
&& !format
->has_stencil
;
1546 anv_format_is_depth_or_stencil(const struct anv_format
*format
)
1548 return format
->has_depth
|| format
->has_stencil
;
1552 * Subsurface of an anv_image.
1554 struct anv_surface
{
1555 struct isl_surf isl
;
1558 * Offset from VkImage's base address, as bound by vkBindImageMemory().
1565 /* The original VkFormat provided by the client. This may not match any
1566 * of the actual surface formats.
1569 const struct anv_format
*format
;
1572 uint32_t array_size
;
1573 uint32_t samples
; /**< VkImageCreateInfo::samples */
1574 VkImageUsageFlags usage
; /**< Superset of VkImageCreateInfo::usage. */
1575 VkImageTiling tiling
; /** VkImageCreateInfo::tiling */
1580 /* Set when bound */
1582 VkDeviceSize offset
;
1587 * For each foo, anv_image::foo_surface is valid if and only if
1588 * anv_image::format has a foo aspect.
1590 * The hardware requires that the depth buffer and stencil buffer be
1591 * separate surfaces. From Vulkan's perspective, though, depth and stencil
1592 * reside in the same VkImage. To satisfy both the hardware and Vulkan, we
1593 * allocate the depth and stencil buffers as separate surfaces in the same
1597 struct anv_surface color_surface
;
1600 struct anv_surface depth_surface
;
1601 struct anv_surface stencil_surface
;
1606 static inline uint32_t
1607 anv_get_layerCount(const struct anv_image
*image
,
1608 const VkImageSubresourceRange
*range
)
1610 return range
->layerCount
== VK_REMAINING_ARRAY_LAYERS
?
1611 image
->array_size
- range
->baseArrayLayer
: range
->layerCount
;
1614 static inline uint32_t
1615 anv_get_levelCount(const struct anv_image
*image
,
1616 const VkImageSubresourceRange
*range
)
1618 return range
->levelCount
== VK_REMAINING_MIP_LEVELS
?
1619 image
->levels
- range
->baseMipLevel
: range
->levelCount
;
1623 struct anv_image_view
{
1624 const struct anv_image
*image
; /**< VkImageViewCreateInfo::image */
1626 uint32_t offset
; /**< Offset into bo. */
1628 VkImageAspectFlags aspect_mask
;
1630 uint32_t base_layer
;
1632 VkExtent3D extent
; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
1634 /** RENDER_SURFACE_STATE when using image as a color render target. */
1635 struct anv_state color_rt_surface_state
;
1637 /** RENDER_SURFACE_STATE when using image as a sampler surface. */
1638 struct anv_state sampler_surface_state
;
1640 /** RENDER_SURFACE_STATE when using image as a storage image. */
1641 struct anv_state storage_surface_state
;
1643 struct brw_image_param storage_image_param
;
1646 struct anv_image_create_info
{
1647 const VkImageCreateInfo
*vk_info
;
1648 isl_tiling_flags_t isl_tiling_flags
;
1652 VkResult
anv_image_create(VkDevice _device
,
1653 const struct anv_image_create_info
*info
,
1654 const VkAllocationCallbacks
* alloc
,
1657 struct anv_surface
*
1658 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
,
1659 VkImageAspectFlags aspect_mask
);
1661 void anv_image_view_init(struct anv_image_view
*view
,
1662 struct anv_device
*device
,
1663 const VkImageViewCreateInfo
* pCreateInfo
,
1664 struct anv_cmd_buffer
*cmd_buffer
,
1666 VkImageUsageFlags usage_mask
);
1668 struct anv_buffer_view
{
1669 enum isl_format format
; /**< VkBufferViewCreateInfo::format */
1671 uint32_t offset
; /**< Offset into bo. */
1672 uint64_t range
; /**< VkBufferViewCreateInfo::range */
1674 struct anv_state surface_state
;
1675 struct anv_state storage_surface_state
;
1677 struct brw_image_param storage_image_param
;
1680 const struct anv_format
*
1681 anv_format_for_descriptor_type(VkDescriptorType type
);
1683 static inline struct VkExtent3D
1684 anv_sanitize_image_extent(const VkImageType imageType
,
1685 const struct VkExtent3D imageExtent
)
1687 switch (imageType
) {
1688 case VK_IMAGE_TYPE_1D
:
1689 return (VkExtent3D
) { imageExtent
.width
, 1, 1 };
1690 case VK_IMAGE_TYPE_2D
:
1691 return (VkExtent3D
) { imageExtent
.width
, imageExtent
.height
, 1 };
1692 case VK_IMAGE_TYPE_3D
:
1695 unreachable("invalid image type");
1699 static inline struct VkOffset3D
1700 anv_sanitize_image_offset(const VkImageType imageType
,
1701 const struct VkOffset3D imageOffset
)
1703 switch (imageType
) {
1704 case VK_IMAGE_TYPE_1D
:
1705 return (VkOffset3D
) { imageOffset
.x
, 0, 0 };
1706 case VK_IMAGE_TYPE_2D
:
1707 return (VkOffset3D
) { imageOffset
.x
, imageOffset
.y
, 0 };
1708 case VK_IMAGE_TYPE_3D
:
1711 unreachable("invalid image type");
1716 void anv_fill_buffer_surface_state(struct anv_device
*device
,
1717 struct anv_state state
,
1718 enum isl_format format
,
1719 uint32_t offset
, uint32_t range
,
1722 void anv_image_view_fill_image_param(struct anv_device
*device
,
1723 struct anv_image_view
*view
,
1724 struct brw_image_param
*param
);
1725 void anv_buffer_view_fill_image_param(struct anv_device
*device
,
1726 struct anv_buffer_view
*view
,
1727 struct brw_image_param
*param
);
1729 struct anv_sampler
{
1733 struct anv_framebuffer
{
1738 uint32_t attachment_count
;
1739 struct anv_image_view
* attachments
[0];
1742 struct anv_subpass
{
1743 uint32_t input_count
;
1744 uint32_t * input_attachments
;
1745 uint32_t color_count
;
1746 uint32_t * color_attachments
;
1747 uint32_t * resolve_attachments
;
1748 uint32_t depth_stencil_attachment
;
1750 /** Subpass has at least one resolve attachment */
1754 struct anv_render_pass_attachment
{
1755 const struct anv_format
*format
;
1757 VkAttachmentLoadOp load_op
;
1758 VkAttachmentLoadOp stencil_load_op
;
1761 struct anv_render_pass
{
1762 uint32_t attachment_count
;
1763 uint32_t subpass_count
;
1764 uint32_t * subpass_attachments
;
1765 struct anv_render_pass_attachment
* attachments
;
1766 struct anv_subpass subpasses
[0];
1769 extern struct anv_render_pass anv_meta_dummy_renderpass
;
1771 struct anv_query_pool_slot
{
1777 struct anv_query_pool
{
1783 VkResult
anv_device_init_meta(struct anv_device
*device
);
1784 void anv_device_finish_meta(struct anv_device
*device
);
1786 void *anv_lookup_entrypoint(const char *name
);
1788 void anv_dump_image_to_ppm(struct anv_device
*device
,
1789 struct anv_image
*image
, unsigned miplevel
,
1790 unsigned array_layer
, const char *filename
);
1792 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType) \
1794 static inline struct __anv_type * \
1795 __anv_type ## _from_handle(__VkType _handle) \
1797 return (struct __anv_type *) _handle; \
1800 static inline __VkType \
1801 __anv_type ## _to_handle(struct __anv_type *_obj) \
1803 return (__VkType) _obj; \
1806 #define ANV_DEFINE_NONDISP_HANDLE_CASTS(__anv_type, __VkType) \
1808 static inline struct __anv_type * \
1809 __anv_type ## _from_handle(__VkType _handle) \
1811 return (struct __anv_type *)(uintptr_t) _handle; \
1814 static inline __VkType \
1815 __anv_type ## _to_handle(struct __anv_type *_obj) \
1817 return (__VkType)(uintptr_t) _obj; \
1820 #define ANV_FROM_HANDLE(__anv_type, __name, __handle) \
1821 struct __anv_type *__name = __anv_type ## _from_handle(__handle)
1823 ANV_DEFINE_HANDLE_CASTS(anv_cmd_buffer
, VkCommandBuffer
)
1824 ANV_DEFINE_HANDLE_CASTS(anv_device
, VkDevice
)
1825 ANV_DEFINE_HANDLE_CASTS(anv_instance
, VkInstance
)
1826 ANV_DEFINE_HANDLE_CASTS(anv_physical_device
, VkPhysicalDevice
)
1827 ANV_DEFINE_HANDLE_CASTS(anv_queue
, VkQueue
)
1829 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool
, VkCommandPool
)
1830 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer
, VkBuffer
)
1831 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view
, VkBufferView
)
1832 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_pool
, VkDescriptorPool
)
1833 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set
, VkDescriptorSet
)
1834 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout
, VkDescriptorSetLayout
)
1835 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory
, VkDeviceMemory
)
1836 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence
, VkFence
)
1837 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_event
, VkEvent
)
1838 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer
, VkFramebuffer
)
1839 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image
, VkImage
)
1840 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image_view
, VkImageView
);
1841 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_cache
, VkPipelineCache
)
1842 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline
, VkPipeline
)
1843 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_pipeline_layout
, VkPipelineLayout
)
1844 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_query_pool
, VkQueryPool
)
1845 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_render_pass
, VkRenderPass
)
1846 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_sampler
, VkSampler
)
1847 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_shader_module
, VkShaderModule
)
1849 #define ANV_DEFINE_STRUCT_CASTS(__anv_type, __VkType) \
1851 static inline const __VkType * \
1852 __anv_type ## _to_ ## __VkType(const struct __anv_type *__anv_obj) \
1854 return (const __VkType *) __anv_obj; \
1857 #define ANV_COMMON_TO_STRUCT(__VkType, __vk_name, __common_name) \
1858 const __VkType *__vk_name = anv_common_to_ ## __VkType(__common_name)
1860 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkMemoryBarrier
)
1861 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkBufferMemoryBarrier
)
1862 ANV_DEFINE_STRUCT_CASTS(anv_common
, VkImageMemoryBarrier
)
1864 /* Gen-specific function declarations */
1866 # include "anv_genX.h"
1868 # define genX(x) gen7_##x
1869 # include "anv_genX.h"
1871 # define genX(x) gen75_##x
1872 # include "anv_genX.h"
1874 # define genX(x) gen8_##x
1875 # include "anv_genX.h"
1877 # define genX(x) gen9_##x
1878 # include "anv_genX.h"