Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ See docs/process.md for more on how version tagging works.

6.0.13 - 10/19/26
-----------------
- Fixed invalid fixed-function fragment shaders generated by
`LEGACY_GL_EMULATION` for `GL_COMBINE` texture environments that use the
texture more than once while a texture matrix is in use. (#27946)
- Support for windows .bat/.ps1 launcher files was completely removed. These
days we use a native windows launcher program. (#27359)
- `MEMORY64=2` was marked as deprecated. (#27953)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/libglemu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ var LibraryGLEmulation = {
if (TEX_MATRIX_UNIFORM_PREFIX != null) {
texCoordExpr = `(${TEX_MATRIX_UNIFORM_PREFIX}${texUnitID} * ${texCoordExpr})`;
}
return `${func}(${TEX_UNIT_UNIFORM_PREFIX}${texUnitID}, ${texCoordExpr}.xy)`;
return `${func}(${TEX_UNIT_UNIFORM_PREFIX}${texUnitID}, ${texCoordExpr}.xy)/*endload*/`;
}

function getTypeFromCombineOp(op) {
Expand Down Expand Up @@ -1314,7 +1314,7 @@ var LibraryGLEmulation = {
var lines = this.env.genPassLines(passOutputVar, passInputVar, texUnitID).join('\n');

var texLoadLines = '';
var texLoadRegex = /(texture.*?\(.*?\))/g;
var texLoadRegex = /(texture.*?\(.*?\))\/\*endload\*\//g;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If any texture load expression (such as texture2DProj, as noted in the PR description) does not have the /*endload*/ sentinel, the non-greedy .*? in the regex /(texture.*?\(.*?\))\/\*endload\*\//g can backtrack and match across multiple texture calls to find the next available /*endload*/. This would incorrectly merge multiple distinct texture calls into a single invalid expression, leading to shader compilation failures. Using (?:(?!texture).)*? instead of .*? ensures that the match cannot cross another texture keyword, making the regex robust against missing sentinels or other occurrences of the word texture on the same line.

        var texLoadRegex = /(texture(?:(?!texture).)*?\((?:(?!texture).)*?\))\/\*endload\*\//g;

var loadCounter = 0;
var load;

Expand All @@ -1330,7 +1330,7 @@ var LibraryGLEmulation = {
texLoadLines += texLoadLine + '\n'; // Store the generated texture load statements in a temp string to not confuse regex search in progress.
lines = lines.split(texLoadExpr).join(texLoadVar);
// Reset regex search, since we modified the string.
texLoadRegex = /(texture.*\(.*\))/g;
texLoadRegex = /(texture.*?\(.*?\))\/\*endload\*\//g;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If any texture load expression (such as texture2DProj, as noted in the PR description) does not have the /*endload*/ sentinel, the non-greedy .*? in the regex /(texture.*?\(.*?\))\/\*endload\*\//g can backtrack and match across multiple texture calls to find the next available /*endload*/. This would incorrectly merge multiple distinct texture calls into a single invalid expression, leading to shader compilation failures. Using (?:(?!texture).)*? instead of .*? ensures that the match cannot cross another texture keyword, making the regex robust against missing sentinels or other occurrences of the word texture on the same line.

            texLoadRegex = /(texture(?:(?!texture).)*?\((?:(?!texture).)*?\))\/\*endload\*\//g;

}
}
return [texLoadLines + lines];
Expand Down
130 changes: 130 additions & 0 deletions test/browser/test_gl_texenv_combine_texmatrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Regression test for https://github.com/emscripten-core/emscripten/issues/27946
//
// GL_COMBINE texture environments that use the texture more than once, drawn
// while a texture matrix is in use. The generated fragment shader samples the
// texture as `texture2D(u_texUnit0, (u_textureMatrix0 * v_texCoord0).xy)`, and
// the pass that merges repeated texture loads into one variable used to find
// them with a regex that stopped at the first `)` -- the one closing the
// matrix product -- which turned the shader into invalid GLSL, so nothing was
// drawn.

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

void read_pixel(int x, int y, GLubyte rgba[4]) {
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
}

void check_rgb(const char *name, const GLubyte px[4], int r, int g, int b) {
printf("%s pixel: %d,%d,%d,%d\n", name, px[0], px[1], px[2], px[3]);
assert(abs(px[0] - r) <= 8 && abs(px[1] - g) <= 8 && abs(px[2] - b) <= 8);
}

int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_Surface *screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL);
if (!screen) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}

glClearColor(0, 0, 0, 0);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);

// A single half-transparent green texel.
GLubyte texel[4] = {0x00, 0xFF, 0x00, 0x80};
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texel);
glEnable(GL_TEXTURE_2D);

// Any texture matrix use puts the matrix into the sample expression.
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.25, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);

// One interleaved array (the emulation's fast path): a red quad on each
// half of the screen, with texture coordinates.
typedef struct {
GLfloat x, y;
GLfloat s, t;
GLubyte rgba[4];
} Vertex;
Vertex quads[8] = {
{-1.0, -1.0, 0.0, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 0.0, -1.0, 1.0, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 0.0, 1.0, 1.0, 1.0, {0xFF, 0x00, 0x00, 0xFF}},
{-1.0, 1.0, 0.0, 1.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 0.0, -1.0, 0.0, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 1.0, -1.0, 1.0, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 1.0, 1.0, 1.0, 1.0, {0xFF, 0x00, 0x00, 0xFF}},
{ 0.0, 1.0, 0.0, 1.0, {0xFF, 0x00, 0x00, 0xFF}},
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &quads[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &quads[0].s);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), quads[0].rgba);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

// Left half: rgb = texture.rgb * texture.a, two loads on one line.
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

// Right half: rgb = mix(primary color, texture, texture.a), the texture
// loaded on two lines.
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glDrawArrays(GL_TRIANGLE_FAN, 4, 4);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

GLubyte px[4];
read_pixel(160, 240, px);
check_rgb("left", px, 0x00, 0x80, 0x00); // green * 0.5
read_pixel(480, 240, px);
check_rgb("right", px, 0x7F, 0x80, 0x00); // half red, half green

SDL_GL_SwapBuffers();
SDL_Quit();
return 0;
}
4 changes: 4 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,10 @@ def test_gl_renderers(self):
def test_gl_stride(self):
self.reftest('gl_stride.c', cflags=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@requires_graphics_hardware
def test_gl_texenv_combine_texmatrix(self):
self.btest_exit('test_gl_texenv_combine_texmatrix.c', cflags=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@requires_graphics_hardware
def test_gl_vertex_buffer_pre(self):
self.reftest('gl_vertex_buffer_pre.c', cflags=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
Expand Down