19HGLRC GHOST_ContextWGL::s_sharedHGLRC =
nullptr;
20int GHOST_ContextWGL::s_sharedCount = 0;
21std::list<GHOST_ContextWGL::OffscreenWindowHandle>
22 GHOST_ContextWGL::s_sharedOffscreenWindowHandles;
27 return strstr((
const char *)glGetString(GL_VENDOR),
"Intel") !=
nullptr;
34 int contextProfileMask,
35 int contextMajorVersion,
36 int contextMinorVersion,
38 int contextResetNotificationStrategy)
42 context_profile_mask_(contextProfileMask),
43 context_major_version_(contextMajorVersion),
44 context_minor_version_(contextMinorVersion),
45 context_flags_(contextFlags),
46 alpha_background_(alphaBackground),
47 context_reset_notification_strategy_(contextResetNotificationStrategy),
50 dummy_vendor_(nullptr),
51 dummy_renderer_(nullptr),
52 dummy_version_(nullptr),
54 offscreen_window_handle_(nullptr)
56 if (h_wnd_ ==
nullptr) {
59 for (OffscreenWindowHandle &handle : s_sharedOffscreenWindowHandles) {
61 offscreen_window_handle_ = &handle;
65 if (offscreen_window_handle_ ==
nullptr) {
66 offscreen_window_handle_ = &s_sharedOffscreenWindowHandles.emplace_back();
68 offscreen_window_handle_->used =
true;
69 h_wnd_ = offscreen_window_handle_->h_wnd;
70 h_DC_ = offscreen_window_handle_->h_DC;
78 if (h_GLRC_ !=
nullptr) {
79 if (h_GLRC_ == ::wglGetCurrentContext()) {
80 WIN32_CHK(::wglMakeCurrent(
nullptr,
nullptr));
83 if (h_GLRC_ != s_sharedHGLRC || s_sharedCount == 1) {
88 if (s_sharedCount == 0) {
89 s_sharedHGLRC =
nullptr;
92 WIN32_CHK(::wglDeleteContext(h_GLRC_));
99 if (dummy_renderer_) {
100 free((
void *)dummy_renderer_);
101 free((
void *)dummy_vendor_);
102 free((
void *)dummy_version_);
114 if (epoxy_has_wgl_extension(h_DC_,
"WGL_EXT_swap_control")) {
124 if (epoxy_has_wgl_extension(h_DC_,
"WGL_EXT_swap_control")) {
125 interval_out = ::wglGetSwapIntervalEXT();
135 if (WIN32_CHK(::wglMakeCurrent(h_DC_, h_GLRC_))) {
148 if (WIN32_CHK(::wglMakeCurrent(h_DC_,
nullptr))) {
168 if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL) || !(pfd.dwFlags & PFD_DRAW_TO_WINDOW) ||
169 !(pfd.dwFlags & PFD_DOUBLEBUFFER) ||
170 !(pfd.iPixelType == PFD_TYPE_RGBA) ||
171 (pfd.cColorBits > 32) ||
172 (pfd.dwFlags & PFD_GENERIC_FORMAT))
179 weight += pfd.cColorBits - 8;
181 if (preferredPFD.cAlphaBits > 0 && pfd.cAlphaBits > 0) {
194 int iPixelFormat = 0;
197 int iStereoPixelFormat = 0;
198 int stereoWeight = 0;
201 int iLastResortPixelFormat = ::ChoosePixelFormat(hDC, &preferredPFD);
203 WIN32_CHK(iLastResortPixelFormat != 0);
205 int lastPFD = ::DescribePixelFormat(hDC, 1,
sizeof(PIXELFORMATDESCRIPTOR),
nullptr);
207 WIN32_CHK(lastPFD != 0);
209 for (
int i = 1; i <= lastPFD; i++) {
210 PIXELFORMATDESCRIPTOR pfd;
211 int check = ::DescribePixelFormat(hDC, i,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
213 WIN32_CHK(check == lastPFD);
222 if (
w > stereoWeight && (preferredPFD.dwFlags & pfd.dwFlags & PFD_STEREO)) {
224 iStereoPixelFormat = i;
229 if (iStereoPixelFormat != 0) {
230 iPixelFormat = iStereoPixelFormat;
233 if (iPixelFormat == 0) {
234 fprintf(stderr,
"Warning! Using result of ChoosePixelFormat.\n");
235 iPixelFormat = iLastResortPixelFormat;
250 SetLastError(NO_ERROR);
252 DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
253 WIN32_CHK(GetLastError() == NO_ERROR);
256 WCHAR lpClassName[257] =
L"";
257 count = GetClassNameW(hWnd, lpClassName,
sizeof(lpClassName) /
sizeof(WCHAR));
258 WIN32_CHK(count != 0);
260 const int titleLength = GetWindowTextLengthW(hWnd);
261 WIN32_CHK(titleLength != 0 || GetLastError() == NO_ERROR);
264 std::wstring WindowName(
static_cast<size_t>(titleLength) + 1,
L'\0');
265 count = GetWindowTextW(hWnd, WindowName.data(),
static_cast<int>(WindowName.size()));
266 WIN32_CHK(count >= 0);
268 WindowName.resize(
static_cast<size_t>(count));
270 DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
271 WIN32_CHK(GetLastError() == NO_ERROR);
274 GetWindowRect(hWnd, &rect);
275 WIN32_CHK(GetLastError() == NO_ERROR);
277 HWND hWndParent = (HWND)GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);
278 WIN32_CHK(GetLastError() == NO_ERROR);
280 HMENU hMenu = GetMenu(hWnd);
281 WIN32_CHK(GetLastError() == NO_ERROR);
283 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
284 WIN32_CHK(GetLastError() == NO_ERROR);
286 HWND hwndCloned = CreateWindowExW(dwExStyle,
292 rect.right - rect.left,
293 rect.bottom - rect.top,
299 WIN32_CHK(hwndCloned !=
nullptr);
309 out.push_back(WGL_SUPPORT_OPENGL_ARB);
310 out.push_back(GL_TRUE);
312 out.push_back(WGL_DRAW_TO_WINDOW_ARB);
313 out.push_back(GL_TRUE);
315 out.push_back(WGL_DOUBLE_BUFFER_ARB);
316 out.push_back(GL_TRUE);
318 out.push_back(WGL_ACCELERATION_ARB);
319 out.push_back(WGL_FULL_ACCELERATION_ARB);
322 out.push_back(WGL_STEREO_ARB);
323 out.push_back(GL_TRUE);
326 out.push_back(WGL_PIXEL_TYPE_ARB);
327 out.push_back(WGL_TYPE_RGBA_ARB);
329 out.push_back(WGL_COLOR_BITS_ARB);
333 out.push_back(WGL_ALPHA_BITS_ARB);
363 sizeof(PIXELFORMATDESCRIPTOR),
365 (DWORD)(PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW |
367 (stereoVisual ? PFD_STEREO : 0)),
369 (BYTE)(needAlpha ? 32 : 24),
376 (BYTE)(needAlpha ? 8 : 0),
393 SetLastError(NO_ERROR);
396 WIN32_CHK(GetLastError() == NO_ERROR);
399 WIN32_CHK(GetLastError() == NO_ERROR);
407 PIXELFORMATDESCRIPTOR chosenPFD;
408 if (!WIN32_CHK(::DescribePixelFormat(
424 if (!WIN32_CHK(
dummyHDC !=
nullptr)) {
445 "WGL_ARB_create_context_profile");
447 hDC,
"WGL_ARB_create_context_robustness");
468int GHOST_ContextWGL::_choose_pixel_format_arb_1(
bool stereoVisual,
bool needAlpha)
470 std::vector<int> iAttributes;
472#define _MAX_PIXEL_FORMATS 32
474 int iPixelFormat = 0;
480 WIN32_CHK(wglChoosePixelFormatARB(
483 if (nNumFormats > 0) {
484 iPixelFormat = iPixelFormats[0];
488 if (iPixelFormat != 0) {
490 int alphaBits, iQuery = WGL_ALPHA_BITS_ARB;
491 wglGetPixelFormatAttribivARB(h_DC_, iPixelFormat, 0, 1, &iQuery, &alphaBits);
492 if (alphaBits == 0) {
493 fprintf(stderr,
"Warning! Unable to find a frame buffer with alpha channel.\n");
500int GHOST_ContextWGL::choose_pixel_format_arb(
bool stereoVisual,
bool needAlpha)
504 iPixelFormat = _choose_pixel_format_arb_1(stereoVisual, needAlpha);
506 if (iPixelFormat == 0 && stereoVisual) {
507 fprintf(stderr,
"Warning! Unable to find a stereo pixel format.\n");
509 iPixelFormat = _choose_pixel_format_arb_1(
false, needAlpha);
520 fprintf(stderr,
"%s: %s\n", name, context);
522 if (dummy && strcmp(dummy, context) != 0) {
523 fprintf(stderr,
"Warning! Dummy %s: %s\n", name, dummy);
530 SetLastError(NO_ERROR);
532 HGLRC prevHGLRC = ::wglGetCurrentContext();
533 WIN32_CHK(GetLastError() == NO_ERROR);
535 HDC prevHDC = ::wglGetCurrentDC();
536 WIN32_CHK(GetLastError() == NO_ERROR);
539 const bool needAlpha = alpha_background_;
543 int iPixelFormat = 0;
549 if (iPixelFormat == 0) {
553 if (iPixelFormat == 0) {
557 PIXELFORMATDESCRIPTOR chosenPFD;
558 int lastPFD = ::DescribePixelFormat(
559 h_DC_, iPixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD);
561 if (!WIN32_CHK(lastPFD != 0)) {
565 if (needAlpha && chosenPFD.cAlphaBits == 0) {
566 fprintf(stderr,
"Warning! Unable to find a pixel format with an alpha channel.\n");
569 if (!WIN32_CHK(::SetPixelFormat(h_DC_, iPixelFormat, &chosenPFD))) {
575 int profileBitCore = context_profile_mask_ & WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
576 int profileBitCompat = context_profile_mask_ & WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
579 fprintf(stderr,
"Warning! OpenGL core profile not available.\n");
583 fprintf(stderr,
"Warning! OpenGL compatibility profile not available.\n");
589 profileMask |= profileBitCore;
593 profileMask |= profileBitCompat;
596 if (profileMask != context_profile_mask_) {
597 fprintf(stderr,
"Warning! Ignoring untested OpenGL context profile mask bits.");
600 std::vector<int> iAttributes;
603 iAttributes.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
604 iAttributes.push_back(profileMask);
607 if (context_major_version_ != 0) {
608 iAttributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
609 iAttributes.push_back(context_major_version_);
612 if (context_minor_version_ != 0) {
613 iAttributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
614 iAttributes.push_back(context_minor_version_);
617 if (context_flags_ != 0) {
618 iAttributes.push_back(WGL_CONTEXT_FLAGS_ARB);
619 iAttributes.push_back(context_flags_);
622 if (context_reset_notification_strategy_ != 0) {
624 iAttributes.push_back(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
625 iAttributes.push_back(context_reset_notification_strategy_);
628 fprintf(stderr,
"Warning! Cannot set the reset notification strategy.");
632 iAttributes.push_back(0);
634 h_GLRC_ = ::wglCreateContextAttribsARB(h_DC_, s_sharedHGLRC, &(iAttributes[0]));
641 const bool silent = context_major_version_ > 3;
642 if (!WIN32_CHK_SILENT(h_GLRC_ !=
nullptr, silent)) {
649 if (s_sharedHGLRC ==
nullptr) {
650 s_sharedHGLRC = h_GLRC_;
653 if (!WIN32_CHK(::wglMakeCurrent(h_DC_, h_GLRC_))) {
670 glEnable(GL_POINT_SPRITE);
674 ::SwapBuffers(h_DC_);
678 const char *vendor =
reinterpret_cast<const char *
>(glGetString(GL_VENDOR));
679 const char *renderer =
reinterpret_cast<const char *
>(glGetString(GL_RENDERER));
680 const char *version =
reinterpret_cast<const char *
>(glGetString(GL_VERSION));
686 fprintf(stderr,
"Context Version: %d.%d\n", context_major_version_, context_minor_version_);
693 ::wglMakeCurrent(prevHDC, prevHGLRC);
702 if (offscreen_window_handle_) {
703 offscreen_window_handle_->used =
false;
704 offscreen_window_handle_ =
nullptr;
713GHOST_ContextWGL::OffscreenWindowHandle::OffscreenWindowHandle()
715 h_wnd = CreateWindowA(
"STATIC",
717 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
724 GetModuleHandle(
nullptr),
729GHOST_ContextWGL::OffscreenWindowHandle::~OffscreenWindowHandle()
731 if (h_DC !=
nullptr) {
732 WIN32_CHK(::ReleaseDC(h_wnd, h_DC));
734 if (h_wnd !=
nullptr) {
735 WIN32_CHK(::DestroyWindow(h_wnd));
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)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
GHOST_ContextWGL(const GHOST_ContextParams &context_params, bool alphaBackground, HWND hWnd, HDC hDC, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
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 getSwapInterval(int &interval_out) override
GHOST_TSuccess swapBufferRelease() override
GHOST_Context(const GHOST_ContextParams &context_params)
static GHOST_Context * active_context_
virtual GHOST_TVSyncModes getVSync()
GHOST_ContextParams context_params_
#define assert(assertion)
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