aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/pugl/pugl/detail/win_gl.c
blob: 8cdad760485e52214588f8b757fbf706baf4b3e3 (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
/*
  Copyright 2012-2020 David Robillard <d@drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/**
   @file win_gl.c
   @brief OpenGL graphics backend for Windows.
*/

#include "pugl/detail/stub.h"
#include "pugl/detail/types.h"
#include "pugl/detail/win.h"
#include "pugl/pugl_gl.h"

#include <windows.h>

#include <GL/gl.h>

#include <stdbool.h>
#include <stdlib.h>

#define WGL_DRAW_TO_WINDOW_ARB    0x2001
#define WGL_ACCELERATION_ARB      0x2003
#define WGL_SUPPORT_OPENGL_ARB    0x2010
#define WGL_DOUBLE_BUFFER_ARB     0x2011
#define WGL_PIXEL_TYPE_ARB        0x2013
#define WGL_RED_BITS_ARB          0x2015
#define WGL_GREEN_BITS_ARB        0x2017
#define WGL_BLUE_BITS_ARB         0x2019
#define WGL_ALPHA_BITS_ARB        0x201b
#define WGL_DEPTH_BITS_ARB        0x2022
#define WGL_STENCIL_BITS_ARB      0x2023
#define WGL_FULL_ACCELERATION_ARB 0x2027
#define WGL_TYPE_RGBA_ARB         0x202b
#define WGL_SAMPLE_BUFFERS_ARB    0x2041
#define WGL_SAMPLES_ARB           0x2042

#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_FLAGS_ARB         0x2094
#define WGL_CONTEXT_PROFILE_MASK_ARB  0x9126

#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB          0x00000001
#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
#define WGL_CONTEXT_DEBUG_BIT_ARB                 0x00000001

typedef HGLRC (*WglCreateContextAttribs)(HDC, HGLRC, const int*);
typedef BOOL (*WglSwapInterval)(int);
typedef BOOL (*WglChoosePixelFormat)(
	HDC, const int*, const FLOAT*, UINT, int*, UINT*);

typedef struct {
	WglChoosePixelFormat    wglChoosePixelFormat;
	WglCreateContextAttribs wglCreateContextAttribs;
	WglSwapInterval         wglSwapInterval;
} PuglWinGlProcs;

typedef struct {
	PuglWinGlProcs procs;
	HGLRC          hglrc;
} PuglWinGlSurface;

// Struct to manage the fake window used during configuration
typedef struct {
	HWND hwnd;
	HDC  hdc;
} PuglFakeWindow;

static PuglStatus
puglWinError(PuglFakeWindow* fakeWin, const PuglStatus status)
{
	if (fakeWin->hwnd) {
		ReleaseDC(fakeWin->hwnd, fakeWin->hdc);
		DestroyWindow(fakeWin->hwnd);
	}

	return status;
}

static PuglWinGlProcs puglWinGlGetProcs(void)
{
	const PuglWinGlProcs procs = {
		(WglChoosePixelFormat)(
			wglGetProcAddress("wglChoosePixelFormatARB")),
		(WglCreateContextAttribs)(
			wglGetProcAddress("wglCreateContextAttribsARB")),
		(WglSwapInterval)(
			wglGetProcAddress("wglSwapIntervalEXT"))
	};

	return procs;
}

static PuglStatus
puglWinGlConfigure(PuglView* view)
{
	PuglInternals* impl = view->impl;

	// clang-format off
	const int pixelAttrs[] = {
		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
		WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB,
		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
		WGL_DOUBLE_BUFFER_ARB,  view->hints[PUGL_DOUBLE_BUFFER],
		WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
		WGL_SAMPLE_BUFFERS_ARB, view->hints[PUGL_SAMPLES] ? 1 : 0,
		WGL_SAMPLES_ARB,        view->hints[PUGL_SAMPLES],
		WGL_RED_BITS_ARB,       view->hints[PUGL_RED_BITS],
		WGL_GREEN_BITS_ARB,     view->hints[PUGL_GREEN_BITS],
		WGL_BLUE_BITS_ARB,      view->hints[PUGL_BLUE_BITS],
		WGL_ALPHA_BITS_ARB,     view->hints[PUGL_ALPHA_BITS],
		WGL_DEPTH_BITS_ARB,     view->hints[PUGL_DEPTH_BITS],
		WGL_STENCIL_BITS_ARB,   view->hints[PUGL_STENCIL_BITS],
		0,
	};
	// clang-format on

	PuglWinGlSurface* const surface =
		(PuglWinGlSurface*)calloc(1, sizeof(PuglWinGlSurface));
	impl->surface = surface;

	// Create fake window for getting at GL context
	PuglStatus         st      = PUGL_SUCCESS;
	PuglFakeWindow     fakeWin = {0, 0};
	static const char* title   = "Pugl Configuration";
	if ((st = puglWinCreateWindow(view, title, &fakeWin.hwnd, &fakeWin.hdc))) {
		return puglWinError(&fakeWin, st);
	}

	// Set pixel format for fake window
	const PuglWinPFD fakePfd  = puglWinGetPixelFormatDescriptor(view->hints);
	const int        fakePfId = ChoosePixelFormat(fakeWin.hdc, &fakePfd);
	if (!fakePfId) {
		return puglWinError(&fakeWin, PUGL_SET_FORMAT_FAILED);
	} else if (!SetPixelFormat(fakeWin.hdc, fakePfId, &fakePfd)) {
		return puglWinError(&fakeWin, PUGL_SET_FORMAT_FAILED);
	}

	// Create fake GL context to get at the functions we need
	HGLRC fakeRc = wglCreateContext(fakeWin.hdc);
	if (!fakeRc) {
		return puglWinError(&fakeWin, PUGL_CREATE_CONTEXT_FAILED);
	}

	// Enter fake context and get extension functions
	wglMakeCurrent(fakeWin.hdc, fakeRc);
	surface->procs = puglWinGlGetProcs();

	if (surface->procs.wglChoosePixelFormat) {
		// Choose pixel format based on attributes
		UINT numFormats = 0;
		if (!surface->procs.wglChoosePixelFormat(
			    fakeWin.hdc, pixelAttrs, NULL, 1u, &impl->pfId, &numFormats)) {
			return puglWinError(&fakeWin, PUGL_SET_FORMAT_FAILED);
		}

		DescribePixelFormat(
			impl->hdc, impl->pfId, sizeof(impl->pfd), &impl->pfd);
	} else {
		// Modern extensions not available, use basic pixel format
		impl->pfd  = fakePfd;
		impl->pfId = fakePfId;
	}

	// Dispose of fake window and context
	wglMakeCurrent(NULL, NULL);
	wglDeleteContext(fakeRc);
	ReleaseDC(fakeWin.hwnd, fakeWin.hdc);
	DestroyWindow(fakeWin.hwnd);

	return PUGL_SUCCESS;
}

static PuglStatus
puglWinGlCreate(PuglView* view)
{
	PuglInternals* const    impl    = view->impl;
	PuglWinGlSurface* const surface = (PuglWinGlSurface*)impl->surface;
	PuglStatus              st      = PUGL_SUCCESS;

	const int contextAttribs[] = {
	    WGL_CONTEXT_MAJOR_VERSION_ARB,
	    view->hints[PUGL_CONTEXT_VERSION_MAJOR],

	    WGL_CONTEXT_MINOR_VERSION_ARB,
	    view->hints[PUGL_CONTEXT_VERSION_MINOR],

	    WGL_CONTEXT_FLAGS_ARB,
	    (view->hints[PUGL_USE_DEBUG_CONTEXT] ? WGL_CONTEXT_DEBUG_BIT_ARB : 0),

	    WGL_CONTEXT_PROFILE_MASK_ARB,
	    (view->hints[PUGL_USE_COMPAT_PROFILE]
	         ? WGL_CONTEXT_CORE_PROFILE_BIT_ARB
	         : WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB),

	    0};

	// Create real window with desired pixel format
	if ((st = puglWinCreateWindow(view, "Pugl", &impl->hwnd, &impl->hdc))) {
		return st;
	} else if (!SetPixelFormat(impl->hdc, impl->pfId, &impl->pfd)) {
		ReleaseDC(impl->hwnd, impl->hdc);
		DestroyWindow(impl->hwnd);
		impl->hwnd = NULL;
		impl->hdc  = NULL;
		return PUGL_SET_FORMAT_FAILED;
	}

	// Create GL context
	if (surface->procs.wglCreateContextAttribs &&
	    !(surface->hglrc = surface->procs.wglCreateContextAttribs(
		      impl->hdc, 0, contextAttribs))) {
		return PUGL_CREATE_CONTEXT_FAILED;
	} else if (!(surface->hglrc = wglCreateContext(impl->hdc))) {
		return PUGL_CREATE_CONTEXT_FAILED;
	}

	// Enter context and set swap interval
	wglMakeCurrent(impl->hdc, surface->hglrc);
	const int swapInterval = view->hints[PUGL_SWAP_INTERVAL];
	if (surface->procs.wglSwapInterval && swapInterval != PUGL_DONT_CARE) {
		surface->procs.wglSwapInterval(swapInterval);
	}

	return PUGL_SUCCESS;
}

static PuglStatus
puglWinGlDestroy(PuglView* view)
{
	PuglWinGlSurface* surface = (PuglWinGlSurface*)view->impl->surface;
	if (surface) {
		wglMakeCurrent(NULL, NULL);
		wglDeleteContext(surface->hglrc);
		free(surface);
		view->impl->surface = NULL;
	}

	return PUGL_SUCCESS;
}

static PuglStatus
puglWinGlEnter(PuglView* view, const PuglEventExpose* expose)
{
	PuglWinGlSurface* surface = (PuglWinGlSurface*)view->impl->surface;

	wglMakeCurrent(view->impl->hdc, surface->hglrc);

	if (expose) {
		PAINTSTRUCT ps;
		BeginPaint(view->impl->hwnd, &ps);
	}

	return PUGL_SUCCESS;
}

static PuglStatus
puglWinGlLeave(PuglView* view, const PuglEventExpose* expose)
{
	if (expose) {
		PAINTSTRUCT ps;
		EndPaint(view->impl->hwnd, &ps);
		SwapBuffers(view->impl->hdc);
	}

	wglMakeCurrent(NULL, NULL);
	return PUGL_SUCCESS;
}

PuglGlFunc
puglGetProcAddress(const char* name)
{
	const PuglGlFunc func = (PuglGlFunc)wglGetProcAddress(name);

	/* Windows has the annoying property that wglGetProcAddress returns NULL
	   for functions from OpenGL 1.1, so we fall back to pulling them directly
	   from opengl32.dll */

	return func
		? func
		: (PuglGlFunc)GetProcAddress(GetModuleHandle("opengl32.dll"), name);
}

const PuglBackend*
puglGlBackend(void)
{
	static const PuglBackend backend = {puglWinGlConfigure,
	                                    puglWinGlCreate,
	                                    puglWinGlDestroy,
	                                    puglWinGlEnter,
	                                    puglWinGlLeave,
	                                    puglStubGetContext};

	return &backend;
}