Added .Graphics support for .NET Standard 2.0

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-05 20:40:18 +01:00
1 親 c188aaa9f3
コミット 2b1e88fbd8
7ファイル変更
+153 -10

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -5,5 +5,6 @@
<add key="BenchmarkDotNet-CI" value="https://ci.appveyor.com/nuget/benchmarkdotnet" />
<add key="ComputeSharp-Local" value="src\ComputeSharp.Package\bin\Release" />
<add key="SixLabors-CI" value="https://www.myget.org/F/sixlabors/api/v3/index.json" />
<add key="TerraFX-Local" value="pkg" />
</packageSources>
</configuration>
+9 -3
ファイルの表示
@@ -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
{
+6 -2
ファイルの表示
@@ -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
{
+3
ファイルの表示
@@ -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
+5 -2
ファイルの表示
@@ -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
{
+7 -3
ファイルの表示
@@ -1,13 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit" Version="7.0.0-preview4" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta2-449553204" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta2" />
</ItemGroup>
<!-- .NET Standard 2.0 doesn't have Unsafe.NullRef<T>() -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
+122
ファイルの表示
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ComputeSharp
{
namespace System
{
/// <summary>
/// A polyfill class for the <see cref="GC"/> type.
/// </summary>
internal static class GC
{
/// <summary>
/// Allocates an array while skipping zero-initialization, if possible.
/// </summary>
/// <typeparam name="T">Specifies the type of the array element.</typeparam>
/// <param name="length">Specifies the length of the array.</param>
/// <param name="pinned">Specifies whether the allocated array must be pinned.</param>
/// <returns>An array object with uninitialized memory, if possible.</returns>
public static T[] AllocateUninitializedArray<T>(int length, bool pinned = false)
{
return new T[length];
}
}
namespace Collections.Generic
{
/// <summary>
/// Extensions for the <see cref="Queue{T}"/> type.
/// </summary>
internal static class QueueExtensions
{
/// <summary>
/// Removes the object at the beginning of the <see cref="Queue{T}"/> instance and copies it to the result parameter.
/// </summary>
/// <typeparam name="T">The type of values in the input <see cref="Queue{T}"/>.</typeparam>
/// <param name="queue">The input <see cref="Queue{T}"/>.</param>
/// <param name="result">The removed object.</param>
/// <returns>Whether or not the object was successfully removed.</returns>
public static bool TryDequeue<T>(this Queue<T> queue, out T? result)
{
if (queue.Count > 0)
{
result = queue.Dequeue();
return true;
}
result = default;
return false;
}
}
}
namespace Runtime.InteropServices
{
/// <summary>
/// A polyfill class for the <see cref="MemoryMarshal"/> type.
/// </summary>
internal static class MemoryMarshal
{
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The type of the array elements.</typeparam>
/// <param name="array">The array to analyze.</param>
/// <returns>Reference to the 0th element in array.</returns>
public static ref T GetArrayDataReference<T>(T[] array)
{
IntPtr offset = TypeInfo<T>.ArrayDataByteOffset;
RawObjectData rawObject = Unsafe.As<RawObjectData>(array)!;
ref byte r0 = ref rawObject.Data;
ref byte r1 = ref Unsafe.AddByteOffset(ref r0, offset);
ref T r2 = ref Unsafe.As<byte, T>(ref r1);
return ref r2;
}
/// <summary>
/// A private generic class to preload type info for arbitrary runtime types.
/// </summary>
/// <typeparam name="T">The type to load info for.</typeparam>
private static class TypeInfo<T>
{
/// <summary>
/// The byte offset to the first <typeparamref name="T"/> element in a SZ array.
/// </summary>
public static readonly IntPtr ArrayDataByteOffset = MeasureArrayDataByteOffset();
/// <summary>
/// Computes the value for <see cref="ArrayDataByteOffset"/>.
/// </summary>
/// <returns>The value of <see cref="ArrayDataByteOffset"/> for the current runtime.</returns>
private static IntPtr MeasureArrayDataByteOffset()
{
T[] array = new T[1];
RawObjectData rawObject = Unsafe.As<RawObjectData>(array)!;
ref byte r0 = ref rawObject.Data;
ref byte r1 = ref Unsafe.As<T, byte>(ref array[0]);
return Unsafe.ByteOffset(ref r0, ref r1);
}
}
/// <summary>
/// Mapping type for a raw object.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private sealed class RawObjectData
{
[FieldOffset(0)]
public byte Data;
}
}
}
}
}