Table of Contents

Class Buffer

Namespace
Stride.Graphics
Assembly
Stride.Graphics.dll

All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.

[DataSerializer(typeof(BufferSerializer))]
[DataSerializerGlobal(typeof(ReferenceSerializer<Buffer>), null, DataSerializerGenericMode.None, false, false, Profile = "Content")]
[ContentSerializer(typeof(DataContentSerializer<Buffer>))]
public class Buffer : GraphicsResource, IDisposable, IComponent, IReferencable, ICollectorHolder
Inheritance
Buffer
Implements
Derived
Inherited Members
Extension Methods

Remarks

This class is able to create constant buffers, indexelementCountrtex buffers, structured buffer, raw buffers, argument buffers.

Constructors

Buffer()

public Buffer()

Buffer(GraphicsDevice)

Initializes a new instance of the Buffer class.

protected Buffer(GraphicsDevice device)

Parameters

device GraphicsDevice

The GraphicsDevice.

Fields

elementCount

protected int elementCount

Field Value

int

Properties

Description

Gets the description of this buffer.

public BufferDescription Description { get; }

Property Value

BufferDescription

ElementCount

Gets the number of elements.

public int ElementCount { get; protected set; }

Property Value

int

Remarks

This value is valid for structured buffers, raw buffers and index buffers that are used as a SharedResourceView.

Flags

Buffer flags describing the type of buffer.

public BufferFlags Flags { get; }

Property Value

BufferFlags

InitialCounterOffset

The initial Append/Consume buffer counter offset. A value of -1 indicates the current offset should be kept. Any other values set the hidden counter for that Appendable/Consumable Buffer. This value is only relevant for Buffers which have the 'Append' or 'Counter' flag, otherwise it is ignored. The value get's initialized to -1.

public int InitialCounterOffset { get; set; }

Property Value

int

SizeInBytes

Gets the size of the buffer in bytes.

public int SizeInBytes { get; }

Property Value

int

The size of the buffer in bytes.

StructureByteStride

The size of the structure (in bytes) when it represents a structured/typed buffer.

public int StructureByteStride { get; }

Property Value

int

Usage

Value that identifies how the buffer is to be read from and written to.

public GraphicsResourceUsage Usage { get; }

Property Value

GraphicsResourceUsage

ViewFlags

Gets the type of this buffer view.

public BufferFlags ViewFlags { get; }

Property Value

BufferFlags

ViewFormat

Gets the format of this buffer view.

public PixelFormat ViewFormat { get; }

Property Value

PixelFormat

Methods

Clone()

Clones this instance.

public Buffer Clone()

Returns

Buffer

A clone of this instance

Remarks

This method will not copy the content of the buffer to the clone

GetData(CommandList, Buffer, DataPointer)

Copies the content of this buffer from GPU memory to a CPU memory using a specific staging resource.

public void GetData(CommandList commandList, Buffer stagingTexture, DataPointer toData)

Parameters

commandList CommandList
stagingTexture Buffer

The staging buffer used to transfer the buffer.

toData DataPointer

To data pointer.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice.

Exceptions

ArgumentException

When strides is different from optimal strides, and TData is not the same size as the pixel format, or Width * Height != toData.Length

GetData<TData>(CommandList)

Gets the content of this buffer to an array of data.

public TData[] GetData<TData>(CommandList commandList) where TData : unmanaged

Parameters

commandList CommandList

Returns

TData[]

Type Parameters

TData

The type of the T data.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice. This method creates internally a stagging resource if this texture is not already a stagging resouce, copies to it and map it to memory. Use method with explicit staging resource for optimal performances.

GetData<TData>(CommandList, Buffer, ref TData)

Copies the content of this buffer from GPU memory to an array of data on CPU memory using a specific staging resource.

public void GetData<TData>(CommandList commandList, Buffer stagingTexture, ref TData toData) where TData : unmanaged

Parameters

commandList CommandList
stagingTexture Buffer

The staging buffer used to transfer the buffer.

toData TData

To data.

Type Parameters

TData

The type of the T data.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice.

Exceptions

ArgumentException

When strides is different from optimal strides, and TData is not the same size as the pixel format, or Width * Height != toData.Length

GetData<TData>(CommandList, Buffer, TData[])

Copies the content of this buffer from GPU memory to an array of data on CPU memory using a specific staging resource.

public void GetData<TData>(CommandList commandList, Buffer stagingTexture, TData[] toData) where TData : unmanaged

Parameters

commandList CommandList
stagingTexture Buffer

The staging buffer used to transfer the buffer.

toData TData[]

To data.

Type Parameters

TData

The type of the T data.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice.

Exceptions

ArgumentException

When strides is different from optimal strides, and TData is not the same size as the pixel format, or Width * Height != toData.Length

GetData<TData>(CommandList, ref TData)

Copies the content of this buffer to an array of data.

public void GetData<TData>(CommandList commandList, ref TData toData) where TData : unmanaged

Parameters

commandList CommandList
toData TData

The destination buffer to receive a copy of the texture datas.

Type Parameters

TData

The type of the T data.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice. This method creates internally a stagging resource if this texture is not already a stagging resouce, copies to it and map it to memory. Use method with explicit staging resource for optimal performances.

GetData<TData>(CommandList, TData[])

Copies the content of this buffer to an array of data.

public void GetData<TData>(CommandList commandList, TData[] toData) where TData : unmanaged

Parameters

commandList CommandList
toData TData[]

The destination buffer to receive a copy of the texture datas.

Type Parameters

TData

The type of the T data.

Remarks

This method is only working when called from the main thread that is accessing the main GraphicsDevice. This method creates internally a stagging resource if this texture is not already a stagging resouce, copies to it and map it to memory. Use method with explicit staging resource for optimal performances.

InitializeFromImpl(BufferDescription, BufferFlags, PixelFormat, nint)

Initializes a new instance of the Buffer class.

protected Buffer InitializeFromImpl(BufferDescription description, BufferFlags viewFlags, PixelFormat viewFormat, nint dataPointer)

Parameters

description BufferDescription

The description.

viewFlags BufferFlags

Type of the buffer.

viewFormat PixelFormat

The view format.

dataPointer nint

The data pointer.

Returns

Buffer

New(GraphicsDevice, BufferDescription, PixelFormat)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat = PixelFormat.None)

Parameters

device GraphicsDevice

The GraphicsDevice.

description BufferDescription

The description of the buffer.

viewFormat PixelFormat

View format used if the buffer is used as a shared resource view.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, DataPointer, int, BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

dataPointer DataPointer

The data pointer.

elementSize int

Size of the element.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, DataPointer, int, BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

dataPointer DataPointer

The data pointer.

elementSize int

Size of the element.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, byte[], int, BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance from a byte array.

public static Buffer New(GraphicsDevice device, byte[] initialValue, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)

Parameters

device GraphicsDevice

The GraphicsDevice.

initialValue byte[]

The initial value of this buffer.

elementSize int

Size of an element. Must be equal to 2 or 4 for an index buffer, or to the size of a struct for a structured/typed buffer. Can be set to 0 for other buffers.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, int, BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

bufferSize int

Size of the buffer in bytes.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, int, BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

bufferSize int

Size of the buffer in bytes.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, int, int, BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

bufferSize int

Size of the buffer in bytes.

elementSize int

Size of an element in the buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New(GraphicsDevice, int, int, BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)

Parameters

device GraphicsDevice

The GraphicsDevice.

bufferSize int

Size of the buffer in bytes.

elementSize int

Size of an element in the buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer

An instance of a new Buffer

New<T>(GraphicsDevice, int, BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer<T> New<T>(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged

Parameters

device GraphicsDevice

The GraphicsDevice.

elementCount int

Number of T elment in this buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer<T>

An instance of a new Buffer

Type Parameters

T

New<T>(GraphicsDevice, ref T, BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer<T> New<T>(GraphicsDevice device, ref T value, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged

Parameters

device GraphicsDevice

The GraphicsDevice.

value T

The initial value of this buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer<T>

An instance of a new Buffer

Type Parameters

T

Type of the buffer, to get the sizeof from.

New<T>(GraphicsDevice, ref T, BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer<T> New<T>(GraphicsDevice device, ref T value, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged

Parameters

device GraphicsDevice

The GraphicsDevice.

value T

The initial value of this buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer<T>

An instance of a new Buffer

Type Parameters

T

Type of the buffer, to get the sizeof from.

New<T>(GraphicsDevice, T[], BufferFlags, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer<T> New<T>(GraphicsDevice device, T[] initialValue, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged

Parameters

device GraphicsDevice

The GraphicsDevice.

initialValue T[]

The initial value of this buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

usage GraphicsResourceUsage

The usage.

Returns

Buffer<T>

An instance of a new Buffer

Type Parameters

T

Type of the buffer, to get the sizeof from.

New<T>(GraphicsDevice, T[], BufferFlags, PixelFormat, GraphicsResourceUsage)

Creates a new Buffer instance.

public static Buffer<T> New<T>(GraphicsDevice device, T[] initialValue, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged

Parameters

device GraphicsDevice

The GraphicsDevice.

initialValue T[]

The initial value of this buffer.

bufferFlags BufferFlags

The buffer flags to specify the type of buffer.

viewFormat PixelFormat

The view format must be specified if the buffer is declared as a shared resource view.

usage GraphicsResourceUsage

The usage.

Returns

Buffer<T>

An instance of a new Buffer

Type Parameters

T

Type of the buffer, to get the sizeof from.

OnDestroyed()

Called when graphics device has been detected to be internally destroyed.

protected override void OnDestroyed()

OnNameChanged()

Called when Name property was changed.

protected override void OnNameChanged()

OnRecreate()

Called when graphics device has been recreated.

protected override bool OnRecreate()

Returns

bool

True if item transitioned to a Active state.

Recreate(nint)

Explicitly recreate buffer with given data. Usually called after a GraphicsDevice reset.

public void Recreate(nint dataPointer)

Parameters

dataPointer nint

RecreateWith(nint)

Reload Buffer from given data if GraphicsDevice has been reset.

public Buffer RecreateWith(nint dataPointer)

Parameters

dataPointer nint

The data pointer.

Returns

Buffer

This instance.

RecreateWith<T>(T[])

Reload Buffer from given data if GraphicsDevice has been reset.

public Buffer RecreateWith<T>(T[] dataPointer) where T : unmanaged

Parameters

dataPointer T[]

The data pointer.

Returns

Buffer

This instance.

Type Parameters

T

Recreate<T>(T[])

Explicitly recreate buffer with given data. Usually called after a GraphicsDevice reset.

public void Recreate<T>(T[] dataPointer) where T : unmanaged

Parameters

dataPointer T[]

Type Parameters

T

SetData(CommandList, DataPointer, int)

Copies the content an array of data on CPU memory to this buffer into GPU memory.

public void SetData(CommandList commandList, DataPointer fromData, int offsetInBytes = 0)

Parameters

commandList CommandList
fromData DataPointer

A data pointer.

offsetInBytes int

The offset in bytes to write to.

Remarks

See the unmanaged documentation about Map/UnMap for usage and restrictions.

Exceptions

ArgumentException

SetData<TData>(CommandList, ref TData, int)

Copies the content an array of data on CPU memory to this buffer into GPU memory.

public void SetData<TData>(CommandList commandList, ref TData fromData, int offsetInBytes = 0) where TData : unmanaged

Parameters

commandList CommandList
fromData TData

The data to copy from.

offsetInBytes int

The offset in bytes to write to.

Type Parameters

TData

The type of the T data.

Remarks

See the unmanaged documentation about Map/UnMap for usage and restrictions.

Exceptions

ArgumentException

SetData<TData>(CommandList, TData[], int)

Copies the content an array of data on CPU memory to this buffer into GPU memory.

public void SetData<TData>(CommandList commandList, TData[] fromData, int offsetInBytes = 0) where TData : unmanaged

Parameters

commandList CommandList
fromData TData[]

The data to copy from.

offsetInBytes int

The offset in bytes to write to.

Type Parameters

TData

The type of the T data.

Remarks

See the unmanaged documentation about Map/UnMap for usage and restrictions.

Exceptions

ArgumentException

ToStaging()

Return an equivalent staging texture CPU read-writable from this instance.

public Buffer ToStaging()

Returns

Buffer

A new instance of this buffer as a staging resource