/// <summary> /// Allocates a new <see cref="Buffer{T}"/> instance of the specified type. /// </summary> /// <typeparam name="T">The type of items in the buffer.</typeparam> /// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param> /// <param name="type">The type of buffer to allocate.</param> /// <param name="length">The length of the buffer to create.</param> /// <param name="allocationMode">The allocation mode to use for the new resource.</param> /// <returns>A <see cref="Buffer{T}"/> instance of the requested size.</returns> public static Buffer <T> AllocateBuffer <T>(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return(type switch { _ when type == typeof(ConstantBuffer <>) => device.AllocateConstantBuffer <T>(length, allocationMode), _ when type == typeof(ReadOnlyBuffer <>) => device.AllocateReadOnlyBuffer <T>(length, allocationMode), _ when type == typeof(ReadWriteBuffer <>) => device.AllocateReadWriteBuffer <T>(length, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) });