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
33 #include "brw_device_info.h"
34 #include "util/macros.h"
37 #include <vulkan/vulkan.h>
38 #include <vulkan/vulkan_intel.h>
39 #include <vulkan/vk_wsi_lunarg.h>
41 #include "entrypoints.h"
43 #include "brw_context.h"
49 static inline uint32_t
50 ALIGN_U32(uint32_t v
, uint32_t a
)
52 return (v
+ a
- 1) & ~(a
- 1);
56 ALIGN_I32(int32_t v
, int32_t a
)
58 return (v
+ a
- 1) & ~(a
- 1);
61 #define for_each_bit(b, dword) \
62 for (uint32_t __dword = (dword); \
63 (b) = __builtin_ffs(__dword) - 1, __dword; \
64 __dword &= ~(1 << (b)))
66 /* Define no kernel as 1, since that's an illegal offset for a kernel */
70 VkStructureType sType
;
74 /* Whenever we generate an error, pass it through this function. Useful for
75 * debugging, where we can break on it. Only call at error site, not when
76 * propagating errors. Might be useful to plug in a stack trace here.
79 static inline VkResult
80 vk_error(VkResult error
)
83 fprintf(stderr
, "vk_error: %x\n", error
);
89 void __anv_finishme(const char *file
, int line
, const char *format
, ...);
92 * Print a FINISHME message, including its source location.
94 #define anv_finishme(format, ...) \
95 __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);
97 #define stub_return(v) \
99 anv_finishme("stub %s", __func__); \
105 anv_finishme("stub %s", __func__); \
110 * A dynamically growable, circular buffer. Elements are added at head and
111 * removed from tail. head and tail are free-running uint32_t indices and we
112 * only compute the modulo with size when accessing the array. This way,
113 * number of bytes in the queue is always head - tail, even in case of
120 uint32_t element_size
;
125 int anv_vector_init(struct anv_vector
*queue
, uint32_t element_size
, uint32_t size
);
126 void *anv_vector_add(struct anv_vector
*queue
);
127 void *anv_vector_remove(struct anv_vector
*queue
);
130 anv_vector_length(struct anv_vector
*queue
)
132 return (queue
->head
- queue
->tail
) / queue
->element_size
;
136 anv_vector_finish(struct anv_vector
*queue
)
141 #define anv_vector_foreach(elem, queue) \
142 static_assert(__builtin_types_compatible_p(__typeof__(queue), struct anv_vector *), ""); \
143 for (uint32_t __anv_vector_offset = (queue)->tail; \
144 elem = (queue)->data + (__anv_vector_offset & ((queue)->size - 1)), __anv_vector_offset < (queue)->head; \
145 __anv_vector_offset += (queue)->element_size)
153 /* This field is here for the benefit of the aub dumper. It can (and for
154 * userptr bos it must) be set to the cpu map of the buffer. Destroying
155 * the bo won't clean up the mmap, it's still the responsibility of the bo
156 * user to do that. */
160 /* Represents a lock-free linked list of "free" things. This is used by
161 * both the block pool and the state pools. Unfortunately, in order to
162 * solve the ABA problem, we can't use a single uint32_t head.
164 union anv_free_list
{
168 /* A simple count that is incremented every time the head changes. */
174 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
176 struct anv_block_pool
{
177 struct anv_device
*device
;
185 * Array of mmaps and gem handles owned by the block pool, reclaimed when
186 * the block pool is destroyed.
188 struct anv_vector mmap_cleanups
;
193 union anv_free_list free_list
;
196 struct anv_block_state
{
212 struct anv_fixed_size_state_pool
{
214 union anv_free_list free_list
;
215 struct anv_block_state block
;
218 #define ANV_MIN_STATE_SIZE_LOG2 6
219 #define ANV_MAX_STATE_SIZE_LOG2 10
221 #define ANV_STATE_BUCKETS (ANV_MAX_STATE_SIZE_LOG2 - ANV_MIN_STATE_SIZE_LOG2)
223 struct anv_state_pool
{
224 struct anv_block_pool
*block_pool
;
225 struct anv_fixed_size_state_pool buckets
[ANV_STATE_BUCKETS
];
228 struct anv_state_stream
{
229 struct anv_block_pool
*block_pool
;
231 uint32_t current_block
;
235 void anv_block_pool_init(struct anv_block_pool
*pool
,
236 struct anv_device
*device
, uint32_t block_size
);
237 void anv_block_pool_init_slave(struct anv_block_pool
*pool
,
238 struct anv_block_pool
*master_pool
,
239 uint32_t num_blocks
);
240 void anv_block_pool_finish(struct anv_block_pool
*pool
);
241 uint32_t anv_block_pool_alloc(struct anv_block_pool
*pool
);
242 void anv_block_pool_free(struct anv_block_pool
*pool
, uint32_t offset
);
243 void anv_state_pool_init(struct anv_state_pool
*pool
,
244 struct anv_block_pool
*block_pool
);
245 struct anv_state
anv_state_pool_alloc(struct anv_state_pool
*pool
,
246 size_t state_size
, size_t alignment
);
247 void anv_state_pool_free(struct anv_state_pool
*pool
, struct anv_state state
);
248 void anv_state_stream_init(struct anv_state_stream
*stream
,
249 struct anv_block_pool
*block_pool
);
250 void anv_state_stream_finish(struct anv_state_stream
*stream
);
251 struct anv_state
anv_state_stream_alloc(struct anv_state_stream
*stream
,
252 uint32_t size
, uint32_t alignment
);
255 * Implements a pool of re-usable BOs. The interface is identical to that
256 * of block_pool except that each block is its own BO.
259 struct anv_device
*device
;
266 void anv_bo_pool_init(struct anv_bo_pool
*pool
,
267 struct anv_device
*device
, uint32_t block_size
);
268 void anv_bo_pool_finish(struct anv_bo_pool
*pool
);
269 VkResult
anv_bo_pool_alloc(struct anv_bo_pool
*pool
, struct anv_bo
*bo
);
270 void anv_bo_pool_free(struct anv_bo_pool
*pool
, const struct anv_bo
*bo
);
275 typedef void (*anv_object_destructor_cb
)(struct anv_device
*,
280 anv_object_destructor_cb destructor
;
283 struct anv_physical_device
{
284 struct anv_instance
* instance
;
289 const struct brw_device_info
* info
;
292 struct anv_instance
{
293 void * pAllocUserData
;
294 PFN_vkAllocFunction pfnAlloc
;
295 PFN_vkFreeFunction pfnFree
;
297 uint32_t physicalDeviceCount
;
298 struct anv_physical_device physicalDevice
;
301 struct anv_meta_state
{
308 VkDescriptorSetLayout ds_layout
;
312 VkDynamicRsState rs_state
;
313 VkDynamicCbState cb_state
;
314 VkDynamicDsState ds_state
;
319 struct anv_instance
* instance
;
321 struct brw_device_info info
;
327 struct anv_bo_pool batch_bo_pool
;
329 struct anv_block_pool dynamic_state_block_pool
;
330 struct anv_state_pool dynamic_state_pool
;
332 struct anv_block_pool instruction_block_pool
;
333 struct anv_block_pool surface_state_block_pool
;
334 struct anv_block_pool binding_table_block_pool
;
335 struct anv_state_pool surface_state_pool
;
337 struct anv_meta_state meta_state
;
339 struct anv_state float_border_colors
;
340 struct anv_state uint32_border_colors
;
342 struct anv_compiler
* compiler
;
343 struct anv_aub_writer
* aub_writer
;
344 pthread_mutex_t mutex
;
348 struct anv_device
* device
;
350 struct anv_state_pool
* pool
;
353 * Serial number of the most recently completed batch executed on the
356 struct anv_state completed_serial
;
359 * The next batch submitted to the engine will be assigned this serial
362 uint32_t next_serial
;
364 uint32_t last_collected_serial
;
368 anv_device_alloc(struct anv_device
* device
,
371 VkSystemAllocType allocType
);
374 anv_device_free(struct anv_device
* device
,
377 void* anv_gem_mmap(struct anv_device
*device
,
378 uint32_t gem_handle
, uint64_t offset
, uint64_t size
);
379 void anv_gem_munmap(void *p
, uint64_t size
);
380 uint32_t anv_gem_create(struct anv_device
*device
, size_t size
);
381 void anv_gem_close(struct anv_device
*device
, int gem_handle
);
382 int anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
);
383 int anv_gem_wait(struct anv_device
*device
, int gem_handle
, int64_t *timeout_ns
);
384 int anv_gem_execbuffer(struct anv_device
*device
,
385 struct drm_i915_gem_execbuffer2
*execbuf
);
386 int anv_gem_set_tiling(struct anv_device
*device
, int gem_handle
,
387 uint32_t stride
, uint32_t tiling
);
388 int anv_gem_create_context(struct anv_device
*device
);
389 int anv_gem_destroy_context(struct anv_device
*device
, int context
);
390 int anv_gem_get_param(int fd
, uint32_t param
);
391 int anv_gem_get_aperture(struct anv_device
*device
, uint64_t *size
);
392 int anv_gem_handle_to_fd(struct anv_device
*device
, int gem_handle
);
393 int anv_gem_fd_to_handle(struct anv_device
*device
, int fd
);
394 int anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
);
396 VkResult
anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
);
398 struct anv_reloc_list
{
401 struct drm_i915_gem_relocation_entry
* relocs
;
402 struct anv_bo
** reloc_bos
;
405 VkResult
anv_reloc_list_init(struct anv_reloc_list
*list
,
406 struct anv_device
*device
);
407 void anv_reloc_list_finish(struct anv_reloc_list
*list
,
408 struct anv_device
*device
);
410 struct anv_batch_bo
{
413 /* Bytes actually consumed in this batch BO */
416 /* These offsets reference the per-batch reloc list */
420 struct anv_batch_bo
* prev_batch_bo
;
424 struct anv_device
* device
;
430 struct anv_reloc_list relocs
;
432 /* This callback is called (with the associated user data) in the event
433 * that the batch runs out of space.
435 VkResult (*extend_cb
)(struct anv_batch
*, void *);
439 void *anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
);
440 void anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
);
441 uint64_t anv_batch_emit_reloc(struct anv_batch
*batch
,
442 void *location
, struct anv_bo
*bo
, uint32_t offset
);
449 #define __gen_address_type struct anv_address
450 #define __gen_user_data struct anv_batch
452 static inline uint64_t
453 __gen_combine_address(struct anv_batch
*batch
, void *location
,
454 const struct anv_address address
, uint32_t delta
)
456 if (address
.bo
== NULL
) {
459 assert(batch
->start
<= location
&& location
< batch
->end
);
461 return anv_batch_emit_reloc(batch
, location
, address
.bo
, address
.offset
+ delta
);
465 #include "gen7_pack.h"
466 #include "gen75_pack.h"
467 #undef GEN8_3DSTATE_MULTISAMPLE
468 #include "gen8_pack.h"
470 #define anv_batch_emit(batch, cmd, ...) do { \
471 struct cmd __template = { \
475 void *__dst = anv_batch_emit_dwords(batch, cmd ## _length); \
476 cmd ## _pack(batch, __dst, &__template); \
479 #define anv_batch_emitn(batch, n, cmd, ...) ({ \
480 struct cmd __template = { \
482 .DwordLength = n - cmd ## _length_bias, \
485 void *__dst = anv_batch_emit_dwords(batch, n); \
486 cmd ## _pack(batch, __dst, &__template); \
490 #define anv_batch_emit_merge(batch, dwords0, dwords1) \
494 assert(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
495 dw = anv_batch_emit_dwords((batch), ARRAY_SIZE(dwords0)); \
496 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
497 dw[i] = (dwords0)[i] | (dwords1)[i]; \
500 #define GEN8_MOCS { \
501 .MemoryTypeLLCeLLCCacheabilityControl = WB, \
502 .TargetCache = L3DefertoPATforLLCeLLCselection, \
506 struct anv_device_memory
{
508 VkDeviceSize map_size
;
512 struct anv_dynamic_vp_state
{
513 struct anv_object base
;
514 struct anv_state sf_clip_vp
;
515 struct anv_state cc_vp
;
516 struct anv_state scissor
;
519 struct anv_dynamic_rs_state
{
520 uint32_t state_sf
[GEN8_3DSTATE_SF_length
];
521 uint32_t state_raster
[GEN8_3DSTATE_RASTER_length
];
524 struct anv_dynamic_ds_state
{
525 uint32_t state_wm_depth_stencil
[GEN8_3DSTATE_WM_DEPTH_STENCIL_length
];
526 uint32_t state_color_calc
[GEN8_COLOR_CALC_STATE_length
];
529 struct anv_dynamic_cb_state
{
530 uint32_t state_color_calc
[GEN8_COLOR_CALC_STATE_length
];
534 struct anv_descriptor_slot
{
539 struct anv_descriptor_set_layout
{
541 uint32_t surface_count
;
542 struct anv_descriptor_slot
*surface_start
;
543 uint32_t sampler_count
;
544 struct anv_descriptor_slot
*sampler_start
;
545 } stage
[VK_NUM_SHADER_STAGE
];
548 uint32_t num_dynamic_buffers
;
549 uint32_t shader_stages
;
550 struct anv_descriptor_slot entries
[0];
553 struct anv_descriptor
{
554 struct anv_sampler
*sampler
;
555 struct anv_surface_view
*view
;
558 struct anv_descriptor_set
{
559 struct anv_descriptor descriptors
[0];
566 struct anv_pipeline_layout
{
568 struct anv_descriptor_set_layout
*layout
;
569 uint32_t surface_start
[VK_NUM_SHADER_STAGE
];
570 uint32_t sampler_start
[VK_NUM_SHADER_STAGE
];
576 uint32_t surface_count
;
577 uint32_t sampler_count
;
578 } stage
[VK_NUM_SHADER_STAGE
];
582 struct anv_device
* device
;
590 #define ANV_CMD_BUFFER_PIPELINE_DIRTY (1 << 0)
591 #define ANV_CMD_BUFFER_RS_DIRTY (1 << 2)
592 #define ANV_CMD_BUFFER_DS_DIRTY (1 << 3)
593 #define ANV_CMD_BUFFER_CB_DIRTY (1 << 4)
595 struct anv_vertex_binding
{
596 struct anv_buffer
* buffer
;
600 struct anv_descriptor_set_binding
{
601 struct anv_descriptor_set
* set
;
602 uint32_t dynamic_offsets
[128];
605 struct anv_cmd_buffer
{
606 struct anv_object base
;
607 struct anv_device
* device
;
609 struct drm_i915_gem_execbuffer2 execbuf
;
610 struct drm_i915_gem_exec_object2
* exec2_objects
;
611 struct anv_bo
** exec2_bos
;
612 uint32_t exec2_array_length
;
617 struct anv_batch batch
;
618 struct anv_batch_bo
* last_batch_bo
;
619 struct anv_batch_bo
* surface_batch_bo
;
620 uint32_t surface_next
;
621 struct anv_reloc_list surface_relocs
;
622 struct anv_state_stream binding_table_state_stream
;
623 struct anv_state_stream surface_state_stream
;
624 struct anv_state_stream dynamic_state_stream
;
626 /* State required while building cmd buffer */
629 uint32_t descriptors_dirty
;
630 struct anv_pipeline
* pipeline
;
631 struct anv_framebuffer
* framebuffer
;
632 struct anv_dynamic_rs_state
* rs_state
;
633 struct anv_dynamic_ds_state
* ds_state
;
634 struct anv_dynamic_vp_state
* vp_state
;
635 struct anv_dynamic_cb_state
* cb_state
;
636 struct anv_vertex_binding vertex_bindings
[MAX_VBS
];
637 struct anv_descriptor_set_binding descriptors
[MAX_SETS
];
640 void anv_cmd_buffer_dump(struct anv_cmd_buffer
*cmd_buffer
);
641 void anv_aub_writer_destroy(struct anv_aub_writer
*writer
);
644 struct anv_object base
;
646 struct drm_i915_gem_execbuffer2 execbuf
;
647 struct drm_i915_gem_exec_object2 exec2_objects
[1];
656 struct anv_pipeline
{
657 struct anv_object base
;
658 struct anv_device
* device
;
659 struct anv_batch batch
;
660 uint32_t batch_data
[256];
661 struct anv_shader
* shaders
[VK_NUM_SHADER_STAGE
];
662 struct anv_pipeline_layout
* layout
;
665 struct brw_vs_prog_data vs_prog_data
;
666 struct brw_wm_prog_data wm_prog_data
;
667 struct brw_gs_prog_data gs_prog_data
;
668 struct brw_stage_prog_data
* prog_data
[VK_NUM_SHADER_STAGE
];
672 uint32_t nr_vs_entries
;
675 uint32_t nr_gs_entries
;
678 struct anv_bo vs_scratch_bo
;
679 struct anv_bo ps_scratch_bo
;
680 struct anv_bo gs_scratch_bo
;
682 uint32_t active_stages
;
683 struct anv_state_stream program_stream
;
684 struct anv_state blend_state
;
689 uint32_t gs_vertex_count
;
692 uint32_t binding_stride
[MAX_VBS
];
694 uint32_t state_sf
[GEN8_3DSTATE_SF_length
];
695 uint32_t state_raster
[GEN8_3DSTATE_RASTER_length
];
696 uint32_t state_wm_depth_stencil
[GEN8_3DSTATE_WM_DEPTH_STENCIL_length
];
699 struct anv_pipeline_create_info
{
701 bool disable_viewport
;
702 bool disable_scissor
;
708 anv_pipeline_create(VkDevice device
,
709 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
710 const struct anv_pipeline_create_info
*extra
,
711 VkPipeline
*pPipeline
);
713 struct anv_compiler
*anv_compiler_create(struct anv_device
*device
);
714 void anv_compiler_destroy(struct anv_compiler
*compiler
);
715 int anv_compiler_run(struct anv_compiler
*compiler
, struct anv_pipeline
*pipeline
);
716 void anv_compiler_free(struct anv_pipeline
*pipeline
);
726 const struct anv_format
*
727 anv_format_for_vk_format(VkFormat format
);
738 uint32_t stencil_offset
;
739 uint32_t stencil_stride
;
745 struct anv_swap_chain
* swap_chain
;
748 struct anv_surface_view
{
749 struct anv_state surface_state
;
757 struct anv_image_create_info
{
761 VkResult
anv_image_create(VkDevice _device
,
762 const VkImageCreateInfo
*pCreateInfo
,
763 const struct anv_image_create_info
*extra
,
766 void anv_image_view_init(struct anv_surface_view
*view
,
767 struct anv_device
*device
,
768 const VkImageViewCreateInfo
* pCreateInfo
,
769 struct anv_cmd_buffer
*cmd_buffer
);
771 void anv_color_attachment_view_init(struct anv_surface_view
*view
,
772 struct anv_device
*device
,
773 const VkColorAttachmentViewCreateInfo
* pCreateInfo
,
774 struct anv_cmd_buffer
*cmd_buffer
);
780 struct anv_depth_stencil_view
{
783 uint32_t depth_offset
;
784 uint32_t depth_stride
;
785 uint32_t depth_format
;
787 uint32_t stencil_offset
;
788 uint32_t stencil_stride
;
791 struct anv_framebuffer
{
792 struct anv_object base
;
793 uint32_t color_attachment_count
;
794 const struct anv_surface_view
* color_attachments
[MAX_RTS
];
795 const struct anv_depth_stencil_view
* depth_stencil
;
797 uint32_t sample_count
;
802 /* Viewport for clears */
803 VkDynamicVpState vp_state
;
806 struct anv_render_pass_layer
{
807 VkAttachmentLoadOp color_load_op
;
808 VkClearColor clear_color
;
811 struct anv_render_pass
{
814 uint32_t num_clear_layers
;
816 struct anv_render_pass_layer layers
[0];
819 void anv_device_init_meta(struct anv_device
*device
);
822 anv_cmd_buffer_clear(struct anv_cmd_buffer
*cmd_buffer
,
823 struct anv_render_pass
*pass
);
826 anv_lookup_entrypoint(const char *name
);