aboutsummaryrefslogtreecommitdiff
path: root/pugl/pugl/detail/win_gl.c
blob: 17ee68d80956d6ceb448e7cfefbab1b7dccfa2e6 (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
/*
  Copyright 2012-2019 David Robillard <http://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 OpenGL graphics backend for Windows.
*/

#include "pugl/detail/types.h"
#include "pugl/detail/win.h"
#include "pugl/pugl_gl_backend.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_COLOR_BITS_ARB        0x2014
#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_LAYER_PLANE_ARB   0x2093
#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

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 int
puglWinError(PuglFakeWindow* fakeWin, const int 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 int
puglWinGlConfigure(PuglView* view)
{
	PuglInternals* impl = view->impl;

	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.double_buffer,
		WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
		WGL_SAMPLE_BUFFERS_ARB, view->hints.samples ? 1 : 0,
		WGL_SAMPLES_ARB,        view->hints.samples,
		WGL_RED_BITS_ARB,       view->hints.red_bits,
		WGL_GREEN_BITS_ARB,     view->hints.green_bits,
		WGL_BLUE_BITS_ARB,      view->hints.blue_bits,
		WGL_ALPHA_BITS_ARB,     view->hints.alpha_bits,
		WGL_DEPTH_BITS_ARB,     view->hints.depth_bits,
		WGL_STENCIL_BITS_ARB,   view->hints.stencil_bits,
		0,
	};

	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 };
	if ((st = puglWinCreateWindow(view, "Pugl Configuration",
	                              &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_ERR_SET_FORMAT);
	} else if (!SetPixelFormat(fakeWin.hdc, fakePfId, &fakePfd)) {
		return puglWinError(&fakeWin, PUGL_ERR_SET_FORMAT);
	}

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

	// 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_ERR_SET_FORMAT);
		}

		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 0;
}

static int
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.context_version_major,
		WGL_CONTEXT_MINOR_VERSION_ARB, view->hints.context_version_minor,
		WGL_CONTEXT_PROFILE_MASK_ARB,
		(view->hints.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_ERR_SET_FORMAT;
	}

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

	// Enter context and set swap interval
	wglMakeCurrent(impl->hdc, surface->hglrc);
	if (surface->procs.wglSwapInterval) {
		surface->procs.wglSwapInterval(1);
	}

	return 0;
}

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

	return 0;
}

static int
puglWinGlEnter(PuglView* view, bool drawing)
{
	PuglWinGlSurface* surface = (PuglWinGlSurface*)view->impl->surface;

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

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

	return 0;
}

static int
puglWinGlLeave(PuglView* view, bool drawing)
{
	if (drawing) {
		PAINTSTRUCT ps;
		EndPaint(view->impl->hwnd, &ps);
		SwapBuffers(view->impl->hdc);
	}

	wglMakeCurrent(NULL, NULL);

	return 0;
}

static int
puglWinGlResize(PuglView* PUGL_UNUSED(view),
                int       PUGL_UNUSED(width),
                int       PUGL_UNUSED(height))
{
	return 0;
}

static void*
puglWinGlGetContext(PuglView* PUGL_UNUSED(view))
{
	return NULL;
}

PuglGlFunc
puglGetProcAddress(const char* name)
{
	return (PuglGlFunc)wglGetProcAddress(name);
}

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

	return &backend;
}