import { BufferData , gl , IBuffer , IBufferInitializationOptions , isSafari , } from '@antv/g-webgpu-core' ; import * as WebGPUConstants from '@webgpu/types/dist/constants' ; import { WebGPUEngine } from '.' ; export default class WebGPUBuffer implements IBuffer { private buffer : GPUBuffer ; constructor ( private engine , private options ) { const { data , usage , type } = options ; this . buffer = this . createBuffer ( data instanceof Array ? new Float32Array ( data ) : data , usage || WebGPUConstants . BufferUsage . Vertex | WebGPUConstants . BufferUsage . CopyDst ) ; } public get ( ) { return this . buffer ; } public destroy ( ) { this . buffer . destroy ( ) ; } public subData ( { data , offset } ) { this . setSubData ( this . buffer , offset , data instanceof Array ? new Float32Array ( data ) : data ) ; } private createBuffer ( view , flags ) { const padding = view . byteLength % 4 ; const verticesBufferDescriptor = { size : view . byteLength + padding , usage : flags , } ; const buffer = this . engine . device . createBuffer ( verticesBufferDescriptor ) ; this . setSubData ( buffer , 0 , view ) ; return buffer ; } see https see https see https private setSubData ( destBuffer , destOffset , srcArrayBuffer ) { const queue = isSafari ? this . engine . device . getQueue ( ) : this . engine . device . defaultQueue ; queue . writeBuffer ( destBuffer , destOffset , srcArrayBuffer ) ; } }