Added extension method to create packed buffer from values

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-17 02:22:06 +02:00
1 親 82479fcae9
コミット 0fa28d089f
1ファイル変更
+48
+48
ファイルの表示
@@ -3,7 +3,9 @@ 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;
namespace ComputeSharp.Graphics.Buffers.Extensions
@@ -56,5 +58,51 @@ namespace ComputeSharp.Graphics.Buffers.Extensions
return buffer;
}
/// <summary>
/// Allocates a new constant buffer with the specified generic values
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
/// <param name="data">The input <see cref="object"/> array to copy to the allocated buffer</param>
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance (as a <see cref="GraphicsResource"/>) with the input data</returns>
[Pure]
public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, object[] data)
{
Span<Vector4> temporarySpan = stackalloc Vector4[data.Length];
ref byte r0 = ref Unsafe.As<Vector4, byte>(ref temporarySpan.GetPinnableReference());
int offset = 0;
// Local method to write a value in the temporary buffer
void WriteValue<T>(T value, ref byte target) where T : unmanaged
{
int size = Unsafe.SizeOf<T>();
if (offset % 16 > 16 - size) offset += 16 - offset % 16;
Unsafe.As<byte, T>(ref Unsafe.Add(ref target, offset)) = value;
offset += size;
}
// Iterate on the input values and write them with the proper padding
for (int j = 0; j < data.Length; j++)
{
switch (data[j])
{
case bool b: WriteValue(b, ref r0); break;
case int i: WriteValue(i, ref r0); break;
case uint u: WriteValue(u, ref r0); break;
case float f: WriteValue(f, ref r0); break;
case double d: WriteValue(d, ref r0); break;
case Vector2 v2: WriteValue(v2, ref r0); break;
case Vector3 v3: WriteValue(v3, ref r0); break;
case Vector4 v4: WriteValue(v4, ref r0); break;
default: throw new InvalidOperationException($"Invalid item of type {data[j].GetType()}");
}
}
// Create the array to return
ConstantBuffer<Vector4> buffer = new ConstantBuffer<Vector4>(device, data.Length);
buffer.SetData(temporarySpan);
return buffer;
}
}
}