This commit is contained in:
Spartan322
2025-06-05 19:48:09 -04:00
772 changed files with 22320 additions and 10966 deletions

View File

@@ -76,6 +76,11 @@ for ext in sys_env["JS_EXTERNS"]:
sys_env["ENV"]["EMCC_CLOSURE_ARGS"] += " --externs " + ext.abspath
sys_env["ENV"]["EMCC_CLOSURE_ARGS"] = sys_env["ENV"]["EMCC_CLOSURE_ARGS"].strip()
if len(env["EXPORTED_FUNCTIONS"]):
sys_env.Append(LINKFLAGS=["-sEXPORTED_FUNCTIONS=" + repr(sorted(list(set(env["EXPORTED_FUNCTIONS"]))))])
if len(env["EXPORTED_RUNTIME_METHODS"]):
sys_env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=" + repr(sorted(list(set(env["EXPORTED_RUNTIME_METHODS"]))))])
build = []
build_targets = ["#bin/redot${PROGSUFFIX}.js", "#bin/redot${PROGSUFFIX}.wasm"]
if env["dlink_enabled"]:

View File

@@ -131,7 +131,7 @@ Error AudioDriverWeb::init() {
}
mix_rate = audio_context.mix_rate;
channel_count = audio_context.channel_count;
buffer_length = closest_power_of_2((latency * mix_rate / 1000));
buffer_length = closest_power_of_2(uint32_t(latency * mix_rate / 1000));
Error err = create(buffer_length, channel_count);
if (err != OK) {
return err;

View File

@@ -102,6 +102,9 @@ def library_emitter(target, source, env):
def configure(env: "SConsEnvironment"):
env.Append(LIBEMITTER=[library_emitter])
env["EXPORTED_FUNCTIONS"] = ["_main"]
env["EXPORTED_RUNTIME_METHODS"] = []
# Validate arch.
supported_arches = ["wasm32"]
validate_arch(env["arch"], get_name(), supported_arches)
@@ -244,12 +247,12 @@ def configure(env: "SConsEnvironment"):
env.Append(CCFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sDEFAULT_PTHREAD_STACK_SIZE=%sKB" % env["default_pthread_stack_size"]])
env.Append(LINKFLAGS=["-sPTHREAD_POOL_SIZE=8"])
env.Append(LINKFLAGS=["-sPTHREAD_POOL_SIZE='Module[\"emscriptenPoolSize\"]||8'"])
env.Append(LINKFLAGS=["-sWASM_MEM_MAX=2048MB"])
if not env["dlink_enabled"]:
# Workaround https://github.com/emscripten-core/emscripten/issues/21844#issuecomment-2116936414.
# Not needed (and potentially dangerous) when dlink_enabled=yes, since we set EXPORT_ALL=1 in that case.
env.Append(LINKFLAGS=["-sEXPORTED_FUNCTIONS=['__emscripten_thread_crashed','_main']"])
env["EXPORTED_FUNCTIONS"] += ["__emscripten_thread_crashed"]
elif env["proxy_to_pthread"]:
print_warning('"threads=no" support requires "proxy_to_pthread=no", disabling proxy to pthread.')
@@ -276,7 +279,7 @@ def configure(env: "SConsEnvironment"):
if env["proxy_to_pthread"]:
env.Append(LINKFLAGS=["-sPROXY_TO_PTHREAD=1"])
env.Append(CPPDEFINES=["PROXY_TO_PTHREAD_ENABLED"])
env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['_emscripten_proxy_main']"])
env["EXPORTED_RUNTIME_METHODS"] += ["_emscripten_proxy_main"]
# https://github.com/emscripten-core/emscripten/issues/18034#issuecomment-1277561925
env.Append(LINKFLAGS=["-sTEXTDECODER=0"])
@@ -303,7 +306,13 @@ def configure(env: "SConsEnvironment"):
env.Append(LINKFLAGS=["-sINVOKE_RUN=0"])
# callMain for manual start, cwrap for the mono version.
env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['callMain','cwrap']"])
# Make sure also to have those memory-related functions available.
heap_arrays = [f"HEAP{heap_type}{heap_size}" for heap_size in [8, 16, 32, 64] for heap_type in ["", "U"]] + [
"HEAPF32",
"HEAPF64",
]
env["EXPORTED_RUNTIME_METHODS"] += ["callMain", "cwrap"] + heap_arrays
env["EXPORTED_FUNCTIONS"] += ["_malloc", "_free"]
# Add code that allow exiting runtime.
env.Append(LINKFLAGS=["-sEXIT_RUNTIME=1"])

View File

@@ -307,18 +307,18 @@ int DisplayServerWeb::_mouse_button_callback(int p_pressed, int p_button, double
return true;
}
void DisplayServerWeb::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
void DisplayServerWeb::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure) {
#ifdef PROXY_TO_PTHREAD_ENABLED
if (!Thread::is_main_thread()) {
callable_mp_static(DisplayServerWeb::_mouse_move_callback).call_deferred(p_x, p_y, p_rel_x, p_rel_y, p_modifiers);
callable_mp_static(DisplayServerWeb::_mouse_move_callback).call_deferred(p_x, p_y, p_rel_x, p_rel_y, p_modifiers, p_pressure);
return;
}
#endif
_mouse_move_callback(p_x, p_y, p_rel_x, p_rel_y, p_modifiers);
_mouse_move_callback(p_x, p_y, p_rel_x, p_rel_y, p_modifiers, p_pressure);
}
void DisplayServerWeb::_mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
void DisplayServerWeb::_mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure) {
BitField<MouseButtonMask> input_mask = Input::get_singleton()->get_mouse_button_mask();
// For motion outside the canvas, only read mouse movement if dragging
// started inside the canvas; imitating desktop app behavior.
@@ -334,6 +334,7 @@ void DisplayServerWeb::_mouse_move_callback(double p_x, double p_y, double p_rel
ev->set_position(pos);
ev->set_global_position(pos);
ev->set_pressure((float)p_pressure);
ev->set_relative(Vector2(p_rel_x, p_rel_y));
ev->set_relative_screen_position(ev->get_relative());
@@ -1219,30 +1220,54 @@ int DisplayServerWeb::get_primary_screen() const {
}
Point2i DisplayServerWeb::screen_get_position(int p_screen) const {
return Point2i(); // TODO offsetX/Y?
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i());
return Point2i(0, 0); // TODO offsetX/Y?
}
Size2i DisplayServerWeb::screen_get_size(int p_screen) const {
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i());
int size[2];
godot_js_display_screen_size_get(size, size + 1);
return Size2(size[0], size[1]);
}
Rect2i DisplayServerWeb::screen_get_usable_rect(int p_screen) const {
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i());
int size[2];
godot_js_display_window_size_get(size, size + 1);
return Rect2i(0, 0, size[0], size[1]);
}
int DisplayServerWeb::screen_get_dpi(int p_screen) const {
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, 72);
return godot_js_display_screen_dpi_get();
}
float DisplayServerWeb::screen_get_scale(int p_screen) const {
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f);
return godot_js_display_pixel_ratio_get();
}
float DisplayServerWeb::screen_get_refresh_rate(int p_screen) const {
p_screen = _get_screen_index(p_screen);
int screen_count = get_screen_count();
ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK);
return SCREEN_REFRESH_RATE_FALLBACK; // Web doesn't have much of a need for the screen refresh rate, and there's no native way to do so.
}
@@ -1289,7 +1314,8 @@ void DisplayServerWeb::window_set_title(const String &p_title, WindowID p_window
}
int DisplayServerWeb::window_get_current_screen(WindowID p_window) const {
return 1;
ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, INVALID_SCREEN);
return 0;
}
void DisplayServerWeb::window_set_current_screen(int p_screen, WindowID p_window) {

View File

@@ -122,8 +122,8 @@ private:
static void _fullscreen_change_callback(int p_fullscreen);
WASM_EXPORT static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
static int _mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
WASM_EXPORT static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
static void _mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
WASM_EXPORT static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure);
static void _mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure);
WASM_EXPORT static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
static int _mouse_wheel_callback(double p_delta_x, double p_delta_y);
WASM_EXPORT static void touch_callback(int p_type, int p_count);

View File

@@ -79,9 +79,17 @@
- [b]Landscape:[/b] Forces a horizontal layout (wider than it is taller).
- [b]Portrait:[/b] Forces a vertical layout (taller than it is wider).
</member>
<member name="variant/emscripten_pool_size" type="int" setter="" getter="">
The number of threads that emscripten will allocate at startup. A smaller value will allocate fewer threads and consume fewer system resources, but you may run the risk of running out of threads in the pool and needing to allocate more threads at run time which may cause a deadlock.
[b]Note:[/b] Some browsers have a hard cap on the number of threads that can be allocated, so it is best to be cautious and keep this number low.
</member>
<member name="variant/extensions_support" type="bool" setter="" getter="">
If [code]true[/code] enables [GDExtension] support for this web build.
</member>
<member name="variant/godot_pool_size" type="int" setter="" getter="">
Override for the default size of the [WorkerThreadPool]. This setting is used when [member ProjectSettings.threading/worker_pool/max_threads] size is set to -1 (which it is by default). This size must be smaller than [member variant/emscripten_pool_size] otherwise deadlocks may occur.
When using threads this size needs to be large enough to accommodate features that rely on having a dedicated thread like [member ProjectSettings.physics/2d/run_on_separate_thread] or [member ProjectSettings.rendering/driver/threads/thread_model]. In general, it is best to ensure that this is at least 4 and is at least 2 or 3 less than [member variant/emscripten_pool_size].
</member>
<member name="variant/thread_support" type="bool" setter="" getter="">
If [code]true[/code], the exported game will support threads. It requires [url=https://web.dev/articles/coop-coep]a "cross-origin isolated" website[/url], which may be difficult to set up and is limited for security reasons (such as not being able to communicate with third-party websites).
If [code]false[/code], the exported game will not support threads. As a result, it is more prone to performance and audio issues, but will only require to be run on an HTTPS website.

View File

@@ -145,11 +145,13 @@ void EditorExportPlatformWeb::_fix_html(Vector<uint8_t> &p_html, const Ref<Edito
config["canvasResizePolicy"] = p_preset->get("html/canvas_resize_policy");
config["experimentalVK"] = p_preset->get("html/experimental_virtual_keyboard");
config["focusCanvas"] = p_preset->get("html/focus_canvas_on_start");
config["godotPoolSize"] = p_preset->get("variant/godot_pool_size");
config["gdextensionLibs"] = libs;
config["executable"] = p_name;
config["args"] = args;
config["fileSizes"] = p_file_sizes;
config["ensureCrossOriginIsolationHeaders"] = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
config["emscriptenPoolSize"] = p_preset->get("variant/emscripten_pool_size");
String head_include;
if (p_preset->get("html/export_icon")) {
@@ -362,6 +364,8 @@ void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options)
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/extensions_support"), false)); // GDExtension support.
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/thread_support"), false)); // Thread support (i.e. run with or without COEP/COOP headers).
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "variant/emscripten_pool_size"), 8));
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "variant/godot_pool_size"), 4));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer

View File

@@ -52,13 +52,14 @@ extern void godot_js_os_fs_sync(void (*p_callback)());
extern int godot_js_os_execute(const char *p_json);
extern void godot_js_os_shell_open(const char *p_uri);
extern int godot_js_os_hw_concurrency_get();
extern int godot_js_os_thread_pool_size_get();
extern int godot_js_os_has_feature(const char *p_ftr);
extern int godot_js_pwa_cb(void (*p_callback)());
extern int godot_js_pwa_update();
// Input
extern void godot_js_input_mouse_button_cb(int (*p_callback)(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers));
extern void godot_js_input_mouse_move_cb(void (*p_callback)(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers));
extern void godot_js_input_mouse_move_cb(void (*p_callback)(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure));
extern void godot_js_input_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y));
extern void godot_js_input_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords);
extern void godot_js_input_key_cb(void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);

View File

@@ -133,6 +133,16 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
* @type {Array.<string>}
*/
fileSizes: [],
/**
* @ignore
* @type {number}
*/
emscriptenPoolSize: 8,
/**
* @ignore
* @type {number}
*/
godotPoolSize: 4,
/**
* A callback function for handling Godot's ``OS.execute`` calls.
*
@@ -259,6 +269,8 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
this.serviceWorker = parse('serviceWorker', this.serviceWorker);
this.gdextensionLibs = parse('gdextensionLibs', this.gdextensionLibs);
this.fileSizes = parse('fileSizes', this.fileSizes);
this.emscriptenPoolSize = parse('emscriptenPoolSize', this.emscriptenPoolSize);
this.godotPoolSize = parse('godotPoolSize', this.godotPoolSize);
this.args = parse('args', this.args);
this.onExecute = parse('onExecute', this.onExecute);
this.onExit = parse('onExit', this.onExit);
@@ -278,6 +290,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
'thisProgram': this.executable,
'noExitRuntime': false,
'dynamicLibraries': [`${loadPath}.side.wasm`].concat(this.gdextensionLibs),
'emscriptenPoolSize': this.emscriptenPoolSize,
'instantiateWasm': function (imports, onSuccess) {
function done(result) {
onSuccess(result['instance'], result['module']);
@@ -350,6 +363,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
'locale': locale,
'persistentDrops': this.persistentDrops,
'virtualKeyboard': this.experimentalVK,
'godotPoolSize': this.godotPoolSize,
'focusCanvas': this.focusCanvas,
'onExecute': this.onExecute,
'onExit': function (p_code) {

View File

@@ -517,9 +517,9 @@ const GodotInput = {
const rel_pos_x = evt.movementX * rw;
const rel_pos_y = evt.movementY * rh;
const modifiers = GodotInput.getModifiers(evt);
func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers);
func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers, evt.pressure);
}
GodotEventListeners.add(window, 'mousemove', move_cb, false);
GodotEventListeners.add(window, 'pointermove', move_cb, false);
},
godot_js_input_mouse_wheel_cb__proxy: 'sync',

View File

@@ -63,6 +63,7 @@ const GodotConfig = {
canvas_resize_policy: 2, // Adaptive
virtual_keyboard: false,
persistent_drops: false,
godot_pool_size: 4,
on_execute: null,
on_exit: null,
@@ -72,6 +73,7 @@ const GodotConfig = {
GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];
GodotConfig.persistent_drops = !!p_opts['persistentDrops'];
GodotConfig.godot_pool_size = p_opts['godotPoolSize'];
GodotConfig.on_execute = p_opts['onExecute'];
GodotConfig.on_exit = p_opts['onExit'];
if (p_opts['focusCanvas']) {
@@ -348,6 +350,17 @@ const GodotOS = {
return concurrency < 2 ? concurrency : 2;
},
godot_js_os_thread_pool_size_get__proxy: 'sync',
godot_js_os_thread_pool_size_get__sig: 'i',
godot_js_os_thread_pool_size_get: function () {
if (typeof PThread === 'undefined') {
// Threads aren't supported, so default to `1`.
return 1;
}
return GodotConfig.godot_pool_size;
},
godot_js_os_download_buffer__proxy: 'sync',
godot_js_os_download_buffer__sig: 'viiii',
godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {

View File

@@ -150,6 +150,14 @@ String OS_Web::get_unique_id() const {
ERR_FAIL_V_MSG("", "OS::get_unique_id() is not available on the Web platform.");
}
int OS_Web::get_default_thread_pool_size() const {
#ifdef THREADS_ENABLED
return godot_js_os_thread_pool_size_get();
#else // No threads.
return 1;
#endif
}
bool OS_Web::_check_internal_feature_support(const String &p_feature) {
if (p_feature == "web") {
return true;
@@ -174,9 +182,9 @@ String OS_Web::get_name() const {
return "Web";
}
void OS_Web::add_frame_delay(bool p_can_draw) {
void OS_Web::add_frame_delay(bool p_can_draw, bool p_wake_for_events) {
#ifndef PROXY_TO_PTHREAD_ENABLED
OS::add_frame_delay(p_can_draw);
OS::add_frame_delay(p_can_draw, p_wake_for_events);
#endif
}

View File

@@ -93,7 +93,7 @@ public:
int get_process_exit_code(const ProcessID &p_pid) const override;
int get_processor_count() const override;
String get_unique_id() const override;
int get_default_thread_pool_size() const override { return 1; }
int get_default_thread_pool_size() const override;
String get_executable_path() const override;
Error shell_open(const String &p_uri) override;
@@ -101,7 +101,7 @@ public:
// Override default OS implementation which would block the main thread with delay_usec.
// Implemented in web_main.cpp loop callback instead.
void add_frame_delay(bool p_can_draw) override;
void add_frame_delay(bool p_can_draw, bool p_wake_for_events) override;
void vibrate_handheld(int p_duration_ms, float p_amplitude) override;