files/en-us/web/api/gpubuffer/index.md
{{APIRef("WebGPU API")}}{{SecureContext_Header}}{{AvailableInWorkers}}
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.
A GPUBuffer object instance is created using the {{domxref("GPUDevice.createBuffer()")}} method.
{{InheritanceDiagram}}
GPUBuffer.GPUBuffer's memory allocation, in bytes.GPUBuffer.GPUBuffer.GPUBuffer in the specified range.GPUBuffer. Returns a {{jsxref("Promise")}} that resolves when the GPUBuffer's content is ready to be accessed with {{domxref("GPUBuffer.getMappedRange()")}}.GPUBuffer, making its contents available for use by the GPU again.In our basic compute demo, we create an output buffer to read GPU calculations to, and a staging buffer to be mapped for JavaScript access.
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,
});
Later on, once the stagingBuffer contains the results of the GPU computation, a combination of GPUBuffer methods are used to read the data back to JavaScript so that it can then be logged to the console:
GPUBuffer for reading.GPUBuffer's contents.GPUBuffer again, once we have read the content into JavaScript as needed.// map staging buffer to read results back to JS
await stagingBuffer.mapAsync(
GPUMapMode.READ,
0, // Offset
BUFFER_SIZE, // Length
);
const copyArrayBuffer = stagingBuffer.getMappedRange(0, BUFFER_SIZE);
const data = copyArrayBuffer.slice(0);
stagingBuffer.unmap();
console.log(new Float32Array(data));
{{Specifications}}
{{Compat}}