* Get a uniform buffer from the per-dispatch pool. * * Each dispatch in a batched encoder needs its own uniform buffer because * queue.writeBuffer() executes immediately while compute passes are deferred. * Reusing a shared buffer would overwrite data before earlier dispatches * consu
(nbytes: number)
| 620 | * @returns A GPUBuffer with UNIFORM | COPY_DST usage, at least nbytes large. |
| 621 | */ |
| 622 | private getUniformFromPool(nbytes: number): GPUBuffer { |
| 623 | const dispatchIdx = this.pendingDispatchCount++; |
| 624 | if (dispatchIdx < this.uniformBufferPool.length && |
| 625 | this.uniformBufferPoolSizes[dispatchIdx] >= nbytes) { |
| 626 | return this.uniformBufferPool[dispatchIdx]; |
| 627 | } |
| 628 | // Destroy old undersized buffer if it exists. |
| 629 | if (dispatchIdx < this.uniformBufferPool.length) { |
| 630 | this.uniformBufferPool[dispatchIdx].destroy(); |
| 631 | } |
| 632 | const buffer = this.device.createBuffer({ |
| 633 | size: nbytes, |
| 634 | usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
| 635 | }); |
| 636 | this.uniformBufferPool[dispatchIdx] = buffer; |
| 637 | this.uniformBufferPoolSizes[dispatchIdx] = nbytes; |
| 638 | return buffer; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Internal impl of createShader for both async and sync mode. |