Skip to content

Commit

Permalink
Merge pull request #1997 from billhollings/fix-Intel-timestampPeriod
Browse files Browse the repository at this point in the history
Fix VkPhysicalDeviceLimits::timestampPeriod calculations on Intel GPU.
  • Loading branch information
billhollings authored Aug 17, 2023
2 parents b3c9f86 + 530bde1 commit 02a8c01
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1567,16 +1567,24 @@
return rslt;
}

// Mark both CPU and GPU timestamps, and if needed, update the timestamp period for this device.
// On Apple GPUs, the CPU & GPU timestamps are the same, and the timestamp period never changes.
// If needed, update the timestamp period for this device, using a crude lowpass filter to level out
// wild temporary changes, particularly during initial queries before much GPU activity has occurred.
// On Apple GPUs, CPU & GPU timestamps are the same, and timestamp period never changes.
void MVKPhysicalDevice::updateTimestampsAndPeriod() {
if (_properties.vendorID == kAppleVendorId) {
_prevGPUTimestamp = _prevCPUTimestamp = mvkGetElapsedNanoseconds();
} else {
MTLTimestamp earlierCPUTs = _prevCPUTimestamp;
MTLTimestamp earlierGPUTs = _prevGPUTimestamp;
[_mtlDevice sampleTimestamps: &_prevCPUTimestamp gpuTimestamp: &_prevGPUTimestamp];
_properties.limits.timestampPeriod = (double)(_prevCPUTimestamp - earlierCPUTs) / (double)(_prevGPUTimestamp - earlierGPUTs);
if (_properties.vendorID == kAppleVendorId) { return; }

MTLTimestamp earlierCPUTs = _prevCPUTimestamp;
MTLTimestamp earlierGPUTs = _prevGPUTimestamp;
[_mtlDevice sampleTimestamps: &_prevCPUTimestamp gpuTimestamp: &_prevGPUTimestamp];
double elapsedCPUNanos = _prevCPUTimestamp - earlierCPUTs;
double elapsedGPUTicks = _prevGPUTimestamp - earlierGPUTs;
if (elapsedCPUNanos && elapsedGPUTicks) { // Ensure not zero
float tsPeriod = elapsedCPUNanos / elapsedGPUTicks;

// Basic lowpass filter Y = (1 - a)Y + a*X.
// The lower a is, the slower Y will change over time.
static const float a = 0.05;
_properties.limits.timestampPeriod = ((1.0 - a) * _properties.limits.timestampPeriod) + (a * tsPeriod);
}
}

Expand Down

0 comments on commit 02a8c01

Please sign in to comment.