Class Buffer
All-in-one GPU Buffer that is able to represent many types of Buffers (shader Constant Buffers, Structured Buffers, Raw Buffers, Argument Buffers, etc.).
[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
-
objectBuffer
- Implements
- Derived
-
Buffer<T>
- Inherited Members
- Extension Methods
Remarks
Buffer constains static methods for creating new Buffers by specifying all their characteristics.
Also look for the following static methods that aid in the creation of specific kinds of Buffers: Buffer.Argument (for Argument Buffers), Buffer.Constant (for Constant Buffers), Buffer.Index (for Index Buffers), Buffer.Raw (for Raw Buffers), Buffer.Structured (for Structured Buffers), Buffer.Typed (for Typed Buffers), and Buffer.Vertex (for Vertex Buffers).
Consult the documentation of your graphics API for more information on each kind of Buffer.
Constructors
Buffer()
public Buffer()
Buffer(GraphicsDevice)
Initializes a new instance of the Buffer class.
protected Buffer(GraphicsDevice device)
Parameters
deviceGraphicsDeviceThe GraphicsDevice the Buffer belongs to.
Buffer(GraphicsDevice, string?)
Initializes a new instance of the Buffer class.
protected Buffer(GraphicsDevice device, string? name)
Parameters
deviceGraphicsDeviceThe GraphicsDevice the Buffer belongs to.
namestringA string to use as a name for identifying the Buffer. Useful when debugging. Specify null to use the type's name instead.
Fields
elementCount
protected int elementCount
Field Value
Properties
Description
Gets a description of the Buffer.
public BufferDescription Description { get; }
Property Value
ElementCount
Gets the number of elements in the Buffer.
public int ElementCount { get; protected set; }
Property Value
Remarks
This value is valid for Structured Buffers, Raw Buffers, and Index Buffers that are used as a Shared Resource View.
Flags
Gets a combination of flags describing the type of the Buffer.
public BufferFlags Flags { get; }
Property Value
InitialCounterOffset
Gets or sets the initial Append / Consume Buffer counter offset.
public int InitialCounterOffset { get; set; }
Property Value
- int
A value of -1 indicates the current offset should be kept. Any other values set the hidden counter for that Appendable / Consumable Buffer. The default value is -1.
Remarks
This value is only relevant for Buffers which have the StructuredAppendBuffer or StructuredCounterBuffer flags, otherwise it is ignored.
SizeInBytes
Gets the size of the Buffer in bytes.
public int SizeInBytes { get; }
Property Value
StructureByteStride
Gets the size of the structure (in bytes) when the Buffer represents a typed / structured buffer.
public int StructureByteStride { get; }
Property Value
Usage
Gets a value that indicates how the Buffer is to be read from and written to.
public GraphicsResourceUsage Usage { get; }
Property Value
ViewFlags
Gets a combination of flags describing how a View over the Buffer should behave.
public BufferFlags ViewFlags { get; }
Property Value
ViewFormat
Gets the format of the elements of the Buffer as interpreted through a View.
public PixelFormat ViewFormat { get; }
Property Value
Methods
Clone()
Returns a new Buffer with exactly the same characteristics as the Buffer, but does not copy its contents.
public Buffer Clone()
Returns
- Buffer
A clone of the Buffer.
GetData(CommandList, Buffer, DataPointer)
Copies the contents of the Buffer from GPU memory to a CPU memory pointer using a specific staging resource.
[Obsolete("This method is obsolete. Use the Span-based methods instead")]
public void GetData(CommandList commandList, Buffer stagingBuffer, DataPointer toData)
Parameters
commandListCommandListThe CommandList.
stagingBufferBufferThe staging buffer used to transfer the data from GPU memory.
toDataDataPointerTo destination data pointer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
GetData<TData>(CommandList)
Gets the contents of the Buffer as an array of data.
public TData[] GetData<TData>(CommandList commandList) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
Returns
- TData[]
An array of data with the contents of the Buffer.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
This method creates internally a staging resource (if this Buffer is not already a staging resource), copies to it and map it to memory. Use a method that allows to specify an explicit staging resource for optimal performance.
GetData<TData>(CommandList, Buffer, Span<TData>)
Copies the content of the Buffer from GPU memory to a CPU memory pointer using a specific staging resource.
public void GetData<TData>(CommandList commandList, Buffer stagingBuffer, Span<TData> toData) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
stagingBufferBufferThe staging buffer used to transfer the data from GPU memory.
toDataSpan<TData>To destination span where the read data will be written.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
GetData<TData>(CommandList, Buffer, ref TData)
Copies a single data element of the Buffer from GPU memory to data on CPU memory using a specific staging resource.
public void GetData<TData>(CommandList commandList, Buffer stagingBuffer, ref TData toData) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
stagingBufferBufferThe staging buffer used to transfer the data from GPU memory.
toDataTDataWhen this method returns, contains the element read from the Buffer.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
GetData<TData>(CommandList, Buffer, TData[])
Copies data from the Buffer from GPU memory into an array on CPU memory using a specific staging resource.
public void GetData<TData>(CommandList commandList, Buffer stagingBuffer, TData[] toData) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
stagingBufferBufferThe staging buffer used to transfer the data from GPU memory.
toDataTData[]Array where the read data should be copied.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
GetData<TData>(CommandList, ref TData)
Gets a single data element of the Buffer.
public void GetData<TData>(CommandList commandList, ref TData toData) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
toDataTDataWhen this method returns, contains the element read from the Buffer.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
This method creates internally a staging resource (if this Buffer is not already a staging resource), copies to it and map it to memory. Use a method that allows to specify an explicit staging resource for optimal performance.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
GetData<TData>(CommandList, TData[])
Copies the contents of the Buffer to an array of data.
public void GetData<TData>(CommandList commandList, TData[] toData) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
toDataTData[]The destination array where to copy the Buffer contents.
Type Parameters
TDataThe type of the data to read from the Buffer.
Remarks
This method only works when called from the main thread that is accessing the main GraphicsDevice.
This method creates internally a staging resource (if this Buffer is not already a staging resource), copies to it and map it to memory. Use a method that allows to specify an explicit staging resource for optimal performance.
Exceptions
- ArgumentException
The length of the destination data buffer (
toData) is larger than the size of the Buffer.
InitializeFromImpl(ref readonly BufferDescription, BufferFlags, PixelFormat, nint)
Initializes this Buffer instance with the provided options.
protected Buffer InitializeFromImpl(ref readonly BufferDescription description, BufferFlags viewFlags, PixelFormat viewFormat, nint dataPointer)
Parameters
descriptionBufferDescriptionA BufferDescription structure describing the buffer characteristics.
viewFlagsBufferFlagsA combination of flags determining how the Views over this buffer should behave.
viewFormatPixelFormatView format used if the buffer is used as a Shader Resource View, or None if not.
dataPointernintThe data pointer to the data to initialize the buffer with.
Returns
Exceptions
- ArgumentException
Element size (
StructureByteStride) must be greater than zero for Structured Buffers.
New(GraphicsDevice, BufferDescription, PixelFormat)
Creates a new Buffer.
public static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat = PixelFormat.None)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
descriptionBufferDescriptionThe description of the Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
Returns
New(GraphicsDevice, DataPointer, int, BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
[Obsolete("This method is obsolete. Use the span-based methods instead")]
public static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
dataPointerDataPointerThe data pointer to the initial data the Buffer will contain.
elementSizeintThe size of an element in bytes.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, DataPointer, int, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
[Obsolete("This method is obsolete. Use the span-based methods instead")]
public static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
dataPointerDataPointerThe data pointer to the initial data the Buffer will contain.
elementSizeintThe size of an element in bytes.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, int, BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
bufferSizeintSize of the Buffer in bytes.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, int, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
bufferSizeintSize of the Buffer in bytes.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, int, int, BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
bufferSizeintSize of the Buffer in bytes.
elementSizeintSize of an element in the Buffer.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, int, int, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
bufferSizeintSize of the Buffer in bytes.
elementSizeintSize of an element in the Buffer.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New(GraphicsDevice, ReadOnlySpan<byte>, int, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer from a byte array.
public static Buffer New(GraphicsDevice device, ReadOnlySpan<byte> initialValues, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
initialValuesReadOnlySpan<byte>The initial data the Buffer will contain.
elementSizeintThe size of an element in bytes.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
New<T>(GraphicsDevice, int, BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
elementCountintNumber of elements of type
Tthe Buffer will contain.bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of elements the Buffer will contain.
New<T>(GraphicsDevice, ReadOnlySpan<T>, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, ReadOnlySpan<T> initialValues, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
initialValuesReadOnlySpan<T>The initial data the Buffer will contain.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of the elements the Buffer will contain.
New<T>(GraphicsDevice, ref readonly T, BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, ref readonly T value, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
valueTThe initial value for the element in the Buffer.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of the element the Buffer will contain.
New<T>(GraphicsDevice, ref readonly T, BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, ref readonly T value, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
valueTThe initial value for the element in the Buffer.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of the element the Buffer will contain.
New<T>(GraphicsDevice, T[], BufferFlags, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, T[] initialValue, BufferFlags bufferFlags, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
initialValueT[]The initial data the Buffer will contain.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of the elements the Buffer will contain.
New<T>(GraphicsDevice, T[], BufferFlags, PixelFormat, GraphicsResourceUsage)
Creates a new Buffer.
public static Buffer<T> New<T>(GraphicsDevice device, T[] initialValue, BufferFlags bufferFlags, PixelFormat viewFormat = PixelFormat.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : unmanaged
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
initialValueT[]The initial value of the Buffer.
bufferFlagsBufferFlagsThe buffer flags to specify the type of Buffer.
viewFormatPixelFormatView format used if the Buffer is used as a Shader Resource View, or None if not.
usageGraphicsResourceUsageThe usage for the Buffer, which determines who can read/write data.
Returns
Type Parameters
TThe type of the elements the Buffer will contain.
OnDestroyed(bool)
Called when the GraphicsDevice has been detected to be internally destroyed, or when the Destroy() methad has been called. Raises the Destroyed event.
protected override void OnDestroyed(bool immediately = false)
Parameters
immediatelyboolA value indicating whether the resource should be destroyed immediately (true), or if it can be deferred until it's safe to do so (false).
Remarks
This method releases the underlying native resources (Silk.NET.Direct3D11.ID3D11Resource and Silk.NET.Direct3D11.ID3D11DeviceChild).
OnNameChanged()
Called when the Name property has changed.
protected override void OnNameChanged()
OnRecreate()
Called when the GraphicsDevice has been recreated.
protected override bool OnRecreate()
Returns
Recreate(nint)
Recreates this buffer explicitly with the provided data. Usually called after the GraphicsDevice has been reset.
public void Recreate(nint dataPointer)
Parameters
dataPointernintThe data pointer to the data to use to recreate the buffer with. Specify Zero if no initial data is needed.
RecreateWith(nint)
Sets the Buffer to be recreated with the specified data whenever the GraphicsDevice it depends on is reset.
public Buffer RecreateWith(nint dataPointer)
Parameters
dataPointernintThe data pointer to the data to use to recreate the Buffer with.
Returns
- Buffer
This instance.
RecreateWith<T>(T[])
Sets the Buffer to be recreated with the specified data whenever the GraphicsDevice it depends on is reset.
public Buffer RecreateWith<T>(T[] data) where T : unmanaged
Parameters
dataT[]The data to use to recreate the Buffer with.
Returns
- Buffer
This instance.
Type Parameters
TThe type of the elements the Buffer will contain.
Recreate<T>(T[])
Recreates the Buffer explicitly with the provided data. Usually called after the GraphicsDevice has been reset.
public void Recreate<T>(T[] data) where T : unmanaged
Parameters
dataT[]The data to use to recreate the Buffer with.
Type Parameters
TThe type of the elements the Buffer will contain.
SetData(CommandList, DataPointer, int)
Copies data from a pointer to data on CPU memory into the Buffer in GPU memory.
[Obsolete("This method is obsolete. Use the Span-based methods instead")]
public void SetData(CommandList commandList, DataPointer fromData, int offsetInBytes = 0)
Parameters
commandListCommandListThe CommandList.
fromDataDataPointerThe pointer to the data to copy from.
offsetInBytesintThe offset in bytes from the start of the Buffer where data is to be written.
Remarks
See MapSubResource(GraphicsResource, int, MapMode, bool, int, int) and UpdateSubResource(GraphicsResource, int, DataBox, ResourceRegion) for more information about usage and restrictions.
Exceptions
- ArgumentException
offsetInBytesis only supported for Buffers declared with Default.
SetData<TData>(CommandList, ReadOnlySpan<TData>, int)
Copies data from a span of data on CPU memory into the Buffer in GPU memory.
public void SetData<TData>(CommandList commandList, ReadOnlySpan<TData> fromData, int offsetInBytes = 0) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
fromDataReadOnlySpan<TData>The span of data to copy from.
offsetInBytesintThe offset in bytes from the start of the Buffer where data is to be written.
Type Parameters
TDataThe type of the data to write into the Buffer.
Remarks
See MapSubResource(GraphicsResource, int, MapMode, bool, int, int) and UpdateSubResource(GraphicsResource, int, DataBox, ResourceRegion) for more information about usage and restrictions.
Exceptions
- ArgumentException
The length of
fromDatais larger than the size of the Buffer.- ArgumentException
offsetInBytesis only supported for Buffers declared with Default.
SetData<TData>(CommandList, ref readonly TData, int)
Copies the contents an array of data on CPU memory into the Buffer in GPU memory.
public void SetData<TData>(CommandList commandList, ref readonly TData fromData, int offsetInBytes = 0) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
fromDataTDataThe data to copy from.
offsetInBytesintThe offset in bytes to write to.
Type Parameters
TDataThe type of the data to write into the Buffer.
Remarks
See MapSubResource(GraphicsResource, int, MapMode, bool, int, int) and UpdateSubResource(GraphicsResource, int, DataBox, ResourceRegion) for more information about usage and restrictions.
Exceptions
- ArgumentException
offsetInBytesis only supported for Buffers declared with Default.
SetData<TData>(CommandList, TData[], int)
Copies the contents of an array of data on CPU memory into the Buffer in GPU memory.
public void SetData<TData>(CommandList commandList, TData[] fromData, int offsetInBytes = 0) where TData : unmanaged
Parameters
commandListCommandListThe CommandList.
fromDataTData[]The array of data to copy from.
offsetInBytesintThe offset in bytes from the start of the Buffer where data is to be written.
Type Parameters
TDataThe type of the data to write into the Buffer.
Remarks
See MapSubResource(GraphicsResource, int, MapMode, bool, int, int) and UpdateSubResource(GraphicsResource, int, DataBox, ResourceRegion) for more information about usage and restrictions.
Exceptions
- ArgumentException
offsetInBytesis only supported for Buffers declared with Default.
ToStaging()
Returns a staging Buffer that can be read / written by the CPU that is equivalent to the Buffer.
public Buffer ToStaging()
Returns
- Buffer
A new instance of the Buffer as a staging resource.