UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.54 kB
import{math as e}from"../../core/math/math.js";class i{constructor(e){this.device=void 0,this.device=e}}class s{constructor(){this.gpuBuffer=void 0,this.stagingBuffer=void 0,this.offset=void 0,this.size=void 0}}class t{constructor(){this.storage=void 0,this.gpuBuffer=void 0,this.offset=void 0}}class f{constructor(e,i,s){this.bufferSize=void 0,this.gpuBuffers=[],this.stagingBuffers=[],this.usedBuffers=[],this.activeBuffer=null,this.device=e,this.bufferSize=i,this.bufferAlignment=s}destroy(){this.gpuBuffers.forEach((e=>{e.destroy(this.device)})),this.gpuBuffers=null,this.stagingBuffers.forEach((e=>{e.destroy(this.device)})),this.stagingBuffers=null,this.usedBuffers=null,this.activeBuffer=null}alloc(i,t){if(this.activeBuffer){const i=e.roundUp(this.activeBuffer.size,this.bufferAlignment);this.bufferSize-i<t&&this.scheduleSubmit()}if(!this.activeBuffer){let e=this.gpuBuffers.pop();e||(e=this.createBuffer(this.device,this.bufferSize,!1));let i=this.stagingBuffers.pop();i||(i=this.createBuffer(this.device,this.bufferSize,!0)),this.activeBuffer=new s,this.activeBuffer.stagingBuffer=i,this.activeBuffer.gpuBuffer=e,this.activeBuffer.offset=0,this.activeBuffer.size=0}const f=this.activeBuffer,u=e.roundUp(f.size,this.bufferAlignment);i.gpuBuffer=f.gpuBuffer,i.offset=u,i.storage=f.stagingBuffer.alloc(u,t),f.size=u+t}scheduleSubmit(){this.activeBuffer&&(this.usedBuffers.push(this.activeBuffer),this.activeBuffer=null)}submit(){this.scheduleSubmit()}}export{i as DynamicBuffer,t as DynamicBufferAllocation,f as DynamicBuffers};