Skip to content

Commit

Permalink
Merge pull request #65644 from Faless/web/4.x_misc_alpha_fixes
Browse files Browse the repository at this point in the history
[Web] Small fixes and enhancements.
  • Loading branch information
Faless authored Sep 11, 2022
2 parents c658fa8 + 27f22b2 commit 200b9cd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions platform/web/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,7 @@ def configure(env):

# Add code that allow exiting runtime.
env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"])

# This workaround creates a closure that prevents the garbage collector from freeing the WebGL context.
# We also only use WebGL2, and changing context version is not widely supported anyway.
env.Append(LINKFLAGS=["-s", "GL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])
4 changes: 2 additions & 2 deletions platform/web/display_server_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,10 @@ DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode
if (wants_webgl2 && !webgl2_init_failed) {
EmscriptenWebGLContextAttributes attributes;
emscripten_webgl_init_context_attributes(&attributes);
//attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
attributes.alpha = true;
attributes.alpha = OS::get_singleton()->is_layered_allowed();
attributes.antialias = false;
attributes.majorVersion = 2;
attributes.explicitSwapControl = true;

webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
Expand Down
3 changes: 2 additions & 1 deletion platform/web/js/engine/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
if (!(this.canvas instanceof HTMLCanvasElement)) {
const nodes = document.getElementsByTagName('canvas');
if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
this.canvas = nodes[0];
const first = nodes[0];
this.canvas = /** @type {!HTMLCanvasElement} */ (first);
}
if (!this.canvas) {
throw new Error('No canvas found in page');
Expand Down
2 changes: 2 additions & 0 deletions platform/web/js/libs/audio.worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class GodotProcessor extends AudioWorkletProcessor {
this.running = false;
this.output = null;
this.input = null;
this.lock = null;
this.notifier = null;
} else if (p_cmd === 'start_nothreads') {
this.output = new RingBuffer(p_data[0], p_data[0].length, false);
} else if (p_cmd === 'chunk') {
Expand Down
9 changes: 7 additions & 2 deletions platform/web/js/libs/library_godot_audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,21 @@ const GodotAudioWorklet = {
if (GodotAudioWorklet.promise === null) {
return;
}
GodotAudioWorklet.promise.then(function () {
const p = GodotAudioWorklet.promise;
p.then(function () {
GodotAudioWorklet.worklet.port.postMessage({
'cmd': 'stop',
'data': null,
});
GodotAudioWorklet.worklet.disconnect();
GodotAudioWorklet.worklet.port.onmessage = null;
GodotAudioWorklet.worklet = null;
GodotAudioWorklet.promise = null;
resolve();
}).catch(function (err) { /* aborted? */ });
}).catch(function (err) {
// Aborted?
GodotRuntime.error(err);
});
});
},
},
Expand Down
14 changes: 9 additions & 5 deletions platform/web/js/libs/library_godot_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ autoAddDeps(GodotConfig, '$GodotConfig');
mergeInto(LibraryManager.library, GodotConfig);

const GodotFS = {
$GodotFS__deps: ['$ERRNO_CODES', '$FS', '$IDBFS', '$GodotRuntime'],
$GodotFS__deps: ['$FS', '$IDBFS', '$GodotRuntime'],
$GodotFS__postset: [
'Module["initFS"] = GodotFS.init;',
'Module["copyToFS"] = GodotFS.copy_to_fs;',
].join(''),
$GodotFS: {
// ERRNO_CODES works every odd version of emscripten, but this will break too eventually.
ENOENT: 44,
_idbfs: false,
_syncing: false,
_mount_points: [],
Expand All @@ -138,8 +140,9 @@ const GodotFS = {
try {
FS.stat(dir);
} catch (e) {
if (e.errno !== ERRNO_CODES.ENOENT) {
throw e;
if (e.errno !== GodotFS.ENOENT) {
// Let mkdirTree throw in case, we cannot trust the above check.
GodotRuntime.error(e);
}
FS.mkdirTree(dir);
}
Expand Down Expand Up @@ -208,8 +211,9 @@ const GodotFS = {
try {
FS.stat(dir);
} catch (e) {
if (e.errno !== ERRNO_CODES.ENOENT) {
throw e;
if (e.errno !== GodotFS.ENOENT) {
// Let mkdirTree throw in case, we cannot trust the above check.
GodotRuntime.error(e);
}
FS.mkdirTree(dir);
}
Expand Down
26 changes: 25 additions & 1 deletion platform/web/web_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ void cleanup_after_sync() {
emscripten_set_main_loop(exit_callback, -1, false);
}

void early_cleanup() {
emscripten_cancel_main_loop(); // After this, we can exit!
int exit_code = OS_Web::get_singleton()->get_exit_code();
memdelete(os);
os = nullptr;
emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing.
}

void early_cleanup_sync() {
emscripten_set_main_loop(early_cleanup, -1, false);
}

void main_loop_callback() {
uint64_t current_ticks = os->get_ticks_usec();

Expand Down Expand Up @@ -87,7 +99,19 @@ extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
// We must override main when testing is enabled
TEST_MAIN_OVERRIDE

Main::setup(argv[0], argc - 1, &argv[1]);
Error err = Main::setup(argv[0], argc - 1, &argv[1]);

// Proper shutdown in case of setup failure.
if (err != OK) {
int exit_code = (int)err;
if (err == ERR_HELP) {
exit_code = 0; // Called with --help.
}
os->set_exit_code(exit_code);
// Will only exit after sync.
godot_js_os_finish_async(early_cleanup_sync);
return exit_code;
}

// Ease up compatibility.
ResourceLoader::set_abort_on_missing_resources(false);
Expand Down

0 comments on commit 200b9cd

Please sign in to comment.