Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libhtml5_webgl.js
4150 views
1
/**
2
* @license
3
* Copyright 2014 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
var LibraryHtml5WebGL = {
8
// Writes a JS typed array containing 32-bit floats or ints to memory
9
$writeGLArray: (arr, dst, dstLength, heapType) => {
10
#if ASSERTIONS
11
assert(arr);
12
assert(typeof arr.length != 'undefined');
13
#endif
14
var len = arr.length;
15
var writeLength = dstLength < len ? dstLength : len;
16
var heap = heapType ? HEAPF32 : HEAP32;
17
// Works because HEAPF32 and HEAP32 have the same bytes-per-element
18
dst = {{{ getHeapOffset('dst', 'float') }}};
19
for (var i = 0; i < writeLength; ++i) {
20
heap[dst + i] = arr[i];
21
}
22
return len;
23
},
24
25
$webglPowerPreferences__internal: true,
26
$webglPowerPreferences: ['default', 'low-power', 'high-performance'],
27
28
#if PTHREADS && OFFSCREEN_FRAMEBUFFER
29
// In offscreen framebuffer mode, we implement a proxied version of the
30
// emscripten_webgl_create_context() function in JS.
31
emscripten_webgl_create_context_proxied__proxy: 'sync',
32
emscripten_webgl_create_context_proxied__deps: ['emscripten_webgl_do_create_context'],
33
emscripten_webgl_create_context_proxied: (target, attributes) =>
34
_emscripten_webgl_do_create_context(target, attributes),
35
36
// The other proxied GL commands are defined in C (guarded by the
37
// __EMSCRIPTEN_OFFSCREEN_FRAMEBUFFER__ definition).
38
#else
39
// When not in offscreen framebuffer mode, these functions are implemented
40
// in JS and forwarded without any proxying.
41
emscripten_webgl_create_context: 'emscripten_webgl_do_create_context',
42
43
emscripten_webgl_get_current_context: 'emscripten_webgl_do_get_current_context',
44
45
emscripten_webgl_commit_frame: 'emscripten_webgl_do_commit_frame',
46
#endif
47
48
#if OFFSCREENCANVAS_SUPPORT
49
emscripten_webgl_do_create_context__postset: `
50
registerPreMainLoop(() => {
51
// If the current GL context is an OffscreenCanvas, but it was initialized
52
// with implicit swap mode, perform the swap on behalf of the user.
53
if (GL.currentContext && !GL.currentContextIsProxied && !GL.currentContext.attributes.explicitSwapControl && GL.currentContext.GLctx.commit) {
54
GL.currentContext.GLctx.commit();
55
}
56
});`,
57
#endif
58
59
emscripten_webgl_do_create_context__deps: [
60
#if OFFSCREENCANVAS_SUPPORT
61
'$registerPreMainLoop',
62
'malloc',
63
'emscripten_supports_offscreencanvas',
64
#endif
65
#if PTHREADS && OFFSCREEN_FRAMEBUFFER
66
'emscripten_webgl_create_context_proxied',
67
#endif
68
'$webglPowerPreferences', '$findCanvasEventTarget'],
69
// This function performs proxying manually, depending on the style of context that is to be created.
70
emscripten_webgl_do_create_context: (target, attributes) => {
71
#if ASSERTIONS
72
assert(attributes);
73
#endif
74
var attr32 = {{{ getHeapOffset('attributes', 'i32') }}};
75
var powerPreference = HEAP32[attr32 + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.powerPreference }}}>>2)];
76
var contextAttributes = {
77
'alpha': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.alpha }}}],
78
'depth': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.depth }}}],
79
'stencil': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.stencil }}}],
80
'antialias': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.antialias }}}],
81
'premultipliedAlpha': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha }}}],
82
'preserveDrawingBuffer': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer }}}],
83
'powerPreference': webglPowerPreferences[powerPreference],
84
'failIfMajorPerformanceCaveat': !!HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.failIfMajorPerformanceCaveat }}}],
85
// The following are not predefined WebGL context attributes in the WebGL specification, so the property names can be minified by Closure.
86
majorVersion: HEAP32[attr32 + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion }}}>>2)],
87
minorVersion: HEAP32[attr32 + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.minorVersion }}}>>2)],
88
enableExtensionsByDefault: HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault }}}],
89
explicitSwapControl: HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl }}}],
90
proxyContextToMainThread: HEAP32[attr32 + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.proxyContextToMainThread }}}>>2)],
91
renderViaOffscreenBackBuffer: HEAP8[attributes + {{{ C_STRUCTS.EmscriptenWebGLContextAttributes.renderViaOffscreenBackBuffer }}}]
92
};
93
94
95
#if ASSERTIONS
96
// TODO: Make these into hard errors at some point in the future
97
if (contextAttributes.majorVersion !== 1 && contextAttributes.majorVersion !== 2) {
98
err(`Invalid WebGL version requested: ${contextAttributes.majorVersion}`);
99
}
100
#if MIN_WEBGL_VERSION >= 2
101
if (contextAttributes.majorVersion !== 2) {
102
err('WebGL 1 requested but only WebGL 2 is supported (MIN_WEBGL_VERSION is 2)');
103
}
104
#elif MAX_WEBGL_VERSION == 1
105
if (contextAttributes.majorVersion !== 1) {
106
err('WebGL 2 requested but only WebGL 1 is supported (set -sMAX_WEBGL_VERSION=2 to fix the problem)');
107
}
108
#endif
109
#endif
110
111
var canvas = findCanvasEventTarget(target);
112
#if OFFSCREENCANVAS_SUPPORT
113
// If our canvas from findCanvasEventTarget is actually an offscreen canvas record, we should extract the inner canvas.
114
if (canvas?.canvas) { canvas = canvas.canvas; }
115
#endif
116
#if GL_DEBUG
117
var targetStr = UTF8ToString(target);
118
#endif
119
120
#if PTHREADS && OFFSCREEN_FRAMEBUFFER
121
// Create a WebGL context that is proxied to main thread if canvas was not found on worker, or if explicitly requested to do so.
122
if (ENVIRONMENT_IS_PTHREAD) {
123
if (contextAttributes.proxyContextToMainThread === {{{ cDefs.EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS }}} ||
124
(!canvas && contextAttributes.proxyContextToMainThread === {{{ cDefs.EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK }}})) {
125
// When WebGL context is being proxied via the main thread, we must render using an offscreen FBO render target to avoid WebGL's
126
// "implicit swap when callback exits" behavior. TODO: If OffscreenCanvas is supported, explicitSwapControl=true and still proxying,
127
// then this can be avoided, since OffscreenCanvas enables explicit swap control.
128
#if GL_DEBUG
129
if (contextAttributes.proxyContextToMainThread === {{{ cDefs.EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS }}}) dbg('EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS enabled, proxying WebGL rendering from pthread to main thread.');
130
if (!canvas && contextAttributes.proxyContextToMainThread === {{{ cDefs.EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK }}}) dbg(`Specified canvas target "${targetStr}" is not an OffscreenCanvas in the current pthread, but EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK is set. Proxying WebGL rendering from pthread to main thread.`);
131
dbg('Performance warning: forcing renderViaOffscreenBackBuffer=true and preserveDrawingBuffer=true since proxying WebGL rendering.');
132
#endif
133
// We will be proxying - if OffscreenCanvas is supported, we can proxy a bit more efficiently by avoiding having to create an Offscreen FBO.
134
if (!_emscripten_supports_offscreencanvas()) {
135
{{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.renderViaOffscreenBackBuffer, '1', 'i8') }}};
136
{{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer, '1', 'i8') }}};
137
}
138
return _emscripten_webgl_create_context_proxied(target, attributes);
139
}
140
}
141
#endif
142
143
if (!canvas) {
144
#if GL_DEBUG
145
dbg(`emscripten_webgl_create_context failed: Unknown canvas target "${targetStr}"!`);
146
#endif
147
return 0;
148
}
149
150
#if OFFSCREENCANVAS_SUPPORT
151
if (canvas.offscreenCanvas) canvas = canvas.offscreenCanvas;
152
153
#if GL_DEBUG
154
if (_emscripten_supports_offscreencanvas() && canvas instanceof OffscreenCanvas) dbg(`emscripten_webgl_create_context: Creating an OffscreenCanvas-based WebGL context on target "${targetStr}"`);
155
else if (typeof HTMLCanvasElement != 'undefined' && canvas instanceof HTMLCanvasElement) dbg(`emscripten_webgl_create_context: Creating an HTMLCanvasElement-based WebGL context on target "${targetStr}"`);
156
#endif
157
158
if (contextAttributes.explicitSwapControl) {
159
var supportsOffscreenCanvas = canvas.transferControlToOffscreen || (_emscripten_supports_offscreencanvas() && canvas instanceof OffscreenCanvas);
160
161
if (!supportsOffscreenCanvas) {
162
#if OFFSCREEN_FRAMEBUFFER
163
if (!contextAttributes.renderViaOffscreenBackBuffer) {
164
contextAttributes.renderViaOffscreenBackBuffer = true;
165
#if GL_DEBUG
166
dbg('emscripten_webgl_create_context: Performance warning, OffscreenCanvas is not supported but explicitSwapControl was requested, so force-enabling renderViaOffscreenBackBuffer=true to allow explicit swapping!');
167
#endif
168
}
169
#else
170
#if GL_DEBUG
171
dbg('emscripten_webgl_create_context failed: OffscreenCanvas is not supported but explicitSwapControl was requested!');
172
#endif
173
return 0;
174
#endif
175
}
176
177
if (canvas.transferControlToOffscreen) {
178
#if GL_DEBUG
179
dbg(`explicitSwapControl requested: canvas.transferControlToOffscreen() on canvas "${targetStr}" to get .commit() function and not rely on implicit WebGL swap`);
180
#endif
181
if (!canvas.controlTransferredOffscreen) {
182
GL.offscreenCanvases[canvas.id] = {
183
canvas: canvas.transferControlToOffscreen(),
184
canvasSharedPtr: _malloc(12),
185
id: canvas.id
186
};
187
canvas.controlTransferredOffscreen = true;
188
} else if (!GL.offscreenCanvases[canvas.id]) {
189
#if GL_DEBUG
190
dbg(`OffscreenCanvas is supported, and canvas "${canvas.id}" has already before been transferred offscreen, but there is no known OffscreenCanvas with that name!`);
191
#endif
192
return 0;
193
}
194
canvas = GL.offscreenCanvases[canvas.id].canvas;
195
}
196
}
197
#else // !OFFSCREENCANVAS_SUPPORT
198
#if OFFSCREEN_FRAMEBUFFER
199
if (contextAttributes.explicitSwapControl && !contextAttributes.renderViaOffscreenBackBuffer) {
200
contextAttributes.renderViaOffscreenBackBuffer = true;
201
#if GL_DEBUG
202
dbg('emscripten_webgl_create_context: Performance warning, not building with OffscreenCanvas support enabled but explicitSwapControl was requested, so force-enabling renderViaOffscreenBackBuffer=true to allow explicit swapping!');
203
#endif
204
}
205
#else
206
if (contextAttributes.explicitSwapControl) {
207
#if GL_DEBUG
208
dbg('emscripten_webgl_create_context failed: explicitSwapControl is not supported, please rebuild with -sOFFSCREENCANVAS_SUPPORT to enable targeting the experimental OffscreenCanvas specification, or rebuild with -sOFFSCREEN_FRAMEBUFFER to emulate explicitSwapControl in the absence of OffscreenCanvas support!');
209
#endif
210
return 0;
211
}
212
#endif // ~!OFFSCREEN_FRAMEBUFFER
213
214
#endif // ~!OFFSCREENCANVAS_SUPPORT
215
216
var contextHandle = GL.createContext(canvas, contextAttributes);
217
return contextHandle;
218
},
219
220
#if PTHREADS && OFFSCREEN_FRAMEBUFFER
221
// Runs on the calling thread, proxies if needed.
222
emscripten_webgl_make_context_current_calling_thread__sig: 'ip',
223
emscripten_webgl_make_context_current_calling_thread: (contextHandle) => {
224
var success = GL.makeContextCurrent(contextHandle);
225
if (success) GL.currentContextIsProxied = false; // If succeeded above, we will have a local GL context from this thread (worker or main).
226
return success ? {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}} : {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
227
},
228
// This function gets called in a pthread, after it has successfully activated (with make_current()) a proxied GL context to itself from the main thread.
229
// In this scenario, the pthread does not hold a high-level JS object to the GL context, because it lives on the main thread, in which case we record
230
// an integer pointer as a token value to represent the GL context activation from another thread. (when this function is called, the main browser thread
231
// has already accepted the GL context activation for our pthread, so that side is good)
232
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
233
_emscripten_proxied_gl_context_activated_from_main_browser_thread__deps: ['$registerPreMainLoop'],
234
_emscripten_proxied_gl_context_activated_from_main_browser_thread__postjs: `
235
// If the current GL context is a proxied regular WebGL context, and was
236
// initialized with implicit swap mode on the main thread, and we are on the
237
// parent thread, perform the swap on behalf of the user.
238
registerPreMainLoop(() => {
239
if (GL.currentContext && GL.currentContextIsProxied) {
240
var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i32') }}};
241
if (!explicitSwapControl) _emscripten_webgl_commit_frame();
242
}
243
});`,
244
#endif
245
_emscripten_proxied_gl_context_activated_from_main_browser_thread: (contextHandle) => {
246
GLctx = Module['ctx'] = GL.currentContext = contextHandle;
247
GL.currentContextIsProxied = true;
248
},
249
#else
250
emscripten_webgl_make_context_current: (contextHandle) => {
251
var success = GL.makeContextCurrent(contextHandle);
252
return success ? {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}} : {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
253
},
254
#endif
255
256
emscripten_webgl_do_get_current_context: () => GL.currentContext ? GL.currentContext.handle : 0,
257
258
emscripten_webgl_get_drawing_buffer_size__proxy: 'sync_on_webgl_context_handle_thread',
259
emscripten_webgl_get_drawing_buffer_size: (contextHandle, width, height) => {
260
var GLContext = GL.getContext(contextHandle);
261
262
if (!GLContext || !GLContext.GLctx || !width || !height) {
263
return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
264
}
265
{{{ makeSetValue('width', '0', 'GLContext.GLctx.drawingBufferWidth', 'i32') }}};
266
{{{ makeSetValue('height', '0', 'GLContext.GLctx.drawingBufferHeight', 'i32') }}};
267
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
268
},
269
270
emscripten_webgl_do_commit_frame: () => {
271
#if TRACE_WEBGL_CALLS
272
var threadId = (typeof _pthread_self != 'undefined') ? _pthread_self : () => 1;
273
err(`[Thread ${threadId()}, GL ctx: ${GL.currentContext.handle}]: emscripten_webgl_do_commit_frame()`);
274
#endif
275
if (!GL.currentContext || !GL.currentContext.GLctx) {
276
#if GL_DEBUG
277
dbg('emscripten_webgl_commit_frame() failed: no GL context set current via emscripten_webgl_make_context_current()!');
278
#endif
279
return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_TARGET }}};
280
}
281
282
#if OFFSCREEN_FRAMEBUFFER
283
if (GL.currentContext.defaultFbo) {
284
GL.blitOffscreenFramebuffer(GL.currentContext);
285
#if GL_DEBUG && OFFSCREENCANVAS_SUPPORT
286
if (GL.currentContext.GLctx.commit) dbg('emscripten_webgl_commit_frame(): Offscreen framebuffer should never have gotten created when canvas is in OffscreenCanvas mode, since it is redundant and not necessary');
287
#endif
288
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
289
}
290
#endif
291
if (!GL.currentContext.attributes.explicitSwapControl) {
292
#if GL_DEBUG
293
dbg('emscripten_webgl_commit_frame() cannot be called for canvases with implicit swap control mode!');
294
#endif
295
return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_TARGET }}};
296
}
297
// We would do GL.currentContext.GLctx.commit(); here, but the current implementation
298
// in browsers has removed it - swap is implicit, so this function is a no-op for now
299
// (until/unless the spec changes).
300
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
301
},
302
303
emscripten_webgl_get_context_attributes__proxy: 'sync_on_webgl_context_handle_thread',
304
emscripten_webgl_get_context_attributes__deps: ['$webglPowerPreferences'],
305
emscripten_webgl_get_context_attributes: (c, a) => {
306
if (!a) return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
307
c = GL.contexts[c];
308
if (!c) return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_TARGET }}};
309
var t = c.GLctx;
310
if (!t) return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_TARGET }}};
311
t = t.getContextAttributes();
312
313
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.alpha, 't.alpha', 'i8') }}};
314
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.depth, 't.depth', 'i8') }}};
315
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.stencil, 't.stencil', 'i8') }}};
316
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.antialias, 't.antialias', 'i8') }}};
317
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha, 't.premultipliedAlpha', 'i8') }}};
318
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer, 't.preserveDrawingBuffer', 'i8') }}};
319
var power = t['powerPreference'] && webglPowerPreferences.indexOf(t['powerPreference']);
320
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.powerPreference, 'power', 'i32') }}};
321
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.failIfMajorPerformanceCaveat, 't.failIfMajorPerformanceCaveat', 'i8') }}};
322
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion, 'c.version', 'i32') }}};
323
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.minorVersion, 0, 'i32') }}};
324
#if GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS
325
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault, 'c.attributes.enableExtensionsByDefault', 'i8') }}};
326
#endif
327
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
328
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'c.attributes.explicitSwapControl', 'i8') }}};
329
#endif
330
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
331
},
332
333
emscripten_webgl_destroy_context__proxy: 'sync_on_webgl_context_handle_thread',
334
emscripten_webgl_destroy_context: (contextHandle) => {
335
if (GL.currentContext == contextHandle) GL.currentContext = 0;
336
GL.deleteContext(contextHandle);
337
},
338
339
#if PTHREADS
340
// Special function that will be invoked on the thread calling emscripten_webgl_destroy_context(), before routing
341
// the call over to the target thread.
342
$emscripten_webgl_destroy_context_before_on_calling_thread__deps: ['emscripten_webgl_get_current_context', 'emscripten_webgl_make_context_current'],
343
$emscripten_webgl_destroy_context_before_on_calling_thread: (contextHandle) => {
344
if (_emscripten_webgl_get_current_context() == contextHandle) _emscripten_webgl_make_context_current(0);
345
},
346
#endif
347
348
emscripten_webgl_enable_extension__deps: [
349
#if GL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS
350
#if MIN_WEBGL_VERSION == 1
351
'$webgl_enable_ANGLE_instanced_arrays',
352
'$webgl_enable_OES_vertex_array_object',
353
'$webgl_enable_WEBGL_draw_buffers',
354
#endif
355
#if MAX_WEBGL_VERSION >= 2
356
'$webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance',
357
'$webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance',
358
#endif
359
'$webgl_enable_EXT_polygon_offset_clamp',
360
'$webgl_enable_EXT_clip_control',
361
'$webgl_enable_WEBGL_polygon_mode',
362
'$webgl_enable_WEBGL_multi_draw',
363
#endif
364
],
365
emscripten_webgl_enable_extension__proxy: 'sync_on_webgl_context_handle_thread',
366
emscripten_webgl_enable_extension: (contextHandle, extension) => {
367
var context = GL.getContext(contextHandle);
368
var extString = UTF8ToString(extension);
369
#if GL_EXTENSIONS_IN_PREFIXED_FORMAT
370
if (extString.startsWith('GL_')) extString = extString.slice(3); // Allow enabling extensions both with "GL_" prefix and without.
371
#endif
372
373
#if GL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS
374
// Switch-board that pulls in code for all GL extensions, even if those are not used :/
375
// Build with -sGL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS=0 to avoid this.
376
377
#if MIN_WEBGL_VERSION == 1
378
// Obtain function entry points to WebGL 1 extension related functions.
379
if (extString == 'ANGLE_instanced_arrays') webgl_enable_ANGLE_instanced_arrays(GLctx);
380
if (extString == 'OES_vertex_array_object') webgl_enable_OES_vertex_array_object(GLctx);
381
if (extString == 'WEBGL_draw_buffers') webgl_enable_WEBGL_draw_buffers(GLctx);
382
#endif
383
384
#if MAX_WEBGL_VERSION >= 2
385
if (extString == 'WEBGL_draw_instanced_base_vertex_base_instance') webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance(GLctx);
386
if (extString == 'WEBGL_multi_draw_instanced_base_vertex_base_instance') webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance(GLctx);
387
#endif
388
389
if (extString == 'WEBGL_multi_draw') webgl_enable_WEBGL_multi_draw(GLctx);
390
if (extString == 'EXT_polygon_offset_clamp') webgl_enable_EXT_polygon_offset_clamp(GLctx);
391
if (extString == 'EXT_clip_control') webgl_enable_EXT_clip_control(GLctx);
392
if (extString == 'WEBGL_polygon_mode') webgl_enable_WEBGL_polygon_mode(GLctx);
393
394
#elif ASSERTIONS || GL_ASSERTIONS
395
if (['ANGLE_instanced_arrays',
396
'OES_vertex_array_object',
397
'WEBGL_draw_buffers',
398
'WEBGL_multi_draw',
399
'EXT_polygon_offset_clamp',
400
'EXT_clip_control',
401
'WEBGL_polygon_mode',
402
'WEBGL_draw_instanced_base_vertex_base_instance',
403
'WEBGL_multi_draw_instanced_base_vertex_base_instance'].includes(extString)) {
404
err('When building with -sGL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS=0, function emscripten_webgl_enable_extension() cannot be used to enable extension '
405
+ extString + '! Use one of the functions emscripten_webgl_enable_*() to enable it!');
406
}
407
#endif
408
409
var ext = context.GLctx.getExtension(extString);
410
return !!ext;
411
},
412
413
emscripten_supports_offscreencanvas: () =>
414
// TODO: Add a new build mode, e.g. OFFSCREENCANVAS_SUPPORT=2, which
415
// necessitates OffscreenCanvas support at build time, and "return 1;" here in that build mode.
416
#if OFFSCREENCANVAS_SUPPORT
417
typeof OffscreenCanvas != 'undefined'
418
#else
419
0
420
#endif
421
,
422
423
$registerWebGlEventCallback__deps: ['$JSEvents', '$findEventTarget'],
424
$registerWebGlEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
425
#if PTHREADS
426
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
427
#endif
428
429
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
430
target ||= Module['canvas'];
431
#endif
432
433
var webGlEventHandlerFunc = (e = event) => {
434
#if PTHREADS
435
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, 0, userData);
436
else
437
#endif
438
if ({{{ makeDynCall('iiii', 'callbackfunc') }}}(eventTypeId, 0, userData)) e.preventDefault();
439
};
440
441
var eventHandler = {
442
target: findEventTarget(target),
443
eventTypeString,
444
callbackfunc,
445
handlerFunc: webGlEventHandlerFunc,
446
useCapture
447
};
448
JSEvents.registerOrRemoveHandler(eventHandler);
449
},
450
451
emscripten_set_webglcontextlost_callback_on_thread__proxy: 'sync',
452
emscripten_set_webglcontextlost_callback_on_thread__deps: ['$registerWebGlEventCallback'],
453
emscripten_set_webglcontextlost_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
454
registerWebGlEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST }}}, "webglcontextlost", targetThread);
455
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
456
},
457
458
emscripten_set_webglcontextrestored_callback_on_thread__proxy: 'sync',
459
emscripten_set_webglcontextrestored_callback_on_thread__deps: ['$registerWebGlEventCallback'],
460
emscripten_set_webglcontextrestored_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
461
registerWebGlEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED }}}, "webglcontextrestored", targetThread);
462
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
463
},
464
465
emscripten_is_webgl_context_lost__proxy: 'sync_on_webgl_context_handle_thread',
466
emscripten_is_webgl_context_lost: (contextHandle) =>
467
!GL.contexts[contextHandle] || GL.contexts[contextHandle].GLctx.isContextLost(), // No context ~> lost context.
468
469
emscripten_webgl_get_supported_extensions__proxy: 'sync_on_current_webgl_context_thread',
470
emscripten_webgl_get_supported_extensions__deps: ['$stringToNewUTF8'],
471
// Here we report the full list of extensions supported by WebGL rather than
472
// using getEmscriptenSupportedExtensions which filters the list based on
473
// what is has explicit support in.
474
emscripten_webgl_get_supported_extensions: () =>
475
stringToNewUTF8(GLctx.getSupportedExtensions().join(' ')),
476
477
emscripten_webgl_get_program_parameter_d__proxy: 'sync_on_current_webgl_context_thread',
478
emscripten_webgl_get_program_parameter_d: (program, param) =>
479
GLctx.getProgramParameter(GL.programs[program], param),
480
481
emscripten_webgl_get_program_info_log_utf8__proxy: 'sync_on_current_webgl_context_thread',
482
emscripten_webgl_get_program_info_log_utf8__deps: ['$stringToNewUTF8'],
483
emscripten_webgl_get_program_info_log_utf8: (program) =>
484
stringToNewUTF8(GLctx.getProgramInfoLog(GL.programs[program])),
485
486
emscripten_webgl_get_shader_parameter_d__proxy: 'sync_on_current_webgl_context_thread',
487
emscripten_webgl_get_shader_parameter_d: (shader, param) =>
488
GLctx.getShaderParameter(GL.shaders[shader], param),
489
490
emscripten_webgl_get_shader_info_log_utf8__proxy: 'sync_on_current_webgl_context_thread',
491
emscripten_webgl_get_shader_info_log_utf8__deps: ['$stringToNewUTF8'],
492
emscripten_webgl_get_shader_info_log_utf8: (shader) =>
493
stringToNewUTF8(GLctx.getShaderInfoLog(GL.shaders[shader])),
494
495
emscripten_webgl_get_shader_source_utf8__proxy: 'sync_on_current_webgl_context_thread',
496
emscripten_webgl_get_shader_source_utf8__deps: ['$stringToNewUTF8'],
497
emscripten_webgl_get_shader_source_utf8: (shader) =>
498
stringToNewUTF8(GLctx.getShaderSource(GL.shaders[shader])),
499
500
emscripten_webgl_get_vertex_attrib_d__proxy: 'sync_on_current_webgl_context_thread',
501
emscripten_webgl_get_vertex_attrib_d: (index, param) =>
502
GLctx.getVertexAttrib(index, param),
503
504
emscripten_webgl_get_vertex_attrib_o__proxy: 'sync_on_current_webgl_context_thread',
505
emscripten_webgl_get_vertex_attrib_o: (index, param) => {
506
var obj = GLctx.getVertexAttrib(index, param);
507
return obj?.name;
508
},
509
510
emscripten_webgl_get_vertex_attrib_v__proxy: 'sync_on_current_webgl_context_thread',
511
emscripten_webgl_get_vertex_attrib_v__deps: ['$writeGLArray'],
512
emscripten_webgl_get_vertex_attrib_v: (index, param, dst, dstLength, dstType) =>
513
writeGLArray(GLctx.getVertexAttrib(index, param), dst, dstLength, dstType),
514
515
emscripten_webgl_get_uniform_d__proxy: 'sync_on_current_webgl_context_thread',
516
emscripten_webgl_get_uniform_d__deps: ['$webglGetUniformLocation'],
517
emscripten_webgl_get_uniform_d: (program, location) =>
518
GLctx.getUniform(GL.programs[program], webglGetUniformLocation(location)),
519
520
emscripten_webgl_get_uniform_v__proxy: 'sync_on_current_webgl_context_thread',
521
emscripten_webgl_get_uniform_v__deps: ['$writeGLArray', '$webglGetUniformLocation'],
522
emscripten_webgl_get_uniform_v: (program, location, dst, dstLength, dstType) =>
523
writeGLArray(GLctx.getUniform(GL.programs[program], webglGetUniformLocation(location)), dst, dstLength, dstType),
524
525
emscripten_webgl_get_parameter_v__proxy: 'sync_on_current_webgl_context_thread',
526
emscripten_webgl_get_parameter_v__deps: ['$writeGLArray'],
527
emscripten_webgl_get_parameter_v: (param, dst, dstLength, dstType) =>
528
writeGLArray(GLctx.getParameter(param), dst, dstLength, dstType),
529
530
emscripten_webgl_get_parameter_d__proxy: 'sync_on_current_webgl_context_thread',
531
emscripten_webgl_get_parameter_d: (param) => GLctx.getParameter(param),
532
533
emscripten_webgl_get_parameter_o__proxy: 'sync_on_current_webgl_context_thread',
534
emscripten_webgl_get_parameter_o: (param) => {
535
var obj = GLctx.getParameter(param);
536
return obj?.name;
537
},
538
539
emscripten_webgl_get_parameter_utf8__deps: ['$stringToNewUTF8'],
540
emscripten_webgl_get_parameter_utf8__proxy: 'sync_on_current_webgl_context_thread',
541
emscripten_webgl_get_parameter_utf8: (param) => stringToNewUTF8(GLctx.getParameter(param)),
542
543
emscripten_webgl_get_parameter_i64v__proxy: 'sync_on_current_webgl_context_thread',
544
emscripten_webgl_get_parameter_i64v__deps: ['$writeI53ToI64'],
545
emscripten_webgl_get_parameter_i64v: (param, dst) => writeI53ToI64(dst, GLctx.getParameter(param)),
546
};
547
548
549
function handleWebGLProxying(funcs) {
550
#if PTHREADS
551
// Process 'sync_on_webgl_context_handle_thread' and
552
// 'sync_on_current_webgl_context_thread' pseudo-proxying modes to appropriate
553
// proxying mechanism, either proxying on-demand, unconditionally, or never,
554
// depending on build modes.
555
// 'sync_on_webgl_context_handle_thread' is used for function signatures that
556
// take a HTML5 WebGL context handle object as the first argument.
557
// 'sync_on_current_webgl_context_thread' is used for functions that operate on
558
// the implicit "current WebGL context" as activated via
559
// emscripten_webgl_make_current() function.
560
function listOfNFunctionArgs(func) {
561
const args = [];
562
for (var i = 0; i < func.length; ++i) {
563
args.push('p' + i);
564
}
565
return args;
566
}
567
568
const targetingOffscreenCanvas = {{{ OFFSCREENCANVAS_SUPPORT }}};
569
const targetingOffscreenFramebuffer = {{{ OFFSCREEN_FRAMEBUFFER }}};
570
571
for (const i in funcs) {
572
// Is this a function that takes GL context handle as first argument?
573
const proxyContextHandle = funcs[i + '__proxy'] == 'sync_on_webgl_context_handle_thread';
574
575
// Is this a function that operates on the implicit current GL context object?
576
const proxyCurrentContext = funcs[i + '__proxy'] == 'sync_on_current_webgl_context_thread';
577
578
if (!proxyContextHandle && !proxyCurrentContext) {
579
continue; // no resolving of pseudo-proxying needed for this function.
580
}
581
582
if (targetingOffscreenCanvas && (targetingOffscreenFramebuffer || proxyContextHandle)) {
583
// Dynamically check at runtime whether the current thread owns the GL context
584
// handle/current GL context object. If not, proxy the call to main thread.
585
// TODO: this handles the calling pthread and main thread cases, but not yet
586
// the case from pthread->pthread.
587
const sig = funcs[i + '__sig'] || LibraryManager.library[i + '__sig']
588
assert(sig);
589
funcs[i + '_calling_thread'] = funcs[i];
590
funcs[i + '_main_thread'] = i + '_calling_thread';
591
funcs[i + '_main_thread__proxy'] = 'sync';
592
funcs[i + '_main_thread__sig'] = sig;
593
funcs[i + '__deps'] ??= [];
594
funcs[i + '__deps'].push(i + '_calling_thread');
595
funcs[i + '__deps'].push(i + '_main_thread');
596
delete funcs[i + '__proxy'];
597
const funcArgs = listOfNFunctionArgs(funcs[i]);
598
const funcArgsString = funcArgs.join(',');
599
const retStatement = sig[0] != 'v' ? 'return' : '';
600
const contextCheck = proxyContextHandle ? 'GL.contexts[p0]' : 'GLctx';
601
var funcBody = `${retStatement} ${contextCheck} ? _${i}_calling_thread(${funcArgsString}) : _${i}_main_thread(${funcArgsString});`;
602
if (funcs[i + '_before_on_calling_thread']) {
603
funcs[i + '__deps'].push('$' + i + '_before_on_calling_thread');
604
funcBody = `${i}_before_on_calling_thread(${funcArgsString}); ` + funcBody;
605
}
606
funcs[i] = new Function(funcArgs, funcBody);
607
} else if (targetingOffscreenFramebuffer) {
608
// When targeting only OFFSCREEN_FRAMEBUFFER, unconditionally proxy all GL
609
// calls to main thread.
610
funcs[i + '__proxy'] = 'sync';
611
} else {
612
// Building without OFFSCREENCANVAS_SUPPORT or OFFSCREEN_FRAMEBUFFER; or building
613
// with OFFSCREENCANVAS_SUPPORT and no OFFSCREEN_FRAMEBUFFER: the application
614
// will only utilize WebGL in the main browser thread, and in the calling thread.
615
// Remove the WebGL proxying directives.
616
delete funcs[i + '__proxy'];
617
}
618
}
619
#else
620
// In single threaded mode just delete our custom __proxy addributes, otherwise
621
// they will causes errors in the JS compiler.
622
for (const i in funcs) {
623
delete funcs[i + '__proxy'];
624
}
625
#endif // PTHREADS
626
}
627
628
handleWebGLProxying(LibraryHtml5WebGL);
629
630
#if LibraryManager.has('libwebgl.js')
631
autoAddDeps(LibraryHtml5WebGL, '$GL');
632
#endif
633
634
addToLibrary(LibraryHtml5WebGL);
635
636