Skip to content

Commit a26b925

Browse files
committed
[Vulkan] Add support for VK_KHR_portability_enumeration
If this extension is not enabled, instance creation will fail when using MoltenVK via the Vulkan loader. This is not necessary when loading the MoltenVK library directly.
1 parent 3e3537a commit a26b925

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/Aardvark.Rendering.Vulkan/Core/Platform/DeviceChooserAuto.fs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ type DeviceChooserAuto =
1919
| VkPhysicalDeviceType.Cpu -> 2
2020
| _ -> 1
2121

22+
static let scorePortability (device: PhysicalDevice) =
23+
if device.HasExtension KHRPortabilitySubset.Name then 0
24+
else 100
25+
2226
/// Selects the device with the highest score according to the given function.
2327
new (score: PhysicalDevice -> int) =
2428
{ inherit DeviceChooser(); score = score }
2529

2630
/// Prefers either dedicated or integrated GPUs.
31+
/// Non-conformant devices are chosen last.
2732
new (preferDedicated: bool) =
28-
let score = if preferDedicated then deviceTypeScoreDedicated else deviceTypeScoreIntegrated
29-
DeviceChooserAuto(fun device -> score device.Type)
33+
let typeScore = if preferDedicated then deviceTypeScoreDedicated else deviceTypeScoreIntegrated
34+
DeviceChooserAuto(fun device -> scorePortability device + typeScore device.Type)
3035

3136
/// Selects the first reported device.
37+
/// Non-conformant devices are chosen last.
3238
new () =
33-
DeviceChooserAuto(fun _ -> 0)
39+
DeviceChooserAuto(scorePortability)
3440

3541
override _.IgnoreCache = true
36-
override this.Choose(devices) = devices |> Array.sortByDescending this.score |> Array.head
42+
override this.Choose(devices) = devices |> Seq.sortByDescending this.score |> Seq.head

src/Aardvark.Rendering.Vulkan/Core/Platform/Extensions.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module Extensions =
3131

3232
let MemoryPriority = EXTMemoryPriority.Name
3333

34+
let PortabilityEnumeration = KHRPortabilityEnumeration.Name
35+
3436
let Shader8Bit16Bit = [
3537
KHR8bitStorage.Name
3638
KHRShaderFloat16Int8.Name

src/Aardvark.Rendering.Vulkan/Core/Platform/Instance.fs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.FSharp.NativeInterop
77
open Aardvark.Base
88
open Aardvark.Rendering
99
open EXTLayerSettings
10+
open KHRPortabilityEnumeration
1011
open Vulkan11
1112

1213
#nowarn "9"
@@ -156,6 +157,12 @@ type Instance(apiVersion : Version, layers : string seq, extensions : string seq
156157
let! pLayerSettingsCreateInfo =
157158
VkLayerSettingsCreateInfoEXT(uint32 layerSettings.Count, layerSettings.Pointer)
158159

160+
let flags =
161+
if isExtensionEnabled KHRPortabilityEnumeration.Name then
162+
VkInstanceCreateFlags.EnumeratePortabilityBitKhr
163+
else
164+
VkInstanceCreateFlags.None
165+
159166
let pNext =
160167
if layerSettings.Count > 0 then
161168
pLayerSettingsCreateInfo.Address
@@ -165,20 +172,22 @@ type Instance(apiVersion : Version, layers : string seq, extensions : string seq
165172
let! pInfo =
166173
VkInstanceCreateInfo(
167174
pNext,
168-
VkInstanceCreateFlags.None,
175+
flags,
169176
pApplicationInfo,
170177
uint32 layers.Length, pLayers,
171178
uint32 extensions.Length, pExtensions
172179
)
173180

174181
let! pInstance = VkInstance.Zero
182+
let result = VkRaw.vkCreateInstance(pInfo, NativePtr.zero, pInstance)
183+
184+
if result = VkResult.Success then
185+
return Some (!!pInstance, apiVersion)
175186

176-
let res = VkRaw.vkCreateInstance(pInfo, NativePtr.zero, pInstance)
177-
let instance = NativePtr.read pInstance
178-
if res = VkResult.Success then
179-
return Some (instance, apiVersion)
180187
elif apiVersion.Minor > 0 then
188+
Log.warn $"[Vulkan] Failed to create instance for version {apiVersion} ({result})"
181189
return tryCreate (Version(apiVersion.Major, apiVersion.Minor - 1, apiVersion.Build))
190+
182191
else
183192
return None
184193
}

src/Aardvark.Rendering.Vulkan/Runtime/Headless.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type HeadlessVulkanApplication(debug: IDebugConfig,
2626
yield Extensions.MemoryBudget
2727
yield Extensions.MemoryPriority
2828
yield Extensions.DeviceFault
29+
yield Extensions.PortabilityEnumeration
2930

3031
yield! Extensions.Maintenance
3132
yield! Extensions.Raytracing debug.RaytracingValidationEnabled

0 commit comments

Comments
 (0)