forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryCache.swift
More file actions
52 lines (45 loc) · 1.18 KB
/
GeometryCache.swift
File metadata and controls
52 lines (45 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// GeometryCache.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 06/08/2021.
// Copyright © 2021 Nick Lockwood. All rights reserved.
//
import Euclid
#if canImport(LRUCache)
import LRUCache
#endif
public final class GeometryCache {
private let cache: LRUCache<Key, Mesh>
public init(memoryLimit: Int = 1_000_000_000) {
cache = LRUCache(totalCostLimit: memoryLimit)
}
}
extension GeometryCache {
struct Key: Hashable {
let type: GeometryType
let material: Material?
let smoothing: Angle?
let transform: Transform
let children: [Key]
}
subscript(geometry: Geometry) -> Mesh? {
get { cache.value(forKey: geometry.cacheKey) }
set {
cache.setValue(
newValue,
forKey: geometry.cacheKey,
cost: newValue?.memoryUsage ?? 0
)
}
}
}
private extension Mesh {
var memoryUsage: Int {
let vertexSize = MemoryLayout<Vertex>.stride
let polygonSize = 512 // Estimated
return polygons.reduce(0) { count, polygon in
count + polygonSize + polygon.vertices.count * vertexSize
}
}
}