diff --git a/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs b/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs index 6634e708..687f86bb 100644 --- a/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs +++ b/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; -using ComputeSharp.Graphics; namespace ComputeSharp.Benchmark.Blas { diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index e53489c2..f41b8007 100644 --- a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -4,7 +4,7 @@ using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; #if NETSTANDARD2_0 -using GC = ComputeSharp.System.GC; +using GC = Polyfills.GC; #endif namespace ComputeSharp.Graphics.Commands.Interop diff --git a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs index e7f1e242..511ce147 100644 --- a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.Contracts; using System.Text; -using ComputeSharp.Graphics; using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp.Exceptions diff --git a/src/ComputeSharp.Graphics/Properties/Polyfills.cs b/src/ComputeSharp.Graphics/Properties/Polyfills.cs index 1e457f1a..cfd91f55 100644 --- a/src/ComputeSharp.Graphics/Properties/Polyfills.cs +++ b/src/ComputeSharp.Graphics/Properties/Polyfills.cs @@ -4,90 +4,84 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace ComputeSharp +namespace Polyfills { - namespace System + /// + /// A polyfill class for the type. + /// + internal static class GC { /// - /// A polyfill class for the type. + /// Allocates an array while skipping zero-initialization, if possible. /// - internal static class GC + /// 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]; + } + } + + /// + /// 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 { /// - /// Allocates an array while skipping zero-initialization, if possible. + /// The byte offset to the first element in a SZ array. /// - /// 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) + public static readonly IntPtr ArrayDataByteOffset = MeasureArrayDataByteOffset(); + + /// + /// Computes the value for . + /// + /// The value of for the current runtime. + private static IntPtr MeasureArrayDataByteOffset() { - return new T[length]; + 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); } } - namespace Runtime.InteropServices + /// + /// Mapping type for a raw object. + /// + [StructLayout(LayoutKind.Explicit)] + private sealed class RawObjectData { - /// - /// 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; - } - } + [FieldOffset(0)] + public byte Data; } } } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs index 76fd6afd..d8434aa6 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs @@ -10,7 +10,7 @@ using FX = TerraFX.Interop.Windows; #if NET5_0 using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal; #else -using MemoryMarshal = ComputeSharp.System.Runtime.InteropServices.MemoryMarshal; +using MemoryMarshal = Polyfills.MemoryMarshal; #endif namespace ComputeSharp.Resources diff --git a/src/ComputeSharp.Graphics/Resources/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/BufferDebugView{T}.cs index fb4da827..bd78980e 100644 --- a/src/ComputeSharp.Graphics/Resources/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Views/BufferDebugView{T}.cs @@ -2,7 +2,7 @@ #if NET5_0 using GC = System.GC; #else -using GC = ComputeSharp.System.GC; +using GC = Polyfills.GC; #endif namespace ComputeSharp.Resources.Views diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 3ceb999b..04683a7f 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -4,7 +4,6 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Renderer; diff --git a/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs b/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs index 9c65823a..da4de180 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs @@ -1,5 +1,4 @@ using System.Runtime.CompilerServices; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Shaders.Translation.Interop; diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs index 76fe2554..db6b8bef 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs @@ -4,13 +4,12 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Reflection; using System.Runtime.CompilerServices; -using ComputeSharp.Graphics; using ComputeSharp.Shaders.Translation.Models; using TerraFX.Interop; #if NET5_0 using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal; #else -using MemoryMarshal = ComputeSharp.System.Runtime.InteropServices.MemoryMarshal; +using MemoryMarshal = Polyfills.MemoryMarshal; #endif namespace ComputeSharp.Shaders.Translation diff --git a/src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs b/src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs index 2732e4d8..a838cc5b 100644 --- a/src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs +++ b/src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs @@ -6,7 +6,7 @@ using Microsoft.Toolkit.Diagnostics; #if NET5_0 using GC = System.GC; #else -using GC = ComputeSharp.System.GC; +using GC = Polyfills.GC; #endif namespace ComputeSharp diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index c138b30b..affd1d93 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Helpers; namespace ComputeSharp diff --git a/tests/ComputeSharp.Tests/InitializationTests.cs b/tests/ComputeSharp.Tests/InitializationTests.cs index 586b5b4e..661896e0 100644 --- a/tests/ComputeSharp.Tests/InitializationTests.cs +++ b/tests/ComputeSharp.Tests/InitializationTests.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using ComputeSharp.Graphics; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests