cables_dev/cables/src/core/cgp/cgp_gpubuffer.js
import { EventTarget } from "../eventtarget.js";
export default class GPUBuffer extends EventTarget
{
constructor(cgp, name, data = null, options = {})
{
super();
this.id = CABLES.shortId();
this._name = name;
this.floatArr = null;
this._gpuBuffer = null;
this.setData([0, 0, 0, 0]);
this.needsUpdate = true;
this._length = 0;
if (options.buffCfg)
{
this._buffCfg = options.buffCfg;
}
if (data)
this.setData(data);
if (options.length) this.setLength(options.length);
this.updateGpuBuffer(cgp);
}
setData(d)
{
// console.log((new Error()).stack);
this.floatArr = new Float32Array(d);
this.setLength(this.floatArr.length);
// console.log(this.name, this.floatArr);
this.needsUpdate = true;
}
setLength(s)
{
this._length = s;
if (!this.floatArr || s != this.floatArr.length)
{
this.floatArr = new Float32Array(this._length);
this.needsUpdate = true;
}
}
updateGpuBuffer(cgp)
{
if (cgp) this._cgp = cgp;
if (!this._cgp || !this._cgp.device)
{
console.log("no cgp...", this._name, this._cgp);
return;
}
this._cgp.pushErrorScope("updateGpuBuffer");
if (!this._gpuBuffer)
{
this._buffCfg = this._buffCfg || {};
this._buffCfg.label = "gpuBuffer-" + this._name;
if (!this._buffCfg.hasOwnProperty("size") && this.floatArr) this._buffCfg.size = this.floatArr.length * 4;
this._buffCfg.usage = this._buffCfg.usage || (GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC);
this._gpuBuffer = this._cgp.device.createBuffer(this._buffCfg);
}
if (this.floatArr)
this._cgp.device.queue.writeBuffer(
this._gpuBuffer,
0,
this.floatArr.buffer,
this.floatArr.byteOffset,
this.floatArr.byteLength
);
// this._gpuBuffer.unmap();
this._cgp.popErrorScope();
this.needsUpdate = false;
}
get gpuBuffer()
{
if (!this._gpuBuffer || this.needsUpdate) this.updateGpuBuffer();
return this._gpuBuffer;
}
get length()
{
return this._length;
}
getSizeBytes()
{
return this.floatArr.length * 4;
}
dispose()
{
// setTimeout(() =>
// {
// if (this._gpuBuffer) this._gpuBuffer.destroy();
// }, 100);
}
}