| 1 | // Copyright 2012 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/GL/GLInterface/WGL.h" |
| 5 | |
| 6 | #include <windows.h> |
| 7 | #include <array> |
| 8 | #include <string> |
| 9 | |
| 10 | #include "Common/Logging/Log.h" |
| 11 | #include "Common/MsgHandler.h" |
| 12 | |
| 13 | // from wglext.h |
| 14 | #ifndef WGL_ARB_pbuffer |
| 15 | #define WGL_ARB_pbuffer 1 |
| 16 | DECLARE_HANDLE(HPBUFFERARB); |
| 17 | #define WGL_DRAW_TO_PBUFFER_ARB 0x202D |
| 18 | #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E |
| 19 | #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F |
| 20 | #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030 |
| 21 | #define WGL_PBUFFER_LARGEST_ARB 0x2033 |
| 22 | #define WGL_PBUFFER_WIDTH_ARB 0x2034 |
| 23 | #define WGL_PBUFFER_HEIGHT_ARB 0x2035 |
| 24 | #define WGL_PBUFFER_LOST_ARB 0x2036 |
| 25 | typedef HPBUFFERARB(WINAPI* PFNWGLCREATEPBUFFERARBPROC)(HDC hDC, int iPixelFormat, int iWidth, |
| 26 | int iHeight, const int* piAttribList); |
| 27 | typedef HDC(WINAPI* PFNWGLGETPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer); |
| 28 | typedef int(WINAPI* PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer, HDC hDC); |
| 29 | typedef BOOL(WINAPI* PFNWGLDESTROYPBUFFERARBPROC)(HPBUFFERARB hPbuffer); |
| 30 | typedef BOOL(WINAPI* PFNWGLQUERYPBUFFERARBPROC)(HPBUFFERARB hPbuffer, int iAttribute, int* piValue); |
| 31 | #endif /* WGL_ARB_pbuffer */ |
| 32 | |
| 33 | #ifndef WGL_ARB_pixel_format |
| 34 | #define WGL_ARB_pixel_format 1 |
| 35 | #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000 |
| 36 | #define WGL_DRAW_TO_WINDOW_ARB 0x2001 |
| 37 | #define WGL_DRAW_TO_BITMAP_ARB 0x2002 |
| 38 | #define WGL_ACCELERATION_ARB 0x2003 |
| 39 | #define WGL_NEED_PALETTE_ARB 0x2004 |
| 40 | #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005 |
| 41 | #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006 |
| 42 | #define WGL_SWAP_METHOD_ARB 0x2007 |
| 43 | #define WGL_NUMBER_OVERLAYS_ARB 0x2008 |
| 44 | #define WGL_NUMBER_UNDERLAYS_ARB 0x2009 |
| 45 | #define WGL_TRANSPARENT_ARB 0x200A |
| 46 | #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037 |
| 47 | #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038 |
| 48 | #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039 |
| 49 | #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A |
| 50 | #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B |
| 51 | #define WGL_SHARE_DEPTH_ARB 0x200C |
| 52 | #define WGL_SHARE_STENCIL_ARB 0x200D |
| 53 | #define WGL_SHARE_ACCUM_ARB 0x200E |
| 54 | #define WGL_SUPPORT_GDI_ARB 0x200F |
| 55 | #define WGL_SUPPORT_OPENGL_ARB 0x2010 |
| 56 | #define WGL_DOUBLE_BUFFER_ARB 0x2011 |
| 57 | #define WGL_STEREO_ARB 0x2012 |
| 58 | #define WGL_PIXEL_TYPE_ARB 0x2013 |
| 59 | #define WGL_COLOR_BITS_ARB 0x2014 |
| 60 | #define WGL_RED_BITS_ARB 0x2015 |
| 61 | #define WGL_RED_SHIFT_ARB 0x2016 |
| 62 | #define WGL_GREEN_BITS_ARB 0x2017 |
| 63 | #define WGL_GREEN_SHIFT_ARB 0x2018 |
| 64 | #define WGL_BLUE_BITS_ARB 0x2019 |
| 65 | #define WGL_BLUE_SHIFT_ARB 0x201A |
| 66 | #define WGL_ALPHA_BITS_ARB 0x201B |
| 67 | #define WGL_ALPHA_SHIFT_ARB 0x201C |
| 68 | #define WGL_ACCUM_BITS_ARB 0x201D |
| 69 | #define WGL_ACCUM_RED_BITS_ARB 0x201E |
| 70 | #define WGL_ACCUM_GREEN_BITS_ARB 0x201F |
| 71 | #define WGL_ACCUM_BLUE_BITS_ARB 0x2020 |
| 72 | #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021 |
| 73 | #define WGL_DEPTH_BITS_ARB 0x2022 |
| 74 | #define WGL_STENCIL_BITS_ARB 0x2023 |
| 75 | #define WGL_AUX_BUFFERS_ARB 0x2024 |
| 76 | #define WGL_NO_ACCELERATION_ARB 0x2025 |
| 77 | #define WGL_GENERIC_ACCELERATION_ARB 0x2026 |
| 78 | #define WGL_FULL_ACCELERATION_ARB 0x2027 |
| 79 | #define WGL_SWAP_EXCHANGE_ARB 0x2028 |
| 80 | #define WGL_SWAP_COPY_ARB 0x2029 |
| 81 | #define WGL_SWAP_UNDEFINED_ARB 0x202A |
| 82 | #define WGL_TYPE_RGBA_ARB 0x202B |
| 83 | #define WGL_TYPE_COLORINDEX_ARB 0x202C |
| 84 | typedef BOOL(WINAPI* PFNWGLGETPIXELFORMATATTRIBIVARBPROC)(HDC hdc, int iPixelFormat, |
| 85 | int iLayerPlane, UINT nAttributes, |
| 86 | const int* piAttributes, int* piValues); |
| 87 | typedef BOOL(WINAPI* PFNWGLGETPIXELFORMATATTRIBFVARBPROC)(HDC hdc, int iPixelFormat, |
| 88 | int iLayerPlane, UINT nAttributes, |
| 89 | const int* piAttributes, FLOAT* pfValues); |
| 90 | typedef BOOL(WINAPI* PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int* piAttribIList, |
| 91 | const FLOAT* pfAttribFList, UINT nMaxFormats, |
| 92 | int* piFormats, UINT* nNumFormats); |
| 93 | #endif /* WGL_ARB_pixel_format */ |
| 94 | |
| 95 | #ifndef WGL_ARB_create_context |
| 96 | #define WGL_ARB_create_context 1 |
| 97 | #define WGL_CONTEXT_DEBUG_BIT_ARB 0x00000001 |
| 98 | #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 |
| 99 | #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 |
| 100 | #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 |
| 101 | #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093 |
| 102 | #define WGL_CONTEXT_FLAGS_ARB 0x2094 |
| 103 | #define ERROR_INVALID_VERSION_ARB 0x2095 |
| 104 | typedef HGLRC(WINAPI* PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC hDC, HGLRC hShareContext, |
| 105 | const int* attribList); |
| 106 | #endif /* WGL_ARB_create_context */ |
| 107 | |
| 108 | #ifndef WGL_ARB_create_context_profile |
| 109 | #define WGL_ARB_create_context_profile 1 |
| 110 | #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126 |
| 111 | #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 |
| 112 | #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002 |
| 113 | #define ERROR_INVALID_PROFILE_ARB 0x2096 |
| 114 | #endif /* WGL_ARB_create_context_profile */ |
| 115 | |
| 116 | #ifndef WGL_EXT_swap_control |
| 117 | #define WGL_EXT_swap_control 1 |
| 118 | typedef BOOL(WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int interval); |
| 119 | typedef int(WINAPI* PFNWGLGETSWAPINTERVALEXTPROC)(void); |
| 120 | #endif /* WGL_EXT_swap_control */ |
| 121 | |
| 122 | // Persistent pointers |
| 123 | static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr; |
| 124 | static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; |
| 125 | static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = nullptr; |
| 126 | static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = nullptr; |
| 127 | static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = nullptr; |
| 128 | static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = nullptr; |
| 129 | static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = nullptr; |
| 130 | |
| 131 | static void LoadWGLExtensions() |
| 132 | { |
| 133 | wglSwapIntervalEXT = |
| 134 | reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT")); |
| 135 | wglCreateContextAttribsARB = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>( |
| 136 | wglGetProcAddress("wglCreateContextAttribsARB")); |
| 137 | wglChoosePixelFormatARB = reinterpret_cast<PFNWGLCHOOSEPIXELFORMATARBPROC>( |
| 138 | wglGetProcAddress("wglChoosePixelFormatARB")); |
| 139 | wglCreatePbufferARB = |
| 140 | reinterpret_cast<PFNWGLCREATEPBUFFERARBPROC>(wglGetProcAddress("wglCreatePbufferARB")); |
| 141 | wglGetPbufferDCARB = |
| 142 | reinterpret_cast<PFNWGLGETPBUFFERDCARBPROC>(wglGetProcAddress("wglGetPbufferDCARB")); |
| 143 | wglReleasePbufferDCARB = |
| 144 | reinterpret_cast<PFNWGLRELEASEPBUFFERDCARBPROC>(wglGetProcAddress("wglReleasePbufferDCARB")); |
| 145 | wglDestroyPbufferARB = |
| 146 | reinterpret_cast<PFNWGLDESTROYPBUFFERARBPROC>(wglGetProcAddress("wglDestroyPbufferARB")); |
| 147 | } |
| 148 | |
| 149 | static void ClearWGLExtensionPointers() |
| 150 | { |
| 151 | wglSwapIntervalEXT = nullptr; |
| 152 | wglCreateContextAttribsARB = nullptr; |
| 153 | wglChoosePixelFormatARB = nullptr; |
| 154 | wglCreatePbufferARB = nullptr; |
| 155 | wglGetPbufferDCARB = nullptr; |
| 156 | wglReleasePbufferDCARB = nullptr; |
| 157 | wglDestroyPbufferARB = nullptr; |
| 158 | } |
| 159 | |
| 160 | GLContextWGL::~GLContextWGL() |
| 161 | { |
| 162 | if (m_rc) |
| 163 | { |
| 164 | if (wglGetCurrentContext() == m_rc && !wglMakeCurrent(m_dc, nullptr)) |
| 165 | NOTICE_LOG_FMT(VIDEO, "Could not release drawing context."); |
| 166 | |
| 167 | if (!wglDeleteContext(m_rc)) |
| 168 | ERROR_LOG_FMT(VIDEO, "Attempt to release rendering context failed."); |
| 169 | |
| 170 | m_rc = nullptr; |
| 171 | } |
| 172 | |
| 173 | if (m_dc) |
| 174 | { |
| 175 | if (m_pbuffer_handle) |
| 176 | { |
| 177 | wglReleasePbufferDCARB(static_cast<HPBUFFERARB>(m_pbuffer_handle), m_dc); |
| 178 | m_dc = nullptr; |
| 179 | |
| 180 | wglDestroyPbufferARB(static_cast<HPBUFFERARB>(m_pbuffer_handle)); |
| 181 | m_pbuffer_handle = nullptr; |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | if (!ReleaseDC(m_window_handle, m_dc)) |
| 186 | ERROR_LOG_FMT(VIDEO, "Attempt to release device context failed."); |
| 187 | |
| 188 | m_dc = nullptr; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bool GLContextWGL::IsHeadless() const |
| 194 | { |
| 195 | return !m_window_handle; |
| 196 | } |
| 197 | |
| 198 | void GLContextWGL::SwapInterval(int interval) |
| 199 | { |
| 200 | if (wglSwapIntervalEXT) |
| 201 | { |
| 202 | wglSwapIntervalEXT(interval); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | ERROR_LOG_FMT(VIDEO, |
| 207 | "No support for SwapInterval (framerate clamped to monitor refresh rate)."); |
| 208 | } |
| 209 | } |
| 210 | void GLContextWGL::Swap() |
| 211 | { |
| 212 | SwapBuffers(m_dc); |
| 213 | } |
| 214 | |
| 215 | void* GLContextWGL::GetFuncAddress(const std::string& name) |
| 216 | { |
| 217 | FARPROC func = wglGetProcAddress(name.c_str()); |
| 218 | if (func == nullptr) |
| 219 | { |
| 220 | // Using GetModuleHandle here is okay, since we import functions from opengl32.dll, it's |
| 221 | // guaranteed to be loaded. |
| 222 | HMODULE opengl_module = GetModuleHandle(TEXT("opengl32.dll")); |
| 223 | func = GetProcAddress(opengl_module, name.c_str()); |
| 224 | } |
| 225 | |
| 226 | return reinterpret_cast<void*>(func); |
| 227 | } |
| 228 | |
| 229 | // Create rendering window. |
| 230 | // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() |
| 231 | bool GLContextWGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool core) |
| 232 | { |
| 233 | if (!wsi.render_surface) |
| 234 | return false; |
| 235 | |
| 236 | RECT window_rect = {}; |
| 237 | m_window_handle = static_cast<HWND>(wsi.render_surface); |
| 238 | if (!GetClientRect(m_window_handle, &window_rect)) |
| 239 | return false; |
| 240 | |
| 241 | // Clear extension function pointers before creating the first context. |
| 242 | ClearWGLExtensionPointers(); |
| 243 | |
| 244 | // Control window size and picture scaling |
| 245 | int twidth = window_rect.right - window_rect.left; |
| 246 | int theight = window_rect.bottom - window_rect.top; |
| 247 | m_backbuffer_width = twidth; |
| 248 | m_backbuffer_height = theight; |
| 249 | |
| 250 | const DWORD stereo_flag = stereo ? PFD_STEREO : 0; |
| 251 | |
| 252 | // clang-format off |
| 253 | static const PIXELFORMATDESCRIPTOR pfd = { |
| 254 | sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor |
| 255 | 1, // Version Number |
| 256 | PFD_DRAW_TO_WINDOW | // Format Must Support Window |
| 257 | PFD_SUPPORT_OPENGL | // Format Must Support OpenGL |
| 258 | PFD_DOUBLEBUFFER | // Must Support Double Buffering |
| 259 | stereo_flag, // Could Support Quad Buffering |
| 260 | PFD_TYPE_RGBA, // Request An RGBA Format |
| 261 | 32, // Select Our Color Depth |
| 262 | 0, |
| 263 | 0, 0, 0, 0, 0, // Color Bits Ignored |
| 264 | 0, // 8bit Alpha Buffer |
| 265 | 0, // Shift Bit Ignored |
| 266 | 0, // No Accumulation Buffer |
| 267 | 0, 0, 0, 0, // Accumulation Bits Ignored |
| 268 | 0, // 0Bit Z-Buffer (Depth Buffer) |
| 269 | 0, // 0bit Stencil Buffer |
| 270 | 0, // No Auxiliary Buffer |
| 271 | PFD_MAIN_PLANE, // Main Drawing Layer |
| 272 | 0, // Reserved |
| 273 | 0, 0, 0 // Layer Masks Ignored |
| 274 | }; |
| 275 | // clang-format on |
| 276 | |
| 277 | m_dc = GetDC(m_window_handle); |
| 278 | if (!m_dc) |
| 279 | { |
| 280 | PanicAlertFmt("(1) Can't create an OpenGL Device context. Fail."); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | const int pixel_format = ChoosePixelFormat(m_dc, &pfd); |
| 285 | if (pixel_format == 0) |
| 286 | { |
| 287 | PanicAlertFmt("(2) Can't find a suitable PixelFormat."); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | if (!SetPixelFormat(m_dc, pixel_format, &pfd)) |
| 292 | { |
| 293 | PanicAlertFmt("(3) Can't set the PixelFormat."); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | m_rc = wglCreateContext(m_dc); |
| 298 | if (!m_rc) |
| 299 | { |
| 300 | PanicAlertFmt("(4) Can't create an OpenGL rendering context."); |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | // WGL only supports desktop GL, for now. |
| 305 | m_opengl_mode = Mode::OpenGL; |
| 306 | |
| 307 | if (core) |
| 308 | { |
| 309 | // Make the fallback context current, temporarily. |
| 310 | // This is because we need an active context to use wglCreateContextAttribsARB. |
| 311 | if (!wglMakeCurrent(m_dc, m_rc)) |
| 312 | { |
| 313 | PanicAlertFmt("(5) Can't make dummy WGL context current."); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | // Load WGL extension function pointers. |
| 318 | LoadWGLExtensions(); |
| 319 | |
| 320 | // Attempt creating a core context. |
| 321 | HGLRC core_context = CreateCoreContext(m_dc, nullptr); |
| 322 | |
| 323 | // Switch out the temporary context before continuing, regardless of whether we got a core |
| 324 | // context. If we didn't get a core context, the caller expects that the context is not current. |
| 325 | if (!wglMakeCurrent(m_dc, nullptr)) |
| 326 | { |
| 327 | PanicAlertFmt("(6) Failed to switch out temporary context"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | // If we got a core context, destroy and replace the temporary context. |
| 332 | if (core_context) |
| 333 | { |
| 334 | wglDeleteContext(m_rc); |
| 335 | m_rc = core_context; |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | WARN_LOG_FMT(VIDEO, "Failed to create a core OpenGL context. Using fallback context."); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return MakeCurrent(); |
| 344 | } |
| 345 | |
| 346 | std::unique_ptr<GLContext> GLContextWGL::CreateSharedContext() |
| 347 | { |
| 348 | // WGL does not support surfaceless contexts, so we use a 1x1 pbuffer instead. |
| 349 | HANDLE pbuffer; |
| 350 | HDC dc; |
| 351 | if (!CreatePBuffer(m_dc, 1, 1, &pbuffer, &dc)) |
| 352 | return nullptr; |
| 353 | |
| 354 | HGLRC rc = CreateCoreContext(dc, m_rc); |
| 355 | if (!rc) |
| 356 | { |
| 357 | wglReleasePbufferDCARB(static_cast<HPBUFFERARB>(pbuffer), dc); |
| 358 | wglDestroyPbufferARB(static_cast<HPBUFFERARB>(pbuffer)); |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | std::unique_ptr<GLContextWGL> context = std::make_unique<GLContextWGL>(); |
| 363 | context->m_pbuffer_handle = pbuffer; |
| 364 | context->m_dc = dc; |
| 365 | context->m_rc = rc; |
| 366 | context->m_opengl_mode = m_opengl_mode; |
| 367 | context->m_is_shared = true; |
| 368 | return context; |
| 369 | } |
| 370 | |
| 371 | HGLRC GLContextWGL::CreateCoreContext(HDC dc, HGLRC share_context) |
| 372 | { |
| 373 | if (!wglCreateContextAttribsARB) |
| 374 | { |
| 375 | WARN_LOG_FMT(VIDEO, "Missing WGL_ARB_create_context extension"); |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | for (const auto& version : s_desktop_opengl_versions) |
| 380 | { |
| 381 | // Construct list of attributes. Prefer a forward-compatible, core context. |
| 382 | std::array<int, 5 * 2> attribs = {WGL_CONTEXT_PROFILE_MASK_ARB, |
| 383 | WGL_CONTEXT_CORE_PROFILE_BIT_ARB, |
| 384 | #ifdef _DEBUG |
| 385 | WGL_CONTEXT_FLAGS_ARB, |
| 386 | WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | |
| 387 | WGL_CONTEXT_DEBUG_BIT_ARB, |
| 388 | #else |
| 389 | WGL_CONTEXT_FLAGS_ARB, |
| 390 | WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, |
| 391 | #endif |
| 392 | WGL_CONTEXT_MAJOR_VERSION_ARB, |
| 393 | version.first, |
| 394 | WGL_CONTEXT_MINOR_VERSION_ARB, |
| 395 | version.second, |
| 396 | 0, |
| 397 | 0}; |
| 398 | |
| 399 | // Attempt creating this context. |
| 400 | HGLRC core_context = wglCreateContextAttribsARB(dc, share_context, attribs.data()); |
| 401 | if (core_context) |
| 402 | { |
| 403 | INFO_LOG_FMT(VIDEO, "WGL: Created a GL {}.{} core context", version.first, version.second); |
| 404 | return core_context; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | WARN_LOG_FMT(VIDEO, "Unable to create a core OpenGL context of an acceptable version"); |
| 409 | return nullptr; |
| 410 | } |
| 411 | |
| 412 | bool GLContextWGL::CreatePBuffer(HDC onscreen_dc, int width, int height, HANDLE* pbuffer_handle, |
| 413 | HDC* pbuffer_dc) |
| 414 | { |
| 415 | if (!wglChoosePixelFormatARB || !wglCreatePbufferARB || !wglGetPbufferDCARB || |
| 416 | !wglReleasePbufferDCARB || !wglDestroyPbufferARB) |
| 417 | { |
| 418 | ERROR_LOG_FMT(VIDEO, "Missing WGL_ARB_pbuffer extension"); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | static constexpr std::array<int, 7 * 2> pf_iattribs = { |
| 423 | WGL_DRAW_TO_PBUFFER_ARB, |
| 424 | 1, // Pixel format compatible with pbuffer rendering |
| 425 | WGL_RED_BITS_ARB, |
| 426 | 0, |
| 427 | WGL_GREEN_BITS_ARB, |
| 428 | 0, |
| 429 | WGL_BLUE_BITS_ARB, |
| 430 | 0, |
| 431 | WGL_DEPTH_BITS_ARB, |
| 432 | 0, |
| 433 | WGL_STENCIL_BITS_ARB, |
| 434 | 0, |
| 435 | 0, |
| 436 | 0}; |
| 437 | |
| 438 | static constexpr std::array<float, 1 * 2> pf_fattribs = {}; |
| 439 | |
| 440 | int pixel_format; |
| 441 | UINT num_pixel_formats; |
| 442 | if (!wglChoosePixelFormatARB(onscreen_dc, pf_iattribs.data(), pf_fattribs.data(), 1, |
| 443 | &pixel_format, &num_pixel_formats)) |
| 444 | { |
| 445 | ERROR_LOG_FMT(VIDEO, "Failed to obtain a compatible pbuffer pixel format"); |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | static constexpr std::array<int, 1 * 2> pb_attribs = {}; |
| 450 | |
| 451 | HPBUFFERARB pbuffer = |
| 452 | wglCreatePbufferARB(onscreen_dc, pixel_format, width, height, pb_attribs.data()); |
| 453 | if (!pbuffer) |
| 454 | { |
| 455 | ERROR_LOG_FMT(VIDEO, "Failed to create pbuffer"); |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | HDC dc = wglGetPbufferDCARB(pbuffer); |
| 460 | if (!dc) |
| 461 | { |
| 462 | ERROR_LOG_FMT(VIDEO, "Failed to get drawing context from pbuffer"); |
| 463 | wglDestroyPbufferARB(pbuffer); |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | *pbuffer_handle = pbuffer; |
| 468 | *pbuffer_dc = dc; |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | bool GLContextWGL::MakeCurrent() |
| 473 | { |
| 474 | return wglMakeCurrent(m_dc, m_rc) == TRUE; |
| 475 | } |
| 476 | |
| 477 | bool GLContextWGL::ClearCurrent() |
| 478 | { |
| 479 | return wglMakeCurrent(m_dc, nullptr) == TRUE; |
| 480 | } |
| 481 | |
| 482 | // Update window width, size and etc. Called from Render.cpp |
| 483 | void GLContextWGL::Update() |
| 484 | { |
| 485 | RECT rcWindow; |
| 486 | GetClientRect(m_window_handle, &rcWindow); |
| 487 | |
| 488 | // Get the new window width and height |
| 489 | m_backbuffer_width = rcWindow.right - rcWindow.left; |
| 490 | m_backbuffer_height = rcWindow.bottom - rcWindow.top; |
| 491 | } |