-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathimpls.rs
5087 lines (4774 loc) · 174 KB
/
impls.rs
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
use super::*;
use hal::{
adapter::PhysicalDevice,
command::CommandBuffer,
device::{Device, WaitFor},
pool::CommandPool as _,
pso::DescriptorPool,
queue::{Queue as _, QueueFamily},
window::{PresentMode, PresentationSurface as _, Surface as _},
{command as com, memory, pass, pso, queue}, {Features, Instance},
};
use std::{
borrow::{Borrow, Cow},
cell::Cell,
env,
ffi::{CStr, CString},
mem,
os::raw::{c_int, c_void},
ptr,
};
const VERSION: (u32, u32, u32) = (1, 0, 66);
const DRIVER_VERSION: u32 = 1;
unsafe fn make_slice<'a, T: 'a>(pointer: *const T, count: usize) -> &'a [T] {
if count == 0 {
&[]
} else {
slice::from_raw_parts(pointer, count)
}
}
unsafe fn make_slice_mut<'a, T: 'a>(pointer: *mut T, count: usize) -> &'a mut [T] {
if count == 0 {
&mut []
} else {
slice::from_raw_parts_mut(pointer, count)
}
}
fn map_oom(oom: hal::device::OutOfMemory) -> VkResult {
match oom {
hal::device::OutOfMemory::Host => VkResult::VK_ERROR_OUT_OF_HOST_MEMORY,
hal::device::OutOfMemory::Device => VkResult::VK_ERROR_OUT_OF_DEVICE_MEMORY,
}
}
fn map_alloc_error(alloc_error: hal::device::AllocationError) -> VkResult {
match alloc_error {
hal::device::AllocationError::OutOfMemory(oom) => map_oom(oom),
hal::device::AllocationError::TooManyObjects => VkResult::VK_ERROR_TOO_MANY_OBJECTS,
}
}
#[macro_export]
macro_rules! proc_addr {
($name:expr, $($vk:ident, $pfn_vk:ident => $gfx:expr,)*) => (
match $name {
$(
stringify!($vk) => mem::transmute::<$pfn_vk, _>(Some(*&$gfx)),
)*
_ => None
}
);
}
#[inline]
pub unsafe extern "C" fn gfxCreateInstance(
pCreateInfo: *const VkInstanceCreateInfo,
_pAllocator: *const VkAllocationCallbacks,
pInstance: *mut VkInstance,
) -> VkResult {
#[cfg(feature = "env_logger")]
{
let _ = env_logger::try_init();
let backend = if cfg!(feature = "gfx-backend-vulkan") {
"Vulkan"
} else if cfg!(feature = "gfx-backend-dx12") {
"DX12"
} else if cfg!(feature = "gfx-backend-metal") {
"Metal"
} else {
"Other"
};
println!("gfx-portability backend: {}", backend);
}
#[allow(unused_mut)]
// Metal branch performs mutation, so we silence the warning on other backends.
let mut backend =
back::Instance::create("portability", 1).expect("failed to create backend instance");
#[cfg(feature = "gfx-backend-metal")]
{
if let Ok(value) = env::var("GFX_METAL_ARGUMENTS") {
backend.experiments.argument_buffers = match value.to_lowercase().as_str() {
"yes" => true,
"no" => false,
other => panic!("unknown arguments option: {}", other),
};
println!(
"GFX: arguments override {:?}",
backend.experiments.argument_buffers
);
}
}
let adapters = backend
.enumerate_adapters()
.into_iter()
.map(Handle::new)
.collect();
let create_info = &*pCreateInfo;
let application_info = create_info.pApplicationInfo.as_ref();
if let Some(ai) = application_info {
// Compare major and minor parts of version only - patch is ignored
let (supported_major, supported_minor, _) = VERSION;
let requested_major_minor = ai.apiVersion >> 12;
let version_supported = requested_major_minor & (supported_major << 10 | supported_minor)
== requested_major_minor;
if !version_supported {
return VkResult::VK_ERROR_INCOMPATIBLE_DRIVER;
}
}
let mut enabled_extensions = Vec::new();
if create_info.enabledExtensionCount != 0 {
for raw in slice::from_raw_parts(
create_info.ppEnabledExtensionNames,
create_info.enabledExtensionCount as _,
) {
let cstr = CStr::from_ptr(*raw);
if !INSTANCE_EXTENSIONS
.iter()
.any(|&(ref name, _)| name == &cstr.to_bytes_with_nul())
{
return VkResult::VK_ERROR_EXTENSION_NOT_PRESENT;
}
let owned = cstr.to_str().expect("Invalid extension name").to_owned();
enabled_extensions.push(owned);
}
}
*pInstance = Handle::new(RawInstance {
backend,
adapters,
enabled_extensions,
});
VkResult::VK_SUCCESS
}
#[inline]
pub unsafe extern "C" fn gfxDestroyInstance(
instance: VkInstance,
_pAllocator: *const VkAllocationCallbacks,
) {
if let Some(i) = instance.unbox() {
for adapter in i.adapters {
let _ = adapter.unbox();
}
}
#[cfg(feature = "nightly")]
{
Handle::report_leaks();
}
}
#[inline]
pub unsafe extern "C" fn gfxEnumeratePhysicalDevices(
instance: VkInstance,
pPhysicalDeviceCount: *mut u32,
pPhysicalDevices: *mut VkPhysicalDevice,
) -> VkResult {
let num_adapters = instance.adapters.len();
// If NULL, number of devices is returned.
if pPhysicalDevices.is_null() {
*pPhysicalDeviceCount = num_adapters as _;
return VkResult::VK_SUCCESS;
}
let output = slice::from_raw_parts_mut(pPhysicalDevices, *pPhysicalDeviceCount as _);
let num_output = output.len();
let (code, count) = if num_output < num_adapters {
(VkResult::VK_INCOMPLETE, num_output)
} else {
(VkResult::VK_SUCCESS, num_adapters)
};
output[..count].copy_from_slice(&instance.adapters[..count]);
*pPhysicalDeviceCount = count as _;
code
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceQueueFamilyProperties(
adapter: VkPhysicalDevice,
pQueueFamilyPropertyCount: *mut u32,
pQueueFamilyProperties: *mut VkQueueFamilyProperties,
) {
let families = &adapter.queue_families;
// If NULL, number of queue families is returned.
if pQueueFamilyProperties.is_null() {
*pQueueFamilyPropertyCount = families.len() as _;
return;
}
let output = slice::from_raw_parts_mut(pQueueFamilyProperties, *pQueueFamilyPropertyCount as _);
if output.len() > families.len() {
*pQueueFamilyPropertyCount = families.len() as _;
}
for (ref mut out, ref family) in output.iter_mut().zip(families.iter()) {
**out = VkQueueFamilyProperties {
queueFlags: match family.queue_type() {
hal::queue::QueueType::General => {
VkQueueFlagBits::VK_QUEUE_GRAPHICS_BIT as u32
| VkQueueFlagBits::VK_QUEUE_COMPUTE_BIT as u32
| VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT as u32
}
hal::queue::QueueType::Graphics => {
VkQueueFlagBits::VK_QUEUE_GRAPHICS_BIT as u32
| VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT as u32
}
hal::queue::QueueType::Compute => {
VkQueueFlagBits::VK_QUEUE_COMPUTE_BIT as u32
| VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT as u32
}
hal::queue::QueueType::Transfer => VkQueueFlagBits::VK_QUEUE_TRANSFER_BIT as u32,
},
queueCount: family.max_queues() as _,
timestampValidBits: 0, //TODO
minImageTransferGranularity: VkExtent3D {
width: 1,
height: 1,
depth: 1,
}, //TODO
}
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceQueueFamilyProperties2KHR(
adapter: VkPhysicalDevice,
pQueueFamilyPropertyCount: *mut u32,
pQueueFamilyProperties: *mut VkQueueFamilyProperties2KHR,
) {
gfxGetPhysicalDeviceQueueFamilyProperties(
adapter,
pQueueFamilyPropertyCount,
if pQueueFamilyProperties.is_null() {
ptr::null_mut()
} else {
&mut (*pQueueFamilyProperties).queueFamilyProperties
},
);
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceFeatures(
adapter: VkPhysicalDevice,
pFeatures: *mut VkPhysicalDeviceFeatures,
) {
let features = adapter.physical_device.features();
*pFeatures = conv::features_from_hal(features);
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceFeatures2KHR(
adapter: VkPhysicalDevice,
pFeatures: *mut VkPhysicalDeviceFeatures2KHR,
) {
let features = adapter.physical_device.features();
let mut ptr = pFeatures as *const VkStructureType;
while !ptr.is_null() {
ptr = match *ptr {
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR => {
let data = (ptr as *mut VkPhysicalDeviceFeatures2KHR).as_mut().unwrap();
data.features = conv::features_from_hal(features);
data.pNext
}
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PORTABILITY_SUBSET_FEATURES_KHR => {
let data = (ptr as *mut VkPhysicalDevicePortabilitySubsetFeaturesKHR)
.as_mut()
.unwrap();
data.events = VK_TRUE;
if features.contains(hal::Features::TRIANGLE_FAN) {
data.triangleFans = VK_TRUE;
}
if features.contains(hal::Features::SEPARATE_STENCIL_REF_VALUES) {
data.separateStencilMaskRef = VK_TRUE;
}
if features.contains(hal::Features::SAMPLER_MIP_LOD_BIAS) {
data.samplerMipLodBias = VK_TRUE;
}
if features.contains(hal::Features::MUTABLE_COMPARISON_SAMPLER) {
data.mutableComparisonSamplers = VK_TRUE;
}
//TODO: turn these into a feature flags
if cfg!(feature = "gfx-backend-metal") {
data.constantAlphaColorBlendFactors = VK_TRUE;
data.imageViewFormatReinterpretation = VK_TRUE;
}
if cfg!(feature = "gfx-backend-dx12") {
data.vertexAttributeAccessBeyondStride = VK_TRUE;
}
data.pNext
}
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGELESS_FRAMEBUFFER_FEATURES_KHR => {
let data = (ptr as *mut VkPhysicalDeviceImagelessFramebufferFeaturesKHR).as_mut().unwrap();
data.imagelessFramebuffer = true;
data.pNext
}
other => {
warn!("Unrecognized {:?}, skipping", other);
(ptr as *const VkBaseStruct).as_ref().unwrap().pNext
}
} as *const VkStructureType;
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceFormatProperties(
adapter: VkPhysicalDevice,
format: VkFormat,
pFormatProperties: *mut VkFormatProperties,
) {
let properties = adapter
.physical_device
.format_properties(conv::map_format(format));
*pFormatProperties = conv::format_properties_from_hal(properties);
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceFormatProperties2KHR(
adapter: VkPhysicalDevice,
format: VkFormat,
pFormatProperties: *mut VkFormatProperties2KHR,
) {
gfxGetPhysicalDeviceFormatProperties(
adapter,
format,
&mut (*pFormatProperties).formatProperties,
)
}
fn get_physical_device_image_format_properties(
adapter: VkPhysicalDevice,
info: &VkPhysicalDeviceImageFormatInfo2KHR,
) -> Option<VkImageFormatProperties> {
adapter
.physical_device
.image_format_properties(
conv::map_format(info.format).unwrap(),
match info.type_ {
VkImageType::VK_IMAGE_TYPE_1D => 1,
VkImageType::VK_IMAGE_TYPE_2D => 2,
VkImageType::VK_IMAGE_TYPE_3D => 3,
other => panic!("Unexpected image type: {:?}", other),
},
conv::map_tiling(info.tiling),
conv::map_image_usage(info.usage),
conv::map_image_create_flags(info.flags),
)
.map(conv::image_format_properties_from_hal)
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceImageFormatProperties(
adapter: VkPhysicalDevice,
format: VkFormat,
type_: VkImageType,
tiling: VkImageTiling,
usage: VkImageUsageFlags,
flags: VkImageCreateFlags,
pImageFormatProperties: *mut VkImageFormatProperties,
) -> VkResult {
let info = VkPhysicalDeviceImageFormatInfo2KHR {
sType: VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR,
pNext: ptr::null(),
format,
type_,
tiling,
usage,
flags,
};
match get_physical_device_image_format_properties(adapter, &info) {
Some(props) => {
*pImageFormatProperties = props;
VkResult::VK_SUCCESS
}
None => VkResult::VK_ERROR_FORMAT_NOT_SUPPORTED,
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceImageFormatProperties2KHR(
adapter: VkPhysicalDevice,
pImageFormatInfo: *const VkPhysicalDeviceImageFormatInfo2KHR,
pImageFormatProperties: *mut VkImageFormatProperties2KHR,
) -> VkResult {
let mut properties = None;
let mut ptr = pImageFormatInfo as *const VkStructureType;
while !ptr.is_null() {
ptr = match *ptr {
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR => {
let data = (ptr as *const VkPhysicalDeviceImageFormatInfo2KHR)
.as_ref()
.unwrap();
properties = get_physical_device_image_format_properties(adapter, data);
data.pNext
}
other => {
warn!("Unrecognized {:?}, skipping", other);
(ptr as *const VkBaseStruct).as_ref().unwrap().pNext
}
} as *const VkStructureType;
}
match properties {
Some(props) => {
(*pImageFormatProperties).imageFormatProperties = props;
VkResult::VK_SUCCESS
}
None => VkResult::VK_ERROR_FORMAT_NOT_SUPPORTED,
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceProperties(
adapter: VkPhysicalDevice,
pProperties: *mut VkPhysicalDeviceProperties,
) {
let adapter_info = &adapter.info;
let limits = conv::limits_from_hal(adapter.physical_device.properties().limits);
let sparse_properties = mem::zeroed(); // TODO
let (major, minor, patch) = VERSION;
let device_name = {
let c_string = CString::new(adapter_info.name.clone()).unwrap();
let c_str = c_string.as_bytes_with_nul();
let mut name = [0; VK_MAX_PHYSICAL_DEVICE_NAME_SIZE as _];
let len = name.len().min(c_str.len()) - 1;
name[..len].copy_from_slice(&c_str[..len]);
mem::transmute(name)
};
use hal::adapter::DeviceType;
let device_type = match adapter.info.device_type {
DeviceType::IntegratedGpu => VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
DeviceType::DiscreteGpu => VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU,
DeviceType::VirtualGpu => VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU,
DeviceType::Other => VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_OTHER,
DeviceType::Cpu => VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_CPU,
};
*pProperties = VkPhysicalDeviceProperties {
apiVersion: (major << 22) | (minor << 12) | patch,
driverVersion: DRIVER_VERSION,
vendorID: adapter_info.vendor as _,
deviceID: adapter_info.device as _,
deviceType: device_type,
deviceName: device_name,
pipelineCacheUUID: [0; 16usize],
limits,
sparseProperties: sparse_properties,
};
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceProperties2KHR(
adapter: VkPhysicalDevice,
pProperties: *mut VkPhysicalDeviceProperties2KHR,
) {
let mut ptr = pProperties as *const VkStructureType;
while !ptr.is_null() {
ptr = match *ptr {
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR => {
let data =
(ptr as *mut VkPhysicalDeviceProperties2KHR).as_mut().unwrap()
;
gfxGetPhysicalDeviceProperties(adapter, &mut data.properties);
data.pNext
}
VkStructureType::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PORTABILITY_SUBSET_PROPERTIES_KHR => {
let data =
(ptr as *mut VkPhysicalDevicePortabilitySubsetPropertiesKHR).as_mut().unwrap()
;
let limits = adapter.physical_device.properties().limits;
data.minVertexInputBindingStrideAlignment = limits.min_vertex_input_binding_stride_alignment as u32;
data.pNext
}
other => {
warn!("Unrecognized {:?}, skipping", other);
(ptr as *const VkBaseStruct).as_ref().unwrap()
.pNext
}
} as *const VkStructureType;
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceMemoryProperties(
adapter: VkPhysicalDevice,
pMemoryProperties: *mut VkPhysicalDeviceMemoryProperties,
) {
let properties = adapter.physical_device.memory_properties();
let memory_properties = &mut *pMemoryProperties;
let num_types = properties.memory_types.len();
memory_properties.memoryTypeCount = num_types as _;
for i in 0..num_types {
let ty = &properties.memory_types[i];
memory_properties.memoryTypes[i] = VkMemoryType {
propertyFlags: conv::memory_properties_from_hal(ty.properties),
heapIndex: ty.heap_index as _,
};
}
let num_heaps = properties.memory_heaps.len();
memory_properties.memoryHeapCount = num_heaps as _;
for i in 0..num_heaps {
let heap = &properties.memory_heaps[i];
memory_properties.memoryHeaps[i] = VkMemoryHeap {
size: heap.size,
flags: conv::memory_heap_flags_from_hal(heap.flags),
};
}
}
#[inline]
pub unsafe extern "C" fn gfxGetPhysicalDeviceMemoryProperties2KHR(
adapter: VkPhysicalDevice,
pMemoryProperties: *mut VkPhysicalDeviceMemoryProperties2KHR,
) {
gfxGetPhysicalDeviceMemoryProperties(adapter, &mut (*pMemoryProperties).memoryProperties);
}
#[inline]
pub unsafe extern "C" fn gfxGetInstanceProcAddr(
_instance: VkInstance,
pName: *const ::std::os::raw::c_char,
) -> PFN_vkVoidFunction {
let name = CStr::from_ptr(pName);
let name = match name.to_str() {
Ok(name) => name,
Err(_) => return None,
};
let device_addr = gfxGetDeviceProcAddr(DispatchHandle::null(), pName);
if device_addr.is_some() {
return device_addr;
}
proc_addr! { name,
vkCreateInstance, PFN_vkCreateInstance => gfxCreateInstance,
vkDestroyInstance, PFN_vkDestroyInstance => gfxDestroyInstance,
vkCreateDevice, PFN_vkCreateDevice => gfxCreateDevice,
vkGetDeviceProcAddr, PFN_vkGetDeviceProcAddr => gfxGetDeviceProcAddr,
vkEnumeratePhysicalDevices, PFN_vkEnumeratePhysicalDevices => gfxEnumeratePhysicalDevices,
vkEnumerateInstanceLayerProperties, PFN_vkEnumerateInstanceLayerProperties => gfxEnumerateInstanceLayerProperties,
vkEnumerateInstanceExtensionProperties, PFN_vkEnumerateInstanceExtensionProperties => gfxEnumerateInstanceExtensionProperties,
vkEnumerateDeviceExtensionProperties, PFN_vkEnumerateDeviceExtensionProperties => gfxEnumerateDeviceExtensionProperties,
vkEnumerateDeviceLayerProperties, PFN_vkEnumerateDeviceLayerProperties => gfxEnumerateDeviceLayerProperties,
vkGetPhysicalDeviceFeatures, PFN_vkGetPhysicalDeviceFeatures => gfxGetPhysicalDeviceFeatures,
vkGetPhysicalDeviceFeatures2KHR, PFN_vkGetPhysicalDeviceFeatures2KHR => gfxGetPhysicalDeviceFeatures2KHR,
vkGetPhysicalDeviceProperties, PFN_vkGetPhysicalDeviceProperties => gfxGetPhysicalDeviceProperties,
vkGetPhysicalDeviceProperties2KHR, PFN_vkGetPhysicalDeviceProperties2KHR => gfxGetPhysicalDeviceProperties2KHR,
vkGetPhysicalDeviceFormatProperties, PFN_vkGetPhysicalDeviceFormatProperties => gfxGetPhysicalDeviceFormatProperties,
vkGetPhysicalDeviceFormatProperties2KHR, PFN_vkGetPhysicalDeviceFormatProperties2KHR => gfxGetPhysicalDeviceFormatProperties2KHR,
vkGetPhysicalDeviceImageFormatProperties, PFN_vkGetPhysicalDeviceImageFormatProperties => gfxGetPhysicalDeviceImageFormatProperties,
vkGetPhysicalDeviceImageFormatProperties2KHR, PFN_vkGetPhysicalDeviceImageFormatProperties2KHR => gfxGetPhysicalDeviceImageFormatProperties2KHR,
vkGetPhysicalDeviceMemoryProperties, PFN_vkGetPhysicalDeviceMemoryProperties => gfxGetPhysicalDeviceMemoryProperties,
vkGetPhysicalDeviceMemoryProperties2KHR, PFN_vkGetPhysicalDeviceMemoryProperties2KHR => gfxGetPhysicalDeviceMemoryProperties2KHR,
vkGetPhysicalDeviceQueueFamilyProperties, PFN_vkGetPhysicalDeviceQueueFamilyProperties => gfxGetPhysicalDeviceQueueFamilyProperties,
vkGetPhysicalDeviceQueueFamilyProperties2KHR, PFN_vkGetPhysicalDeviceQueueFamilyProperties2KHR => gfxGetPhysicalDeviceQueueFamilyProperties2KHR,
vkGetPhysicalDeviceSparseImageFormatProperties, PFN_vkGetPhysicalDeviceSparseImageFormatProperties => gfxGetPhysicalDeviceSparseImageFormatProperties,
vkGetPhysicalDeviceSparseImageFormatProperties2KHR, PFN_vkGetPhysicalDeviceSparseImageFormatProperties2KHR => gfxGetPhysicalDeviceSparseImageFormatProperties2KHR,
vkGetPhysicalDeviceSurfaceSupportKHR, PFN_vkGetPhysicalDeviceSurfaceSupportKHR => gfxGetPhysicalDeviceSurfaceSupportKHR,
vkGetPhysicalDeviceSurfaceCapabilitiesKHR, PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR => gfxGetPhysicalDeviceSurfaceCapabilitiesKHR,
vkGetPhysicalDeviceSurfaceCapabilities2KHR, PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR => gfxGetPhysicalDeviceSurfaceCapabilities2KHR,
vkGetPhysicalDeviceSurfaceFormatsKHR, PFN_vkGetPhysicalDeviceSurfaceFormatsKHR => gfxGetPhysicalDeviceSurfaceFormatsKHR,
vkGetPhysicalDeviceSurfaceFormats2KHR, PFN_vkGetPhysicalDeviceSurfaceFormats2KHR => gfxGetPhysicalDeviceSurfaceFormats2KHR,
vkGetPhysicalDeviceSurfacePresentModesKHR, PFN_vkGetPhysicalDeviceSurfacePresentModesKHR => gfxGetPhysicalDeviceSurfacePresentModesKHR,
vkGetPhysicalDeviceWin32PresentationSupportKHR, PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR => gfxGetPhysicalDeviceWin32PresentationSupportKHR,
vkCreateXlibSurfaceKHR, PFN_vkCreateXlibSurfaceKHR => gfxCreateXlibSurfaceKHR,
vkCreateXcbSurfaceKHR, PFN_vkCreateXcbSurfaceKHR => gfxCreateXcbSurfaceKHR,
vkCreateWin32SurfaceKHR, PFN_vkCreateWin32SurfaceKHR => gfxCreateWin32SurfaceKHR,
vkCreateMetalSurfaceEXT, PFN_vkCreateMetalSurfaceEXT => gfxCreateMetalSurfaceEXT,
vkCreateMacOSSurfaceMVK, PFN_vkCreateMacOSSurfaceMVK => gfxCreateMacOSSurfaceMVK,
vkDestroySurfaceKHR, PFN_vkDestroySurfaceKHR => gfxDestroySurfaceKHR,
}
}
#[inline]
pub unsafe extern "C" fn gfxGetDeviceProcAddr(
gpu: VkDevice,
pName: *const ::std::os::raw::c_char,
) -> PFN_vkVoidFunction {
let name = CStr::from_ptr(pName);
let name = match name.to_str() {
Ok(name) => name,
Err(_) => return None,
};
// Requesting the function pointer to an extensions which is available but not
// enabled with an valid device requires returning NULL.
if let Some(gpu) = gpu.as_ref() {
match name {
"vkCreateSwapchainKHR"
| "vkDestroySwapchainKHR"
| "vkGetSwapchainImagesKHR"
| "vkAcquireNextImageKHR"
| "vkQueuePresentKHR" => {
if !gpu.has_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME) {
return None;
}
}
_ => {}
}
}
proc_addr! { name,
vkGetDeviceProcAddr, PFN_vkGetDeviceProcAddr => gfxGetDeviceProcAddr,
vkDestroyDevice, PFN_vkDestroyDevice => gfxDestroyDevice,
vkGetDeviceMemoryCommitment, PFN_vkGetDeviceMemoryCommitment => gfxGetDeviceMemoryCommitment,
vkCreateSwapchainKHR, PFN_vkCreateSwapchainKHR => gfxCreateSwapchainKHR,
vkDestroySwapchainKHR, PFN_vkDestroySwapchainKHR => gfxDestroySwapchainKHR,
vkGetSwapchainImagesKHR, PFN_vkGetSwapchainImagesKHR => gfxGetSwapchainImagesKHR,
vkAcquireNextImageKHR, PFN_vkAcquireNextImageKHR => gfxAcquireNextImageKHR,
vkQueuePresentKHR, PFN_vkQueuePresentKHR => gfxQueuePresentKHR,
vkCreateSampler, PFN_vkCreateSampler => gfxCreateSampler,
vkDestroySampler, PFN_vkDestroySampler => gfxDestroySampler,
vkCreateShaderModule, PFN_vkCreateShaderModule => gfxCreateShaderModule,
vkDestroyShaderModule, PFN_vkDestroyShaderModule => gfxDestroyShaderModule,
vkGetDeviceQueue, PFN_vkGetDeviceQueue => gfxGetDeviceQueue,
vkAllocateMemory, PFN_vkAllocateMemory => gfxAllocateMemory,
vkFreeMemory, PFN_vkFreeMemory => gfxFreeMemory,
vkMapMemory, PFN_vkMapMemory => gfxMapMemory,
vkUnmapMemory, PFN_vkUnmapMemory => gfxUnmapMemory,
vkFlushMappedMemoryRanges, PFN_vkFlushMappedMemoryRanges => gfxFlushMappedMemoryRanges,
vkInvalidateMappedMemoryRanges, PFN_vkInvalidateMappedMemoryRanges => gfxInvalidateMappedMemoryRanges,
vkCreateBuffer, PFN_vkCreateBuffer => gfxCreateBuffer,
vkDestroyBuffer, PFN_vkDestroyBuffer => gfxDestroyBuffer,
vkGetBufferMemoryRequirements, PFN_vkGetBufferMemoryRequirements => gfxGetBufferMemoryRequirements,
vkBindBufferMemory, PFN_vkBindBufferMemory => gfxBindBufferMemory,
vkCreateBufferView, PFN_vkCreateBufferView => gfxCreateBufferView,
vkDestroyBufferView, PFN_vkDestroyBufferView => gfxDestroyBufferView,
vkCreateImage, PFN_vkCreateImage => gfxCreateImage,
vkDestroyImage, PFN_vkDestroyImage => gfxDestroyImage,
vkGetImageMemoryRequirements, PFN_vkGetImageMemoryRequirements => gfxGetImageMemoryRequirements,
//vkGetImageMemoryRequirements2KHR, PFN_vkGetImageMemoryRequirements2KHR => gfxGetImageMemoryRequirements2KHR,
vkGetImageSparseMemoryRequirements, PFN_vkGetImageSparseMemoryRequirements => gfxGetImageSparseMemoryRequirements,
vkBindImageMemory, PFN_vkBindImageMemory => gfxBindImageMemory,
vkCreateImageView, PFN_vkCreateImageView => gfxCreateImageView,
vkDestroyImageView, PFN_vkDestroyImageView => gfxDestroyImageView,
vkGetImageSubresourceLayout, PFN_vkGetImageSubresourceLayout => gfxGetImageSubresourceLayout,
vkCreateRenderPass, PFN_vkCreateRenderPass => gfxCreateRenderPass,
vkDestroyRenderPass, PFN_vkDestroyRenderPass => gfxDestroyRenderPass,
vkCreateFramebuffer, PFN_vkCreateFramebuffer => gfxCreateFramebuffer,
vkDestroyFramebuffer, PFN_vkDestroyFramebuffer => gfxDestroyFramebuffer,
vkGetRenderAreaGranularity, PFN_vkGetRenderAreaGranularity => gfxGetRenderAreaGranularity,
vkCreatePipelineLayout, PFN_vkCreatePipelineLayout => gfxCreatePipelineLayout,
vkDestroyPipelineLayout, PFN_vkDestroyPipelineLayout => gfxDestroyPipelineLayout,
vkCreateGraphicsPipelines, PFN_vkCreateGraphicsPipelines => gfxCreateGraphicsPipelines,
vkCreateComputePipelines, PFN_vkCreateComputePipelines => gfxCreateComputePipelines,
vkDestroyPipeline, PFN_vkDestroyPipeline => gfxDestroyPipeline,
vkCreatePipelineCache, PFN_vkCreatePipelineCache => gfxCreatePipelineCache,
vkDestroyPipelineCache, PFN_vkDestroyPipelineCache => gfxDestroyPipelineCache,
vkGetPipelineCacheData, PFN_vkGetPipelineCacheData => gfxGetPipelineCacheData,
vkMergePipelineCaches, PFN_vkMergePipelineCaches => gfxMergePipelineCaches,
vkCreateCommandPool, PFN_vkCreateCommandPool => gfxCreateCommandPool,
vkDestroyCommandPool, PFN_vkDestroyCommandPool => gfxDestroyCommandPool,
vkResetCommandPool, PFN_vkResetCommandPool => gfxResetCommandPool,
vkTrimCommandPoolKHR, PFN_vkTrimCommandPoolKHR => gfxTrimCommandPoolKHR,
vkAllocateCommandBuffers, PFN_vkAllocateCommandBuffers => gfxAllocateCommandBuffers,
vkFreeCommandBuffers, PFN_vkFreeCommandBuffers => gfxFreeCommandBuffers,
vkBeginCommandBuffer, PFN_vkBeginCommandBuffer => gfxBeginCommandBuffer,
vkEndCommandBuffer, PFN_vkEndCommandBuffer => gfxEndCommandBuffer,
vkResetCommandBuffer, PFN_vkResetCommandBuffer => gfxResetCommandBuffer,
vkCreateDescriptorSetLayout, PFN_vkCreateDescriptorSetLayout => gfxCreateDescriptorSetLayout,
vkDestroyDescriptorSetLayout, PFN_vkDestroyDescriptorSetLayout => gfxDestroyDescriptorSetLayout,
vkCreateDescriptorPool, PFN_vkCreateDescriptorPool => gfxCreateDescriptorPool,
vkDestroyDescriptorPool, PFN_vkDestroyDescriptorPool => gfxDestroyDescriptorPool,
vkResetDescriptorPool, PFN_vkResetDescriptorPool => gfxResetDescriptorPool,
vkAllocateDescriptorSets, PFN_vkAllocateDescriptorSets => gfxAllocateDescriptorSets,
vkFreeDescriptorSets, PFN_vkFreeDescriptorSets => gfxFreeDescriptorSets,
vkUpdateDescriptorSets, PFN_vkUpdateDescriptorSets => gfxUpdateDescriptorSets,
vkCreateFence, PFN_vkCreateFence => gfxCreateFence,
vkDestroyFence, PFN_vkDestroyFence => gfxDestroyFence,
vkWaitForFences, PFN_vkWaitForFences => gfxWaitForFences,
vkResetFences, PFN_vkResetFences => gfxResetFences,
vkGetFenceStatus, PFN_vkGetFenceStatus => gfxGetFenceStatus,
vkCreateSemaphore, PFN_vkCreateSemaphore => gfxCreateSemaphore,
vkDestroySemaphore, PFN_vkDestroySemaphore => gfxDestroySemaphore,
vkCreateEvent, PFN_vkCreateEvent => gfxCreateEvent,
vkDestroyEvent, PFN_vkDestroyEvent => gfxDestroyEvent,
vkGetEventStatus, PFN_vkGetEventStatus => gfxGetEventStatus,
vkSetEvent, PFN_vkSetEvent => gfxSetEvent,
vkResetEvent, PFN_vkResetEvent => gfxResetEvent,
vkQueueSubmit, PFN_vkQueueSubmit => gfxQueueSubmit,
vkQueueBindSparse, PFN_vkQueueBindSparse => gfxQueueBindSparse,
vkQueueWaitIdle, PFN_vkQueueWaitIdle => gfxQueueWaitIdle,
vkDeviceWaitIdle, PFN_vkDeviceWaitIdle => gfxDeviceWaitIdle,
vkCreateQueryPool, PFN_vkCreateQueryPool => gfxCreateQueryPool,
vkDestroyQueryPool, PFN_vkDestroyQueryPool => gfxDestroyQueryPool,
vkGetQueryPoolResults, PFN_vkGetQueryPoolResults => gfxGetQueryPoolResults,
vkDebugMarkerSetObjectTagEXT, PFN_vkDebugMarkerSetObjectTagEXT => gfxDebugMarkerSetObjectTagEXT,
vkDebugMarkerSetObjectNameEXT, PFN_vkDebugMarkerSetObjectNameEXT => gfxDebugMarkerSetObjectNameEXT,
vkCmdBindPipeline, PFN_vkCmdBindPipeline => gfxCmdBindPipeline,
vkCmdSetViewport, PFN_vkCmdSetViewport => gfxCmdSetViewport,
vkCmdSetScissor, PFN_vkCmdSetScissor => gfxCmdSetScissor,
vkCmdSetLineWidth, PFN_vkCmdSetLineWidth => gfxCmdSetLineWidth,
vkCmdSetDepthBias, PFN_vkCmdSetDepthBias => gfxCmdSetDepthBias,
vkCmdSetBlendConstants, PFN_vkCmdSetBlendConstants => gfxCmdSetBlendConstants,
vkCmdSetDepthBounds, PFN_vkCmdSetDepthBounds => gfxCmdSetDepthBounds,
vkCmdSetStencilCompareMask, PFN_vkCmdSetStencilCompareMask => gfxCmdSetStencilCompareMask,
vkCmdSetStencilWriteMask, PFN_vkCmdSetStencilWriteMask => gfxCmdSetStencilWriteMask,
vkCmdSetStencilReference, PFN_vkCmdSetStencilReference => gfxCmdSetStencilReference,
vkCmdBindDescriptorSets, PFN_vkCmdBindDescriptorSets => gfxCmdBindDescriptorSets,
vkCmdBindIndexBuffer, PFN_vkCmdBindIndexBuffer => gfxCmdBindIndexBuffer,
vkCmdBindVertexBuffers, PFN_vkCmdBindVertexBuffers => gfxCmdBindVertexBuffers,
vkCmdDraw, PFN_vkCmdDraw => gfxCmdDraw,
vkCmdDrawIndexed, PFN_vkCmdDrawIndexed => gfxCmdDrawIndexed,
vkCmdDrawIndirect, PFN_vkCmdDrawIndirect => gfxCmdDrawIndirect,
vkCmdDrawIndexedIndirect, PFN_vkCmdDrawIndexedIndirect => gfxCmdDrawIndexedIndirect,
vkCmdDispatch, PFN_vkCmdDispatch => gfxCmdDispatch,
vkCmdDispatchIndirect, PFN_vkCmdDispatchIndirect => gfxCmdDispatchIndirect,
vkCmdCopyBuffer, PFN_vkCmdCopyBuffer => gfxCmdCopyBuffer,
vkCmdCopyImage, PFN_vkCmdCopyImage => gfxCmdCopyImage,
vkCmdBlitImage, PFN_vkCmdBlitImage => gfxCmdBlitImage,
vkCmdCopyBufferToImage, PFN_vkCmdCopyBufferToImage => gfxCmdCopyBufferToImage,
vkCmdCopyImageToBuffer, PFN_vkCmdCopyImageToBuffer => gfxCmdCopyImageToBuffer,
vkCmdUpdateBuffer, PFN_vkCmdUpdateBuffer => gfxCmdUpdateBuffer,
vkCmdFillBuffer, PFN_vkCmdFillBuffer => gfxCmdFillBuffer,
vkCmdClearColorImage, PFN_vkCmdClearColorImage => gfxCmdClearColorImage,
vkCmdClearDepthStencilImage, PFN_vkCmdClearDepthStencilImage => gfxCmdClearDepthStencilImage,
vkCmdClearAttachments, PFN_vkCmdClearAttachments => gfxCmdClearAttachments,
vkCmdResolveImage, PFN_vkCmdResolveImage => gfxCmdResolveImage,
vkCmdSetEvent, PFN_vkCmdSetEvent => gfxCmdSetEvent,
vkCmdResetEvent, PFN_vkCmdResetEvent => gfxCmdResetEvent,
vkCmdWaitEvents, PFN_vkCmdWaitEvents => gfxCmdWaitEvents,
vkCmdBeginQuery, PFN_vkCmdBeginQuery => gfxCmdBeginQuery,
vkCmdEndQuery, PFN_vkCmdEndQuery => gfxCmdEndQuery,
vkCmdResetQueryPool, PFN_vkCmdResetQueryPool => gfxCmdResetQueryPool,
vkCmdWriteTimestamp, PFN_vkCmdWriteTimestamp => gfxCmdWriteTimestamp,
vkCmdCopyQueryPoolResults, PFN_vkCmdCopyQueryPoolResults => gfxCmdCopyQueryPoolResults,
vkCmdPushConstants, PFN_vkCmdPushConstants => gfxCmdPushConstants,
vkCmdNextSubpass, PFN_vkCmdNextSubpass => gfxCmdNextSubpass,
vkCmdExecuteCommands, PFN_vkCmdExecuteCommands => gfxCmdExecuteCommands,
vkCmdPipelineBarrier, PFN_vkCmdPipelineBarrier => gfxCmdPipelineBarrier,
vkCmdBeginRenderPass, PFN_vkCmdBeginRenderPass => gfxCmdBeginRenderPass,
vkCmdEndRenderPass, PFN_vkCmdEndRenderPass => gfxCmdEndRenderPass,
vkCmdDebugMarkerBeginEXT, PFN_vkCmdDebugMarkerBeginEXT => gfxCmdDebugMarkerBeginEXT,
vkCmdDebugMarkerEndEXT, PFN_vkCmdDebugMarkerEndEXT => gfxCmdDebugMarkerEndEXT,
vkCmdDebugMarkerInsertEXT, PFN_vkCmdDebugMarkerInsertEXT => gfxCmdDebugMarkerInsertEXT,
}
}
#[inline]
pub unsafe extern "C" fn gfxCreateDevice(
adapter: VkPhysicalDevice,
pCreateInfo: *const VkDeviceCreateInfo,
_pAllocator: *const VkAllocationCallbacks,
pDevice: *mut VkDevice,
) -> VkResult {
let dev_info = &*pCreateInfo;
let queue_infos = slice::from_raw_parts(
dev_info.pQueueCreateInfos,
dev_info.queueCreateInfoCount as _,
);
let max_queue_count = queue_infos
.iter()
.map(|info| info.queueCount as usize)
.max()
.unwrap_or(0);
let priorities = vec![1.0; max_queue_count];
let request_infos = queue_infos
.iter()
.map(|info| {
let family = &adapter.queue_families[info.queueFamilyIndex as usize];
(family, &priorities[..info.queueCount as usize])
})
.collect::<Vec<_>>();
let enabled = if let Some(ef) = dev_info.pEnabledFeatures.as_ref() {
fn feat(on: u32, flag: Features) -> Features {
if on != 0 {
flag
} else {
Features::empty()
}
}
// Attributes on expressions are experimental for now. Use function as workaround.
#[rustfmt::skip]
fn feats(ef: &VkPhysicalDeviceFeatures) -> Features {
feat(ef.robustBufferAccess, Features::ROBUST_BUFFER_ACCESS) |
feat(ef.fullDrawIndexUint32, Features::FULL_DRAW_INDEX_U32) |
feat(ef.imageCubeArray, Features::IMAGE_CUBE_ARRAY) |
feat(ef.independentBlend, Features::INDEPENDENT_BLENDING) |
feat(ef.geometryShader, Features::GEOMETRY_SHADER) |
feat(ef.tessellationShader, Features::TESSELLATION_SHADER) |
feat(ef.sampleRateShading, Features::SAMPLE_RATE_SHADING) |
feat(ef.dualSrcBlend, Features::DUAL_SRC_BLENDING) |
feat(ef.logicOp, Features::LOGIC_OP) |
feat(ef.multiDrawIndirect, Features::MULTI_DRAW_INDIRECT) |
feat(ef.drawIndirectFirstInstance, Features::DRAW_INDIRECT_FIRST_INSTANCE) |
feat(ef.depthClamp, Features::DEPTH_CLAMP) |
feat(ef.depthBiasClamp, Features::DEPTH_BIAS_CLAMP) |
feat(ef.fillModeNonSolid, Features::NON_FILL_POLYGON_MODE) |
feat(ef.depthBounds, Features::DEPTH_BOUNDS) |
feat(ef.wideLines, Features::LINE_WIDTH) |
feat(ef.largePoints, Features::POINT_SIZE) |
feat(ef.alphaToOne, Features::ALPHA_TO_ONE) |
feat(ef.multiViewport, Features::MULTI_VIEWPORTS) |
feat(ef.samplerAnisotropy, Features::SAMPLER_ANISOTROPY) |
feat(ef.textureCompressionETC2, Features::FORMAT_ETC2) |
feat(ef.textureCompressionASTC_LDR, Features::FORMAT_ASTC_LDR) |
feat(ef.textureCompressionBC, Features::FORMAT_BC) |
feat(ef.occlusionQueryPrecise, Features::PRECISE_OCCLUSION_QUERY) |
feat(ef.pipelineStatisticsQuery, Features::PIPELINE_STATISTICS_QUERY) |
feat(ef.vertexPipelineStoresAndAtomics, Features::VERTEX_STORES_AND_ATOMICS) |
feat(ef.fragmentStoresAndAtomics, Features::FRAGMENT_STORES_AND_ATOMICS) |
feat(ef.shaderTessellationAndGeometryPointSize, Features::SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE) |
feat(ef.shaderImageGatherExtended, Features::SHADER_IMAGE_GATHER_EXTENDED) |
feat(ef.shaderStorageImageExtendedFormats, Features::SHADER_STORAGE_IMAGE_EXTENDED_FORMATS) |
feat(ef.shaderStorageImageMultisample, Features::SHADER_STORAGE_IMAGE_MULTISAMPLE) |
feat(ef.shaderStorageImageReadWithoutFormat, Features::SHADER_STORAGE_IMAGE_READ_WITHOUT_FORMAT) |
feat(ef.shaderStorageImageWriteWithoutFormat, Features::SHADER_STORAGE_IMAGE_WRITE_WITHOUT_FORMAT) |
feat(ef.shaderUniformBufferArrayDynamicIndexing, Features::SHADER_UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING) |
feat(ef.shaderSampledImageArrayDynamicIndexing, Features::SHADER_SAMPLED_IMAGE_ARRAY_DYNAMIC_INDEXING) |
feat(ef.shaderStorageBufferArrayDynamicIndexing, Features::SHADER_STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING) |
feat(ef.shaderStorageImageArrayDynamicIndexing, Features::SHADER_STORAGE_IMAGE_ARRAY_DYNAMIC_INDEXING) |
feat(ef.shaderClipDistance, Features::SHADER_CLIP_DISTANCE) |
feat(ef.shaderCullDistance, Features::SHADER_CULL_DISTANCE) |
feat(ef.shaderFloat64, Features::SHADER_FLOAT64) |
feat(ef.shaderInt64, Features::SHADER_INT64) |
feat(ef.shaderInt16, Features::SHADER_INT16) |
feat(ef.shaderResourceResidency, Features::SHADER_RESOURCE_RESIDENCY) |
feat(ef.shaderResourceMinLod, Features::SHADER_RESOURCE_MIN_LOD) |
feat(ef.sparseBinding, Features::SPARSE_BINDING) |
feat(ef.sparseResidencyBuffer, Features::SPARSE_RESIDENCY_BUFFER) |
feat(ef.sparseResidencyImage2D, Features::SPARSE_RESIDENCY_IMAGE_2D) |
feat(ef.sparseResidencyImage3D, Features::SPARSE_RESIDENCY_IMAGE_3D) |
feat(ef.sparseResidency2Samples, Features::SPARSE_RESIDENCY_2_SAMPLES) |
feat(ef.sparseResidency4Samples, Features::SPARSE_RESIDENCY_4_SAMPLES) |
feat(ef.sparseResidency8Samples, Features::SPARSE_RESIDENCY_8_SAMPLES) |
feat(ef.sparseResidency16Samples, Features::SPARSE_RESIDENCY_16_SAMPLES) |
feat(ef.sparseResidencyAliased, Features::SPARSE_RESIDENCY_ALIASED) |
feat(ef.variableMultisampleRate, Features::VARIABLE_MULTISAMPLE_RATE) |
feat(ef.inheritedQueries, Features::INHERITED_QUERIES)
}
feats(&ef)
} else {
Features::empty()
};
#[cfg(feature = "renderdoc")]
let mut renderdoc = {
use renderdoc::RenderDoc;
RenderDoc::new().expect("Failed to init renderdoc")
};
let gpu = adapter.physical_device.open(&request_infos, enabled);
match gpu {
Ok(mut gpu) => {
#[cfg(feature = "gfx-backend-metal")]
{
use back::OnlineRecording;
if let Ok(value) = env::var("GFX_METAL_RECORDING") {
gpu.device.online_recording = match value.to_lowercase().as_str() {
"immediate" => OnlineRecording::Immediate,
"deferred" => OnlineRecording::Deferred,
//"remote" => OnlineRecording::Remote(dispatch::QueuePriority::Default),
other => panic!("unknown recording option: {}", other),
};
println!("GFX: recording override {:?}", gpu.device.online_recording);
}
}
let queues = queue_infos
.iter()
.map(|info| {
let queues = gpu
.queue_groups
.iter()
.position(|group| group.family.0 == info.queueFamilyIndex as usize)
.map(|i| gpu.queue_groups.swap_remove(i).queues)
.unwrap()
.into_iter()
.map(|raw| {
DispatchHandle::new(Queue {
raw,
temp_semaphores: Vec::new(),
})
})
.collect();
(info.queueFamilyIndex, queues)
})
.collect();
#[cfg(feature = "renderdoc")]
let rd_device = {
use renderdoc::api::RenderDocV100;
let rd_device = gpu.device.as_raw();
renderdoc.start_frame_capture(rd_device, ::std::ptr::null());
rd_device
};
let mut enabled_extensions = Vec::new();
if dev_info.enabledExtensionCount != 0 {
for raw in slice::from_raw_parts(
dev_info.ppEnabledExtensionNames,
dev_info.enabledExtensionCount as _,
) {
let cstr = CStr::from_ptr(*raw);
if !DEVICE_EXTENSIONS
.iter()
.any(|&(ref name, _)| name == &cstr.to_bytes_with_nul())
{
return VkResult::VK_ERROR_EXTENSION_NOT_PRESENT;
}
let owned = cstr.to_str().expect("Invalid extension name").to_owned();
enabled_extensions.push(owned);
}
}
let gpu = Gpu {
device: gpu.device,
queues,
enabled_extensions,
#[cfg(feature = "renderdoc")]
renderdoc,
#[cfg(feature = "renderdoc")]
capturing: rd_device as *mut _,
};
*pDevice = DispatchHandle::new(gpu);
VkResult::VK_SUCCESS
}
Err(err) => {
error!("{:?}", err);
conv::map_err_device_creation(err)
}
}
}
#[inline]
pub unsafe extern "C" fn gfxDestroyDevice(
gpu: VkDevice,
_pAllocator: *const VkAllocationCallbacks,
) {
// release all the owned command queues
if let Some(mut d) = gpu.unbox() {
#[cfg(feature = "renderdoc")]
{
use renderdoc::api::RenderDocV100;
let device = gpu.capturing as *mut c_void;
d.renderdoc.end_frame_capture(device as *mut _, ptr::null());
}
for (_, family) in d.queues.drain() {
for queue in family {
let _ = queue.unbox();
}
}
}
}
const INSTANCE_EXTENSIONS: &[(&'static [u8], u32)] = &[
(VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION),
#[cfg(target_os = "linux")]
(
VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
VK_KHR_XLIB_SURFACE_SPEC_VERSION,
),