aboutsummaryrefslogtreecommitdiffstats
path: root/Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp
blob: 9bfcec94d2a13c416c43b871a07015f1ee8dc6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/************************************************************************************

Filename    :   Render_GL_Win32 Device.cpp
Content     :   Win32 OpenGL Device implementation
Created     :   September 10, 2012
Authors     :   Andrew Reisse, Michael Antonov, David Borel

Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "Render_GL_Win32_Device.h"
#include "OVR_CAPI_GL.h"

#include <dwmapi.h>

namespace OVR { namespace Render { namespace GL { namespace Win32 {

typedef HRESULT (__stdcall *PFNDWMENABLECOMPOSITIONPROC) (UINT);

#pragma warning(disable : 4995)
PFNDWMENABLECOMPOSITIONPROC DwmEnableComposition;


// ***** GL::Win32::RenderDevice
    
RenderDevice::RenderDevice(const Render::RendererParams& p, HWND win, HGLRC gl)
    : GL::RenderDevice(p)
    , Window(win)
    , WglContext(gl)
	, PreFullscreen(0, 0, 0, 0)
    , HMonitor(0)
	, FSDesktop(0, 0, 0, 0)
{
    OVR_UNUSED(p);
}

// Implement static initializer function to create this class.
Render::RenderDevice* RenderDevice::CreateDevice(const RendererParams& rp, void* oswnd)
{
    HWND hwnd = (HWND)oswnd;
	HDC dc = GetDC(hwnd);
    
    if (!DwmEnableComposition)
    {
	    HINSTANCE hInst = LoadLibrary( L"dwmapi.dll" );
	    OVR_ASSERT(hInst);
        DwmEnableComposition = (PFNDWMENABLECOMPOSITIONPROC)GetProcAddress( hInst, "DwmEnableComposition" );
        OVR_ASSERT(DwmEnableComposition);
    }

    DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
    {
		PIXELFORMATDESCRIPTOR pfd;
		memset(&pfd, 0, sizeof(pfd));

		pfd.nSize       = sizeof(pfd);
		pfd.nVersion    = 1;
		pfd.iPixelType  = PFD_TYPE_RGBA;
		pfd.dwFlags     = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
		pfd.cColorBits  = 32;
		pfd.cDepthBits  = 16;

		int pf = ChoosePixelFormat(dc, &pfd);
		if (!pf)
		{
			ReleaseDC(hwnd, dc);
			return NULL;
		}
		
		if (!SetPixelFormat(dc, pf, &pfd))
		{
			ReleaseDC(hwnd, dc);
			return NULL;
		}

		HGLRC context = wglCreateContext(dc);
		if (!wglMakeCurrent(dc, context))
		{
			wglDeleteContext(context);
			ReleaseDC(hwnd, dc);
			return NULL;
		}
		
		wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
		wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");

		wglDeleteContext(context);
    }


	int iAttributes[] = {
		//WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
        WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
        WGL_COLOR_BITS_ARB, 32,
        WGL_DEPTH_BITS_ARB, 16,
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
		WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, GL_TRUE,
        0, 0};

    float fAttributes[] = {0,0};

	int pf = 0;
	UINT numFormats = 0;

	if (!wglChoosePixelFormatARB(dc, iAttributes, fAttributes, 1, &pf, &numFormats))
    {
        ReleaseDC(hwnd, dc);
        return NULL;
    }

    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(pfd));

    if (!SetPixelFormat(dc, pf, &pfd))
    {
        ReleaseDC(hwnd, dc);
        return NULL;
    }

	GLint attribs[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
		WGL_CONTEXT_MINOR_VERSION_ARB, 1,
		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
		0
	};

	HGLRC context = wglCreateContextAttribsARB(dc, 0, attribs);
	if (!wglMakeCurrent(dc, context))
	{
		wglDeleteContext(context);
		ReleaseDC(hwnd, dc);
		return NULL;
	}

    InitGLExtensions();

    return new RenderDevice(rp, hwnd, context);
}

ovrRenderAPIConfig RenderDevice::Get_ovrRenderAPIConfig() const
{
	static ovrGLConfig cfg;
	cfg.OGL.Header.API         = ovrRenderAPI_OpenGL;
	cfg.OGL.Header.RTSize      = Sizei(WindowWidth, WindowHeight);
	cfg.OGL.Header.Multisample = Params.Multisample;
	cfg.OGL.Window             = Window;

	return cfg.Config;
}

void RenderDevice::Present(bool useVsync)
{
	BOOL success;
	int swapInterval = (useVsync) ? 1 : 0;
	if (wglGetSwapIntervalEXT() != swapInterval)
		wglSwapIntervalEXT(swapInterval);

	HDC dc = GetDC(Window);
	success = SwapBuffers(dc);
	ReleaseDC(Window, dc);

    OVR_ASSERT(success);
}

void RenderDevice::Shutdown()
{
    //Release any remaining GL resources.
    GL::RenderDevice::Shutdown();

    if (WglContext)
    {
        wglMakeCurrent(NULL,NULL);
        wglDeleteContext(WglContext);
        WglContext = NULL;
        Window = NULL;
    }
}

bool RenderDevice::SetParams(const RendererParams& newParams)
{
    Params = newParams;
    //TODO: Apply changes now.
	return true;
}

BOOL CALLBACK MonitorEnumFunc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData)
{
    RenderDevice* renderer = (RenderDevice*)dwData;

    MONITORINFOEX monitor;
    monitor.cbSize = sizeof(monitor);

    if (::GetMonitorInfo(hMonitor, &monitor) && monitor.szDevice[0])
    {
        DISPLAY_DEVICE dispDev;
        memset(&dispDev, 0, sizeof(dispDev));
        dispDev.cb = sizeof(dispDev);

        if (::EnumDisplayDevices(monitor.szDevice, 0, &dispDev, 0))
        {
            if (strstr(String(dispDev.DeviceName).ToCStr(), renderer->GetParams().Display.MonitorName.ToCStr()))
            {
                renderer->HMonitor = hMonitor;
                renderer->FSDesktop.x = monitor.rcMonitor.left;
                renderer->FSDesktop.y = monitor.rcMonitor.top;
				renderer->FSDesktop.w = monitor.rcMonitor.right - monitor.rcMonitor.left;
				renderer->FSDesktop.h = monitor.rcMonitor.bottom - monitor.rcMonitor.top;
                return FALSE;
            }
        }
    }

    return TRUE;
}

bool RenderDevice::SetFullscreen(DisplayMode fullscreen)
{
	if (fullscreen == Params.Fullscreen)
    {
        return true;
    }

    if (Params.Fullscreen == Display_FakeFullscreen)
    {
        SetWindowLong(Window, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS);
        SetWindowPos(Window, HWND_NOTOPMOST, PreFullscreen.x, PreFullscreen.y,
                     PreFullscreen.w, PreFullscreen.h, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }
    else if (Params.Fullscreen == Display_Fullscreen)
    {
        {
            // Find out the name of the device this window
            // is on (this is for multi-monitor setups)
            HMONITOR hMonitor = MonitorFromWindow(Window, MONITOR_DEFAULTTOPRIMARY);
            MONITORINFOEX monInfo;
            memset(&monInfo, 0, sizeof(MONITORINFOEX));
            monInfo.cbSize = sizeof(MONITORINFOEX);
            GetMonitorInfo(hMonitor, &monInfo);

            // Restore the display resolution
            ChangeDisplaySettingsEx(monInfo.szDevice, NULL, NULL, 0, NULL);
            //ChangeDisplaySettings(NULL, 0);
        }
        {
            // Restore the window styles
            DWORD style = (DWORD)GetWindowLongPtr(Window, GWL_STYLE);
			DWORD exstyle = (DWORD)GetWindowLongPtr(Window, GWL_EXSTYLE);
            SetWindowLongPtr(Window, GWL_STYLE, style | WS_OVERLAPPEDWINDOW);
            SetWindowLongPtr(Window, GWL_EXSTYLE, exstyle & (~(WS_EX_APPWINDOW | WS_EX_TOPMOST)));
            
            MONITORINFOEX monInfo;
            memset(&monInfo, 0, sizeof(MONITORINFOEX));
            monInfo.cbSize = sizeof(MONITORINFOEX);
            GetMonitorInfo(HMonitor, &monInfo);

            // Restore the window size/position
            SetWindowPos(Window, NULL, PreFullscreen.x, PreFullscreen.y, PreFullscreen.w, PreFullscreen.h,
                            SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER);
        }
    }
	

    if (!Params.Display.MonitorName.IsEmpty())
    {
        EnumDisplayMonitors(0, 0, MonitorEnumFunc, (LPARAM)this);
    }

    if (fullscreen == Display_FakeFullscreen)
    {
        // Get WINDOWPLACEMENT before changing style to get OVERLAPPED coordinates,
        // which we will restore.
        WINDOWPLACEMENT wp;
        wp.length = sizeof(wp);
        GetWindowPlacement(Window, &wp);
        PreFullscreen.w = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
        PreFullscreen.h = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
        PreFullscreen.x = wp.rcNormalPosition.left;
        PreFullscreen.y = wp.rcNormalPosition.top;
        // Warning: SetWindowLong sends message computed based on old size (incorrect).
        // A proper work-around would be to mask that message out during window frame change in Platform.
        SetWindowLong(Window, GWL_STYLE, WS_OVERLAPPED | WS_VISIBLE | WS_CLIPSIBLINGS);
        SetWindowPos(Window, NULL, FSDesktop.x, FSDesktop.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_FRAMECHANGED);

        // Relocate cursor into the window to avoid losing focus on first click.
        POINT oldCursor;
        if (GetCursorPos(&oldCursor) &&
            ((oldCursor.x < FSDesktop.x) || (oldCursor.x > (FSDesktop.x + PreFullscreen.w)) ||
            (oldCursor.y < FSDesktop.y) || (oldCursor.x > (FSDesktop.y + PreFullscreen.h))))
        {
            // TBD: FullScreen window logic should really be in platform; it causes world rotation
            // in relative mouse mode.
            ::SetCursorPos(FSDesktop.x, FSDesktop.y);
        }
    }
    else if (fullscreen == Display_Fullscreen)
    {
        // Find out the name of the device this window
        // is on (this is for multi-monitor setups)
        MONITORINFOEX monInfo;
        memset(&monInfo, 0, sizeof(MONITORINFOEX));
        monInfo.cbSize = sizeof(MONITORINFOEX);
        GetMonitorInfo(HMonitor, &monInfo);

        // Save the current window position/size
        RECT rect;
        GetWindowRect(Window, &rect);
        PreFullscreen.x = rect.left;
        PreFullscreen.y = rect.top;
        PreFullscreen.w = rect.right - rect.left;
        PreFullscreen.h = rect.bottom - rect.top;

        // Save the window style and set it for fullscreen mode
        DWORD style = (DWORD)GetWindowLongPtr(Window, GWL_STYLE);
        DWORD exstyle = (DWORD)GetWindowLongPtr(Window, GWL_EXSTYLE);
        SetWindowLongPtr(Window, GWL_STYLE, style & (~WS_OVERLAPPEDWINDOW));
        SetWindowLongPtr(Window, GWL_EXSTYLE, exstyle | WS_EX_APPWINDOW | WS_EX_TOPMOST);

		ChangeDisplaySettingsEx(monInfo.szDevice, NULL, NULL, CDS_FULLSCREEN, NULL);

        // We need to call GetMonitorInfo() again becase
        // details may have changed with the resolution
        GetMonitorInfo(HMonitor, &monInfo);

		int x = monInfo.rcMonitor.left;
		int y = monInfo.rcMonitor.top;
		int w = monInfo.rcMonitor.right - monInfo.rcMonitor.left;
		int h = monInfo.rcMonitor.bottom - monInfo.rcMonitor.top;

        // Set the window's size and position so
        // that it covers the entire screen
        SetWindowPos(Window, HWND_TOPMOST, x, y, w, h, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_FRAMECHANGED);
    }

    Params.Fullscreen = fullscreen;
    return true;
}

}}}}