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
75 lines (66 loc) · 2.05 KB
/
GeometryCache.swift
File metadata and controls
75 lines (66 loc) · 2.05 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// 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: Mesh, associatedData: [Material: Any])>
/// The number of entries currently stored in the cache
public var count: Int { cache.count }
/// Initialize the cache with a given storage limit
/// - Parameter memoryLimit: The maximum amount of data to cache (in bytes)
public init(memoryLimit: Int = 1_000_000_000) {
self.cache = LRUCache(totalCostLimit: memoryLimit)
}
}
extension GeometryCache {
struct Key: Hashable {
let type: GeometryType
let material: Material?
let smoothing: Angle?
let transform: Transform
let flipped: Bool
let children: [Key]
}
subscript(mesh geometry: Geometry) -> Mesh? {
get { cache.value(forKey: geometry.cacheKey)?.mesh }
set {
guard let newValue else {
cache.removeValue(forKey: geometry.cacheKey)
return
}
cache.setValue(
(newValue, [:]),
forKey: geometry.cacheKey,
cost: newValue.memoryUsage
)
}
}
subscript(associatedData geometry: Geometry) -> Any? {
get {
cache.value(forKey: geometry.cacheKey)?
.associatedData[geometry.material]
}
set {
if var value = cache.value(forKey: geometry.cacheKey) {
value.associatedData[geometry.material] = newValue
cache.setValue(value, forKey: geometry.cacheKey)
}
}
}
}
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
}
}
}