Table of Contents

Interface VertexBufferHelper.IReader<TDest>

Namespace
Stride.Graphics
Assembly
Stride.Graphics.dll
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

sourcePointer byte*

Points to the first element in the vertex buffer, read it as a TSource* to retrieve its value

elementCount int

The amount of vertices. This is not equivalent to the size of the vertex buffer, or the size in bytes taken by individual vertices

stride int

The size in bytes taken by individual vertices, add it to sourcePointer to point to the next element

destination Span<TDest>

The span passed into the Read<TConverter, TSource>(byte*, int, int, Span<TDest>) method call

Type Parameters

TConverter

A helper to convert between TSource and TDest properly

TSource

The source type this vertex buffer was built with, for example Vector2 or Byte4, use TConverter to convert it into a 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);
       }
   }
}