forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphicsAdapter.Vulkan.cs
More file actions
171 lines (148 loc) · 6.9 KB
/
Copy pathGraphicsAdapter.Vulkan.cs
File metadata and controls
171 lines (148 loc) · 6.9 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
// 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;
namespace Stride.Graphics
{
/// <summary>
/// Provides methods to retrieve and manipulate graphics adapters. This is the equivalent to <see cref="Adapter1"/>.
/// </summary>
/// <msdn-id>ff471329</msdn-id>
/// <unmanaged>IDXGIAdapter1</unmanaged>
/// <unmanaged-short>IDXGIAdapter1</unmanaged-short>
public partial class GraphicsAdapter
{
private VkPhysicalDevice defaultPhysicalDevice;
private VkPhysicalDevice debugPhysicalDevice;
private readonly int adapterOrdinal;
private readonly VkPhysicalDeviceProperties properties;
/// <summary>
/// The raw Vulkan API version supported by the physical device.
/// </summary>
internal VkVersion NativeApiVersion => properties.apiVersion;
private static readonly Dictionary<int, string> VendorNames = new Dictionary<int, string>
{
[0x1002] = "AMD",
[0x1010] = "ImgTec",
[0x10005] = "Mesa",
[0x10DE] = "NVIDIA",
[0x13B5] = "ARM",
[0x5143] = "Qualcomm",
[0x8086] = "INTEL",
};
/// <summary>
/// Initializes a new instance of the <see cref="GraphicsAdapter" /> class.
/// </summary>
/// <param name="physicalDevice">The default factory.</param>
/// <param name="adapterOrdinal">The adapter ordinal.</param>
internal unsafe GraphicsAdapter(VkPhysicalDevice defaultPhysicalDevice, VkPhysicalDeviceProperties properties, VkPhysicalDeviceDriverProperties driverProperties, int adapterOrdinal)
{
this.adapterOrdinal = adapterOrdinal;
this.defaultPhysicalDevice = defaultPhysicalDevice;
this.properties = properties;
DriverInfo = new AdapterDriverInfo
{
GpuName = Description,
VendorId = properties.vendorID,
DeviceId = properties.deviceID,
VendorName = VendorNames.TryGetValue((int)properties.vendorID, out var v) ? v : null,
DriverId = driverProperties.sType == VkStructureType.PhysicalDeviceDriverProperties ? driverProperties.driverID.ToString() : null,
DriverName = driverProperties.sType == VkStructureType.PhysicalDeviceDriverProperties ? Marshal.PtrToStringAnsi((IntPtr)driverProperties.driverName) : null,
DriverInfo = driverProperties.sType == VkStructureType.PhysicalDeviceDriverProperties ? Marshal.PtrToStringAnsi((IntPtr)driverProperties.driverInfo) : null,
DriverVersion = FormatDriverVersion(properties.vendorID, properties.driverVersion),
ApiName = GraphicsDevice.Platform.ToString(),
ApiVersion = FormatApiVersion(properties.apiVersion),
};
// TODO VULKAN
//var displayProperties = physicalDevice.DisplayProperties;
//outputs = new GraphicsOutput[displayProperties.Length];
//for (var i = 0; i < outputs.Length; i++)
// outputs[i] = new GraphicsOutput(this, displayProperties[i], i).DisposeBy(this);
graphicsOutputs = [ new GraphicsOutput() ];
}
// NVIDIA packs 10/8/8/6 bits; Intel-on-Windows packs 18/14; everyone else uses the
// standard VK_MAKE_API_VERSION layout (major<<22 | minor<<12 | patch).
private static string FormatDriverVersion(uint vendorId, uint v) => vendorId switch
{
0x10DE => $"{v >> 22}.{(v >> 14) & 0xFF}.{(v >> 6) & 0xFF}.{v & 0x3F}",
0x8086 when OperatingSystem.IsWindows() => $"{v >> 14}.{v & 0x3FFF}",
_ => $"{v >> 22}.{(v >> 12) & 0x3FF}.{v & 0xFFF}",
};
private static string FormatApiVersion(uint v) => $"{v >> 22}.{(v >> 12) & 0x3FF}.{v & 0xFFF}";
/// <summary>
/// Gets the description of this adapter.
/// </summary>
/// <value>The description.</value>
public unsafe string Description
{
get
{
// TODO VULKAN api
var propertiesCopy = properties;
var description = Marshal.PtrToStringAnsi((IntPtr)propertiesCopy.deviceName);
if (VendorNames.TryGetValue(VendorId, out var vendorName))
description = $"{vendorName} {description}";
return description;
}
}
/// <summary>
/// Gets or sets the vendor identifier.
/// </summary>
/// <value>
/// The vendor identifier.
/// </value>
public int VendorId
{
get
{
return (int)properties.vendorID;
}
}
/// <summary>
/// Determines if this instance of GraphicsAdapter is the default adapter.
/// </summary>
public bool IsDefaultAdapter
{
get
{
return adapterOrdinal == 0;
}
}
internal unsafe VkPhysicalDevice GetPhysicalDevice(bool enableValidation)
{
if (enableValidation)
{
if (debugPhysicalDevice == VkPhysicalDevice.Null)
{
GraphicsAdapterFactoryInstance defaultInstance = GraphicsAdapterFactory.GetInstance(true);
uint physicalDevicesCount = 0;
defaultInstance.NativeInstanceApi.vkEnumeratePhysicalDevices(defaultInstance.NativeInstance, &physicalDevicesCount, null).CheckResult();
Span<VkPhysicalDevice> nativePhysicalDevices = stackalloc VkPhysicalDevice[(int)physicalDevicesCount];
defaultInstance.NativeInstanceApi.vkEnumeratePhysicalDevices(defaultInstance.NativeInstance, nativePhysicalDevices).CheckResult();
debugPhysicalDevice = nativePhysicalDevices[adapterOrdinal];
}
return debugPhysicalDevice;
}
else
{
return defaultPhysicalDevice;
}
}
/// <summary>
/// Tests to see if the adapter supports the requested profile.
/// </summary>
/// <param name="graphicsProfile">The graphics profile.</param>
/// <returns>true if the profile is supported</returns>
public bool IsProfileSupported(GraphicsProfile graphicsProfile)
{
// TODO VULKAN
return true;
//return SharpDX.Direct3D11.Device.IsSupportedFeatureLevel(this.NativeAdapter, (SharpDX.Direct3D.FeatureLevel)graphicsProfile);
}
}
}
#endif