Skip to content

Commit

Permalink
Be more firendly to OpenGL ES.
Browse files Browse the repository at this point in the history
Add some fixes to make renderer easier to build with OpenGL ES 1.x
headers.

Add runtime checks for OpenGL ES profile.
  • Loading branch information
skullernet committed Dec 1, 2014
1 parent c507771 commit e0f92dd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/refresh/gl/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*
*/

#if 0
#ifdef GL_VERSION_ES_CM_1_0
#define QGL_INDEX_TYPE GLushort
#define QGL_INDEX_ENUM GL_UNSIGNED_SHORT
#else
Expand Down Expand Up @@ -98,6 +98,8 @@ typedef struct {
} glRefdef_t;

typedef struct {
qboolean es_profile;

int version_major;
int version_minor;

Expand All @@ -113,9 +115,15 @@ typedef struct {
int stencilbits;
} glConfig_t;

#define AT_LEAST_OPENGL(major, minor) \
#define AT_LEAST_OPENGL_ANY(major, minor) \
(gl_config.version_major > major || (gl_config.version_major == major && gl_config.version_minor >= minor))

#define AT_LEAST_OPENGL(major, minor) \
(!gl_config.es_profile && AT_LEAST_OPENGL_ANY(major, minor))

#define AT_LEAST_OPENGL_ES(major, minor) \
(gl_config.es_profile && AT_LEAST_OPENGL_ANY(major, minor))

extern glStatic_t gl_static;
extern glConfig_t gl_config;
extern glRefdef_t glr;
Expand Down
21 changes: 18 additions & 3 deletions src/refresh/gl/images.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ static void gl_bilerp_pics_changed(cvar_t *self)

static void gl_texturebits_changed(cvar_t *self)
{
if (self->integer > 16) {
// ES doesn't support internal format != external
if (AT_LEAST_OPENGL_ES(1, 0)) {
gl_tex_alpha_format = GL_RGBA;
gl_tex_solid_format = GL_RGBA;
#ifdef GL_VERSION_1_1
} else if (self->integer > 16) {
gl_tex_alpha_format = GL_RGBA8;
gl_tex_solid_format = GL_RGB8;
} else if (self->integer > 8) {
Expand All @@ -167,6 +172,7 @@ static void gl_texturebits_changed(cvar_t *self)
} else if (self->integer > 0) {
gl_tex_alpha_format = GL_RGBA2;
gl_tex_solid_format = GL_R3_G3_B2;
#endif
} else {
gl_tex_alpha_format = GL_RGBA;
gl_tex_solid_format = GL_RGB;
Expand Down Expand Up @@ -274,7 +280,8 @@ static int GL_GrayScaleTexture(byte *in, int inwidth, int inheight, imagetype_t
p[2] = y + (b - y) * colorscale;
}

if (colorscale == 0)
// ES doesn't support internal format != external
if (colorscale == 0 && !AT_LEAST_OPENGL_ES(1, 0))
return GL_LUMINANCE;

return gl_tex_solid_format;
Expand Down Expand Up @@ -522,9 +529,12 @@ static void GL_Upscale32(byte *data, int width, int height, int maxlevel, imaget

GL_Upload32(data, width, height, maxlevel, type, flags);

#ifdef GL_TEXTURE_MAX_LEVEL
if (AT_LEAST_OPENGL(1, 2))
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxlevel);
#endif

#ifdef GL_TEXTURE_LOD_BIAS
// adjust LOD for resampled textures
if (upload_width != width || upload_height != height) {
float du = upload_width / (float)width;
Expand All @@ -534,6 +544,7 @@ static void GL_Upscale32(byte *data, int width, int height, int maxlevel, imaget
if (AT_LEAST_OPENGL(1, 4))
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, bias);
}
#endif
}

static void GL_SetFilterAndRepeat(imagetype_t type, imageflags_t flags)
Expand Down Expand Up @@ -586,12 +597,16 @@ static void GL_SetFilterAndRepeat(imagetype_t type, imageflags_t flags)
if (type == IT_WALL || type == IT_SKIN || (flags & IF_REPEAT)) {
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
} else if (AT_LEAST_OPENGL(1, 2)) {
#ifdef GL_CLAMP_TO_EDGE
} else if (AT_LEAST_OPENGL(1, 2) || AT_LEAST_OPENGL_ES(1, 0)) {
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#endif
#ifdef GL_CLAMP
} else {
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
#endif
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/refresh/gl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,10 @@ static size_t GL_ViewCluster_m(char *buffer, size_t size)
static void gl_lightmap_changed(cvar_t *self)
{
lm.scale = Cvar_ClampValue(gl_coloredlightmaps, 0, 1);
lm.comp = lm.scale ? GL_RGB : GL_LUMINANCE;
if (AT_LEAST_OPENGL_ES(1, 0))
lm.comp = GL_RGBA; // ES doesn't support internal format != external
else
lm.comp = lm.scale ? GL_RGB : GL_LUMINANCE;
lm.add = 255 * Cvar_ClampValue(gl_brightness, -1, 1);
lm.modulate = gl_modulate->value * gl_modulate_world->value;
lm.dirty = qtrue; // rebuild all lightmaps next frame
Expand Down Expand Up @@ -806,6 +809,22 @@ static qboolean GL_SetupConfig(void)
return qfalse;
}

// parse ES profile prefix
if (!strncmp(version, "OpenGL ES", 9)) {
version += 9;
if (version[0] == '-' && version[1] && version[2] && version[3] == ' ') {
version += 4;
} else if (version[0] == ' ') {
version += 1;
} else {
Com_EPrintf("OpenGL returned invalid version string\n");
return qfalse;
}
gl_config.es_profile = qtrue;
} else {
gl_config.es_profile = qfalse;
}

// parse version
gl_config.version_major = strtoul(version, &p, 10);
if (*p == '.') {
Expand All @@ -820,7 +839,7 @@ static qboolean GL_SetupConfig(void)
}

// OpenGL 1.0 doesn't have vertex arrays
if (!AT_LEAST_OPENGL(1, 1)) {
if (!AT_LEAST_OPENGL(1, 1) && !AT_LEAST_OPENGL_ES(1, 0)) {
Com_EPrintf("OpenGL version 1.1 or higher required\n");
return qfalse;
}
Expand Down
6 changes: 6 additions & 0 deletions src/refresh/gl/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ void GL_StateBits(glStateBits_t bits)
}
}

#ifdef GL_ARB_fragment_program
if ((diff & GLS_WARP_ENABLE) && gl_static.prognum_warp) {
if (bits & GLS_WARP_ENABLE) {
vec4_t param;
Expand All @@ -160,6 +161,7 @@ void GL_StateBits(glStateBits_t bits)
qglDisable(GL_FRAGMENT_PROGRAM_ARB);
}
}
#endif

if (diff & GLS_CULL_DISABLE) {
if (bits & GLS_CULL_DISABLE) {
Expand Down Expand Up @@ -461,6 +463,7 @@ void GL_DisableOutlines(void)

void GL_InitPrograms(void)
{
#ifdef GL_ARB_fragment_program
GLuint prog = 0;

if (gl_config.ext_supported & QGL_ARB_fragment_program) {
Expand Down Expand Up @@ -496,10 +499,12 @@ void GL_InitPrograms(void)

qglBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
gl_static.prognum_warp = prog;
#endif
}

void GL_ShutdownPrograms(void)
{
#ifdef GL_ARB_fragment_program
if (!qglDeleteProgramsARB) {
return;
}
Expand All @@ -511,4 +516,5 @@ void GL_ShutdownPrograms(void)

QGL_ShutdownExtensions(QGL_ARB_fragment_program);
gl_config.ext_enabled &= ~QGL_ARB_fragment_program;
#endif
}

0 comments on commit e0f92dd

Please sign in to comment.