Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageVersion Include="Microsoft.NETCore.Platforms" Version="7.0.4" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="PolySharp" Version="1.15.0" />
<PackageVersion Include="SDL3-CS" Version="3.3.7" />
<PackageVersion Include="SDL3-CS.Native" Version="3.3.7" />
<PackageVersion Include="ServiceWire" Version="5.6.0" />
<PackageVersion Include="SharpDX" Version="4.2.0" />
<PackageVersion Include="SharpDX.D3DCompiler" Version="4.2.0" />
Expand Down Expand Up @@ -123,4 +125,4 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions sources/engine/Stride.Graphics/Stride.Graphics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
</ProjectReference>
<ProjectReference Include="..\Stride.Shaders\Stride.Shaders.csproj" />
<ProjectReference Include="..\Stride\Stride.csproj" />
<PackageReference Include="SDL3-CS" />
<PackageReference Include="SDL3-CS.Native" />
<PackageReference Include="System.Memory" />
<PackageReference Include="WinPixEventRuntime" Condition="'$(TargetFramework)' == '$(StrideFramework)'" />
<PackageReference Include="Vortice.Vulkan" Condition="'$(TargetFramework)' == '$(StrideFramework)'" />
Expand Down
14 changes: 14 additions & 0 deletions sources/engine/Stride.Graphics/Vulkan/DisplayInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Stride.Core.Mathematics;
using Vortice.Vulkan;

namespace Stride.Graphics
{
public record DisplayInfo(
uint DisplayId,
nint Handle,
string DisplayName,
Rectangle Bounds,
DisplayMode? CurrentMode,
DisplayMode[] SupportedModes,
VkSurfaceFormatKHR[] SupportedFormats);
}
85 changes: 43 additions & 42 deletions sources/engine/Stride.Graphics/Vulkan/GraphicsAdapter.Vulkan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Stride.Core;
using Vortice.Vulkan;

using static Vortice.Vulkan.Vulkan;
Expand All @@ -21,10 +22,11 @@ namespace Stride.Graphics
public partial class GraphicsAdapter
{
private VkPhysicalDevice defaultPhysicalDevice;
private VkPhysicalDevice debugPhysicalDevice;
private Lazy<VkPhysicalDevice> debugPhysicalDevice;

private readonly int adapterOrdinal;
private readonly VkPhysicalDeviceProperties properties;
private readonly string description;
private readonly VkPhysicalDeviceProperties deviceProperties;

private static readonly Dictionary<int, string> VendorNames = new Dictionary<int, string>
{
Expand All @@ -41,18 +43,24 @@ public partial class GraphicsAdapter
/// </summary>
/// <param name="physicalDevice">The default factory.</param>
/// <param name="adapterOrdinal">The adapter ordinal.</param>
internal GraphicsAdapter(VkPhysicalDevice defaultPhysicalDevice, VkPhysicalDeviceProperties properties, int adapterOrdinal)
internal unsafe GraphicsAdapter(VkPhysicalDevice defaultPhysicalDevice,
List<DisplayInfo> displayInfos,
VkPhysicalDeviceProperties deviceProperties,
int adapterOrdinal)
{
this.adapterOrdinal = adapterOrdinal;
this.debugPhysicalDevice = new Lazy<VkPhysicalDevice>(GetDebugPhysicalDevice);
this.defaultPhysicalDevice = defaultPhysicalDevice;
this.properties = properties;

// 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() ];
this.deviceProperties = deviceProperties;

description = Marshal.PtrToStringAnsi((IntPtr)deviceProperties.deviceName);
if (VendorNames.TryGetValue(VendorId, out var vendorName))
description = $"{vendorName} {description}";

graphicsOutputs = new GraphicsOutput[displayInfos.Count];

for (var index = 0; index < graphicsOutputs.Length; index++)
graphicsOutputs[index] = new GraphicsOutput(this, displayInfos[index], index).DisposeBy(this);
}

/// <summary>
Expand All @@ -63,13 +71,6 @@ 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;
}
}
Expand All @@ -84,7 +85,7 @@ public int VendorId
{
get
{
return (int)properties.vendorID;
return (int)deviceProperties.vendorID;
}
}

Expand All @@ -101,26 +102,9 @@ public bool IsDefaultAdapter

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;
}
return enableValidation
? debugPhysicalDevice.Value
: defaultPhysicalDevice;
}

/// <summary>
Expand All @@ -130,9 +114,26 @@ internal unsafe VkPhysicalDevice GetPhysicalDevice(bool enableValidation)
/// <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);
// Lower profiles are always supported on any conformant Vulkan device
if (graphicsProfile <= GraphicsProfile.Level_10_1)
return true;

if (graphicsProfile >= GraphicsProfile.Level_11_0)
return deviceProperties.apiVersion >= VkVersion.Version_1_1;

return false;
}

private unsafe VkPhysicalDevice GetDebugPhysicalDevice()
{
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();

return nativePhysicalDevices[adapterOrdinal];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,28 @@ internal static unsafe void InitializeInternal()

// 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++)
{
VkPhysicalDeviceProperties properties;
defaultInstance.NativeInstanceApi.vkGetPhysicalDeviceProperties(nativePhysicalDevices[index], out properties);
var adapter = new GraphicsAdapter(nativePhysicalDevices[index], properties, 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);
}
Expand Down
Loading