emscripten-core / emscripten-core/emscripten

SDL2 fails to create opengl window in wasm64 builds

Open
#20,273 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

SDL2 fails to create an opengl window with error message "Could not create GLES window surface"

Reproducible code:

```c
#include "SDL.h"
#include
#include

#include
#include

#define WIDTH 640
#define HEIGHT 480

void onDraw(void *arg) {
SDL_Window *window = *((SDL_Window **)arg);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
}

int main(int argc, char *argv[]) {
int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
if (res) {
fprintf(stderr, "Error initializing SDL %s", SDL_GetError());
}
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

SDL_Window *window = SDL_CreateWindow("OpenGL Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT,
SDL_WINDOW_OPENGL);
if (!window) {
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
return 0;
}
SDL_SetWindowResizable(window, SDL_TRUE);

SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
fprintf(stderr, "Couldn't create context: %s\n", SDL_GetError());
return 0;
}

GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
{
const GLchar *source = "attribute vec4 apos;"
"attribute vec4 acolor;"
"varying vec4 color;"
"void main() {"
"color = acolor;"
"gl_Position = apos;"
"}";
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &source, NULL);
glCompileShader(vertex_shader);
}
{
const GLchar *source = "precision lowp float;"
"varying vec4 color;"
"void main() {"
"gl_FragColor = color;"
"}";
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &source, NULL);
glCompileShader(fragment_shader);
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glUseProgram(program);
// clang-format off
const float pos_and_color[] = {
// x, y, r, g, b
-0.6f, -0.6f, 1, 0, 0,
0.6f, -0.6f, 0, 1, 0,
0.f, 0.6f, 0, 0, 1,
};
// clang-format on
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(pos_and_color), pos_and_color,
GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 20, (void *)8);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

emscripten_set_main_loop_arg(onDraw, &window, 0, 1);
}
```

## 32-bit works.

```sh
$ emcc main.c -o index.html -sUSE_SDL=2 -sUSE_WEBGL2
$ python3 -m http.server
```
![image](https://github.com/emscripten-core/emscripten/assets/40538987/881dbe8a-99cb-42ba-bcc6-001f5c3f6c8c)


## 64-bit doesn't work

```sh
$ emcc main.c -o index.html -sMEMORY64=1 -sMAXIMUM_MEMORY=6GB -sALLOW_MEMORY_GROWTH -sWASM_BIGINT=1 -sUSE_SDL=2 -sUSE_WEBGL2
$ python3 -m http.server
```
![image](https://github.com/emscripten-core/emscripten/assets/40538987/e1902f05-8091-4323-85f3-8ed2115a90ab)

**Version of emscripten/emsdk:**
```sh
emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.46-git (7c7321c265c47d54dacdd25d00c2c3c9ec2a8425)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

**Version of browser:**
Chromium Version 119.0.6011.0 (Developer Build) custom (64-bit)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.