WebGPU Example GPU Buffer creation taking data to from cpu to gpu data is written using writeBuffer taken back to CPU using mapping Note you could use mapping to read write the data but it s to show the different flags features more than one way to const adapter await navigator gpu requestAdapter const device await adapter requestDevice let gpuDst device createBuffer size 4 bytes usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST GPUBufferUsage COPY_SRC GPUBufferUsage MAP_READ let cpuSrc new Uint8Array 4 ArrayBuffer cpuSrc set 1 2 3 4 console log cpuSrc byteLength cpuSrc byteLength console log cpuSrc 0 cpuSrc 0 device queue writeBuffer gpuDst dst buffer 0 dst offset cpuSrc src buffer 0 src offset cpuSrc byteLength multiple of 4 bytes device queue submit get back use mapping note the flag MAP_READ await gpuDst mapAsync 0x0001 mode 0 offset 4 size let gmap gpuDst getMappedRange 0 offset 4 size copy the contents let cpuArr new Uint8Array gmap var origData Array from cpuArr gpuDst unmap console log data from GPU origData console log done
0 device queue writeBuffer gpuDst dst buffer 0 dst offset cpuSrc src buffer 0 src offset cpuSrc byteLength multiple of 4 bytes device queue submit get back use mapping note the flag MAP_READ await gpuDst mapAsync 0x0001 mode 0 offset 4 size let gmap gpuDst getMappedRange 0 offset 4 size copy the contents let cpuArr new Uint8Array gmap var origData Array from cpuArr gpuDst unmap console log data from GPU origData console log done