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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ See docs/process.md for more on how version tagging works.

6.0.13 - 10/19/26
-----------------
- The fixed-function fragment shaders generated by `LEGACY_GL_EMULATION` now use
`highp` precision where the GPU supports it, instead of always `mediump`,
which is half precision on some GPUs and cannot hold GL 1.x texture
coordinates accurately. (#27948)
- 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: 6 additions & 0 deletions src/lib/libglemu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,13 @@ var LibraryGLEmulation = {
}

var fsSource = [
// GL 1.x fixed function computes in (at least) fp32, which
// mediump does not guarantee (it is fp16 on many mobile GPUs).
'#ifdef GL_FRAGMENT_PRECISION_HIGH',
'precision highp float;',
'#else',
'precision mediump float;',
'#endif',
texUnitVaryingList,
texUnitUniformList,
'varying vec4 v_color;',
Expand Down
107 changes: 107 additions & 0 deletions test/browser/test_gl_ffp_texcoord_precision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Regression test for https://github.com/emscripten-core/emscripten/issues/27948
//
// The fixed-function fragment shaders that LEGACY_GL_EMULATION generates used
// to declare `precision mediump float`. Where mediump really is half precision
// (for example on Apple GPUs), texture coordinates not far from zero lose most
// of their fraction: around 1000, fp16 can only represent steps of 0.5. GL 1.x
// requires texture coordinates up to 2^10 in magnitude, computed to about one
// part in 10^5, so the generated shaders must use highp where it is available.
//
// On GPUs where mediump is full precision this test passes either way.

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

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

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

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Requesting a 16-bit color depth (16) in SDL_SetVideoMode can lead to precision loss in the color channels (e.g., RGB565 or RGBA4444) depending on how the platform/emulation layer handles it. Since the test asserts precise color values (e.g., px[c] > 200 and px[c] < 50), it is safer and more robust to request a 32-bit depth (32) to ensure 8-bit precision per channel (RGBA8888), which aligns with standard WebGL backbuffer precision.

Suggested change
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, 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 4x1 texture of red, green, blue and white texels, sampled NEAREST, so
// each texel covers a quarter of the s range.
GLubyte texels[4][4] = {
{0xFF, 0x00, 0x00, 0xFF},
{0x00, 0xFF, 0x00, 0xFF},
{0x00, 0x00, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
};
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, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels);
glEnable(GL_TEXTURE_2D);

// Texture coordinates run from 1000 to 1001 across the screen, and the
// texture matrix brings them back to 0..1.
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(-1000.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);

GLfloat xy[4][2] = {
{-1.0, -1.0},
{ 1.0, -1.0},
{ 1.0, 1.0},
{-1.0, 1.0},
};
GLfloat st[4][2] = {
{1000.0, 0.0},
{1001.0, 0.0},
{1001.0, 1.0},
{1000.0, 1.0},
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, xy);
glTexCoordPointer(2, GL_FLOAT, 0, st);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

// The middle of each quarter of the screen must show its own texel. With
// half-precision texture coordinates the second quarter (s = 1000.375)
// rounds to 1000.5 and shows the third texel instead.
const GLubyte expected[4][3] = {
{0xFF, 0x00, 0x00},
{0x00, 0xFF, 0x00},
{0x00, 0x00, 0xFF},
{0xFF, 0xFF, 0xFF},
};
for (int i = 0; i < 4; i++) {
GLubyte px[4];
read_pixel(80 + 160 * i, 240, px);
printf("quarter %d pixel: %d,%d,%d,%d\n", i, px[0], px[1], px[2], px[3]);
for (int c = 0; c < 3; c++) {
assert(expected[i][c] ? px[c] > 200 : px[c] < 50);
}
}

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_ffp_texcoord_precision(self):
self.btest_exit('test_gl_ffp_texcoord_precision.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