-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGraphicsAdapterFactory.Vulkan.cs
More file actions
319 lines (270 loc) · 13.6 KB
/
GraphicsAdapterFactory.Vulkan.cs
File metadata and controls
319 lines (270 loc) · 13.6 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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_GRAPHICS_API_VULKAN
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Vortice.Vulkan;
using static Vortice.Vulkan.Vulkan;
using Stride.Core;
using Stride.Core.Diagnostics;
using System.Text;
using System.Diagnostics;
namespace Stride.Graphics
{
public static partial class GraphicsAdapterFactory
{
private static GraphicsAdapterFactoryInstance defaultInstance;
private static GraphicsAdapterFactoryInstance debugInstance;
/// <summary>
/// Initializes all adapters with the specified factory.
/// </summary>
internal static unsafe void InitializeInternal()
{
var result = vkInitialize();
result.CheckResult();
// Create the default instance to enumerate physical devices
defaultInstance = new GraphicsAdapterFactoryInstance(false);
var nativeInstance = defaultInstance.NativeInstance;
var nativeInstanceApi = defaultInstance.NativeInstanceApi;
uint physicalDevicesCount = 0;
defaultInstance.NativeInstanceApi.vkEnumeratePhysicalDevices(defaultInstance.NativeInstance, &physicalDevicesCount, null).CheckResult();
if (physicalDevicesCount == 0)
throw new Exception("Vulkan: Failed to find GPUs with Vulkan support");
Span<VkPhysicalDevice> nativePhysicalDevices = stackalloc VkPhysicalDevice[(int)physicalDevicesCount];
defaultInstance.NativeInstanceApi.vkEnumeratePhysicalDevices(defaultInstance.NativeInstance, nativePhysicalDevices).CheckResult();
var adapterList = new List<GraphicsAdapter>();
for (int index = 0; index < nativePhysicalDevices.Length; index++)
{
VkPhysicalDevice nativePhysicalDevice = nativePhysicalDevices[index];
List<DisplayInfo> displayInfos = VulkanDisplayHelper.GetDisplayInfos(nativeInstance, nativeInstanceApi, nativePhysicalDevice);
defaultInstance.NativeInstanceApi.vkGetPhysicalDeviceProperties(nativePhysicalDevice, out VkPhysicalDeviceProperties deviceProperties);
var adapter = new GraphicsAdapter(nativePhysicalDevice, displayInfos, deviceProperties, index);
staticCollector.Add(adapter);
adapterList.Add(adapter);
}
defaultAdapter = adapterList.Count > 0 ? adapterList[0] : null;
adapters = adapterList.ToArray();
staticCollector.Add(new AnonymousDisposable(Cleanup));
}
private static void Cleanup()
{
if (defaultInstance != null)
{
defaultInstance.Dispose();
defaultInstance = null;
}
if (debugInstance != null)
{
debugInstance.Dispose();
debugInstance = null;
}
}
/// <summary>
/// Gets the <see cref="GraphicsAdapterFactoryInstance"/> used by all GraphicsAdapter.
/// </summary>
internal static GraphicsAdapterFactoryInstance GetInstance(bool enableValidation)
{
lock (StaticLock)
{
Initialize();
if (enableValidation)
{
return debugInstance ?? (debugInstance = new GraphicsAdapterFactoryInstance(true));
}
else
{
return defaultInstance;
}
}
}
}
internal class GraphicsAdapterFactoryInstance : IDisposable
{
private VkDebugUtilsMessengerEXT debugReportCallback;
internal VkInstance NativeInstance;
internal VkInstanceApi NativeInstanceApi;
internal bool HasXlibSurfaceSupport;
// We use GraphicsDevice (similar to OpenGL)
private static readonly Logger Log = GlobalLogger.GetLogger("GraphicsDevice");
public unsafe GraphicsAdapterFactoryInstance(bool enableValidation)
{
var pEngineName = new VkUtf8ReadOnlyString("Stride"u8);
var applicationInfo = new VkApplicationInfo
{
pEngineName = pEngineName,
apiVersion = new VkVersion(1, 4, 304)
};
Span<VkUtf8String> validationLayerNames = stackalloc VkUtf8String[]
{
VK_LAYER_KHRONOS_VALIDATION_EXTENSION_NAME,
};
var enabledLayerNames = new List<VkUtf8String>();
if (enableValidation)
{
uint count = 0;
var callResult = vkEnumerateInstanceLayerProperties(&count, null);
if (callResult == VkResult.Success && count > 0)
{
VkLayerProperties[] layers = new VkLayerProperties[(int)count];
vkEnumerateInstanceLayerProperties(layers).CheckResult();
for (int index = 0; index < count; index++)
{
var properties = layers[index];
var name = new VkUtf8String(properties.layerName);
var indexOfLayerName = validationLayerNames.IndexOf(name);
if (indexOfLayerName >= 0)
enabledLayerNames.Add(validationLayerNames[indexOfLayerName]);
}
// Check if validation was really available
enableValidation = enabledLayerNames.Count > 0;
}
}
var supportedExtensionNames = stackalloc VkUtf8String[]
{
VK_KHR_SURFACE_EXTENSION_NAME,
VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
VK_KHR_XCB_SURFACE_EXTENSION_NAME,
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
};
var supportedExtensions = new Span<VkUtf8String>(supportedExtensionNames, 6);
var availableExtensionNames = GetAvailableExtensionNames(supportedExtensions);
ValidateSurfaceExtensionNamesAvailability(availableExtensionNames);
var desiredExtensionNames = new HashSet<VkUtf8String>
{
VK_KHR_SURFACE_EXTENSION_NAME,
GetPlatformRelatedSurfaceExtensionName(availableExtensionNames)
};
HasXlibSurfaceSupport = desiredExtensionNames.Contains(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
bool enableDebugReport = enableValidation && availableExtensionNames.Contains(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
if (enableDebugReport)
desiredExtensionNames.Add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
using VkStringArray ppEnabledLayerNames = new(enabledLayerNames);
using VkStringArray ppEnabledExtensionNames = new(desiredExtensionNames);
var instanceCreateInfo = new VkInstanceCreateInfo
{
sType = VkStructureType.InstanceCreateInfo,
pApplicationInfo = &applicationInfo,
enabledLayerCount = ppEnabledLayerNames.Length,
ppEnabledLayerNames = ppEnabledLayerNames,
enabledExtensionCount = ppEnabledExtensionNames.Length,
ppEnabledExtensionNames = ppEnabledExtensionNames,
};
VkResult result = vkCreateInstance(&instanceCreateInfo, out NativeInstance);
if (result != VK_SUCCESS)
throw new InvalidOperationException($"Failed to create vulkan instance: {result}");
NativeInstanceApi = GetApi(NativeInstance);
// Check if validation layer was available (otherwise detected count is 0)
if (enableValidation)
{
var createInfo = new VkDebugUtilsMessengerCreateInfoEXT
{
sType = VkStructureType.DebugUtilsMessengerCreateInfoEXT,
messageSeverity = VkDebugUtilsMessageSeverityFlagsEXT.Verbose | VkDebugUtilsMessageSeverityFlagsEXT.Info | VkDebugUtilsMessageSeverityFlagsEXT.Error | VkDebugUtilsMessageSeverityFlagsEXT.Warning,
messageType = VkDebugUtilsMessageTypeFlagsEXT.General | VkDebugUtilsMessageTypeFlagsEXT.Validation | VkDebugUtilsMessageTypeFlagsEXT.Performance,
pfnUserCallback = &DebugReport
};
NativeInstanceApi.vkCreateDebugUtilsMessengerEXT(NativeInstance, &createInfo, null, out debugReportCallback).CheckResult();
}
}
private unsafe static HashSet<VkUtf8String> GetAvailableExtensionNames(Span<VkUtf8String> supportedExtensionNames)
{
var availableExtensionNames = new HashSet<VkUtf8String>();
vkEnumerateInstanceExtensionProperties(out uint extensionCount).CheckResult();
var extensionProperties = new VkExtensionProperties[extensionCount];
vkEnumerateInstanceExtensionProperties(extensionProperties).CheckResult();
for (int index = 0; index < extensionCount; index++)
{
var extensionProperty = extensionProperties[index];
var name = new VkUtf8String(extensionProperty.extensionName).Span;
var indexOfExtensionName = supportedExtensionNames.IndexOf(name);
if (indexOfExtensionName >= 0)
availableExtensionNames.Add(supportedExtensionNames[indexOfExtensionName]);
}
return availableExtensionNames;
}
private static void ValidateSurfaceExtensionNamesAvailability(HashSet<VkUtf8String> availableExtensionNames)
{
if (!availableExtensionNames.Contains(VK_KHR_SURFACE_EXTENSION_NAME))
throw new InvalidOperationException($"Required extension {Encoding.UTF8.GetString(VK_KHR_SURFACE_EXTENSION_NAME)} is not available");
if (Platform.Type == PlatformType.Windows)
{
if (!availableExtensionNames.Contains(VK_KHR_WIN32_SURFACE_EXTENSION_NAME))
throw new InvalidOperationException($"Required extension {Encoding.UTF8.GetString(VK_KHR_WIN32_SURFACE_EXTENSION_NAME)} is not available");
}
else if (Platform.Type == PlatformType.Android)
{
if (!availableExtensionNames.Contains(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME))
throw new InvalidOperationException($"Required extension {Encoding.UTF8.GetString(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)} is not available");
}
else if (Platform.Type == PlatformType.Linux)
{
if (!availableExtensionNames.Contains(VK_KHR_XLIB_SURFACE_EXTENSION_NAME)
&& !availableExtensionNames.Contains(VK_KHR_XCB_SURFACE_EXTENSION_NAME))
{
throw new InvalidOperationException("None of the supported surface extensions VK_KHR_xcb_surface or VK_KHR_xlib_surface is available");
}
}
}
private static VkUtf8String GetPlatformRelatedSurfaceExtensionName(HashSet<VkUtf8String> availableExtensionNames)
{
VkUtf8String surfaceExtensionName = VK_KHR_SURFACE_EXTENSION_NAME;
if (Platform.Type == PlatformType.Windows)
{
surfaceExtensionName = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
}
else if (Platform.Type == PlatformType.Android)
{
surfaceExtensionName = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
}
else if (Platform.Type == PlatformType.Linux)
{
if (availableExtensionNames.Contains(VK_KHR_XLIB_SURFACE_EXTENSION_NAME))
{
surfaceExtensionName = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
}
else if (availableExtensionNames.Contains(VK_KHR_XCB_SURFACE_EXTENSION_NAME))
{
surfaceExtensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
}
}
return surfaceExtensionName;
}
[UnmanagedCallersOnly]
private unsafe static uint DebugReport(VkDebugUtilsMessageSeverityFlagsEXT severity, VkDebugUtilsMessageTypeFlagsEXT types, VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* userData)
{
var message = new VkUtf8String(pCallbackData->pMessage).ToString();
Debug.WriteLine($"Vulkan: {severity} {message}");
// Redirect to log
if (severity == VkDebugUtilsMessageSeverityFlagsEXT.Error)
{
Log.Error(message);
}
else if (severity == VkDebugUtilsMessageSeverityFlagsEXT.Warning)
{
Log.Warning(message);
}
else if (severity == VkDebugUtilsMessageSeverityFlagsEXT.Info)
{
Log.Info(message);
}
else if (severity == VkDebugUtilsMessageSeverityFlagsEXT.Verbose)
{
Log.Verbose(message);
}
return VK_FALSE;
}
public unsafe void Dispose()
{
if (debugReportCallback != VkDebugUtilsMessengerEXT.Null)
{
NativeInstanceApi.vkDestroyDebugUtilsMessengerEXT(NativeInstance, debugReportCallback, null);
debugReportCallback = VkDebugUtilsMessengerEXT.Null;
}
NativeInstanceApi.vkDestroyInstance(NativeInstance, null);
}
}
}
#endif