aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Backend/Vulkan/VulkanPhysicalDevice.cs
blob: 11444d30c0a996d32c6f66249570192e4a47d9cb (plain) (blame)
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
using Ryujinx.Graphics.Vulkan;
using Silk.NET.Core;
using Silk.NET.Vulkan;
using Silk.NET.Vulkan.Extensions.KHR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace Ryujinx.Ava.Ui.Vulkan
{
    public unsafe class VulkanPhysicalDevice
    {
        private VulkanPhysicalDevice(PhysicalDevice apiHandle, Vk api, uint queueCount, uint queueFamilyIndex)
        {
            InternalHandle = apiHandle;
            Api = api;
            QueueCount = queueCount;
            QueueFamilyIndex = queueFamilyIndex;

            api.GetPhysicalDeviceProperties(apiHandle, out var properties);

            DeviceName = Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName);
            DeviceId = VulkanInitialization.StringFromIdPair(properties.VendorID, properties.DeviceID);

            var version = (Version32)properties.ApiVersion;
            ApiVersion = new Version((int)version.Major, (int)version.Minor, 0, (int)version.Patch);
        }

        internal PhysicalDevice InternalHandle { get; }
        internal Vk Api { get; }
        public uint QueueCount { get; }
        public uint QueueFamilyIndex { get; }
        public IntPtr Handle => InternalHandle.Handle;

        public string DeviceName { get; }
        public string DeviceId { get; }
        public Version ApiVersion { get; }
        public static Dictionary<PhysicalDevice, PhysicalDeviceProperties> PhysicalDevices { get; private set; }
        public static IEnumerable<KeyValuePair<PhysicalDevice, PhysicalDeviceProperties>> SuitableDevices { get; private set; }

        internal static void SelectAvailableDevices(VulkanInstance instance,
            VulkanSurface surface, bool preferDiscreteGpu, string preferredDevice)
        {
            uint physicalDeviceCount;

            instance.Api.EnumeratePhysicalDevices(instance.InternalHandle, &physicalDeviceCount, null).ThrowOnError();

            var physicalDevices = new PhysicalDevice[physicalDeviceCount];

            fixed (PhysicalDevice* pPhysicalDevices = physicalDevices)
            {
                instance.Api.EnumeratePhysicalDevices(instance.InternalHandle, &physicalDeviceCount, pPhysicalDevices)
                    .ThrowOnError();
            }

            PhysicalDevices = new Dictionary<PhysicalDevice, PhysicalDeviceProperties>();

            foreach (var physicalDevice in physicalDevices)
            {
                instance.Api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
                PhysicalDevices.Add(physicalDevice, properties);
            }

            SuitableDevices = PhysicalDevices.Where(x => IsSuitableDevice(
                instance.Api,
                x.Key,
                x.Value,
                surface.ApiHandle,
                out _,
                out _));
        }

        internal static VulkanPhysicalDevice FindSuitablePhysicalDevice(VulkanInstance instance,
            VulkanSurface surface, bool preferDiscreteGpu, string preferredDevice)
        {
            SelectAvailableDevices(instance, surface, preferDiscreteGpu, preferredDevice);

            uint queueFamilyIndex = 0;
            uint queueCount = 0;

            if (!string.IsNullOrWhiteSpace(preferredDevice))
            {
                var physicalDevice = SuitableDevices.FirstOrDefault(x => VulkanInitialization.StringFromIdPair(x.Value.VendorID, x.Value.DeviceID) == preferredDevice);

                queueFamilyIndex = FindSuitableQueueFamily(instance.Api, physicalDevice.Key,
                    surface.ApiHandle, out queueCount);
                if (queueFamilyIndex != int.MaxValue)
                {
                    return new VulkanPhysicalDevice(physicalDevice.Key, instance.Api, queueCount, queueFamilyIndex);
                }
            }

            if (preferDiscreteGpu)
            {
                var discreteGpus = SuitableDevices.Where(p => p.Value.DeviceType == PhysicalDeviceType.DiscreteGpu);

                foreach (var gpu in discreteGpus)
                {
                    queueFamilyIndex = FindSuitableQueueFamily(instance.Api, gpu.Key,
                    surface.ApiHandle, out queueCount);
                    if (queueFamilyIndex != int.MaxValue)
                    {
                        return new VulkanPhysicalDevice(gpu.Key, instance.Api, queueCount, queueFamilyIndex);
                    }
                }
            }

            foreach (var physicalDevice in SuitableDevices)
            {
                queueFamilyIndex = FindSuitableQueueFamily(instance.Api, physicalDevice.Key,
                     surface.ApiHandle, out queueCount);
                if (queueFamilyIndex != int.MaxValue)
                {
                    return new VulkanPhysicalDevice(physicalDevice.Key, instance.Api, queueCount, queueFamilyIndex);
                }
            }

            throw new Exception("No suitable physical device found");
        }

        private static unsafe bool IsSuitableDevice(Vk api, PhysicalDevice physicalDevice, PhysicalDeviceProperties properties, SurfaceKHR surface,
            out uint queueCount, out uint familyIndex)
        {
            queueCount = 0;
            familyIndex = 0;

            if (properties.DeviceType == PhysicalDeviceType.Cpu) return false;

            var extensionMatches = 0;
            uint propertiesCount;

            api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, null).ThrowOnError();

            var extensionProperties = new ExtensionProperties[propertiesCount];

            fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
            {
                api.EnumerateDeviceExtensionProperties(
                    physicalDevice,
                    (byte*)null,
                    &propertiesCount,
                    pExtensionProperties).ThrowOnError();

                for (var i = 0; i < propertiesCount; i++)
                {
                    var extensionName = Marshal.PtrToStringAnsi((IntPtr)pExtensionProperties[i].ExtensionName);

                    if (VulkanInitialization.RequiredExtensions.Contains(extensionName))
                    {
                        extensionMatches++;
                    }
                }
            }

            if (extensionMatches == VulkanInitialization.RequiredExtensions.Length)
            {
                familyIndex = FindSuitableQueueFamily(api, physicalDevice, surface, out queueCount);

                return familyIndex != uint.MaxValue;
            }

            return false;
        }

        internal unsafe string[] GetSupportedExtensions()
        {
            uint propertiesCount;

            Api.EnumerateDeviceExtensionProperties(InternalHandle, (byte*)null, &propertiesCount, null).ThrowOnError();

            var extensionProperties = new ExtensionProperties[propertiesCount];

            fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
            {
                Api.EnumerateDeviceExtensionProperties(InternalHandle, (byte*)null, &propertiesCount, pExtensionProperties)
                    .ThrowOnError();
            }

            return extensionProperties.Select(x => Marshal.PtrToStringAnsi((IntPtr)x.ExtensionName)).ToArray();
        }

        private static uint FindSuitableQueueFamily(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface,
            out uint queueCount)
        {
            const QueueFlags RequiredFlags = QueueFlags.QueueGraphicsBit | QueueFlags.QueueComputeBit;

            var khrSurface = new KhrSurface(api.Context);

            uint propertiesCount;

            api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, null);

            var properties = new QueueFamilyProperties[propertiesCount];

            fixed (QueueFamilyProperties* pProperties = properties)
            {
                api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, pProperties);
            }

            for (uint index = 0; index < propertiesCount; index++)
            {
                var queueFlags = properties[index].QueueFlags;

                khrSurface.GetPhysicalDeviceSurfaceSupport(physicalDevice, index, surface, out var surfaceSupported)
                    .ThrowOnError();

                if (queueFlags.HasFlag(RequiredFlags) && surfaceSupported)
                {
                    queueCount = properties[index].QueueCount;
                    return index;
                }
            }

            queueCount = 0;
            return uint.MaxValue;
        }
    }
}