diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs index bdde644f..d454a8f7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -1,10 +1,7 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; -using System.Linq; using System.Numerics; -using System.Reflection; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; @@ -15,60 +12,16 @@ namespace ComputeSharp.Graphics.Buffers.Extensions /// internal static class GraphicsResourceExtensions { - /// - /// The that maps types to the necessary data to create buffers, for quick lookup - /// - private static readonly Dictionary TypeMapping = new Dictionary(); - - /// - /// Allocates a new constant buffer with the specified generic value - /// - /// The instance to use to allocate the buffer - /// The input to copy to the allocated buffer - /// A constant instance (as a ) with the input data - [Pure] - public static GraphicsResource AllocateConstantBufferFromReflectedSingleValue(this GraphicsDevice device, object data) - { - Type dataType = data.GetType(); - if (!TypeMapping.TryGetValue(dataType, out var info)) - { - // Get the generic buffer constructor - Type bufferType = typeof(ConstantBuffer<>).MakeGenericType(dataType); - info.Constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First(); - - // Create the reusable array - Type arrayType = dataType.MakeArrayType(); - ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance - | BindingFlags.Public - | BindingFlags.Instance - | BindingFlags.OptionalParamBinding); - info.Array = (IList)arrayConstructors[0].Invoke(new object[] { 1 }); - - // Set the input data to the new buffer - info.Setter = bufferType.GetMethod(nameof(HlslBuffer.SetData), new[] { arrayType }); - - // Cache for later reuse - TypeMapping.Add(dataType, info); - } - - // Create the buffer and set the content - GraphicsResource buffer = (GraphicsResource)info.Constructor.Invoke(new object[] { device, 1 }); - info.Array[0] = data; - info.Setter.Invoke(buffer, new object[] { info.Array }); - - return buffer; - } - /// /// Allocates a new constant buffer with the specified generic values /// /// The instance to use to allocate the buffer - /// The input array to copy to the allocated buffer + /// The input sequence to copy to the allocated buffer /// A constant instance (as a ) with the input data [Pure] - public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, object[] data) + public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, IReadOnlyList data) { - Span temporarySpan = stackalloc Vector4[data.Length]; + Span temporarySpan = stackalloc Vector4[data.Count]; ref byte r0 = ref Unsafe.As(ref temporarySpan.GetPinnableReference()); int offset = 0; @@ -82,7 +35,7 @@ namespace ComputeSharp.Graphics.Buffers.Extensions } // Iterate on the input values and write them with the proper padding - for (int j = 0; j < data.Length; j++) + for (int j = 0; j < data.Count; j++) { switch (data[j]) { @@ -103,7 +56,7 @@ namespace ComputeSharp.Graphics.Buffers.Extensions } // Create the array to return - ConstantBuffer buffer = new ConstantBuffer(device, data.Length); + ConstantBuffer buffer = new ConstantBuffer(device, data.Count); buffer.SetData(temporarySpan); return buffer;