Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. GPUDevice
  4. createBuffer()

GPUDevice: createBuffer() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Note: This feature is available inWeb Workers.

ThecreateBuffer() method of theGPUDevice interface creates aGPUBuffer in which to store raw data to use in GPU operations.

Syntax

js
createBuffer(descriptor)

Parameters

descriptor

An object containing the following properties:

labelOptional

A string providing a label that can be used to identify the object, for example inGPUError messages or console warnings.

mappedAtCreationOptional

A boolean. If set totrue, the buffer will be mapped upon creation, meaning that you can set the values inside the buffer immediately by callingGPUBuffer.getMappedRange(). The default value isfalse.

Note that it is valid to setmappedAtCreation: true so you can set the buffer's initial data, even if theGPUBufferUsage.MAP_READ orGPUBufferUsage.MAP_WRITE usage flags are not set.

size

A number representing the size of the buffer, in bytes. IfmappedAtCreation is set totrue, this must be a multiple of4.

usage

Thebitwise flags representing the allowed usages for theGPUBuffer. The possible values are in theGPUBuffer.usage value table.

Note that multiple possible usages can be specified by separating values withbitwise OR, for example:GPUBufferUsage.COPY_SRC | GPUBufferUsage.MAP_WRITE.

Return value

AGPUBuffer object instance.

Exceptions

RangeErrorDOMException

Thrown ifmappedAtCreation is set totrue, and the specifiedsize is not a multiple of4.

Validation

The following criteria must be met when callingcreateBuffer(), otherwise aGPUValidationError is generated and an invalidGPUBuffer object is returned:

  • A validusage is specified.
  • GPUBufferUsage.MAP_READ is specified, and no additional flags are specified other thanGPUBufferUsage.COPY_DST.
  • GPUBufferUsage.MAP_WRITE is specified, and no additional flags are specified other thanGPUBufferUsage.COPY_SRC.

Note:If the buffer allocation fails without any specific side-effects, aGPUOutOfMemoryError object is generated.

Examples

In ourbasic compute demo, we create an output buffer to read GPU calculations to, and a staging buffer to be mapped for JavaScript access.

js
const output = device.createBuffer({  size: BUFFER_SIZE,  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,});const stagingBuffer = device.createBuffer({  size: BUFFER_SIZE,  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,});

Specifications

Specification
WebGPU
# dom-gpudevice-createbuffer

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp