Repository navigation
Expand file tree
/
Copy pathpyrowave_c.cpp
More file actions
1997 lines (1666 loc) · 65.4 KB
/
Copy pathpyrowave_c.cpp
File metadata and controls
1997 lines (1666 loc) · 65.4 KB
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 (c) 2026 Hans-Kristian Arntzen
// Copyright (c) 2026 AlkaidLab contributors
// SPDX-License-Identifier: MIT AND GPL-3.0-only
#include "context.hpp"
#include "device.hpp"
#include "image.hpp"
#include "buffer.hpp"
#include "pyrowave.h"
#include "pyrowave_decoder.hpp"
#include "pyrowave_bitstream_header.hpp"
#include "pyrowave_encoder.hpp"
#include "logging.hpp"
#include "slangmosh_scaler.hpp"
#include "scaler.hpp"
using namespace Granite;
using namespace Vulkan;
using namespace PyroWave;
struct NullLogger : Util::LoggingInterface
{
bool log(const char *, const char *, va_list) override
{
#ifdef VULKAN_DEBUG
return false;
#else
return true;
#endif
}
};
static NullLogger null_logger;
extern "C" {
void pyrowave_get_api_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
{
*major = PYROWAVE_API_VERSION_MAJOR;
*minor = PYROWAVE_API_VERSION_MINOR;
*patch = PYROWAVE_API_VERSION_PATCH;
}
struct pyrowave_device_opaque
{
Context context;
Device device;
VkCommandBuffer cmd = VK_NULL_HANDLE;
CommandBuffer::Type queue_type = CommandBuffer::Type::Generic;
};
void pyrowave_device_set_command_buffer(pyrowave_device device, VkCommandBuffer cmd)
{
device->cmd = cmd;
}
VkQueueGlobalPriority pyrowave_device_get_global_priority(pyrowave_device device)
{
return device->device.get_device_features().global_compute_priority;
}
pyrowave_result pyrowave_create_device_by_compat2(
// If non-zero, needs to match VkPhysicalDeviceProperties::vendorID/deviceID.
// Risks picking the wrong device if there are multiple ICDs for the same GPU.
uint32_t vid, uint32_t pid,
const pyrowave_uuid *device_uuid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::deviceUUID
const pyrowave_uuid *driver_uuid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::driverUUID
const pyrowave_luid *device_luid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::deviceLUID
VkQueueGlobalPriority global_priority,
pyrowave_device *device)
{
// TODO: Find a better way to do this.
Util::set_thread_logging_interface(&null_logger);
if (!Context::init_loader(nullptr))
return PYROWAVE_ERROR_NO_VULKAN;
auto *dev = new pyrowave_device_opaque();
dev->context.set_num_thread_indices(1);
dev->context.set_system_handles({});
VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
app_info.apiVersion = VK_API_VERSION_1_2;
app_info.pApplicationName = "pyrowave-c";
app_info.pEngineName = "Granite";
dev->context.set_application_info(&app_info);
// Just enable video extensions so that we can use video image usage, but don't bother creating queues for it, etc.
if (!dev->context.init_instance(nullptr, 0, CONTEXT_CREATION_ENABLE_VIDEO_FEATURE_ONLY_BIT))
{
delete dev;
return PYROWAVE_ERROR_NO_VULKAN;
}
uint32_t count;
if (vkEnumeratePhysicalDevices(dev->context.get_instance(), &count, nullptr) != VK_SUCCESS)
{
delete dev;
return PYROWAVE_ERROR_NO_VULKAN;
}
std::vector<VkPhysicalDevice> gpus(count);
if (vkEnumeratePhysicalDevices(dev->context.get_instance(), &count, gpus.data()) < 0)
{
delete dev;
return PYROWAVE_ERROR_NO_VULKAN;
}
VkPhysicalDevice selected_gpu = VK_NULL_HANDLE;
for (auto &gpu : gpus)
{
VkPhysicalDeviceProperties props = {};
vkGetPhysicalDeviceProperties(gpu, &props);
// Is this even possible these days?
if (props.apiVersion < VK_API_VERSION_1_2)
continue;
VkPhysicalDeviceIDProperties ids = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 props2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &ids };
vkGetPhysicalDeviceProperties2(gpu, &props2);
if (vid && props2.properties.vendorID != vid)
continue;
if (pid && props2.properties.deviceID != pid)
continue;
if (device_uuid && memcmp(device_uuid, ids.deviceUUID, VK_UUID_SIZE) != 0)
continue;
if (driver_uuid && memcmp(driver_uuid, ids.driverUUID, VK_UUID_SIZE) != 0)
continue;
if (device_luid && !ids.deviceLUIDValid)
continue;
if (device_luid && memcmp(device_luid, ids.deviceLUID, VK_LUID_SIZE) != 0)
continue;
ContextCreationFlags flags = CONTEXT_CREATION_ENABLE_VIDEO_FEATURE_ONLY_BIT;
if (global_priority == VK_QUEUE_GLOBAL_PRIORITY_REALTIME)
flags |= CONTEXT_CREATION_ENABLE_COMPUTE_REALTIME_GLOBAL_PRIORITY_BIT;
else if (global_priority == VK_QUEUE_GLOBAL_PRIORITY_HIGH)
flags |= CONTEXT_CREATION_ENABLE_COMPUTE_HIGH_GLOBAL_PRIORITY_BIT;
if (dev->context.init_device(gpu, VK_NULL_HANDLE, nullptr, 0, flags))
{
selected_gpu = gpu;
break;
}
}
if (!selected_gpu)
{
delete dev;
return PYROWAVE_ERROR_NO_VULKAN;
}
ContextOptions context_opts = {};
context_opts.memory_priorities = false;
context_opts.lean_memory_mode = true;
dev->device.set_context(dev->context, context_opts);
*device = dev;
if (global_priority > VK_QUEUE_GLOBAL_PRIORITY_MEDIUM)
pyrowave_device_set_queue_type(dev, VK_QUEUE_COMPUTE_BIT);
return PYROWAVE_SUCCESS;
}
pyrowave_result pyrowave_create_device_by_compat(
// If non-zero, needs to match VkPhysicalDeviceProperties::vendorID/deviceID.
// Risks picking the wrong device if there are multiple ICDs for the same GPU.
uint32_t vid, uint32_t pid,
const pyrowave_uuid *device_uuid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::deviceUUID
const pyrowave_uuid *driver_uuid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::driverUUID
const pyrowave_luid *device_luid, // If non-NULL, needs to match VkPhysicalDeviceIDProperties::deviceLUID
pyrowave_device *device)
{
return pyrowave_create_device_by_compat2(vid, pid, device_uuid, driver_uuid, device_luid,
VK_QUEUE_GLOBAL_PRIORITY_MEDIUM, device);
}
pyrowave_result pyrowave_create_default_device(pyrowave_device *device)
{
return pyrowave_create_device_by_compat(0, 0, nullptr, nullptr, nullptr, device);
}
static std::mutex global_device_lock;
pyrowave_result pyrowave_create_device(const pyrowave_device_create_info *info, pyrowave_device *out_device)
{
// TODO: Find a better way to do this.
Util::set_thread_logging_interface(&null_logger);
// Safety against concurrent device creations since we're setting global function pointer state here.
std::lock_guard<std::mutex> holder{global_device_lock};
if (!Context::init_loader(info->GetInstanceProcAddr))
return PYROWAVE_ERROR_INVALID_ARGUMENT;
struct MyInstanceFactory : InstanceFactory
{
const pyrowave_device_create_info *info = nullptr;
VkInstance create_instance(const VkInstanceCreateInfo *) override
{
return info->instance;
}
// Lifetime of any data in create info must remain as long as Context is alive.
const VkInstanceCreateInfo *get_existing_create_info() override
{
return info->instance_create_info;
}
bool factory_owns_created_instance() override
{
return true;
}
} instance;
struct MyDeviceFactory : DeviceFactory
{
const pyrowave_device_create_info *info = nullptr;
VkDevice create_device(VkPhysicalDevice, const VkDeviceCreateInfo *) override
{
return info->device;
}
const VkDeviceCreateInfo *get_existing_create_info() override
{
return info->device_create_info;
}
bool factory_owns_created_device() override
{
return true;
}
VkQueue get_queue(uint32_t family_index, uint32_t index) override
{
for (uint32_t i = 0; i < info->queue_info_count; i++)
if (info->queue_info[i].familyIndex == family_index && info->queue_info[i].index == index)
return info->queue_info[i].queue;
return VK_NULL_HANDLE;
}
} device;
instance.info = info;
device.info = info;
auto dev = new pyrowave_device_opaque;
dev->context.set_instance_factory(&instance);
dev->context.set_device_factory(&device);
if (!dev->context.init_instance(nullptr, 0))
return PYROWAVE_ERROR_NO_VULKAN;
if (!dev->context.init_device(info->physical_device, VK_NULL_HANDLE, nullptr, 0))
return PYROWAVE_ERROR_NO_VULKAN;
ContextOptions context_opts = {};
context_opts.memory_priorities = false;
context_opts.lean_memory_mode = true;
dev->device.set_context(dev->context, context_opts);
dev->device.set_queue_lock(
[cb = info->queue_lock_callback, userdata = info->userdata]() {
if (cb)
cb(userdata);
},
[cb = info->queue_unlock_callback, userdata = info->userdata]() {
if (cb)
cb(userdata);
});
*out_device = dev;
return PYROWAVE_SUCCESS;
}
void pyrowave_device_report_performance_stats(pyrowave_device device, pyrowave_message_cb cb, void *userdata, bool reset)
{
Util::set_thread_logging_interface(&null_logger);
device->device.timestamp_log([=](const std::string &tag, const TimestampIntervalReport &report)
{
char msg[256];
snprintf(msg, sizeof(msg), "%s: %.3f ms per frame", tag.c_str(), report.time_per_frame_context * 1e3);
cb(userdata, msg);
});
if (device->device.get_device_features().supports_memory_budget)
{
HeapBudget budgets[VK_MAX_MEMORY_HEAPS];
device->device.get_memory_budget(budgets);
for (uint32_t i = 0; i < device->device.get_memory_properties().memoryHeapCount; i++)
{
char msg[256];
snprintf(msg, sizeof(msg), "Memory Heap %u (%s): "
"MaxSize %.3f MiB, BudgetSize %.3f MiB, DeviceUsage %.3f MiB, TrackedUsage %.3f MiB", i,
(device->device.get_memory_properties().memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
? "DEVICE"
: "HOST",
float(budgets[i].max_size) / (1024.0f * 1024.0f),
float(budgets[i].budget_size) / (1024.0f * 1024.0f),
float(budgets[i].device_usage) / (1024.0f * 1024.0f),
float(budgets[i].tracked_usage) / (1024.0f * 1024.0f));
cb(userdata, msg);
}
}
if (reset)
device->device.timestamp_log_reset();
}
void pyrowave_device_get_vk_device_handles(
pyrowave_device device,
VkInstance *vk_instance, VkPhysicalDevice *vk_physical_device,
VkDevice *vk_device)
{
Util::set_thread_logging_interface(&null_logger);
if (vk_instance)
*vk_instance = device->device.get_instance();
if (vk_physical_device)
*vk_physical_device = device->device.get_physical_device();
if (vk_device)
*vk_device = device->device.get_device();
}
static bool pyrowave_device_confirm_external_semaphore_support(pyrowave_device device)
{
VkSemaphoreTypeCreateInfo type_info = { VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO };
VkPhysicalDeviceExternalSemaphoreInfo sem_info =
{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, &type_info };
VkExternalSemaphoreProperties sem_props = { VK_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_PROPERTIES };
#ifdef _WIN32
sem_info.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT;
#else
sem_info.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
#endif
type_info.semaphoreType = VK_SEMAPHORE_TYPE_BINARY;
vkGetPhysicalDeviceExternalSemaphoreProperties(device->device.get_physical_device(), &sem_info, &sem_props);
if (!(sem_props.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT))
return false;
type_info.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE;
vkGetPhysicalDeviceExternalSemaphoreProperties(device->device.get_physical_device(), &sem_info, &sem_props);
if (!(sem_props.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT))
return false;
#ifdef _WIN32
// Despite being a timeline, D3D12_FENCE was added before TIMELINE was added, and AMD drivers have
// at least gotten confused when trying to use TIMELINE type here in the past.
sem_info.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT;
sem_info.pNext = nullptr;
vkGetPhysicalDeviceExternalSemaphoreProperties(device->device.get_physical_device(), &sem_info, &sem_props);
if (!(sem_props.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT))
return false;
#endif
return true;
}
static bool pyrowave_device_confirm_external_memory_support(pyrowave_device device)
{
VkExternalImageFormatProperties external_props = { VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES };
VkImageFormatProperties2 props2 = { VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, &external_props };
VkPhysicalDeviceExternalImageFormatInfo external_format_info =
{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO };
// TODO: Android?
#ifndef _WIN32
if (!device->device.get_device_features().supports_drm_modifiers)
return false;
#endif
static const VkExternalMemoryHandleTypeFlagBits required_external_types[] = {
#ifdef _WIN32
VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
#else
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
#endif
};
VkPhysicalDeviceImageDrmFormatModifierInfoEXT modifier_info =
{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT };
for (auto type : required_external_types)
{
external_format_info.handleType = type;
external_format_info.pNext = nullptr;
if (type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)
{
VkDrmFormatModifierPropertiesEXT mod;
VkDrmFormatModifierPropertiesListEXT modifier_list =
{ VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT };
modifier_list.drmFormatModifierCount = 1;
modifier_list.pDrmFormatModifierProperties = &mod;
VkFormatProperties3 props3 = { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3, &modifier_list };
device->device.get_format_properties(VK_FORMAT_R8_UNORM, &props3);
if (modifier_list.drmFormatModifierCount != 1)
return false;
modifier_info.drmFormatModifier = mod.drmFormatModifier;
modifier_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
external_format_info.pNext = &modifier_info;
}
if (!device->device.get_image_format_properties(
VK_FORMAT_R8_UNORM, VK_IMAGE_TYPE_2D, type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT ?
VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT : VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
0, &external_format_info, &props2))
return false;
if (!(external_props.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT))
return false;
}
return true;
}
bool pyrowave_device_confirm_interop_support(pyrowave_device device)
{
Util::set_thread_logging_interface(&null_logger);
if (!device->device.get_device_features().supports_external)
return false;
if (!pyrowave_device_confirm_external_semaphore_support(device))
return false;
if (!pyrowave_device_confirm_external_memory_support(device))
return false;
return true;
}
pyrowave_result
pyrowave_device_set_queue_type(pyrowave_device device, VkQueueFlagBits queue_flags)
{
if (!device)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (queue_flags != VK_QUEUE_GRAPHICS_BIT && queue_flags != VK_QUEUE_COMPUTE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
device->queue_type = queue_flags == VK_QUEUE_GRAPHICS_BIT ? CommandBuffer::Type::Generic : CommandBuffer::Type::AsyncCompute;
return PYROWAVE_SUCCESS;
}
void pyrowave_device_destroy(pyrowave_device device)
{
Util::set_thread_logging_interface(&null_logger);
delete device;
}
struct pyrowave_sync_object_opaque
{
Device *device = nullptr;
Semaphore semaphore;
};
pyrowave_result
pyrowave_sync_object_create(const pyrowave_sync_object_create_info *info, pyrowave_sync_object *out_sync)
{
Util::set_thread_logging_interface(&null_logger);
if (!info->device)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if ((info->handle_type & (
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT |
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT |
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT |
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT |
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT)) == 0)
{
return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
auto &device = info->device->device;
if (!device.get_device_features().supports_external)
return PYROWAVE_ERROR_NOT_IMPLEMENTED;
auto sem = device.request_semaphore_external(info->semaphore_type, info->handle_type);
if (!sem)
return PYROWAVE_ERROR_UNSUPPORTED_EXTERNAL_HANDLE;
ExternalHandle ext = {};
ext.handle = (decltype(ext.handle))info->external_handle;
ext.semaphore_handle_type = info->handle_type;
if (ext && !sem->import_from_handle(ext))
return PYROWAVE_ERROR_FAILED_EXTERNAL_HANDLE;
if (!ext && !(info->import_flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT))
return PYROWAVE_ERROR_INVALID_ARGUMENT;
auto *sync = new pyrowave_sync_object_opaque();
sync->device = &device;
sync->semaphore = std::move(sem);
*out_sync = sync;
return PYROWAVE_SUCCESS;
}
VkSemaphore
pyrowave_sync_object_get_semaphore(pyrowave_sync_object sync)
{
if (!sync)
return VK_NULL_HANDLE;
Util::set_thread_logging_interface(&null_logger);
return sync->semaphore->get_semaphore();
}
pyrowave_result
pyrowave_sync_object_export_handle(pyrowave_sync_object sync, pyrowave_os_handle *handle)
{
if (!sync)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
Util::set_thread_logging_interface(&null_logger);
if (auto native_handle = sync->semaphore->export_to_handle())
{
*handle = (pyrowave_os_handle)native_handle.handle;
return PYROWAVE_SUCCESS;
}
else
{
return PYROWAVE_ERROR_FAILED_EXTERNAL_HANDLE;
}
}
pyrowave_result
pyrowave_sync_object_cpu_wait(pyrowave_sync_object sync, uint64_t value, uint64_t timeout)
{
if (!sync || sync->semaphore->get_semaphore_type() != VK_SEMAPHORE_TYPE_TIMELINE)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
Util::set_thread_logging_interface(&null_logger);
return sync->semaphore->wait_timeline_timeout(value, timeout) ? PYROWAVE_SUCCESS : PYROWAVE_TIMEOUT;
}
pyrowave_result
pyrowave_sync_object_cpu_signal(pyrowave_sync_object sync, uint64_t value)
{
if (!sync || sync->semaphore->get_semaphore_type() != VK_SEMAPHORE_TYPE_TIMELINE)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
Util::set_thread_logging_interface(&null_logger);
auto &table = sync->device->get_device_table();
VkSemaphoreSignalInfo signal_info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO };
signal_info.semaphore = sync->semaphore->get_semaphore();
signal_info.value = value;
VkResult vr;
if (table.vkSignalSemaphore)
vr = table.vkSignalSemaphore(sync->device->get_device(), &signal_info);
else if (table.vkSignalSemaphoreKHR)
vr = table.vkSignalSemaphoreKHR(sync->device->get_device(), &signal_info);
else
return PYROWAVE_ERROR_GENERIC;
return vr == VK_SUCCESS ? PYROWAVE_SUCCESS : PYROWAVE_ERROR_GENERIC;
}
void pyrowave_sync_object_destroy(pyrowave_sync_object sync)
{
auto *device = sync->device;
Util::set_thread_logging_interface(&null_logger);
delete sync;
device->next_frame_context();
}
struct pyrowave_image_opaque
{
Device *device = nullptr;
ImageHandle img;
};
pyrowave_result pyrowave_image_create(const pyrowave_image_create_info *info, pyrowave_image *out_image)
{
Util::set_thread_logging_interface(&null_logger);
if (!info->device || !info->image_create_info)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
auto &device = info->device->device;
if (info->image_create_info->sharingMode != VK_SHARING_MODE_EXCLUSIVE)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (info->handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT &&
info->image_create_info->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (info->handle_type != VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT &&
info->image_create_info->tiling != VK_IMAGE_TILING_OPTIMAL)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (info->image_create_info->imageType != VK_IMAGE_TYPE_2D)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
ImageCreateInfo image_create_info = {};
image_create_info.domain = ImageDomain::Physical;
image_create_info.misc = IMAGE_MISC_EXTERNAL_MEMORY_BIT | IMAGE_MISC_NO_DEFAULT_VIEWS_BIT;
image_create_info.external.handle = (decltype(image_create_info.external.handle))info->external_handle;
image_create_info.external.memory_handle_type = info->handle_type;
image_create_info.pnext = const_cast<void *>(info->image_create_info->pNext);
image_create_info.layout = ImageLayout::General;
image_create_info.initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.type = info->image_create_info->imageType;
image_create_info.format = info->image_create_info->format;
image_create_info.flags = info->image_create_info->flags;
image_create_info.width = info->image_create_info->extent.width;
image_create_info.height = info->image_create_info->extent.height;
image_create_info.depth = info->image_create_info->extent.depth;
image_create_info.layers = info->image_create_info->arrayLayers;
image_create_info.levels = info->image_create_info->mipLevels;
image_create_info.samples = info->image_create_info->samples;
image_create_info.usage = info->image_create_info->usage;
if (device.get_device_features().driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
(info->handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT ||
info->handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT))
{
VkFormatProperties3 format_properties = { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3 };
device.get_format_properties(image_create_info.format, &format_properties);
if (format_properties.optimalTilingFeatures & VK_FORMAT_FEATURE_2_VIDEO_ENCODE_INPUT_BIT_KHR)
{
// NVIDIA workaround. For planar formats, the D3D side assumes video compatible layouts.
image_create_info.usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
// If we're on an older driver, just pass it through as-is.
// Normally we have to pass down a codec profile, but this is mostly noise.
if (device.get_device_features().video_maintenance1_features.videoMaintenance1)
image_create_info.flags |= VK_IMAGE_CREATE_VIDEO_PROFILE_INDEPENDENT_BIT_KHR;
}
}
auto img = device.create_image(image_create_info);
if (!img)
return PYROWAVE_ERROR_FAILED_EXTERNAL_HANDLE;
auto *image = new pyrowave_image_opaque();
image->device = &device;
image->img = std::move(img);
*out_image = image;
return PYROWAVE_SUCCESS;
}
VkImage pyrowave_image_get_handle(pyrowave_image image)
{
Util::set_thread_logging_interface(&null_logger);
return image->img->get_image();
}
pyrowave_result
pyrowave_image_get_image_view(pyrowave_image image, VkImageAspectFlagBits aspect,
VkImageUsageFlagBits usage, pyrowave_image_view *view)
{
Util::set_thread_logging_interface(&null_logger);
if ((aspect & (VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_PLANE_0_BIT |
VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)) == 0)
{
return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
if (usage != VK_IMAGE_USAGE_SAMPLED_BIT && usage != VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
auto &img = *image->img;
*view = {};
view->image = img.get_image();
view->image_format = img.get_format();
view->width = img.get_width();
view->height = img.get_height();
// Imported images just use GENERAL.
view->layout = VK_IMAGE_LAYOUT_GENERAL;
if (aspect == VK_IMAGE_ASPECT_COLOR_BIT)
{
if (usage == VK_IMAGE_USAGE_STORAGE_BIT &&
(img.get_format() == VK_FORMAT_R16G16B16A16_SFLOAT ||
img.get_format() == VK_FORMAT_B10G11R11_UFLOAT_PACK32))
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->aspect = VK_IMAGE_ASPECT_COLOR_BIT;
view->swizzle = VK_COMPONENT_SWIZZLE_IDENTITY;
view->view_format = img.get_format();
if (view->view_format == VK_FORMAT_R8G8B8A8_SRGB)
view->view_format = VK_FORMAT_R8G8B8A8_UNORM;
else if (view->view_format == VK_FORMAT_B8G8R8A8_SRGB)
view->view_format = VK_FORMAT_B8G8R8A8_UNORM;
else if (view->view_format == VK_FORMAT_A8B8G8R8_SRGB_PACK32)
view->view_format = VK_FORMAT_A8B8G8R8_UNORM_PACK32;
return PYROWAVE_SUCCESS;
}
// Handle the usual suspects.
switch (img.get_format())
{
// Normal explicit planar formats.
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_R16_UNORM:
view->aspect = VK_IMAGE_ASPECT_COLOR_BIT;
view->swizzle = VK_COMPONENT_SWIZZLE_IDENTITY;
view->view_format = img.get_format();
break;
// Wide-gamut/scRGB capture input for the scaled HDR10 path. The scaler
// samples these formats directly; they are not valid decoder storage planes.
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->aspect = VK_IMAGE_ASPECT_COLOR_BIT;
view->swizzle = VK_COMPONENT_SWIZZLE_IDENTITY;
view->view_format = img.get_format();
break;
case VK_FORMAT_R8G8_UNORM:
case VK_FORMAT_R16G16_UNORM:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (aspect == VK_IMAGE_ASPECT_PLANE_0_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->aspect = VK_IMAGE_ASPECT_COLOR_BIT;
view->swizzle = aspect == VK_IMAGE_ASPECT_PLANE_2_BIT ? VK_COMPONENT_SWIZZLE_G : VK_COMPONENT_SWIZZLE_R;
view->view_format = img.get_format();
break;
// Special 4:4:4 HDR10 format
case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->aspect = VK_IMAGE_ASPECT_COLOR_BIT;
view->view_format = img.get_format();
switch (aspect)
{
case VK_IMAGE_ASPECT_PLANE_0_BIT: view->swizzle = VK_COMPONENT_SWIZZLE_R; break;
case VK_IMAGE_ASPECT_PLANE_1_BIT: view->swizzle = VK_COMPONENT_SWIZZLE_G; break;
case VK_IMAGE_ASPECT_PLANE_2_BIT: view->swizzle = VK_COMPONENT_SWIZZLE_B; break;
default: return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
break;
// 3-plane YCbCr
case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
view->view_format = VK_FORMAT_R8_UNORM;
view->aspect = aspect;
break;
case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
view->view_format = VK_FORMAT_R10X6_UNORM_PACK16;
view->aspect = aspect;
break;
case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
view->view_format = VK_FORMAT_R16_UNORM;
view->aspect = aspect;
break;
// 2-plane YCbCr
case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
case VK_FORMAT_G8_B8R8_2PLANE_444_UNORM:
switch (aspect)
{
case VK_IMAGE_ASPECT_PLANE_0_BIT:
view->view_format = VK_FORMAT_R8_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_0_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_1_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_R;
view->view_format = VK_FORMAT_R8G8_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_2_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_G;
view->view_format = VK_FORMAT_R8G8_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
default:
return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
break;
case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16:
switch (aspect)
{
case VK_IMAGE_ASPECT_PLANE_0_BIT:
view->view_format = VK_FORMAT_R10X6_UNORM_PACK16;
view->aspect = VK_IMAGE_ASPECT_PLANE_0_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_1_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_R;
view->view_format = VK_FORMAT_R10X6G10X6_UNORM_2PACK16;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_2_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_G;
view->view_format = VK_FORMAT_R10X6G10X6_UNORM_2PACK16;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
default:
return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
break;
case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
case VK_FORMAT_G16_B16R16_2PLANE_444_UNORM:
switch (aspect)
{
case VK_IMAGE_ASPECT_PLANE_0_BIT:
view->view_format = VK_FORMAT_R16_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_0_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_1_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_R;
view->view_format = VK_FORMAT_R16G16_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
case VK_IMAGE_ASPECT_PLANE_2_BIT:
if (usage == VK_IMAGE_USAGE_STORAGE_BIT)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
view->swizzle = VK_COMPONENT_SWIZZLE_G;
view->view_format = VK_FORMAT_R16G16_UNORM;
view->aspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
break;
default:
return PYROWAVE_ERROR_INVALID_ARGUMENT;
}
break;
default:
return PYROWAVE_ERROR_NOT_IMPLEMENTED;
}
return PYROWAVE_SUCCESS;
}
void pyrowave_image_destroy(pyrowave_image image)
{
auto *device = image->device;
Util::set_thread_logging_interface(&null_logger);
delete image;
// Pump frame contexts through to make sure memory gets freed eventually.
device->next_frame_context();
}
struct pyrowave_encoder_opaque
{
Device *device = nullptr;
pyrowave_device pyro_device = nullptr;
Encoder encoder;
Fence queued_fence;
BufferHandle queued_meta;
BufferHandle queued_bitstream;
ChromaSubsampling chroma = {};
int width = 0;
int height = 0;
pyrowave_color_metadata color_metadata {
PYROWAVE_COLOR_PRIMARIES_BT709,
PYROWAVE_TRANSFER_BT709,
PYROWAVE_YCBCR_BT709,
PYROWAVE_YCBCR_FULL,
0,
};
// For scaling path.
ImageHandle scaler_planes[3];
VideoScaler scaler;
};
static bool valid_color_metadata(const pyrowave_color_metadata &metadata)
{
const bool valid_primaries = metadata.primaries == PYROWAVE_COLOR_PRIMARIES_BT709 ||
metadata.primaries == PYROWAVE_COLOR_PRIMARIES_BT2020;
const bool valid_transfer = metadata.transfer == PYROWAVE_TRANSFER_BT709 ||
metadata.transfer == PYROWAVE_TRANSFER_PQ ||
metadata.transfer == PYROWAVE_TRANSFER_HLG;
const bool valid_transform = metadata.transform == PYROWAVE_YCBCR_BT709 ||
metadata.transform == PYROWAVE_YCBCR_BT2020;
const bool valid_range = metadata.range == PYROWAVE_YCBCR_FULL ||
metadata.range == PYROWAVE_YCBCR_LIMITED;
return valid_primaries && valid_transfer && valid_transform && valid_range &&
metadata.chroma_siting <= 1;
}
pyrowave_result
pyrowave_encoder_set_color_metadata(pyrowave_encoder encoder,
const pyrowave_color_metadata *metadata)
{
if (!encoder || !metadata || !valid_color_metadata(*metadata))
return PYROWAVE_ERROR_INVALID_ARGUMENT;
encoder->encoder.set_color_metadata({
static_cast<pyrowave_color_primaries>(metadata->primaries),
static_cast<pyrowave_transfer_function>(metadata->transfer),
static_cast<pyrowave_ycbcr_transform>(metadata->transform),
static_cast<pyrowave_ycbcr_range>(metadata->range),
metadata->chroma_siting,
});
encoder->color_metadata = *metadata;
return PYROWAVE_SUCCESS;
}
pyrowave_result
pyrowave_encoder_create(const pyrowave_encoder_create_info *info, pyrowave_encoder *encoder)
{
Util::set_thread_logging_interface(&null_logger);
if (!info->device)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (info->width <= 0 || info->height <= 0)
return PYROWAVE_ERROR_INVALID_ARGUMENT;
if (info->chroma == PYROWAVE_CHROMA_SUBSAMPLING_420 && (info->width % 2 || info->height % 2))
return PYROWAVE_ERROR_INVALID_ARGUMENT;
auto *enc = new pyrowave_encoder_opaque();
enc->pyro_device = info->device;
enc->device = &info->device->device;
enc->chroma = ChromaSubsampling(info->chroma);
enc->width = info->width;
enc->height = info->height;
if (!enc->encoder.init(&info->device->device, info->width, info->height, enc->chroma))
{
delete enc;
return PYROWAVE_ERROR_GENERIC;
}
ResourceLayout layout;
Shaders<> shaders(info->device->device, layout, 0);
enc->scaler.set_program(shaders.scaler);
*encoder = enc;
return PYROWAVE_SUCCESS;
}
struct WrappedViewBuffers : ViewBuffers
{
ImageHandle wrapped_images[3];
ImageViewHandle image_views[3];
bool wrap(Device *device, const pyrowave_gpu_buffers *buffers, VkImageUsageFlags usage);
};
static bool wrap_view(Device *device, const pyrowave_image_view &view, ImageHandle &wrapped_image,
ImageViewHandle &wrapped_view,
VkImageUsageFlags usage)
{
ImageCreateInfo image_info = {};
image_info.usage = usage;
image_info.type = VK_IMAGE_TYPE_2D;
image_info.domain = ImageDomain::Physical;
image_info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
image_info.width = view.width;