Repository navigation
GLEmulation: use highp in generated fixed-function fragment shaders #28062
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
Open
TheTomBritton
wants to merge
1
commit into
emscripten-core:main
Choose a base branch
from
TheTomBritton:glemu-fs-highp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Requesting a 16-bit color depth (
16) inSDL_SetVideoModecan 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] > 200andpx[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.