|
| 1 | +export class GeometryBufferCache { |
| 2 | + constructor(renderer) { |
| 3 | + this.renderer = renderer; |
| 4 | + this.cache = {}; |
| 5 | + } |
| 6 | + |
| 7 | + numCached() { |
| 8 | + return Object.keys(this.cache).length; |
| 9 | + } |
| 10 | + |
| 11 | + isCached(gid) { |
| 12 | + return this.cache[gid] !== undefined; |
| 13 | + } |
| 14 | + |
| 15 | + getGeometryByID(gid) { |
| 16 | + return this.cache[gid]?.geometry; |
| 17 | + } |
| 18 | + |
| 19 | + getCached(model) { |
| 20 | + return this.getCachedID(model.gid); |
| 21 | + } |
| 22 | + |
| 23 | + getCachedID(gid) { |
| 24 | + return this.cache[gid]; |
| 25 | + } |
| 26 | + |
| 27 | + ensureCached(geometry) { |
| 28 | + const gid = geometry.gid; |
| 29 | + if (!gid) { |
| 30 | + throw new Error('The p5.Geometry you passed in has no gid property!'); |
| 31 | + } |
| 32 | + |
| 33 | + if (this.isCached(geometry.gid)) return this.getCached(geometry); |
| 34 | + |
| 35 | + const gl = this.renderer.GL; |
| 36 | + |
| 37 | + //initialize the gl buffers for our geom groups |
| 38 | + this.freeBuffers(gid); |
| 39 | + |
| 40 | + if (Object.keys(this.cache).length > 1000) { |
| 41 | + const key = Object.keys(this.cache)[0]; |
| 42 | + this.freeBuffers(key); |
| 43 | + } |
| 44 | + |
| 45 | + //create a new entry in our cache |
| 46 | + const buffers = {}; |
| 47 | + this.cache[gid] = buffers; |
| 48 | + |
| 49 | + buffers.geometry = geometry; |
| 50 | + |
| 51 | + let indexBuffer = buffers.indexBuffer; |
| 52 | + |
| 53 | + if (geometry.faces.length) { |
| 54 | + // allocate space for faces |
| 55 | + if (!indexBuffer) indexBuffer = buffers.indexBuffer = gl.createBuffer(); |
| 56 | + const vals = geometry.faces.flat(); |
| 57 | + |
| 58 | + // If any face references a vertex with an index greater than the maximum |
| 59 | + // un-singed 16 bit integer, then we need to use a Uint32Array instead of a |
| 60 | + // Uint16Array |
| 61 | + const hasVertexIndicesOverMaxUInt16 = vals.some(v => v > 65535); |
| 62 | + let type = hasVertexIndicesOverMaxUInt16 ? Uint32Array : Uint16Array; |
| 63 | + this.renderer._bindBuffer(indexBuffer, gl.ELEMENT_ARRAY_BUFFER, vals, type); |
| 64 | + |
| 65 | + // If we're using a Uint32Array for our indexBuffer we will need to pass a |
| 66 | + // different enum value to WebGL draw triangles. This happens in |
| 67 | + // the _drawElements function. |
| 68 | + buffers.indexBufferType = hasVertexIndicesOverMaxUInt16 |
| 69 | + ? gl.UNSIGNED_INT |
| 70 | + : gl.UNSIGNED_SHORT; |
| 71 | + } else { |
| 72 | + // the index buffer is unused, remove it |
| 73 | + if (indexBuffer) { |
| 74 | + gl.deleteBuffer(indexBuffer); |
| 75 | + buffers.indexBuffer = null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return buffers; |
| 80 | + } |
| 81 | + |
| 82 | + freeBuffers(gid) { |
| 83 | + const buffers = this.cache[gid]; |
| 84 | + if (!buffers) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + delete this.cache[gid]; |
| 89 | + |
| 90 | + const gl = this.renderer.GL; |
| 91 | + if (buffers.indexBuffer) { |
| 92 | + gl.deleteBuffer(buffers.indexBuffer); |
| 93 | + } |
| 94 | + |
| 95 | + function freeBuffers(defs) { |
| 96 | + for (const def of defs) { |
| 97 | + if (buffers[def.dst]) { |
| 98 | + gl.deleteBuffer(buffers[def.dst]); |
| 99 | + buffers[def.dst] = null; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // free all the buffers |
| 105 | + freeBuffers(this.renderer.buffers.stroke); |
| 106 | + freeBuffers(this.renderer.buffers.fill); |
| 107 | + freeBuffers(this.renderer.buffers.user); |
| 108 | + } |
| 109 | +} |
0 commit comments