Turn TMS_USE_GLES compile-time option into a runtime use_gles setting

This of course requires support from the SDL library Principia is linked to, but should still make it more accessible to switch between the two on Linux
This commit is contained in:
ROllerozxa 2026-06-23 23:38:23 +02:00
commit 462369618e
12 changed files with 138 additions and 132 deletions

View file

@ -1202,20 +1202,20 @@ game::init_framebuffers()
this->bloom_fb = 0;
}
#ifndef TMS_USE_GLES
if (settings["postprocess"]->v.b) {
tms_infof("Postprocess time");
this->main_fb = tms_fb_alloc(_tms.window_width, _tms.window_height, 0);
tms_fb_add_texture(this->main_fb, GL_RGBA, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
tms_fb_enable_depth(this->main_fb, GL_DEPTH_COMPONENT16);
if (!_tms.use_gles) {
if (settings["postprocess"]->v.b) {
tms_infof("Postprocess time");
this->main_fb = tms_fb_alloc(_tms.window_width, _tms.window_height, 0);
tms_fb_add_texture(this->main_fb, GL_RGBA, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
tms_fb_enable_depth(this->main_fb, GL_DEPTH_COMPONENT16);
this->bloom_fb = tms_fb_alloc(_tms.window_width, _tms.window_height, 1);
tms_fb_add_texture(this->bloom_fb, GL_RGBA, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR);
} else {
this->main_fb = 0;
this->bloom_fb = 0;
this->bloom_fb = tms_fb_alloc(_tms.window_width, _tms.window_height, 1);
tms_fb_add_texture(this->bloom_fb, GL_RGBA, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR);
} else {
this->main_fb = 0;
this->bloom_fb = 0;
}
}
#endif
}
void
@ -2859,10 +2859,8 @@ game::render()
tms_fb_swap_blur3x3(tms_pipeline_get_framebuffer(3));
}
#ifndef TMS_USE_GLES
if (settings["postprocess"]->v.b)
if (!_tms.use_gles && settings["postprocess"]->v.b)
tms_fb_bind(this->main_fb);
#endif
tms_assertf((ierr = glGetError()) == 0, "gl error %d in game::render after shadow/ao", ierr);
glDisable(GL_BLEND);
@ -2871,10 +2869,8 @@ game::render()
tms_assertf((ierr = glGetError()) == 0, "gl error %d in game::render before bg", ierr);
#ifndef TMS_USE_GLES
if (settings["gamma_correct"]->v.b && !settings["postprocess"]->v.b)
if (!_tms.use_gles && settings["gamma_correct"]->v.b && !settings["postprocess"]->v.b)
glEnable(GL_FRAMEBUFFER_SRGB);
#endif
if (this->state.abo_architect_mode) {
glClearColor(.25f, .25f, .25f, 1.f);
@ -2994,11 +2990,9 @@ game::render()
tms_assertf((ierr = glGetError()) == 0, "gl error %d after render foreground", ierr);
}
#ifndef TMS_USE_GLES
if (settings["gamma_correct"]->v.b && !settings["postprocess"]->v.b) {
if (!_tms.use_gles && settings["gamma_correct"]->v.b && !settings["postprocess"]->v.b) {
glDisable(GL_FRAMEBUFFER_SRGB);
}
#endif
glDepthMask(true);
glDisable(GL_BLEND);
@ -3007,8 +3001,7 @@ game::render()
tms_ddraw_set_matrices(this->dd, this->cam->view, this->cam->projection);
//tms_ddraw_line3d(this->dd, 0, 0, 0, this->light.x*2.f, this->light.y*2.f, this->light.z*2.f);
#ifndef TMS_USE_GLES
if (settings["postprocess"]->v.b) {
if (!_tms.use_gles && settings["postprocess"]->v.b) {
tms_fb_unbind(this->main_fb);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
@ -3064,7 +3057,6 @@ game::render()
glEnable(GL_DEPTH_TEST);
tms_assertf((ierr = glGetError()) == 0, "gl error %d after postprocess", ierr);
}
#endif
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);

View file

@ -260,19 +260,17 @@ void init_framebuffers() {
if (settings["shadow_map_precision"]->v.i == 0) {
shadow_map_precision = GL_RGB;
} else if (settings["shadow_map_precision"]->v.i == 1) {
#ifdef TMS_USE_GLES
/* Android does not seem to have either GL_RGB16F or GL_RGBA16F defined */
shadow_map_precision = GL_RGB;
#else
shadow_map_precision = GL_RGB16F;
#endif
if (!_tms.use_gles)
/* Android does not seem to have either GL_RGB16F or GL_RGBA16F defined */
shadow_map_precision = GL_RGB;
else
shadow_map_precision = GL_RGB16F;
} else if (settings["shadow_map_precision"]->v.i == 2) {
#ifdef TMS_USE_GLES
/* Android does not seem to have either GL_RGB32F or GL_RGBA32F defined */
shadow_map_precision = GL_RGB;
#else
shadow_map_precision = GL_RGB32F;
#endif
if (_tms.use_gles)
/* Android does not seem to have either GL_RGB32F or GL_RGBA32F defined */
shadow_map_precision = GL_RGB;
else
shadow_map_precision = GL_RGB32F;
}
tms_fb_add_texture(gi_fb, shadow_map_precision, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
@ -1128,46 +1126,49 @@ void tproject_quit() {
}
void setup_opengl_settings() {
#ifdef TMS_USE_GLES
if (settings["shadow_map_precision"]->is_uninitialized()) {
settings["shadow_map_precision"]->v.i = 0;
}
if (settings["shadow_map_depth_texture"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_OES_depth_texture") != 0) {
tms_infof("GL_OES_depth_texture: YES");
settings["shadow_map_depth_texture"]->v.b = 1;
} else {
tms_infof("GL_OES_depth_texture: NO");
settings["shadow_map_depth_texture"]->v.b = 0;
}
}
#else
if (settings["shadow_map_precision"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_ARB_texture_float") != 0
|| strstr(_tms.gl_extensions, "GL_ATI_texture_float") != 0) {
tms_infof("GL_ARB_texture_float: YES");
settings["shadow_map_precision"]->v.i = 1;
} else {
tms_infof("GL_ARB_texture_float: NO");
if (_tms.use_gles) {
if (settings["shadow_map_precision"]->is_uninitialized()) {
settings["shadow_map_precision"]->v.i = 0;
}
}
if (settings["gamma_correct"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_EXT_texture_sRGB") != 0
&& strstr(_tms.gl_extensions, "GL_EXT_framebuffer_sRGB") != 0) {
tms_infof("GL_EXT_texture_sRGB+GL_EXT_framebuffer_sRGB: YES");
settings["gamma_correct"]->v.b = 1;
} else {
tms_infof("GL_EXT_texture_sRGB+GL_EXT_framebuffer_sRGB: NO");
settings["gamma_correct"]->v.b = 0;
if (settings["shadow_map_depth_texture"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_OES_depth_texture") != 0) {
tms_infof("GL_OES_depth_texture: YES");
settings["shadow_map_depth_texture"]->v.b = 1;
} else {
tms_infof("GL_OES_depth_texture: NO");
settings["shadow_map_depth_texture"]->v.b = 0;
}
}
}
// GL_ARB_depth_texture is OpenGL 1.5+, so we can always assume it to exist on desktop
settings["shadow_map_depth_texture"]->v.b = 1;
#endif
// XXX: We need to really fix gamma correction on GLES...
settings["gamma_correct"]->v.b = 0;
} else {
if (settings["shadow_map_precision"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_ARB_texture_float") != 0
|| strstr(_tms.gl_extensions, "GL_ATI_texture_float") != 0) {
tms_infof("GL_ARB_texture_float: YES");
settings["shadow_map_precision"]->v.i = 1;
} else {
tms_infof("GL_ARB_texture_float: NO");
settings["shadow_map_precision"]->v.i = 0;
}
}
if (settings["gamma_correct"]->is_uninitialized()) {
if (strstr(_tms.gl_extensions, "GL_EXT_texture_sRGB") != 0
&& strstr(_tms.gl_extensions, "GL_EXT_framebuffer_sRGB") != 0) {
tms_infof("GL_EXT_texture_sRGB+GL_EXT_framebuffer_sRGB: YES");
settings["gamma_correct"]->v.b = 1;
} else {
tms_infof("GL_EXT_texture_sRGB+GL_EXT_framebuffer_sRGB: NO");
settings["gamma_correct"]->v.b = 0;
}
}
// GL_ARB_depth_texture is OpenGL 1.5+, so we can always assume it to exist on desktop
settings["shadow_map_depth_texture"]->v.b = 1;
}
}
void tproject_preinit() {
@ -1176,6 +1177,10 @@ void tproject_preinit() {
if (!settings.load())
tms_infof("ERROR!");
if (settings["use_gles"]->v.b)
_tms.use_gles = true;
settings.save();
tms_infof("Shadow quality: %d (%dx%d)",

View file

@ -1055,14 +1055,14 @@ material_factory::init_shaders()
tms_shader_global_clear_defines();
#ifndef TMS_USE_GLES
tms_shader_global_define_vs("lowp", "");
tms_shader_global_define_fs("lowp", "");
tms_shader_global_define_vs("mediump", "");
tms_shader_global_define_fs("mediump", "");
tms_shader_global_define_vs("highp", "");
tms_shader_global_define_fs("highp", "");
#endif
if (!_tms.use_gles) {
tms_shader_global_define_vs("lowp", "");
tms_shader_global_define_fs("lowp", "");
tms_shader_global_define_vs("mediump", "");
tms_shader_global_define_fs("mediump", "");
tms_shader_global_define_vs("highp", "");
tms_shader_global_define_fs("highp", "");
}
if (settings["shadow_map_precision"]->v.i == 0 && !settings["shadow_map_depth_texture"]->is_true())
tms_shader_global_define("SHADOW_BIAS", ".15");

View file

@ -50,8 +50,10 @@ static void apply_very_bad_settings()
#ifdef TMS_USE_GLES
#define GAMMA_CORRECT_DEFAULT false
#define USE_GLES true
#else
#define GAMMA_CORRECT_DEFAULT -1
#define USE_GLES false
#endif
void
@ -62,6 +64,8 @@ _settings::init()
/** -Graphics **/
this->add("debug", S_BOOL, false);
this->add("use_gles", S_BOOL, USE_GLES);
// Shadows
this->add("enable_shadows", S_BOOL, ENABLE_SHADOWS_DEFAULT);
this->add("shadow_quality", S_UINT8, 1);

View file

@ -233,27 +233,27 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv)
tms_infof("Device PPCM: %f %f", _tms.xppcm, _tms.yppcm);
#endif
#ifdef TMS_USE_GLES
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
if (_tms.use_gles) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
#else
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
#endif
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
} else {
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
}
SDL_GLContext gl_context = SDL_GL_CreateContext(_window);
if (gl_context == NULL)
tms_fatalf("Error creating GL Context: %s", SDL_GetError());
#ifdef TMS_USE_GLES
int version = gladLoadGLES2((GLADloadfunc)SDL_GL_GetProcAddress);
#else
int version = gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress);
#endif
int version;
if (_tms.use_gles)
version = gladLoadGLES2((GLADloadfunc)SDL_GL_GetProcAddress);
else
version = gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress);
tms_infof("Loaded GL version %d.%d", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
tms_infof("GL Info: %s/%s/%s", glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_VERSION));

View file

@ -700,10 +700,10 @@ tms_fb_add_texture(struct tms_fb *fb, int format,
bufs[y] = GL_COLOR_ATTACHMENT0 + y;
}
#ifndef TMS_USE_GLES
glDrawBuffers(fb->num_textures+1, bufs);
tms_assertf((ierr = glGetError()) == 0, "gl error %d in tms_fb_add_texture %d 15", ierr, x);
#endif
if (!tms.use_gles) {
glDrawBuffers(fb->num_textures+1, bufs);
tms_assertf((ierr = glGetError()) == 0, "gl error %d in tms_fb_add_texture %d 15", ierr, x);
}
}
fb->num_textures ++;

View file

@ -138,7 +138,10 @@ static GLint compile(struct tms_shader *sh, GLenum st, const char *src)
num_src += sh->num_fs_defines + _tms_global_shader.num_fs_defines;
char *sources[num_src+1];
sources[0] = TMS_GLSL_HEADER;
if (tms.use_gles)
sources[0] = TMS_GLSL_HEADER_GLES;
else
sources[0] = TMS_GLSL_HEADER_GL;
for (int x=0; x<sh->num_defines; x++)
sources[1+x] = sh->defines[x];

View file

@ -6,15 +6,14 @@
#include <tms/math/vector.h>
#include <tms/core/tms.h>
#ifdef TMS_USE_GLES
#define TMS_GLSL_HEADER\
#define TMS_GLSL_HEADER_GLES\
"#version 100\n"\
"precision mediump float;"\
"precision mediump int;"\
"\n"
#else
#define TMS_GLSL_HEADER "#version 110\n"
#endif
#define TMS_GLSL_HEADER_GL "#version 110\n"
struct tms_mesh_attribute;

View file

@ -379,22 +379,18 @@ tms_texture_upload(struct tms_texture *tex)
colors = GL_RGB;
format = GL_RGB;
#ifndef TMS_USE_GLES
if (tex->gamma_correction) {
if (!tms.use_gles && tex->gamma_correction) {
format = GL_SRGB;
}
#endif
break;
case 4:
colors = GL_RGBA;
format = GL_RGBA;
#ifndef TMS_USE_GLES
if (tex->gamma_correction) {
if (!tms.use_gles && tex->gamma_correction) {
format = GL_SRGB8_ALPHA8;
}
#endif
break;
default:
@ -408,12 +404,12 @@ tms_texture_upload(struct tms_texture *tex)
glBindTexture(GL_TEXTURE_2D, tex->gl_texture);
#ifndef TMS_USE_GLES
glEnable(GL_TEXTURE_2D);
if (tex->filter == TMS_MIPMAP) {
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
if (!tms.use_gles) {
glEnable(GL_TEXTURE_2D);
if (tex->filter == TMS_MIPMAP) {
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
}
}
#endif
glTexImage2D(GL_TEXTURE_2D, 0, format,
tex->width, tex->height,
@ -426,15 +422,16 @@ tms_texture_upload(struct tms_texture *tex)
if (tex->filter == TMS_MIPMAP) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#ifdef TMS_USE_GLES
glGenerateMipmap(GL_TEXTURE_2D);
int err = glGetError();
if (err != 0) {
tms_infof("error: could not create mipmaps (%d)", err);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (tms.use_gles) {
glGenerateMipmap(GL_TEXTURE_2D);
int err = glGetError();
if (err != 0) {
tms_infof("error: could not create mipmaps (%d)", err);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
#endif
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, tex->filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, tex->filter);

View file

@ -19,6 +19,7 @@ struct tms_singleton _tms = {
.gl_extensions = "",
.delta_cap = 0,
.emulating_portrait = 0,
.use_gles = false
};
int tms_preinit(void) {
@ -38,14 +39,15 @@ tms_init(void)
tmat4_set_ortho(tms.window_projection, 0, tms.window_width, 0, tms.window_height, 1, -1);
#ifndef TMS_USE_GLES
tms_shader_global_define_vs("lowp", "");
tms_shader_global_define_fs("lowp", "");
tms_shader_global_define_vs("mediump", "");
tms_shader_global_define_fs("mediump", "");
tms_shader_global_define_vs("highp", "");
tms_shader_global_define_fs("highp", "");
#endif
if (!tms.use_gles) {
tms_shader_global_define_vs("lowp", "");
tms_shader_global_define_fs("lowp", "");
tms_shader_global_define_vs("mediump", "");
tms_shader_global_define_fs("mediump", "");
tms_shader_global_define_vs("highp", "");
tms_shader_global_define_fs("highp", "");
}
tproject_init();
tproject_init_pipelines();

View file

@ -91,6 +91,11 @@ extern struct tms_singleton {
int emulating_portrait;
int in_frame;
/// Whether to use OpenGL ES 2.0 or not. (False uses desktop OpenGL 2.1+ codepaths)
/// This variable is set from the "use_gles" setting during preinit and is the same as
/// `settings["use_gles"]->v.b`, but is also available in TMS code.
bool use_gles;
float window_projection[16];
struct tms_screen *screen; /**< current screen */
struct tms_screen *active_screen;

View file

@ -1,5 +1,6 @@
#include "varray.h"
#include "gbuffer.h"
#include "tms.h"
#include <string.h>
#include <tms/core/err.h>
@ -117,12 +118,10 @@ tms_varray_bind_attributes(struct tms_varray *va,
;
}
#ifndef TMS_USE_GLES
if (m->component_type == GL_UNSIGNED_INT || m->component_type == GL_INT)
if (!tms.use_gles && (m->component_type == GL_UNSIGNED_INT || m->component_type == GL_INT))
glVertexAttribIPointer(locations[x], m->num_components, m->component_type,
bufdata->vsize, (void*)(uintptr_t)m->offset);
else
#endif
glVertexAttribPointer(locations[x], m->num_components, m->component_type,
0, bufdata->vsize, (void*)(uintptr_t)m->offset);