32 int contextProfileMask,
33 int contextMajorVersion,
34 int contextMinorVersion,
36 int contextResetNotificationStrategy)
40 m_contextProfileMask(contextProfileMask),
41 m_contextMajorVersion(contextMajorVersion),
42 m_contextMinorVersion(contextMinorVersion),
43 m_contextFlags(contextFlags),
44 m_alphaBackground(alphaBackground),
45 m_contextResetNotificationStrategy(contextResetNotificationStrategy),
146 if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL) || !(pfd.dwFlags & PFD_DRAW_TO_WINDOW) ||
147 !(pfd.dwFlags & PFD_DOUBLEBUFFER) ||
148 !(pfd.iPixelType == PFD_TYPE_RGBA) ||
149 (pfd.cColorBits > 32) ||
150 (pfd.dwFlags & PFD_GENERIC_FORMAT))
157 weight += pfd.cColorBits - 8;
159 if (preferredPFD.cAlphaBits > 0 && pfd.cAlphaBits > 0) {
172 int iPixelFormat = 0;
175 int iStereoPixelFormat = 0;
176 int stereoWeight = 0;
179 int iLastResortPixelFormat = ::ChoosePixelFormat(hDC, &preferredPFD);
181 WIN32_CHK(iLastResortPixelFormat != 0);
183 int lastPFD = ::DescribePixelFormat(hDC, 1,
sizeof(PIXELFORMATDESCRIPTOR),
nullptr);
185 WIN32_CHK(lastPFD != 0);
187 for (
int i = 1;
i <= lastPFD;
i++) {
188 PIXELFORMATDESCRIPTOR pfd;
189 int check = ::DescribePixelFormat(hDC,
i,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
191 WIN32_CHK(check == lastPFD);
200 if (
w > stereoWeight && (preferredPFD.dwFlags & pfd.dwFlags & PFD_STEREO)) {
202 iStereoPixelFormat =
i;
207 if (iStereoPixelFormat != 0) {
208 iPixelFormat = iStereoPixelFormat;
211 if (iPixelFormat == 0) {
212 fprintf(stderr,
"Warning! Using result of ChoosePixelFormat.\n");
213 iPixelFormat = iLastResortPixelFormat;
228 SetLastError(NO_ERROR);
230 DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
231 WIN32_CHK(GetLastError() == NO_ERROR);
233 WCHAR lpClassName[100] =
L"";
234 count = GetClassNameW(hWnd, lpClassName,
sizeof(lpClassName));
235 WIN32_CHK(
count != 0);
237 WCHAR lpWindowName[100] =
L"";
238 count = GetWindowTextW(hWnd, lpWindowName,
sizeof(lpWindowName));
239 WIN32_CHK(
count != 0);
241 DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
242 WIN32_CHK(GetLastError() == NO_ERROR);
245 GetWindowRect(hWnd, &rect);
246 WIN32_CHK(GetLastError() == NO_ERROR);
248 HWND hWndParent = (HWND)GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);
249 WIN32_CHK(GetLastError() == NO_ERROR);
251 HMENU hMenu = GetMenu(hWnd);
252 WIN32_CHK(GetLastError() == NO_ERROR);
254 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
255 WIN32_CHK(GetLastError() == NO_ERROR);
257 HWND hwndCloned = CreateWindowExW(dwExStyle,
263 rect.right - rect.left,
264 rect.bottom - rect.top,
270 WIN32_CHK(hwndCloned !=
nullptr);
280 out.push_back(WGL_SUPPORT_OPENGL_ARB);
281 out.push_back(GL_TRUE);
283 out.push_back(WGL_DRAW_TO_WINDOW_ARB);
284 out.push_back(GL_TRUE);
286 out.push_back(WGL_DOUBLE_BUFFER_ARB);
287 out.push_back(GL_TRUE);
289 out.push_back(WGL_ACCELERATION_ARB);
290 out.push_back(WGL_FULL_ACCELERATION_ARB);
293 out.push_back(WGL_STEREO_ARB);
294 out.push_back(GL_TRUE);
297 out.push_back(WGL_PIXEL_TYPE_ARB);
298 out.push_back(WGL_TYPE_RGBA_ARB);
300 out.push_back(WGL_COLOR_BITS_ARB);
304 out.push_back(WGL_ALPHA_BITS_ARB);
460 wglGetPixelFormatAttribivARB(m_hDC, iPixelFormat, 0, 1, &iQuery, &alphaBits);
498 SetLastError(NO_ERROR);
500 HGLRC prevHGLRC = ::wglGetCurrentContext();
501 WIN32_CHK(GetLastError() == NO_ERROR);
503 HDC prevHDC = ::wglGetCurrentDC();
504 WIN32_CHK(GetLastError() == NO_ERROR);
507 const bool needAlpha = m_alphaBackground;
511 int iPixelFormat = 0;
514 iPixelFormat = choose_pixel_format_arb(
m_stereoVisual, needAlpha);
517 if (iPixelFormat == 0)
520 if (iPixelFormat == 0) {
524 PIXELFORMATDESCRIPTOR chosenPFD;
525 int lastPFD = ::DescribePixelFormat(
526 m_hDC, iPixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD);
528 if (!WIN32_CHK(lastPFD != 0)) {
532 if (needAlpha && chosenPFD.cAlphaBits == 0) {
533 fprintf(stderr,
"Warning! Unable to find a pixel format with an alpha channel.\n");
536 if (!WIN32_CHK(::SetPixelFormat(m_hDC, iPixelFormat, &chosenPFD))) {
542 int profileBitCore = m_contextProfileMask & WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
543 int profileBitCompat = m_contextProfileMask & WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
546 fprintf(stderr,
"Warning! OpenGL core profile not available.\n");
550 fprintf(stderr,
"Warning! OpenGL compatibility profile not available.\n");
556 profileMask |= profileBitCore;
560 profileMask |= profileBitCompat;
563 if (profileMask != m_contextProfileMask) {
564 fprintf(stderr,
"Warning! Ignoring untested OpenGL context profile mask bits.");
567 std::vector<int> iAttributes;
570 iAttributes.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
571 iAttributes.push_back(profileMask);
574 if (m_contextMajorVersion != 0) {
575 iAttributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
576 iAttributes.push_back(m_contextMajorVersion);
579 if (m_contextMinorVersion != 0) {
580 iAttributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
581 iAttributes.push_back(m_contextMinorVersion);
584 if (m_contextFlags != 0) {
585 iAttributes.push_back(WGL_CONTEXT_FLAGS_ARB);
586 iAttributes.push_back(m_contextFlags);
589 if (m_contextResetNotificationStrategy != 0) {
591 iAttributes.push_back(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
592 iAttributes.push_back(m_contextResetNotificationStrategy);
595 fprintf(stderr,
"Warning! Cannot set the reset notification strategy.");
599 iAttributes.push_back(0);
601 m_hGLRC = ::wglCreateContextAttribsARB(m_hDC, s_sharedHGLRC, &(iAttributes[0]));
608 const bool silent = m_contextMajorVersion > 3;
609 if (!WIN32_CHK_SILENT(m_hGLRC !=
nullptr, silent)) {
616 if (s_sharedHGLRC ==
nullptr) {
617 s_sharedHGLRC = m_hGLRC;
620 if (!WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC))) {
630 glEnable(GL_POINT_SPRITE);
634 ::SwapBuffers(m_hDC);
638 const char *vendor =
reinterpret_cast<const char *
>(glGetString(GL_VENDOR));
639 const char *renderer =
reinterpret_cast<const char *
>(glGetString(GL_RENDERER));
640 const char *version =
reinterpret_cast<const char *
>(glGetString(GL_VERSION));
646 fprintf(stderr,
"Context Version: %d.%d\n", m_contextMajorVersion, m_contextMinorVersion);
653 ::wglMakeCurrent(prevHDC, prevHGLRC);
static void makeAttribList(std::vector< int > &out, bool stereoVisual, bool needAlpha)
static int choose_pixel_format_legacy(HDC hDC, PIXELFORMATDESCRIPTOR &preferredPFD)
#define _MAX_PIXEL_FORMATS
static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd, PIXELFORMATDESCRIPTOR &preferredPFD)
static void reportContextString(const char *name, const char *dummy, const char *context)
static bool is_crappy_intel_card()
static HWND clone_window(HWND hWnd, LPVOID lpParam)
GHOST_ContextWGL(bool stereoVisual, bool alphaBackground, HWND hWnd, HDC hDC, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_TSuccess getSwapInterval(int &intervalOut) override
GHOST_TSuccess releaseNativeHandles() override
GHOST_TSuccess releaseDrawingContext() override
GHOST_TSuccess initializeDrawingContext() override
GHOST_TSuccess setSwapInterval(int interval) override
~GHOST_ContextWGL() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess swapBuffers() override
static GHOST_Context * active_context_
GHOST_Context(bool stereoVisual)
bool has_WGL_ARB_create_context_robustness
bool has_WGL_ARB_pixel_format
PIXELFORMATDESCRIPTOR preferredPFD
DummyContextWGL(HDC hDC, HWND hWnd, bool stereoVisual, bool needAlpha)
bool has_WGL_ARB_create_context_profile
bool has_WGL_ARB_create_context