2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-02-12 15:43:54 +01:00
|
|
|
/* context_gl_windows.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-11-19 17:52:18 +01:00
|
|
|
#if defined(OPENGL_ENABLED) || defined(GLES_ENABLED)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2008
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
#include "context_gl_windows.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-11-07 03:18:55 +01:00
|
|
|
#include <dwmapi.h>
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
|
|
|
#define WGL_CONTEXT_FLAGS_ARB 0x2094
|
2014-02-10 02:10:30 +01:00
|
|
|
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
|
2018-03-06 14:53:30 +01:00
|
|
|
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
|
|
|
|
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-01-16 12:07:58 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
// Workaround GCC warning from -Wcast-function-type.
|
|
|
|
#define wglGetProcAddress (void *)wglGetProcAddress
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
void ContextGL_Windows::release_current() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wglMakeCurrent(hDC, NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
void ContextGL_Windows::make_current() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wglMakeCurrent(hDC, hRC);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-10-03 14:13:39 +02:00
|
|
|
HDC ContextGL_Windows::get_hdc() {
|
|
|
|
return hDC;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGLRC ContextGL_Windows::get_hglrc() {
|
|
|
|
return hRC;
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
int ContextGL_Windows::get_window_width() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OS::get_singleton()->get_video_mode().width;
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
int ContextGL_Windows::get_window_height() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OS::get_singleton()->get_video_mode().height;
|
|
|
|
}
|
|
|
|
|
2019-11-07 03:18:55 +01:00
|
|
|
bool ContextGL_Windows::should_vsync_via_compositor() {
|
|
|
|
|
|
|
|
if (OS::get_singleton()->is_window_fullscreen() || !OS::get_singleton()->is_vsync_via_compositor_enabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: All Windows versions supported by Godot have a compositor.
|
|
|
|
// It can be disabled on earlier Windows versions.
|
|
|
|
BOOL dwm_enabled;
|
|
|
|
|
|
|
|
if (SUCCEEDED(DwmIsCompositionEnabled(&dwm_enabled))) {
|
|
|
|
return dwm_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
void ContextGL_Windows::swap_buffers() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
SwapBuffers(hDC);
|
2019-11-07 03:18:55 +01:00
|
|
|
|
|
|
|
if (use_vsync) {
|
|
|
|
bool vsync_via_compositor_now = should_vsync_via_compositor();
|
|
|
|
|
2020-01-15 23:04:23 +01:00
|
|
|
if (vsync_via_compositor_now && wglGetSwapIntervalEXT() == 0) {
|
2019-11-07 03:18:55 +01:00
|
|
|
DwmFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vsync_via_compositor_now != vsync_via_compositor) {
|
|
|
|
// The previous frame had a different operating mode than this
|
|
|
|
// frame. Set the 'vsync_via_compositor' member variable and the
|
|
|
|
// OpenGL swap interval to their proper values.
|
|
|
|
set_use_vsync(true);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
void ContextGL_Windows::set_use_vsync(bool p_use) {
|
2016-06-06 00:14:33 +02:00
|
|
|
|
2019-11-07 03:18:55 +01:00
|
|
|
vsync_via_compositor = p_use && should_vsync_via_compositor();
|
|
|
|
|
2016-06-06 00:14:33 +02:00
|
|
|
if (wglSwapIntervalEXT) {
|
2019-11-07 03:18:55 +01:00
|
|
|
int swap_interval = (p_use && !vsync_via_compositor) ? 1 : 0;
|
|
|
|
wglSwapIntervalEXT(swap_interval);
|
2016-06-06 00:14:33 +02:00
|
|
|
}
|
2019-11-07 03:18:55 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
use_vsync = p_use;
|
2016-06-06 00:14:33 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
bool ContextGL_Windows::is_using_vsync() const {
|
2016-06-06 00:14:33 +02:00
|
|
|
|
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
Error ContextGL_Windows::initialize() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static PIXELFORMATDESCRIPTOR pfd = {
|
|
|
|
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
2014-02-10 02:10:30 +01:00
|
|
|
1,
|
2017-03-05 16:44:50 +01:00
|
|
|
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
|
|
|
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
|
|
|
PFD_DOUBLEBUFFER,
|
2018-10-04 15:38:52 +02:00
|
|
|
(BYTE)PFD_TYPE_RGBA,
|
2018-10-17 08:46:50 +02:00
|
|
|
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
|
2018-10-04 15:38:52 +02:00
|
|
|
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
|
2018-10-17 08:46:50 +02:00
|
|
|
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
|
2018-10-04 15:38:52 +02:00
|
|
|
(BYTE)0, // Shift Bit Ignored
|
|
|
|
(BYTE)0, // No Accumulation Buffer
|
|
|
|
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
|
|
|
|
(BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
|
|
|
|
(BYTE)0, // No Stencil Buffer
|
|
|
|
(BYTE)0, // No Auxiliary Buffer
|
|
|
|
(BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
|
|
|
|
(BYTE)0, // Reserved
|
2017-03-05 16:44:50 +01:00
|
|
|
0, 0, 0 // Layer Masks Ignored
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-09-06 23:50:18 +02:00
|
|
|
hDC = GetDC(hWnd);
|
|
|
|
if (!hDC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:50:18 +02:00
|
|
|
pixel_format = ChoosePixelFormat(hDC, &pfd);
|
|
|
|
if (!pixel_format) // Did Windows Find A Matching Pixel Format?
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:50:18 +02:00
|
|
|
BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
|
|
|
|
if (!ret) // Are We Able To Set The Pixel Format?
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:50:18 +02:00
|
|
|
hRC = wglCreateContext(hDC);
|
|
|
|
if (!hRC) // Are We Able To Get A Rendering Context?
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wglMakeCurrent(hDC, hRC);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (opengl_3_context) {
|
|
|
|
|
|
|
|
int attribs[] = {
|
2017-03-05 16:44:50 +01:00
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
|
|
|
//and it shall be forward compatible so that we can only use up to date functionality
|
2018-03-06 14:53:30 +01:00
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
2017-11-13 17:40:07 +01:00
|
|
|
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
|
2017-03-05 16:44:50 +01:00
|
|
|
0
|
|
|
|
}; //zero indicates the end of the array
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
|
2017-03-05 16:44:50 +01:00
|
|
|
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
|
|
|
wglDeleteContext(hRC);
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:50:18 +02:00
|
|
|
HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
|
|
|
|
if (!new_hRC) {
|
2014-02-10 02:10:30 +01:00
|
|
|
wglDeleteContext(hRC);
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return false
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
wglMakeCurrent(hDC, NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
wglDeleteContext(hRC);
|
2017-03-05 16:44:50 +01:00
|
|
|
hRC = new_hRC;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
|
2020-01-15 23:04:23 +01:00
|
|
|
wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
|
2017-01-14 12:26:56 +01:00
|
|
|
//glWrapperInit(wrapper_get_proc_address);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
ContextGL_Windows::ContextGL_Windows(HWND hwnd, bool p_opengl_3_context) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
opengl_3_context = p_opengl_3_context;
|
|
|
|
hWnd = hwnd;
|
|
|
|
use_vsync = false;
|
2019-11-07 03:18:55 +01:00
|
|
|
vsync_via_compositor = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
ContextGL_Windows::~ContextGL_Windows() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|