[GLFW] Respect GLFW_NO_API flag (#12907)
In order to support the combined use of GLFW and WebGPU, GLFW
should not assign anything to the Emscripten modules context variable.
GLFW supports not creating any OpenGL context using the windows
attribute GLFW_CLIENT_API.
Basil Fierz authored 3 years ago
GitHub committed 3 years ago
526 | 526 | * Alexey Sokolov <sokolov@google.com> (copyright owned by Google, LLC) |
527 | 527 | * Ivan Romanovski <ivan.romanovski@gmail.com> |
528 | 528 | * Max Brunsfeld <maxbrunsfeld@gmail.com> |
529 | * Basil Fierz <basil.fierz@hotmail.com> |
999 | 999 | for (i = 0; i < GLFW.windows.length && GLFW.windows[i] == null; i++) { |
1000 | 1000 | // no-op |
1001 | 1001 | } |
1002 | var useWebGL = GLFW.hints[0x00022001] > 0; // Use WebGL when we are told to based on GLFW_CLIENT_API | |
1002 | 1003 | if (i == GLFW.windows.length) { |
1003 | var contextAttributes = { | |
1004 | antialias: (GLFW.hints[0x0002100D] > 1), // GLFW_SAMPLES | |
1005 | depth: (GLFW.hints[0x00021005] > 0), // GLFW_DEPTH_BITS | |
1006 | stencil: (GLFW.hints[0x00021006] > 0), // GLFW_STENCIL_BITS | |
1007 | alpha: (GLFW.hints[0x00021004] > 0) // GLFW_ALPHA_BITS | |
1004 | if (useWebGL) { | |
1005 | var contextAttributes = { | |
1006 | antialias: (GLFW.hints[0x0002100D] > 1), // GLFW_SAMPLES | |
1007 | depth: (GLFW.hints[0x00021005] > 0), // GLFW_DEPTH_BITS | |
1008 | stencil: (GLFW.hints[0x00021006] > 0), // GLFW_STENCIL_BITS | |
1009 | alpha: (GLFW.hints[0x00021004] > 0) // GLFW_ALPHA_BITS | |
1010 | } | |
1011 | #if OFFSCREEN_FRAMEBUFFER | |
1012 | // TODO: Make GLFW explicitly aware of whether it is being proxied or not, and set these to true only when proxying is being performed. | |
1013 | GL.enableOffscreenFramebufferAttributes(contextAttributes); | |
1014 | #endif | |
1015 | Module.ctx = Browser.createContext(Module['canvas'], true, true, contextAttributes); | |
1016 | } else { | |
1017 | Browser.init(); | |
1008 | 1018 | } |
1009 | #if OFFSCREEN_FRAMEBUFFER | |
1010 | // TODO: Make GLFW explicitly aware of whether it is being proxied or not, and set these to true only when proxying is being performed. | |
1011 | GL.enableOffscreenFramebufferAttributes(contextAttributes); | |
1012 | #endif | |
1013 | Module.ctx = Browser.createContext(Module['canvas'], true, true, contextAttributes); | |
1014 | 1019 | } |
1015 | 1020 | |
1016 | 1021 | // If context creation failed, do not return a valid window |
1017 | if (!Module.ctx) return 0; | |
1022 | if (!Module.ctx && useWebGL) return 0; | |
1018 | 1023 | |
1019 | 1024 | // Get non alive id |
1020 | 1025 | var win = new GLFW_Window(id, width, height, title, monitor, share); |
83 | 83 | { |
84 | 84 | int x, y, w, h; |
85 | 85 | glfwDefaultWindowHints(); |
86 | glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); | |
86 | glfwWindowHint(GLFW_CLIENT_API, CLIENT_API); | |
87 | 87 | |
88 | 88 | window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL); |
89 | 89 | assert(window != NULL); |
141 | 141 | window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL); |
142 | 142 | assert(window != NULL); |
143 | 143 | |
144 | assert(glfwGetWindowAttrib(window, GLFW_CLIENT_API) == GLFW_OPENGL_ES_API); | |
144 | assert(glfwGetWindowAttrib(window, GLFW_CLIENT_API) == CLIENT_API); | |
145 | 145 | |
146 | 146 | assert(glfwGetWindowUserPointer(window) == NULL); |
147 | 147 | glfwSetWindowUserPointer(window, userptr); |
194 | 194 | glfwSetTime(0); |
195 | 195 | } |
196 | 196 | |
197 | #if CLIENT_API == GLFW_OPENGL_ES_API | |
197 | 198 | { |
198 | 199 | glfwMakeContextCurrent(window); // stub |
199 | 200 | assert(glfwGetCurrentContext() == window); |
205 | 206 | assert(glfwExtensionSupported("nonexistant") == 0); |
206 | 207 | assert(glfwGetProcAddress("nonexistant") == NULL); |
207 | 208 | } |
209 | #endif | |
208 | 210 | |
209 | 211 | glfwTerminate(); |
210 | 212 |
2782 | 2782 | in_html('200') |
2783 | 2783 | |
2784 | 2784 | @requires_graphics_hardware |
2785 | def test_glfw3(self): | |
2785 | @parameterized({ | |
2786 | 'no_gl': (['-DCLIENT_API=GLFW_NO_API'],), | |
2787 | 'gl_es': (['-DCLIENT_API=GLFW_OPENGL_ES_API'],) | |
2788 | }) | |
2789 | def test_glfw3(self, args): | |
2786 | 2790 | for opts in [[], ['-s', 'LEGACY_GL_EMULATION=1'], ['-Os', '--closure', '1']]: |
2787 | 2791 | print(opts) |
2788 | self.btest(path_from_root('tests', 'glfw3.c'), args=['-s', 'USE_GLFW=3', '-lglfw', '-lGL'] + opts, expected='1') | |
2792 | self.btest(path_from_root('tests', 'glfw3.c'), args=['-s', 'USE_GLFW=3', '-lglfw', '-lGL'] + args + opts, expected='1') | |
2789 | 2793 | |
2790 | 2794 | @requires_graphics_hardware |
2791 | 2795 | def test_glfw_events(self): |