Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / libre-soc / vulkan / libresoc_private.h
1 /*
2 * Copyright © 2019 Raspberry Pi
3 *
4 * based in part on anv driver which is:
5 * Copyright © 2015 Intel Corporation
6 *
7 * based in part on libresoc driver which is:
8 * Copyright © 2016 Red Hat.
9 * Copyright © 2016 Bas Nieuwenhuizen
10 *
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:
17 *
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
20 * Software.
21 *
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
28 * IN THE SOFTWARE.
29 */
30 #ifndef LIBRESOC_PRIVATE_H
31 #define LIBRESOC_PRIVATE_H
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <vulkan/vulkan.h>
36 #include <vulkan/vk_icd.h>
37
38 #include "vk_alloc.h"
39 #include "vk_debug_report.h"
40 #include "util/xmlconfig.h"
41 #include "compiler/shader_enums.h"
42
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"
48
49 #include "wsi_common.h"
50 #define LIBRESOC_MAX_QUEUE_FAMILIES 1
51
52 static inline gl_shader_stage
53 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
54 {
55 assert(__builtin_popcount(vk_stage) == 1);
56 return ffs(vk_stage) - 1;
57 }
58
59 static inline VkShaderStageFlagBits
60 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
61 {
62 return (1 << mesa_stage);
63 }
64
65 static inline uint32_t
66 align_u32(uint32_t v, uint32_t a)
67 {
68 assert(a != 0 && a == (a & -a));
69 return (v + a - 1) & ~(a - 1);
70 }
71
72 static inline uint32_t
73 align_u32_npot(uint32_t v, uint32_t a)
74 {
75 return (v + a - 1) / a * a;
76 }
77
78 static inline uint64_t
79 align_u64(uint64_t v, uint64_t a)
80 {
81 assert(a != 0 && a == (a & -a));
82 return (v + a - 1) & ~(a - 1);
83 }
84
85 static inline int32_t
86 align_i32(int32_t v, int32_t a)
87 {
88 assert(a != 0 && a == (a & -a));
89 return (v + a - 1) & ~(a - 1);
90 }
91
92 /** Alignment must be a power of 2. */
93 static inline bool
94 libresoc_is_aligned(uintmax_t n, uintmax_t a)
95 {
96 assert(a == (a & -a));
97 return (n & (a - 1)) == 0;
98 }
99
100 static inline uint32_t
101 round_up_u32(uint32_t v, uint32_t a)
102 {
103 return (v + a - 1) / a;
104 }
105
106 static inline uint64_t
107 round_up_u64(uint64_t v, uint64_t a)
108 {
109 return (v + a - 1) / a;
110 }
111
112 static inline uint32_t
113 libresoc_minify(uint32_t n, uint32_t levels)
114 {
115 if (unlikely(n == 0))
116 return 0;
117 else
118 return MAX2(n >> levels, 1);
119 }
120 static inline float
121 libresoc_clamp_f(float f, float min, float max)
122 {
123 assert(min < max);
124
125 if (f > max)
126 return max;
127 else if (f < min)
128 return min;
129 else
130 return f;
131 }
132
133 static inline bool
134 libresoc_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
135 {
136 if (*inout_mask & clear_mask) {
137 *inout_mask &= ~clear_mask;
138 return true;
139 } else {
140 return false;
141 }
142 }
143
144 struct libresoc_fence {
145 struct vk_object_base base;
146 };
147
148 struct libresoc_image_create_info {
149 const VkImageCreateInfo *vk_info;
150 bool scanout;
151 bool no_metadata_planes;
152 };
153
154 struct libresoc_image {
155 struct vk_object_base base;
156 VkImageType type;
157 /* The original VkFormat provided by the client. This may not match any
158 * of the actual surface formats.
159 */
160 VkFormat vk_format;
161 VkImageAspectFlags aspects;
162 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
163 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
164 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
165
166 VkDeviceSize size;
167 uint32_t alignment;
168
169 unsigned queue_family_mask;
170 bool exclusive;
171 bool shareable;
172
173 };
174
175 VkResult libresoc_image_create(VkDevice _device,
176 const struct libresoc_image_create_info *info,
177 const VkAllocationCallbacks* alloc,
178 VkImage *pImage);
179
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;
186 };
187
188 struct libresoc_semaphore {
189 struct vk_object_base base;
190 };
191
192 struct libresoc_instance;
193 struct libresoc_device;
194 struct cache_entry;
195
196 struct libresoc_pipeline_cache {
197 struct vk_object_base base;
198 struct libresoc_device * device;
199 pthread_mutex_t mutex;
200 VkPipelineCacheCreateFlags flags;
201
202 uint32_t total_size;
203 uint32_t table_size;
204 uint32_t kernel_count;
205 struct cache_entry ** hash_table;
206 bool modified;
207
208 VkAllocationCallbacks alloc;
209 };
210
211
212 struct libresoc_shader_binary;
213 struct libresoc_shader_variant;
214
215 struct libresoc_pipeline {
216 struct vk_object_base base;
217 struct libresoc_device * device;
218
219
220 VkShaderStageFlags active_stages;
221
222 };
223 void
224 libresoc_pipeline_cache_init(struct libresoc_pipeline_cache *cache,
225 struct libresoc_device *device);
226 void
227 libresoc_pipeline_cache_finish(struct libresoc_pipeline_cache *cache);
228 bool
229 libresoc_pipeline_cache_load(struct libresoc_pipeline_cache *cache,
230 const void *data, size_t size);
231
232 bool
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);
238
239 void
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);
245
246 VkResult libresoc_init_wsi(struct libresoc_physical_device *physical_device);
247 void libresoc_finish_wsi(struct libresoc_physical_device *physical_device);
248
249 struct libresoc_device {
250
251 struct vk_device vk;
252 VkAllocationCallbacks alloc;
253
254 struct libresoc_instance *instance;
255
256 struct libresoc_device_extension_table enabled_extensions;
257 struct libresoc_device_dispatch_table dispatch;
258
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;
263
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;
273 /* FIXME: stub */
274 };
275
276
277 struct libresoc_physical_device {
278 VK_LOADER_DATA _loader_data;
279
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;
285
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];
290 int local_fd;
291 int master_fd;
292 VkPhysicalDeviceMemoryProperties memory_properties;
293 /* FIXME: stub */
294 };
295
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;
302 };
303
304 struct libresoc_instance {
305 struct vk_object_base base;
306
307 VkAllocationCallbacks alloc;
308
309 uint32_t apiVersion;
310
311 uint64_t debug_flags;
312 char * engineName;
313 uint32_t engineVersion;
314 struct libresoc_app_info app_info;
315
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;
323
324 struct driOptionCache dri_options;
325 struct driOptionCache available_dri_options;
326 struct vk_debug_report_instance debug_report_callbacks;
327 };
328
329 struct libresoc_deferred_queue_submission;
330 struct libresoc_queue {
331 VK_LOADER_DATA _loader_data;
332
333 struct libresoc_device *device;
334
335 uint32_t queue_family_index;
336 int queue_idx;
337 VkDeviceQueueCreateFlags flags;
338
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;
345 bool thread_exit;
346 bool thread_running;
347 /* FIXME: stub */
348 };
349
350 struct libresoc_cmd_buffer_upload {
351 uint8_t *map;
352 unsigned offset;
353 uint64_t size;
354 struct list_head list;
355 };
356
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,
363 };
364
365 struct libresoc_cmd_buffer {
366 struct vk_object_base base;
367
368 struct libresoc_device * device;
369
370 struct libresoc_cmd_pool * pool;
371 struct list_head pool_link;
372
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;
381
382 uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
383 VkShaderStageFlags push_constant_stages;
384 // struct libresoc_descriptor_set meta_push_descriptors;
385
386 // struct libresoc_descriptor_state descriptors[MAX_BIND_POINTS];
387
388 struct libresoc_cmd_buffer_upload upload;
389
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;
398
399 VkResult record_result;
400
401 };
402
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;
408 uint32_t heap_index;
409 uint64_t alloc_size;
410 void * map;
411 void * user_ptr;
412 };
413
414 void libresoc_free_memory(struct libresoc_device *device,
415 const VkAllocationCallbacks* pAllocator,
416 struct libresoc_device_memory *mem);
417
418 uint32_t libresoc_physical_device_api_version(struct libresoc_physical_device *dev);
419
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);
423
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);
427
428 bool
429 libresoc_instance_entrypoint_is_enabled(int index, uint32_t core_version,
430 const struct libresoc_instance_extension_table *instance);
431 bool
432 libresoc_physical_device_entrypoint_is_enabled(int index, uint32_t core_version,
433 const struct libresoc_instance_extension_table *instance);
434 bool
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);
438
439 void *libresoc_lookup_entrypoint(const char *name);
440
441 const char *
442 libresoc_get_debug_option_name(int id);
443
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 */
448 };
449
450 struct libresoc_binning_settings
451 libresoc_get_binning_settings(const struct libresoc_physical_device *pdev);
452
453 struct vk_format_description;
454 uint32_t libresoc_translate_buffer_dataformat(const struct vk_format_description *desc,
455 int first_non_void);
456 uint32_t libresoc_translate_buffer_numformat(const struct vk_format_description *desc,
457 int first_non_void);
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,
462 int first_non_void);
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,
468 int first_non_void);
469 uint32_t libresoc_translate_tex_numformat(VkFormat format,
470 const struct vk_format_description *desc,
471 int first_non_void);
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,
477 VkFormat format2);
478 bool libresoc_device_supports_etc(struct libresoc_physical_device *physical_device);
479
480
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.
483 *
484 * If this is false reads that don't use the htile should be able to return
485 * correct results.
486 */
487 bool libresoc_layout_is_htile_compressed(const struct libresoc_image *image,
488 VkImageLayout layout,
489 bool in_render_loop,
490 unsigned queue_mask);
491
492 bool libresoc_layout_can_fast_clear(const struct libresoc_image *image,
493 VkImageLayout layout,
494 bool in_render_loop,
495 unsigned queue_mask);
496
497 bool libresoc_layout_dcc_compressed(const struct libresoc_device *device,
498 const struct libresoc_image *image,
499 VkImageLayout layout,
500 bool in_render_loop,
501 unsigned queue_mask);
502 VkResult
503 libresoc_graphics_pipeline_create(VkDevice device,
504 VkPipelineCache cache,
505 const VkGraphicsPipelineCreateInfo *pCreateInfo,
506 const VkAllocationCallbacks *alloc,
507 VkPipeline *pPipeline);
508
509 #define libresoc_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
510
511 VkResult __vk_errorf(struct libresoc_instance *instance, VkResult error,
512 const char *file, int line,
513 const char *format, ...);
514
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__);
517
518 void libresoc_loge(const char *format, ...) libresoc_printflike(1, 2);
519 void libresoc_loge_v(const char *format, va_list va);
520
521 #define LIBRESOC_DEFINE_HANDLE_CASTS(__libresoc_type, __VkType) \
522 \
523 static inline struct __libresoc_type * \
524 __libresoc_type ## _from_handle(__VkType _handle) \
525 { \
526 return (struct __libresoc_type *) _handle; \
527 } \
528 \
529 static inline __VkType \
530 __libresoc_type ## _to_handle(struct __libresoc_type *_obj) \
531 { \
532 return (__VkType) _obj; \
533 }
534
535 #define LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(__libresoc_type, __VkType) \
536 \
537 static inline struct __libresoc_type * \
538 __libresoc_type ## _from_handle(__VkType _handle) \
539 { \
540 return (struct __libresoc_type *)(uintptr_t) _handle; \
541 } \
542 \
543 static inline __VkType \
544 __libresoc_type ## _to_handle(struct __libresoc_type *_obj) \
545 { \
546 return (__VkType)(uintptr_t) _obj; \
547 }
548
549 #define LIBRESOC_FROM_HANDLE(__libresoc_type, __name, __handle) \
550 struct __libresoc_type *__name = __libresoc_type ## _from_handle(__handle)
551
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)
557
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)
580
581 #endif /* LIBRESOC_PRIVATE_H */