From 24e9dedfc55d8be40b25772cf9218d498e62372b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 1 Aug 2022 23:22:35 +0200 Subject: [PATCH 01/11] Add DynamicallyAccessedMembersAttribute polyfills --- .../ComputeSharp.NetStandard.projitems | 2 + .../DynamicallyAccessedMemberTypes.cs | 91 +++++++++++++++++++ .../DynamicallyAccessedMembersAttribute.cs | 30 ++++++ 3 files changed, 123 insertions(+) create mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs create mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index f9ab186d..5e1d84a5 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -11,6 +11,8 @@ + + diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs new file mode 100644 index 00000000..f17b7e21 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs @@ -0,0 +1,91 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Specifies the types of members that are dynamically accessed. +/// +[Flags] +internal enum DynamicallyAccessedMemberTypes +{ + /// + /// Specifies no members. + /// + None = 0, + + /// + /// Specifies the default, parameterless public constructor. + /// + PublicParameterlessConstructor = 0x0001, + + /// + /// Specifies all public constructors. + /// + PublicConstructors = 0x0002 | PublicParameterlessConstructor, + + /// + /// Specifies all non-public constructors. + /// + NonPublicConstructors = 0x0004, + + /// + /// Specifies all public methods. + /// + PublicMethods = 0x0008, + + /// + /// Specifies all non-public methods. + /// + NonPublicMethods = 0x0010, + + /// + /// Specifies all public fields. + /// + PublicFields = 0x0020, + + /// + /// Specifies all non-public fields. + /// + NonPublicFields = 0x0040, + + /// + /// Specifies all public nested types. + /// + PublicNestedTypes = 0x0080, + + /// + /// Specifies all non-public nested types. + /// + NonPublicNestedTypes = 0x0100, + + /// + /// Specifies all public properties. + /// + PublicProperties = 0x0200, + + /// + /// Specifies all non-public properties. + /// + NonPublicProperties = 0x0400, + + /// + /// Specifies all public events. + /// + PublicEvents = 0x0800, + + /// + /// Specifies all non-public events. + /// + NonPublicEvents = 0x1000, + + /// + /// Specifies all interfaces implemented by the type. + /// + Interfaces = 0x2000, + + /// + /// Specifies all members. + /// + All = ~None +} \ No newline at end of file diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs new file mode 100644 index 00000000..8e71ef84 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Indicates that certain members on a specified are accessed dynamically, +/// for example through . +/// +[AttributeUsage( + AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | + AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, + Inherited = false)] +internal sealed class DynamicallyAccessedMembersAttribute : Attribute +{ + /// + /// Initializes a new instance of the class with the specified member types. + /// + /// The types of members dynamically accessed. + public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) + { + MemberTypes = memberTypes; + } + + /// + /// Gets the which specifies the type of members dynamically accessed. + /// + public DynamicallyAccessedMemberTypes MemberTypes { get; } +} \ No newline at end of file From a5f5e76dfa1360485d52ab8746078bc8b32f244c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 1 Aug 2022 23:24:24 +0200 Subject: [PATCH 02/11] Add trimming annotations to TypeInfo --- src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs index 1b0896c6..2b4948f6 100644 --- a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs +++ b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace ComputeSharp.Graphics.Helpers; @@ -7,7 +8,7 @@ namespace ComputeSharp.Graphics.Helpers; /// A class providing info on specific types. /// /// The input type to extract info for. -internal static class TypeInfo +internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> where T : unmanaged { /// @@ -20,7 +21,7 @@ internal static class TypeInfo /// /// The current type to check. /// Whether or not is or contains a field. - private static bool ChecksIsDoubleOrContainsDoubles(Type type) + private static bool ChecksIsDoubleOrContainsDoubles([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] Type type) { if (type == typeof(double)) return true; From 9f68fc31da7b55f1052e3ed41b65999a633a6529 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 1 Aug 2022 23:48:38 +0200 Subject: [PATCH 03/11] Fix NAOT trimming annotations for ComputeSharp --- .../GraphicsDeviceMismatchException.cs | 3 +- .../Graphics/Extensions/BufferExtensions.cs | 29 ++++++++++--------- .../GraphicsDeviceExtensions.Resources.cs | 25 ++++++++-------- .../Extensions/ReadBackBufferExtensions.cs | 7 +++-- .../Extensions/StructuredBufferExtensions.cs | 11 +++---- .../Extensions/UploadBufferExtensions.cs | 7 +++-- .../Graphics/Helpers/TypeInfo{T}.cs | 2 ++ .../__Internals/GraphicsResourceHelper.cs | 3 +- .../Graphics/Interop/InteropServices.cs | 5 ++-- .../Graphics/Resources/Abstract/Buffer{T}.cs | 6 ++-- .../Resources/Abstract/StructuredBuffer{T}.cs | 5 ++-- .../Graphics/Resources/ConstantBuffer{T}.cs | 3 +- .../Resources/Debug/BufferDebugView{T}.cs | 3 +- .../Graphics/Resources/ReadOnlyBuffer{T}.cs | 3 +- .../Graphics/Resources/ReadWriteBuffer{T}.cs | 3 +- .../Extensions/ComputeContextExtensions.cs | 7 +++-- 16 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 6283a35b..36ede967 100644 --- a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Text; using ComputeSharp.Interop; using ComputeSharp.Resources; @@ -49,7 +50,7 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - internal static void Throw(Buffer buffer, GraphicsDevice device) + internal static void Throw<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, GraphicsDevice device) where T : unmanaged { throw Create(buffer, buffer.GraphicsDevice, device); diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index 6045ba1e..35a0b9cb 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; @@ -19,7 +20,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// A array with the contents of the input buffer. - public static T[] ToArray(this Buffer source) + public static T[] ToArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source) where T : unmanaged { Guard.IsNotNull(source); @@ -35,7 +36,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The number of items to read. /// A array with the contents of the specified range from the current buffer. - public static T[] ToArray(this Buffer source, int offset, int count) + public static T[] ToArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, int offset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -54,7 +55,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The input array to write data to. - public static void CopyTo(this Buffer source, T[] destination) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, T[] destination) where T : unmanaged { Guard.IsNotNull(source); @@ -72,7 +73,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -89,7 +90,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The input to write data to. - public static void CopyTo(this Buffer source, Span destination) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Span destination) where T : unmanaged { Guard.IsNotNull(source); @@ -104,7 +105,7 @@ public static class BufferExtensions /// The input instance to read data from. /// The input to write data to. /// The offset to start reading data from. - public static void CopyTo(this Buffer source, Span destination, int sourceOffset) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Span destination, int sourceOffset) where T : unmanaged { Guard.IsNotNull(source); @@ -118,7 +119,7 @@ public static class BufferExtensions /// The type of items stored on the buffers. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this Buffer source, Buffer destination) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Buffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -136,7 +137,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -151,7 +152,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input array to read data from. - public static void CopyFrom(this Buffer destination, T[] source) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, T[] source) where T : unmanaged { Guard.IsNotNull(destination); @@ -169,7 +170,7 @@ public static class BufferExtensions /// The starting offset within to read data from. /// The offset to start writing data to. /// The number of items to write. - public static void CopyFrom(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); @@ -186,7 +187,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input to read data from. - public static void CopyFrom(this Buffer destination, ReadOnlySpan source) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(destination); @@ -201,7 +202,7 @@ public static class BufferExtensions /// The target instance to write data to. /// The input to read data from. /// The offset to start writing data to. - public static void CopyFrom(this Buffer destination, ReadOnlySpan source, int destinationOffset) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, ReadOnlySpan source, int destinationOffset) where T : unmanaged { Guard.IsNotNull(destination); @@ -215,7 +216,7 @@ public static class BufferExtensions /// The type of items stored on the buffers. /// The input instance to write data data to. /// The source instance to read data from. - public static void CopyFrom(this Buffer destination, Buffer source) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, Buffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -233,7 +234,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index a434df13..e545adbf 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; @@ -17,7 +18,7 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -32,7 +33,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A constant instance with the contents of the input array. - public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] source) + public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -48,7 +49,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A constant instance with the contents of the input . - public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, ReadOnlySpan source) + public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -67,7 +68,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A constant instance with the contents of the input . - public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Buffer source) + public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); @@ -88,7 +89,7 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -103,7 +104,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A read write instance with the contents of the input array. - public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[] source) + public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -119,7 +120,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, ReadOnlySpan source) + public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -138,7 +139,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Buffer source) + public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); @@ -575,7 +576,7 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -590,7 +591,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A read write instance with the contents of the input array. - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] source) + public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -606,7 +607,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan source) + public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -625,7 +626,7 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer source) + public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs index 0940cf5d..ce322cbf 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -14,7 +15,7 @@ public static class ReadBackBufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input instance to read data from. - public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this ReadBackBuffer destination, StructuredBuffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -32,7 +33,7 @@ public static class ReadBackBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index 1166042e..6f86a471 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -14,7 +15,7 @@ public static class StructuredBufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer source, ReadBackBuffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -32,7 +33,7 @@ public static class StructuredBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -47,7 +48,7 @@ public static class StructuredBufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input instance to read data from. - public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer destination, UploadBuffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -65,7 +66,7 @@ public static class StructuredBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs index 118eeb9e..4a3aa8d4 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -14,7 +15,7 @@ public static class UploadBufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this UploadBuffer source, StructuredBuffer destination) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this UploadBuffer source, StructuredBuffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -32,7 +33,7 @@ public static class UploadBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); diff --git a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs index 2b4948f6..cc860a7e 100644 --- a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs +++ b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs @@ -29,7 +29,9 @@ internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMe foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { +#pragma warning disable IL2072 // 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' if (ChecksIsDoubleOrContainsDoubles(fieldInfo.FieldType)) +#pragma warning restore IL2072 { return true; } diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index fd4dea7c..23997e72 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Interop; @@ -62,7 +63,7 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the buffer. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, GraphicsDevice device) where T : unmanaged { Guard.IsNotNull(buffer); diff --git a/src/ComputeSharp/Graphics/Interop/InteropServices.cs b/src/ComputeSharp/Graphics/Interop/InteropServices.cs index 5427cb64..f6e06929 100644 --- a/src/ComputeSharp/Graphics/Interop/InteropServices.cs +++ b/src/ComputeSharp/Graphics/Interop/InteropServices.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using ComputeSharp.Core.Extensions; using ComputeSharp.Resources; @@ -49,7 +50,7 @@ public static unsafe class InteropServices /// The address of a pointer to an interface with the IID specified in . /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. - public static void GetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) + public static void GetID3D12Resource<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { using var _0 = buffer.GetReferenceTrackingLease(); @@ -179,7 +180,7 @@ public static unsafe class InteropServices /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// /// The instance has been disposed. - public static int TryGetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) + public static int TryGetID3D12Resource<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { using var _0 = buffer.GetReferenceTrackingLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 2d97587d..1fb3a508 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -1,5 +1,5 @@ -using System; -using System.Buffers; +using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Helpers; @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public unsafe abstract class Buffer : NativeObject, IGraphicsResource +public unsafe abstract class Buffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : NativeObject, IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 81a977de..88f47a2e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -14,7 +15,7 @@ namespace ComputeSharp.Resources; /// A representing a typed structured buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public abstract class StructuredBuffer : Buffer +public abstract class StructuredBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : Buffer where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index f7002cdc..e131d2a9 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Helpers; @@ -19,7 +20,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ConstantBuffer : Buffer +public sealed class ConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : Buffer where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs index 6ee0d853..a290ea03 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; #if NET6_0_OR_GREATER using GC = System.GC; #else @@ -11,7 +12,7 @@ namespace ComputeSharp.Resources.Debug; /// A debug proxy used to display items in a instance. /// /// The type of items to display. -internal sealed class BufferDebugView +internal sealed class BufferDebugView<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs index 46613dae..98d7108e 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; @@ -12,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadOnlyBuffer : StructuredBuffer +public sealed class ReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : StructuredBuffer where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs index 469669ff..315eae39 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; @@ -12,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteBuffer : StructuredBuffer +public sealed class ReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : StructuredBuffer where T : unmanaged { /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 6b6bc55f..f150cd76 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; @@ -20,7 +21,7 @@ public static class ComputeContextExtensions /// The type of items stored on the buffer. /// The to use to insert the resource barrier. /// The input instance to insert the barrier for. - public static unsafe void Barrier(this in ComputeContext context, ReadWriteBuffer buffer) + public static unsafe void Barrier<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { Guard.IsNotNull(buffer); @@ -136,7 +137,7 @@ public static class ComputeContextExtensions /// The type of items stored on the buffer. /// The to use to clear the resource. /// The input instance to clear. - public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) + public static unsafe void Clear<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { Guard.IsNotNull(buffer); From 2254053960aabe37816963d91c25f7a2d09f7e02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 00:31:07 +0200 Subject: [PATCH 04/11] Remove reflection from SwapChain sample --- samples/ComputeSharp.SwapChain/Program.cs | 34 +++++++++++------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index f34c3b3c..bdf8f3d4 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Reflection; using System.Runtime.CompilerServices; using ComputeSharp.SwapChain.Backend; using ComputeSharp.SwapChain.Shaders; @@ -17,20 +16,19 @@ class Program /// /// The mapping of available samples to choose from. /// - private static readonly Win32Application[] Samples = new Win32Application[] + private static readonly (Type ShaderType, Win32Application Application)[] Samples = new (Type, Win32Application)[] { - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds)), - new SwapChainApplication(static time => new((float)time.TotalSeconds, RustyMetal)) + (typeof(HelloWorld), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(FourColorGradient), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(ColorfulInfinity), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(FractalTiling), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(TwoTiledTruchet), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(MengerJourney), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(Octagrams), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(ProteanClouds), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(ExtrudedTruchetPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(PyramidPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (typeof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))) }; static void Main() @@ -45,7 +43,7 @@ class Program for (int i = 0; i < Samples.Length; i++) { - Console.WriteLine($"{i}: {Samples[i].GetType().GenericTypeArguments[0].Name}"); + Console.WriteLine($"{i}: {Samples[i].ShaderType.Name}"); } Console.WriteLine($"{Samples.Length}+: Exit (Use Escape, 'Q', or Alt + F4 to exit a sample once chosen)"); @@ -60,9 +58,9 @@ class Program if (index >= 0 && index < Samples.Length) { Console.WriteLine(); - Console.WriteLine($"Starting {Samples[index].GetType().GenericTypeArguments[0].Name}..."); + Console.WriteLine($"Starting {Samples[index].ShaderType.Name}..."); - Win32ApplicationRunner.Run(Samples[index]); + Win32ApplicationRunner.Run(Samples[index].Application); } } while (index >= 0 && index < Samples.Length); @@ -75,7 +73,7 @@ class Program /// A texture with the data from the image at the specified name. private static ReadOnlyTexture2D LoadTexture([CallerMemberName] string name = null!) { - string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Textures", $"{name}.png"); + string filename = Path.Combine(AppContext.BaseDirectory, "Textures", $"{name}.png"); return GraphicsDevice.GetDefault().LoadReadOnlyTexture2D(filename); } From 5bedaa33a32b861bcf4e99ac5292d482aad1fc72 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 00:34:52 +0200 Subject: [PATCH 05/11] Remove reflection from SwapChain sample setup --- samples/ComputeSharp.SwapChain/Program.cs | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index bdf8f3d4..3e54f1ef 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -16,19 +16,19 @@ class Program /// /// The mapping of available samples to choose from. /// - private static readonly (Type ShaderType, Win32Application Application)[] Samples = new (Type, Win32Application)[] + private static readonly (string ShaderName, Win32Application Application)[] Samples = new (string, Win32Application)[] { - (typeof(HelloWorld), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(FourColorGradient), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(ColorfulInfinity), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(FractalTiling), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(TwoTiledTruchet), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(MengerJourney), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(Octagrams), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(ProteanClouds), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(ExtrudedTruchetPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(PyramidPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (typeof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))) + (nameof(HelloWorld), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(FourColorGradient), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(ColorfulInfinity), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(FractalTiling), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(TwoTiledTruchet), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(MengerJourney), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(Octagrams), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(ProteanClouds), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(ExtrudedTruchetPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(PyramidPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))) }; static void Main() @@ -43,7 +43,7 @@ class Program for (int i = 0; i < Samples.Length; i++) { - Console.WriteLine($"{i}: {Samples[i].ShaderType.Name}"); + Console.WriteLine($"{i}: {Samples[i].ShaderName}"); } Console.WriteLine($"{Samples.Length}+: Exit (Use Escape, 'Q', or Alt + F4 to exit a sample once chosen)"); @@ -58,7 +58,7 @@ class Program if (index >= 0 && index < Samples.Length) { Console.WriteLine(); - Console.WriteLine($"Starting {Samples[index].ShaderType.Name}..."); + Console.WriteLine($"Starting {Samples[index].ShaderName}..."); Win32ApplicationRunner.Run(Samples[index].Application); } From 3d0217dc7dbbacc5ced8b183c51ef09b61ee4f4d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 00:57:52 +0200 Subject: [PATCH 06/11] Add missing ContouredLayers sample --- samples/ComputeSharp.SwapChain/Program.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index 3e54f1ef..29381a57 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -11,7 +11,7 @@ class Program /// /// A texture for \Textures\RustyMetal.png. /// - private static readonly IReadOnlyNormalizedTexture2D RustyMetal = LoadTexture(); + private static readonly Lazy> RustyMetal = new(static () => LoadTexture()); /// /// The mapping of available samples to choose from. @@ -28,7 +28,8 @@ class Program (nameof(ProteanClouds), new SwapChainApplication(static time => new((float)time.TotalSeconds))), (nameof(ExtrudedTruchetPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), (nameof(PyramidPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))) + (nameof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))), + (nameof(ContouredLayers), new SwapChainApplication(static time => new((float)time.TotalSeconds, RustyMetal.Value))) }; static void Main() From 6415972f51765f3b3eff05b4c8ff6c886d6703cd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 20:34:07 +0200 Subject: [PATCH 07/11] Minor fixes to polyfills --- .../MemberNotNullAttribute.cs | 5 ++- .../CompilerServices/IsExternalInit.cs | 5 ++- .../UnmanagedCallersOnlyAttribute.cs | 9 ++++-- .../Runtime/Versioning/OSPlatformAttribute.cs | 5 ++- .../Versioning/SupportedOSVersionAttribute.cs | 32 +++++++++++-------- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs index 5659d922..7c3bade3 100644 --- a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs @@ -1,4 +1,7 @@ -namespace System.Diagnostics.CodeAnalysis; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis; /// /// Specifies that the method or property will ensure that the listed field and property members have not-null values. diff --git a/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs b/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs index a914bf06..775f605a 100644 --- a/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs +++ b/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs @@ -1,4 +1,7 @@ -using System.ComponentModel; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; namespace System.Runtime.CompilerServices; diff --git a/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs index c9cbc8f0..b35b322b 100644 --- a/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs +++ b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/UnmanagedCallersOnlyAttribute.cs @@ -1,6 +1,9 @@ -using System; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. -namespace ComputeSharp.NetStandard.System.Runtime.InteropServices; +using System; + +namespace ComputeSharp.NetStandard.System.Runtime.InteropServices; // This cannot be in System.Runtime.InteropServices, or Roslyn will recognize it /// /// Any method marked with can be directly called from native code. @@ -9,6 +12,6 @@ namespace ComputeSharp.NetStandard.System.Runtime.InteropServices; /// 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 +internal sealed class UnmanagedCallersOnlyAttribute : Attribute { } \ No newline at end of file diff --git a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/OSPlatformAttribute.cs b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/OSPlatformAttribute.cs index 2cf0bc16..ad3bf2ff 100644 --- a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/OSPlatformAttribute.cs +++ b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/OSPlatformAttribute.cs @@ -1,4 +1,7 @@ -namespace System.Runtime.Versioning; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning; /// /// Base type for all platform-specific API attributes. diff --git a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs index 9e829f55..3cc8bf1d 100644 --- a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs +++ b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs @@ -1,4 +1,7 @@ -namespace System.Runtime.Versioning; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning; /// /// Records the operating system (and minimum version) that supports an API. Multiple @@ -9,19 +12,20 @@ /// or use guards to prevent calls to APIs on unsupported operating systems. /// A given platform should only be specified once. /// -[AttributeUsage(AttributeTargets.Assembly | - AttributeTargets.Class | - AttributeTargets.Constructor | - AttributeTargets.Enum | - AttributeTargets.Event | - AttributeTargets.Field | - AttributeTargets.Interface | - AttributeTargets.Method | - AttributeTargets.Module | - AttributeTargets.Property | - AttributeTargets.Struct, - AllowMultiple = true, - Inherited = false)] +[AttributeUsage( + AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Enum | + AttributeTargets.Event | + AttributeTargets.Field | + AttributeTargets.Interface | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, + Inherited = false)] internal sealed class SupportedOSPlatformAttribute : OSPlatformAttribute { public SupportedOSPlatformAttribute(string platformName) From e78b90c9418c5efa75ef8d4bc03361218474ab54 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 20:34:15 +0200 Subject: [PATCH 08/11] Add UnconditionalSuppressMessageAttribute polyfill --- .../ComputeSharp.NetStandard.projitems | 1 + .../UnconditionalSuppressMessageAttribute.cs | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 5e1d84a5..fc6100e5 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -14,6 +14,7 @@ + diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs new file mode 100644 index 00000000..360dfaf3 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs @@ -0,0 +1,84 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a +/// single code artifact. +/// +/// +/// is different than +/// in that it doesn't have a +/// . So it is always preserved in the compiled assembly. +/// +[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] +internal sealed class UnconditionalSuppressMessageAttribute : Attribute +{ + /// + /// Initializes a new instance of the + /// class, specifying the category of the tool and the identifier for an analysis rule. + /// + /// The category for the attribute. + /// The identifier of the analysis rule the attribute applies to. + public UnconditionalSuppressMessageAttribute(string category, string checkId) + { + Category = category; + CheckId = checkId; + } + + /// + /// Gets the category identifying the classification of the attribute. + /// + /// + /// The property describes the tool or tool analysis category + /// for which a message suppression attribute applies. + /// + public string Category { get; } + + /// + /// Gets the identifier of the analysis tool rule to be suppressed. + /// + /// + /// Concatenated together, the and + /// properties form a unique check identifier. + /// + public string CheckId { get; } + + /// + /// Gets or sets the scope of the code that is relevant for the attribute. + /// + /// + /// The Scope property is an optional argument that specifies the metadata scope for which + /// the attribute is relevant. + /// + public string? Scope { get; set; } + + /// + /// Gets or sets a fully qualified path that represents the target of the attribute. + /// + /// + /// The property is an optional argument identifying the analysis target + /// of the attribute. An example value is "System.IO.Stream.ctor():System.Void". + /// Because it is fully qualified, it can be long, particularly for targets such as parameters. + /// The analysis tool user interface should be capable of automatically formatting the parameter. + /// + public string? Target { get; set; } + + /// + /// Gets or sets an optional argument expanding on exclusion criteria. + /// + /// + /// The property is an optional argument that specifies additional + /// exclusion where the literal metadata target is not sufficiently precise. For example, + /// the cannot be applied within a method, + /// and it may be desirable to suppress a violation against a statement in the method that will + /// give a rule violation, but not against all statements in the method. + /// + public string? MessageId { get; set; } + + /// + /// Gets or sets the justification for suppressing the code analysis message. + /// + public string? Justification { get; set; } +} \ No newline at end of file From be62eed772e39768e499a59bdf1e7e0d5c3ffe9e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Aug 2022 20:36:23 +0200 Subject: [PATCH 09/11] Replace pragma with [UnconditionalSuppressMessage] in TypeInfo --- src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs index cc860a7e..6e4939bf 100644 --- a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs +++ b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs @@ -21,6 +21,10 @@ internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMe /// /// The current type to check. /// Whether or not is or contains a field. + [UnconditionalSuppressMessage( + "ReflectionAnalysis", + "IL2026:RequiresUnreferencedCode", + Justification = "The types of all fields (recursively) are already guaranteed to be preserved given it's all value types (plus T is already annotated).")] private static bool ChecksIsDoubleOrContainsDoubles([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] Type type) { if (type == typeof(double)) return true; @@ -29,9 +33,7 @@ internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMe foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { -#pragma warning disable IL2072 // 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' if (ChecksIsDoubleOrContainsDoubles(fieldInfo.FieldType)) -#pragma warning restore IL2072 { return true; } From 3deaeb0328b757cdd71b5a5af43850aac0b55a76 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 5 Aug 2022 14:10:09 +0200 Subject: [PATCH 10/11] Fix trimming annotations, use [RequiresUnreferencedCode] --- .../ComputeSharp.NetStandard.projitems | 3 +- .../DynamicallyAccessedMemberTypes.cs | 91 ------------------- .../DynamicallyAccessedMembersAttribute.cs | 30 ------ .../RequiresUnreferencedCodeAttribute.cs | 40 ++++++++ .../UnconditionalSuppressMessageAttribute.cs | 3 +- .../GraphicsDeviceMismatchException.cs | 3 +- .../Graphics/Extensions/BufferExtensions.cs | 29 +++--- .../GraphicsDeviceExtensions.Resources.cs | 36 +++++--- .../Extensions/ReadBackBufferExtensions.cs | 7 +- .../Extensions/StructuredBufferExtensions.cs | 11 +-- .../Extensions/UploadBufferExtensions.cs | 7 +- .../Graphics/Helpers/TypeInfo{T}.cs | 10 +- .../__Internals/GraphicsResourceHelper.cs | 3 +- .../Graphics/Interop/InteropServices.cs | 5 +- .../Graphics/Resources/Abstract/Buffer{T}.cs | 3 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 3 +- .../Graphics/Resources/ConstantBuffer{T}.cs | 3 +- .../Resources/Debug/BufferDebugView{T}.cs | 3 +- .../Graphics/Resources/ReadOnlyBuffer{T}.cs | 3 +- .../Graphics/Resources/ReadWriteBuffer{T}.cs | 3 +- .../Extensions/ComputeContextExtensions.cs | 4 +- 21 files changed, 112 insertions(+), 188 deletions(-) delete mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs delete mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs create mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index fc6100e5..65761606 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -11,9 +11,8 @@ - - + diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs deleted file mode 100644 index f17b7e21..00000000 --- a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs +++ /dev/null @@ -1,91 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Diagnostics.CodeAnalysis; - -/// -/// Specifies the types of members that are dynamically accessed. -/// -[Flags] -internal enum DynamicallyAccessedMemberTypes -{ - /// - /// Specifies no members. - /// - None = 0, - - /// - /// Specifies the default, parameterless public constructor. - /// - PublicParameterlessConstructor = 0x0001, - - /// - /// Specifies all public constructors. - /// - PublicConstructors = 0x0002 | PublicParameterlessConstructor, - - /// - /// Specifies all non-public constructors. - /// - NonPublicConstructors = 0x0004, - - /// - /// Specifies all public methods. - /// - PublicMethods = 0x0008, - - /// - /// Specifies all non-public methods. - /// - NonPublicMethods = 0x0010, - - /// - /// Specifies all public fields. - /// - PublicFields = 0x0020, - - /// - /// Specifies all non-public fields. - /// - NonPublicFields = 0x0040, - - /// - /// Specifies all public nested types. - /// - PublicNestedTypes = 0x0080, - - /// - /// Specifies all non-public nested types. - /// - NonPublicNestedTypes = 0x0100, - - /// - /// Specifies all public properties. - /// - PublicProperties = 0x0200, - - /// - /// Specifies all non-public properties. - /// - NonPublicProperties = 0x0400, - - /// - /// Specifies all public events. - /// - PublicEvents = 0x0800, - - /// - /// Specifies all non-public events. - /// - NonPublicEvents = 0x1000, - - /// - /// Specifies all interfaces implemented by the type. - /// - Interfaces = 0x2000, - - /// - /// Specifies all members. - /// - All = ~None -} \ No newline at end of file diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs deleted file mode 100644 index 8e71ef84..00000000 --- a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/DynamicallyAccessedMembersAttribute.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Diagnostics.CodeAnalysis; - -/// -/// Indicates that certain members on a specified are accessed dynamically, -/// for example through . -/// -[AttributeUsage( - AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | - AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method | - AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, - Inherited = false)] -internal sealed class DynamicallyAccessedMembersAttribute : Attribute -{ - /// - /// Initializes a new instance of the class with the specified member types. - /// - /// The types of members dynamically accessed. - public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) - { - MemberTypes = memberTypes; - } - - /// - /// Gets the which specifies the type of members dynamically accessed. - /// - public DynamicallyAccessedMemberTypes MemberTypes { get; } -} \ No newline at end of file diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs new file mode 100644 index 00000000..7009fdd3 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified method requires dynamic access to code that is not referenced + /// statically, for example through . + /// + /// + /// This allows tools to understand which methods are unsafe to call when removing unreferenced + /// code from an application. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class, Inherited = false)] + internal sealed class RequiresUnreferencedCodeAttribute : Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified message. + /// + /// + /// A message that contains information about the usage of unreferenced code. + /// + public RequiresUnreferencedCodeAttribute(string message) + { + Message = message; + } + + /// + /// Gets a message that contains information about the usage of unreferenced code. + /// + public string Message { get; } + + /// + /// Gets or sets an optional URL that contains more information about the method, + /// why it requires unreferenced code, and what options a consumer has to deal with it. + /// + public string? Url { get; set; } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs index 360dfaf3..d5419bb3 100644 --- a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs @@ -4,8 +4,7 @@ namespace System.Diagnostics.CodeAnalysis; /// -/// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a -/// single code artifact. +/// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a single code artifact. /// /// /// is different than diff --git a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 36ede967..6283a35b 100644 --- a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.Text; using ComputeSharp.Interop; using ComputeSharp.Resources; @@ -50,7 +49,7 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - internal static void Throw<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, GraphicsDevice device) + internal static void Throw(Buffer buffer, GraphicsDevice device) where T : unmanaged { throw Create(buffer, buffer.GraphicsDevice, device); diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index 35a0b9cb..6045ba1e 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; @@ -20,7 +19,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// A array with the contents of the input buffer. - public static T[] ToArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source) + public static T[] ToArray(this Buffer source) where T : unmanaged { Guard.IsNotNull(source); @@ -36,7 +35,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The number of items to read. /// A array with the contents of the specified range from the current buffer. - public static T[] ToArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, int offset, int count) + public static T[] ToArray(this Buffer source, int offset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -55,7 +54,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The input array to write data to. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, T[] destination) + public static void CopyTo(this Buffer source, T[] destination) where T : unmanaged { Guard.IsNotNull(source); @@ -73,7 +72,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -90,7 +89,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The input to write data to. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Span destination) + public static void CopyTo(this Buffer source, Span destination) where T : unmanaged { Guard.IsNotNull(source); @@ -105,7 +104,7 @@ public static class BufferExtensions /// The input instance to read data from. /// The input to write data to. /// The offset to start reading data from. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Span destination, int sourceOffset) + public static void CopyTo(this Buffer source, Span destination, int sourceOffset) where T : unmanaged { Guard.IsNotNull(source); @@ -119,7 +118,7 @@ public static class BufferExtensions /// The type of items stored on the buffers. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Buffer destination) + public static void CopyTo(this Buffer source, Buffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -137,7 +136,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -152,7 +151,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input array to read data from. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, T[] source) + public static void CopyFrom(this Buffer destination, T[] source) where T : unmanaged { Guard.IsNotNull(destination); @@ -170,7 +169,7 @@ public static class BufferExtensions /// The starting offset within to read data from. /// The offset to start writing data to. /// The number of items to write. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); @@ -187,7 +186,7 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input to read data from. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, ReadOnlySpan source) + public static void CopyFrom(this Buffer destination, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(destination); @@ -202,7 +201,7 @@ public static class BufferExtensions /// The target instance to write data to. /// The input to read data from. /// The offset to start writing data to. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, ReadOnlySpan source, int destinationOffset) + public static void CopyFrom(this Buffer destination, ReadOnlySpan source, int destinationOffset) where T : unmanaged { Guard.IsNotNull(destination); @@ -216,7 +215,7 @@ public static class BufferExtensions /// The type of items stored on the buffers. /// The input instance to write data data to. /// The source instance to read data from. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, Buffer source) + public static void CopyFrom(this Buffer destination, Buffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -234,7 +233,7 @@ public static class BufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index e545adbf..5d3f3172 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -18,7 +18,8 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -33,7 +34,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A constant instance with the contents of the input array. - public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -49,7 +51,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A constant instance with the contents of the input . - public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -68,7 +71,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A constant instance with the contents of the input . - public static ConstantBuffer AllocateConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); @@ -89,7 +93,8 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -104,7 +109,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A read write instance with the contents of the input array. - public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -120,7 +126,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -139,7 +146,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadOnlyBuffer AllocateReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); @@ -576,7 +584,8 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A zeroed instance of size . - public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { Guard.IsNotNull(device); @@ -591,7 +600,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input array with the data to copy on the allocated buffer. /// A read write instance with the contents of the input array. - public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, T[] source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { Guard.IsNotNull(device); @@ -607,7 +617,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, ReadOnlySpan source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { Guard.IsNotNull(device); @@ -626,7 +637,8 @@ partial class GraphicsDeviceExtensions /// The instance to use to allocate the buffer. /// The input with the data to copy on the allocated buffer. /// A read write instance with the contents of the input . - public static ReadWriteBuffer AllocateReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this GraphicsDevice device, Buffer source) + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { Guard.IsNotNull(device); diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs index ce322cbf..0940cf5d 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using CommunityToolkit.Diagnostics; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -15,7 +14,7 @@ public static class ReadBackBufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input instance to read data from. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this ReadBackBuffer destination, StructuredBuffer source) + public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -33,7 +32,7 @@ public static class ReadBackBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index 6f86a471..1166042e 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using CommunityToolkit.Diagnostics; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -15,7 +14,7 @@ public static class StructuredBufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer source, ReadBackBuffer destination) + public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -33,7 +32,7 @@ public static class StructuredBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); @@ -48,7 +47,7 @@ public static class StructuredBufferExtensions /// The type of items stored on the buffer. /// The target instance to write data to. /// The input instance to read data from. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer destination, UploadBuffer source) + public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source) where T : unmanaged { Guard.IsNotNull(destination); @@ -66,7 +65,7 @@ public static class StructuredBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(destination); diff --git a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs index 4a3aa8d4..118eeb9e 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using CommunityToolkit.Diagnostics; +using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -15,7 +14,7 @@ public static class UploadBufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this UploadBuffer source, StructuredBuffer destination) + public static void CopyTo(this UploadBuffer source, StructuredBuffer destination) where T : unmanaged { Guard.IsNotNull(source); @@ -33,7 +32,7 @@ public static class UploadBufferExtensions /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Guard.IsNotNull(source); diff --git a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs index 6e4939bf..5f113201 100644 --- a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs +++ b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs @@ -8,7 +8,8 @@ namespace ComputeSharp.Graphics.Helpers; /// A class providing info on specific types. /// /// The input type to extract info for. -internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> +[RequiresUnreferencedCode("This method reads type info of all fields of the input type (recursively).")] +internal static class TypeInfo where T : unmanaged { /// @@ -21,11 +22,8 @@ internal static class TypeInfo<[DynamicallyAccessedMembers(DynamicallyAccessedMe /// /// The current type to check. /// Whether or not is or contains a field. - [UnconditionalSuppressMessage( - "ReflectionAnalysis", - "IL2026:RequiresUnreferencedCode", - Justification = "The types of all fields (recursively) are already guaranteed to be preserved given it's all value types (plus T is already annotated).")] - private static bool ChecksIsDoubleOrContainsDoubles([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] Type type) + [RequiresUnreferencedCode("This method reads type info of all fields of the input type (recursively).")] + private static bool ChecksIsDoubleOrContainsDoubles(Type type) { if (type == typeof(double)) return true; diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 23997e72..fd4dea7c 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -1,6 +1,5 @@ using System; using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Interop; @@ -63,7 +62,7 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the buffer. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) where T : unmanaged { Guard.IsNotNull(buffer); diff --git a/src/ComputeSharp/Graphics/Interop/InteropServices.cs b/src/ComputeSharp/Graphics/Interop/InteropServices.cs index f6e06929..5427cb64 100644 --- a/src/ComputeSharp/Graphics/Interop/InteropServices.cs +++ b/src/ComputeSharp/Graphics/Interop/InteropServices.cs @@ -1,6 +1,5 @@ using System; using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; using ComputeSharp.Core.Extensions; using ComputeSharp.Resources; @@ -50,7 +49,7 @@ public static unsafe class InteropServices /// The address of a pointer to an interface with the IID specified in . /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. - public static void GetID3D12Resource<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, Guid* riid, void** ppvObject) + public static void GetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { using var _0 = buffer.GetReferenceTrackingLease(); @@ -180,7 +179,7 @@ public static unsafe class InteropServices /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// /// The instance has been disposed. - public static int TryGetID3D12Resource<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(Buffer buffer, Guid* riid, void** ppvObject) + public static int TryGetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { using var _0 = buffer.GetReferenceTrackingLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 1fb3a508..05fd79ee 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public unsafe abstract class Buffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : NativeObject, IGraphicsResource +public unsafe abstract class Buffer : NativeObject, IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -63,6 +63,7 @@ public unsafe abstract class Buffer<[DynamicallyAccessedMembers(DynamicallyAcces /// The size in bytes of each buffer item (including padding, if any). /// The resource type for the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode) { if (resourceType == ResourceType.Constant) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 88f47a2e..5ccda018 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -15,7 +15,7 @@ namespace ComputeSharp.Resources; /// A representing a typed structured buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public abstract class StructuredBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : Buffer +public abstract class StructuredBuffer : Buffer where T : unmanaged { /// @@ -25,6 +25,7 @@ public abstract class StructuredBuffer<[DynamicallyAccessedMembers(DynamicallyAc /// The number of items to store in the current buffer. /// The buffer type for the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] private protected unsafe StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode) : base(device, length, (uint)sizeof(T), resourceType, allocationMode) { diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index e131d2a9..56f4d842 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -20,7 +20,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : Buffer +public sealed class ConstantBuffer : Buffer where T : unmanaged { /// @@ -34,6 +34,7 @@ public sealed class ConstantBuffer<[DynamicallyAccessedMembers(DynamicallyAccess /// The associated with the current instance. /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] internal ConstantBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) : base(device, length, (uint)GetPaddedSize(), ResourceType.Constant, allocationMode) { diff --git a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs index a290ea03..6ee0d853 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; #if NET6_0_OR_GREATER using GC = System.GC; #else @@ -12,7 +11,7 @@ namespace ComputeSharp.Resources.Debug; /// A debug proxy used to display items in a instance. /// /// The type of items to display. -internal sealed class BufferDebugView<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> +internal sealed class BufferDebugView where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs index 98d7108e..ffa70ebb 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadOnlyBuffer{T}.cs @@ -13,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : StructuredBuffer +public sealed class ReadOnlyBuffer : StructuredBuffer where T : unmanaged { /// @@ -22,6 +22,7 @@ public sealed class ReadOnlyBuffer<[DynamicallyAccessedMembers(DynamicallyAccess /// The associated with the current instance. /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] internal ReadOnlyBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) : base(device, length, ResourceType.ReadOnly, allocationMode) { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs index 315eae39..45e82eed 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteBuffer{T}.cs @@ -13,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the buffer. [DebuggerTypeProxy(typeof(BufferDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T> : StructuredBuffer +public sealed class ReadWriteBuffer : StructuredBuffer where T : unmanaged { /// @@ -22,6 +22,7 @@ public sealed class ReadWriteBuffer<[DynamicallyAccessedMembers(DynamicallyAcces /// The associated with the current instance. /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] internal ReadWriteBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) : base(device, length, ResourceType.ReadWrite, allocationMode) { diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index f150cd76..94c85fe2 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -21,7 +21,7 @@ public static class ComputeContextExtensions /// The type of items stored on the buffer. /// The to use to insert the resource barrier. /// The input instance to insert the barrier for. - public static unsafe void Barrier<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this in ComputeContext context, ReadWriteBuffer buffer) + public static unsafe void Barrier(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { Guard.IsNotNull(buffer); @@ -137,7 +137,7 @@ public static class ComputeContextExtensions /// The type of items stored on the buffer. /// The to use to clear the resource. /// The input instance to clear. - public static unsafe void Clear<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] T>(this in ComputeContext context, ReadWriteBuffer buffer) + public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { Guard.IsNotNull(buffer); From bc15b590c03357c5396d48e9886a37b99539cf50 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 5 Aug 2022 14:10:55 +0200 Subject: [PATCH 11/11] Remove leftover [UnconditionalSuppressMessage] polyfill --- .../ComputeSharp.NetStandard.projitems | 1 - .../UnconditionalSuppressMessageAttribute.cs | 83 ------------------- 2 files changed, 84 deletions(-) delete mode 100644 src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 65761606..9c229e84 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -13,7 +13,6 @@ - diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs deleted file mode 100644 index d5419bb3..00000000 --- a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/UnconditionalSuppressMessageAttribute.cs +++ /dev/null @@ -1,83 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Diagnostics.CodeAnalysis; - -/// -/// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a single code artifact. -/// -/// -/// is different than -/// in that it doesn't have a -/// . So it is always preserved in the compiled assembly. -/// -[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] -internal sealed class UnconditionalSuppressMessageAttribute : Attribute -{ - /// - /// Initializes a new instance of the - /// class, specifying the category of the tool and the identifier for an analysis rule. - /// - /// The category for the attribute. - /// The identifier of the analysis rule the attribute applies to. - public UnconditionalSuppressMessageAttribute(string category, string checkId) - { - Category = category; - CheckId = checkId; - } - - /// - /// Gets the category identifying the classification of the attribute. - /// - /// - /// The property describes the tool or tool analysis category - /// for which a message suppression attribute applies. - /// - public string Category { get; } - - /// - /// Gets the identifier of the analysis tool rule to be suppressed. - /// - /// - /// Concatenated together, the and - /// properties form a unique check identifier. - /// - public string CheckId { get; } - - /// - /// Gets or sets the scope of the code that is relevant for the attribute. - /// - /// - /// The Scope property is an optional argument that specifies the metadata scope for which - /// the attribute is relevant. - /// - public string? Scope { get; set; } - - /// - /// Gets or sets a fully qualified path that represents the target of the attribute. - /// - /// - /// The property is an optional argument identifying the analysis target - /// of the attribute. An example value is "System.IO.Stream.ctor():System.Void". - /// Because it is fully qualified, it can be long, particularly for targets such as parameters. - /// The analysis tool user interface should be capable of automatically formatting the parameter. - /// - public string? Target { get; set; } - - /// - /// Gets or sets an optional argument expanding on exclusion criteria. - /// - /// - /// The property is an optional argument that specifies additional - /// exclusion where the literal metadata target is not sufficiently precise. For example, - /// the cannot be applied within a method, - /// and it may be desirable to suppress a violation against a statement in the method that will - /// give a rule violation, but not against all statements in the method. - /// - public string? MessageId { get; set; } - - /// - /// Gets or sets the justification for suppressing the code analysis message. - /// - public string? Justification { get; set; } -} \ No newline at end of file