summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/src/glcontext_eagl.mm
blob: a03a64998ac6a75d8bc2aa03bda02ae42ae899ec (plain) (blame)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "bgfx_p.h"

#if BX_PLATFORM_IOS && (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)
#	include <UIKit/UIKit.h>
#	include <QuartzCore/CAEAGLLayer.h>
#	include "renderer_gl.h"

namespace bgfx { namespace gl
{
#	define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
#	include "glimports.h"

	static void* s_opengles = NULL;

	struct SwapChainGL
	{
		SwapChainGL(EAGLContext *_context, CAEAGLLayer *_layer)
			: m_context(_context)
			, m_fbo(0)
			, m_colorRbo(0)
			, m_depthStencilRbo(0)
		{
			_layer.contentsScale = [UIScreen mainScreen].scale;

			_layer.opaque = [_layer.style valueForKey:@"opaque"] == nil
				? true
				: [[_layer.style valueForKey:@"opaque"] boolValue]
				;

			_layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys
				: [NSNumber numberWithBool:false]
				, kEAGLDrawablePropertyRetainedBacking
				, kEAGLColorFormatRGBA8
				, kEAGLDrawablePropertyColorFormat
				, nil
				];

			[EAGLContext setCurrentContext:_context];

			GL_CHECK(glGenFramebuffers(1, &m_fbo) );
			GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );

			GL_CHECK(glGenRenderbuffers(1, &m_colorRbo) );
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );

			[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_layer];
			GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRbo) );

			GLint width;
			GLint height;
			GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width) );
			GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height) );
			BX_TRACE("Screen size: %d x %d", width, height);

			m_width = width;
			m_height = height;
			m_layer = _layer;

			createFrameBuffers(m_width, m_height);
		}

		~SwapChainGL()
		{
			destroyFrameBuffers();
		}

		void destroyFrameBuffers()
		{
			GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
			if (0 != m_fbo)
			{
				GL_CHECK(glDeleteFramebuffers(1, &m_fbo) );
				m_fbo = 0;
			}

			if (0 != m_colorRbo)
			{
				GL_CHECK(glDeleteRenderbuffers(1, &m_colorRbo) );
				m_colorRbo = 0;
			}

			if (0 != m_depthStencilRbo)
			{
				GL_CHECK(glDeleteRenderbuffers(1, &m_depthStencilRbo) );
				m_depthStencilRbo = 0;
			}
		}

		void createFrameBuffers(GLint _width, GLint _height)
		{
			GL_CHECK(glGenRenderbuffers(1, &m_depthStencilRbo) );
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilRbo) );
			GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, _width, _height) );
			GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );
			GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );

			GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
			BX_CHECK(GL_FRAMEBUFFER_COMPLETE == err, "glCheckFramebufferStatus failed 0x%08x", err);
			BX_UNUSED(err);

			makeCurrent();

			GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
			GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );

			swapBuffers();

			GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );

			swapBuffers();
		}

		void makeCurrent()
		{
			[EAGLContext setCurrentContext:m_context];
			GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );

			GLint newWidth = m_layer.bounds.size.width*[UIScreen mainScreen].scale;
			GLint newHeight = m_layer.bounds.size.height*[UIScreen mainScreen].scale;
			resize(newWidth, newHeight);
		}

		void resize(GLint _width, GLint _height)
		{
			if (m_width  == _width
			&&  m_height == _height)
			{
				return;
			}

			destroyFrameBuffers();

			m_width = _width;
			m_height = _height;

			createFrameBuffers(m_width, m_height);
		}

		void swapBuffers()
		{
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );
			[m_context presentRenderbuffer:GL_RENDERBUFFER];
		}

		EAGLContext* m_context;

		CAEAGLLayer *m_layer;
		GLuint m_fbo;
		GLuint m_colorRbo;
		GLuint m_depthStencilRbo;
		GLint m_width;
		GLint m_height;
	};

	void GlContext::create(uint32_t _width, uint32_t _height)
	{
		s_opengles = bx::dlopen("/System/Library/Frameworks/OpenGLES.framework/OpenGLES");
		BX_CHECK(NULL != s_opengles, "OpenGLES dynamic library is not found!");

		BX_UNUSED(_width, _height);
		CAEAGLLayer* layer = (__bridge CAEAGLLayer*)g_platformData.nwh;
		layer.opaque = [layer.style valueForKey:@"opaque"] == nil ? true : [[layer.style valueForKey:@"opaque"] boolValue];

		layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys
			: [NSNumber numberWithBool:false]
			, kEAGLDrawablePropertyRetainedBacking
			, kEAGLColorFormatRGBA8
			, kEAGLDrawablePropertyColorFormat
			, nil
			];

		EAGLContext* context = (__bridge EAGLContext*)g_platformData.context;
		if (NULL == context)
		{
			context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
			if (NULL == context)
			{
				context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
			}
		}
		BX_CHECK(NULL != context, "No valid OpenGLES context.");

		m_context = (__bridge void*)context;
		[EAGLContext setCurrentContext:context];
		[CATransaction flush];

		GL_CHECK(glGenFramebuffers(1, &m_fbo) );
		GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );

		GL_CHECK(glGenRenderbuffers(1, &m_colorRbo) );
		GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );

		[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRbo) );

		GLint width;
		GLint height;
		GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width) );
		GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height) );
		BX_TRACE("Screen size: %d x %d", width, height);

		GL_CHECK(glGenRenderbuffers(1, &m_depthStencilRbo) );
		GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilRbo) );
		GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height) );
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );

		BX_CHECK(GL_FRAMEBUFFER_COMPLETE ==  glCheckFramebufferStatus(GL_FRAMEBUFFER)
			, "glCheckFramebufferStatus failed 0x%08x"
			, glCheckFramebufferStatus(GL_FRAMEBUFFER)
			);

		makeCurrent();
		GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
		GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
		swap(NULL);
		GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
		swap(NULL);

		import();

		g_internalData.context = m_context;
	}

	void GlContext::destroy()
	{
		if (0 != m_fbo)
		{
			GL_CHECK(glDeleteFramebuffers(1, &m_fbo) );
			m_fbo = 0;
		}

		if (0 != m_colorRbo)
		{
			GL_CHECK(glDeleteRenderbuffers(1, &m_colorRbo) );
			m_colorRbo = 0;
		}

		if (0 != m_depthStencilRbo)
		{
			GL_CHECK(glDeleteRenderbuffers(1, &m_depthStencilRbo) );
			m_depthStencilRbo = 0;
		}

		EAGLContext* context = (__bridge EAGLContext*)m_context;

		bx::dlclose(s_opengles);
	}

	void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
	{
		BX_UNUSED(_width, _height, _flags);
		BX_TRACE("resize context");

		if (0 != m_fbo)
		{
			GL_CHECK(glDeleteFramebuffers(1, &m_fbo) );
			m_fbo = 0;
		}

		if (0 != m_colorRbo)
		{
			GL_CHECK(glDeleteRenderbuffers(1, &m_colorRbo) );
			m_colorRbo = 0;
		}

		if (0 != m_depthStencilRbo)
		{
			GL_CHECK(glDeleteRenderbuffers(1, &m_depthStencilRbo) );
			m_depthStencilRbo = 0;
		}

		GL_CHECK(glGenFramebuffers(1, &m_fbo) );
		GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );

		GL_CHECK(glGenRenderbuffers(1, &m_colorRbo) );
		GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );

		[((__bridge EAGLContext*)m_context) renderbufferStorage:GL_RENDERBUFFER fromDrawable:(__bridge CAEAGLLayer*)g_platformData.nwh];
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRbo) );

		GLint width;
		GLint height;
		GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width) );
		GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height) );
		BX_TRACE("Screen size: %d x %d", width, height);

		GL_CHECK(glGenRenderbuffers(1, &m_depthStencilRbo) );
		GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilRbo) );
		GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height) );
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );
		GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRbo) );

		BX_CHECK(GL_FRAMEBUFFER_COMPLETE ==  glCheckFramebufferStatus(GL_FRAMEBUFFER)
			, "glCheckFramebufferStatus failed 0x%08x"
			, glCheckFramebufferStatus(GL_FRAMEBUFFER)
			);
	}

	uint64_t GlContext::getCaps() const
	{
		return BGFX_CAPS_SWAP_CHAIN;
	}

	SwapChainGL* GlContext::createSwapChain(void* _nwh)
	{
		return BX_NEW(g_allocator, SwapChainGL)(/*m_display, m_config,*/ (__bridge EAGLContext*)m_context, (__bridge CAEAGLLayer*)_nwh);
	}

	void GlContext::destroySwapChain(SwapChainGL* _swapChain)
	{
		BX_DELETE(g_allocator, _swapChain);
	}

	void GlContext::swap(SwapChainGL* _swapChain)
	{
		makeCurrent(_swapChain);

		if (NULL == _swapChain)
		{
			GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );
			EAGLContext* context = (__bridge EAGLContext*)m_context;
			[context presentRenderbuffer:GL_RENDERBUFFER];
		}
		else
		{
		    _swapChain->swapBuffers();
		}
	}

	void GlContext::makeCurrent(SwapChainGL* _swapChain)
	{
		if (m_current != _swapChain)
		{
			m_current = _swapChain;

			if (NULL == _swapChain)
			{
				[EAGLContext setCurrentContext:(__bridge EAGLContext*)m_context];
				GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );
			}
			else
			{
				_swapChain->makeCurrent();
			}
		}
	}

	void GlContext::import()
	{
		BX_TRACE("Import:");
#	define GL_EXTENSION(_optional, _proto, _func, _import)                        \
		{                                                                         \
			if (_func == NULL)                                                    \
			{                                                                     \
				_func = (_proto)bx::dlsym(s_opengles, #_import);                  \
				BX_TRACE("%p " #_func " (" #_import ")", _func);                  \
			}                                                                     \
			BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize      \
				, "Failed to create OpenGLES context. EAGLGetProcAddress(\"%s\")" \
				, #_import);                                                      \
		}
#	include "glimports.h"
	}

} /* namespace gl */ } // namespace bgfx

#endif // BX_PLATFORM_IOS && (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)