diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 5e1d264c..0cf0f324 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -22,6 +22,7 @@ + diff --git a/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs new file mode 100644 index 00000000..c9cbc8f0 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs @@ -0,0 +1,14 @@ +using System; + +namespace ComputeSharp.NetStandard.System.Runtime.InteropServices; + +/// +/// Any method marked with can be directly called from native code. +/// +/// +/// This polyfill is deliberately in the wrong namespace to avoid the C# compiler failing to build below .NET 5. +/// +[AttributeUsage(AttributeTargets.Method, Inherited = false)] +public sealed class UnmanagedCallersOnlyAttribute : Attribute +{ +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs index efc66035..1c41b5e7 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs @@ -7,6 +7,7 @@ using TerraFX.Interop.Windows; using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers; #else using RuntimeHelpers = ComputeSharp.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers; +using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute; #endif #pragma warning disable CA1416 @@ -85,6 +86,32 @@ internal static partial class DeviceHelper /// private unsafe struct IDXGIFactory4As6Backcompat { +#if !NET6_0_OR_GREATER + /// + private delegate uint ReleaseDelegate(IDXGIFactory4As6Backcompat* @this); + + /// + private delegate int EnumWarpAdapterDelegate(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter); + + /// + private delegate int EnumAdapterByGpuPreferenceDelegate(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter); + + /// + /// A cached instance wrapping . + /// + private static readonly ReleaseDelegate ReleaseWrapper = Release; + + /// + /// A cached instance wrapping . + /// + private static readonly EnumWarpAdapterDelegate EnumWarpAdapterWrapper = EnumWarpAdapter; + + /// + /// A cached instance wrapping . + /// + private static readonly EnumAdapterByGpuPreferenceDelegate EnumAdapterByGpuPreferenceWrapper = EnumAdapterByGpuPreference; +#endif + /// /// The shared method table pointer for all instances. /// @@ -138,20 +165,8 @@ internal static partial class DeviceHelper *dxgiFactory6 = (IDXGIFactory6*)@this; } -#if !NET6_0_OR_GREATER - /// - private delegate uint ReleaseDelegate(IDXGIFactory4As6Backcompat* @this); - - /// - /// A cached instance wrapping . - /// - private static readonly ReleaseDelegate ReleaseWrapper = Release; -#endif - /// -#if NET6_0_OR_GREATER [UnmanagedCallersOnly] -#endif public static uint Release(IDXGIFactory4As6Backcompat* @this) { @this->dxgiFactory4->Release(); @@ -161,39 +176,15 @@ internal static partial class DeviceHelper return 0; } -#if !NET6_0_OR_GREATER - /// - private delegate int EnumWarpAdapterDelegate(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter); - - /// - /// A cached instance wrapping . - /// - private static readonly EnumWarpAdapterDelegate EnumWarpAdapterWrapper = EnumWarpAdapter; -#endif - /// -#if NET6_0_OR_GREATER [UnmanagedCallersOnly] -#endif public static int EnumWarpAdapter(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter) { return @this->dxgiFactory4->EnumWarpAdapter(riid, ppvAdapter); } -#if !NET6_0_OR_GREATER - /// - private delegate int EnumAdapterByGpuPreferenceDelegate(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter); - - /// - /// A cached instance wrapping . - /// - private static readonly EnumAdapterByGpuPreferenceDelegate EnumAdapterByGpuPreferenceWrapper = EnumAdapterByGpuPreference; -#endif - /// -#if NET6_0_OR_GREATER [UnmanagedCallersOnly] -#endif public static int EnumAdapterByGpuPreference(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter) { return @this->dxgiFactory4->EnumAdapters1(Adapter, (IDXGIAdapter1**)ppvAdapter);