diff --git a/ChangeLog.md b/ChangeLog.md index c9ec38b00e617..aec628d59be1b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -43,6 +43,12 @@ See docs/process.md for more on how version tagging works. - Building with `-sJSPI` now bumps the default minimum supported browser versions to the first ones that support JSPI: Chrome 137, Firefox 153, Safari 27 and Node.js 24. (#28334) +- The `glUniform*()` functions now raise `GL_INVALID_OPERATION` when called + with a location that is not a valid uniform location of the current program + (and is not -1), e.g. a hardcoded integer location that was not acquired from + `glGetUniformLocation()`. Previously such calls were silently ignored. This + check is not performed in `-sGL_TRACK_ERRORS=0` or + `-sGL_EXPLICIT_UNIFORM_LOCATION` builds. (#16367) 7.0.0 - 11/06/26 ---------------- diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index 8a09b5fda4580..a993bef11f205 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -2164,6 +2164,18 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { program.uniformLocsById[location] = webglLoc = GLctx.getUniformLocation(program, program.uniformArrayNamesById[location] + (webglLoc > 0 ? `[${webglLoc}]` : '')); } // Else an already cached WebGLUniformLocation, return it. +#if GL_TRACK_ERRORS && !GL_EXPLICIT_UNIFORM_LOCATION + // In GLES, calling glUniform*() with a location that is neither -1 nor a + // valid uniform location of the program generates GL_INVALID_OPERATION. + // WebGL API would silently ignore null/undefined locations (those would + // be the same scenario as -1 in native GLES), so we must manually raise + // the error here. + // However, we cannot enable this when -sGL_EXPLICIT_UNIFORM_LOCATION is used, + // since explicit locations of a uniform that the linker deemed inactive must + // be silently ignored, and the locations taken up by inactive uniforms are + // currently not being tracked. + if (!webglLoc && location != -1) GL.recordError(GL_INVALID_OPERATION); +#endif return webglLoc; #if GL_TRACK_ERRORS } else { diff --git a/test/browser/webgl_uniform_invalid_location.c b/test/browser/webgl_uniform_invalid_location.c new file mode 100644 index 0000000000000..1c4c3c39f1162 --- /dev/null +++ b/test/browser/webgl_uniform_invalid_location.c @@ -0,0 +1,127 @@ +// Copyright 2026 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +// Tests that calling glUniform*() with a uniform location that is not valid +// for the current program generates GL_INVALID_OPERATION, like it does in +// native OpenGL ES. In particular this covers passing a hardcoded integer +// location without having acquired it from glGetUniformLocation(). +// See https://github.com/emscripten-core/emscripten/issues/16367 +// +// WebGL represents uniform locations as opaque WebGLUniformLocation objects, +// and silently ignores uniform uploads to a null location, so this error +// needs to be generated by the Emscripten GL library. + +#include +#include +#include +#include + +static GLuint compile_shader(GLenum type, const char *src) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + GLint compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + assert(compiled && "Shader compile failed"); + return shader; +} + +static void assert_uniforms(GLuint program, GLint loc_val, GLint loc_num, float x, float y, float z, int num) { + float val[3] = { -1.f, -1.f, -1.f }; + glGetUniformfv(program, loc_val, val); + assert(val[0] == x && val[1] == y && val[2] == z); + GLint n = -1; + glGetUniformiv(program, loc_num, &n); + assert(n == num); + assert(glGetError() == GL_NO_ERROR); +} + +int main() { + EmscriptenWebGLContextAttributes attr; + emscripten_webgl_init_context_attributes(&attr); + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr); + assert(ctx); + emscripten_webgl_make_context_current(ctx); + + GLuint vs = compile_shader(GL_VERTEX_SHADER, + "uniform vec3 u_val;\n" + "uniform int u_num;\n" + "void main() { gl_Position = vec4(u_val, float(u_num)); }\n"); + + GLuint fs = compile_shader(GL_FRAGMENT_SHADER, + "precision mediump float;\n" + "void main() { gl_FragColor = vec4(1.0); }\n"); + + GLuint program = glCreateProgram(); + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + + GLint linked = 0; + glGetProgramiv(program, GL_LINK_STATUS, &linked); + assert(linked && "Program link failed"); + + glUseProgram(program); + assert(glGetError() == GL_NO_ERROR); + + const float val[] = { 1.f, 2.f, 3.f }; + + // Upload uniforms using a hardcoded integer location, without ever having + // called glGetUniformLocation(). Such a location is not valid, so the calls + // must raise GL_INVALID_OPERATION instead of being silently ignored. + glUniform3f(/*location=*/0, 1.f, 2.f, 3.f); + assert(glGetError() == GL_INVALID_OPERATION); + // glGetError() clears the error. + assert(glGetError() == GL_NO_ERROR); + + glUniform3fv(/*location=*/0, 1, val); + assert(glGetError() == GL_INVALID_OPERATION); + + glUniform1i(/*location=*/0, 42); + assert(glGetError() == GL_INVALID_OPERATION); + + // Location -1 is always silently ignored without raising an error. + glUniform3f(-1, 1.f, 2.f, 3.f); + glUniform3fv(-1, 1, val); + glUniform1i(-1, 42); + assert(glGetError() == GL_NO_ERROR); + + // Uploading via locations acquired from glGetUniformLocation() works. + GLint loc_val = glGetUniformLocation(program, "u_val"); + GLint loc_num = glGetUniformLocation(program, "u_num"); + assert(loc_val != -1); + assert(loc_num != -1); + assert(glGetError() == GL_NO_ERROR); + + // None of the erroneous calls above modified any uniform state. + assert_uniforms(program, loc_val, loc_num, 0.f, 0.f, 0.f, 0); + + glUniform3fv(loc_val, 1, val); + glUniform1i(loc_num, 42); + assert(glGetError() == GL_NO_ERROR); + assert_uniforms(program, loc_val, loc_num, 1.f, 2.f, 3.f, 42); + + // After uniform locations have been queried, uploading to a location that + // does not exist in the program still raises GL_INVALID_OPERATION, and + // leaves the existing uniforms untouched. + GLint invalid_loc = (loc_val > loc_num ? loc_val : loc_num) + 100; + + glUniform3f(invalid_loc, 4.f, 5.f, 6.f); + assert(glGetError() == GL_INVALID_OPERATION); + + glUniform3fv(invalid_loc, 1, val); + assert(glGetError() == GL_INVALID_OPERATION); + + glUniform1i(invalid_loc, 43); + assert(glGetError() == GL_INVALID_OPERATION); + + glUniform3f(-1, 4.f, 5.f, 6.f); + assert(glGetError() == GL_NO_ERROR); + + assert_uniforms(program, loc_val, loc_num, 1.f, 2.f, 3.f, 42); + + printf("Test passed!\n"); + return 0; +} diff --git a/test/test_browser.py b/test/test_browser.py index 67a220b16b202..e5fd69149a240 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1225,6 +1225,16 @@ def test_webgl_explicit_uniform_location(self): def test_webgl_uniform_before_get_location(self, args): self.btest_exit('webgl_uniform_before_get_location.c', cflags=args + ['-sGL_EXPLICIT_UNIFORM_LOCATION', '-sMIN_WEBGL_VERSION=2']) + # Tests that glUniform*() with an invalid location raises GL_INVALID_OPERATION (#16367) + @parameterized({ + '': ([],), + 'assertions': (['-sGL_ASSERTIONS'],), + 'closure': (['-O2', '-g1', '--closure=1'],), + }) + @requires_graphics_hardware + def test_webgl_uniform_invalid_location(self, args): + self.btest_exit('webgl_uniform_invalid_location.c', cflags=args) + @parameterized({ '': ([],), 'assertions': (['-sGL_ASSERTIONS'],),