From 2b1e88fbd84a80c6a6f3795615ab7e2c305d88ed Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 29 Dec 2020 22:35:48 +0100 Subject: [PATCH] Added .Graphics support for .NET Standard 2.0 --- nuget.config | 1 + .../Buffers/Abstract/Buffer{T}.cs | 12 +- .../Buffers/Views/BufferDebugView{T}.cs | 8 +- .../Interop/ID3D12CommandAllocatorPool.cs | 3 + .../ID3D12DescriptorHandleAllocator.cs | 7 +- .../ComputeSharp.Graphics.csproj | 10 +- .../Properties/Polyfills.cs | 122 ++++++++++++++++++ 7 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Properties/Polyfills.cs diff --git a/nuget.config b/nuget.config index e71007c9..4f62278c 100644 --- a/nuget.config +++ b/nuget.config @@ -5,5 +5,6 @@ + diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 106501bc..b4bb2c61 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -1,6 +1,5 @@ using System.Buffers; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Extensions; @@ -8,6 +7,11 @@ using ComputeSharp.Graphics.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; +#if NET5_0 +using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal; +#else +using MemoryMarshal = ComputeSharp.System.Runtime.InteropServices.MemoryMarshal; +#endif namespace ComputeSharp.Graphics.Buffers.Abstract { @@ -138,9 +142,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract try { - source.GetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0); + ref T r0 = ref MemoryMarshal.GetArrayDataReference(array); - SetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0); + source.GetData(ref r0, source.Length, 0); + + SetData(ref r0, source.Length, 0); } finally { diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs index 59950292..9325d70e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -1,6 +1,10 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using ComputeSharp.Graphics.Buffers.Abstract; +#if NET5_0 +using GC = System.GC; +#else +using GC = ComputeSharp.System.GC; +#endif namespace ComputeSharp.Graphics.Buffers.Views { diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs index 85461480..fe88514c 100644 --- a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; +#if NETSTANDARD2_0 +using ComputeSharp.System.Collections.Generic; +#endif using TerraFX.Interop; namespace ComputeSharp.Graphics.Commands.Interop diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index fc8e03f4..e53489c2 100644 --- a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -1,8 +1,11 @@ -using ComputeSharp.Graphics.Extensions; +using System; +using ComputeSharp.Graphics.Extensions; using Microsoft.Toolkit.Diagnostics; -using System; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; +#if NETSTANDARD2_0 +using GC = ComputeSharp.System.GC; +#endif namespace ComputeSharp.Graphics.Commands.Interop { diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 8c720d45..3d2eefe6 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -1,13 +1,17 @@  - net5.0 - true + netstandard2.0;net5.0 - + + + + + + diff --git a/src/ComputeSharp.Graphics/Properties/Polyfills.cs b/src/ComputeSharp.Graphics/Properties/Polyfills.cs new file mode 100644 index 00000000..d9c2c634 --- /dev/null +++ b/src/ComputeSharp.Graphics/Properties/Polyfills.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace ComputeSharp +{ + namespace System + { + /// + /// A polyfill class for the type. + /// + internal static class GC + { + /// + /// Allocates an array while skipping zero-initialization, if possible. + /// + /// Specifies the type of the array element. + /// Specifies the length of the array. + /// Specifies whether the allocated array must be pinned. + /// An array object with uninitialized memory, if possible. + public static T[] AllocateUninitializedArray(int length, bool pinned = false) + { + return new T[length]; + } + } + + namespace Collections.Generic + { + /// + /// Extensions for the type. + /// + internal static class QueueExtensions + { + /// + /// Removes the object at the beginning of the instance and copies it to the result parameter. + /// + /// The type of values in the input . + /// The input . + /// The removed object. + /// Whether or not the object was successfully removed. + public static bool TryDequeue(this Queue queue, out T? result) + { + if (queue.Count > 0) + { + result = queue.Dequeue(); + + return true; + } + + result = default; + + return false; + } + } + } + + namespace Runtime.InteropServices + { + /// + /// A polyfill class for the type. + /// + internal static class MemoryMarshal + { + /// + /// Returns a reference to the 0th element of array. If the array is empty, returns + /// a reference to where the 0th element would have been stored. Such a reference + /// may be used for pinning but must never be dereferenced. + /// + /// The type of the array elements. + /// The array to analyze. + /// Reference to the 0th element in array. + public static ref T GetArrayDataReference(T[] array) + { + IntPtr offset = TypeInfo.ArrayDataByteOffset; + RawObjectData rawObject = Unsafe.As(array)!; + ref byte r0 = ref rawObject.Data; + ref byte r1 = ref Unsafe.AddByteOffset(ref r0, offset); + ref T r2 = ref Unsafe.As(ref r1); + + return ref r2; + } + + /// + /// A private generic class to preload type info for arbitrary runtime types. + /// + /// The type to load info for. + private static class TypeInfo + { + /// + /// The byte offset to the first element in a SZ array. + /// + public static readonly IntPtr ArrayDataByteOffset = MeasureArrayDataByteOffset(); + + /// + /// Computes the value for . + /// + /// The value of for the current runtime. + private static IntPtr MeasureArrayDataByteOffset() + { + T[] array = new T[1]; + RawObjectData rawObject = Unsafe.As(array)!; + ref byte r0 = ref rawObject.Data; + ref byte r1 = ref Unsafe.As(ref array[0]); + + return Unsafe.ByteOffset(ref r0, ref r1); + } + } + + /// + /// Mapping type for a raw object. + /// + [StructLayout(LayoutKind.Explicit)] + private sealed class RawObjectData + { + [FieldOffset(0)] + public byte Data; + } + } + } + } +}