Created
December 20, 2019 08:09
-
-
Save gszauer/1e9c496717588e2bba137ddf98bcef42 to your computer and use it in GitHub Desktop.
Win32-Wait-For-VSYNCH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef LONG NTSTATUS; | |
typedef UINT D3DKMT_HANDLE; | |
typedef UINT D3DDDI_VIDEO_PRESENT_SOURCE_ID; | |
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) | |
#define STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002L) | |
typedef struct _D3DKMT_WAITFORVERTICALBLANKEVENT { | |
D3DKMT_HANDLE hAdapter; // in: adapter handle | |
D3DKMT_HANDLE hDevice; // in: device handle [Optional] | |
D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId; // in: adapter's VidPN Source ID | |
} D3DKMT_WAITFORVERTICALBLANKEVENT; | |
typedef struct _D3DKMT_OPENADAPTERFROMHDC { | |
HDC hDc; // in: DC that maps to a single display | |
D3DKMT_HANDLE hAdapter; // out: adapter handle | |
LUID AdapterLuid; // out: adapter LUID | |
D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId; // out: VidPN source ID for that particular display | |
} D3DKMT_OPENADAPTERFROMHDC; | |
extern "C" { | |
NTSTATUS __cdecl D3DKMTWaitForVerticalBlankEvent(const D3DKMT_WAITFORVERTICALBLANKEVENT*); | |
NTSTATUS __cdecl D3DKMTOpenAdapterFromHdc(D3DKMT_OPENADAPTERFROMHDC* lpParams); | |
} | |
D3DKMT_WAITFORVERTICALBLANKEVENT GetVBlankHandle() { | |
//https://docs.microsoft.com/en-us/windows/desktop/gdi/getting-information-on-a-display-monitor | |
DISPLAY_DEVICE dd; | |
dd.cb = sizeof(DISPLAY_DEVICE); | |
DWORD deviceNum = 0; | |
while (EnumDisplayDevices(NULL, deviceNum, &dd, 0)) { | |
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) { | |
break; | |
} | |
deviceNum++; | |
} | |
HDC hdc = CreateDC(NULL, dd.DeviceName, NULL, NULL); | |
if (hdc == NULL) { | |
//error("CreateDC failed"); | |
} | |
D3DKMT_OPENADAPTERFROMHDC OpenAdapterData; | |
OpenAdapterData.hDc = hdc; | |
if (STATUS_SUCCESS != D3DKMTOpenAdapterFromHdc(&OpenAdapterData)) { | |
//error("failed to open adapter from hDc"); | |
} | |
DeleteDC(hdc); | |
D3DKMT_WAITFORVERTICALBLANKEVENT we; | |
we.hAdapter = OpenAdapterData.hAdapter; | |
we.hDevice = 0; | |
we.VidPnSourceId = OpenAdapterData.VidPnSourceId; | |
return we; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In init | |
auto VBlankHandle = GetVBlankHandle(); | |
// In game loop | |
SwapBuffers(hdc); | |
D3DKMTWaitForVerticalBlankEvent(&VBlankHandle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!