Skip to content

Commit fb39aa4

Browse files
committed
Merge pull request #105833 from marcosc90/perf-web-shader-source
[Web] Optimize `GL.getSource` for known-length shader sources
2 parents ffb7358 + 197b307 commit fb39aa4

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

drivers/gles3/shader_gles3.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
321321
String builder_string = builder.as_string();
322322
CharString cs = builder_string.utf8();
323323
const char *cstr = cs.ptr();
324-
glShaderSource(spec.vert_id, 1, &cstr, nullptr);
324+
GLint cstr_len = cs.length();
325+
glShaderSource(spec.vert_id, 1, &cstr, &cstr_len);
325326
glCompileShader(spec.vert_id);
326327

327328
glGetShaderiv(spec.vert_id, GL_COMPILE_STATUS, &status);
@@ -369,7 +370,8 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
369370
String builder_string = builder.as_string();
370371
CharString cs = builder_string.utf8();
371372
const char *cstr = cs.ptr();
372-
glShaderSource(spec.frag_id, 1, &cstr, nullptr);
373+
GLint cstr_len = cs.length();
374+
glShaderSource(spec.frag_id, 1, &cstr, &cstr_len);
373375
glCompileShader(spec.frag_id);
374376

375377
glGetShaderiv(spec.frag_id, GL_COMPILE_STATUS, &status);

platform/web/SCsub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ sys_env.AddJSExterns(
5454
]
5555
)
5656

57+
sys_env.AddJSPost(
58+
[
59+
"js/patches/patch_em_gl.js",
60+
]
61+
)
62+
5763
if env["javascript_eval"]:
5864
sys_env.AddJSLibraries(["js/libs/library_godot_javascript_singleton.js"])
5965

6066
for lib in sys_env["JS_LIBS"]:
6167
sys_env.Append(LINKFLAGS=["--js-library", lib.abspath])
6268
for js in sys_env["JS_PRE"]:
6369
sys_env.Append(LINKFLAGS=["--pre-js", js.abspath])
70+
for js in sys_env["JS_POST"]:
71+
sys_env.Append(LINKFLAGS=["--post-js", js.abspath])
6472

6573
# Add JS externs to Closure.
6674
sys_env["ENV"]["EMCC_CLOSURE_ARGS"] = sys_env["ENV"].get("EMCC_CLOSURE_ARGS", "")
@@ -98,6 +106,7 @@ else:
98106

99107
sys_env.Depends(build[0], sys_env["JS_LIBS"])
100108
sys_env.Depends(build[0], sys_env["JS_PRE"])
109+
sys_env.Depends(build[0], sys_env["JS_POST"])
101110
sys_env.Depends(build[0], sys_env["JS_EXTERNS"])
102111

103112
engine = [

platform/web/detect.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from emscripten_helpers import (
66
add_js_externs,
77
add_js_libraries,
8+
add_js_post,
89
add_js_pre,
910
create_engine_file,
1011
create_template_zip,
@@ -169,12 +170,14 @@ def configure(env: "SConsEnvironment"):
169170
jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
170171
env.Append(BUILDERS={"BuildJS": jscc})
171172

172-
# Add helper method for adding libraries, externs, pre-js.
173+
# Add helper method for adding libraries, externs, pre-js, post-js.
173174
env["JS_LIBS"] = []
174175
env["JS_PRE"] = []
176+
env["JS_POST"] = []
175177
env["JS_EXTERNS"] = []
176178
env.AddMethod(add_js_libraries, "AddJSLibraries")
177179
env.AddMethod(add_js_pre, "AddJSPre")
180+
env.AddMethod(add_js_post, "AddJSPost")
178181
env.AddMethod(add_js_externs, "AddJSExterns")
179182

180183
# Add method that joins/compiles our Engine files.

platform/web/emscripten_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,9 @@ def add_js_pre(env, js_pre):
125125
env.Append(JS_PRE=env.File(js_pre))
126126

127127

128+
def add_js_post(env, js_post):
129+
env.Append(JS_POST=env.File(js_post))
130+
131+
128132
def add_js_externs(env, externs):
129133
env.Append(JS_EXTERNS=env.File(externs))

platform/web/eslint.config.cjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ const emscriptenGlobals = {
1919
'HEAP8': true,
2020
'HEAPF32': true,
2121
'HEAPU8': true,
22+
'HEAPU32': true,
2223
'IDBFS': true,
2324
'LibraryManager': true,
2425
'Module': true,
2526
'UTF8ToString': true,
27+
'UTF8Decoder': true,
2628
'_emscripten_webgl_get_current_context': true,
2729
'_free': true,
2830
'_malloc': true,
2931
'autoAddDeps': true,
32+
'addOnPostRun': true,
3033
'getValue': true,
3134
'lengthBytesUTF8': true,
3235
'mergeInto': true,
@@ -138,7 +141,12 @@ module.exports = [
138141

139142
// libraries and modules (browser)
140143
{
141-
files: ['js/libs/**/*.js', 'platform/web/js/libs/**/*.js', 'modules/**/*.js'],
144+
files: [
145+
'js/libs/**/*.js',
146+
'platform/web/js/libs/**/*.js',
147+
'platform/web/js/patches/**/*.js',
148+
'modules/**/*.js'
149+
],
142150
languageOptions: {
143151
globals: {
144152
...globals.browser,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**************************************************************************/
2+
/* patch_em_gl.js */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
addOnPostRun(function () {
32+
GL.getSource = (shader, count, string, length) => {
33+
let source = '';
34+
for (let i = 0; i < count; ++i) {
35+
const ptr = HEAPU32[(string + i * 4) >> 2];
36+
const len = length ? HEAPU32[(length + i * 4) >> 2] : undefined;
37+
if (len) {
38+
const endPtr = ptr + len;
39+
const slice = HEAPU8.buffer instanceof ArrayBuffer
40+
? HEAPU8.subarray(ptr, endPtr)
41+
: HEAPU8.slice(ptr, endPtr);
42+
source += UTF8Decoder.decode(slice);
43+
} else {
44+
source += UTF8ToString(ptr, len);
45+
}
46+
}
47+
return source;
48+
};
49+
});

0 commit comments

Comments
 (0)