Reputation: 1966
I have the following code snippet which creates an OpenGL context, gets the function pointer to wglCreateContextAttribsARB, and then goes on to do other things:
WNDCLASSEX wc;
auto windowClassName = "WindowClassName";
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = ::DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = nullptr;
wc.hIcon = ::LoadIcon(nullptr, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = nullptr;
wc.lpszClassName = windowClassName;
wc.hIconSm = ::LoadIcon(nullptr, IDI_APPLICATION);
::RegisterClassEx(&wc);
// Step 2: Creating the Window
auto hWnd = ::CreateWindowEx(WS_EX_CLIENTEDGE,
windowClassName,
"Disposable window to get function pointer",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
nullptr, nullptr, nullptr, nullptr);
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
// Get the device context (DC)
auto hDC = ::GetDC(hWnd);
// Set the pixel format for the DC
::ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ::ChoosePixelFormat(hDC, &pfd);
::SetPixelFormat(hDC, iFormat, &pfd);
// Create and enable the render context (RC)
auto hRC = ::wglCreateContext(hDC);
::wglMakeCurrent(hDC, hRC);
PROC wglCreateContextAttribsARBPtr =
::wglGetProcAddress("wglCreateContextAttribsARB");
ASSERT(wglCreateContextAttribsARBPtr);
If I put this code in a main() function in a single C++ file, compile it with vs2010, and run it in the target environment, it all works fine. wglCreateContextAttribsARBPtr is populated with a function pointer, and that function pointer is used later to create a context which is core OpenGL 3.3.
If I put this exact same code (literally copy/paste) it into a huge existing code base (in such a way that it runs before any other OpenGL calls), using same compiler, same compiler flags, and run it in the same environment, wglCreateContextAttribsARBPtr is left as null.
Googling has told me this could be caused by a bad SetPixelFormat call, so I have tried a consider amount of variations to this function, including iterating through all valid pixel formats, to no avail.
However, the exact same code works in the exact same environment - so I have no idea what could possibly be wrong.
Any help in getting wglCreateContextAttribsARBPtr would be greatly appreciated.
Upvotes: 1
Views: 1151
Reputation: 162367
Your code lacks to test, if the OpenGL context you get for your proxy window is actually using the GPUs drivers. When reporting "GDI GENERIC" your program fell into using the software fallback rasterizer, which doesn't support all the fancy stuff. The most likely reason for why you're seeing this is, that you ask for a pixelformat that's not supported by your GPU.
Recommendations: First query the screen color depth (see http://blogs.msdn.com/b/oldnewthing/archive/2010/10/13/10075056.aspx) and use the values obtained that way to set the appropriate values in the PIXELFORMATDESCRIPTOR.
Also using DescribePixelFormat
you could enumerate all the possible pixelformats and look for one that has the PFD_GENERIC_FORMAT
flag not set.
If I put this exact same code (literally copy/paste) it into a huge existing code base (in such a way that it runs before any other OpenGL calls)
How do you ensure, that no other OpenGL / wgl calls were made?
Upvotes: 1