Interface VertexBufferHelper.IReader<TDest>
public interface VertexBufferHelper.IReader<TDest>
Type Parameters
TDest
Examples
Implementing Copy<TSemantic, TValue>(Span<TValue>, int) manually:
Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);
var vertexPositions = new Vector3[count];
var myReader = new CopyTo();
helper.Read<PositionSemantic, Vector3, CopyTo>(vertexPositions, myReader);
struct CopyTo : IReader<Vector3>
{
public unsafe void Read<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride, Span<T> destination)
where TConverter : IConverter<TSource, T> where TSource : unmanaged
{
if (destination.Length != elementCount)
throw new ArgumentException($"{nameof(destination)} length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})");
fixed (T* ptrDest = destination)
{
byte* end = sourcePointer + elementCount * stride;
T* dest = ptrDest;
for (; sourcePointer < end; sourcePointer += stride, dest++)
TConverter.Convert(*(TSource*)sourcePointer, out *dest);
}
}
}
Methods
Read<TConverter, TSource>(byte*, int, int, Span<TDest>)
void Read<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride, Span<TDest> destination) where TConverter : IConverter<TSource, TDest> where TSource : unmanaged
Parameters
sourcePointerbyte*Points to the first element in the vertex buffer, read it as a TSource* to retrieve its value
elementCountintThe amount of vertices. This is not equivalent to the size of the vertex buffer, or the size in bytes taken by individual vertices
strideintThe size in bytes taken by individual vertices, add it to
sourcePointerto point to the next elementdestinationSpan<TDest>The span passed into the Read<TConverter, TSource>(byte*, int, int, Span<TDest>) method call
Type Parameters
TConverterA helper to convert between
TSourceandTDestproperlyTSourceThe source type this vertex buffer was built with, for example Vector2 or Byte4, use
TConverterto convert it into aTDest.
Examples
Implementing Copy<TSemantic, TValue>(Span<TValue>, int) manually:
Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);
var vertexPositions = new Vector3[count];
var myReader = new CopyTo();
helper.Read<PositionSemantic, Vector3, CopyTo>(vertexPositions, myReader);
struct CopyTo : IReader<Vector3>
{
public unsafe void Read<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride, Span<T> destination)
where TConverter : IConverter<TSource, T> where TSource : unmanaged
{
if (destination.Length != elementCount)
throw new ArgumentException($"{nameof(destination)} length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})");
fixed (T* ptrDest = destination)
{
byte* end = sourcePointer + elementCount * stride;
T* dest = ptrDest;
for (; sourcePointer < end; sourcePointer += stride, dest++)
TConverter.Convert(*(TSource*)sourcePointer, out *dest);
}
}
}