Table of Contents

Class GPUBuffer

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The GPUBuffer interface of the {{domxref("WebGPU API", "WebGPU API", "", "nocode")}} represents a block of memory that can be used to store raw data to use in GPU operations.

[Value("GPUBuffer")]
public class GPUBuffer
Inheritance
GPUBuffer
Inherited Members

Remarks

A GPUBuffer object instance is created using the CreateBuffer(GPUBufferDescriptor) method.

-The WebGPU API

See also on MDN

Constructors

GPUBuffer()

public GPUBuffer()

Properties

MapState

The mapState read-only property of the
GPUBuffer interface represents the mapped state of the GPUBuffer.

[Value("mapState")]
public GPUBufferMapState MapState { get; }

Property Value

GPUBufferMapState

An enumerated value. Possible values are:

Remarks

Size

The size read-only property of the
GPUBuffer interface represents the length of the GPUBuffer's memory allocation, in bytes.

[Value("size")]
public ulong Size { get; }

Property Value

ulong

A number.

Remarks

size is set via the size property in the descriptor object passed into the originating CreateBuffer(GPUBufferDescriptor) call.

-The WebGPU API

See also on MDN

Usage

The usage read-only property of the
GPUBuffer interface contains the {{glossary("bitwise flags")}} representing the allowed usages of the GPUBuffer.

[Value("usage")]
public ulong Usage { get; }

Property Value

ulong

The bitwise flags representing the original usages set when the GPUBuffer was first created. The returned number is the sum of decimal values representing the different flags, as seen in the table below.

Bitwise flagUsage descriptionHex equiv.Decimal equiv.
GPUBufferUsage.COPY_SRCThe buffer can be used as the source of a copy operation, for example the source argument of a CopyBufferToBuffer(GPUBuffer, ulong, GPUBuffer, ulong, ulong) call.0x00044
GPUBufferUsage.COPY_DSTThe buffer can be used as the destination of a copy/write operation, for example the destination argument of a CopyTextureToBuffer(GPUTexelCopyTextureInfo, GPUTexelCopyBufferInfo, Union222) call.0x00088
GPUBufferUsage.INDEXThe buffer can be used as an index buffer, for example as the buffer argument passed to GPURenderPassEncoder.SetIndexBuffer.0x001016
GPUBufferUsage.INDIRECTThe buffer can be used to store indirect command arguments, for example as the indirectBuffer argument of a GPURenderPassEncoder.DrawIndirect or DispatchWorkgroupsIndirect(GPUBuffer, ulong) call.0x0100256
GPUBufferUsage.MAP_READThe buffer can be mapped for reading, for example when calling MapAsync(ulong, ulong, ulong) with a mode of GPUMapMode.READ. This flag may only be combined with GPUBufferUsage.COPY_DST.0x00011
GPUBufferUsage.MAP_WRITEThe buffer can be mapped for writing, for example when calling MapAsync(ulong, ulong, ulong) with a mode of GPUMapMode.WRITE. This flag may only be combined with GPUBufferUsage.COPY_SRC.0x00022
GPUBufferUsage.QUERY_RESOLVEThe buffer can be used to capture query results, for example as the destination argument of a ResolveQuerySet(GPUQuerySet, ulong, ulong, GPUBuffer, ulong) call.0x0200512
GPUBufferUsage.STORAGEThe buffer can be used as a storage buffer, for example as a resource in a bind group entry when creating a GPUBindGroup (via CreateBindGroup(GPUBindGroupDescriptor)), which adheres to a GPUBindGroupLayout entry with a buffer binding layout type of "storage" or "read-only-storage".0x0080128
GPUBufferUsage.UNIFORMThe buffer can be used as a uniform buffer, for example as a resource in a bind group entry when creating a GPUBindGroup (via CreateBindGroup(GPUBindGroupDescriptor)), which adheres to a GPUBindGroupLayout entry with a buffer binding layout type of "uniform".0x004064
GPUBufferUsage.VERTEXThe buffer can be used as a vertex buffer, for example as the buffer argument passed to GPURenderPassEncoder.SetVertexBuffer.0x002032

Remarks

usage is set via the usage property in the descriptor object passed into the originating CreateBuffer(GPUBufferDescriptor) call.

-The WebGPU API

See also on MDN

Methods

Destroy()

The destroy() method of the
GPUBuffer interface destroys the GPUBuffer.

[Value("destroy")]
public GlobalObject.Undefined Destroy()

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

GetMappedRange(ulong, ulong)

The getMappedRange() method of the
GPUBuffer interface returns an {{jsxref("ArrayBuffer")}} containing the mapped contents of the GPUBuffer in the specified range.

[Value("getMappedRange")]
public ArrayBuffer GetMappedRange(ulong offset = 0, ulong size = 0)

Parameters

offset ulong
size ulong

Returns

ArrayBuffer

An ArrayBuffer.

Remarks

This can only happen once the GPUBuffer has been successfully mapped with MapAsync(ulong, ulong, ulong) (this can be checked via MapState). While the GPUBuffer is mapped it cannot be used in any GPU commands.

When you have finished working with the GPUBuffer values, call Unmap() to unmap it, making it accessible to the GPU again. A TypeError is thrown if an attempt is made to detach the {{jsxref("ArrayBuffer")}} in any way other than via Unmap(), such as by calling {{jsxref("ArrayBuffer/transfer", "transfer()")}}.

-The WebGPU API

See also on MDN

MapAsync(ulong, ulong, ulong)

The mapAsync() method of the
GPUBuffer interface maps the specified range of the GPUBuffer. It returns a Promise that resolves when the GPUBuffer's content is ready to be accessed. While the GPUBuffer is mapped it cannot be used in any GPU commands.

[Value("mapAsync")]
public Task<GlobalObject.Undefined> MapAsync(ulong mode, ulong offset = 0, ulong size = 0)

Parameters

mode ulong
offset ulong
size ulong

Returns

Task<GlobalObject.Undefined>

A Promise that resolves to GlobalObject.Undefined when the GPUBuffer&apos;s content is ready to be accessed.

Remarks

Once the buffer is successfully mapped (which can be checked via MapState), calls to GetMappedRange(ulong, ulong) will return an {{jsxref("ArrayBuffer")}} containing the GPUBuffer&apos;s current values, to be read and updated by JavaScript as required.

When you have finished working with the GPUBuffer values, call Unmap() to unmap it, making it accessible to the GPU again.

-The WebGPU API

See also on MDN

Unmap()

The unmap() method of the
GPUBuffer interface unmaps the mapped range of the GPUBuffer, making its contents available for use by the GPU again after it has previously been mapped with MapAsync(ulong, ulong, ulong) (the GPU cannot access a mapped GPUBuffer).

[Value("unmap")]
public GlobalObject.Undefined Unmap()

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

When unmap() is called, any {{jsxref("ArrayBuffer")}}s created via GetMappedRange(ulong, ulong) are detached.

-The WebGPU API

See also on MDN