Skip to content

Commit

Permalink
Fix potential crasher in GPUCounters.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwa committed Oct 15, 2024
1 parent 5c10b95 commit db75aa4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Sources/RenderKit/GPUCounters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,24 @@ public final class GPUCounters: @unchecked Sendable {
public func gatherData() throws {
let timestamp = getMachTimeInNanoseconds()
if let lastSampleTimestamp {
let frameTime = timestamp - lastSampleTimestamp
addMeasurement(id: .frame, timestamp: timestamp, value: frameTime)
addMeasurement(id: .frame, timestamp: timestamp, from: lastSampleTimestamp, to: timestamp)
}
if let counterSampleBuffer {
let data = try counterSampleBuffer.resolveCounterRange(0 ..< 6)
data?.withUnsafeBytes { buffer in
if let counterSampleBuffer, let data = try counterSampleBuffer.resolveCounterRange(0 ..< 6) {
data.withUnsafeBytes { buffer in
let timestamps = buffer.bindMemory(to: MTLCounterResultTimestamp.self)
addMeasurement(id: .vertexShader, timestamp: timestamp, value: timestamps[1].timestamp - timestamps[0].timestamp)
addMeasurement(id: .fragmentShader, timestamp: timestamp, value: timestamps[3].timestamp - timestamps[2].timestamp)
addMeasurement(id: .computeShader, timestamp: timestamp, value: timestamps[5].timestamp - timestamps[4].timestamp)
addMeasurement(id: .vertexShader, timestamp: timestamp, from: timestamps[0].timestamp, to: timestamps[1].timestamp)
addMeasurement(id: .fragmentShader, timestamp: timestamp, from: timestamps[2].timestamp, to: timestamps[3].timestamp)
addMeasurement(id: .computeShader, timestamp: timestamp, from: timestamps[4].timestamp, to: timestamps[5].timestamp)
}
}
lastSampleTimestamp = timestamp
}

func addMeasurement(id: Measurement.Kind, timestamp: UInt64, value: UInt64) {
func addMeasurement(id: Measurement.Kind, timestamp: UInt64, from: UInt64, to: UInt64) {
let (value, overflow) = to.subtractingReportingOverflow(from)
guard overflow == false else {
return
}
measurements.withLock { measurements in
measurements[id, default: .init(id: id, maxSamples: maxSamples)].addSample(timestamp: timestamp, value: value)
}
Expand Down

0 comments on commit db75aa4

Please sign in to comment.