-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathRenderer.swift
More file actions
193 lines (162 loc) · 5.72 KB
/
Copy pathRenderer.swift
File metadata and controls
193 lines (162 loc) · 5.72 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright © 2026 Apple Inc.
import Foundation
import MLX
import QuartzCore
private let iterationsPerTick = 200
enum RendererKind: String, CaseIterable, Identifiable {
case conv2d = "Conv2D"
case roll = "Roll"
case sor = "SOR"
case sorFullSpeed = "SOR Full Speed"
var id: String { rawValue }
var iterations: Int {
switch self {
case .conv2d, .roll: 200
case .sor: 1
case .sorFullSpeed: 200
}
}
func apply(room: inout Room) {
switch self {
case .conv2d:
computeJacobiConv2d(state: &room, count: self.iterations)
case .roll:
computeJacobiStencil(state: &room, count: self.iterations)
case .sor, .sorFullSpeed:
computeSOR(state: &room, count: self.iterations)
}
}
}
@MainActor @Observable
class Renderer {
public var configuration: Configuration
private var room: Room?
public var kind: RendererKind = .conv2d {
didSet {
if kind != oldValue {
recentTimes.removeAll(keepingCapacity: true)
averageFrameTime = nil
lastDisplayUpdate = 0
}
}
}
public var image: IOSurface?
private var animationTimer: Timer?
public private(set) var isAnimating = false
private var renderTask: Task<Void, Never>?
/// smoothed average render time per iteration. Note: each visible frame may be
/// several iterations. See ``RendererKind/iterations``.
public private(set) var averageFrameTime: TimeInterval?
/// FPS as reciprocal of the frame time -- this is the maximum rate it could run.
public var averageFPS: Double? {
guard let t = averageFrameTime, t > 0 else { return nil }
return 1.0 / t
}
@ObservationIgnored
private var recentTimes: [TimeInterval] = []
private let recentTimesCapacity = 30
@ObservationIgnored
private var lastDisplayUpdate: CFTimeInterval = 0
private let displayUpdateInterval: CFTimeInterval = 0.5
init() {
var c = Configuration()
for _ in 0 ..< Int.random(in: 6 ... 12) {
c.addRandomWall()
}
for _ in 0 ..< Int.random(in: 8 ... 12) {
c.addRandomHeatSource()
}
self.configuration = c
}
public func start() {
let room = self.room ?? configuration.asRoom()
renderTask = Task.detached { [self] in
let wallColor = full(room.temperature.shape, values: UInt32(0xff80_8080))
.view(dtype: .uint8)
.reshaped(room.temperature.shape + [-1])
var raster = applyLUT(
room.temperature, lut: MLXArray(lut), max: 1.0, maxValue: 0xffff_ffff)
raster = which(room.wallMask.expandedDimensions(axis: -1), wallColor, raster)
let image = createIOSurface(bgra: raster)
await MainActor.run {
self.image = image
self.room = room
self.renderTask = nil
}
}
}
public func rebuildRoom() {
var c = Configuration()
for _ in 0 ..< Int.random(in: 6 ... 12) {
c.addRandomWall()
}
for _ in 0 ..< Int.random(in: 8 ... 12) {
c.addRandomHeatSource()
}
self.configuration = c
self.room = nil
self.recentTimes.removeAll(keepingCapacity: true)
self.averageFrameTime = nil
self.lastDisplayUpdate = 0
start()
}
public func render() {
var room = self.room ?? configuration.asRoom()
renderTask = Task.detached { [self, kind] in
// compute
let start = Date.timeIntervalSinceReferenceDate
// important: evaluate the result, otherwise
// the time for the computation lands below when
// we produce an image from it.
kind.apply(room: &room)
eval(room.temperature)
let end = Date.timeIntervalSinceReferenceDate
let timePerIteration = (end - start) / Double(kind.iterations)
// render -- mix the room temperature with the walls
let wallColor = full(room.temperature.shape, values: UInt32(0xff80_8080))
.view(dtype: .uint8)
.reshaped(room.temperature.shape + [-1])
var raster = applyLUT(
room.temperature, lut: MLXArray(lut), max: 1.0, maxValue: 0xffff_ffff)
raster = which(room.wallMask.expandedDimensions(axis: -1), wallColor, raster)
let image = createIOSurface(bgra: raster)
await MainActor.run {
self.image = image
self.room = room
self.recordFrameTime(timePerIteration)
self.renderTask = nil
}
}
}
private func recordFrameTime(_ time: TimeInterval) {
recentTimes.append(time)
if recentTimes.count > recentTimesCapacity {
recentTimes.removeFirst(recentTimes.count - recentTimesCapacity)
}
let now = CACurrentMediaTime()
if now - lastDisplayUpdate >= displayUpdateInterval {
lastDisplayUpdate = now
averageFrameTime = recentTimes.reduce(0, +) / TimeInterval(recentTimes.count)
}
}
public func startAnimation() {
stopAnimation()
isAnimating = true
animationTimer = Timer.scheduledTimer(withTimeInterval: 1.0 / 60.0, repeats: true) {
[weak self] _ in
Task { @MainActor in
self?.tick()
}
}
}
public func stopAnimation() {
animationTimer?.invalidate()
animationTimer = nil
isAnimating = false
}
public func tick() {
if renderTask == nil {
render()
}
}
}