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
|
/*
* Copyright 2011-2022 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
#include "bgfx_p.h"
#if BGFX_CONFIG_RENDERER_OPENGLES
# include "renderer_gl.h"
# if BGFX_USE_HTML5
# include "emscripten.h"
// from emscripten gl.c, because we're not going to go
// through egl
extern "C" void* emscripten_GetProcAddress(const char *name_);
extern "C" void* emscripten_webgl1_get_proc_address(const char *name_);
extern "C" void* emscripten_webgl2_get_proc_address(const char *name_);
namespace bgfx { namespace gl
{
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
# include "glimports.h"
static EmscriptenWebGLContextAttributes s_attrs;
struct SwapChainGL
{
SwapChainGL(int _context, const char* _canvas)
: m_context(_context)
{
m_canvas = (char*)BX_ALLOC(g_allocator, strlen(_canvas) + 1);
strcpy(m_canvas, _canvas);
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();
}
~SwapChainGL()
{
EMSCRIPTEN_CHECK(emscripten_webgl_destroy_context(m_context) );
BX_FREE(g_allocator, m_canvas);
}
void makeCurrent()
{
EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(m_context) );
}
void swapBuffers()
{
// There is no swapBuffers equivalent. A swap happens whenever control is returned back
// to the browser.
}
int m_context;
char* m_canvas;
};
void GlContext::create(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
{
// assert?
if (m_primary != NULL)
return;
const char* canvas = (const char*) g_platformData.nwh;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE) g_platformData.context;
if (context > 0)
{
if (emscripten_webgl_get_context_attributes(context, &s_attrs) >= 0)
{
import(s_attrs.majorVersion);
m_primary = BX_NEW(g_allocator, SwapChainGL)(context, canvas);
}
else
{
BX_TRACE("Invalid WebGL context. (Canvas handle: '%s', context handle: %d)", canvas, context);
}
}
else
{
m_primary = createSwapChain((void*)canvas);
}
if (0 != _width
&& 0 != _height)
{
EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) );
}
makeCurrent(m_primary);
}
void GlContext::destroy()
{
if (m_primary)
{
if (m_current == m_primary)
{
m_current = NULL;
}
BX_DELETE(g_allocator, m_primary);
m_primary = NULL;
}
}
void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t /* _flags */)
{
if (m_primary == NULL)
{
return;
}
EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) );
}
SwapChainGL* GlContext::createSwapChain(void* _nwh)
{
emscripten_webgl_init_context_attributes(&s_attrs);
// Work around bug https://bugs.chromium.org/p/chromium/issues/detail?id=1045643 in Chrome
// by having alpha always enabled.
s_attrs.alpha = true;
s_attrs.premultipliedAlpha = false;
s_attrs.depth = true;
s_attrs.stencil = true;
s_attrs.enableExtensionsByDefault = true;
s_attrs.antialias = false;
s_attrs.minorVersion = 0;
const char* canvas = (const char*) _nwh;
int32_t error = 0;
for (int version = 2; version >= 1; --version)
{
s_attrs.majorVersion = version;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvas, &s_attrs);
if (context > 0)
{
EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(context) );
SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(context, canvas);
import(version);
return swapChain;
}
error = (int32_t)context;
}
BX_TRACE("Failed to create WebGL context. (Canvas handle: '%s', last attempt error %d)", canvas, error);
BX_UNUSED(error);
return NULL;
}
uint64_t GlContext::getCaps() const
{
return BGFX_CAPS_SWAP_CHAIN;
}
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
{
BX_DELETE(g_allocator, _swapChain);
}
void GlContext::swap(SwapChainGL* /* _swapChain */)
{
}
void GlContext::makeCurrent(SwapChainGL* _swapChain)
{
if (m_current != _swapChain)
{
m_current = _swapChain;
if (NULL == _swapChain)
{
if (NULL != m_primary)
{
m_primary->makeCurrent();
}
}
else
{
_swapChain->makeCurrent();
}
}
}
template<typename Fn>
static Fn getProcAddress(int _version, const char* _name)
{
Fn func = reinterpret_cast<Fn>(emscripten_webgl1_get_proc_address(_name) );
if (NULL == func && _version >= 2)
{
func = reinterpret_cast<Fn>(emscripten_webgl2_get_proc_address(_name) );
}
return func;
}
void GlContext::import(int _webGLVersion)
{
BX_TRACE("Import:");
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = getProcAddress<_proto>(_webGLVersion, #_import); \
BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize \
, "Failed to create WebGL/OpenGLES context. GetProcAddress(\"%s\")" \
, #_import \
); \
} \
}
# include "glimports.h"
# undef GL_EXTENSION
}
} /* namespace gl */ } // namespace bgfx
# endif // BGFX_USE_EGL
#endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
|