From 3deaeb0328b757cdd71b5a5af43850aac0b55a76 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 5 Aug 2022 14:10:09 +0200 Subject: [PATCH] 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);