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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<ItemGroup>
<Compile Remove="Compiler\CubemapEffect.sdfx.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Silk.NET.Direct3D11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\tests\xunit.runner.stride\xunit.runner.stride.csproj" />
<ProjectReference Include="..\Stride.Engine\Stride.Engine.csproj" />
Expand Down
53 changes: 53 additions & 0 deletions sources/engine/Stride.Graphics.Tests/TestGraphicsProfileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

#if STRIDE_GRAPHICS_API_DIRECT3D

using System;

using Xunit;

using Silk.NET.Core.Native;

namespace Stride.Graphics.Tests;

/// <summary>
/// Regression tests for the GraphicsProfile -> D3DFeatureLevel mapping in GraphicsProfileHelper.
/// Direct3D-only: the helper and D3DFeatureLevel exist only under STRIDE_GRAPHICS_API_DIRECT3D.
/// </summary>
public class TestGraphicsProfileHelper
{
[Theory]
[InlineData(GraphicsProfile.Level_9_1, D3DFeatureLevel.Level91)]
[InlineData(GraphicsProfile.Level_9_2, D3DFeatureLevel.Level92)]
[InlineData(GraphicsProfile.Level_9_3, D3DFeatureLevel.Level93)]
[InlineData(GraphicsProfile.Level_10_0, D3DFeatureLevel.Level100)]
[InlineData(GraphicsProfile.Level_10_1, D3DFeatureLevel.Level101)]
[InlineData(GraphicsProfile.Level_11_0, D3DFeatureLevel.Level110)]
[InlineData(GraphicsProfile.Level_11_1, D3DFeatureLevel.Level111)]
[InlineData(GraphicsProfile.Level_11_2, D3DFeatureLevel.Level111)]
public void Profile_MapsToExpectedFeatureLevel(GraphicsProfile profile, D3DFeatureLevel expected)
{
Assert.Equal(expected, profile.ToFeatureLevel());
}

[Fact]
public void EveryProfile_MapsToADefinedFeatureLevel()
{
// Guards against a profile added later without a matching entry in the mapping.
foreach (GraphicsProfile profile in Enum.GetValues<GraphicsProfile>())
{
var featureLevel = profile.ToFeatureLevel();
Assert.True(Enum.IsDefined(featureLevel),
$"{profile} (0x{(int)profile:X4}) maps to undefined D3DFeatureLevel 0x{(int)featureLevel:X4}");
}
}

[Fact]
public void UnknownProfile_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() => ((GraphicsProfile)0x1234).ToFeatureLevel());
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public bool IsProfileSupported(GraphicsProfile graphicsProfile)
ID3D11DeviceContext* deviceContext = null;

D3DFeatureLevel matchedFeatureLevel = 0;
var featureLevel = (D3DFeatureLevel) graphicsProfile;
var featureLevel = graphicsProfile.ToFeatureLevel();
var featureLevels = stackalloc D3DFeatureLevel[] { featureLevel };

HResult result = d3d11.CreateDevice(pAdapter: null, D3DDriverType.Hardware, Software: IntPtr.Zero,
Expand Down
36 changes: 19 additions & 17 deletions sources/engine/Stride.Graphics/Direct3D/GraphicsProfileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#if STRIDE_GRAPHICS_API_DIRECT3D

using Silk.NET.Core.Native;
using System;

using Stride.Core.UnsafeExtensions;
using Silk.NET.Core.Native;

namespace Stride.Graphics;

Expand All @@ -14,26 +14,28 @@ namespace Stride.Graphics;
/// </summary>
internal static class GraphicsProfileHelper
{
/// <summary>
/// Converts an array of <see cref="GraphicsProfile"/>s to an array of corresponding <see cref="D3DFeatureLevel"/>s.
/// </summary>
/// <param name="profiles">An array of <see cref="GraphicsProfile"/>s to convert.</param>
/// <returns>An array of Direct3D <see cref="D3DFeatureLevel"/>s.</returns>
public static D3DFeatureLevel[] ToFeatureLevel(this GraphicsProfile[] profiles)
{
if (profiles is null or [])
return null;

var featureLevels = profiles.AsReadOnlySpan<GraphicsProfile, D3DFeatureLevel>().ToArray();
return featureLevels;
}

/// <summary>
/// Converts a <see cref="GraphicsProfile"/> to its corresponding <see cref="D3DFeatureLevel"/>.
/// </summary>
/// <param name="profile">A <see cref="GraphicsProfile"/> to convert.</param>
/// <returns>A Direct3D <see cref="D3DFeatureLevel"/>.</returns>
public static D3DFeatureLevel ToFeatureLevel(this GraphicsProfile profile) => (D3DFeatureLevel) profile;
/// <exception cref="ArgumentOutOfRangeException"><paramref name="profile"/> is not a known profile.</exception>
/// <remarks>
/// Direct3D has no feature level 11.2, as 11.2 was an API revision running on 11_1 hardware, so
/// <see cref="GraphicsProfile.Level_11_2"/> maps to 11_1, its real capability tier.
/// </remarks>
public static D3DFeatureLevel ToFeatureLevel(this GraphicsProfile profile) => profile switch
{
GraphicsProfile.Level_9_1 => D3DFeatureLevel.Level91,
GraphicsProfile.Level_9_2 => D3DFeatureLevel.Level92,
GraphicsProfile.Level_9_3 => D3DFeatureLevel.Level93,
GraphicsProfile.Level_10_0 => D3DFeatureLevel.Level100,
GraphicsProfile.Level_10_1 => D3DFeatureLevel.Level101,
GraphicsProfile.Level_11_0 => D3DFeatureLevel.Level110,
GraphicsProfile.Level_11_1 or GraphicsProfile.Level_11_2 => D3DFeatureLevel.Level111,

_ => throw new ArgumentOutOfRangeException(nameof(profile), profile, "Unknown graphics profile.")
};

/// <summary>
/// Converts a <see cref="D3DFeatureLevel"/> to its corresponding <see cref="GraphicsProfile"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static string ShaderProfileFromGraphicsProfile(GraphicsProfile graphicsProfile)
GraphicsProfile.Level_9_3 => "4_0_level_9_3",
GraphicsProfile.Level_10_0 => "4_0",
GraphicsProfile.Level_10_1 => "4_1",
GraphicsProfile.Level_11_0 or GraphicsProfile.Level_11_1 => "5_0",
GraphicsProfile.Level_11_0 or GraphicsProfile.Level_11_1 or GraphicsProfile.Level_11_2 => "5_0",

_ => throw new ArgumentException("Graphics Profile not supported.", nameof(graphicsProfile))
};
Expand Down