Skip to content

Commit

Permalink
Use DeviceCache to store shape primitives in (#6891)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Aug 22, 2024
1 parent 2dd5a6a commit a14023a
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/framework/graphics/primitive-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@ import { Mesh } from '../../scene/mesh.js';
import { BoxGeometry } from '../../scene/geometry/box-geometry.js';
import { SphereGeometry } from '../../scene/geometry/sphere-geometry.js';
import { PlaneGeometry } from '../../scene/geometry/plane-geometry.js';
import { DeviceCache } from '../../platform/graphics/device-cache.js';

// cached mesh primitives
const shapePrimitives = [];
// class used to hold primitives in the device cache
class PrimitivesCache {
map = new Map();

// destroy all created primitives when the device is destroyed
destroy(device) {
this.map.forEach(primData => primData.mesh.destroy());
}
}

const _primitivesCache = new DeviceCache();

// returns Primitive data, used by ModelComponent and RenderComponent
function getShapePrimitive(device, type) {
const getShapePrimitive = (device, type) => {

// find in cache
let primData = null;
for (let i = 0; i < shapePrimitives.length; i++) {
if (shapePrimitives[i].type === type && shapePrimitives[i].device === device) {
primData = shapePrimitives[i].primData;
}
}
// cache for the device
const cache = _primitivesCache.get(device, () => {
return new PrimitivesCache();
});

let primData = cache.map.get(type);

// not in cache, create new
if (!primData) {
Expand Down Expand Up @@ -72,15 +81,11 @@ function getShapePrimitive(device, type) {

primData = { mesh: mesh, area: area };

// add to cache
shapePrimitives.push({
type: type,
device: device,
primData: primData
});
cache.map.set(type, primData);

}

return primData;
}
};

export { getShapePrimitive };

0 comments on commit a14023a

Please sign in to comment.