| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifdef _WIN32 |
| 6 |
#define WIN32_MEAN_AND_LEAN |
| 7 |
#include <windows.h> |
| 8 |
#else |
| 9 |
#include <unistd.h> |
| 10 |
#endif |
| 11 |
#include <SDL2/SDL.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdbool.h> |
| 14 |
#include "hpc.h" |
| 15 |
|
| 16 |
#define FRAC_BITS 63 |
| 17 |
#define FRAC_SCALE (1ULL << FRAC_BITS) |
| 18 |
#define FRAC_MASK (FRAC_SCALE-1) |
| 19 |
|
| 20 |
hpcFreq_t hpcFreq; |
| 21 |
|
| 22 |
#ifdef _WIN32 |
| 23 |
|
| 24 |
#define STATUS_SUCCESS 0 |
| 25 |
|
| 26 |
static bool canAdjustTimerResolution; |
| 27 |
|
| 28 |
static NTSTATUS (__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval); |
| 29 |
static NTSTATUS (__stdcall *NtQueryTimerResolution)(PULONG MinimumResolution, PULONG MaximumResolution, PULONG ActualResolution); |
| 30 |
static NTSTATUS (__stdcall *NtSetTimerResolution)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution); |
| 31 |
|
| 32 |
static void (*usleep)(int32_t usec); |
| 33 |
|
| 34 |
static void usleepGood(int32_t usec) |
| 35 |
{ |
| 36 |
LARGE_INTEGER delayInterval; |
| 37 |
|
| 38 |
|
| 39 |
usec *= -10; |
| 40 |
|
| 41 |
delayInterval.HighPart = 0xFFFFFFFF; |
| 42 |
delayInterval.LowPart = usec; |
| 43 |
NtDelayExecution(false, &delayInterval); |
| 44 |
} |
| 45 |
|
| 46 |
static void usleepPoor(int32_t usec) |
| 47 |
{ |
| 48 |
Sleep((usec + 500) / 1000); |
| 49 |
} |
| 50 |
|
| 51 |
static void windowsSetupUsleep(void) |
| 52 |
{ |
| 53 |
NtDelayExecution = (NTSTATUS (__stdcall *)(BOOL, PLARGE_INTEGER))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution"); |
| 54 |
usleep = (NtDelayExecution != NULL) ? usleepGood : usleepPoor; |
| 55 |
|
| 56 |
NtQueryTimerResolution = (NTSTATUS (__stdcall *)(PULONG, PULONG, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryTimerResolution"); |
| 57 |
NtSetTimerResolution = (NTSTATUS (__stdcall *)(ULONG, BOOLEAN, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetTimerResolution"); |
| 58 |
canAdjustTimerResolution = (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL); |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
void hpc_Init(void) |
| 63 |
{ |
| 64 |
#ifdef _WIN32 |
| 65 |
windowsSetupUsleep(); |
| 66 |
#endif |
| 67 |
hpcFreq.freq64 = SDL_GetPerformanceFrequency(); |
| 68 |
|
| 69 |
double dFreq = (double)hpcFreq.freq64; |
| 70 |
|
| 71 |
hpcFreq.dFreqMulMs = 1000.0 / dFreq; |
| 72 |
hpcFreq.dFreqMulMicro = (1000.0 * 1000.0) / dFreq; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
static uint64_t getFrac64FromU64DivU32(uint64_t dividend, uint32_t divisor) |
| 77 |
{ |
| 78 |
if (dividend == 0 || divisor == 0 || divisor >= dividend) |
| 79 |
return 0; |
| 80 |
|
| 81 |
dividend %= divisor; |
| 82 |
|
| 83 |
if (dividend == 0) |
| 84 |
return 0; |
| 85 |
|
| 86 |
const uint32_t quotient = (uint32_t)((dividend << 32) / divisor); |
| 87 |
const uint32_t remainder = (uint32_t)((dividend << 32) % divisor); |
| 88 |
|
| 89 |
const uint32_t resultHi = quotient; |
| 90 |
const uint32_t resultLo = (uint32_t)(((uint64_t)remainder << 32) / divisor); |
| 91 |
|
| 92 |
return ((uint64_t)resultHi << 32) | resultLo; |
| 93 |
} |
| 94 |
|
| 95 |
void hpc_SetDurationInHz(hpc_t *hpc, uint32_t hz) |
| 96 |
{ |
| 97 |
|
| 98 |
hpc->durationInt = hpcFreq.freq64 / hz; |
| 99 |
hpc->durationFrac = getFrac64FromU64DivU32(hpcFreq.freq64, hz) >> 1; |
| 100 |
|
| 101 |
hpc->resetFrame = hz * (60 * 30); |
| 102 |
} |
| 103 |
|
| 104 |
void hpc_ResetCounters(hpc_t *hpc) |
| 105 |
{ |
| 106 |
hpc->endTimeInt = SDL_GetPerformanceCounter() + hpc->durationInt; |
| 107 |
hpc->endTimeFrac = hpc->durationFrac; |
| 108 |
} |
| 109 |
|
| 110 |
void hpc_Wait(hpc_t *hpc) |
| 111 |
{ |
| 112 |
#ifdef _WIN32 |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ULONG curRes, minRes, maxRes, junk; |
| 117 |
if (canAdjustTimerResolution && NtQueryTimerResolution(&minRes, &maxRes, &curRes) == STATUS_SUCCESS) |
| 118 |
{ |
| 119 |
if (curRes != 5000 && maxRes <= 5000) |
| 120 |
NtSetTimerResolution(5000, TRUE, &junk); |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
const uint64_t currTime64 = SDL_GetPerformanceCounter(); |
| 125 |
if (currTime64 < hpc->endTimeInt) |
| 126 |
{ |
| 127 |
uint64_t timeLeft64 = hpc->endTimeInt - currTime64; |
| 128 |
|
| 129 |
|
| 130 |
if (timeLeft64 > INT32_MAX) |
| 131 |
timeLeft64 = INT32_MAX; |
| 132 |
|
| 133 |
const int32_t timeLeft32 = (int32_t)timeLeft64; |
| 134 |
|
| 135 |
int32_t microSecsLeft = (int32_t)((timeLeft32 * hpcFreq.dFreqMulMicro) + 0.5); |
| 136 |
if (microSecsLeft > 0) |
| 137 |
usleep(microSecsLeft); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
hpc->endTimeInt += hpc->durationInt; |
| 143 |
|
| 144 |
|
| 145 |
hpc->endTimeFrac += hpc->durationFrac; |
| 146 |
if (hpc->endTimeFrac >= FRAC_SCALE) |
| 147 |
{ |
| 148 |
hpc->endTimeFrac &= FRAC_MASK; |
| 149 |
hpc->endTimeInt++; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
hpc->frameCounter++; |
| 158 |
if (hpc->frameCounter >= hpc->resetFrame) |
| 159 |
{ |
| 160 |
hpc->frameCounter = 0; |
| 161 |
hpc_ResetCounters(hpc); |
| 162 |
} |
| 163 |
} |