-
Notifications
You must be signed in to change notification settings - Fork 40
/
timeline_semaphore.c
2582 lines (2136 loc) · 94.9 KB
/
timeline_semaphore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* For glibc to expose pthread_condattr_setclock */
#define _POSIX_C_SOURCE 200112
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include <vulkan/vulkan.h>
#include <vulkan/vk_layer.h>
#include "hash_table.h"
#include "list.h"
#include "vk_alloc.h"
#include "vk_util.h"
#define ENTRY_POINTS \
ENTRY_POINT(GetInstanceProcAddr) \
\
ENTRY_POINT(EnumeratePhysicalDevices) \
ENTRY_POINT(EnumerateDeviceExtensionProperties) \
ENTRY_POINT(GetPhysicalDeviceFeatures2) \
ENTRY_POINT(GetPhysicalDeviceFeatures2KHR) \
ENTRY_POINT(GetPhysicalDeviceProperties2) \
ENTRY_POINT(GetPhysicalDeviceProperties2KHR) \
ENTRY_POINT(GetPhysicalDeviceQueueFamilyProperties) \
ENTRY_POINT(GetPhysicalDeviceExternalSemaphoreProperties) \
\
ENTRY_POINT(DestroyInstance) \
\
ENTRY_POINT(GetDeviceProcAddr) \
ENTRY_POINT(GetDeviceQueue) \
ENTRY_POINT(DestroyDevice) \
ENTRY_POINT(DeviceWaitIdle) \
ENTRY_POINT(ImportSemaphoreFdKHR) \
\
ENTRY_POINT(CreateFence) \
ENTRY_POINT(DestroyFence) \
ENTRY_POINT(ResetFences) \
ENTRY_POINT(GetFenceStatus) \
ENTRY_POINT(WaitForFences) \
\
ENTRY_POINT(CreateSemaphore) \
ENTRY_POINT(DestroySemaphore) \
\
ENTRY_POINT(QueueWaitIdle) \
ENTRY_POINT(QueueSubmit) \
ENTRY_POINT(QueueBindSparse) \
\
ENTRY_POINT(AcquireNextImageKHR) \
ENTRY_POINT(QueuePresentKHR)
struct vulkan_vtable {
#define ENTRY_POINT(name) PFN_vk##name name;
ENTRY_POINTS
#undef ENTRY_POINT
};
struct object_map {
struct hash_table *map;
pthread_mutex_t lock;
};
/* Attached to VkInstance/VkPhysicalDevice objects */
struct instance_data {
struct vulkan_vtable vtable;
VkInstance instance;
uint32_t physicalDeviceCount;
VkAllocationCallbacks alloc;
};
struct device_data;
/* Attached to VkQueue objects */
struct queue_data {
struct device_data *device;
VkQueue queue;
VkQueueFamilyProperties props;
struct list_head waiting_submissions; /* list of struct queue_submit */
/* Used to transfer temporary payloads from one */
struct list_head wait_points; /* list of struct timeline_wait_point */
};
/* Attached to VkDevice objects */
struct device_data {
pthread_mutex_t lock;
pthread_cond_t queue_submit;
struct instance_data *instance;
PFN_vkSetDeviceLoaderData set_device_loader_data;
struct vulkan_vtable vtable;
VkPhysicalDevice physical_device;
VkDevice device;
struct queue_data *queues;
uint32_t n_queues;
struct object_map semaphores;
struct object_map temporary_import_semaphores;
VkAllocationCallbacks alloc;
struct list_head free_points; /* List of struct timeline_points */
struct list_head free_wait_points; /* list of struct timeline_wait_point */
struct list_head free_point_semaphores; /* list of struct timeline_point_semaphore */
struct list_head free_point_fences; /* list of struct timeline_point_fence */
};
/* Attached to VkSemaphore objects of timeline type */
struct timeline_semaphore {
struct device_data *device;
/* Last point to have been signaled */
uint64_t highest_past;
/* Last point to have been submitted for signaling */
uint64_t highest_pending;
VkAllocationCallbacks alloc;
struct list_head points; /* list of struct timeline_point */
struct list_head wait_points; /* list of struct timeline_wait_point */
struct list_head link;
};
/* Fence associated with timeline values to allow for host wait operations. */
struct timeline_point_fence {
/* Fence associated with a semaphore signaling, we use this to check the
* status of the semaphore.
*/
VkFence fence;
uint32_t refcount;
struct list_head link;
};
/* Semaphore associated with timeline values to allow for device
* synchronization.
*/
struct timeline_point_semaphore {
/* Semaphore substitued on QueueSubmit() */
VkSemaphore semaphore;
/* If this point has never been waited upon & signaled. We can directly use
* the semaphore that has been signaled but not yet waited upon, otherwise
* we need to created a new semaphore submit it on the queue and wait on
* that new semaphore.
*/
bool device_waited;
bool device_signaled;
/* Where the semaphore was submitted for signaling. */
struct queue_data *queue;
uint32_t refcount;
struct list_head link;
};
/* Represents a point on the timeline */
struct timeline_point {
struct timeline_semaphore *timeline;
struct list_head link;
int waiting;
uint64_t serial;
/* Where the semaphore was submitted for signaling. */
struct queue_data *queue;
/* Semaphore for device side wait/signal operations. */
struct timeline_point_semaphore *semaphore;
/* Fence we use to do host side wait operations of the timeline point. */
struct timeline_point_fence *fence;
};
/* For a given point we might need multiple semaphore to allow for the 1 to N
* wait relationship that doesn't exist with binary semaphores.
*/
struct timeline_wait_point {
struct timeline_point *point;
struct list_head link;
struct timeline_point_semaphore *semaphore;
/* This is the fence we use to do host side wait operations of the timeline
* point.
*/
struct timeline_point_fence *fence;
};
struct queue_submit {
VkStructureType stype;
VkBaseOutStructure pnext;
union {
/* Copied from VkSubmitInfo */
struct {
VkPipelineStageFlags *wait_stage_mask;
VkCommandBuffer *command_buffers;
uint32_t n_command_buffers;
};
/* Copied from VkBindSparseInfo */
struct {
VkSparseBufferMemoryBindInfo *buffer_binds;
uint32_t n_buffer_binds;
VkSparseImageOpaqueMemoryBindInfo *image_opaque_binds;
uint32_t n_image_opaque_binds;
VkSparseImageMemoryBindInfo *image_binds;
uint32_t n_image_binds;
};
};
/* Fence coming from the application. */
VkFence fence;
/* Translated timeline semaphores into binary semaphores by the layer.
* Includes a first set of clone binary semaphores followed by semaphores
* coming from the layer to emulate timeline semaphores.
*/
VkSemaphore *wait_semaphores;
VkSemaphore *signal_semaphores;
uint32_t n_wait_semaphores;
uint32_t n_signal_semaphores;
/* These are semaphores we need to signal before doing the actual
* QueueSubmit onto the implementation. Each semaphore might be signaled on
* a different queue.
*/
struct timeline_point_semaphore **serialize_semaphores;
uint32_t n_serialize_semaphores;
/* Array of timeline semaphores the layer has to wait on to be available
* before submitting them to the implementation.
*/
struct timeline_semaphore_ref {
struct timeline_semaphore *semaphore;
uint64_t value;
} *wait_timeline_semaphores, *signal_timeline_semaphores;
uint32_t n_wait_timeline_semaphores;
uint32_t n_signal_timeline_semaphores;
struct list_head link;
};
static void free_submit_info(struct device_data *device, struct queue_submit *info);
static void timeline_wait_point_free_locked(struct device_data *device,
struct timeline_wait_point *wait_point);
static VkResult device_submit_deferred_locked(struct device_data *device);
/**/
static uint64_t gettime_ns(void)
{
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, ¤t);
return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec;
}
static uint64_t absolute_timeout(uint64_t timeout)
{
if (timeout == 0)
return 0;
uint64_t current_time = gettime_ns();
uint64_t max_timeout = (uint64_t) INT64_MAX - current_time;
timeout = MIN2(max_timeout, timeout);
return (current_time + timeout);
}
/* Mapping of dispatchable objects */
static struct object_map global_map = {
.map = NULL,
.lock = PTHREAD_MUTEX_INITIALIZER,
};
static inline void object_map_ensure_initialized(struct object_map *map)
{
if (!map->map)
map->map = hash_table_new();
}
static void object_map(struct object_map *map, void *obj, void *data)
{
pthread_mutex_lock(&map->lock);
object_map_ensure_initialized(map);
hash_table_insert(map->map, (uintptr_t) obj, data);
pthread_mutex_unlock(&map->lock);
}
static void object_unmap(struct object_map *map, void *obj)
{
pthread_mutex_lock(&map->lock);
hash_table_remove(map->map, (uintptr_t) obj);
pthread_mutex_unlock(&map->lock);
}
static void *object_find(struct object_map *map, void *obj)
{
pthread_mutex_lock(&map->lock);
object_map_ensure_initialized(map);
void *data = hash_table_search(map->map, (uintptr_t) obj);
pthread_mutex_unlock(&map->lock);
return data;
}
/**/
#define VK_CHECK(expr) \
do { \
VkResult __result = (expr); \
if (__result != VK_SUCCESS) { \
fprintf(stderr, "'%s' line %i failed with %i\n", \
#expr, __LINE__, __result); \
} \
} while (0)
/**/
static void *VKAPI_PTR default_alloc_func(void *pUserData, size_t size, size_t align, VkSystemAllocationScope allocationScope) {
return malloc(size);
}
static void *VKAPI_PTR default_realloc_func(void *pUserData, void *pOriginal, size_t size, size_t align,
VkSystemAllocationScope allocationScope) {
return realloc(pOriginal, size);
}
static void VKAPI_PTR default_free_func(void *pUserData, void *pMemory) { free(pMemory); }
static const VkAllocationCallbacks default_alloc = {
.pUserData = NULL,
.pfnAllocation = default_alloc_func,
.pfnReallocation = default_realloc_func,
.pfnFree = default_free_func,
};
/**/
static void fill_instance_vtable(struct vulkan_vtable *vtable, VkInstance instance,
PFN_vkGetInstanceProcAddr get_proc_addr)
{
#define ENTRY_POINT(name) \
vtable->name = (PFN_vk##name) get_proc_addr(instance, "vk"#name);
ENTRY_POINTS
#undef ENTRY_POINT
}
static void fill_device_vtable(struct vulkan_vtable *vtable, VkDevice device,
PFN_vkGetDeviceProcAddr get_proc_addr)
{
#define ENTRY_POINT(name) vtable->name = (PFN_vk##name) get_proc_addr(device, "vk"#name);
ENTRY_POINTS
#undef ENTRY_POINT
}
/**/
static VkLayerInstanceCreateInfo *get_instance_chain_info(const VkInstanceCreateInfo *pCreateInfo,
VkLayerFunction func)
{
vk_foreach_struct_const(item, pCreateInfo->pNext) {
if (item->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
((VkLayerInstanceCreateInfo *) item)->function == func)
return (VkLayerInstanceCreateInfo *) item;
}
unreachable("instance chain info not found");
return NULL;
}
static VkLayerDeviceCreateInfo *get_device_chain_info(const VkDeviceCreateInfo *pCreateInfo,
VkLayerFunction func)
{
vk_foreach_struct_const(item, pCreateInfo->pNext) {
if (item->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
((VkLayerDeviceCreateInfo *) item)->function == func)
return (VkLayerDeviceCreateInfo *)item;
}
unreachable("device chain info not found");
return NULL;
}
/**/
static void instance_destroy(struct instance_data *instance)
{
VkAllocationCallbacks alloc = instance->alloc;
object_unmap(&global_map, instance->instance);
vk_free(&alloc, instance);
}
static VkResult instance_new(VkInstance _instance,
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr,
const VkAllocationCallbacks *allocator)
{
struct instance_data *instance =
vk_zalloc(allocator, sizeof(*instance), 8, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (!instance)
return VK_ERROR_OUT_OF_HOST_MEMORY;
instance->instance = _instance;
instance->alloc = *allocator;
fill_instance_vtable(&instance->vtable,
instance->instance,
fpGetInstanceProcAddr);
object_map(&global_map, instance->instance, instance);
return VK_SUCCESS;
}
/**/
static void device_destroy(struct device_data *device)
{
VkAllocationCallbacks alloc = device->alloc;
for (uint32_t i = 0; i < device->n_queues; i++) {
struct queue_data *queue = &device->queues[i];
list_for_each_entry_safe(struct timeline_wait_point, wait_point,
&queue->wait_points, link) {
list_del(&wait_point->link);
timeline_wait_point_free_locked(device, wait_point);
}
list_for_each_entry_safe(struct queue_submit, submit,
&queue->waiting_submissions, link) {
list_del(&submit->link);
free_submit_info(device, submit);
}
object_unmap(&global_map, queue->queue);
}
list_for_each_entry_safe(struct timeline_point_fence, fence,
&device->free_point_fences, link) {
list_del(&fence->link);
device->vtable.DestroyFence(device->device, fence->fence, &device->alloc);
vk_free(&device->alloc, fence);
}
list_for_each_entry_safe(struct timeline_point, point,
&device->free_points, link) {
list_del(&point->link);
vk_free(&device->alloc, point);
}
list_for_each_entry_safe(struct timeline_wait_point, wait_point,
&device->free_wait_points, link) {
assert(wait_point->fence == NULL);
assert(wait_point->semaphore == NULL);
list_del(&wait_point->link);
vk_free(&device->alloc, wait_point);
}
list_for_each_entry_safe(struct timeline_point_semaphore, semaphore,
&device->free_point_semaphores, link) {
device->vtable.DestroySemaphore(device->device,
semaphore->semaphore,
&device->alloc);
list_del(&semaphore->link);
vk_free(&device->alloc, semaphore);
}
pthread_mutex_destroy(&device->lock);
object_unmap(&global_map, device->device);
pthread_mutex_destroy(&device->semaphores.lock);
hash_table_destroy(device->semaphores.map);
vk_free(&alloc, device);
}
static VkResult device_new(VkDevice _device,
VkPhysicalDevice physical_device,
PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr,
PFN_vkSetDeviceLoaderData pfnSetDeviceLoaderData,
const VkDeviceCreateInfo *create_info,
const VkAllocationCallbacks *allocator,
struct instance_data *instance)
{
uint32_t n_queues = 0;
for (uint32_t i = 0; i < create_info->queueCreateInfoCount; i++)
n_queues += create_info->pQueueCreateInfos[i].queueCount;
struct device_data *device =
vk_zalloc(allocator,
sizeof(*device) + n_queues * sizeof(*device->queues),
8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!device)
return VK_ERROR_OUT_OF_HOST_MEMORY;
device->alloc = *allocator;
device->instance = instance;
device->device = _device;
device->physical_device = physical_device;
device->set_device_loader_data = pfnSetDeviceLoaderData;
device->queues = (struct queue_data *) (device + 1);
list_inithead(&device->free_point_fences);
list_inithead(&device->free_point_semaphores);
list_inithead(&device->free_wait_points);
list_inithead(&device->free_points);
object_map(&global_map, device->device, device);
fill_device_vtable(&device->vtable, device->device, fpGetDeviceProcAddr);
if (pthread_mutex_init(&device->lock, NULL) != 0)
goto err;
pthread_condattr_t condattr;
if (pthread_condattr_init(&condattr) != 0)
goto err;
#if !defined(__APPLE__)
if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
pthread_condattr_destroy(&condattr);
goto err;
}
#endif
if (pthread_cond_init(&device->queue_submit, &condattr) != 0) {
pthread_condattr_destroy(&condattr);
goto err;
}
pthread_condattr_destroy(&condattr);
if (pthread_mutex_init(&device->semaphores.lock, NULL) != 0)
goto err;
device->semaphores.map = hash_table_new();
if (pthread_mutex_init(&device->temporary_import_semaphores.lock, NULL) != 0)
goto err;
device->temporary_import_semaphores.map = hash_table_new();
uint32_t queue_family_count;
instance->vtable.GetPhysicalDeviceQueueFamilyProperties(device->physical_device,
&queue_family_count,
NULL);
VkQueueFamilyProperties *queue_family_props =
vk_alloc(&device->alloc, sizeof(*queue_family_props) * queue_family_count,
8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
if (!queue_family_props)
goto err;
instance->vtable.GetPhysicalDeviceQueueFamilyProperties(device->physical_device,
&queue_family_count,
queue_family_props);
for (uint32_t qci = 0; qci < create_info->queueCreateInfoCount; qci++) {
uint32_t queue_family_idx = create_info->pQueueCreateInfos[qci].queueFamilyIndex;
for (uint32_t qi = 0; qi < create_info->pQueueCreateInfos[qci].queueCount; qi++) {
struct queue_data *queue = &device->queues[device->n_queues++];
queue->device = device;
queue->props = queue_family_props[queue_family_idx];
list_inithead(&queue->waiting_submissions);
list_inithead(&queue->wait_points);
device->vtable.GetDeviceQueue(device->device,
queue_family_idx, qi, &queue->queue);
VK_CHECK(device->set_device_loader_data(device->device, queue->queue));
object_map(&global_map, queue->queue, queue);
}
}
return VK_SUCCESS;
err:
device_destroy(device);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
/**/
static VkResult device_get_point_fence_locked(struct device_data *device,
struct timeline_point_fence **fence)
{
if (!list_empty(&device->free_point_fences)) {
*fence = list_first_entry(&device->free_point_fences,
struct timeline_point_fence, link);
list_del(&(*fence)->link);
(*fence)->refcount = 1;
return device->vtable.ResetFences(device->device, 1, &(*fence)->fence);
}
*fence = vk_zalloc(&device->alloc, sizeof(**fence), 8,
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!(*fence))
return VK_ERROR_OUT_OF_HOST_MEMORY;
(*fence)->refcount = 1;
VkResult result = device->vtable.CreateFence(
device->device,
&(VkFenceCreateInfo) {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
},
&device->alloc,
&(*fence)->fence);
if (result != VK_SUCCESS)
vk_free(&device->alloc, *fence);
return result;
}
static struct timeline_point_fence *point_fence_ref_locked(struct timeline_point_fence *fence)
{
fence->refcount++;
return fence;
}
static void point_fence_unref_locked(struct device_data *device,
struct timeline_point_fence *fence)
{
assert(!fence || fence->refcount > 0);
if (!fence || --fence->refcount)
return;
list_add(&fence->link, &device->free_point_fences);
}
static VkResult device_get_point_semaphore_locked(struct device_data *device,
struct timeline_point_semaphore **semaphore)
{
if (!list_empty(&device->free_point_semaphores)) {
*semaphore = list_first_entry(&device->free_point_semaphores,
struct timeline_point_semaphore, link);
list_del(&(*semaphore)->link);
} else {
*semaphore = vk_zalloc(&device->alloc, sizeof(**semaphore), 8,
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!(*semaphore))
return VK_ERROR_OUT_OF_HOST_MEMORY;
VkResult result = device->vtable.CreateSemaphore(
device->device,
&(VkSemaphoreCreateInfo) {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
},
&device->alloc,
&(*semaphore)->semaphore);
if (result != VK_SUCCESS) {
vk_free(&device->alloc, *semaphore);
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
}
(*semaphore)->queue = NULL;
(*semaphore)->refcount = 1;
(*semaphore)->device_waited = false;
(*semaphore)->device_signaled = false;
return VK_SUCCESS;
}
static struct timeline_point_semaphore *point_semaphore_ref_locked(struct timeline_point_semaphore *semaphore)
{
semaphore->refcount++;
return semaphore;
}
static void point_semaphore_unref_locked(struct device_data *device,
struct timeline_point_semaphore *semaphore)
{
assert(!semaphore || semaphore->refcount > 0);
if (!semaphore || --semaphore->refcount)
return;
/* If the semaphore was waited upon, it's a candidate to be reused,
* otherwise we need to destroy it.
*/
if (semaphore->device_waited) {
list_add(&semaphore->link, &device->free_point_semaphores);
} else {
device->vtable.DestroySemaphore(device->device, semaphore->semaphore, &device->alloc);
vk_free(&device->alloc, semaphore);
}
}
static void timeline_wait_point_free_locked(struct device_data *device,
struct timeline_wait_point *wait_point)
{
point_semaphore_unref_locked(device, wait_point->semaphore);
wait_point->semaphore = NULL;
point_fence_unref_locked(device, wait_point->fence);
wait_point->fence = NULL;
list_add(&wait_point->link, &device->free_wait_points);
}
static void timeline_point_free_locked(struct device_data *device,
struct timeline_point *point)
{
point_semaphore_unref_locked(device, point->semaphore);
point->semaphore = NULL;
point_fence_unref_locked(device, point->fence);
point->fence = NULL;
list_add(&point->link, &device->free_points);
}
static VkResult gc_wait_point_list_locked(struct device_data *device, struct list_head *list)
{
list_for_each_entry_safe(struct timeline_wait_point, wait_point, list, link) {
VkResult result = device->vtable.GetFenceStatus(device->device,
wait_point->fence->fence);
if (result == VK_NOT_READY) {
break;
} else if (result != VK_SUCCESS) {
return result;
}
list_del(&wait_point->link);
timeline_wait_point_free_locked(device, wait_point);
}
return VK_SUCCESS;
}
static VkResult timeline_gc_locked(struct device_data *device,
struct timeline_semaphore *semaphore)
{
/* Garbage collect all serializing semaphores. */
VkResult result = gc_wait_point_list_locked(device, &semaphore->wait_points);
if (result != VK_SUCCESS)
return result;
/* Now look at the points in order to garbage collect the timeline. */
list_for_each_entry_safe(struct timeline_point, point,
&semaphore->points, link) {
/* If someone is waiting on this time point, consider it busy and don't
* try to recycle it. There's a slim possibility that it's no longer
* busy by the time we look at it but we would be recycling it out from
* under a waiter and that can lead to weird races.
*
* We walk the list in-order so if this time point is still busy so is
* every following time point
*/
assert(point->waiting >= 0);
if (point->waiting)
return VK_SUCCESS;
/* We might have points that have been signaled but another submission
* depended on them and we had to create serialization semaphores,
* therefore adding more wait points. We try to garbage collect those.
*/
result = device->vtable.GetFenceStatus(device->device,
point->fence->fence);
if (result == VK_NOT_READY) {
/* We walk the list in-order so if this time point is still busy so
* is every following time point
*/
return VK_SUCCESS;
} else if (result != VK_SUCCESS) {
return result;
}
assert(semaphore->highest_past < point->serial);
semaphore->highest_past = point->serial;
list_del(&point->link);
timeline_point_free_locked(device, point);
}
return VK_SUCCESS;
}
static VkResult timeline_wait_locked(struct device_data *device,
struct timeline_semaphore **semaphores,
const uint64_t *serials,
uint32_t n_semaphores,
bool wait_all,
uint64_t abs_timeout_ns)
{
void *alloc;
struct timeline_point **points;
VkFence *fences;
VkResult result = VK_TIMEOUT;
VK_MULTIALLOC(ma);
vk_multialloc_add(&ma, &points, n_semaphores);
vk_multialloc_add(&ma, &fences, n_semaphores);
if (!(alloc = vk_multialloc_alloc(&ma, &device->alloc, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE)))
return VK_ERROR_OUT_OF_HOST_MEMORY;
do {
uint32_t n_fences = 0;
uint32_t n_ready = 0;
/* First look for any signaled semaphore or pick up the fences
* associated with the semaphores.
*/
for (uint32_t i = 0; i < n_semaphores; i++) {
timeline_gc_locked(device, semaphores[i]);
if (semaphores[i]->highest_past >= serials[i]) {
if (!wait_all) {
result = VK_SUCCESS;
goto out;
}
n_ready++;
} else {
list_for_each_entry(struct timeline_point, point, &semaphores[i]->points, link) {
if (point->serial < serials[i])
continue;
points[n_fences] = point;
fences[n_fences] = point->fence->fence;
n_fences++;
if (!wait_all) {
result = device->vtable.GetFenceStatus(device->device, point->fence->fence);
if (result != VK_NOT_READY)
goto out;
}
}
}
}
if (n_ready == n_semaphores) {
result = VK_SUCCESS;
goto out;
}
if ((n_fences + n_ready) == n_semaphores) {
/* If we have all fences available, wait on them, otherwise wait for the
* broadcast queue_submit to collect more fences.
*/
for (uint32_t i = 0; i < n_semaphores; i++)
points[i]->waiting++;
uint64_t now = gettime_ns();
pthread_mutex_unlock(&device->lock);
result = device->vtable.WaitForFences(device->device, n_fences, fences, true,
now > abs_timeout_ns ? 0 : abs_timeout_ns - now);
pthread_mutex_lock(&device->lock);
for (uint32_t i = 0; i < n_semaphores; i++)
points[i]->waiting--;
goto out;
} else {
struct timespec abstime = {
.tv_sec = abs_timeout_ns / NSEC_PER_SEC,
.tv_nsec = abs_timeout_ns % NSEC_PER_SEC,
};
int ret = pthread_cond_timedwait(&device->queue_submit,
&device->lock, &abstime);
assert(ret != EINVAL);
(void)ret;
}
} while (gettime_ns() < abs_timeout_ns);
out:
vk_free(&device->alloc, alloc);
return result;
}
static VkResult timeline_create_wait_point_locked(struct timeline_semaphore *semaphore,
uint64_t serial,
struct timeline_point_fence *fence,
struct timeline_point_semaphore **out_point_semaphore)
{
struct device_data *device = semaphore->device;
*out_point_semaphore = NULL;
if (semaphore->highest_past >= serial)
return VK_SUCCESS;
assert(serial <= semaphore->highest_pending);
struct timeline_point *point = NULL;
list_for_each_entry_rev(struct timeline_point, _point, &semaphore->points, link) {
if (_point->serial < serial)
continue;
point = _point;
break;
}
/* highest_past < serial <= semaphore->highest_pending
*
* implies there is at least one point in the list that match our criteria.
*/
assert(point);
struct timeline_wait_point *wait_point;
if (!list_empty(&device->free_wait_points)) {
wait_point = list_first_entry(&device->free_wait_points,
struct timeline_wait_point, link);
list_del(&wait_point->link);
} else {
wait_point = vk_alloc(&device->alloc, sizeof(*wait_point), 8,
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!wait_point)
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
struct timeline_point_semaphore *point_semaphore = NULL;
/* If the point we created in the timeline was never waited upon but
* already signaled, we can just use it.
*/
if (!point->semaphore->device_waited &&
point->semaphore->device_signaled) {
point_semaphore = point_semaphore_ref_locked(point->semaphore);
}
VkResult result = VK_SUCCESS;
if (!point_semaphore) {
result = device_get_point_semaphore_locked(device, &point_semaphore);
if (result != VK_SUCCESS) {
vk_free(&device->alloc, wait_point);
return result;
}
point_semaphore->queue = point->queue;
}
wait_point->point = point;
wait_point->semaphore = point_semaphore;
wait_point->fence = point_fence_ref_locked(fence);
list_addtail(&wait_point->link, &semaphore->wait_points);
*out_point_semaphore = wait_point->semaphore;
return result;
}
static VkResult timeline_create_point_locked(struct queue_data *queue,
struct timeline_semaphore *semaphore,
uint64_t serial,
struct timeline_point_fence *fence,
struct timeline_point **out_point)
{
struct device_data *device = queue->device;
/* Timelines must always increase */
assert(serial > semaphore->highest_pending);
struct timeline_point *point;
if (!list_empty(&device->free_points)) {
point = list_first_entry(&device->free_points,
struct timeline_point, link);
list_del(&point->link);
} else {
point = vk_zalloc(&device->alloc, sizeof(*point), 8,
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!point)
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
assert(!point->waiting);
point->timeline = semaphore;
point->serial = serial;
point->queue = queue;