bc4240d8d76e1be322d7b86b04a24bc7e3eb937a
[mesa.git] / src / vulkan / overlay-layer / overlay.cpp
1 /*
2 * Copyright © 2019 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <assert.h>
27
28 #include <vulkan/vulkan.h>
29 #include <vulkan/vk_layer.h>
30
31 #include "git_sha1.h"
32
33 #include "imgui.h"
34
35 #include "overlay_params.h"
36
37 #include "util/debug.h"
38 #include "util/hash_table.h"
39 #include "util/list.h"
40 #include "util/ralloc.h"
41 #include "util/os_time.h"
42 #include "util/os_socket.h"
43 #include "util/simple_mtx.h"
44
45 #include "vk_enum_to_str.h"
46 #include "vk_util.h"
47
48 /* Mapped from VkInstace/VkPhysicalDevice */
49 struct instance_data {
50 struct vk_instance_dispatch_table vtable;
51 VkInstance instance;
52
53 struct overlay_params params;
54 bool pipeline_statistics_enabled;
55
56 bool first_line_printed;
57
58 int control_client;
59 };
60
61 struct frame_stat {
62 uint64_t stats[OVERLAY_PARAM_ENABLED_MAX];
63 };
64
65 /* Mapped from VkDevice */
66 struct queue_data;
67 struct device_data {
68 struct instance_data *instance;
69
70 PFN_vkSetDeviceLoaderData set_device_loader_data;
71
72 struct vk_device_dispatch_table vtable;
73 VkPhysicalDevice physical_device;
74 VkDevice device;
75
76 VkPhysicalDeviceProperties properties;
77
78 struct queue_data *graphic_queue;
79
80 struct queue_data **queues;
81 uint32_t n_queues;
82
83 /* For a single frame */
84 struct frame_stat frame_stats;
85 };
86
87 /* Mapped from VkCommandBuffer */
88 struct command_buffer_data {
89 struct device_data *device;
90
91 VkCommandBufferLevel level;
92
93 VkCommandBuffer cmd_buffer;
94 VkQueryPool pipeline_query_pool;
95 VkQueryPool timestamp_query_pool;
96 uint32_t query_index;
97
98 struct frame_stat stats;
99
100 struct list_head link; /* link into queue_data::running_command_buffer */
101 };
102
103 /* Mapped from VkQueue */
104 struct queue_data {
105 struct device_data *device;
106
107 VkQueue queue;
108 VkQueueFlags flags;
109 uint32_t family_index;
110 uint64_t timestamp_mask;
111
112 VkFence queries_fence;
113
114 struct list_head running_command_buffer;
115 };
116
117 struct overlay_draw {
118 struct list_head link;
119
120 VkCommandBuffer command_buffer;
121
122 VkSemaphore semaphore;
123 VkFence fence;
124
125 VkBuffer vertex_buffer;
126 VkDeviceMemory vertex_buffer_mem;
127 VkDeviceSize vertex_buffer_size;
128
129 VkBuffer index_buffer;
130 VkDeviceMemory index_buffer_mem;
131 VkDeviceSize index_buffer_size;
132 };
133
134 /* Mapped from VkSwapchainKHR */
135 struct swapchain_data {
136 struct device_data *device;
137
138 VkSwapchainKHR swapchain;
139 unsigned width, height;
140 VkFormat format;
141
142 uint32_t n_images;
143 VkImage *images;
144 VkImageView *image_views;
145 VkFramebuffer *framebuffers;
146
147 VkRenderPass render_pass;
148
149 VkDescriptorPool descriptor_pool;
150 VkDescriptorSetLayout descriptor_layout;
151 VkDescriptorSet descriptor_set;
152
153 VkSampler font_sampler;
154
155 VkPipelineLayout pipeline_layout;
156 VkPipeline pipeline;
157
158 VkCommandPool command_pool;
159
160 struct list_head draws; /* List of struct overlay_draw */
161
162 bool font_uploaded;
163 VkImage font_image;
164 VkImageView font_image_view;
165 VkDeviceMemory font_mem;
166 VkBuffer upload_font_buffer;
167 VkDeviceMemory upload_font_buffer_mem;
168
169 /**/
170 ImGuiContext* imgui_context;
171 ImVec2 window_size;
172
173 /**/
174 uint64_t n_frames;
175 uint64_t last_present_time;
176
177 unsigned n_frames_since_update;
178 uint64_t last_fps_update;
179 double fps;
180
181 enum overlay_param_enabled stat_selector;
182 double time_dividor;
183 struct frame_stat stats_min, stats_max;
184 struct frame_stat frames_stats[200];
185
186 /* Over a single frame */
187 struct frame_stat frame_stats;
188
189 /* Over fps_sampling_period */
190 struct frame_stat accumulated_stats;
191 };
192
193 static const VkQueryPipelineStatisticFlags overlay_query_flags =
194 VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT |
195 VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT |
196 VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT |
197 VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT |
198 VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT |
199 VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT |
200 VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
201 VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT |
202 VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT |
203 VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT |
204 VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
205 #define OVERLAY_QUERY_COUNT (11)
206
207 static struct hash_table_u64 *vk_object_to_data = NULL;
208 static simple_mtx_t vk_object_to_data_mutex = _SIMPLE_MTX_INITIALIZER_NP;
209
210 thread_local ImGuiContext* __MesaImGui;
211
212 static inline void ensure_vk_object_map(void)
213 {
214 if (!vk_object_to_data)
215 vk_object_to_data = _mesa_hash_table_u64_create(NULL);
216 }
217
218 #define HKEY(obj) ((uint64_t)(obj))
219 #define FIND(type, obj) ((type *)find_object_data(HKEY(obj)))
220
221 static void *find_object_data(uint64_t obj)
222 {
223 simple_mtx_lock(&vk_object_to_data_mutex);
224 ensure_vk_object_map();
225 void *data = _mesa_hash_table_u64_search(vk_object_to_data, obj);
226 simple_mtx_unlock(&vk_object_to_data_mutex);
227 return data;
228 }
229
230 static void map_object(uint64_t obj, void *data)
231 {
232 simple_mtx_lock(&vk_object_to_data_mutex);
233 ensure_vk_object_map();
234 _mesa_hash_table_u64_insert(vk_object_to_data, obj, data);
235 simple_mtx_unlock(&vk_object_to_data_mutex);
236 }
237
238 static void unmap_object(uint64_t obj)
239 {
240 simple_mtx_lock(&vk_object_to_data_mutex);
241 _mesa_hash_table_u64_remove(vk_object_to_data, obj);
242 simple_mtx_unlock(&vk_object_to_data_mutex);
243 }
244
245 /**/
246
247 #define VK_CHECK(expr) \
248 do { \
249 VkResult __result = (expr); \
250 if (__result != VK_SUCCESS) { \
251 fprintf(stderr, "'%s' line %i failed with %s\n", \
252 #expr, __LINE__, vk_Result_to_str(__result)); \
253 } \
254 } while (0)
255
256 /**/
257
258 static VkLayerInstanceCreateInfo *get_instance_chain_info(const VkInstanceCreateInfo *pCreateInfo,
259 VkLayerFunction func)
260 {
261 vk_foreach_struct(item, pCreateInfo->pNext) {
262 if (item->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
263 ((VkLayerInstanceCreateInfo *) item)->function == func)
264 return (VkLayerInstanceCreateInfo *) item;
265 }
266 unreachable("instance chain info not found");
267 return NULL;
268 }
269
270 static VkLayerDeviceCreateInfo *get_device_chain_info(const VkDeviceCreateInfo *pCreateInfo,
271 VkLayerFunction func)
272 {
273 vk_foreach_struct(item, pCreateInfo->pNext) {
274 if (item->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
275 ((VkLayerDeviceCreateInfo *) item)->function == func)
276 return (VkLayerDeviceCreateInfo *)item;
277 }
278 unreachable("device chain info not found");
279 return NULL;
280 }
281
282 static struct VkBaseOutStructure *
283 clone_chain(const struct VkBaseInStructure *chain)
284 {
285 struct VkBaseOutStructure *head = NULL, *tail = NULL;
286
287 vk_foreach_struct_const(item, chain) {
288 size_t item_size = vk_structure_type_size(item);
289 struct VkBaseOutStructure *new_item =
290 (struct VkBaseOutStructure *)malloc(item_size);;
291
292 memcpy(new_item, item, item_size);
293
294 if (!head)
295 head = new_item;
296 if (tail)
297 tail->pNext = new_item;
298 tail = new_item;
299 }
300
301 return head;
302 }
303
304 static void
305 free_chain(struct VkBaseOutStructure *chain)
306 {
307 while (chain) {
308 void *node = chain;
309 chain = chain->pNext;
310 free(node);
311 }
312 }
313
314 /**/
315
316 static struct instance_data *new_instance_data(VkInstance instance)
317 {
318 struct instance_data *data = rzalloc(NULL, struct instance_data);
319 data->instance = instance;
320 data->control_client = -1;
321 map_object(HKEY(data->instance), data);
322 return data;
323 }
324
325 static void destroy_instance_data(struct instance_data *data)
326 {
327 if (data->params.output_file)
328 fclose(data->params.output_file);
329 if (data->params.control >= 0)
330 os_socket_close(data->params.control);
331 unmap_object(HKEY(data->instance));
332 ralloc_free(data);
333 }
334
335 static void instance_data_map_physical_devices(struct instance_data *instance_data,
336 bool map)
337 {
338 uint32_t physicalDeviceCount = 0;
339 instance_data->vtable.EnumeratePhysicalDevices(instance_data->instance,
340 &physicalDeviceCount,
341 NULL);
342
343 VkPhysicalDevice *physicalDevices = (VkPhysicalDevice *) malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount);
344 instance_data->vtable.EnumeratePhysicalDevices(instance_data->instance,
345 &physicalDeviceCount,
346 physicalDevices);
347
348 for (uint32_t i = 0; i < physicalDeviceCount; i++) {
349 if (map)
350 map_object(HKEY(physicalDevices[i]), instance_data);
351 else
352 unmap_object(HKEY(physicalDevices[i]));
353 }
354
355 free(physicalDevices);
356 }
357
358 /**/
359 static struct device_data *new_device_data(VkDevice device, struct instance_data *instance)
360 {
361 struct device_data *data = rzalloc(NULL, struct device_data);
362 data->instance = instance;
363 data->device = device;
364 map_object(HKEY(data->device), data);
365 return data;
366 }
367
368 static struct queue_data *new_queue_data(VkQueue queue,
369 const VkQueueFamilyProperties *family_props,
370 uint32_t family_index,
371 struct device_data *device_data)
372 {
373 struct queue_data *data = rzalloc(device_data, struct queue_data);
374 data->device = device_data;
375 data->queue = queue;
376 data->flags = family_props->queueFlags;
377 data->timestamp_mask = (1ull << family_props->timestampValidBits) - 1;
378 data->family_index = family_index;
379 list_inithead(&data->running_command_buffer);
380 map_object(HKEY(data->queue), data);
381
382 /* Fence synchronizing access to queries on that queue. */
383 VkFenceCreateInfo fence_info = {};
384 fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
385 fence_info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
386 VK_CHECK(device_data->vtable.CreateFence(device_data->device,
387 &fence_info,
388 NULL,
389 &data->queries_fence));
390
391 if (data->flags & VK_QUEUE_GRAPHICS_BIT)
392 device_data->graphic_queue = data;
393
394 return data;
395 }
396
397 static void destroy_queue(struct queue_data *data)
398 {
399 struct device_data *device_data = data->device;
400 device_data->vtable.DestroyFence(device_data->device, data->queries_fence, NULL);
401 unmap_object(HKEY(data->queue));
402 ralloc_free(data);
403 }
404
405 static void device_map_queues(struct device_data *data,
406 const VkDeviceCreateInfo *pCreateInfo)
407 {
408 for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
409 data->n_queues += pCreateInfo->pQueueCreateInfos[i].queueCount;
410 data->queues = ralloc_array(data, struct queue_data *, data->n_queues);
411
412 struct instance_data *instance_data = data->instance;
413 uint32_t n_family_props;
414 instance_data->vtable.GetPhysicalDeviceQueueFamilyProperties(data->physical_device,
415 &n_family_props,
416 NULL);
417 VkQueueFamilyProperties *family_props =
418 (VkQueueFamilyProperties *)malloc(sizeof(VkQueueFamilyProperties) * n_family_props);
419 instance_data->vtable.GetPhysicalDeviceQueueFamilyProperties(data->physical_device,
420 &n_family_props,
421 family_props);
422
423 uint32_t queue_index = 0;
424 for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
425 for (uint32_t j = 0; j < pCreateInfo->pQueueCreateInfos[i].queueCount; j++) {
426 VkQueue queue;
427 data->vtable.GetDeviceQueue(data->device,
428 pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex,
429 j, &queue);
430
431 VK_CHECK(data->set_device_loader_data(data->device, queue));
432
433 data->queues[queue_index++] =
434 new_queue_data(queue, &family_props[pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex],
435 pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex, data);
436 }
437 }
438
439 free(family_props);
440 }
441
442 static void device_unmap_queues(struct device_data *data)
443 {
444 for (uint32_t i = 0; i < data->n_queues; i++)
445 destroy_queue(data->queues[i]);
446 }
447
448 static void destroy_device_data(struct device_data *data)
449 {
450 unmap_object(HKEY(data->device));
451 ralloc_free(data);
452 }
453
454 /**/
455 static struct command_buffer_data *new_command_buffer_data(VkCommandBuffer cmd_buffer,
456 VkCommandBufferLevel level,
457 VkQueryPool pipeline_query_pool,
458 VkQueryPool timestamp_query_pool,
459 uint32_t query_index,
460 struct device_data *device_data)
461 {
462 struct command_buffer_data *data = rzalloc(NULL, struct command_buffer_data);
463 data->device = device_data;
464 data->cmd_buffer = cmd_buffer;
465 data->level = level;
466 data->pipeline_query_pool = pipeline_query_pool;
467 data->timestamp_query_pool = timestamp_query_pool;
468 data->query_index = query_index;
469 list_inithead(&data->link);
470 map_object(HKEY(data->cmd_buffer), data);
471 return data;
472 }
473
474 static void destroy_command_buffer_data(struct command_buffer_data *data)
475 {
476 unmap_object(HKEY(data->cmd_buffer));
477 list_delinit(&data->link);
478 ralloc_free(data);
479 }
480
481 /**/
482 static struct swapchain_data *new_swapchain_data(VkSwapchainKHR swapchain,
483 struct device_data *device_data)
484 {
485 struct instance_data *instance_data = device_data->instance;
486 struct swapchain_data *data = rzalloc(NULL, struct swapchain_data);
487 data->device = device_data;
488 data->swapchain = swapchain;
489 data->window_size = ImVec2(instance_data->params.width, instance_data->params.height);
490 list_inithead(&data->draws);
491 map_object(HKEY(data->swapchain), data);
492 return data;
493 }
494
495 static void destroy_swapchain_data(struct swapchain_data *data)
496 {
497 unmap_object(HKEY(data->swapchain));
498 ralloc_free(data);
499 }
500
501 struct overlay_draw *get_overlay_draw(struct swapchain_data *data)
502 {
503 struct device_data *device_data = data->device;
504 struct overlay_draw *draw = list_is_empty(&data->draws) ?
505 NULL : list_first_entry(&data->draws, struct overlay_draw, link);
506
507 VkSemaphoreCreateInfo sem_info = {};
508 sem_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
509
510 if (draw && device_data->vtable.GetFenceStatus(device_data->device, draw->fence) == VK_SUCCESS) {
511 list_del(&draw->link);
512 VK_CHECK(device_data->vtable.ResetFences(device_data->device,
513 1, &draw->fence));
514 list_addtail(&draw->link, &data->draws);
515 return draw;
516 }
517
518 draw = rzalloc(data, struct overlay_draw);
519
520 VkCommandBufferAllocateInfo cmd_buffer_info = {};
521 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
522 cmd_buffer_info.commandPool = data->command_pool;
523 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
524 cmd_buffer_info.commandBufferCount = 1;
525 VK_CHECK(device_data->vtable.AllocateCommandBuffers(device_data->device,
526 &cmd_buffer_info,
527 &draw->command_buffer));
528 VK_CHECK(device_data->set_device_loader_data(device_data->device,
529 draw->command_buffer));
530
531
532 VkFenceCreateInfo fence_info = {};
533 fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
534 VK_CHECK(device_data->vtable.CreateFence(device_data->device,
535 &fence_info,
536 NULL,
537 &draw->fence));
538
539 VK_CHECK(device_data->vtable.CreateSemaphore(device_data->device, &sem_info,
540 NULL, &draw->semaphore));
541
542 list_addtail(&draw->link, &data->draws);
543
544 return draw;
545 }
546
547 static const char *param_unit(enum overlay_param_enabled param)
548 {
549 switch (param) {
550 case OVERLAY_PARAM_ENABLED_frame_timing:
551 case OVERLAY_PARAM_ENABLED_acquire_timing:
552 case OVERLAY_PARAM_ENABLED_present_timing:
553 return "(us)";
554 case OVERLAY_PARAM_ENABLED_gpu_timing:
555 return "(ns)";
556 default:
557 return "";
558 }
559 }
560
561 static void parse_command(struct instance_data *instance_data,
562 const char *cmd, unsigned cmdlen,
563 const char *param, unsigned paramlen)
564 {
565 /* TODO: Parse commands here. */
566 }
567
568 #define BUFSIZE 4096
569
570 /**
571 * This function will process commands through the control file.
572 *
573 * A command starts with a colon, followed by the command, and followed by an
574 * option '=' and a parameter. It has to end with a semi-colon. A full command
575 * + parameter looks like:
576 *
577 * :cmd=param;
578 */
579 static void process_char(struct instance_data *instance_data, char c)
580 {
581 static char cmd[BUFSIZE];
582 static char param[BUFSIZE];
583
584 static unsigned cmdpos = 0;
585 static unsigned parampos = 0;
586 static bool reading_cmd = false;
587 static bool reading_param = false;
588
589 switch (c) {
590 case ':':
591 cmdpos = 0;
592 parampos = 0;
593 reading_cmd = true;
594 reading_param = false;
595 break;
596 case ';':
597 if (!reading_cmd)
598 break;
599 cmd[cmdpos++] = '\0';
600 param[parampos++] = '\0';
601 parse_command(instance_data, cmd, cmdpos, param, parampos);
602 reading_cmd = false;
603 reading_param = false;
604 break;
605 case '=':
606 if (!reading_cmd)
607 break;
608 reading_param = true;
609 break;
610 default:
611 if (!reading_cmd)
612 break;
613
614 if (reading_param) {
615 /* overflow means an invalid parameter */
616 if (parampos >= BUFSIZE - 1) {
617 reading_cmd = false;
618 reading_param = false;
619 break;
620 }
621
622 param[parampos++] = c;
623 } else {
624 /* overflow means an invalid command */
625 if (cmdpos >= BUFSIZE - 1) {
626 reading_cmd = false;
627 break;
628 }
629
630 cmd[cmdpos++] = c;
631 }
632 }
633 }
634
635 static void control_send(struct instance_data *instance_data,
636 const char *cmd, unsigned cmdlen,
637 const char *param, unsigned paramlen)
638 {
639 unsigned msglen = 0;
640 char buffer[BUFSIZE];
641
642 assert(cmdlen + paramlen + 3 < BUFSIZE);
643
644 buffer[msglen++] = ':';
645
646 memcpy(&buffer[msglen], cmd, cmdlen);
647 msglen += cmdlen;
648
649 if (paramlen > 0) {
650 buffer[msglen++] = '=';
651 memcpy(&buffer[msglen], param, paramlen);
652 msglen += paramlen;
653 buffer[msglen++] = ';';
654 }
655
656 os_socket_send(instance_data->control_client, buffer, msglen, 0);
657 }
658
659 static void control_send_connection_string(struct device_data *device_data)
660 {
661 struct instance_data *instance_data = device_data->instance;
662
663 const char *controlVersionCmd = "MesaOverlayControlVersion";
664 const char *controlVersionString = "1";
665
666 control_send(instance_data, controlVersionCmd, strlen(controlVersionCmd),
667 controlVersionString, strlen(controlVersionString));
668
669 const char *deviceCmd = "DeviceName";
670 const char *deviceName = device_data->properties.deviceName;
671
672 control_send(instance_data, deviceCmd, strlen(deviceCmd),
673 deviceName, strlen(deviceName));
674
675 const char *mesaVersionCmd = "MesaVersion";
676 const char *mesaVersionString = "Mesa " PACKAGE_VERSION MESA_GIT_SHA1;
677
678 control_send(instance_data, mesaVersionCmd, strlen(mesaVersionCmd),
679 mesaVersionString, strlen(mesaVersionString));
680 }
681
682 static void control_client_check(struct device_data *device_data)
683 {
684 struct instance_data *instance_data = device_data->instance;
685
686 /* Already connected, just return. */
687 if (instance_data->control_client >= 0)
688 return;
689
690 int socket = os_socket_accept(instance_data->params.control);
691 if (socket == -1) {
692 if (errno != EAGAIN && errno != EWOULDBLOCK && errno != ECONNABORTED)
693 fprintf(stderr, "ERROR on socket: %s\n", strerror(errno));
694 return;
695 }
696
697 if (socket >= 0) {
698 os_socket_block(socket, false);
699 instance_data->control_client = socket;
700 control_send_connection_string(device_data);
701 }
702 }
703
704 static void control_client_disconnected(struct instance_data *instance_data)
705 {
706 os_socket_close(instance_data->control_client);
707 instance_data->control_client = -1;
708 }
709
710 static void process_control_socket(struct instance_data *instance_data)
711 {
712 const int client = instance_data->control_client;
713 if (client >= 0) {
714 char buf[BUFSIZE];
715
716 while (true) {
717 ssize_t n = os_socket_recv(client, buf, BUFSIZE, 0);
718
719 if (n == -1) {
720 if (errno == EAGAIN || errno == EWOULDBLOCK) {
721 /* nothing to read, try again later */
722 break;
723 }
724
725 if (errno != ECONNRESET)
726 fprintf(stderr, "ERROR on connection: %s\n", strerror(errno));
727
728 control_client_disconnected(instance_data);
729 } else if (n == 0) {
730 /* recv() returns 0 when the client disconnects */
731 control_client_disconnected(instance_data);
732 }
733
734 for (ssize_t i = 0; i < n; i++) {
735 process_char(instance_data, buf[i]);
736 }
737
738 /* If we try to read BUFSIZE and receive BUFSIZE bytes from the
739 * socket, there's a good chance that there's still more data to be
740 * read, so we will try again. Otherwise, simply be done for this
741 * iteration and try again on the next frame.
742 */
743 if (n < BUFSIZE)
744 break;
745 }
746 }
747 }
748
749 static void snapshot_swapchain_frame(struct swapchain_data *data)
750 {
751 struct device_data *device_data = data->device;
752 struct instance_data *instance_data = device_data->instance;
753 uint32_t f_idx = data->n_frames % ARRAY_SIZE(data->frames_stats);
754 uint64_t now = os_time_get(); /* us */
755
756 if (instance_data->params.control >= 0) {
757 control_client_check(device_data);
758 process_control_socket(instance_data);
759 }
760
761 if (data->last_present_time) {
762 data->frame_stats.stats[OVERLAY_PARAM_ENABLED_frame_timing] =
763 now - data->last_present_time;
764 }
765
766 memset(&data->frames_stats[f_idx], 0, sizeof(data->frames_stats[f_idx]));
767 for (int s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
768 data->frames_stats[f_idx].stats[s] += device_data->frame_stats.stats[s] + data->frame_stats.stats[s];
769 data->accumulated_stats.stats[s] += device_data->frame_stats.stats[s] + data->frame_stats.stats[s];
770 }
771
772 if (data->last_fps_update) {
773 double elapsed = (double)(now - data->last_fps_update); /* us */
774 if (elapsed >= instance_data->params.fps_sampling_period) {
775 data->fps = 1000000.0f * data->n_frames_since_update / elapsed;
776 if (instance_data->params.output_file) {
777 if (!instance_data->first_line_printed) {
778 bool first_column = true;
779
780 instance_data->first_line_printed = true;
781
782 #define OVERLAY_PARAM_BOOL(name) \
783 if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_##name]) { \
784 fprintf(instance_data->params.output_file, \
785 "%s%s%s", first_column ? "" : ", ", #name, \
786 param_unit(OVERLAY_PARAM_ENABLED_##name)); \
787 first_column = false; \
788 }
789 #define OVERLAY_PARAM_CUSTOM(name)
790 OVERLAY_PARAMS
791 #undef OVERLAY_PARAM_BOOL
792 #undef OVERLAY_PARAM_CUSTOM
793 fprintf(instance_data->params.output_file, "\n");
794 }
795
796 for (int s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
797 if (!instance_data->params.enabled[s])
798 continue;
799 if (s == OVERLAY_PARAM_ENABLED_fps) {
800 fprintf(instance_data->params.output_file,
801 "%s%.2f", s == 0 ? "" : ", ", data->fps);
802 } else {
803 fprintf(instance_data->params.output_file,
804 "%s%" PRIu64, s == 0 ? "" : ", ",
805 data->accumulated_stats.stats[s]);
806 }
807 }
808 fprintf(instance_data->params.output_file, "\n");
809 fflush(instance_data->params.output_file);
810 }
811
812 memset(&data->accumulated_stats, 0, sizeof(data->accumulated_stats));
813 data->n_frames_since_update = 0;
814 data->last_fps_update = now;
815 }
816 } else {
817 data->last_fps_update = now;
818 }
819
820 memset(&device_data->frame_stats, 0, sizeof(device_data->frame_stats));
821 memset(&data->frame_stats, 0, sizeof(device_data->frame_stats));
822
823 data->last_present_time = now;
824 data->n_frames++;
825 data->n_frames_since_update++;
826 }
827
828 static float get_time_stat(void *_data, int _idx)
829 {
830 struct swapchain_data *data = (struct swapchain_data *) _data;
831 if ((ARRAY_SIZE(data->frames_stats) - _idx) > data->n_frames)
832 return 0.0f;
833 int idx = ARRAY_SIZE(data->frames_stats) +
834 data->n_frames < ARRAY_SIZE(data->frames_stats) ?
835 _idx - data->n_frames :
836 _idx + data->n_frames;
837 idx %= ARRAY_SIZE(data->frames_stats);
838 /* Time stats are in us. */
839 return data->frames_stats[idx].stats[data->stat_selector] / data->time_dividor;
840 }
841
842 static float get_stat(void *_data, int _idx)
843 {
844 struct swapchain_data *data = (struct swapchain_data *) _data;
845 if ((ARRAY_SIZE(data->frames_stats) - _idx) > data->n_frames)
846 return 0.0f;
847 int idx = ARRAY_SIZE(data->frames_stats) +
848 data->n_frames < ARRAY_SIZE(data->frames_stats) ?
849 _idx - data->n_frames :
850 _idx + data->n_frames;
851 idx %= ARRAY_SIZE(data->frames_stats);
852 return data->frames_stats[idx].stats[data->stat_selector];
853 }
854
855 static void position_layer(struct swapchain_data *data)
856
857 {
858 struct device_data *device_data = data->device;
859 struct instance_data *instance_data = device_data->instance;
860 const float margin = 10.0f;
861
862 ImGui::SetNextWindowBgAlpha(0.5);
863 ImGui::SetNextWindowSize(data->window_size, ImGuiCond_Always);
864 switch (instance_data->params.position) {
865 case LAYER_POSITION_TOP_LEFT:
866 ImGui::SetNextWindowPos(ImVec2(margin, margin), ImGuiCond_Always);
867 break;
868 case LAYER_POSITION_TOP_RIGHT:
869 ImGui::SetNextWindowPos(ImVec2(data->width - data->window_size.x - margin, margin),
870 ImGuiCond_Always);
871 break;
872 case LAYER_POSITION_BOTTOM_LEFT:
873 ImGui::SetNextWindowPos(ImVec2(margin, data->height - data->window_size.y - margin),
874 ImGuiCond_Always);
875 break;
876 case LAYER_POSITION_BOTTOM_RIGHT:
877 ImGui::SetNextWindowPos(ImVec2(data->width - data->window_size.x - margin,
878 data->height - data->window_size.y - margin),
879 ImGuiCond_Always);
880 break;
881 }
882 }
883
884 static void compute_swapchain_display(struct swapchain_data *data)
885 {
886 struct device_data *device_data = data->device;
887 struct instance_data *instance_data = device_data->instance;
888
889 ImGui::SetCurrentContext(data->imgui_context);
890 ImGui::NewFrame();
891 position_layer(data);
892 ImGui::Begin("Mesa overlay");
893 ImGui::Text("Device: %s", device_data->properties.deviceName);
894
895 const char *format_name = vk_Format_to_str(data->format);
896 format_name = format_name ? (format_name + strlen("VK_FORMAT_")) : "unknown";
897 ImGui::Text("Swapchain format: %s", format_name);
898 ImGui::Text("Frames: %" PRIu64, data->n_frames);
899 if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_fps])
900 ImGui::Text("FPS: %.2f" , data->fps);
901
902 /* Recompute min/max */
903 for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
904 data->stats_min.stats[s] = UINT64_MAX;
905 data->stats_max.stats[s] = 0;
906 }
907 for (uint32_t f = 0; f < MIN2(data->n_frames, ARRAY_SIZE(data->frames_stats)); f++) {
908 for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
909 data->stats_min.stats[s] = MIN2(data->frames_stats[f].stats[s],
910 data->stats_min.stats[s]);
911 data->stats_max.stats[s] = MAX2(data->frames_stats[f].stats[s],
912 data->stats_max.stats[s]);
913 }
914 }
915 for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
916 assert(data->stats_min.stats[s] != UINT64_MAX);
917 }
918
919 for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
920 if (!instance_data->params.enabled[s] ||
921 s == OVERLAY_PARAM_ENABLED_fps ||
922 s == OVERLAY_PARAM_ENABLED_frame)
923 continue;
924
925 char hash[40];
926 snprintf(hash, sizeof(hash), "##%s", overlay_param_names[s]);
927 data->stat_selector = (enum overlay_param_enabled) s;
928 data->time_dividor = 1000.0f;
929 if (s == OVERLAY_PARAM_ENABLED_gpu_timing)
930 data->time_dividor = 1000000.0f;
931
932 if (s == OVERLAY_PARAM_ENABLED_frame_timing ||
933 s == OVERLAY_PARAM_ENABLED_acquire_timing ||
934 s == OVERLAY_PARAM_ENABLED_present_timing ||
935 s == OVERLAY_PARAM_ENABLED_gpu_timing) {
936 double min_time = data->stats_min.stats[s] / data->time_dividor;
937 double max_time = data->stats_max.stats[s] / data->time_dividor;
938 ImGui::PlotHistogram(hash, get_time_stat, data,
939 ARRAY_SIZE(data->frames_stats), 0,
940 NULL, min_time, max_time,
941 ImVec2(ImGui::GetContentRegionAvailWidth(), 30));
942 ImGui::Text("%s: %.3fms [%.3f, %.3f]", overlay_param_names[s],
943 get_time_stat(data, ARRAY_SIZE(data->frames_stats) - 1),
944 min_time, max_time);
945 } else {
946 ImGui::PlotHistogram(hash, get_stat, data,
947 ARRAY_SIZE(data->frames_stats), 0,
948 NULL,
949 data->stats_min.stats[s],
950 data->stats_max.stats[s],
951 ImVec2(ImGui::GetContentRegionAvailWidth(), 30));
952 ImGui::Text("%s: %.0f [%" PRIu64 ", %" PRIu64 "]", overlay_param_names[s],
953 get_stat(data, ARRAY_SIZE(data->frames_stats) - 1),
954 data->stats_min.stats[s], data->stats_max.stats[s]);
955 }
956 }
957 data->window_size = ImVec2(data->window_size.x, ImGui::GetCursorPosY() + 10.0f);
958 ImGui::End();
959 ImGui::EndFrame();
960 ImGui::Render();
961 }
962
963 static uint32_t vk_memory_type(struct device_data *data,
964 VkMemoryPropertyFlags properties,
965 uint32_t type_bits)
966 {
967 VkPhysicalDeviceMemoryProperties prop;
968 data->instance->vtable.GetPhysicalDeviceMemoryProperties(data->physical_device, &prop);
969 for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
970 if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
971 return i;
972 return 0xFFFFFFFF; // Unable to find memoryType
973 }
974
975 static void ensure_swapchain_fonts(struct swapchain_data *data,
976 VkCommandBuffer command_buffer)
977 {
978 if (data->font_uploaded)
979 return;
980
981 data->font_uploaded = true;
982
983 struct device_data *device_data = data->device;
984 ImGuiIO& io = ImGui::GetIO();
985 unsigned char* pixels;
986 int width, height;
987 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
988 size_t upload_size = width * height * 4 * sizeof(char);
989
990 /* Upload buffer */
991 VkBufferCreateInfo buffer_info = {};
992 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
993 buffer_info.size = upload_size;
994 buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
995 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
996 VK_CHECK(device_data->vtable.CreateBuffer(device_data->device, &buffer_info,
997 NULL, &data->upload_font_buffer));
998 VkMemoryRequirements upload_buffer_req;
999 device_data->vtable.GetBufferMemoryRequirements(device_data->device,
1000 data->upload_font_buffer,
1001 &upload_buffer_req);
1002 VkMemoryAllocateInfo upload_alloc_info = {};
1003 upload_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1004 upload_alloc_info.allocationSize = upload_buffer_req.size;
1005 upload_alloc_info.memoryTypeIndex = vk_memory_type(device_data,
1006 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1007 upload_buffer_req.memoryTypeBits);
1008 VK_CHECK(device_data->vtable.AllocateMemory(device_data->device,
1009 &upload_alloc_info,
1010 NULL,
1011 &data->upload_font_buffer_mem));
1012 VK_CHECK(device_data->vtable.BindBufferMemory(device_data->device,
1013 data->upload_font_buffer,
1014 data->upload_font_buffer_mem, 0));
1015
1016 /* Upload to Buffer */
1017 char* map = NULL;
1018 VK_CHECK(device_data->vtable.MapMemory(device_data->device,
1019 data->upload_font_buffer_mem,
1020 0, upload_size, 0, (void**)(&map)));
1021 memcpy(map, pixels, upload_size);
1022 VkMappedMemoryRange range[1] = {};
1023 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1024 range[0].memory = data->upload_font_buffer_mem;
1025 range[0].size = upload_size;
1026 VK_CHECK(device_data->vtable.FlushMappedMemoryRanges(device_data->device, 1, range));
1027 device_data->vtable.UnmapMemory(device_data->device,
1028 data->upload_font_buffer_mem);
1029
1030 /* Copy buffer to image */
1031 VkImageMemoryBarrier copy_barrier[1] = {};
1032 copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1033 copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1034 copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1035 copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1036 copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1037 copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1038 copy_barrier[0].image = data->font_image;
1039 copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1040 copy_barrier[0].subresourceRange.levelCount = 1;
1041 copy_barrier[0].subresourceRange.layerCount = 1;
1042 device_data->vtable.CmdPipelineBarrier(command_buffer,
1043 VK_PIPELINE_STAGE_HOST_BIT,
1044 VK_PIPELINE_STAGE_TRANSFER_BIT,
1045 0, 0, NULL, 0, NULL,
1046 1, copy_barrier);
1047
1048 VkBufferImageCopy region = {};
1049 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1050 region.imageSubresource.layerCount = 1;
1051 region.imageExtent.width = width;
1052 region.imageExtent.height = height;
1053 region.imageExtent.depth = 1;
1054 device_data->vtable.CmdCopyBufferToImage(command_buffer,
1055 data->upload_font_buffer,
1056 data->font_image,
1057 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1058 1, &region);
1059
1060 VkImageMemoryBarrier use_barrier[1] = {};
1061 use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1062 use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1063 use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1064 use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1065 use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1066 use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1067 use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1068 use_barrier[0].image = data->font_image;
1069 use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1070 use_barrier[0].subresourceRange.levelCount = 1;
1071 use_barrier[0].subresourceRange.layerCount = 1;
1072 device_data->vtable.CmdPipelineBarrier(command_buffer,
1073 VK_PIPELINE_STAGE_TRANSFER_BIT,
1074 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
1075 0,
1076 0, NULL,
1077 0, NULL,
1078 1, use_barrier);
1079
1080 /* Store our identifier */
1081 io.Fonts->TexID = (ImTextureID)(intptr_t)data->font_image;
1082 }
1083
1084 static void CreateOrResizeBuffer(struct device_data *data,
1085 VkBuffer *buffer,
1086 VkDeviceMemory *buffer_memory,
1087 VkDeviceSize *buffer_size,
1088 size_t new_size, VkBufferUsageFlagBits usage)
1089 {
1090 if (*buffer != VK_NULL_HANDLE)
1091 data->vtable.DestroyBuffer(data->device, *buffer, NULL);
1092 if (*buffer_memory)
1093 data->vtable.FreeMemory(data->device, *buffer_memory, NULL);
1094
1095 VkBufferCreateInfo buffer_info = {};
1096 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1097 buffer_info.size = new_size;
1098 buffer_info.usage = usage;
1099 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1100 VK_CHECK(data->vtable.CreateBuffer(data->device, &buffer_info, NULL, buffer));
1101
1102 VkMemoryRequirements req;
1103 data->vtable.GetBufferMemoryRequirements(data->device, *buffer, &req);
1104 VkMemoryAllocateInfo alloc_info = {};
1105 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1106 alloc_info.allocationSize = req.size;
1107 alloc_info.memoryTypeIndex =
1108 vk_memory_type(data, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
1109 VK_CHECK(data->vtable.AllocateMemory(data->device, &alloc_info, NULL, buffer_memory));
1110
1111 VK_CHECK(data->vtable.BindBufferMemory(data->device, *buffer, *buffer_memory, 0));
1112 *buffer_size = new_size;
1113 }
1114
1115 static struct overlay_draw *render_swapchain_display(struct swapchain_data *data,
1116 struct queue_data *present_queue,
1117 const VkSemaphore *wait_semaphores,
1118 unsigned n_wait_semaphores,
1119 unsigned image_index)
1120 {
1121 ImDrawData* draw_data = ImGui::GetDrawData();
1122 if (draw_data->TotalVtxCount == 0)
1123 return NULL;
1124
1125 struct device_data *device_data = data->device;
1126 struct overlay_draw *draw = get_overlay_draw(data);
1127
1128 device_data->vtable.ResetCommandBuffer(draw->command_buffer, 0);
1129
1130 VkRenderPassBeginInfo render_pass_info = {};
1131 render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1132 render_pass_info.renderPass = data->render_pass;
1133 render_pass_info.framebuffer = data->framebuffers[image_index];
1134 render_pass_info.renderArea.extent.width = data->width;
1135 render_pass_info.renderArea.extent.height = data->height;
1136
1137 VkCommandBufferBeginInfo buffer_begin_info = {};
1138 buffer_begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1139
1140 device_data->vtable.BeginCommandBuffer(draw->command_buffer, &buffer_begin_info);
1141
1142 ensure_swapchain_fonts(data, draw->command_buffer);
1143
1144 /* Bounce the image to display back to color attachment layout for
1145 * rendering on top of it.
1146 */
1147 VkImageMemoryBarrier imb;
1148 imb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1149 imb.pNext = nullptr;
1150 imb.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1151 imb.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1152 imb.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
1153 imb.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1154 imb.image = data->images[image_index];
1155 imb.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1156 imb.subresourceRange.baseMipLevel = 0;
1157 imb.subresourceRange.levelCount = 1;
1158 imb.subresourceRange.baseArrayLayer = 0;
1159 imb.subresourceRange.layerCount = 1;
1160 imb.srcQueueFamilyIndex = present_queue->family_index;
1161 imb.dstQueueFamilyIndex = device_data->graphic_queue->family_index;
1162 device_data->vtable.CmdPipelineBarrier(draw->command_buffer,
1163 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1164 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1165 0, /* dependency flags */
1166 0, nullptr, /* memory barriers */
1167 0, nullptr, /* buffer memory barriers */
1168 1, &imb); /* image memory barriers */
1169
1170 device_data->vtable.CmdBeginRenderPass(draw->command_buffer, &render_pass_info,
1171 VK_SUBPASS_CONTENTS_INLINE);
1172
1173 /* Create/Resize vertex & index buffers */
1174 size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
1175 size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
1176 if (draw->vertex_buffer_size < vertex_size) {
1177 CreateOrResizeBuffer(device_data,
1178 &draw->vertex_buffer,
1179 &draw->vertex_buffer_mem,
1180 &draw->vertex_buffer_size,
1181 vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
1182 }
1183 if (draw->index_buffer_size < index_size) {
1184 CreateOrResizeBuffer(device_data,
1185 &draw->index_buffer,
1186 &draw->index_buffer_mem,
1187 &draw->index_buffer_size,
1188 index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
1189 }
1190
1191 /* Upload vertex & index data */
1192 ImDrawVert* vtx_dst = NULL;
1193 ImDrawIdx* idx_dst = NULL;
1194 VK_CHECK(device_data->vtable.MapMemory(device_data->device, draw->vertex_buffer_mem,
1195 0, vertex_size, 0, (void**)(&vtx_dst)));
1196 VK_CHECK(device_data->vtable.MapMemory(device_data->device, draw->index_buffer_mem,
1197 0, index_size, 0, (void**)(&idx_dst)));
1198 for (int n = 0; n < draw_data->CmdListsCount; n++)
1199 {
1200 const ImDrawList* cmd_list = draw_data->CmdLists[n];
1201 memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
1202 memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
1203 vtx_dst += cmd_list->VtxBuffer.Size;
1204 idx_dst += cmd_list->IdxBuffer.Size;
1205 }
1206 VkMappedMemoryRange range[2] = {};
1207 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1208 range[0].memory = draw->vertex_buffer_mem;
1209 range[0].size = VK_WHOLE_SIZE;
1210 range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1211 range[1].memory = draw->index_buffer_mem;
1212 range[1].size = VK_WHOLE_SIZE;
1213 VK_CHECK(device_data->vtable.FlushMappedMemoryRanges(device_data->device, 2, range));
1214 device_data->vtable.UnmapMemory(device_data->device, draw->vertex_buffer_mem);
1215 device_data->vtable.UnmapMemory(device_data->device, draw->index_buffer_mem);
1216
1217 /* Bind pipeline and descriptor sets */
1218 device_data->vtable.CmdBindPipeline(draw->command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, data->pipeline);
1219 VkDescriptorSet desc_set[1] = { data->descriptor_set };
1220 device_data->vtable.CmdBindDescriptorSets(draw->command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
1221 data->pipeline_layout, 0, 1, desc_set, 0, NULL);
1222
1223 /* Bind vertex & index buffers */
1224 VkBuffer vertex_buffers[1] = { draw->vertex_buffer };
1225 VkDeviceSize vertex_offset[1] = { 0 };
1226 device_data->vtable.CmdBindVertexBuffers(draw->command_buffer, 0, 1, vertex_buffers, vertex_offset);
1227 device_data->vtable.CmdBindIndexBuffer(draw->command_buffer, draw->index_buffer, 0, VK_INDEX_TYPE_UINT16);
1228
1229 /* Setup viewport */
1230 VkViewport viewport;
1231 viewport.x = 0;
1232 viewport.y = 0;
1233 viewport.width = draw_data->DisplaySize.x;
1234 viewport.height = draw_data->DisplaySize.y;
1235 viewport.minDepth = 0.0f;
1236 viewport.maxDepth = 1.0f;
1237 device_data->vtable.CmdSetViewport(draw->command_buffer, 0, 1, &viewport);
1238
1239
1240 /* Setup scale and translation through push constants :
1241 *
1242 * Our visible imgui space lies from draw_data->DisplayPos (top left) to
1243 * draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin
1244 * is typically (0,0) for single viewport apps.
1245 */
1246 float scale[2];
1247 scale[0] = 2.0f / draw_data->DisplaySize.x;
1248 scale[1] = 2.0f / draw_data->DisplaySize.y;
1249 float translate[2];
1250 translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
1251 translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
1252 device_data->vtable.CmdPushConstants(draw->command_buffer, data->pipeline_layout,
1253 VK_SHADER_STAGE_VERTEX_BIT,
1254 sizeof(float) * 0, sizeof(float) * 2, scale);
1255 device_data->vtable.CmdPushConstants(draw->command_buffer, data->pipeline_layout,
1256 VK_SHADER_STAGE_VERTEX_BIT,
1257 sizeof(float) * 2, sizeof(float) * 2, translate);
1258
1259 // Render the command lists:
1260 int vtx_offset = 0;
1261 int idx_offset = 0;
1262 ImVec2 display_pos = draw_data->DisplayPos;
1263 for (int n = 0; n < draw_data->CmdListsCount; n++)
1264 {
1265 const ImDrawList* cmd_list = draw_data->CmdLists[n];
1266 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
1267 {
1268 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
1269 // Apply scissor/clipping rectangle
1270 // FIXME: We could clamp width/height based on clamped min/max values.
1271 VkRect2D scissor;
1272 scissor.offset.x = (int32_t)(pcmd->ClipRect.x - display_pos.x) > 0 ? (int32_t)(pcmd->ClipRect.x - display_pos.x) : 0;
1273 scissor.offset.y = (int32_t)(pcmd->ClipRect.y - display_pos.y) > 0 ? (int32_t)(pcmd->ClipRect.y - display_pos.y) : 0;
1274 scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
1275 scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
1276 device_data->vtable.CmdSetScissor(draw->command_buffer, 0, 1, &scissor);
1277
1278 // Draw
1279 device_data->vtable.CmdDrawIndexed(draw->command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
1280
1281 idx_offset += pcmd->ElemCount;
1282 }
1283 vtx_offset += cmd_list->VtxBuffer.Size;
1284 }
1285
1286 device_data->vtable.CmdEndRenderPass(draw->command_buffer);
1287
1288 /* Bounce the image to display back to present layout. */
1289 imb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1290 imb.pNext = nullptr;
1291 imb.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1292 imb.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1293 imb.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1294 imb.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
1295 imb.image = data->images[image_index];
1296 imb.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1297 imb.subresourceRange.baseMipLevel = 0;
1298 imb.subresourceRange.levelCount = 1;
1299 imb.subresourceRange.baseArrayLayer = 0;
1300 imb.subresourceRange.layerCount = 1;
1301 imb.srcQueueFamilyIndex = device_data->graphic_queue->family_index;
1302 imb.dstQueueFamilyIndex = present_queue->family_index;
1303 device_data->vtable.CmdPipelineBarrier(draw->command_buffer,
1304 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1305 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1306 0, /* dependency flags */
1307 0, nullptr, /* memory barriers */
1308 0, nullptr, /* buffer memory barriers */
1309 1, &imb); /* image memory barriers */
1310
1311 device_data->vtable.EndCommandBuffer(draw->command_buffer);
1312
1313 VkSubmitInfo submit_info = {};
1314 VkPipelineStageFlags stage_wait = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1315 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1316 submit_info.commandBufferCount = 1;
1317 submit_info.pCommandBuffers = &draw->command_buffer;
1318 submit_info.pWaitDstStageMask = &stage_wait;
1319 submit_info.waitSemaphoreCount = n_wait_semaphores;
1320 submit_info.pWaitSemaphores = wait_semaphores;
1321 submit_info.signalSemaphoreCount = 1;
1322 submit_info.pSignalSemaphores = &draw->semaphore;
1323
1324 device_data->vtable.QueueSubmit(device_data->graphic_queue->queue, 1, &submit_info, draw->fence);
1325
1326 return draw;
1327 }
1328
1329 static const uint32_t overlay_vert_spv[] = {
1330 #include "overlay.vert.spv.h"
1331 };
1332 static const uint32_t overlay_frag_spv[] = {
1333 #include "overlay.frag.spv.h"
1334 };
1335
1336 static void setup_swapchain_data_pipeline(struct swapchain_data *data)
1337 {
1338 struct device_data *device_data = data->device;
1339 VkShaderModule vert_module, frag_module;
1340
1341 /* Create shader modules */
1342 VkShaderModuleCreateInfo vert_info = {};
1343 vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1344 vert_info.codeSize = sizeof(overlay_vert_spv);
1345 vert_info.pCode = overlay_vert_spv;
1346 VK_CHECK(device_data->vtable.CreateShaderModule(device_data->device,
1347 &vert_info, NULL, &vert_module));
1348 VkShaderModuleCreateInfo frag_info = {};
1349 frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1350 frag_info.codeSize = sizeof(overlay_frag_spv);
1351 frag_info.pCode = (uint32_t*)overlay_frag_spv;
1352 VK_CHECK(device_data->vtable.CreateShaderModule(device_data->device,
1353 &frag_info, NULL, &frag_module));
1354
1355 /* Font sampler */
1356 VkSamplerCreateInfo sampler_info = {};
1357 sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1358 sampler_info.magFilter = VK_FILTER_LINEAR;
1359 sampler_info.minFilter = VK_FILTER_LINEAR;
1360 sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
1361 sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1362 sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1363 sampler_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1364 sampler_info.minLod = -1000;
1365 sampler_info.maxLod = 1000;
1366 sampler_info.maxAnisotropy = 1.0f;
1367 VK_CHECK(device_data->vtable.CreateSampler(device_data->device, &sampler_info,
1368 NULL, &data->font_sampler));
1369
1370 /* Descriptor pool */
1371 VkDescriptorPoolSize sampler_pool_size = {};
1372 sampler_pool_size.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1373 sampler_pool_size.descriptorCount = 1;
1374 VkDescriptorPoolCreateInfo desc_pool_info = {};
1375 desc_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1376 desc_pool_info.maxSets = 1;
1377 desc_pool_info.poolSizeCount = 1;
1378 desc_pool_info.pPoolSizes = &sampler_pool_size;
1379 VK_CHECK(device_data->vtable.CreateDescriptorPool(device_data->device,
1380 &desc_pool_info,
1381 NULL, &data->descriptor_pool));
1382
1383 /* Descriptor layout */
1384 VkSampler sampler[1] = { data->font_sampler };
1385 VkDescriptorSetLayoutBinding binding[1] = {};
1386 binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1387 binding[0].descriptorCount = 1;
1388 binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
1389 binding[0].pImmutableSamplers = sampler;
1390 VkDescriptorSetLayoutCreateInfo set_layout_info = {};
1391 set_layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
1392 set_layout_info.bindingCount = 1;
1393 set_layout_info.pBindings = binding;
1394 VK_CHECK(device_data->vtable.CreateDescriptorSetLayout(device_data->device,
1395 &set_layout_info,
1396 NULL, &data->descriptor_layout));
1397
1398 /* Descriptor set */
1399 VkDescriptorSetAllocateInfo alloc_info = {};
1400 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1401 alloc_info.descriptorPool = data->descriptor_pool;
1402 alloc_info.descriptorSetCount = 1;
1403 alloc_info.pSetLayouts = &data->descriptor_layout;
1404 VK_CHECK(device_data->vtable.AllocateDescriptorSets(device_data->device,
1405 &alloc_info,
1406 &data->descriptor_set));
1407
1408 /* Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full
1409 * 3d projection matrix
1410 */
1411 VkPushConstantRange push_constants[1] = {};
1412 push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
1413 push_constants[0].offset = sizeof(float) * 0;
1414 push_constants[0].size = sizeof(float) * 4;
1415 VkPipelineLayoutCreateInfo layout_info = {};
1416 layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
1417 layout_info.setLayoutCount = 1;
1418 layout_info.pSetLayouts = &data->descriptor_layout;
1419 layout_info.pushConstantRangeCount = 1;
1420 layout_info.pPushConstantRanges = push_constants;
1421 VK_CHECK(device_data->vtable.CreatePipelineLayout(device_data->device,
1422 &layout_info,
1423 NULL, &data->pipeline_layout));
1424
1425 VkPipelineShaderStageCreateInfo stage[2] = {};
1426 stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1427 stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1428 stage[0].module = vert_module;
1429 stage[0].pName = "main";
1430 stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1431 stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1432 stage[1].module = frag_module;
1433 stage[1].pName = "main";
1434
1435 VkVertexInputBindingDescription binding_desc[1] = {};
1436 binding_desc[0].stride = sizeof(ImDrawVert);
1437 binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
1438
1439 VkVertexInputAttributeDescription attribute_desc[3] = {};
1440 attribute_desc[0].location = 0;
1441 attribute_desc[0].binding = binding_desc[0].binding;
1442 attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
1443 attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
1444 attribute_desc[1].location = 1;
1445 attribute_desc[1].binding = binding_desc[0].binding;
1446 attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
1447 attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
1448 attribute_desc[2].location = 2;
1449 attribute_desc[2].binding = binding_desc[0].binding;
1450 attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
1451 attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
1452
1453 VkPipelineVertexInputStateCreateInfo vertex_info = {};
1454 vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1455 vertex_info.vertexBindingDescriptionCount = 1;
1456 vertex_info.pVertexBindingDescriptions = binding_desc;
1457 vertex_info.vertexAttributeDescriptionCount = 3;
1458 vertex_info.pVertexAttributeDescriptions = attribute_desc;
1459
1460 VkPipelineInputAssemblyStateCreateInfo ia_info = {};
1461 ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
1462 ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1463
1464 VkPipelineViewportStateCreateInfo viewport_info = {};
1465 viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
1466 viewport_info.viewportCount = 1;
1467 viewport_info.scissorCount = 1;
1468
1469 VkPipelineRasterizationStateCreateInfo raster_info = {};
1470 raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1471 raster_info.polygonMode = VK_POLYGON_MODE_FILL;
1472 raster_info.cullMode = VK_CULL_MODE_NONE;
1473 raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
1474 raster_info.lineWidth = 1.0f;
1475
1476 VkPipelineMultisampleStateCreateInfo ms_info = {};
1477 ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
1478 ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
1479
1480 VkPipelineColorBlendAttachmentState color_attachment[1] = {};
1481 color_attachment[0].blendEnable = VK_TRUE;
1482 color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
1483 color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
1484 color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
1485 color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
1486 color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
1487 color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
1488 color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
1489 VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
1490
1491 VkPipelineDepthStencilStateCreateInfo depth_info = {};
1492 depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
1493
1494 VkPipelineColorBlendStateCreateInfo blend_info = {};
1495 blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1496 blend_info.attachmentCount = 1;
1497 blend_info.pAttachments = color_attachment;
1498
1499 VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
1500 VkPipelineDynamicStateCreateInfo dynamic_state = {};
1501 dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1502 dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
1503 dynamic_state.pDynamicStates = dynamic_states;
1504
1505 VkGraphicsPipelineCreateInfo info = {};
1506 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1507 info.flags = 0;
1508 info.stageCount = 2;
1509 info.pStages = stage;
1510 info.pVertexInputState = &vertex_info;
1511 info.pInputAssemblyState = &ia_info;
1512 info.pViewportState = &viewport_info;
1513 info.pRasterizationState = &raster_info;
1514 info.pMultisampleState = &ms_info;
1515 info.pDepthStencilState = &depth_info;
1516 info.pColorBlendState = &blend_info;
1517 info.pDynamicState = &dynamic_state;
1518 info.layout = data->pipeline_layout;
1519 info.renderPass = data->render_pass;
1520 VK_CHECK(
1521 device_data->vtable.CreateGraphicsPipelines(device_data->device, VK_NULL_HANDLE,
1522 1, &info,
1523 NULL, &data->pipeline));
1524
1525 device_data->vtable.DestroyShaderModule(device_data->device, vert_module, NULL);
1526 device_data->vtable.DestroyShaderModule(device_data->device, frag_module, NULL);
1527
1528 ImGuiIO& io = ImGui::GetIO();
1529 unsigned char* pixels;
1530 int width, height;
1531 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
1532
1533 /* Font image */
1534 VkImageCreateInfo image_info = {};
1535 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1536 image_info.imageType = VK_IMAGE_TYPE_2D;
1537 image_info.format = VK_FORMAT_R8G8B8A8_UNORM;
1538 image_info.extent.width = width;
1539 image_info.extent.height = height;
1540 image_info.extent.depth = 1;
1541 image_info.mipLevels = 1;
1542 image_info.arrayLayers = 1;
1543 image_info.samples = VK_SAMPLE_COUNT_1_BIT;
1544 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
1545 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1546 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1547 image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1548 VK_CHECK(device_data->vtable.CreateImage(device_data->device, &image_info,
1549 NULL, &data->font_image));
1550 VkMemoryRequirements font_image_req;
1551 device_data->vtable.GetImageMemoryRequirements(device_data->device,
1552 data->font_image, &font_image_req);
1553 VkMemoryAllocateInfo image_alloc_info = {};
1554 image_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1555 image_alloc_info.allocationSize = font_image_req.size;
1556 image_alloc_info.memoryTypeIndex = vk_memory_type(device_data,
1557 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1558 font_image_req.memoryTypeBits);
1559 VK_CHECK(device_data->vtable.AllocateMemory(device_data->device, &image_alloc_info,
1560 NULL, &data->font_mem));
1561 VK_CHECK(device_data->vtable.BindImageMemory(device_data->device,
1562 data->font_image,
1563 data->font_mem, 0));
1564
1565 /* Font image view */
1566 VkImageViewCreateInfo view_info = {};
1567 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1568 view_info.image = data->font_image;
1569 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
1570 view_info.format = VK_FORMAT_R8G8B8A8_UNORM;
1571 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1572 view_info.subresourceRange.levelCount = 1;
1573 view_info.subresourceRange.layerCount = 1;
1574 VK_CHECK(device_data->vtable.CreateImageView(device_data->device, &view_info,
1575 NULL, &data->font_image_view));
1576
1577 /* Descriptor set */
1578 VkDescriptorImageInfo desc_image[1] = {};
1579 desc_image[0].sampler = data->font_sampler;
1580 desc_image[0].imageView = data->font_image_view;
1581 desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1582 VkWriteDescriptorSet write_desc[1] = {};
1583 write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1584 write_desc[0].dstSet = data->descriptor_set;
1585 write_desc[0].descriptorCount = 1;
1586 write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1587 write_desc[0].pImageInfo = desc_image;
1588 device_data->vtable.UpdateDescriptorSets(device_data->device, 1, write_desc, 0, NULL);
1589 }
1590
1591 static void setup_swapchain_data(struct swapchain_data *data,
1592 const VkSwapchainCreateInfoKHR *pCreateInfo)
1593 {
1594 data->width = pCreateInfo->imageExtent.width;
1595 data->height = pCreateInfo->imageExtent.height;
1596 data->format = pCreateInfo->imageFormat;
1597
1598 data->imgui_context = ImGui::CreateContext();
1599 ImGui::SetCurrentContext(data->imgui_context);
1600
1601 ImGui::GetIO().IniFilename = NULL;
1602 ImGui::GetIO().DisplaySize = ImVec2((float)data->width, (float)data->height);
1603
1604 struct device_data *device_data = data->device;
1605
1606 /* Render pass */
1607 VkAttachmentDescription attachment_desc = {};
1608 attachment_desc.format = pCreateInfo->imageFormat;
1609 attachment_desc.samples = VK_SAMPLE_COUNT_1_BIT;
1610 attachment_desc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1611 attachment_desc.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1612 attachment_desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1613 attachment_desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1614 attachment_desc.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1615 attachment_desc.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
1616 VkAttachmentReference color_attachment = {};
1617 color_attachment.attachment = 0;
1618 color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1619 VkSubpassDescription subpass = {};
1620 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1621 subpass.colorAttachmentCount = 1;
1622 subpass.pColorAttachments = &color_attachment;
1623 VkSubpassDependency dependency = {};
1624 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
1625 dependency.dstSubpass = 0;
1626 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1627 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1628 dependency.srcAccessMask = 0;
1629 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1630 VkRenderPassCreateInfo render_pass_info = {};
1631 render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1632 render_pass_info.attachmentCount = 1;
1633 render_pass_info.pAttachments = &attachment_desc;
1634 render_pass_info.subpassCount = 1;
1635 render_pass_info.pSubpasses = &subpass;
1636 render_pass_info.dependencyCount = 1;
1637 render_pass_info.pDependencies = &dependency;
1638 VK_CHECK(device_data->vtable.CreateRenderPass(device_data->device,
1639 &render_pass_info,
1640 NULL, &data->render_pass));
1641
1642 setup_swapchain_data_pipeline(data);
1643
1644 VK_CHECK(device_data->vtable.GetSwapchainImagesKHR(device_data->device,
1645 data->swapchain,
1646 &data->n_images,
1647 NULL));
1648
1649 data->images = ralloc_array(data, VkImage, data->n_images);
1650 data->image_views = ralloc_array(data, VkImageView, data->n_images);
1651 data->framebuffers = ralloc_array(data, VkFramebuffer, data->n_images);
1652
1653 VK_CHECK(device_data->vtable.GetSwapchainImagesKHR(device_data->device,
1654 data->swapchain,
1655 &data->n_images,
1656 data->images));
1657
1658 /* Image views */
1659 VkImageViewCreateInfo view_info = {};
1660 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1661 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
1662 view_info.format = pCreateInfo->imageFormat;
1663 view_info.components.r = VK_COMPONENT_SWIZZLE_R;
1664 view_info.components.g = VK_COMPONENT_SWIZZLE_G;
1665 view_info.components.b = VK_COMPONENT_SWIZZLE_B;
1666 view_info.components.a = VK_COMPONENT_SWIZZLE_A;
1667 view_info.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
1668 for (uint32_t i = 0; i < data->n_images; i++) {
1669 view_info.image = data->images[i];
1670 VK_CHECK(device_data->vtable.CreateImageView(device_data->device,
1671 &view_info, NULL,
1672 &data->image_views[i]));
1673 }
1674
1675 /* Framebuffers */
1676 VkImageView attachment[1];
1677 VkFramebufferCreateInfo fb_info = {};
1678 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1679 fb_info.renderPass = data->render_pass;
1680 fb_info.attachmentCount = 1;
1681 fb_info.pAttachments = attachment;
1682 fb_info.width = data->width;
1683 fb_info.height = data->height;
1684 fb_info.layers = 1;
1685 for (uint32_t i = 0; i < data->n_images; i++) {
1686 attachment[0] = data->image_views[i];
1687 VK_CHECK(device_data->vtable.CreateFramebuffer(device_data->device, &fb_info,
1688 NULL, &data->framebuffers[i]));
1689 }
1690
1691 /* Command buffer pool */
1692 VkCommandPoolCreateInfo cmd_buffer_pool_info = {};
1693 cmd_buffer_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1694 cmd_buffer_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
1695 cmd_buffer_pool_info.queueFamilyIndex = device_data->graphic_queue->family_index;
1696 VK_CHECK(device_data->vtable.CreateCommandPool(device_data->device,
1697 &cmd_buffer_pool_info,
1698 NULL, &data->command_pool));
1699 }
1700
1701 static void shutdown_swapchain_data(struct swapchain_data *data)
1702 {
1703 struct device_data *device_data = data->device;
1704
1705 list_for_each_entry_safe(struct overlay_draw, draw, &data->draws, link) {
1706 device_data->vtable.DestroySemaphore(device_data->device, draw->semaphore, NULL);
1707 device_data->vtable.DestroyFence(device_data->device, draw->fence, NULL);
1708 device_data->vtable.DestroyBuffer(device_data->device, draw->vertex_buffer, NULL);
1709 device_data->vtable.DestroyBuffer(device_data->device, draw->index_buffer, NULL);
1710 device_data->vtable.FreeMemory(device_data->device, draw->vertex_buffer_mem, NULL);
1711 device_data->vtable.FreeMemory(device_data->device, draw->index_buffer_mem, NULL);
1712 }
1713
1714 for (uint32_t i = 0; i < data->n_images; i++) {
1715 device_data->vtable.DestroyImageView(device_data->device, data->image_views[i], NULL);
1716 device_data->vtable.DestroyFramebuffer(device_data->device, data->framebuffers[i], NULL);
1717 }
1718
1719 device_data->vtable.DestroyRenderPass(device_data->device, data->render_pass, NULL);
1720
1721 device_data->vtable.DestroyCommandPool(device_data->device, data->command_pool, NULL);
1722
1723 device_data->vtable.DestroyPipeline(device_data->device, data->pipeline, NULL);
1724 device_data->vtable.DestroyPipelineLayout(device_data->device, data->pipeline_layout, NULL);
1725
1726 device_data->vtable.DestroyDescriptorPool(device_data->device,
1727 data->descriptor_pool, NULL);
1728 device_data->vtable.DestroyDescriptorSetLayout(device_data->device,
1729 data->descriptor_layout, NULL);
1730
1731 device_data->vtable.DestroySampler(device_data->device, data->font_sampler, NULL);
1732 device_data->vtable.DestroyImageView(device_data->device, data->font_image_view, NULL);
1733 device_data->vtable.DestroyImage(device_data->device, data->font_image, NULL);
1734 device_data->vtable.FreeMemory(device_data->device, data->font_mem, NULL);
1735
1736 device_data->vtable.DestroyBuffer(device_data->device, data->upload_font_buffer, NULL);
1737 device_data->vtable.FreeMemory(device_data->device, data->upload_font_buffer_mem, NULL);
1738
1739 ImGui::DestroyContext(data->imgui_context);
1740 }
1741
1742 static struct overlay_draw *before_present(struct swapchain_data *swapchain_data,
1743 struct queue_data *present_queue,
1744 const VkSemaphore *wait_semaphores,
1745 unsigned n_wait_semaphores,
1746 unsigned imageIndex)
1747 {
1748 struct instance_data *instance_data = swapchain_data->device->instance;
1749 struct overlay_draw *draw = NULL;
1750
1751 snapshot_swapchain_frame(swapchain_data);
1752
1753 if (!instance_data->params.no_display && swapchain_data->n_frames > 0) {
1754 compute_swapchain_display(swapchain_data);
1755 draw = render_swapchain_display(swapchain_data, present_queue,
1756 wait_semaphores, n_wait_semaphores,
1757 imageIndex);
1758 }
1759
1760 return draw;
1761 }
1762
1763 static VkResult overlay_CreateSwapchainKHR(
1764 VkDevice device,
1765 const VkSwapchainCreateInfoKHR* pCreateInfo,
1766 const VkAllocationCallbacks* pAllocator,
1767 VkSwapchainKHR* pSwapchain)
1768 {
1769 struct device_data *device_data = FIND(struct device_data, device);
1770 VkResult result = device_data->vtable.CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
1771 if (result != VK_SUCCESS) return result;
1772
1773 struct swapchain_data *swapchain_data = new_swapchain_data(*pSwapchain, device_data);
1774 setup_swapchain_data(swapchain_data, pCreateInfo);
1775 return result;
1776 }
1777
1778 static void overlay_DestroySwapchainKHR(
1779 VkDevice device,
1780 VkSwapchainKHR swapchain,
1781 const VkAllocationCallbacks* pAllocator)
1782 {
1783 struct swapchain_data *swapchain_data =
1784 FIND(struct swapchain_data, swapchain);
1785
1786 shutdown_swapchain_data(swapchain_data);
1787 swapchain_data->device->vtable.DestroySwapchainKHR(device, swapchain, pAllocator);
1788 destroy_swapchain_data(swapchain_data);
1789 }
1790
1791 static VkResult overlay_QueuePresentKHR(
1792 VkQueue queue,
1793 const VkPresentInfoKHR* pPresentInfo)
1794 {
1795 struct queue_data *queue_data = FIND(struct queue_data, queue);
1796 struct device_data *device_data = queue_data->device;
1797 struct instance_data *instance_data = device_data->instance;
1798 uint32_t query_results[OVERLAY_QUERY_COUNT];
1799
1800 device_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_frame]++;
1801
1802 if (list_length(&queue_data->running_command_buffer) > 0) {
1803 /* Before getting the query results, make sure the operations have
1804 * completed.
1805 */
1806 VK_CHECK(device_data->vtable.ResetFences(device_data->device,
1807 1, &queue_data->queries_fence));
1808 VK_CHECK(device_data->vtable.QueueSubmit(queue, 0, NULL, queue_data->queries_fence));
1809 VK_CHECK(device_data->vtable.WaitForFences(device_data->device,
1810 1, &queue_data->queries_fence,
1811 VK_FALSE, UINT64_MAX));
1812
1813 /* Now get the results. */
1814 list_for_each_entry_safe(struct command_buffer_data, cmd_buffer_data,
1815 &queue_data->running_command_buffer, link) {
1816 list_delinit(&cmd_buffer_data->link);
1817
1818 if (cmd_buffer_data->pipeline_query_pool) {
1819 memset(query_results, 0, sizeof(query_results));
1820 VK_CHECK(device_data->vtable.GetQueryPoolResults(device_data->device,
1821 cmd_buffer_data->pipeline_query_pool,
1822 cmd_buffer_data->query_index, 1,
1823 sizeof(uint32_t) * OVERLAY_QUERY_COUNT,
1824 query_results, 0, VK_QUERY_RESULT_WAIT_BIT));
1825
1826 for (uint32_t i = OVERLAY_PARAM_ENABLED_vertices;
1827 i <= OVERLAY_PARAM_ENABLED_compute_invocations; i++) {
1828 device_data->frame_stats.stats[i] += query_results[i - OVERLAY_PARAM_ENABLED_vertices];
1829 }
1830 }
1831 if (cmd_buffer_data->timestamp_query_pool) {
1832 uint64_t gpu_timestamps[2] = { 0 };
1833 VK_CHECK(device_data->vtable.GetQueryPoolResults(device_data->device,
1834 cmd_buffer_data->timestamp_query_pool,
1835 cmd_buffer_data->query_index * 2, 2,
1836 2 * sizeof(uint64_t), gpu_timestamps, sizeof(uint64_t),
1837 VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT));
1838
1839 gpu_timestamps[0] &= queue_data->timestamp_mask;
1840 gpu_timestamps[1] &= queue_data->timestamp_mask;
1841 device_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_gpu_timing] +=
1842 (gpu_timestamps[1] - gpu_timestamps[0]) *
1843 device_data->properties.limits.timestampPeriod;
1844 }
1845 }
1846 }
1847
1848 /* Otherwise we need to add our overlay drawing semaphore to the list of
1849 * semaphores to wait on. If we don't do that the presented picture might
1850 * be have incomplete overlay drawings.
1851 */
1852 VkResult result = VK_SUCCESS;
1853 if (instance_data->params.no_display) {
1854 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
1855 VkSwapchainKHR swapchain = pPresentInfo->pSwapchains[i];
1856 struct swapchain_data *swapchain_data =
1857 FIND(struct swapchain_data, swapchain);
1858
1859 before_present(swapchain_data,
1860 queue_data,
1861 pPresentInfo->pWaitSemaphores,
1862 pPresentInfo->waitSemaphoreCount,
1863 pPresentInfo->pImageIndices[i]);
1864
1865 VkPresentInfoKHR present_info = *pPresentInfo;
1866 present_info.swapchainCount = 1;
1867 present_info.pSwapchains = &swapchain;
1868
1869 uint64_t ts0 = os_time_get();
1870 result = queue_data->device->vtable.QueuePresentKHR(queue, &present_info);
1871 uint64_t ts1 = os_time_get();
1872 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_present_timing] += ts1 - ts0;
1873 }
1874 } else {
1875 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
1876 VkSwapchainKHR swapchain = pPresentInfo->pSwapchains[i];
1877 struct swapchain_data *swapchain_data =
1878 FIND(struct swapchain_data, swapchain);
1879 VkPresentInfoKHR present_info = *pPresentInfo;
1880 present_info.swapchainCount = 1;
1881 present_info.pSwapchains = &swapchain;
1882
1883 uint32_t image_index = pPresentInfo->pImageIndices[i];
1884
1885 struct overlay_draw *draw = before_present(swapchain_data,
1886 queue_data,
1887 pPresentInfo->pWaitSemaphores,
1888 pPresentInfo->waitSemaphoreCount,
1889 image_index);
1890
1891 /* Because the submission of the overlay draw waits on the semaphores
1892 * handed for present, we don't need to have this present operation
1893 * wait on them as well, we can just wait on the overlay submission
1894 * semaphore.
1895 */
1896 present_info.pWaitSemaphores = &draw->semaphore;
1897 present_info.waitSemaphoreCount = 1;
1898
1899 uint64_t ts0 = os_time_get();
1900 VkResult chain_result = queue_data->device->vtable.QueuePresentKHR(queue, &present_info);
1901 uint64_t ts1 = os_time_get();
1902 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_present_timing] += ts1 - ts0;
1903 if (pPresentInfo->pResults)
1904 pPresentInfo->pResults[i] = chain_result;
1905 if (chain_result != VK_SUCCESS && result == VK_SUCCESS)
1906 result = chain_result;
1907 }
1908 }
1909 return result;
1910 }
1911
1912 static VkResult overlay_AcquireNextImageKHR(
1913 VkDevice device,
1914 VkSwapchainKHR swapchain,
1915 uint64_t timeout,
1916 VkSemaphore semaphore,
1917 VkFence fence,
1918 uint32_t* pImageIndex)
1919 {
1920 struct swapchain_data *swapchain_data =
1921 FIND(struct swapchain_data, swapchain);
1922 struct device_data *device_data = swapchain_data->device;
1923
1924 uint64_t ts0 = os_time_get();
1925 VkResult result = device_data->vtable.AcquireNextImageKHR(device, swapchain, timeout,
1926 semaphore, fence, pImageIndex);
1927 uint64_t ts1 = os_time_get();
1928
1929 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire_timing] += ts1 - ts0;
1930 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire]++;
1931
1932 return result;
1933 }
1934
1935 static VkResult overlay_AcquireNextImage2KHR(
1936 VkDevice device,
1937 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1938 uint32_t* pImageIndex)
1939 {
1940 struct swapchain_data *swapchain_data =
1941 FIND(struct swapchain_data, pAcquireInfo->swapchain);
1942 struct device_data *device_data = swapchain_data->device;
1943
1944 uint64_t ts0 = os_time_get();
1945 VkResult result = device_data->vtable.AcquireNextImage2KHR(device, pAcquireInfo, pImageIndex);
1946 uint64_t ts1 = os_time_get();
1947
1948 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire_timing] += ts1 - ts0;
1949 swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire]++;
1950
1951 return result;
1952 }
1953
1954 static void overlay_CmdDraw(
1955 VkCommandBuffer commandBuffer,
1956 uint32_t vertexCount,
1957 uint32_t instanceCount,
1958 uint32_t firstVertex,
1959 uint32_t firstInstance)
1960 {
1961 struct command_buffer_data *cmd_buffer_data =
1962 FIND(struct command_buffer_data, commandBuffer);
1963 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw]++;
1964 struct device_data *device_data = cmd_buffer_data->device;
1965 device_data->vtable.CmdDraw(commandBuffer, vertexCount, instanceCount,
1966 firstVertex, firstInstance);
1967 }
1968
1969 static void overlay_CmdDrawIndexed(
1970 VkCommandBuffer commandBuffer,
1971 uint32_t indexCount,
1972 uint32_t instanceCount,
1973 uint32_t firstIndex,
1974 int32_t vertexOffset,
1975 uint32_t firstInstance)
1976 {
1977 struct command_buffer_data *cmd_buffer_data =
1978 FIND(struct command_buffer_data, commandBuffer);
1979 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed]++;
1980 struct device_data *device_data = cmd_buffer_data->device;
1981 device_data->vtable.CmdDrawIndexed(commandBuffer, indexCount, instanceCount,
1982 firstIndex, vertexOffset, firstInstance);
1983 }
1984
1985 static void overlay_CmdDrawIndirect(
1986 VkCommandBuffer commandBuffer,
1987 VkBuffer buffer,
1988 VkDeviceSize offset,
1989 uint32_t drawCount,
1990 uint32_t stride)
1991 {
1992 struct command_buffer_data *cmd_buffer_data =
1993 FIND(struct command_buffer_data, commandBuffer);
1994 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indirect]++;
1995 struct device_data *device_data = cmd_buffer_data->device;
1996 device_data->vtable.CmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
1997 }
1998
1999 static void overlay_CmdDrawIndexedIndirect(
2000 VkCommandBuffer commandBuffer,
2001 VkBuffer buffer,
2002 VkDeviceSize offset,
2003 uint32_t drawCount,
2004 uint32_t stride)
2005 {
2006 struct command_buffer_data *cmd_buffer_data =
2007 FIND(struct command_buffer_data, commandBuffer);
2008 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed_indirect]++;
2009 struct device_data *device_data = cmd_buffer_data->device;
2010 device_data->vtable.CmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
2011 }
2012
2013 static void overlay_CmdDrawIndirectCountKHR(
2014 VkCommandBuffer commandBuffer,
2015 VkBuffer buffer,
2016 VkDeviceSize offset,
2017 VkBuffer countBuffer,
2018 VkDeviceSize countBufferOffset,
2019 uint32_t maxDrawCount,
2020 uint32_t stride)
2021 {
2022 struct command_buffer_data *cmd_buffer_data =
2023 FIND(struct command_buffer_data, commandBuffer);
2024 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indirect_count]++;
2025 struct device_data *device_data = cmd_buffer_data->device;
2026 device_data->vtable.CmdDrawIndirectCountKHR(commandBuffer, buffer, offset,
2027 countBuffer, countBufferOffset,
2028 maxDrawCount, stride);
2029 }
2030
2031 static void overlay_CmdDrawIndexedIndirectCountKHR(
2032 VkCommandBuffer commandBuffer,
2033 VkBuffer buffer,
2034 VkDeviceSize offset,
2035 VkBuffer countBuffer,
2036 VkDeviceSize countBufferOffset,
2037 uint32_t maxDrawCount,
2038 uint32_t stride)
2039 {
2040 struct command_buffer_data *cmd_buffer_data =
2041 FIND(struct command_buffer_data, commandBuffer);
2042 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed_indirect_count]++;
2043 struct device_data *device_data = cmd_buffer_data->device;
2044 device_data->vtable.CmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset,
2045 countBuffer, countBufferOffset,
2046 maxDrawCount, stride);
2047 }
2048
2049 static void overlay_CmdDispatch(
2050 VkCommandBuffer commandBuffer,
2051 uint32_t groupCountX,
2052 uint32_t groupCountY,
2053 uint32_t groupCountZ)
2054 {
2055 struct command_buffer_data *cmd_buffer_data =
2056 FIND(struct command_buffer_data, commandBuffer);
2057 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_dispatch]++;
2058 struct device_data *device_data = cmd_buffer_data->device;
2059 device_data->vtable.CmdDispatch(commandBuffer, groupCountX, groupCountY, groupCountZ);
2060 }
2061
2062 static void overlay_CmdDispatchIndirect(
2063 VkCommandBuffer commandBuffer,
2064 VkBuffer buffer,
2065 VkDeviceSize offset)
2066 {
2067 struct command_buffer_data *cmd_buffer_data =
2068 FIND(struct command_buffer_data, commandBuffer);
2069 cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_dispatch_indirect]++;
2070 struct device_data *device_data = cmd_buffer_data->device;
2071 device_data->vtable.CmdDispatchIndirect(commandBuffer, buffer, offset);
2072 }
2073
2074 static void overlay_CmdBindPipeline(
2075 VkCommandBuffer commandBuffer,
2076 VkPipelineBindPoint pipelineBindPoint,
2077 VkPipeline pipeline)
2078 {
2079 struct command_buffer_data *cmd_buffer_data =
2080 FIND(struct command_buffer_data, commandBuffer);
2081 switch (pipelineBindPoint) {
2082 case VK_PIPELINE_BIND_POINT_GRAPHICS: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_graphics]++; break;
2083 case VK_PIPELINE_BIND_POINT_COMPUTE: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_compute]++; break;
2084 case VK_PIPELINE_BIND_POINT_RAY_TRACING_NV: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_raytracing]++; break;
2085 default: break;
2086 }
2087 struct device_data *device_data = cmd_buffer_data->device;
2088 device_data->vtable.CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
2089 }
2090
2091 static VkResult overlay_BeginCommandBuffer(
2092 VkCommandBuffer commandBuffer,
2093 const VkCommandBufferBeginInfo* pBeginInfo)
2094 {
2095 struct command_buffer_data *cmd_buffer_data =
2096 FIND(struct command_buffer_data, commandBuffer);
2097 struct device_data *device_data = cmd_buffer_data->device;
2098
2099 memset(&cmd_buffer_data->stats, 0, sizeof(cmd_buffer_data->stats));
2100
2101 /* We don't record any query in secondary command buffers, just make sure
2102 * we have the right inheritance.
2103 */
2104 if (cmd_buffer_data->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2105 VkCommandBufferBeginInfo *begin_info = (VkCommandBufferBeginInfo *)
2106 clone_chain((const struct VkBaseInStructure *)pBeginInfo);
2107 VkCommandBufferInheritanceInfo *parent_inhe_info = (VkCommandBufferInheritanceInfo *)
2108 vk_find_struct(begin_info, COMMAND_BUFFER_INHERITANCE_INFO);
2109 VkCommandBufferInheritanceInfo inhe_info = {
2110 VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
2111 NULL,
2112 VK_NULL_HANDLE,
2113 0,
2114 VK_NULL_HANDLE,
2115 VK_FALSE,
2116 0,
2117 overlay_query_flags,
2118 };
2119
2120 if (parent_inhe_info)
2121 parent_inhe_info->pipelineStatistics = overlay_query_flags;
2122 else {
2123 inhe_info.pNext = begin_info->pNext;
2124 begin_info->pNext = &inhe_info;
2125 }
2126
2127 VkResult result = device_data->vtable.BeginCommandBuffer(commandBuffer, pBeginInfo);
2128
2129 if (!parent_inhe_info)
2130 begin_info->pNext = inhe_info.pNext;
2131
2132 free_chain((struct VkBaseOutStructure *)begin_info);
2133
2134 return result;
2135 }
2136
2137 /* Otherwise record a begin query as first command. */
2138 VkResult result = device_data->vtable.BeginCommandBuffer(commandBuffer, pBeginInfo);
2139
2140 if (result == VK_SUCCESS) {
2141 if (cmd_buffer_data->pipeline_query_pool) {
2142 device_data->vtable.CmdResetQueryPool(commandBuffer,
2143 cmd_buffer_data->pipeline_query_pool,
2144 cmd_buffer_data->query_index, 1);
2145 }
2146 if (cmd_buffer_data->timestamp_query_pool) {
2147 device_data->vtable.CmdResetQueryPool(commandBuffer,
2148 cmd_buffer_data->timestamp_query_pool,
2149 cmd_buffer_data->query_index * 2, 2);
2150 }
2151 if (cmd_buffer_data->pipeline_query_pool) {
2152 device_data->vtable.CmdBeginQuery(commandBuffer,
2153 cmd_buffer_data->pipeline_query_pool,
2154 cmd_buffer_data->query_index, 0);
2155 }
2156 if (cmd_buffer_data->timestamp_query_pool) {
2157 device_data->vtable.CmdWriteTimestamp(commandBuffer,
2158 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
2159 cmd_buffer_data->timestamp_query_pool,
2160 cmd_buffer_data->query_index * 2);
2161 }
2162 }
2163
2164 return result;
2165 }
2166
2167 static VkResult overlay_EndCommandBuffer(
2168 VkCommandBuffer commandBuffer)
2169 {
2170 struct command_buffer_data *cmd_buffer_data =
2171 FIND(struct command_buffer_data, commandBuffer);
2172 struct device_data *device_data = cmd_buffer_data->device;
2173
2174 if (cmd_buffer_data->timestamp_query_pool) {
2175 device_data->vtable.CmdWriteTimestamp(commandBuffer,
2176 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
2177 cmd_buffer_data->timestamp_query_pool,
2178 cmd_buffer_data->query_index * 2 + 1);
2179 }
2180 if (cmd_buffer_data->pipeline_query_pool) {
2181 device_data->vtable.CmdEndQuery(commandBuffer,
2182 cmd_buffer_data->pipeline_query_pool,
2183 cmd_buffer_data->query_index);
2184 }
2185
2186 return device_data->vtable.EndCommandBuffer(commandBuffer);
2187 }
2188
2189 static VkResult overlay_ResetCommandBuffer(
2190 VkCommandBuffer commandBuffer,
2191 VkCommandBufferResetFlags flags)
2192 {
2193 struct command_buffer_data *cmd_buffer_data =
2194 FIND(struct command_buffer_data, commandBuffer);
2195 struct device_data *device_data = cmd_buffer_data->device;
2196
2197 memset(&cmd_buffer_data->stats, 0, sizeof(cmd_buffer_data->stats));
2198
2199 return device_data->vtable.ResetCommandBuffer(commandBuffer, flags);
2200 }
2201
2202 static void overlay_CmdExecuteCommands(
2203 VkCommandBuffer commandBuffer,
2204 uint32_t commandBufferCount,
2205 const VkCommandBuffer* pCommandBuffers)
2206 {
2207 struct command_buffer_data *cmd_buffer_data =
2208 FIND(struct command_buffer_data, commandBuffer);
2209 struct device_data *device_data = cmd_buffer_data->device;
2210
2211 /* Add the stats of the executed command buffers to the primary one. */
2212 for (uint32_t c = 0; c < commandBufferCount; c++) {
2213 struct command_buffer_data *sec_cmd_buffer_data =
2214 FIND(struct command_buffer_data, pCommandBuffers[c]);
2215
2216 for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++)
2217 cmd_buffer_data->stats.stats[s] += sec_cmd_buffer_data->stats.stats[s];
2218 }
2219
2220 device_data->vtable.CmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2221 }
2222
2223 static VkResult overlay_AllocateCommandBuffers(
2224 VkDevice device,
2225 const VkCommandBufferAllocateInfo* pAllocateInfo,
2226 VkCommandBuffer* pCommandBuffers)
2227 {
2228 struct device_data *device_data = FIND(struct device_data, device);
2229 VkResult result =
2230 device_data->vtable.AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
2231 if (result != VK_SUCCESS)
2232 return result;
2233
2234 VkQueryPool pipeline_query_pool = VK_NULL_HANDLE;
2235 VkQueryPool timestamp_query_pool = VK_NULL_HANDLE;
2236 if (device_data->instance->pipeline_statistics_enabled &&
2237 pAllocateInfo->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
2238 VkQueryPoolCreateInfo pool_info = {
2239 VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
2240 NULL,
2241 0,
2242 VK_QUERY_TYPE_PIPELINE_STATISTICS,
2243 pAllocateInfo->commandBufferCount,
2244 overlay_query_flags,
2245 };
2246 VK_CHECK(device_data->vtable.CreateQueryPool(device_data->device, &pool_info,
2247 NULL, &pipeline_query_pool));
2248 }
2249 if (device_data->instance->params.enabled[OVERLAY_PARAM_ENABLED_gpu_timing]) {
2250 VkQueryPoolCreateInfo pool_info = {
2251 VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
2252 NULL,
2253 0,
2254 VK_QUERY_TYPE_TIMESTAMP,
2255 pAllocateInfo->commandBufferCount * 2,
2256 0,
2257 };
2258 VK_CHECK(device_data->vtable.CreateQueryPool(device_data->device, &pool_info,
2259 NULL, &timestamp_query_pool));
2260 }
2261
2262 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
2263 new_command_buffer_data(pCommandBuffers[i], pAllocateInfo->level,
2264 pipeline_query_pool, timestamp_query_pool,
2265 i, device_data);
2266 }
2267
2268 if (pipeline_query_pool)
2269 map_object(HKEY(pipeline_query_pool), (void *)(uintptr_t) pAllocateInfo->commandBufferCount);
2270 if (timestamp_query_pool)
2271 map_object(HKEY(timestamp_query_pool), (void *)(uintptr_t) pAllocateInfo->commandBufferCount);
2272
2273 return result;
2274 }
2275
2276 static void overlay_FreeCommandBuffers(
2277 VkDevice device,
2278 VkCommandPool commandPool,
2279 uint32_t commandBufferCount,
2280 const VkCommandBuffer* pCommandBuffers)
2281 {
2282 struct device_data *device_data = FIND(struct device_data, device);
2283 for (uint32_t i = 0; i < commandBufferCount; i++) {
2284 struct command_buffer_data *cmd_buffer_data =
2285 FIND(struct command_buffer_data, pCommandBuffers[i]);
2286
2287 /* It is legal to free a NULL command buffer*/
2288 if (!cmd_buffer_data)
2289 continue;
2290
2291 uint64_t count = (uintptr_t)find_object_data(HKEY(cmd_buffer_data->pipeline_query_pool));
2292 if (count == 1) {
2293 unmap_object(HKEY(cmd_buffer_data->pipeline_query_pool));
2294 device_data->vtable.DestroyQueryPool(device_data->device,
2295 cmd_buffer_data->pipeline_query_pool, NULL);
2296 } else if (count != 0) {
2297 map_object(HKEY(cmd_buffer_data->pipeline_query_pool), (void *)(uintptr_t)(count - 1));
2298 }
2299 count = (uintptr_t)find_object_data(HKEY(cmd_buffer_data->timestamp_query_pool));
2300 if (count == 1) {
2301 unmap_object(HKEY(cmd_buffer_data->timestamp_query_pool));
2302 device_data->vtable.DestroyQueryPool(device_data->device,
2303 cmd_buffer_data->timestamp_query_pool, NULL);
2304 } else if (count != 0) {
2305 map_object(HKEY(cmd_buffer_data->timestamp_query_pool), (void *)(uintptr_t)(count - 1));
2306 }
2307 destroy_command_buffer_data(cmd_buffer_data);
2308 }
2309
2310 device_data->vtable.FreeCommandBuffers(device, commandPool,
2311 commandBufferCount, pCommandBuffers);
2312 }
2313
2314 static VkResult overlay_QueueSubmit(
2315 VkQueue queue,
2316 uint32_t submitCount,
2317 const VkSubmitInfo* pSubmits,
2318 VkFence fence)
2319 {
2320 struct queue_data *queue_data = FIND(struct queue_data, queue);
2321 struct device_data *device_data = queue_data->device;
2322
2323 device_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_submit]++;
2324
2325 for (uint32_t s = 0; s < submitCount; s++) {
2326 for (uint32_t c = 0; c < pSubmits[s].commandBufferCount; c++) {
2327 struct command_buffer_data *cmd_buffer_data =
2328 FIND(struct command_buffer_data, pSubmits[s].pCommandBuffers[c]);
2329
2330 /* Merge the submitted command buffer stats into the device. */
2331 for (uint32_t st = 0; st < OVERLAY_PARAM_ENABLED_MAX; st++)
2332 device_data->frame_stats.stats[st] += cmd_buffer_data->stats.stats[st];
2333
2334 /* Attach the command buffer to the queue so we remember to read its
2335 * pipeline statistics & timestamps at QueuePresent().
2336 */
2337 if (!cmd_buffer_data->pipeline_query_pool &&
2338 !cmd_buffer_data->timestamp_query_pool)
2339 continue;
2340
2341 if (list_is_empty(&cmd_buffer_data->link)) {
2342 list_addtail(&cmd_buffer_data->link,
2343 &queue_data->running_command_buffer);
2344 } else {
2345 fprintf(stderr, "Command buffer submitted multiple times before present.\n"
2346 "This could lead to invalid data.\n");
2347 }
2348 }
2349 }
2350
2351 return device_data->vtable.QueueSubmit(queue, submitCount, pSubmits, fence);
2352 }
2353
2354 static VkResult overlay_CreateDevice(
2355 VkPhysicalDevice physicalDevice,
2356 const VkDeviceCreateInfo* pCreateInfo,
2357 const VkAllocationCallbacks* pAllocator,
2358 VkDevice* pDevice)
2359 {
2360 struct instance_data *instance_data =
2361 FIND(struct instance_data, physicalDevice);
2362 VkLayerDeviceCreateInfo *chain_info =
2363 get_device_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
2364
2365 assert(chain_info->u.pLayerInfo);
2366 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
2367 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
2368 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
2369 if (fpCreateDevice == NULL) {
2370 return VK_ERROR_INITIALIZATION_FAILED;
2371 }
2372
2373 // Advance the link info for the next element on the chain
2374 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
2375
2376 VkPhysicalDeviceFeatures device_features = {};
2377 VkDeviceCreateInfo device_info = *pCreateInfo;
2378
2379 if (pCreateInfo->pEnabledFeatures)
2380 device_features = *(pCreateInfo->pEnabledFeatures);
2381 if (instance_data->pipeline_statistics_enabled) {
2382 device_features.inheritedQueries = true;
2383 device_features.pipelineStatisticsQuery = true;
2384 }
2385 device_info.pEnabledFeatures = &device_features;
2386
2387
2388 VkResult result = fpCreateDevice(physicalDevice, &device_info, pAllocator, pDevice);
2389 if (result != VK_SUCCESS) return result;
2390
2391 struct device_data *device_data = new_device_data(*pDevice, instance_data);
2392 device_data->physical_device = physicalDevice;
2393 vk_load_device_commands(*pDevice, fpGetDeviceProcAddr, &device_data->vtable);
2394
2395 instance_data->vtable.GetPhysicalDeviceProperties(device_data->physical_device,
2396 &device_data->properties);
2397
2398 VkLayerDeviceCreateInfo *load_data_info =
2399 get_device_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK);
2400 device_data->set_device_loader_data = load_data_info->u.pfnSetDeviceLoaderData;
2401
2402 device_map_queues(device_data, pCreateInfo);
2403
2404 return result;
2405 }
2406
2407 static void overlay_DestroyDevice(
2408 VkDevice device,
2409 const VkAllocationCallbacks* pAllocator)
2410 {
2411 struct device_data *device_data = FIND(struct device_data, device);
2412 device_unmap_queues(device_data);
2413 device_data->vtable.DestroyDevice(device, pAllocator);
2414 destroy_device_data(device_data);
2415 }
2416
2417 static VkResult overlay_CreateInstance(
2418 const VkInstanceCreateInfo* pCreateInfo,
2419 const VkAllocationCallbacks* pAllocator,
2420 VkInstance* pInstance)
2421 {
2422 VkLayerInstanceCreateInfo *chain_info =
2423 get_instance_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
2424
2425 assert(chain_info->u.pLayerInfo);
2426 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
2427 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
2428 PFN_vkCreateInstance fpCreateInstance =
2429 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
2430 if (fpCreateInstance == NULL) {
2431 return VK_ERROR_INITIALIZATION_FAILED;
2432 }
2433
2434 // Advance the link info for the next element on the chain
2435 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
2436
2437 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
2438 if (result != VK_SUCCESS) return result;
2439
2440 struct instance_data *instance_data = new_instance_data(*pInstance);
2441 vk_load_instance_commands(instance_data->instance,
2442 fpGetInstanceProcAddr,
2443 &instance_data->vtable);
2444 instance_data_map_physical_devices(instance_data, true);
2445
2446 parse_overlay_env(&instance_data->params, getenv("VK_LAYER_MESA_OVERLAY_CONFIG"));
2447
2448 for (int i = OVERLAY_PARAM_ENABLED_vertices;
2449 i <= OVERLAY_PARAM_ENABLED_compute_invocations; i++) {
2450 if (instance_data->params.enabled[i]) {
2451 instance_data->pipeline_statistics_enabled = true;
2452 break;
2453 }
2454 }
2455
2456 return result;
2457 }
2458
2459 static void overlay_DestroyInstance(
2460 VkInstance instance,
2461 const VkAllocationCallbacks* pAllocator)
2462 {
2463 struct instance_data *instance_data = FIND(struct instance_data, instance);
2464 instance_data_map_physical_devices(instance_data, false);
2465 instance_data->vtable.DestroyInstance(instance, pAllocator);
2466 destroy_instance_data(instance_data);
2467 }
2468
2469 static const struct {
2470 const char *name;
2471 void *ptr;
2472 } name_to_funcptr_map[] = {
2473 { "vkGetDeviceProcAddr", (void *) vkGetDeviceProcAddr },
2474 #define ADD_HOOK(fn) { "vk" # fn, (void *) overlay_ ## fn }
2475 ADD_HOOK(AllocateCommandBuffers),
2476 ADD_HOOK(FreeCommandBuffers),
2477 ADD_HOOK(ResetCommandBuffer),
2478 ADD_HOOK(BeginCommandBuffer),
2479 ADD_HOOK(EndCommandBuffer),
2480 ADD_HOOK(CmdExecuteCommands),
2481
2482 ADD_HOOK(CmdDraw),
2483 ADD_HOOK(CmdDrawIndexed),
2484 ADD_HOOK(CmdDrawIndirect),
2485 ADD_HOOK(CmdDrawIndexedIndirect),
2486 ADD_HOOK(CmdDispatch),
2487 ADD_HOOK(CmdDispatchIndirect),
2488 ADD_HOOK(CmdDrawIndirectCountKHR),
2489 ADD_HOOK(CmdDrawIndexedIndirectCountKHR),
2490
2491 ADD_HOOK(CmdBindPipeline),
2492
2493 ADD_HOOK(CreateSwapchainKHR),
2494 ADD_HOOK(QueuePresentKHR),
2495 ADD_HOOK(DestroySwapchainKHR),
2496 ADD_HOOK(AcquireNextImageKHR),
2497 ADD_HOOK(AcquireNextImage2KHR),
2498
2499 ADD_HOOK(QueueSubmit),
2500
2501 ADD_HOOK(CreateDevice),
2502 ADD_HOOK(DestroyDevice),
2503
2504 ADD_HOOK(CreateInstance),
2505 ADD_HOOK(DestroyInstance),
2506 #undef ADD_HOOK
2507 };
2508
2509 static void *find_ptr(const char *name)
2510 {
2511 for (uint32_t i = 0; i < ARRAY_SIZE(name_to_funcptr_map); i++) {
2512 if (strcmp(name, name_to_funcptr_map[i].name) == 0)
2513 return name_to_funcptr_map[i].ptr;
2514 }
2515
2516 return NULL;
2517 }
2518
2519 VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev,
2520 const char *funcName)
2521 {
2522 void *ptr = find_ptr(funcName);
2523 if (ptr) return reinterpret_cast<PFN_vkVoidFunction>(ptr);
2524
2525 if (dev == NULL) return NULL;
2526
2527 struct device_data *device_data = FIND(struct device_data, dev);
2528 if (device_data->vtable.GetDeviceProcAddr == NULL) return NULL;
2529 return device_data->vtable.GetDeviceProcAddr(dev, funcName);
2530 }
2531
2532 VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance,
2533 const char *funcName)
2534 {
2535 void *ptr = find_ptr(funcName);
2536 if (ptr) return reinterpret_cast<PFN_vkVoidFunction>(ptr);
2537
2538 if (instance == NULL) return NULL;
2539
2540 struct instance_data *instance_data = FIND(struct instance_data, instance);
2541 if (instance_data->vtable.GetInstanceProcAddr == NULL) return NULL;
2542 return instance_data->vtable.GetInstanceProcAddr(instance, funcName);
2543 }