summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/src/glcontext_ppapi.cpp
blob: a4887103e63beff4d9e6959bc1f9033fb884807f (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
/*
 * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "bgfx_p.h"

#if BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
#	include <bgfx/bgfxplatform.h>
#	include "renderer_gl.h"

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

	void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/);

	PP_CompletionCallback naclSwapComplete =
	{
		naclSwapCompleteCb,
		NULL,
		PP_COMPLETIONCALLBACK_FLAG_NONE
	};

	struct Ppapi
	{
		Ppapi()
			: m_context(0)
			, m_instance(0)
			, m_instInterface(NULL)
			, m_graphicsInterface(NULL)
			, m_instancedArrays(NULL)
			, m_query(NULL)
			, m_postSwapBuffers(NULL)
			, m_forceSwap(true)
		{
		}

		bool setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers);

		void resize(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
		{
			m_graphicsInterface->ResizeBuffers(m_context, _width, _height);
		}

		void swap()
		{
			glSetCurrentContextPPAPI(m_context);
			m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
		}

		bool isValid() const
		{
			return 0 != m_context;
		}

		PP_Resource m_context;
		PP_Instance m_instance;
		const PPB_Instance* m_instInterface;
		const PPB_Graphics3D* m_graphicsInterface;
		const PPB_OpenGLES2InstancedArrays* m_instancedArrays;
		const PPB_OpenGLES2Query* m_query;
		PostSwapBuffersFn m_postSwapBuffers;
		bool m_forceSwap;
	};

	static Ppapi s_ppapi;

	void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
	{
		// For NaCl bgfx doesn't create render thread, but rendering is always
		// multithreaded. Frame rendering is done on main thread, and context
		// is initialized when PPAPI interfaces are set. Force swap is there to
		// keep calling swap complete callback, so that initialization doesn't
		// deadlock on semaphores.
		if (s_ppapi.m_forceSwap)
		{
			s_ppapi.swap();
		}

		renderFrame();
	}

	static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
	{
		s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor);
	}

	static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
	{
		s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount);
	}

	static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
	{
		s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount);
	}

	static void GL_APIENTRY naclGenQueries(GLsizei _n, GLuint* _queries)
	{
		s_ppapi.m_query->GenQueriesEXT(s_ppapi.m_context, _n, _queries);
	}

	static void GL_APIENTRY naclDeleteQueries(GLsizei _n, const GLuint* _queries)
	{
		s_ppapi.m_query->DeleteQueriesEXT(s_ppapi.m_context, _n, _queries);
	}

	static void GL_APIENTRY naclBeginQuery(GLenum _target, GLuint _id)
	{
		BX_UNUSED(_target);
		s_ppapi.m_query->BeginQueryEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, _id);
	}

	static void GL_APIENTRY naclEndQuery(GLenum _target)
	{
		BX_UNUSED(_target);
		s_ppapi.m_query->EndQueryEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT);
	}

	static void GL_APIENTRY naclGetQueryObjectiv(GLuint _id, GLenum _pname, GLint* _params)
	{
		BX_UNUSED(_id);
		BX_UNUSED(_pname);
		s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, _params);
	}

	static void GL_APIENTRY naclGetQueryObjectui64v(GLuint _id, GLenum _pname, GLuint64* _params)
	{
		BX_UNUSED(_id);
		BX_UNUSED(_pname);
		GLint params;
		s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, &params);
		*_params = params;
	}

	bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
	{
		BX_TRACE("PPAPI Interfaces");

		m_instance = _instance;
		m_instInterface = _instInterface;
		m_graphicsInterface = _graphicsInterface;
		m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
		m_query = glGetQueryInterfacePPAPI();
		m_postSwapBuffers = _postSwapBuffers;

		int32_t attribs[] =
		{
			PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
			PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
			PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
			PP_GRAPHICS3DATTRIB_SAMPLES, 0,
			PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
			PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
			PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
			PP_GRAPHICS3DATTRIB_NONE
		};

		m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
		if (0 == m_context)
		{
			BX_TRACE("Failed to create context!");
			return false;
		}

		m_instInterface->BindGraphics(m_instance, m_context);
		glSetCurrentContextPPAPI(m_context);
		m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);

		if (NULL != m_instancedArrays)
		{
			glVertexAttribDivisor   = naclVertexAttribDivisor;
			glDrawArraysInstanced   = naclDrawArraysInstanced;
			glDrawElementsInstanced = naclDrawElementsInstanced;
		}

		if (NULL != m_query)
		{
			glGenQueries          = naclGenQueries;
			glDeleteQueries       = naclDeleteQueries;
			glBeginQuery          = naclBeginQuery;
			glEndQuery            = naclEndQuery;
			glGetQueryObjectiv    = naclGetQueryObjectiv;
			glGetQueryObjectui64v = naclGetQueryObjectui64v;
		}

		// Prevent render thread creation.
		RenderFrame::Enum result = renderFrame();
		return RenderFrame::NoContext == result;
	}

	void GlContext::create(uint32_t _width, uint32_t _height)
	{
		BX_UNUSED(_width, _height);
		BX_TRACE("GlContext::create");

		g_internalData.context = &s_ppapi.m_context;
	}

	void GlContext::destroy()
	{
	}

	void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
	{
		s_ppapi.m_forceSwap = false;
		s_ppapi.resize(_width, _height, _flags);
	}

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

	SwapChainGL* GlContext::createSwapChain(void* /*_nwh*/)
	{
		BX_CHECK(false, "Shouldn't be called!");
		return NULL;
	}

	void GlContext::destroySwapChain(SwapChainGL*  /*_swapChain*/)
	{
		BX_CHECK(false, "Shouldn't be called!");
	}

	void GlContext::swap(SwapChainGL* /*_swapChain*/)
	{
		s_ppapi.swap();
	}

	void GlContext::makeCurrent(SwapChainGL* /*_swapChain*/)
	{
	}

	void GlContext::import()
	{
	}

	bool GlContext::isValid() const
	{
		return s_ppapi.isValid();
	}

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

namespace bgfx
{
	bool naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
	{
		return gl::s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
	}
} // namespace bgfx

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