Skip to content

Commit

Permalink
port: when loading GL try 3.0 and 3.2 core as well
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Oct 5, 2023
1 parent d3d313c commit ed3bd97
Show file tree
Hide file tree
Showing 4 changed files with 1,025 additions and 192 deletions.
22 changes: 16 additions & 6 deletions port/fast3d/gfx_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ struct Framebuffer {

static std::map<pair<uint64_t, uint32_t>, struct ShaderProgram> shader_program_pool;
static GLuint opengl_vbo;
#ifdef __APPLE__
static GLuint opengl_vao;
#endif
static bool current_depth_mask;

static uint32_t frame_count;
Expand All @@ -59,6 +57,8 @@ static FilteringMode current_filter_mode = FILTER_LINEAR;

static GLenum gl_mirror_clamp = GL_MIRROR_CLAMP_TO_EDGE;

static bool gl_core_profile = false;

static int gfx_opengl_get_max_texture_size() {
GLint max_texture_size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
Expand Down Expand Up @@ -931,13 +931,23 @@ static void gfx_opengl_init(void) {
gl_mirror_clamp = GL_MIRRORED_REPEAT;
}

if ((GLVersion.major == 3 && GLVersion.minor >= 2) || GLVersion.major > 3) {
// check if we're using core profile, which is more strict
int val = 0;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &val);
gl_core_profile = (val == SDL_GL_CONTEXT_PROFILE_CORE);
}

glGenBuffers(1, &opengl_vbo);
glBindBuffer(GL_ARRAY_BUFFER, opengl_vbo);

#ifdef __APPLE__
glGenVertexArrays(1, &opengl_vao);
glBindVertexArray(opengl_vao);
#endif
if (gl_core_profile) {
// warn user that funny things can happen
sysLogPrintf(LOG_WARNING, "GL: using core profile, watch out for errors");
// core will explode if we don't use a VAO for our VBO
glGenVertexArrays(1, &opengl_vao);
glBindVertexArray(opengl_vao);
}

if (GLAD_GL_ARB_depth_clamp) {
glEnable(GL_DEPTH_CLAMP);
Expand Down
32 changes: 28 additions & 4 deletions port/fast3d/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
if (sysArgCheck("--debug-gl")) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
}
Expand All @@ -94,10 +92,36 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
sysFatalError("Could not open SDL window:\n%s", SDL_GetError());
}

ctx = SDL_GL_CreateContext(wnd);
// ideally we need 3.0 compat
// if that doesn't work, try 3.2 core in case we're on mac, 2.1 compat as a last resort
static const u32 glver[][3] = {
{ 3, 0, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY },
{ 3, 2, SDL_GL_CONTEXT_PROFILE_CORE },
{ 2, 1, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY },
};

ctx = NULL;
u32 vmin = 0, vmaj = 0;
for (u32 i = 0; i < sizeof(glver) / sizeof(*glver); ++i) {
vmaj = glver[i][0];
vmin = glver[i][1];
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, vmaj);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, vmin);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, glver[i][2]);
ctx = SDL_GL_CreateContext(wnd);
if (!ctx) {
sysLogPrintf(LOG_WARNING, "GL: could not create GL%d.%d context: %s", vmaj, vmin, SDL_GetError());
} else {
break;
}
}

if (!ctx) {
sysFatalError("Could not create GL2.1 context:\n%s", SDL_GetError());
sysFatalError("Could not create an OpenGL context of any supported version.\nSDL error: %s", SDL_GetError());
} else {
sysLogPrintf(LOG_NOTE, "GL: created GL%d.%d context", vmaj, vmin);
}

SDL_GL_MakeCurrent(wnd, ctx);
SDL_GL_SetSwapInterval(1);

Expand Down
Loading

0 comments on commit ed3bd97

Please sign in to comment.