如果您的机器上有来自不同供应商的 GPU,则很容易选择哪一个与 OpenGL 一起使用。
为此,请在创建 OpenGL 上下文之前调用以下函数:
// pass one of these to choose_ogl_vendor()
#define VENDOR_AMD "PCI\\VEN_1002&"
#define VENDOR_NVIDIA "PCI\\VEN_10DE&"
#define VENDOR_INTEL "PCI\\VEN_8086&"
void choose_ogl_vendor(const char *vendor_id)
{
int idx;
DISPLAY_DEVICEA dd;
HDC dc;
PIXELFORMATDESCRIPTOR pfd;
dd.cb = sizeof(dd);
idx = 0;
while (1) {
if (!EnumDisplayDevicesA(NULL, idx, &dd, 0))
return; // not found!
if (strstr(dd.DeviceID, vendor_id))
break; // there we go
idx += 1;
}
dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
// those flags are not important, they just need to be valid (and nondemanding, just in case).
// later you will use whatever flags you wish when you are creating your actual gl context
pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER|PFD_DEPTH_DONTCARE;
ChoosePixelFormat(dc, &pfd);
DeleteDC(dc);
}
此函数将强制 opengl32.dll 加载您选择的 ogl 驱动程序。
之后,继续进行通常的 OpenGL 上下文创建和初始化工作。
但是请注意,一旦加载了 GPU 供应商的驱动程序,就无法在进程的整个生命周期内更改它。