Table of Contents

Class VertexBuffer

Namespace
Microsoft.Xna.Framework.Graphics
Assembly
MonoGame.Framework.dll

Represents a list of 3D vertices to be streamed to the graphics device.

public class VertexBuffer : GraphicsResource, IDisposable
Inheritance
VertexBuffer
Implements
Derived
Inherited Members

Constructors

VertexBuffer(GraphicsDevice, VertexDeclaration, int, BufferUsage)

Creates a new instance of VertexBuffer

public VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage bufferUsage)

Parameters

graphicsDevice GraphicsDevice

The graphics device.

vertexDeclaration VertexDeclaration

The vertex declaration, which describes per-vertex data.

vertexCount int

The number of vertices.

bufferUsage BufferUsage

Behavior options.

Exceptions

ArgumentNullException

graphicsDevice is null

VertexBuffer(GraphicsDevice, VertexDeclaration, int, BufferUsage, bool)

Creates a new instance of VertexBuffer

protected VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage bufferUsage, bool dynamic)

Parameters

graphicsDevice GraphicsDevice

The graphics device.

vertexDeclaration VertexDeclaration

The vertex declaration, which describes per-vertex data.

vertexCount int

The number of vertices.

bufferUsage BufferUsage

Behavior options.

dynamic bool

Whether this buffer is dynmic.

Exceptions

ArgumentNullException

graphicsDevice is null

VertexBuffer(GraphicsDevice, Type, int, BufferUsage)

Creates a new instance of VertexBuffer

public VertexBuffer(GraphicsDevice graphicsDevice, Type type, int vertexCount, BufferUsage bufferUsage)

Parameters

graphicsDevice GraphicsDevice

The graphics device.

type Type

The data type. Must be a value type which implements the IVertexType interface.

vertexCount int

The number of vertices.

bufferUsage BufferUsage

Behavior options.

Exceptions

ArgumentNullException

graphicsDevice is null

Properties

BufferUsage

A usage hint for optimizing memory placement of this buffer.

public BufferUsage BufferUsage { get; }

Property Value

BufferUsage

VertexCount

Gets the number of vertices.

public int VertexCount { get; }

Property Value

int

VertexDeclaration

Defines per-vertex data in a buffer.

public VertexDeclaration VertexDeclaration { get; }

Property Value

VertexDeclaration

Methods

Dispose(bool)

protected override void Dispose(bool disposing)

Parameters

disposing bool

GetData<T>(int, T[], int, int, int)

Get the vertex data froom this VertexBuffer.

public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride = 0) where T : struct

Parameters

offsetInBytes int

The offset to the first element in the vertex buffer in bytes.

data T[]

An array of T's to be filled.

startIndex int

The index to start filling the data array.

elementCount int

The number of T's to get.

vertexStride int

The size of how a vertex buffer element should be interpreted.

Type Parameters

T

The struct you want to fill.

Remarks

Note that this pulls data from VRAM into main memory and because of that is a very expensive operation. It is often a better idea to keep a copy of the data in main memory.

GetData<T>(T[])

Get the vertex data froom this VertexBuffer.

public void GetData<T>(T[] data) where T : struct

Parameters

data T[]

An array of T's to be filled.

Type Parameters

T

The struct you want to fill.

Remarks

Note that this pulls data from VRAM into main memory and because of that is a very expensive operation. It is often a better idea to keep a copy of the data in main memory.

GetData<T>(T[], int, int)

Get the vertex data froom this VertexBuffer.

public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct

Parameters

data T[]

An array of T's to be filled.

startIndex int

The index to start filling the data array.

elementCount int

The number of T's to get.

Type Parameters

T

The struct you want to fill.

Remarks

Note that this pulls data from VRAM into main memory and because of that is a very expensive operation. It is often a better idea to keep a copy of the data in main memory.

GraphicsDeviceResetting()

The GraphicsDevice is resetting, so GPU resources must be recreated.

protected override void GraphicsDeviceResetting()

SetDataInternal<T>(int, T[], int, int, int, SetDataOptions)

protected void SetDataInternal<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride, SetDataOptions options) where T : struct

Parameters

offsetInBytes int
data T[]
startIndex int
elementCount int
vertexStride int
options SetDataOptions

Type Parameters

T

SetData<T>(int, T[], int, int, int)

Sets the vertex buffer data, specifying the index at which to start copying from the source data array, the number of elements to copy from the source data array, and how far apart elements from the source data array should be when they are copied into the vertex buffer.

public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct

Parameters

offsetInBytes int

Offset in bytes from the beginning of the vertex buffer to the start of the copied data.

data T[]

Data array to be passed to the shader.

startIndex int

Index at which to start copying from data. Must be within the data array bounds.

elementCount int

Number of elements to copy from data. The combination of startIndex and elementCount must be within the data array bounds.

vertexStride int

Specifies how far apart, in bytes, elements from data should be when they are copied into the vertex buffer. In almost all cases this should be sizeof(T), to create a tightly-packed vertex buffer. If you specify sizeof(T), elements from data will be copied into the vertex buffer with no padding between each element. If you specify a value greater than sizeof(T), elements from data will be copied into the vertex buffer with padding between each element. If you specify 0 for this parameter, it will be treated as if you had specified sizeof(T). With the exception of 0, you must specify a value greater than or equal to sizeof(T).

Type Parameters

T

Type of elements in the data array.

Remarks

If T is VertexPositionTexture, but you want to set only the position component of the vertex data, you would call this method as follows as the position is the first piece of data in the buffer:

Vector3[] positions = new Vector3[numVertices];
vertexBuffer.SetData(0, positions, 0, numVertices, vertexBuffer.VertexDeclaration.VertexStride);

Continuing from the previous example, if you want to set only the texture coordinate component of the vertex data, you would call this method as follows (note that offsetInBytes is 12, the size of a Vector3, representing the position):

Vector2[] texCoords = new Vector2[numVertices];
vertexBuffer.SetData(12, texCoords, 0, numVertices, vertexBuffer.VertexDeclaration.VertexStride);

SetData<T>(T[])

Sets the vertex buffer data. This is the same as calling SetData<T>(int, T[], int, int, int) with offsetInBytes and startIndex equal to 0, elementCount equal to data.Length, and vertexStride equal to sizeof(T).

public void SetData<T>(T[] data) where T : struct

Parameters

data T[]

Data array to be passed to the shader.

Type Parameters

T

Type of elements in the data array.

SetData<T>(T[], int, int)

Sets the vertex buffer data, specifying the index at which to start copying from the source data array, and the number of elements to copy from the source data array. This is the same as calling SetData<T>(int, T[], int, int, int) with offsetInBytes equal to 0, and vertexStride equal to sizeof(T).

public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct

Parameters

data T[]

Data array to be passed to the shader.

startIndex int

Index at which to start copying from data. Must be within the data array bounds.

elementCount int

Number of elements to copy from data. The combination of startIndex and elementCount must be within the data array bounds.

Type Parameters

T

Type of elements in the data array.