Repository navigation
GLEmulation: fix texture-load merging when a texture matrix is in use #28061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
|
@@ -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; | ||
| var loadCounter = 0; | ||
| var load; | ||
|
|
||
|
|
@@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If any texture load expression (such as texLoadRegex = /(texture(?:(?!texture).)*?\((?:(?!texture).)*?\))\/\*endload\*\//g; |
||
| } | ||
| } | ||
| return [texLoadLines + lines]; | ||
|
|
||
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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\*\//gcan 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 anothertexturekeyword, making the regex robust against missing sentinels or other occurrences of the wordtextureon the same line.