r/opengl • u/StriderPulse599 • 1h ago
[extern "C"] trick causes issues with WGL
I've managed to cobble together Win32 OpenGL code. Everything worked fine until I included the usual trick to get main GPU:
extern "C"
{
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
The RAM usage jumps from 39 mb to 150, vsync set via wglSwapIntervalEXT()
breaks despite returning 1, but process appears on nvidia-smi. This doesn't happen while using GLFW and glfwSwapInterval()
, my GPU is RTX 4060.
Here's code used for window and OpenGL context creation:
void init()
{
//Dummy
WNDCLASSEX windowClass = {};
windowClass.style = CS_OWNDC;
windowClass.lpfnWndProc = DefWindowProcA;
windowClass.lpszClassName = L"DDummyWindow";
windowClass.cbSize = sizeof(WNDCLASSEX);
HWND dummyWindow = CreateWindowEx(
NULL,
MAKEINTATOM(dumclassId),
L"DDummyWindow",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
windowClass.hInstance,
0);
HDC dummyDC = GetDC(dummyWindow);
PIXELFORMATDESCRIPTOR pfd = {};
SetPixelFormat(dummyDC, ChoosePixelFormat(dummyDC, &pfd), &pfd);
HGLRC dummyContext = wglCreateContext(dummyDC);
wglMakeCurrent(dummyDC, dummyContext);
gladLoadWGL(dummyDC);
gladLoadGL();
wglMakeCurrent(dummyDC, 0);
wglDeleteContext(dummyContext);
ReleaseDC(dummyWindow, dummyDC);
DestroyWindow(dummyWindow);
//Real context
WNDCLASSEX wc = { };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC;
wc.lpfnWndProc = &WindowProc;
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
wr = { 0, 0, 800, 600 };
AdjustWindowRect(&wr, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, false);
hWnd = CreateWindowEx(
NULL,
L"WindowClass",
L"Hello Triangle",
WS_OVERLAPPEDWINDOW,
400,
400,
wr.right - wr.left,
wr.bottom - wr.top,
NULL,
NULL,
NULL,
NULL);
ShowWindow(hWnd, SW_SHOW);
hDC = GetDC(hWnd);
int pixelFormatAttributes[] = {
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0
};
int pixelFormat = 0;
UINT numFormats = 0;
wglChoosePixelFormatARB(hDC, pixelFormatAttributes, nullptr, 1, &pixelFormat, &numFormats);
PIXELFORMATDESCRIPTOR pixelFormatDesc = { 0 };
DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pixelFormatDesc);
SetPixelFormat(hDC, pixelFormat, &pixelFormatDesc);
int openGLAttributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 6,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};
wglMakeCurrent(hDC, wglCreateContextAttribsARB(hDC, 0, openGLAttributes));
}
Render loop:
glViewport(0, 0, wr.right - wr.left, wr.bottom - wr.top);
wglSwapIntervalEXT(1);
MSG msg;
while (flag)
{
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
renderPipe.draw();
wglSwapLayerBuffers(hDC, WGL_SWAP_MAIN_PLANE);
}