Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_ContextCGL.mm « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b96a1dc93de471f3611e4abe5adb70a778050383 (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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Jason Wilkins
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file ghost/intern/GHOST_ContextCGL.mm
 *  \ingroup GHOST
 *
 * Definition of GHOST_ContextCGL class.
 */

#include "GHOST_ContextCGL.h"

#include <Cocoa/Cocoa.h>

#ifdef GHOST_MULTITHREADED_OPENGL
#include <OpenGL/OpenGL.h>
#endif

#include <vector>
#include <cassert>


NSOpenGLContext *GHOST_ContextCGL::s_sharedOpenGLContext = nil;
int              GHOST_ContextCGL::s_sharedCount         = 0;


GHOST_ContextCGL::GHOST_ContextCGL(
        bool stereoVisual,
        GHOST_TUns16  numOfAASamples,
        NSWindow *window,
        NSOpenGLView *openGLView,
        int contextProfileMask,
        int contextMajorVersion,
        int contextMinorVersion,
        int contextFlags,
        int contextResetNotificationStrategy)
    : GHOST_Context(stereoVisual, numOfAASamples),
      m_window(window),
      m_openGLView(openGLView),
      m_contextProfileMask(contextProfileMask),
      m_contextMajorVersion(contextMajorVersion),
      m_contextMinorVersion(contextMinorVersion),
      m_contextFlags(contextFlags),
      m_contextResetNotificationStrategy(contextResetNotificationStrategy),
      m_openGLContext(nil)
{
	assert(window != nil);
	assert(openGLView != nil);
}


GHOST_ContextCGL::~GHOST_ContextCGL()
{
	if (m_openGLContext != nil) {
		if (m_openGLContext == [NSOpenGLContext currentContext])
			[NSOpenGLContext clearCurrentContext];
			[m_openGLView clearGLContext];

		if (m_openGLContext != s_sharedOpenGLContext || s_sharedCount == 1) {
			assert(s_sharedCount > 0);

			s_sharedCount--;

			if (s_sharedCount == 0)
				s_sharedOpenGLContext = nil;

			[m_openGLContext release];
		}
	}
}


GHOST_TSuccess GHOST_ContextCGL::swapBuffers()
{
	if (m_openGLContext != nil) {
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		[m_openGLContext flushBuffer];
		[pool drain];
		return GHOST_kSuccess;
	}
	else {
		return GHOST_kFailure;
	}
}


GHOST_TSuccess GHOST_ContextCGL::setSwapInterval(int interval)
{
	if (m_openGLContext != nil) {
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		[m_openGLContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
		[pool drain];
		return GHOST_kSuccess;
	}
	else {
		return GHOST_kFailure;
	}
}


GHOST_TSuccess GHOST_ContextCGL::getSwapInterval(int &intervalOut)
{
	if (m_openGLContext != nil) {
		GLint interval;

		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

		[m_openGLContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];

		[pool drain];

		intervalOut = static_cast<int>(interval);

		return GHOST_kSuccess;
	}
	else {
		return GHOST_kFailure;
	}
}


GHOST_TSuccess GHOST_ContextCGL::activateDrawingContext()
{
	if (m_openGLContext != nil) {
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		[m_openGLContext makeCurrentContext];

		activateGLEW();

		[pool drain];
		return GHOST_kSuccess;
	}
	else {
		return GHOST_kFailure;
	}
}


GHOST_TSuccess GHOST_ContextCGL::updateDrawingContext()
{
	if (m_openGLContext != nil) {
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		[m_openGLContext update];
		[pool drain];
		return GHOST_kSuccess;
	}
	else {
		return GHOST_kFailure;
	}
}


static void makeAttribList(
        std::vector<NSOpenGLPixelFormatAttribute>& attribs,
        bool stereoVisual,
        int numOfAASamples,
        bool needAlpha,
        bool needStencil)
{
	// Pixel Format Attributes for the windowed NSOpenGLContext
	attribs.push_back(NSOpenGLPFADoubleBuffer);

	// Guarantees the back buffer contents to be valid after a call to NSOpenGLContext object's flushBuffer
	// needed for 'Draw Overlap' drawing method
	attribs.push_back(NSOpenGLPFABackingStore);

	// Force software OpenGL, for debugging
	/* XXX jwilkins: fixed this to work on Intel macs? useful feature for Windows and Linux too?
	 * Maybe a command line flag is better... */
	if (getenv("BLENDER_SOFTWAREGL")) {
		attribs.push_back(NSOpenGLPFARendererID);
#if defined(__ppc__) || defined(__ppc64__)
		attribs.push_back(kCGLRendererAppleSWID);
#else
		attribs.push_back(kCGLRendererGenericFloatID);
#endif
	}
	else {
		attribs.push_back(NSOpenGLPFAAccelerated);
	}

	/* Removed to allow 10.4 builds, and 2 GPUs rendering is not used anyway */
	//attribs.push_back(NSOpenGLPFAAllowOfflineRenderers);

	attribs.push_back(NSOpenGLPFADepthSize);
	attribs.push_back((NSOpenGLPixelFormatAttribute) 32);

	if (stereoVisual)
		attribs.push_back(NSOpenGLPFAStereo);

	if (needAlpha) {
		attribs.push_back(NSOpenGLPFAAlphaSize);
		attribs.push_back((NSOpenGLPixelFormatAttribute) 8);
	}

	if (needStencil) {
		attribs.push_back(NSOpenGLPFAStencilSize);
		attribs.push_back((NSOpenGLPixelFormatAttribute) 8);
	}

	if (numOfAASamples > 0) {
		// Multisample anti-aliasing
		attribs.push_back(NSOpenGLPFAMultisample);

		attribs.push_back(NSOpenGLPFASampleBuffers);
		attribs.push_back((NSOpenGLPixelFormatAttribute) 1);

		attribs.push_back(NSOpenGLPFASamples);
		attribs.push_back((NSOpenGLPixelFormatAttribute) numOfAASamples);

		attribs.push_back(NSOpenGLPFANoRecovery);
	}

	attribs.push_back((NSOpenGLPixelFormatAttribute) 0);
}


GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	std::vector<NSOpenGLPixelFormatAttribute> attribs;
	attribs.reserve(40);

	NSOpenGLContext *prev_openGLContext = [m_openGLView openGLContext];

#ifdef GHOST_OPENGL_ALPHA
	static const bool needAlpha   = true;
#else
	static const bool needAlpha   = false;
#endif

#ifdef GHOST_OPENGL_STENCIL
	static const bool needStencil = true;
#else
	static const bool needStencil = false;
#endif

	makeAttribList(attribs, m_stereoVisual, m_numOfAASamples, needAlpha, needStencil);

	NSOpenGLPixelFormat *pixelFormat;

	pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attribs[0]];

	// Fall back to no multisampling if Antialiasing init failed
	if (m_numOfAASamples > 0 && pixelFormat == nil) {
		// XXX jwilkins: Does CGL only succeed when it makes an exact match on the number of samples?
		// Does this need to explicitly try for a lesser match before giving up?
		// (Now that I think about it, does WGL really require the code that it has for finding a lesser match?)

		attribs.clear();
		makeAttribList(attribs, m_stereoVisual, 0, needAlpha, needStencil);
		pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attribs[0]];
	}

	if (pixelFormat == nil)
		goto error;

	if (m_numOfAASamples > 0) { //Set m_numOfAASamples to the actual value
		GLint actualSamples;
		[pixelFormat getValues:&actualSamples forAttribute:NSOpenGLPFASamples forVirtualScreen:0];

		if (m_numOfAASamples != (GHOST_TUns16)actualSamples) {
			fprintf(stderr,
			        "Warning! Unable to find a multisample pixel format that supports exactly %d samples. "
			        "Substituting one that uses %d samples.\n",
			        m_numOfAASamples, actualSamples);

			m_numOfAASamples = (GHOST_TUns16)actualSamples;
		}
	}

	[m_openGLView setPixelFormat:pixelFormat];

	m_openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:s_sharedOpenGLContext];

	if (m_openGLContext == nil)
		goto error;

	if (s_sharedCount == 0)
		s_sharedOpenGLContext = m_openGLContext;

	s_sharedCount++;

#ifdef GHOST_MULTITHREADED_OPENGL
	//Switch openGL to multhreaded mode
	CGLContextObj cglCtx = (CGLContextObj)[tmpOpenGLContext CGLContextObj];
	if (CGLEnable(cglCtx, kCGLCEMPEngine) == kCGLNoError)
		printf("\nSwitched openGL to multithreaded mode\n");
#endif

#ifdef GHOST_WAIT_FOR_VSYNC
	{
		GLint swapInt = 1;
		/* wait for vsync, to avoid tearing artifacts */
		[m_openGLContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
	}
#endif

	[m_openGLView setOpenGLContext:m_openGLContext];
	[m_openGLContext setView:m_openGLView];

	initContextGLEW();

	initClearGL();
	[m_openGLContext flushBuffer];

	[pool drain];

	return GHOST_kSuccess;

error:

	[m_openGLView setOpenGLContext:prev_openGLContext];

	[pool drain];

	return GHOST_kFailure;
}


GHOST_TSuccess GHOST_ContextCGL::releaseNativeHandles()
{
	m_openGLContext = NULL;
	m_openGLView    = NULL;

	return GHOST_kSuccess;
}