大网数据广告活动
Web 标准与前端开发

GPUQueue: writeBuffer() method

· 52CE 我爱测

The **`writeBuffer()`** method of the GPUQueue interface writes a provided data source into a given GPUBuffer.

WebGPU API

The writeBuffer() method of the GPUQueue interface writes a provided data source into a given GPUBuffer.

This is a convenience function, which provides an alternative to setting buffer data via buffer mapping and buffer-to-buffer copies. It lets the user agent determine the most efficient way to copy the data over.

Syntax

writeBuffer(buffer, bufferOffset, data, dataOffset, size)

Parameters

  • buffer
    • : A GPUBuffer object representing the buffer to write data to.
  • bufferOffset
    • : A number representing the offset, in bytes, to start writing the data at inside the GPUBuffer.
  • data
    • : An object representing the data source to write into the GPUBuffer. This can be an ArrayBuffer, TypedArray, or DataView.
  • dataOffset
    • : A number representing the offset to start writing the data from inside the data source. This value is a number of elements if data is a TypedArray, and a number of bytes if not. If omitted, dataOffset defaults to 0.
  • size
    • : A number representing the size of the content to write from data to buffer. This value is a number of elements if data is a TypedArray, and a number of bytes if not. If omitted, size will be equal to the overall size of data, minus dataOffset.

Return value

None (undefined).

Exceptions

  • OperationError DOMException
    • : The method throws an OperationError if the following criteria are not met:
      • The size of data is equal to or greater than 0.
      • dataOffset is equal to or smaller than the size of data.
      • The size of data (when converted to bytes, in the case of TypedArrays) is a multiple of 4.

Validation

The following criteria must be met when calling writeBuffer(), otherwise a GPUValidationError is generated and the GPUQueue becomes invalid:

  • buffer is available for use, i.e., not unavailable (GPUBuffers are unavailable if they are currently GPUBuffer.mapAsync) or destroyed (with the GPUBuffer.destroy() method).
  • The buffer's GPUBuffer.usage includes the GPUBufferUsage.COPY_DST flag.
  • bufferOffset, when converted to bytes, is a multiple of 4.
  • The size of data - dataOffset + bufferOffset, when converted to bytes, is equal to or less than the buffer's GPUBuffer.size.

Examples

In our basic render demo, we define some vertex data in a Float32Array that we'll use to draw a triangle:

const vertices = new Float32Array([
  0.0, 0.6, 0, 1, 1, 0, 0, 1, -0.5, -0.6, 0, 1, 0, 1, 0, 1, 0.5, -0.6, 0, 1, 0,
  0, 1, 1,
]);

To use this data in a render pipeline, we need to put it into a GPUBuffer. First we'll create the buffer:

const vertexBuffer = device.createBuffer({
  size: vertices.byteLength, // make it big enough to store vertices in
  usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});

To get the data into the buffer we can use writeBuffer():

device.queue.writeBuffer(vertexBuffer, 0, vertices, 0, vertices.length);

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。