Class Buffer<T>
All-in-one GPU buffer that is able to represent many types of Buffers (shader Constant Buffers, Structured Buffers, Raw Buffers, Argument Buffers, etc.), but with typed information.
public class Buffer<T> : Buffer, IDisposable, IComponent, IReferencable, ICollectorHolder where T : unmanaged
Type Parameters
TThe type of the elements of the Buffer.
- Inheritance
-
objectBuffer<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
Buffer<T> constains static methods for creating new Buffers with typed information 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).
You can also check the methods of Buffer for creating Buffers with the maximum flexibility.
Consult the documentation of your graphics API for more information on each kind of Buffer.
Constructors
Buffer(GraphicsDevice, BufferDescription, BufferFlags, PixelFormat, nint, string?)
Initializes a new instance of typed Buffer<T>.
protected Buffer(GraphicsDevice device, BufferDescription description, BufferFlags bufferFlags, PixelFormat viewFormat, nint dataPointer, string? name = null)
Parameters
deviceGraphicsDeviceThe GraphicsDevice.
descriptionBufferDescriptionThe description of the Buffer's characteristics.
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.
dataPointernintThe data pointer to the initial data the Buffer will contain.
namestringA name for the Buffer, used for debugging purposes. Specify null to not set a name and use the name of the type instead.
Fields
ElementSize
The size of the elements in this Buffer<T> (i.e. the size of T).
public readonly int ElementSize
Field Value
Methods
GetData(CommandList)
Gets the contents of the Buffer as an array of data.
public T[] GetData(CommandList commandList)
Parameters
commandListCommandListThe CommandList.
Returns
- T[]
An array of data with the contents of 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.
SetData(CommandList, ref readonly T, int)
Copies the contents an array of data on CPU memory into the Buffer in GPU memory.
public void SetData(CommandList commandList, ref readonly T fromData, int offsetInBytes = 0)
Parameters
commandListCommandListThe CommandList.
fromDataTThe data to copy from.
offsetInBytesintThe offset in bytes to write to.
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(CommandList, T[], int)
Copies the contents of an array of data on CPU memory into the Buffer in GPU memory.
public void SetData(CommandList commandList, T[] fromData, int offsetInBytes = 0)
Parameters
commandListCommandListThe CommandList.
fromDataT[]The array of 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.