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