Repository navigation
Expand file tree
/
Copy pathpyrowave_c_interop_test.cpp
More file actions
2725 lines (2254 loc) · 101 KB
/
Copy pathpyrowave_c_interop_test.cpp
File metadata and controls
2725 lines (2254 loc) · 101 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
#define INITGUID
#include "device.hpp"
#include "context.hpp"
#include "image.hpp"
#include "pyrowave.h"
#include <stdio.h>
#include <cstdlib>
#include <exception>
#include <vector>
#include <cmath>
#include "shaders/slangmosh_test.hpp"
#ifdef _WIN32
#include <d3d11_4.h>
#include <d3d12.h>
#include <dxgi.h>
#include "com_ptr.hpp"
#endif
using namespace Vulkan;
#define ASSERT_THAT(x) do { \
if (!(x)) { fprintf(stderr, "Fatal error executing %s at line %d.\n", #x, __LINE__); std::terminate(); } \
} while(false)
#define CHECKED(x) do { \
pyrowave_result _res = x; \
if (_res != PYROWAVE_SUCCESS) { fprintf(stderr, "Got pyrowave result %d while executing %s at line %d.\n", _res, #x, __LINE__); std::terminate(); } \
} while(false)
#define CHECK_HRESULT(x) ASSERT_THAT(SUCCEEDED(x))
static pyrowave_device create_device_from_granite(Device &device)
{
// Verify that we can create a device from UUID/LUID.
VkPhysicalDeviceIDProperties ids = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 props2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &ids };
vkGetPhysicalDeviceProperties2(device.get_physical_device(), &props2);
pyrowave_uuid device_uuid, driver_uuid;
pyrowave_luid device_luid;
memcpy(device_uuid.uuid, ids.deviceUUID, VK_UUID_SIZE);
memcpy(driver_uuid.uuid, ids.driverUUID, VK_UUID_SIZE);
memcpy(device_luid.luid, ids.deviceLUID, VK_LUID_SIZE);
pyrowave_device pyro_device;
CHECKED(pyrowave_create_device_by_compat(device.get_gpu_properties().vendorID,
device.get_gpu_properties().deviceID, &device_uuid, &driver_uuid,
ids.deviceLUIDValid ? &device_luid : nullptr, VK_QUEUE_GLOBAL_PRIORITY_MEDIUM, &pyro_device));
return pyro_device;
}
static pyrowave_sync_object create_sync_object_from_timeline(pyrowave_device device, SemaphoreHolder &sem)
{
auto exported_timeline = sem.export_to_handle();
ASSERT_THAT(exported_timeline);
pyrowave_sync_object_create_info sync_info = {};
sync_info.device = device;
sync_info.handle_type = exported_timeline.semaphore_handle_type;
sync_info.semaphore_type = VK_SEMAPHORE_TYPE_TIMELINE;
sync_info.external_handle = (pyrowave_os_handle)exported_timeline.handle;
pyrowave_sync_object imported_timeline;
CHECKED(pyrowave_sync_object_create(&sync_info, &imported_timeline));
return imported_timeline;
}
static pyrowave_sync_object create_sync_object_from_binary(pyrowave_device device, SemaphoreHolder &sem)
{
auto exported_timeline = sem.export_to_handle();
ASSERT_THAT(exported_timeline);
pyrowave_sync_object_create_info sync_info = {};
sync_info.device = device;
sync_info.handle_type = exported_timeline.semaphore_handle_type;
sync_info.semaphore_type = VK_SEMAPHORE_TYPE_BINARY;
sync_info.external_handle = (pyrowave_os_handle)exported_timeline.handle;
sync_info.import_flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
pyrowave_sync_object imported_timeline;
CHECKED(pyrowave_sync_object_create(&sync_info, &imported_timeline));
return imported_timeline;
}
static pyrowave_image create_imported_image(pyrowave_device pyro_device, Device &device, Image &img)
{
auto exported = img.export_handle();
ASSERT_THAT(exported);
VkImageCreateInfo image_create_info = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
image_create_info.flags = img.get_create_info().flags;
image_create_info.usage = img.get_create_info().usage;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.format = img.get_create_info().format;
image_create_info.samples = img.get_create_info().samples;
image_create_info.mipLevels = img.get_create_info().levels;
image_create_info.arrayLayers = img.get_create_info().layers;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.imageType = img.get_create_info().type;
image_create_info.extent = { img.get_width(), img.get_height(), img.get_depth() };
pyrowave_image_create_info image_info = {};
image_info.device = pyro_device;
image_info.handle_type = exported.memory_handle_type;
image_info.external_handle = (pyrowave_os_handle)exported.handle;
image_info.image_create_info = &image_create_info;
VkImageDrmFormatModifierExplicitCreateInfoEXT modifier_info =
{ VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT };
VkImageFormatListCreateInfo format_list =
{ VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO };
std::vector<VkSubresourceLayout> drm_plane_layouts;
if (exported.memory_handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)
{
image_create_info.pNext = &modifier_info;
image_create_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
if (img.get_format() == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM)
{
static const VkFormat nv12_formats[] = { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM };
format_list.viewFormatCount = 2;
format_list.pViewFormats = nv12_formats;
}
else if (img.get_format() == VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM)
{
static const VkFormat yuv420p_formats[] = { VK_FORMAT_R8_UNORM };
format_list.viewFormatCount = 1;
format_list.pViewFormats = yuv420p_formats;
}
else
{
format_list.viewFormatCount = 1;
format_list.pViewFormats = &image_create_info.format;
}
// Query which DRM modifier the implementation picked for us and pass it along.
VkImageDrmFormatModifierPropertiesEXT props = { VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT };
device.get_device_table().vkGetImageDrmFormatModifierPropertiesEXT(
device.get_device(), img.get_image(), &props);
modifier_info.drmFormatModifier = props.drmFormatModifier;
VkDrmFormatModifierPropertiesListEXT modifiers = { VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT };
VkFormatProperties3 props3 = { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3, &modifiers };
device.get_format_properties(image_create_info.format, &props3);
std::vector<VkDrmFormatModifierPropertiesEXT> modifiers_props(modifiers.drmFormatModifierCount);
modifiers.pDrmFormatModifierProperties = modifiers_props.data();
device.get_format_properties(image_create_info.format, &props3);
auto itr = std::find_if(modifiers_props.begin(), modifiers_props.end(), [&](const VkDrmFormatModifierPropertiesEXT &prop)
{
return prop.drmFormatModifier == props.drmFormatModifier;
});
ASSERT_THAT(itr != modifiers_props.end());
uint32_t num_memory_planes = itr->drmFormatModifierPlaneCount;
drm_plane_layouts.resize(num_memory_planes);
// DRM format modifiers have per-plane stride information that needs to be queried ...
for (uint32_t i = 0; i < num_memory_planes; i++)
{
VkImageSubresource subresource = {
VkImageAspectFlags(VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT << i),
0, 0
};
device.get_device_table().vkGetImageSubresourceLayout(device.get_device(),
img.get_image(), &subresource, &drm_plane_layouts[i]);
// On import, these must be cleared to be valid.
drm_plane_layouts[i].size = 0;
drm_plane_layouts[i].depthPitch = 0;
if (image_create_info.arrayLayers == 1)
drm_plane_layouts[i].arrayPitch = 0;
}
// Pass it along to import.
modifier_info.drmFormatModifierPlaneCount = num_memory_planes;
modifier_info.pPlaneLayouts = drm_plane_layouts.data();
modifier_info.pNext = &format_list;
}
pyrowave_image imported_image;
CHECKED(pyrowave_image_create(&image_info, &imported_image));
return imported_image;
}
static uint8_t mirror(int v)
{
v &= 511;
if (v > 255)
v = 511 - v;
ASSERT_THAT(v >= 0 && v <= 255);
return uint8_t(v);
}
static ImageHandle create_exportable_test_image(Device &device, VkExternalMemoryHandleTypeFlagBits handle_type,
VkFormat format)
{
auto info = ImageCreateInfo::immutable_2d_image(1280, 720, format);
info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT;
info.misc = IMAGE_MISC_NO_DEFAULT_VIEWS_BIT | IMAGE_MISC_EXTERNAL_MEMORY_BIT;
info.layout = ImageLayout::General;
info.initial_layout = VK_IMAGE_LAYOUT_GENERAL;
info.external.memory_handle_type = handle_type;
// DRM format modifiers require explicit cast list when MUTABLE is used.
VkImageFormatListCreateInfo format_list = { VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO };
VkImageDrmFormatModifierListCreateInfoEXT allowed_modifiers_info =
{ VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT };
std::vector<uint64_t> allowed_modifiers;
if (handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)
{
// Query all modifiers potentially supported by a format.
VkDrmFormatModifierPropertiesListEXT modifiers = { VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT };
VkFormatProperties3 props3 = { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3, &modifiers };
device.get_format_properties(info.format, &props3);
std::vector<VkDrmFormatModifierPropertiesEXT> modifiers_props(modifiers.drmFormatModifierCount);
modifiers.pDrmFormatModifierProperties = modifiers_props.data();
device.get_format_properties(info.format, &props3);
if (format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM)
{
static const VkFormat nv12_formats[] = { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM };
format_list.viewFormatCount = 2;
format_list.pViewFormats = nv12_formats;
}
else if (format == VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM)
{
static const VkFormat yuv420p_formats[] = { VK_FORMAT_R8_UNORM };
format_list.viewFormatCount = 1;
format_list.pViewFormats = yuv420p_formats;
}
else
{
format_list.viewFormatCount = 1;
format_list.pViewFormats = &format;
}
for (uint32_t i = 0; i < modifiers.drmFormatModifierCount; i++)
{
auto &mod = modifiers.pDrmFormatModifierProperties[i];
VkPhysicalDeviceImageDrmFormatModifierInfoEXT modinfo =
{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT, &format_list };
modinfo.drmFormatModifier = mod.drmFormatModifier;
VkImageFormatProperties2 props2 = { VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2 };
// For DRM modifiers, it's a bit involved.
// Step 1 is to query which modifiers are supported for a given image.
if (device.get_image_format_properties(info.format, info.type,
VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
info.usage, info.flags, &modinfo, &props2) &&
info.width <= props2.imageFormatProperties.maxExtent.width &&
info.height <= props2.imageFormatProperties.maxExtent.height)
{
allowed_modifiers.push_back(mod.drmFormatModifier);
}
}
ASSERT_THAT(!allowed_modifiers.empty());
// Implementation picks one of the allowed modifiers.
allowed_modifiers_info.drmFormatModifierCount = allowed_modifiers.size();
allowed_modifiers_info.pDrmFormatModifiers = allowed_modifiers.data();
allowed_modifiers_info.pNext = &format_list;
info.pnext = &allowed_modifiers_info;
}
auto img = device.create_image(info);
if (format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM)
{
auto cmd = device.request_command_buffer();
auto *luma = static_cast<uint8_t *>(cmd->update_image(*img, {}, { 1280, 720, 1 }, 1280, 720, { VK_IMAGE_ASPECT_PLANE_0_BIT, 0, 0, 1 }));
auto *chroma = static_cast<uint16_t *>(cmd->update_image(*img, {}, { 640, 360, 1 }, 640, 360, { VK_IMAGE_ASPECT_PLANE_1_BIT, 0, 0, 1 }));
for (int y = 0; y < 720; y++)
for (int x = 0; x < 1280; x++)
luma[y * 1280 + x] = mirror(y * 3 + 5 * x);
for (int y = 0; y < 360; y++)
for (int x = 0; x < 640; x++)
chroma[y * 640 + x] = (uint16_t(mirror(y * 7 + 5 * x)) << 8) | mirror(y + 3 * x);
device.submit(cmd);
}
return img;
}
static constexpr size_t BitstreamSize = 1000000;
static void send_image_to_encoder(pyrowave_image pyro_image,
pyrowave_sync_object pyro_sync_acquire, uint64_t acquire_value,
pyrowave_sync_object pyro_sync_release, uint64_t release_value,
pyrowave_encoder encoder)
{
pyrowave_gpu_external_reference ref = { pyro_image, VK_QUEUE_FAMILY_EXTERNAL };
pyrowave_gpu_sync_operation acquire = {};
pyrowave_gpu_sync_operation release = {};
pyrowave_gpu_buffers buffers = {};
pyrowave_image_view view = {};
pyrowave_rate_control rate_control = { BitstreamSize };
// Exercise the NV12 scaler path as well.
CHECKED(pyrowave_image_get_image_view(pyro_image,
VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[0]));
CHECKED(pyrowave_image_get_image_view(pyro_image,
VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[1]));
CHECKED(pyrowave_image_get_image_view(pyro_image,
VK_IMAGE_ASPECT_PLANE_2_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[2]));
if (buffers.planes[0].image_format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM)
{
CHECKED(pyrowave_image_get_image_view(pyro_image,
VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &view));
}
acquire.num_images = 1;
acquire.images = &ref;
acquire.sync.semaphore = pyrowave_sync_object_get_semaphore(pyro_sync_acquire);
acquire.sync.value = acquire_value;
release.num_images = 1;
release.images = &ref;
release.sync.semaphore = pyrowave_sync_object_get_semaphore(pyro_sync_release);
release.sync.value = release_value;
if (buffers.planes[0].image_format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM)
{
pyrowave_scaled_encode_info scaled_info = {};
scaled_info.view = view;
scaled_info.force_linear_filtering = true;
scaled_info.skip_dither = true;
scaled_info.input_color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
scaled_info.output_color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
scaled_info.intermediate_plane_format = VK_FORMAT_R8_UNORM;
CHECKED(pyrowave_encoder_encode_gpu_scaled(encoder, &acquire, &release, &scaled_info, &rate_control));
}
else
CHECKED(pyrowave_encoder_encode_gpu(encoder, &acquire, &release, &buffers, &rate_control));
}
static void send_granite_image_to_encoder(Device &device, Image &granite_image, pyrowave_image pyro_image,
SemaphoreHolder &granite_sync, pyrowave_sync_object pyro_sync_acquire, uint64_t acquire_value,
pyrowave_sync_object pyro_sync_release, uint64_t release_value,
pyrowave_encoder encoder)
{
auto cmd = device.request_command_buffer();
cmd->release_image_barrier(granite_image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_QUEUE_FAMILY_EXTERNAL);
device.submit(cmd);
if (acquire_value)
{
auto signal = device.request_timeline_semaphore_as_binary(granite_sync, acquire_value);
device.submit_empty(CommandBuffer::Type::Generic, nullptr, signal.get());
}
#ifdef VULKAN_DEBUG
// VVL doesn't quite understand it when a different device increments the shared timeline.
// It thinks that we're doing a rewind, but that's not the case.
// Only observed on Windows for some reason, but avoids a dumb false positive.
device.wait_idle();
#endif
send_image_to_encoder(pyro_image, pyro_sync_acquire, acquire_value,
pyro_sync_release, release_value, encoder);
// Verify that the release value was signaled properly in finite time.
VkSemaphoreWaitInfo wait_info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO };
wait_info.pSemaphores = &granite_sync.get_semaphore();
wait_info.semaphoreCount = 1;
wait_info.pValues = &release_value;
auto vr = device.get_device_table().vkWaitSemaphores(device.get_device(), &wait_info, 1000000000);
ASSERT_THAT(vr == VK_SUCCESS);
}
static void decode_image(pyrowave_image *pyro_image, bool multiplane,
pyrowave_sync_object pyro_sync, uint64_t release_value,
pyrowave_decoder decoder)
{
// Discard the image(s).
pyrowave_gpu_external_reference acquire_ref[3] = {};
pyrowave_gpu_external_reference release_ref[3] = {};
pyrowave_gpu_sync_operation acquire = {};
pyrowave_gpu_sync_operation release = {};
pyrowave_gpu_buffers buffers = {};
for (int plane = 0; plane < (multiplane ? 1 : 3); plane++)
{
// Discard the output.
acquire_ref[plane] = { pyro_image[plane], VK_QUEUE_FAMILY_IGNORED };
// Release back to other device.
release_ref[plane] = { pyro_image[plane], VK_QUEUE_FAMILY_EXTERNAL };
}
acquire.num_images = multiplane ? 1 : 3;
acquire.images = acquire_ref;
release.num_images = multiplane ? 1 : 3;
release.images = release_ref;
release.sync.semaphore = pyrowave_sync_object_get_semaphore(pyro_sync);
release.sync.value = release_value;
CHECKED(pyrowave_image_get_image_view(pyro_image[0],
VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_USAGE_STORAGE_BIT, &buffers.planes[0]));
CHECKED(pyrowave_image_get_image_view(pyro_image[multiplane ? 0 : 1],
VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_USAGE_STORAGE_BIT, &buffers.planes[1]));
CHECKED(pyrowave_image_get_image_view(pyro_image[multiplane ? 0 : 2],
VK_IMAGE_ASPECT_PLANE_2_BIT, VK_IMAGE_USAGE_STORAGE_BIT, &buffers.planes[2]));
CHECKED(pyrowave_decoder_decode_gpu_buffer(decoder, &acquire, &release, &buffers));
}
static void send_payload_to_decoder(pyrowave_encoder encoder, pyrowave_decoder decoder)
{
size_t num_packets;
CHECKED(pyrowave_encoder_compute_num_packets(encoder, BitstreamSize, &num_packets));
ASSERT_THAT(num_packets == 1);
pyrowave_packet packet;
std::unique_ptr<uint8_t[]> bitstream(new uint8_t[BitstreamSize]);
CHECKED(pyrowave_encoder_packetize(encoder, &packet, BitstreamSize, &num_packets, bitstream.get(), BitstreamSize));
CHECKED(pyrowave_decoder_push_packet(decoder, bitstream.get() + packet.offset, packet.size));
ASSERT_THAT(pyrowave_decoder_decode_is_ready(decoder, false));
}
static void validate_mirror_buffer(const uint8_t *ptr, int width, int height, int row_stride, int dx, int dy)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int reference = mirror(x * dx + y * dy);
int value = ptr[y * row_stride + x];
int d = std::abs(reference - value);
ASSERT_THAT(d <= 1);
}
}
}
static void validate_mirror_buffer(Device &device, Buffer &buf, int width, int height, int dx, int dy)
{
auto *ptr = static_cast<const uint8_t *>(device.map_host_buffer(buf, MEMORY_ACCESS_READ_BIT));
validate_mirror_buffer(ptr, width, height, width, dx, dy);
}
static void validate_granite_image(Device &device, Image &img, SemaphoreHolder &sem, uint64_t acquire_value)
{
auto wait_sem = device.request_timeline_semaphore_as_binary(sem, acquire_value);
wait_sem->signal_external();
device.add_wait_semaphore(CommandBuffer::Type::Generic, std::move(wait_sem), VK_PIPELINE_STAGE_2_COPY_BIT, true);
auto cmd = device.request_command_buffer();
BufferCreateInfo bufinfo = {};
bufinfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufinfo.size = img.get_width() * img.get_height();
bufinfo.domain = BufferDomain::CachedHost;
auto y = device.create_buffer(bufinfo);
bufinfo.size /= 4;
auto cb = device.create_buffer(bufinfo);
auto cr = device.create_buffer(bufinfo);
cmd->acquire_image_barrier(img, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT, VK_QUEUE_FAMILY_EXTERNAL);
cmd->copy_image_to_buffer(*y, img, 0, {}, { img.get_width(), img.get_height(), 1 }, 1280, 720,
{ VK_IMAGE_ASPECT_PLANE_0_BIT, 0, 0, 1 });
cmd->copy_image_to_buffer(*cb, img, 0, {}, { img.get_width() / 2, img.get_height() / 2, 1 }, 640, 360,
{ VK_IMAGE_ASPECT_PLANE_1_BIT, 0, 0, 1 });
cmd->copy_image_to_buffer(*cr, img, 0, {}, { img.get_width() / 2, img.get_height() / 2, 1 }, 640, 360,
{ VK_IMAGE_ASPECT_PLANE_2_BIT, 0, 0, 1 });
cmd->barrier(VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_2_HOST_BIT, VK_ACCESS_2_HOST_WRITE_BIT);
Fence fence;
device.submit(cmd, &fence);
fence->wait();
validate_mirror_buffer(device, *y, 1280, 720, 5, 3);
validate_mirror_buffer(device, *cb, 640, 360, 3, 1);
validate_mirror_buffer(device, *cr, 640, 360, 5, 7);
}
static void test_direct_interop(bool cpu_encoder_first, bool cpu_decoder_first)
{
ASSERT_THAT(Context::init_loader(nullptr));
Context ctx;
ctx.set_num_thread_indices(1);
ctx.set_system_handles({});
VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
app_info.apiVersion = VK_API_VERSION_1_3;
app_info.pApplicationName = "pyrowave-c-test";
app_info.pEngineName = "Granite";
ctx.set_application_info(&app_info);
ASSERT_THAT(ctx.init_instance_and_device(nullptr, 0, nullptr, 0));
Device device;
device.set_context(ctx);
// Fill in a proxy instance create info.
VkInstanceCreateInfo instance_create_info = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
instance_create_info.enabledExtensionCount = device.get_device_features().num_instance_extensions;
instance_create_info.ppEnabledExtensionNames = device.get_device_features().instance_extensions;
instance_create_info.pApplicationInfo = &ctx.get_application_info();
// Fill in a proxy device create info.
VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
VkDeviceQueueCreateInfo queue_info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
queue_info.queueFamilyIndex = device.get_queue_info().family_indices[QUEUE_INDEX_GRAPHICS];
queue_info.queueCount = 1;
device_create_info.pNext = ctx.get_enabled_device_features().pdf2;
device_create_info.enabledExtensionCount = ctx.get_enabled_device_features().num_device_extensions;
device_create_info.ppEnabledExtensionNames = ctx.get_enabled_device_features().device_extensions;
device_create_info.queueCreateInfoCount = 1;
device_create_info.pQueueCreateInfos = &queue_info;
// Hand over a concrete VkQueue we want implementation to use.
pyrowave_device_create_queue_info device_queue_info = {};
device_queue_info.familyIndex = queue_info.queueFamilyIndex;
device_queue_info.index = 0;
device_queue_info.queue = device.get_queue_info().queues[QUEUE_INDEX_GRAPHICS];
pyrowave_device_create_info info = {};
info.GetInstanceProcAddr = vkGetInstanceProcAddr;
info.instance = ctx.get_instance();
info.physical_device = ctx.get_gpu();
info.device = ctx.get_device();
info.device_create_info = &device_create_info;
info.instance_create_info = &instance_create_info;
info.queue_info_count = 1;
info.queue_info = &device_queue_info;
info.userdata = &device;
info.queue_lock_callback = [](void *userdata) { static_cast<Device *>(userdata)->external_queue_lock(); };
info.queue_unlock_callback = [](void *userdata) { static_cast<Device *>(userdata)->external_queue_unlock(); };
pyrowave_encoder encoder;
pyrowave_decoder decoder;
pyrowave_device pyro_device;
CHECKED(pyrowave_create_device(&info, &pyro_device));
pyrowave_encoder_create_info encoder_info = {};
encoder_info.device = pyro_device;
encoder_info.chroma = PYROWAVE_CHROMA_SUBSAMPLING_444;
encoder_info.width = 64;
encoder_info.height = 64;
CHECKED(pyrowave_encoder_create(&encoder_info, &encoder));
pyrowave_decoder_create_info decoder_info = {};
decoder_info.device = pyro_device;
decoder_info.chroma = PYROWAVE_CHROMA_SUBSAMPLING_444;
decoder_info.width = 64;
decoder_info.height = 64;
CHECKED(pyrowave_decoder_create(&decoder_info, &decoder));
uint8_t plane_data[3][64][64];
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
plane_data[0][y][x] = y + x + 1;
plane_data[1][y][x] = y + x + 2;
plane_data[2][y][x] = y + x + 3;
}
}
auto image_info = ImageCreateInfo::immutable_2d_image(64, 64, VK_FORMAT_R8_UNORM);
image_info.layers = 3;
image_info.initial_layout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL;
ImageInitialData initial_data[3] = { { plane_data[0] }, { plane_data[1] }, { plane_data[2] } };
auto input_image = device.create_image(image_info, initial_data);
ASSERT_THAT(input_image);
image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
image_info.initial_layout = VK_IMAGE_LAYOUT_GENERAL;
auto output_image = device.create_image(image_info);
ASSERT_THAT(output_image);
pyrowave_rate_control rate_control = { 100000 };
pyrowave_gpu_buffers gpu_buffers = {};
uint16_t cpu_planes[3][64 * 64] = {};
pyrowave_cpu_buffer cpu_buffer = {};
cpu_buffer.width = 64;
cpu_buffer.height = 64;
cpu_buffer.format = PYROWAVE_CPU_BUFFER_FORMAT_YUV444P10;
for (int plane = 0; plane < 3; plane++)
{
cpu_buffer.data[plane] = cpu_planes[plane];
cpu_buffer.row_stride_in_bytes[plane] = 64 * sizeof(uint16_t);
cpu_buffer.plane_size_in_bytes[plane] = sizeof(cpu_planes[plane]);
}
if (cpu_encoder_first)
{
CHECKED(pyrowave_encoder_encode_cpu(encoder, &cpu_buffer, &rate_control));
size_t cpu_packets = 0;
CHECKED(pyrowave_encoder_compute_num_packets(encoder, rate_control.maximum_bitstream_size, &cpu_packets));
auto invalid = cpu_buffer;
invalid.row_stride_in_bytes[0]++;
ASSERT_THAT(pyrowave_encoder_encode_cpu(encoder, &invalid, &rate_control) ==
PYROWAVE_ERROR_INVALID_ARGUMENT);
}
for (int i = 0; i < 3; i++)
{
gpu_buffers.planes[i].image = input_image->get_image();
gpu_buffers.planes[i].width = 64;
gpu_buffers.planes[i].height = 64;
gpu_buffers.planes[i].image_format = VK_FORMAT_R8_UNORM;
gpu_buffers.planes[i].view_format = VK_FORMAT_R8_UNORM;
gpu_buffers.planes[i].layer = i;
gpu_buffers.planes[i].layout = input_image->get_layout(VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL);
gpu_buffers.planes[i].mip_level = 0;
gpu_buffers.planes[i].aspect = VK_IMAGE_ASPECT_COLOR_BIT;
}
auto cmd = device.request_command_buffer();
// Encode to provided cmd.
// Redirect commands here.
pyrowave_device_set_command_buffer(pyro_device, cmd->get_command_buffer());
CHECKED(pyrowave_encoder_encode_gpu(encoder, nullptr, nullptr, &gpu_buffers, &rate_control));
pyrowave_device_set_command_buffer(pyro_device, VK_NULL_HANDLE);
// Wait on CPU before we call packetization.
Fence fence;
device.submit(cmd, &fence);
fence->wait();
size_t num_packets;
CHECKED(pyrowave_encoder_compute_num_packets(encoder, rate_control.maximum_bitstream_size, &num_packets));
ASSERT_THAT(num_packets == 1);
std::unique_ptr<uint8_t[]> bitstream(new uint8_t[rate_control.maximum_bitstream_size]);
pyrowave_packet packet;
CHECKED(pyrowave_encoder_packetize(encoder, &packet, rate_control.maximum_bitstream_size, &num_packets,
bitstream.get(), rate_control.maximum_bitstream_size));
CHECKED(pyrowave_decoder_push_packet(decoder, bitstream.get() + packet.offset, packet.size));
ASSERT_THAT(pyrowave_decoder_decode_is_ready(decoder, false));
if (cpu_decoder_first)
{
CHECKED(pyrowave_decoder_decode_cpu_buffer_synchronous(decoder, &cpu_buffer));
auto invalid = cpu_buffer;
invalid.row_stride_in_bytes[0]++;
ASSERT_THAT(pyrowave_decoder_decode_cpu_buffer_async(decoder, &invalid, 0) ==
PYROWAVE_ERROR_INVALID_ARGUMENT);
}
for (auto &plane : gpu_buffers.planes)
{
plane.image = output_image->get_image();
plane.layout = VK_IMAGE_LAYOUT_GENERAL;
}
cmd = device.request_command_buffer();
// Redirect commands here.
pyrowave_device_set_command_buffer(pyro_device, cmd->get_command_buffer());
CHECKED(pyrowave_decoder_decode_gpu_buffer(decoder, nullptr, nullptr, &gpu_buffers));
pyrowave_device_set_command_buffer(pyro_device, VK_NULL_HANDLE);
cmd->image_barrier(*output_image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT);
BufferCreateInfo bufinfo = {};
bufinfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufinfo.size = 64 * 64 * 3;
bufinfo.domain = BufferDomain::CachedHost;
auto readback_buffer = device.create_buffer(bufinfo);
cmd->copy_image_to_buffer(*readback_buffer, *output_image, 0, {}, { 64, 64, 1 }, 0, 0,
{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 3 });
cmd->barrier(VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_2_HOST_READ_BIT);
fence.reset();
device.submit(cmd, &fence);
fence->wait();
auto *readback_ptr = static_cast<const uint8_t *>(device.map_host_buffer(*readback_buffer, MEMORY_ACCESS_READ_BIT));
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
int y_delta = std::abs(readback_ptr[0 * 64 * 64 + y * 64 + x] - (y + x + 1));
int cb_delta = std::abs(readback_ptr[1 * 64 * 64 + y * 64 + x] - (y + x + 2));
int cr_delta = std::abs(readback_ptr[2 * 64 * 64 + y * 64 + x] - (y + x + 3));
ASSERT_THAT(y_delta <= 1);
ASSERT_THAT(cb_delta <= 1);
ASSERT_THAT(cr_delta <= 1);
}
}
pyrowave_encoder_destroy(encoder);
pyrowave_decoder_destroy(decoder);
pyrowave_device_destroy(pyro_device);
}
static void test_direct_interop_scaling(VkSamplerYcbcrRange range, uint32_t bit_depth)
{
ASSERT_THAT(Context::init_loader(nullptr));
Context ctx;
ctx.set_num_thread_indices(1);
ctx.set_system_handles({});
VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
app_info.apiVersion = VK_API_VERSION_1_3;
app_info.pApplicationName = "pyrowave-c-test";
app_info.pEngineName = "Granite";
ctx.set_application_info(&app_info);
ASSERT_THAT(ctx.init_instance_and_device(nullptr, 0, nullptr, 0));
Device device;
device.set_context(ctx);
// Fill in a proxy instance create info.
VkInstanceCreateInfo instance_create_info = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
instance_create_info.enabledExtensionCount = device.get_device_features().num_instance_extensions;
instance_create_info.ppEnabledExtensionNames = device.get_device_features().instance_extensions;
instance_create_info.pApplicationInfo = &ctx.get_application_info();
// Fill in a proxy device create info.
VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
VkDeviceQueueCreateInfo queue_info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
queue_info.queueFamilyIndex = device.get_queue_info().family_indices[QUEUE_INDEX_GRAPHICS];
queue_info.queueCount = 1;
device_create_info.pNext = ctx.get_enabled_device_features().pdf2;
device_create_info.enabledExtensionCount = ctx.get_enabled_device_features().num_device_extensions;
device_create_info.ppEnabledExtensionNames = ctx.get_enabled_device_features().device_extensions;
device_create_info.queueCreateInfoCount = 1;
device_create_info.pQueueCreateInfos = &queue_info;
// Hand over a concrete VkQueue we want implementation to use.
pyrowave_device_create_queue_info device_queue_info = {};
device_queue_info.familyIndex = queue_info.queueFamilyIndex;
device_queue_info.index = 0;
device_queue_info.queue = device.get_queue_info().queues[QUEUE_INDEX_GRAPHICS];
pyrowave_device_create_info info = {};
info.GetInstanceProcAddr = vkGetInstanceProcAddr;
info.instance = ctx.get_instance();
info.physical_device = ctx.get_gpu();
info.device = ctx.get_device();
info.device_create_info = &device_create_info;
info.instance_create_info = &instance_create_info;
info.queue_info_count = 1;
info.queue_info = &device_queue_info;
info.userdata = &device;
info.queue_lock_callback = [](void *userdata) { static_cast<Device *>(userdata)->external_queue_lock(); };
info.queue_unlock_callback = [](void *userdata) { static_cast<Device *>(userdata)->external_queue_unlock(); };
pyrowave_encoder encoder;
pyrowave_decoder decoder;
pyrowave_device pyro_device;
CHECKED(pyrowave_create_device(&info, &pyro_device));
pyrowave_encoder_create_info encoder_info = {};
encoder_info.device = pyro_device;
encoder_info.chroma = PYROWAVE_CHROMA_SUBSAMPLING_444;
encoder_info.width = 64;
encoder_info.height = 64;
CHECKED(pyrowave_encoder_create(&encoder_info, &encoder));
pyrowave_decoder_create_info decoder_info = {};
decoder_info.device = pyro_device;
decoder_info.chroma = PYROWAVE_CHROMA_SUBSAMPLING_444;
decoder_info.width = 64;
decoder_info.height = 64;
CHECKED(pyrowave_decoder_create(&decoder_info, &decoder));
uint32_t plane_data[66][67];
for (int y = 0; y < 66; y++)
{
for (int x = 0; x < 67; x++)
{
#if 1
int r = mirror(128 + y * 3 + x * 1);
int g = mirror(128 + y * 5 + x * 3);
int b = mirror(128 + y * 7 + x * 5);
#else
int r = 128;
int g = 128;
int b = 128;
#endif
plane_data[y][x] = r | (g << 8) | (b << 16);
}
}
auto image_info = ImageCreateInfo::immutable_2d_image(67, 66, VK_FORMAT_R8G8B8A8_SRGB);
image_info.initial_layout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL;
image_info.misc = IMAGE_MISC_MUTABLE_SRGB_BIT;
ImageInitialData initial_data = { plane_data };
auto input_image = device.create_image(image_info, &initial_data);
ASSERT_THAT(input_image);
image_info.format = VK_FORMAT_R16_UNORM;
image_info.width = 64;
image_info.height = 64;
image_info.misc = 0;
image_info.layers = 3;
image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
image_info.initial_layout = VK_IMAGE_LAYOUT_GENERAL;
auto output_image = device.create_image(image_info);
ASSERT_THAT(output_image);
pyrowave_rate_control rate_control = { 500000 };
pyrowave_gpu_buffers gpu_buffers = {};
pyrowave_image_view view = {};
view.image = input_image->get_image();
view.width = input_image->get_width();
view.height = input_image->get_height();
view.image_format = input_image->get_format();
view.view_format = VK_FORMAT_R8G8B8A8_UNORM;
view.layer = 0;
view.layout = input_image->get_layout(VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL);
view.mip_level = 0;
view.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
auto cmd = device.request_command_buffer();
// Encode to provided cmd.
// Redirect commands here.
pyrowave_scaled_encode_info scaling = {};
scaling.intermediate_plane_format = VK_FORMAT_R16_UNORM;
scaling.view = view;
scaling.input_color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
scaling.output_color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
scaling.ycbcr_chroma_midpoint = 130.0f / 255.0f;
scaling.ycbcr_range = range;
scaling.ycbcr_range_bit_depth = bit_depth;
scaling.force_linear_filtering = true;
VkRect2D crop_rect = { { 2, 1 }, { 64, 64 } };
scaling.crop_rect = &crop_rect;
pyrowave_device_set_command_buffer(pyro_device, cmd->get_command_buffer());
CHECKED(pyrowave_encoder_encode_gpu_scaled(encoder, nullptr, nullptr, &scaling, &rate_control));
pyrowave_device_set_command_buffer(pyro_device, VK_NULL_HANDLE);
// Wait on CPU before we call packetization.
Fence fence;
device.submit(cmd, &fence);
fence->wait();
size_t num_packets;
CHECKED(pyrowave_encoder_compute_num_packets(encoder, rate_control.maximum_bitstream_size, &num_packets));
ASSERT_THAT(num_packets == 1);
std::unique_ptr<uint8_t[]> bitstream(new uint8_t[rate_control.maximum_bitstream_size]);
pyrowave_packet packet;
CHECKED(pyrowave_encoder_packetize(encoder, &packet, rate_control.maximum_bitstream_size, &num_packets,
bitstream.get(), rate_control.maximum_bitstream_size));
CHECKED(pyrowave_decoder_push_packet(decoder, bitstream.get() + packet.offset, packet.size));
ASSERT_THAT(pyrowave_decoder_decode_is_ready(decoder, false));
for (auto &plane : gpu_buffers.planes)
{
plane.image = output_image->get_image();
plane.layout = VK_IMAGE_LAYOUT_GENERAL;
plane.width = 64;
plane.height = 64;
plane.image_format = VK_FORMAT_R16_UNORM;
plane.view_format = VK_FORMAT_R16_UNORM;
plane.layer = &plane - gpu_buffers.planes;
plane.layout = input_image->get_layout(VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL);
plane.mip_level = 0;
plane.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
}
cmd = device.request_command_buffer();
// Redirect commands here.
pyrowave_device_set_command_buffer(pyro_device, cmd->get_command_buffer());
CHECKED(pyrowave_decoder_decode_gpu_buffer(decoder, nullptr, nullptr, &gpu_buffers));
pyrowave_device_set_command_buffer(pyro_device, VK_NULL_HANDLE);
cmd->image_barrier(*output_image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT);
BufferCreateInfo bufinfo = {};
bufinfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufinfo.size = 64 * 64 * 3 * sizeof(uint16_t);
bufinfo.domain = BufferDomain::CachedHost;
auto readback_buffer = device.create_buffer(bufinfo);
cmd->copy_image_to_buffer(*readback_buffer, *output_image, 0, {}, { 64, 64, 1 }, 0, 0,
{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 3 });
cmd->barrier(VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_2_HOST_READ_BIT);
fence.reset();
device.submit(cmd, &fence);
fence->wait();
auto *readback_ptr = static_cast<const uint16_t *>(device.map_host_buffer(*readback_buffer, MEMORY_ACCESS_READ_BIT));
float luma_scale = 1.0f, luma_offset = 0.0f, chroma_scale = 1.0f;
if (range == VK_SAMPLER_YCBCR_RANGE_ITU_NARROW)
{
float code_unit = float(1u << (bit_depth - 8)) / float((1u << bit_depth) - 1u);
luma_scale = 219.0f * code_unit;
luma_offset = 16.0f * code_unit;
chroma_scale = 224.0f * code_unit;
}
#if 1
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
#if 1
auto r = float(mirror(128 + (y + 1) * 3 + (x + 2) * 1)) / 255.0f;
auto g = float(mirror(128 + (y + 1) * 5 + (x + 2) * 3)) / 255.0f;
auto b = float(mirror(128 + (y + 1) * 7 + (x + 2) * 5)) / 255.0f;
#else
float r = 128.0f / 255.0f;
float g = 128.0f / 255.0f;
float b = 128.0f / 255.0f;
#endif
auto Y = luma_offset + luma_scale * (0.2126f * r + 0.7152f * g + 0.0722f * b);
auto Cb = 130.0f / 255.0f + chroma_scale * (-0.114572f * r - 0.385428f * g + 0.5f * b);
auto Cr = 130.0f / 255.0f + chroma_scale * (0.5f * r - 0.454153f * g - 0.0458471f * b);
float readback_y = float(readback_ptr[0 * 64 * 64 + y * 64 + x]) / float(0xffff);
float readback_cb = float(readback_ptr[1 * 64 * 64 + y * 64 + x]) / float(0xffff);
float readback_cr = float(readback_ptr[2 * 64 * 64 + y * 64 + x]) / float(0xffff);
float y_delta = std::abs(readback_y - Y);
float cb_delta = std::abs(readback_cb - Cb);
float cr_delta = std::abs(readback_cr - Cr);
ASSERT_THAT(y_delta <= 1.0f / 255.0f);
ASSERT_THAT(cb_delta <= 3.0f / 255.0f);
ASSERT_THAT(cr_delta <= 3.0f / 255.0f);
}
}
#endif
pyrowave_encoder_destroy(encoder);
pyrowave_decoder_destroy(decoder);
pyrowave_device_destroy(pyro_device);
}
// Most basic interop scenario, OPAQUE_FD for everything.
static void test_opaque_interop(bool win32_kmt)
{
ASSERT_THAT(Context::init_loader(nullptr));
Context ctx;
ctx.set_num_thread_indices(1);
ctx.set_system_handles({});
ASSERT_THAT(ctx.init_instance_and_device(nullptr, 0, nullptr, 0));
Device device;
device.set_context(ctx);
pyrowave_device pyro_device = create_device_from_granite(device);
auto semaphore_type = win32_kmt ?
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT : ExternalHandle::get_opaque_semaphore_handle_type();
auto memory_type = win32_kmt ?
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT : ExternalHandle::get_opaque_memory_handle_type();
// No impl seems to support OPAQUE_KMT timelines.
auto timeline_sem = device.request_semaphore_external(VK_SEMAPHORE_TYPE_TIMELINE, ExternalHandle::get_opaque_semaphore_handle_type());
ASSERT_THAT(timeline_sem);
pyrowave_sync_object imported_timeline = create_sync_object_from_timeline(pyro_device, *timeline_sem);