2 * Copyright © 2019 Raspberry Pi
4 * based in part on anv driver which is:
5 * Copyright © 2015 Intel Corporation
7 * based in part on libresoc driver which is:
8 * Copyright © 2016 Red Hat.
9 * Copyright © 2016 Bas Nieuwenhuizen
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #ifndef LIBRESOC_PRIVATE_H
31 #define LIBRESOC_PRIVATE_H
35 #include <vulkan/vulkan.h>
36 #include <vulkan/vk_icd.h>
39 #include "vk_debug_report.h"
40 #include "util/xmlconfig.h"
41 #include "compiler/shader_enums.h"
43 #include "vk_object.h"
44 #include "libresoc_entrypoints.h"
45 #include "libresoc_extensions.h"
46 #include "libresoc_constants.h"
47 #include "libresoc_debug.h"
49 #include "wsi_common.h"
50 #define LIBRESOC_MAX_QUEUE_FAMILIES 1
52 static inline gl_shader_stage
53 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage
)
55 assert(__builtin_popcount(vk_stage
) == 1);
56 return ffs(vk_stage
) - 1;
59 static inline VkShaderStageFlagBits
60 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage
)
62 return (1 << mesa_stage
);
65 static inline uint32_t
66 align_u32(uint32_t v
, uint32_t a
)
68 assert(a
!= 0 && a
== (a
& -a
));
69 return (v
+ a
- 1) & ~(a
- 1);
72 static inline uint32_t
73 align_u32_npot(uint32_t v
, uint32_t a
)
75 return (v
+ a
- 1) / a
* a
;
78 static inline uint64_t
79 align_u64(uint64_t v
, uint64_t a
)
81 assert(a
!= 0 && a
== (a
& -a
));
82 return (v
+ a
- 1) & ~(a
- 1);
86 align_i32(int32_t v
, int32_t a
)
88 assert(a
!= 0 && a
== (a
& -a
));
89 return (v
+ a
- 1) & ~(a
- 1);
92 /** Alignment must be a power of 2. */
94 libresoc_is_aligned(uintmax_t n
, uintmax_t a
)
96 assert(a
== (a
& -a
));
97 return (n
& (a
- 1)) == 0;
100 static inline uint32_t
101 round_up_u32(uint32_t v
, uint32_t a
)
103 return (v
+ a
- 1) / a
;
106 static inline uint64_t
107 round_up_u64(uint64_t v
, uint64_t a
)
109 return (v
+ a
- 1) / a
;
112 static inline uint32_t
113 libresoc_minify(uint32_t n
, uint32_t levels
)
115 if (unlikely(n
== 0))
118 return MAX2(n
>> levels
, 1);
121 libresoc_clamp_f(float f
, float min
, float max
)
134 libresoc_clear_mask(uint32_t *inout_mask
, uint32_t clear_mask
)
136 if (*inout_mask
& clear_mask
) {
137 *inout_mask
&= ~clear_mask
;
144 struct libresoc_fence
{
145 struct vk_object_base base
;
148 struct libresoc_image_create_info
{
149 const VkImageCreateInfo
*vk_info
;
151 bool no_metadata_planes
;
154 struct libresoc_image
{
155 struct vk_object_base base
;
157 /* The original VkFormat provided by the client. This may not match any
158 * of the actual surface formats.
161 VkImageAspectFlags aspects
;
162 VkImageUsageFlags usage
; /**< Superset of VkImageCreateInfo::usage. */
163 VkImageTiling tiling
; /** VkImageCreateInfo::tiling */
164 VkImageCreateFlags flags
; /** VkImageCreateInfo::flags */
169 unsigned queue_family_mask
;
175 VkResult
libresoc_image_create(VkDevice _device
,
176 const struct libresoc_image_create_info
*info
,
177 const VkAllocationCallbacks
* alloc
,
180 struct libresoc_cmd_pool
{
181 struct vk_object_base base
;
182 VkAllocationCallbacks alloc
;
183 struct list_head cmd_buffers
;
184 struct list_head free_cmd_buffers
;
185 uint32_t queue_family_index
;
188 struct libresoc_semaphore
{
189 struct vk_object_base base
;
192 struct libresoc_instance
;
193 struct libresoc_device
;
196 struct libresoc_pipeline_cache
{
197 struct vk_object_base base
;
198 struct libresoc_device
* device
;
199 pthread_mutex_t mutex
;
200 VkPipelineCacheCreateFlags flags
;
204 uint32_t kernel_count
;
205 struct cache_entry
** hash_table
;
208 VkAllocationCallbacks alloc
;
212 struct libresoc_shader_binary
;
213 struct libresoc_shader_variant
;
215 struct libresoc_pipeline
{
216 struct vk_object_base base
;
217 struct libresoc_device
* device
;
220 VkShaderStageFlags active_stages
;
224 libresoc_pipeline_cache_init(struct libresoc_pipeline_cache
*cache
,
225 struct libresoc_device
*device
);
227 libresoc_pipeline_cache_finish(struct libresoc_pipeline_cache
*cache
);
229 libresoc_pipeline_cache_load(struct libresoc_pipeline_cache
*cache
,
230 const void *data
, size_t size
);
233 libresoc_create_shader_variants_from_pipeline_cache(struct libresoc_device
*device
,
234 struct libresoc_pipeline_cache
*cache
,
235 const unsigned char *sha1
,
236 struct libresoc_shader_variant
**variants
,
237 bool *found_in_application_cache
);
240 libresoc_pipeline_cache_insert_shaders(struct libresoc_device
*device
,
241 struct libresoc_pipeline_cache
*cache
,
242 const unsigned char *sha1
,
243 struct libresoc_shader_variant
**variants
,
244 struct libresoc_shader_binary
*const *binaries
);
246 VkResult
libresoc_init_wsi(struct libresoc_physical_device
*physical_device
);
247 void libresoc_finish_wsi(struct libresoc_physical_device
*physical_device
);
249 struct libresoc_device
{
252 VkAllocationCallbacks alloc
;
254 struct libresoc_instance
*instance
;
256 struct libresoc_device_extension_table enabled_extensions
;
257 struct libresoc_device_dispatch_table dispatch
;
259 struct libresoc_queue
*queues
[LIBRESOC_MAX_QUEUE_FAMILIES
];
260 int queue_count
[LIBRESOC_MAX_QUEUE_FAMILIES
];
261 struct radeon_cmdbuf
*empty_cs
[LIBRESOC_MAX_QUEUE_FAMILIES
];
262 struct libresoc_physical_device
*physical_device
;
264 /* Backup in-memory cache to be used if the app doesn't provide one */
265 struct libresoc_pipeline_cache
* mem_cache
;
266 /* Condition variable for legacy timelines, to notify waiters when a
267 * new point gets submitted. */
268 pthread_cond_t timeline_cond
;
269 /* Overallocation. */
270 bool overallocation_disallowed
;
271 uint64_t allocated_memory_size
[VK_MAX_MEMORY_HEAPS
];
272 mtx_t overallocation_mutex
;
277 struct libresoc_physical_device
{
278 VK_LOADER_DATA _loader_data
;
280 struct list_head link
;
281 struct libresoc_instance
*instance
;
282 struct wsi_device wsi_device
;
283 struct libresoc_device_extension_table supported_extensions
;
284 struct libresoc_physical_device_dispatch_table dispatch
;
286 char name
[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE
];
287 uint8_t driver_uuid
[VK_UUID_SIZE
];
288 uint8_t device_uuid
[VK_UUID_SIZE
];
289 uint8_t cache_uuid
[VK_UUID_SIZE
];
292 VkPhysicalDeviceMemoryProperties memory_properties
;
296 struct libresoc_app_info
{
297 const char *app_name
;
298 uint32_t app_version
;
299 const char *engine_name
;
300 uint32_t engine_version
;
301 uint32_t api_version
;
304 struct libresoc_instance
{
305 struct vk_object_base base
;
307 VkAllocationCallbacks alloc
;
311 uint64_t debug_flags
;
313 uint32_t engineVersion
;
314 struct libresoc_app_info app_info
;
316 bool physical_devices_enumerated
;
317 struct libresoc_instance_extension_table enabled_extensions
;
318 struct libresoc_instance_dispatch_table dispatch
;
319 struct libresoc_device_dispatch_table device_dispatch
;
320 struct libresoc_physical_device_dispatch_table physical_device_dispatch
;
321 int physical_device_count
;
322 struct list_head physical_devices
;
324 struct driOptionCache dri_options
;
325 struct driOptionCache available_dri_options
;
326 struct vk_debug_report_instance debug_report_callbacks
;
329 struct libresoc_deferred_queue_submission
;
330 struct libresoc_queue
{
331 VK_LOADER_DATA _loader_data
;
333 struct libresoc_device
*device
;
335 uint32_t queue_family_index
;
337 VkDeviceQueueCreateFlags flags
;
339 struct list_head pending_submissions
;
340 pthread_mutex_t pending_mutex
;
341 pthread_mutex_t thread_mutex
;
342 pthread_cond_t thread_cond
;
343 //struct libresoc_deferred_queue_submission *thread_submission;
344 pthread_t submission_thread
;
350 struct libresoc_cmd_buffer_upload
{
354 struct list_head list
;
357 enum libresoc_cmd_buffer_status
{
358 LIBRESOC_CMD_BUFFER_STATUS_INVALID
,
359 LIBRESOC_CMD_BUFFER_STATUS_INITIAL
,
360 LIBRESOC_CMD_BUFFER_STATUS_RECORDING
,
361 LIBRESOC_CMD_BUFFER_STATUS_EXECUTABLE
,
362 LIBRESOC_CMD_BUFFER_STATUS_PENDING
,
365 struct libresoc_cmd_buffer
{
366 struct vk_object_base base
;
368 struct libresoc_device
* device
;
370 struct libresoc_cmd_pool
* pool
;
371 struct list_head pool_link
;
373 VkCommandBufferUsageFlags usage_flags
;
374 VkCommandBufferLevel level
;
375 enum libresoc_cmd_buffer_status status
;
376 //struct radeon_cmdbuf *cs;
377 // struct libresoc_cmd_state state;
378 // struct libresoc_vertex_binding vertex_bindings[MAX_VBS];
379 // struct libresoc_streamout_binding streamout_bindings[MAX_SO_BUFFERS];
380 uint32_t queue_family_index
;
382 uint8_t push_constants
[MAX_PUSH_CONSTANTS_SIZE
];
383 VkShaderStageFlags push_constant_stages
;
384 // struct libresoc_descriptor_set meta_push_descriptors;
386 // struct libresoc_descriptor_state descriptors[MAX_BIND_POINTS];
388 struct libresoc_cmd_buffer_upload upload
;
390 uint32_t scratch_size_per_wave_needed
;
391 uint32_t scratch_waves_wanted
;
392 uint32_t compute_scratch_size_per_wave_needed
;
393 uint32_t compute_scratch_waves_wanted
;
394 uint32_t esgs_ring_size_needed
;
395 uint32_t gsvs_ring_size_needed
;
396 bool tess_rings_needed
;
397 bool sample_positions_needed
;
399 VkResult record_result
;
403 struct libresoc_device_memory
{
404 struct vk_object_base base
;
405 /* for dedicated allocations */
406 struct libresoc_image
*image
;
407 //struct libresoc_buffer *buffer;
414 void libresoc_free_memory(struct libresoc_device
*device
,
415 const VkAllocationCallbacks
* pAllocator
,
416 struct libresoc_device_memory
*mem
);
418 uint32_t libresoc_physical_device_api_version(struct libresoc_physical_device
*dev
);
420 int libresoc_get_instance_entrypoint_index(const char *name
);
421 int libresoc_get_device_entrypoint_index(const char *name
);
422 int libresoc_get_physical_device_entrypoint_index(const char *name
);
424 const char *libresoc_get_instance_entry_name(int index
);
425 const char *libresoc_get_physical_device_entry_name(int index
);
426 const char *libresoc_get_device_entry_name(int index
);
429 libresoc_instance_entrypoint_is_enabled(int index
, uint32_t core_version
,
430 const struct libresoc_instance_extension_table
*instance
);
432 libresoc_physical_device_entrypoint_is_enabled(int index
, uint32_t core_version
,
433 const struct libresoc_instance_extension_table
*instance
);
435 libresoc_device_entrypoint_is_enabled(int index
, uint32_t core_version
,
436 const struct libresoc_instance_extension_table
*instance
,
437 const struct libresoc_device_extension_table
*device
);
439 void *libresoc_lookup_entrypoint(const char *name
);
442 libresoc_get_debug_option_name(int id
);
444 struct libresoc_binning_settings
{
445 unsigned context_states_per_bin
; /* allowed range: [1, 6] */
446 unsigned persistent_states_per_bin
; /* allowed range: [1, 32] */
447 unsigned fpovs_per_batch
; /* allowed range: [0, 255], 0 = unlimited */
450 struct libresoc_binning_settings
451 libresoc_get_binning_settings(const struct libresoc_physical_device
*pdev
);
453 struct vk_format_description
;
454 uint32_t libresoc_translate_buffer_dataformat(const struct vk_format_description
*desc
,
456 uint32_t libresoc_translate_buffer_numformat(const struct vk_format_description
*desc
,
458 bool libresoc_is_buffer_format_supported(VkFormat format
, bool *scaled
);
459 uint32_t libresoc_translate_colorformat(VkFormat format
);
460 uint32_t libresoc_translate_color_numformat(VkFormat format
,
461 const struct vk_format_description
*desc
,
463 uint32_t libresoc_colorformat_endian_swap(uint32_t colorformat
);
464 unsigned libresoc_translate_colorswap(VkFormat format
, bool do_endian_swap
);
465 uint32_t libresoc_translate_dbformat(VkFormat format
);
466 uint32_t libresoc_translate_tex_dataformat(VkFormat format
,
467 const struct vk_format_description
*desc
,
469 uint32_t libresoc_translate_tex_numformat(VkFormat format
,
470 const struct vk_format_description
*desc
,
472 bool libresoc_format_pack_clear_color(VkFormat format
,
473 uint32_t clear_vals
[2],
474 VkClearColorValue
*value
);
475 bool libresoc_is_colorbuffer_format_supported(VkFormat format
, bool *blendable
);
476 bool libresoc_dcc_formats_compatible(VkFormat format1
,
478 bool libresoc_device_supports_etc(struct libresoc_physical_device
*physical_device
);
481 /* Whether the image has a htile that is known consistent with the contents of
482 * the image and is allowed to be in compressed form.
484 * If this is false reads that don't use the htile should be able to return
487 bool libresoc_layout_is_htile_compressed(const struct libresoc_image
*image
,
488 VkImageLayout layout
,
490 unsigned queue_mask
);
492 bool libresoc_layout_can_fast_clear(const struct libresoc_image
*image
,
493 VkImageLayout layout
,
495 unsigned queue_mask
);
497 bool libresoc_layout_dcc_compressed(const struct libresoc_device
*device
,
498 const struct libresoc_image
*image
,
499 VkImageLayout layout
,
501 unsigned queue_mask
);
503 libresoc_graphics_pipeline_create(VkDevice device
,
504 VkPipelineCache cache
,
505 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
506 const VkAllocationCallbacks
*alloc
,
507 VkPipeline
*pPipeline
);
509 #define libresoc_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
511 VkResult
__vk_errorf(struct libresoc_instance
*instance
, VkResult error
,
512 const char *file
, int line
,
513 const char *format
, ...);
515 #define vk_error(instance, error) __vk_errorf(instance, error, __FILE__, __LINE__, NULL);
516 #define vk_errorf(instance, error, format, ...) __vk_errorf(instance, error, __FILE__, __LINE__, format, ## __VA_ARGS__);
518 void libresoc_loge(const char *format
, ...) libresoc_printflike(1, 2);
519 void libresoc_loge_v(const char *format
, va_list va
);
521 #define LIBRESOC_DEFINE_HANDLE_CASTS(__libresoc_type, __VkType) \
523 static inline struct __libresoc_type * \
524 __libresoc_type ## _from_handle(__VkType _handle) \
526 return (struct __libresoc_type *) _handle; \
529 static inline __VkType \
530 __libresoc_type ## _to_handle(struct __libresoc_type *_obj) \
532 return (__VkType) _obj; \
535 #define LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(__libresoc_type, __VkType) \
537 static inline struct __libresoc_type * \
538 __libresoc_type ## _from_handle(__VkType _handle) \
540 return (struct __libresoc_type *)(uintptr_t) _handle; \
543 static inline __VkType \
544 __libresoc_type ## _to_handle(struct __libresoc_type *_obj) \
546 return (__VkType)(uintptr_t) _obj; \
549 #define LIBRESOC_FROM_HANDLE(__libresoc_type, __name, __handle) \
550 struct __libresoc_type *__name = __libresoc_type ## _from_handle(__handle)
552 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_cmd_buffer
, VkCommandBuffer
)
553 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_device
, VkDevice
)
554 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_instance
, VkInstance
)
555 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_physical_device
, VkPhysicalDevice
)
556 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_queue
, VkQueue
)
558 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_cmd_pool
, VkCommandPool
)
559 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_buffer, VkBuffer)
560 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_buffer_view, VkBufferView)
561 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_pool, VkDescriptorPool)
562 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_set, VkDescriptorSet)
563 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_set_layout, VkDescriptorSetLayout)
564 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_update_template, VkDescriptorUpdateTemplate)
565 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_device_memory
, VkDeviceMemory
)
566 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_fence
, VkFence
)
567 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_event, VkEvent)
568 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_framebuffer, VkFramebuffer)
569 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_image
, VkImage
)
570 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_image_view, VkImageView);
571 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline_cache
, VkPipelineCache
)
572 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline
, VkPipeline
)
573 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline_layout, VkPipelineLayout)
574 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_query_pool, VkQueryPool)
575 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_render_pass, VkRenderPass)
576 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_sampler, VkSampler)
577 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_sampler_ycbcr_conversion, VkSamplerYcbcrConversion)
578 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_shader_module
, VkShaderModule
)
579 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_semaphore
, VkSemaphore
)
581 #endif /* LIBRESOC_PRIVATE_H */