(
width: number, height: number, format: GPUTextureFormat,
usage: GPUTextureUsageFlags)
| 27 | constructor(private device: GPUDevice) {} |
| 28 | |
| 29 | acquireTexture( |
| 30 | width: number, height: number, format: GPUTextureFormat, |
| 31 | usage: GPUTextureUsageFlags) { |
| 32 | const bytesPerElement = getBytesPerElement(format); |
| 33 | const byteSize = width * height * bytesPerElement; |
| 34 | const key = getTextureKey(width, height, format, usage); |
| 35 | if (!this.freeTextures.has(key)) { |
| 36 | this.freeTextures.set(key, []); |
| 37 | } |
| 38 | |
| 39 | if (!this.usedTextures.has(key)) { |
| 40 | this.usedTextures.set(key, []); |
| 41 | } |
| 42 | |
| 43 | this.numBytesUsed += byteSize; |
| 44 | this.numUsedTextures++; |
| 45 | |
| 46 | if (this.freeTextures.get(key).length > 0) { |
| 47 | this.numFreeTextures--; |
| 48 | |
| 49 | const newTexture = this.freeTextures.get(key).shift(); |
| 50 | this.usedTextures.get(key).push(newTexture); |
| 51 | return newTexture; |
| 52 | } |
| 53 | |
| 54 | this.numBytesAllocated += byteSize; |
| 55 | |
| 56 | const newTexture = this.device.createTexture({ |
| 57 | size: [width, height], |
| 58 | format, |
| 59 | usage, |
| 60 | }); |
| 61 | this.usedTextures.get(key).push(newTexture); |
| 62 | |
| 63 | return newTexture; |
| 64 | } |
| 65 | |
| 66 | releaseTexture(texture: GPUTexture) { |
| 67 | if (this.freeTextures.size === 0) { |
no test coverage detected