Skip to content

Commit 57d7ad8

Browse files
mvaligurskyMartin Valigursky
andauthored
Extracted lightmap cache functionality from meshInstance to RefCountedCache (playcanvas#3154)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 8539e75 commit 57d7ad8

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/core/ref-counted-cache.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Class implementing reference counting cache for objects
2+
class RefCountedCache {
3+
constructor() {
4+
// The cache. The key is the object being stored in the cache.
5+
// The value is ref count of the object. When that reaches zero,
6+
// destroy function on the object gets called and object is removed
7+
// from the cache.
8+
this.cache = new Map();
9+
}
10+
11+
// add object reference to the cache
12+
incRef(object) {
13+
const refCount = (this.cache.get(object) || 0) + 1;
14+
this.cache.set(object, refCount);
15+
}
16+
17+
// remove object reference from the cache
18+
decRef(object) {
19+
if (object) {
20+
let refCount = this.cache.get(object);
21+
if (refCount) {
22+
refCount--;
23+
if (refCount === 0) {
24+
// destroy object and remove it from cache
25+
this.cache.delete(object);
26+
object.destroy();
27+
} else {
28+
// update new ref count in the cache
29+
this.cache.set(object, refCount);
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
export { RefCountedCache };

src/scene/mesh-instance.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RefCountedCache } from '../core/ref-counted-cache.js';
12
import { BoundingBox } from '../shape/bounding-box.js';
23
import { BoundingSphere } from '../shape/bounding-sphere.js';
34

@@ -168,33 +169,17 @@ class MeshInstance {
168169
static lightmapParamNames = ["texture_lightMap", "texture_dirLightMap"];
169170

170171
// cache of lightmaps internally created by baking using Lightmapper
171-
// the key is the lightmap (texture), the value is reference count .. when it reaches 0, the lightmap gets destroyed.
172172
// this allows us to automatically release realtime baked lightmaps when mesh instances using them are destroyed
173-
static _lightmapCache = new Map();
173+
static _lightmapCache = new RefCountedCache();
174174

175175
// add texture reference to lightmap cache
176176
static incRefLightmap(texture) {
177-
let refCount = MeshInstance._lightmapCache.get(texture) || 0;
178-
refCount++;
179-
MeshInstance._lightmapCache.set(texture, refCount);
177+
this._lightmapCache.incRef(texture);
180178
}
181179

182180
// remove texture reference from lightmap cache
183181
static decRefLightmap(texture) {
184-
if (texture) {
185-
let refCount = MeshInstance._lightmapCache.get(texture);
186-
if (refCount) {
187-
refCount--;
188-
if (refCount === 0) {
189-
// destroy texture and remove it from cache
190-
MeshInstance._lightmapCache.delete(texture);
191-
texture.destroy();
192-
} else {
193-
// update new ref count in the cache
194-
MeshInstance._lightmapCache.set(texture, refCount);
195-
}
196-
}
197-
}
182+
this._lightmapCache.decRef(texture);
198183
}
199184

200185
get renderStyle() {

0 commit comments

Comments
 (0)