Skip to content

Commit

Permalink
Add proxy_to_pthread option to platform=web
Browse files Browse the repository at this point in the history
Co-authored-by: Fabio Alessandrelli <fabio.alessandrelli@gmail.com>
  • Loading branch information
adamscott and Faless committed Sep 16, 2023
1 parent 5f1e56f commit 79b7f4f
Show file tree
Hide file tree
Showing 19 changed files with 359 additions and 40 deletions.
4 changes: 2 additions & 2 deletions drivers/gles3/storage/particles_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p
LocalVector<ParticleInstanceData3D> particle_vector;
particle_vector.resize(particles->amount);
particle_array = particle_vector.ptr();
glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_array);
godot_webgl2_glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_array);
#endif
SortArray<ParticleInstanceData3D, ParticlesViewSort> sorter;
sorter.compare.z_dir = axis;
Expand Down Expand Up @@ -1133,7 +1133,7 @@ void ParticlesStorage::_particles_reverse_lifetime_sort(Particles *particles) {
LocalVector<ParticleInstanceData> particle_vector;
particle_vector.resize(particles->amount);
particle_array = particle_vector.ptr();
glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_array);
godot_webgl2_glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_array);
#endif

uint32_t lifetime_split = (MIN(int(particles->amount * particles->sort_buffer_phase), particles->amount - 1) + 1) % particles->amount;
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/storage/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Vector<uint8_t> Utilities::buffer_get_data(GLenum p_target, GLuint p_buffer, uin
#if defined(__EMSCRIPTEN__)
{
uint8_t *w = ret.ptrw();
glGetBufferSubData(p_target, 0, p_buffer_size, w);
godot_webgl2_glGetBufferSubData(p_target, 0, p_buffer_size, w);
}
#else
void *data = glMapBufferRange(p_target, 0, p_buffer_size, GL_MAP_READ_BIT);
Expand Down
20 changes: 20 additions & 0 deletions modules/webrtc/library_godot_webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const GodotRTCDataChannel = {
},
},

godot_js_rtc_datachannel_ready_state_get__proxy: 'sync',
godot_js_rtc_datachannel_ready_state_get__sig: 'ii',
godot_js_rtc_datachannel_ready_state_get: function (p_id) {
const ref = IDHandler.get(p_id);
Expand All @@ -110,6 +111,7 @@ const GodotRTCDataChannel = {
}
},

godot_js_rtc_datachannel_send__proxy: 'sync',
godot_js_rtc_datachannel_send__sig: 'iiiii',
godot_js_rtc_datachannel_send: function (p_id, p_buffer, p_length, p_raw) {
const ref = IDHandler.get(p_id);
Expand All @@ -131,16 +133,19 @@ const GodotRTCDataChannel = {
return 0;
},

godot_js_rtc_datachannel_is_ordered__proxy: 'sync',
godot_js_rtc_datachannel_is_ordered__sig: 'ii',
godot_js_rtc_datachannel_is_ordered: function (p_id) {
return GodotRTCDataChannel.get_prop(p_id, 'ordered', true);
},

godot_js_rtc_datachannel_id_get__proxy: 'sync',
godot_js_rtc_datachannel_id_get__sig: 'ii',
godot_js_rtc_datachannel_id_get: function (p_id) {
return GodotRTCDataChannel.get_prop(p_id, 'id', 65535);
},

godot_js_rtc_datachannel_max_packet_lifetime_get__proxy: 'sync',
godot_js_rtc_datachannel_max_packet_lifetime_get__sig: 'ii',
godot_js_rtc_datachannel_max_packet_lifetime_get: function (p_id) {
const ref = IDHandler.get(p_id);
Expand All @@ -156,21 +161,25 @@ const GodotRTCDataChannel = {
return 65535;
},

godot_js_rtc_datachannel_max_retransmits_get__proxy: 'sync',
godot_js_rtc_datachannel_max_retransmits_get__sig: 'ii',
godot_js_rtc_datachannel_max_retransmits_get: function (p_id) {
return GodotRTCDataChannel.get_prop(p_id, 'maxRetransmits', 65535);
},

godot_js_rtc_datachannel_is_negotiated__proxy: 'sync',
godot_js_rtc_datachannel_is_negotiated__sig: 'ii',
godot_js_rtc_datachannel_is_negotiated: function (p_id) {
return GodotRTCDataChannel.get_prop(p_id, 'negotiated', 65535);
},

godot_js_rtc_datachannel_get_buffered_amount__proxy: 'sync',
godot_js_rtc_datachannel_get_buffered_amount__sig: 'ii',
godot_js_rtc_datachannel_get_buffered_amount: function (p_id) {
return GodotRTCDataChannel.get_prop(p_id, 'bufferedAmount', 0);
},

godot_js_rtc_datachannel_label_get__proxy: 'sync',
godot_js_rtc_datachannel_label_get__sig: 'ii',
godot_js_rtc_datachannel_label_get: function (p_id) {
const ref = IDHandler.get(p_id);
Expand All @@ -189,12 +198,14 @@ const GodotRTCDataChannel = {
return GodotRuntime.allocString(ref.protocol);
},

godot_js_rtc_datachannel_destroy__proxy: 'sync',
godot_js_rtc_datachannel_destroy__sig: 'vi',
godot_js_rtc_datachannel_destroy: function (p_id) {
GodotRTCDataChannel.close(p_id);
IDHandler.remove(p_id);
},

godot_js_rtc_datachannel_connect__proxy: 'sync',
godot_js_rtc_datachannel_connect__sig: 'viiiiii',
godot_js_rtc_datachannel_connect: function (p_id, p_ref, p_on_open, p_on_message, p_on_error, p_on_close) {
const onopen = GodotRuntime.get_func(p_on_open).bind(null, p_ref);
Expand All @@ -204,6 +215,7 @@ const GodotRTCDataChannel = {
GodotRTCDataChannel.connect(p_id, onopen, onmessage, onerror, onclose);
},

godot_js_rtc_datachannel_close__proxy: 'sync',
godot_js_rtc_datachannel_close__sig: 'vi',
godot_js_rtc_datachannel_close: function (p_id) {
const ref = IDHandler.get(p_id);
Expand Down Expand Up @@ -356,6 +368,7 @@ const GodotRTCPeerConnection = {
},
},

godot_js_rtc_pc_create__proxy: 'sync',
godot_js_rtc_pc_create__sig: 'iiiiiiii',
godot_js_rtc_pc_create: function (p_config, p_ref, p_on_connection_state_change, p_on_ice_gathering_state_change, p_on_signaling_state_change, p_on_ice_candidate, p_on_datachannel) {
const wrap = function (p_func) {
Expand All @@ -371,6 +384,7 @@ const GodotRTCPeerConnection = {
);
},

godot_js_rtc_pc_close__proxy: 'sync',
godot_js_rtc_pc_close__sig: 'vi',
godot_js_rtc_pc_close: function (p_id) {
const ref = IDHandler.get(p_id);
Expand All @@ -380,11 +394,13 @@ const GodotRTCPeerConnection = {
ref.close();
},

godot_js_rtc_pc_destroy__proxy: 'sync',
godot_js_rtc_pc_destroy__sig: 'vi',
godot_js_rtc_pc_destroy: function (p_id) {
GodotRTCPeerConnection.destroy(p_id);
},

godot_js_rtc_pc_offer_create__proxy: 'sync',
godot_js_rtc_pc_offer_create__sig: 'viiii',
godot_js_rtc_pc_offer_create: function (p_id, p_obj, p_on_session, p_on_error) {
const ref = IDHandler.get(p_id);
Expand All @@ -400,6 +416,7 @@ const GodotRTCPeerConnection = {
});
},

godot_js_rtc_pc_local_description_set__proxy: 'sync',
godot_js_rtc_pc_local_description_set__sig: 'viiiii',
godot_js_rtc_pc_local_description_set: function (p_id, p_type, p_sdp, p_obj, p_on_error) {
const ref = IDHandler.get(p_id);
Expand All @@ -417,6 +434,7 @@ const GodotRTCPeerConnection = {
});
},

godot_js_rtc_pc_remote_description_set__proxy: 'sync',
godot_js_rtc_pc_remote_description_set__sig: 'viiiiii',
godot_js_rtc_pc_remote_description_set: function (p_id, p_type, p_sdp, p_obj, p_session_created, p_on_error) {
const ref = IDHandler.get(p_id);
Expand All @@ -442,6 +460,7 @@ const GodotRTCPeerConnection = {
});
},

godot_js_rtc_pc_ice_candidate_add__proxy: 'sync',
godot_js_rtc_pc_ice_candidate_add__sig: 'viiii',
godot_js_rtc_pc_ice_candidate_add: function (p_id, p_mid_name, p_mline_idx, p_sdp) {
const ref = IDHandler.get(p_id);
Expand All @@ -458,6 +477,7 @@ const GodotRTCPeerConnection = {
},

godot_js_rtc_pc_datachannel_create__deps: ['$GodotRTCDataChannel'],
godot_js_rtc_pc_datachannel_create__proxy: 'sync',
godot_js_rtc_pc_datachannel_create__sig: 'iiii',
godot_js_rtc_pc_datachannel_create: function (p_id, p_label, p_config) {
try {
Expand Down
5 changes: 5 additions & 0 deletions modules/websocket/library_godot_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const GodotWebSocket = {
},
},

godot_js_websocket_create__proxy: 'sync',
godot_js_websocket_create__sig: 'iiiiiiii',
godot_js_websocket_create: function (p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) {
const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref);
Expand All @@ -166,6 +167,7 @@ const GodotWebSocket = {
return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close);
},

godot_js_websocket_send__proxy: 'sync',
godot_js_websocket_send__sig: 'iiiii',
godot_js_websocket_send: function (p_id, p_buf, p_buf_len, p_raw) {
const bytes_array = new Uint8Array(p_buf_len);
Expand All @@ -180,18 +182,21 @@ const GodotWebSocket = {
return GodotWebSocket.send(p_id, out);
},

godot_js_websocket_buffered_amount__proxy: 'sync',
godot_js_websocket_buffered_amount__sig: 'ii',
godot_js_websocket_buffered_amount: function (p_id) {
return GodotWebSocket.bufferedAmount(p_id);
},

godot_js_websocket_close__proxy: 'sync',
godot_js_websocket_close__sig: 'viii',
godot_js_websocket_close: function (p_id, p_code, p_reason) {
const code = p_code;
const reason = GodotRuntime.parseString(p_reason);
GodotWebSocket.close(p_id, code, reason);
},

godot_js_websocket_destroy__proxy: 'sync',
godot_js_websocket_destroy__sig: 'vi',
godot_js_websocket_destroy: function (p_id) {
GodotWebSocket.destroy(p_id);
Expand Down
15 changes: 15 additions & 0 deletions platform/web/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def get_opts():
"dlink_enabled", "Enable WebAssembly dynamic linking (GDExtension support). Produces bigger binaries", False
),
BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
BoolVariable(
"proxy_to_pthread",
"Use Emscripten PROXY_TO_PTHREAD option to run the main application code to a separate thread",
True,
),
]


Expand Down Expand Up @@ -217,6 +222,16 @@ def configure(env: "Environment"):
env.Append(LINKFLAGS=["-fvisibility=hidden"])
env.extra_suffix = ".dlink" + env.extra_suffix

# Run the main application in a web worker
if env["proxy_to_pthread"]:
env.Append(LINKFLAGS=["-s", "PROXY_TO_PTHREAD=1"])
env.Append(CPPDEFINES=["PROXY_TO_PTHREAD_ENABLED"])
env.Append(LINKFLAGS=["-s", "EXPORTED_RUNTIME_METHODS=['_emscripten_proxy_main']"])
# https://github.com/emscripten-core/emscripten/issues/18034#issuecomment-1277561925
env.Append(LINKFLAGS=["-s", "TEXTDECODER=0"])
# BigInt support to pass object pointers between contexts
env.Append(LINKFLAGS=["-s", "WASM_BIGINT"])

# Reduce code size by generating less support code (e.g. skip NodeJS support).
env.Append(LINKFLAGS=["-s", "ENVIRONMENT=web,worker"])

Expand Down
Loading

0 comments on commit 79b7f4f

Please sign in to comment.