| 1 | /* |
| 2 | ** Hardware Performance Counter delay routines. |
| 3 | ** |
| 4 | ** These are by no means well written, and are made for specific |
| 5 | ** usage cases. There may be some hackish design choices here. |
| 6 | ** |
| 7 | ** NOTE: hpc_SetDurationInHz() has quite a bit of overhead, so it's |
| 8 | ** recommended to have one hpcFreq_t counter for each delay value, |
| 9 | ** then call hpc_SetDurationInHz()/hpc_SetDurationInMs() on program |
| 10 | ** init instead of every time you want to delay. |
| 11 | */ |
| 12 | |
| 13 | #ifdef _WIN32 |
| 14 | #define WIN32_MEAN_AND_LEAN |
| 15 | #include <windows.h> |
| 16 | #else |
| 17 | #include <unistd.h> |
| 18 | #endif |
| 19 | #include <SDL2/SDL.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdbool.h> |
| 22 | #include "pt2_hpc.h" |
| 23 | |
| 24 | #define DURATION_FRAC_BITS 52 /* more makes little sense */ |
| 25 | #define DURATION_FRAC_SCALE (1ULL << DURATION_FRAC_BITS) |
| 26 | #define DURATION_FRAC_MASK (DURATION_FRAC_SCALE-1) |
| 27 | |
| 28 | hpcFreq_t hpcFreq; |
| 29 | |
| 30 | #ifdef _WIN32 // Windows usleep() implementation |
| 31 | |
| 32 | #define STATUS_SUCCESS 0 |
| 33 | |
| 34 | static bool canAdjustTimerResolution; |
| 35 | |
| 36 | static NTSTATUS (__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval); |
| 37 | static NTSTATUS (__stdcall *NtQueryTimerResolution)(PULONG MinimumResolution, PULONG MaximumResolution, PULONG ActualResolution); |
| 38 | static NTSTATUS (__stdcall *NtSetTimerResolution)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution); |
| 39 | |
| 40 | static void (*usleep)(int32_t usec); |
| 41 | |
| 42 | static void usleepAcceptable(int32_t usec) |
| 43 | { |
| 44 | LARGE_INTEGER delayInterval; |
| 45 | |
| 46 | // NtDelayExecution() delays in 100ns-units, and a negative value means to delay from current time |
| 47 | usec *= -10; |
| 48 | |
| 49 | delayInterval.HighPart = 0xFFFFFFFF; // negative 64-bit value, we only set the lower dword |
| 50 | delayInterval.LowPart = usec; |
| 51 | NtDelayExecution(false, &delayInterval); |
| 52 | } |
| 53 | |
| 54 | static void usleepPoor(int32_t usec) // fallback if no NtDelayExecution() |
| 55 | { |
| 56 | Sleep((usec + 500) / 1000); |
| 57 | } |
| 58 | |
| 59 | static void windowsSetupUsleep(void) |
| 60 | { |
| 61 | NtDelayExecution = (NTSTATUS (__stdcall *)(BOOL, PLARGE_INTEGER))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution"); |
| 62 | usleep = (NtDelayExecution != NULL) ? usleepAcceptable : usleepPoor; |
| 63 | |
| 64 | NtQueryTimerResolution = (NTSTATUS (__stdcall *)(PULONG, PULONG, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryTimerResolution"); |
| 65 | NtSetTimerResolution = (NTSTATUS (__stdcall *)(ULONG, BOOLEAN, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetTimerResolution"); |
| 66 | canAdjustTimerResolution = (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | void hpc_Init(void) |
| 71 | { |
| 72 | #ifdef _WIN32 |
| 73 | windowsSetupUsleep(); |
| 74 | #endif |
| 75 | hpcFreq.freq64 = SDL_GetPerformanceFrequency(); |
| 76 | |
| 77 | double dFreq = (double)hpcFreq.freq64; |
| 78 | |
| 79 | hpcFreq.dFreqMulMs = 1000.0 / dFreq; |
| 80 | hpcFreq.dFreqMulMicro = (1000.0 * 1000.0) / dFreq; |
| 81 | } |
| 82 | |
| 83 | void hpc_SetDurationInHz(hpc_t *hpc, double dHz) |
| 84 | { |
| 85 | const double dTickTime = (double)hpcFreq.freq64 / dHz; |
| 86 | double dTimeInt, dTimeFrac = modf(dTickTime, &dTimeInt); |
| 87 | |
| 88 | hpc->durationInt = (uint32_t)dTimeInt; |
| 89 | hpc->durationFrac = (uint64_t)(dTimeFrac * DURATION_FRAC_SCALE); |
| 90 | hpc->resetFrame = (uint64_t)(dHz * (60*30)); // reset counters every half an hour |
| 91 | } |
| 92 | |
| 93 | void hpc_SetDurationInMs(hpc_t *hpc, double dMs) |
| 94 | { |
| 95 | hpc_SetDurationInHz(hpc, 1000.0 / dMs); |
| 96 | } |
| 97 | |
| 98 | void hpc_ResetCounters(hpc_t *hpc) |
| 99 | { |
| 100 | hpc->endTimeInt = SDL_GetPerformanceCounter() + hpc->durationInt; |
| 101 | hpc->endTimeFrac = hpc->durationFrac; |
| 102 | } |
| 103 | |
| 104 | void hpc_Wait(hpc_t *hpc) |
| 105 | { |
| 106 | #ifdef _WIN32 |
| 107 | /* Make sure resolution is set to 0.5ms (safest minimum) - this is confirmed to improve |
| 108 | ** NtDelayExecution() and Sleep(). This will only be changed when needed, not per frame. |
| 109 | */ |
| 110 | ULONG curRes, minRes, maxRes, junk; |
| 111 | if (canAdjustTimerResolution && NtQueryTimerResolution(&minRes, &maxRes, &curRes) == STATUS_SUCCESS) |
| 112 | { |
| 113 | if (curRes != 5000 && maxRes <= 5000) |
| 114 | NtSetTimerResolution(5000, TRUE, &junk); // 0.5ms |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | const uint64_t currTime64 = SDL_GetPerformanceCounter(); |
| 119 | if (currTime64 < hpc->endTimeInt) |
| 120 | { |
| 121 | uint64_t timeLeft64 = hpc->endTimeInt - currTime64; |
| 122 | |
| 123 | // convert to int32_t for fast SSE2 SIMD usage lateron |
| 124 | if (timeLeft64 > INT32_MAX) |
| 125 | timeLeft64 = INT32_MAX; |
| 126 | |
| 127 | const int32_t timeLeft32 = (int32_t)timeLeft64; |
| 128 | |
| 129 | int32_t microSecsLeft = (int32_t)((timeLeft32 * hpcFreq.dFreqMulMicro) + 0.5); // rounded |
| 130 | if (microSecsLeft > 0) |
| 131 | usleep(microSecsLeft); |
| 132 | } |
| 133 | |
| 134 | // set next end time |
| 135 | |
| 136 | hpc->endTimeInt += hpc->durationInt; |
| 137 | |
| 138 | // handle fractional part |
| 139 | hpc->endTimeFrac += hpc->durationFrac; |
| 140 | if (hpc->endTimeFrac >= DURATION_FRAC_SCALE) |
| 141 | { |
| 142 | hpc->endTimeFrac &= DURATION_FRAC_MASK; |
| 143 | hpc->endTimeInt++; |
| 144 | } |
| 145 | |
| 146 | /* The counter ("endTimeInt") can accumulate major errors after a couple of hours, |
| 147 | ** since each frame is not happening at perfect intervals. |
| 148 | ** To fix this, reset the counter's int & frac once every half an hour. We should only |
| 149 | ** get up to one frame of jitter while they are resetting, then it's back to normal. |
| 150 | */ |
| 151 | hpc->frameCounter++; |
| 152 | if (hpc->frameCounter >= hpc->resetFrame) |
| 153 | { |
| 154 | hpc->frameCounter = 0; |
| 155 | hpc_ResetCounters(hpc); |
| 156 | } |
| 157 | } |