Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions src/library_egl.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ var LibraryEGL = {
currentContext: 0 /* EGL_NO_CONTEXT */,
currentReadSurface: 0 /* EGL_NO_SURFACE */,
currentDrawSurface: 0 /* EGL_NO_SURFACE */,
alpha: false,
depth: true, /*not EGL default, compat with earlier emscripten*/
stencil: true, /*not EGL default, compat with earlier emscripten*/
antialias: true, /*not EGL default, compat with earlier emscripten*/

contextAttributes: {
alpha: false,
depth: false,
stencil: false,
antialias: false
},

stringCache: {},

Expand All @@ -46,19 +49,19 @@ var LibraryEGL = {
var param = {{{ makeGetValue('attribList', '0', 'i32') }}};
if (param == 0x3021 /*EGL_ALPHA_SIZE*/) {
var alphaSize = {{{ makeGetValue('attribList', '4', 'i32') }}};
EGL.alpha = (alphaSize > 0);
EGL.contextAttributes.alpha = (alphaSize > 0);
} else if (param == 0x3025 /*EGL_DEPTH_SIZE*/) {
var depthSize = {{{ makeGetValue('attribList', '4', 'i32') }}};
EGL.depth = (depthSize > 0);
EGL.contextAttributes.depth = (depthSize > 0);
} else if (param == 0x3026 /*EGL_STENCIL_SIZE*/) {
var stencilSize = {{{ makeGetValue('attribList', '4', 'i32') }}};
EGL.stencil = (stencilSize > 0);
EGL.contextAttributes.stencil = (stencilSize > 0);
} else if (param == 0x3031 /*EGL_SAMPLES*/) {
var samples = {{{ makeGetValue('attribList', '4', 'i32') }}};
EGL.antialias = (samples > 0);
EGL.contextAttributes.antialias = (samples > 0);
} else if (param == 0x3032 /*EGL_SAMPLE_BUFFERS*/) {
var samples = {{{ makeGetValue('attribList', '4', 'i32') }}};
EGL.antialias = (samples == 1);
EGL.contextAttributes.antialias = (samples == 1);
} else if (param == 0x3038 /*EGL_NONE*/) {
break;
}
Expand Down Expand Up @@ -159,10 +162,10 @@ var LibraryEGL = {
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);
switch(attribute) {
case 0x3020: // EGL_BUFFER_SIZE
{{{ makeSetValue('value', '0', '32' /* 8 bits for each A,R,G,B. */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.alpha ? 32 : 24' /* 8 bits for each R,G,B. 8 bits for alpha if enabled*/, 'i32') }}};
return 1;
case 0x3021: // EGL_ALPHA_SIZE
{{{ makeSetValue('value', '0', '8' /* 8 bits for alpha channel. */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.alpha ? 8 : 0' /* 8 bits for alpha channel if enabled. */, 'i32') }}};
return 1;
case 0x3022: // EGL_BLUE_SIZE
{{{ makeSetValue('value', '0', '8' /* 8 bits for blue channel. */, 'i32') }}};
Expand All @@ -174,10 +177,10 @@ var LibraryEGL = {
{{{ makeSetValue('value', '0', '8' /* 8 bits for red channel. */, 'i32') }}};
return 1;
case 0x3025: // EGL_DEPTH_SIZE
{{{ makeSetValue('value', '0', '24' /* 24 bits for depth buffer. TODO: This is hardcoded, add support for this! */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.depth ? 24 : 0' /* 24 bits for depth buffer if enabled. */, 'i32') }}};
return 1;
case 0x3026: // EGL_STENCIL_SIZE
{{{ makeSetValue('value', '0', '8' /* 8 bits for stencil buffer. TODO: This is hardcoded, add support for this! */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.stencil ? 8 : 0' /* 8 bits for stencil buffer if enabled. */, 'i32') }}};
return 1;
case 0x3027: // EGL_CONFIG_CAVEAT
// We can return here one of EGL_NONE (0x3038), EGL_SLOW_CONFIG (0x3050) or EGL_NON_CONFORMANT_CONFIG (0x3051).
Expand Down Expand Up @@ -208,10 +211,10 @@ var LibraryEGL = {
{{{ makeSetValue('value', '0', '0x3038' /* EGL_NONE */, 'i32') }}};
return 1;
case 0x3031: // EGL_SAMPLES
{{{ makeSetValue('value', '0', '4' /* 2x2 Multisampling */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.antialias ? 4 : 0' /* 2x2 Multisampling */, 'i32') }}};
return 1;
case 0x3032: // EGL_SAMPLE_BUFFERS
{{{ makeSetValue('value', '0', '1' /* Multisampling enabled */, 'i32') }}};
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.antialias ? 1 : 0' /* Multisampling enabled */, 'i32') }}};
return 1;
case 0x3033: // EGL_SURFACE_TYPE
{{{ makeSetValue('value', '0', '0x0004' /* EGL_WINDOW_BIT */, 'i32') }}};
Expand All @@ -231,6 +234,8 @@ var LibraryEGL = {
{{{ makeSetValue('value', '0', '0' /* Only pbuffers would be bindable, but these are not supported. */, 'i32') }}};
return 1;
case 0x303B: // EGL_MIN_SWAP_INTERVAL
{{{ makeSetValue('value', '0', '0', 'i32') }}};
return 1;
case 0x303C: // EGL_MAX_SWAP_INTERVAL
{{{ makeSetValue('value', '0', '1' /* TODO: Currently this is not strictly true, since user can specify custom presentation interval in JS requestAnimationFrame/emscripten_set_main_loop. */, 'i32') }}};
return 1;
Expand Down Expand Up @@ -294,7 +299,7 @@ var LibraryEGL = {
return 1; /* Magic ID for Emscripten 'default surface' */
},

eglCreateContext__deps: ['glutInitDisplayMode', 'glutCreateWindow', '$GL'],
eglCreateContext__deps: ['$GL'],

// EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
eglCreateContext: function(display, config, hmm, contextAttribs) {
Expand All @@ -319,35 +324,45 @@ var LibraryEGL = {
}
contextAttribs += 8;
}
#if USE_WEBGL2
if (glesContextVersion < 2 || glesContextVersion > 3) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does < 2 || > 3 differ from != 2?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows 3 now.

#else
if (glesContextVersion != 2) {
#endif
#if GL_ASSERTIONS
err('When initializing GLES2/WebGL1 via EGL, one must pass EGL_CONTEXT_CLIENT_VERSION = 2 to GL context attributes! GLES version ' + glesContextVersion + ' is not supported!');
if (glesContextVersion == 3) {
err('When initializing GLES3/WebGL2 via EGL, one must build with -s USE_WEBGL2=1 !');
} else {
err('When initializing GLES2/WebGL1 via EGL, one must pass EGL_CONTEXT_CLIENT_VERSION = 2 to GL context attributes! GLES version ' + glesContextVersion + ' is not supported!');
}
#endif
EGL.setErrorCode(0x3005 /* EGL_BAD_CONFIG */);
return 0; /* EGL_NO_CONTEXT */
}

// convert configuration to GLUT flags
var displayMode = 0x0000; /*GLUT_RGB/GLUT_RGBA*/
displayMode |= 0x0002; /*GLUT_DOUBLE*/
if (EGL.alpha) displayMode |= 0x0008; /*GLUT_ALPHA*/
if (EGL.depth) displayMode |= 0x0010; /*GLUT_DEPTH*/
if (EGL.stencil) displayMode |= 0x0020; /*GLUT_STENCIL*/
if (EGL.antialias) displayMode |= 0x0080; /*GLUT_MULTISAMPLE*/
EGL.contextAttributes.majorVersion = glesContextVersion - 1; // WebGL 1 is GLES 2, WebGL2 is GLES3
EGL.contextAttributes.minorVersion = 0;

_glutInitDisplayMode(displayMode);
EGL.windowID = _glutCreateWindow();
if (EGL.windowID != 0) {
EGL.context = GL.createContext(Module['canvas'], EGL.contextAttributes);

if (EGL.context != 0) {
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);

// Run callbacks so that GL emulation works
GL.makeContextCurrent(EGL.context);
Module.useWebGL = true;
Browser.moduleContextCreatedCallbacks.forEach(function(callback) { callback() });

// Note: This function only creates a context, but it shall not make it active.
GL.makeContextCurrent(null);
return 62004; // Magic ID for Emscripten EGLContext
} else {
EGL.setErrorCode(0x3009 /* EGL_BAD_MATCH */); // By the EGL 1.4 spec, an implementation that does not support GLES2 (WebGL in this case), this error code is set.
return 0; /* EGL_NO_CONTEXT */
}
},

eglDestroyContext__deps: ['glutDestroyWindow', '$GL'],
eglDestroyContext__deps: ['$GL'],

// EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext context);
eglDestroyContext: function(display, context) {
Expand All @@ -360,7 +375,7 @@ var LibraryEGL = {
return 0;
}

_glutDestroyWindow(EGL.windowID);
GL.destroyContext(EGL.context);
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);
if (EGL.currentContext == context) {
EGL.currentContext = 0;
Expand Down Expand Up @@ -474,7 +489,7 @@ var LibraryEGL = {
{{{ makeSetValue('value', '0', '0x30A0' /* EGL_OPENGL_ES_API */, 'i32') }}};
return 1;
case 0x3098: // EGL_CONTEXT_CLIENT_VERSION
{{{ makeSetValue('value', '0', '2' /* GLES2 context */, 'i32') }}}; // We always report the context to be a GLES2 context (and not a GLES1 context)
{{{ makeSetValue('value', '0', 'EGL.contextAttributes.majorVersion + 1', 'i32') }}};
return 1;
case 0x3086: // EGL_RENDER_BUFFER
// The context is bound to the visible canvas window - it's always backbuffered.
Expand Down Expand Up @@ -563,6 +578,7 @@ var LibraryEGL = {
},

// EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
eglMakeCurrent__deps: ['$GL'],
eglMakeCurrent: function(display, draw, read, context) {
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
Expand All @@ -577,6 +593,9 @@ var LibraryEGL = {
EGL.setErrorCode(0x300D /* EGL_BAD_SURFACE */);
return 0;
}

GL.makeContextCurrent(context ? EGL.context : null);

EGL.currentContext = context;
EGL.currentDrawSurface = draw;
EGL.currentReadSurface = read;
Expand Down
6 changes: 1 addition & 5 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2660,10 +2660,6 @@ def path_to_system_js_libraries(library_name):
if len(js_system_libraries[library_name]):
library_files += [js_system_libraries[library_name]]

# TODO: This is unintentional due to historical reasons. Improve EGL to use HTML5 API to avoid depending on GLUT.
if library_name == 'EGL':
library_files += ['library_glut.js']

elif library_name.endswith('.js') and os.path.isfile(path_from_root('src', 'library_' + library_name)):
library_files += ['library_' + library_name]
else:
Expand All @@ -2688,7 +2684,7 @@ def path_to_system_js_libraries_for_settings(link_settings):
if 'USE_SDL=1' in link_settings:
system_js_libraries += ['library_sdl.js']
if 'USE_SDL=2' in link_settings:
system_js_libraries += ['library_egl.js', 'library_glut.js', 'library_gl.js']
system_js_libraries += ['library_egl.js', 'library_gl.js']
return [path_from_root('src', x) for x in system_js_libraries]

@staticmethod
Expand Down