Skip to content
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

Fix glfw3 default hints being modified #20770

Merged
merged 8 commits into from
Nov 30, 2023
13 changes: 9 additions & 4 deletions src/library_glfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,13 @@ var LibraryGLFW = {
}
},

defaultWindowHints: () => {
GLFW.hints = {};
for (var k in GLFW.defaultHints) {
GLFW.hints[k] = GLFW.defaultHints[k];
}
ypujante marked this conversation as resolved.
Show resolved Hide resolved
},

createWindow: (width, height, title, monitor, share) => {
var i, id;
for (i = 0; i < GLFW.windows.length && GLFW.windows[i] !== null; i++) {
Expand Down Expand Up @@ -1124,7 +1131,7 @@ var LibraryGLFW = {
if (GLFW.windows) return 1; // GL_TRUE

GLFW.initialTime = GLFW.getTime();
GLFW.hints = GLFW.defaultHints;
GLFW.defaultWindowHints();
GLFW.windows = new Array()
GLFW.active = null;
GLFW.scale = _emscripten_get_device_pixel_ratio();
Expand Down Expand Up @@ -1320,9 +1327,7 @@ var LibraryGLFW = {

glfwSetGammaRamp: (monitor, ramp) => { throw "glfwSetGammaRamp not implemented."; },

glfwDefaultWindowHints: () => {
GLFW.hints = GLFW.defaultHints;
},
glfwDefaultWindowHints: () => GLFW.defaultWindowHints(),

glfwWindowHint: (target, hint) => {
GLFW.hints[target] = hint;
Expand Down
86 changes: 86 additions & 0 deletions test/browser/test_glfw3_default_hints.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2023 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.
*/

#include <GLFW/glfw3.h>
#include <assert.h>
#include <emscripten/html5.h>

static void checkDefaultWindowHints()
ypujante marked this conversation as resolved.
Show resolved Hide resolved
{
int ok = EM_ASM_INT({
var res = 1;
for (var k in TEST_GLFW3_DEFAULTS_HINTS) {
if(GLFW.defaultHints[k] !== TEST_GLFW3_DEFAULTS_HINTS[k])
ypujante marked this conversation as resolved.
Show resolved Hide resolved
res = 0;
}
return res;
});
assert(ok == 1);
}

int main() {

EM_ASM(
TEST_GLFW3_DEFAULTS_HINTS = {};
TEST_GLFW3_DEFAULTS_HINTS[0x00020001] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00020002] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00020003] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00020004] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00020005] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x0002000A] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002200C] = 0;

TEST_GLFW3_DEFAULTS_HINTS[0x00021001] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021002] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021003] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021004] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021005] = 24;
TEST_GLFW3_DEFAULTS_HINTS[0x00021006] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021007] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00021008] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00021009] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100A] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100B] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100C] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100D] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100E] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100F] = 0;

TEST_GLFW3_DEFAULTS_HINTS[0x00022001] = 0x00030001;
TEST_GLFW3_DEFAULTS_HINTS[0x00022002] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00022003] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022004] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022005] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022006] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022007] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022008] = 0;
);

assert(glfwInit() == GL_TRUE);

// make sure the defaults are the ones expected
checkDefaultWindowHints();

// GLFW_DEPTH_BITS
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
glfwWindowHint(GLFW_DEPTH_BITS, 16);
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 16);

// default hints should NOT be touched!
checkDefaultWindowHints();

// reset hints
glfwDefaultWindowHints();
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if you can test this without using EM_JS and instead using glfwGetWindowParam?

Then maybe we can avoiding hardcoded values like 0x00021005 in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that glfwGetWindowParam is not part of the GLW3 api. According to the documentation it has been replaced by glfwGetWindowAttrib, but it requires a window as an argument which I don't have in the test because I never create a window.


// still not touched
checkDefaultWindowHints();

glfwTerminate();

return 0;
}
4 changes: 4 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,10 @@ def in_html(expected):

in_html('200')

@requires_graphics_hardware
def test_glfw3_default_hints(self):
self.btest_exit('test_glfw3_default_hints.c', args=['-sUSE_GLFW=3', '-lglfw', '-lGL'])
ypujante marked this conversation as resolved.
Show resolved Hide resolved

@requires_graphics_hardware
@parameterized({
'no_gl': (['-DCLIENT_API=GLFW_NO_API'],),
Expand Down