UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

44 lines (41 loc) 1.58 kB
import { BUFFERUSAGE_STORAGE } from './constants.js'; let id = 0; class StorageBuffer { constructor(graphicsDevice, byteSize, bufferUsage = 0, addStorageUsage = true){ this.id = id++; this.device = graphicsDevice; this.byteSize = byteSize; this.bufferUsage = bufferUsage; const usage = addStorageUsage ? BUFFERUSAGE_STORAGE | bufferUsage : bufferUsage; this.impl = graphicsDevice.createBufferImpl(usage); this.impl.allocate(graphicsDevice, byteSize); this.device.buffers.push(this); this.adjustVramSizeTracking(graphicsDevice._vram, this.byteSize); } destroy() { const device = this.device; const idx = device.buffers.indexOf(this); if (idx !== -1) { device.buffers.splice(idx, 1); } this.adjustVramSizeTracking(device._vram, -this.byteSize); this.impl.destroy(device); } adjustVramSizeTracking(vram, size) { vram.sb += size; } read(offset = 0, size = this.byteSize, data = null, immediate = false) { return this.impl.read(this.device, offset, size, data, immediate); } write(bufferOffset = 0, data, dataOffset = 0, size) { this.impl.write(this.device, bufferOffset, data, dataOffset, size); } clear(offset = 0, size = this.byteSize) { this.impl.clear(this.device, offset, size); } copy(srcBuffer, srcOffset = 0, dstOffset = 0, size = srcBuffer.byteSize - srcOffset) { const commandEncoder = this.device.getCommandEncoder(); commandEncoder.copyBufferToBuffer(srcBuffer.impl.buffer, srcOffset, this.impl.buffer, dstOffset, size); } } export { StorageBuffer };