Skip to content

Commit

Permalink
r_openglmipmaps: Generate mipmaps with opengl to improve load times s…
Browse files Browse the repository at this point in the history
…lightly
  • Loading branch information
aufau committed May 7, 2024
1 parent cd96799 commit a8e4f68
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CVARS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ Client-Side

..
:Name: r_openglMipMaps
:Values: "0", "1"
:Default: "1"
:Description:
Enable / Disable OpenGL mipmap generation. Disable to restore
original downsampling algorithms.

..
:Name: r_saberGlow
:Values: "0", "1"
:Default: "1"
Expand Down
7 changes: 7 additions & 0 deletions src/cgame/tr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ typedef enum {
TC_S3TC_DXT
} textureCompression_t;

typedef enum {
QGL_VERSION_1_0,
QGL_VERSION_1_4
} qglVersion_t;

typedef struct {
char renderer_string[MAX_STRING_CHARS];
char vendor_string[MAX_STRING_CHARS];
Expand Down Expand Up @@ -337,6 +342,8 @@ typedef struct {
const char *version_string;
const char *extensions_string;

qglVersion_t glVersion;

int maxTextureSize; // queried from GL
int maxActiveTextures; // multitexture ability

Expand Down
26 changes: 20 additions & 6 deletions src/renderer/tr_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,20 +688,29 @@ static void Upload32( byte * const *mipmaps, qboolean customMip, image_t *image,
if ( !upload->noMipMaps )
{
int miplevel = 0;
qboolean doLightScale = (qboolean)!upload->noLightScale;
qboolean openglMipMaps = (qboolean)(r_openglMipMaps->integer && !r_colorMipLevels->integer && glConfig.glVersion >= QGL_VERSION_1_4);
qboolean processData = qtrue;

if ( openglMipMaps )
{
qglTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
}

while ( 1 ) {
if ( doLightScale )
if ( processData && !upload->noLightScale )
{
R_LightScaleTexture( data, width, height, qfalse );
doLightScale = qfalse;
}

if ( r_colorMipLevels->integer )
{
R_BlendOverTexture( data, width * height, mipBlendColors[miplevel] );
}

qglTexImage2D( GL_TEXTURE_2D, miplevel, image->internalFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
if ( !openglMipMaps || processData )
{
qglTexImage2D( GL_TEXTURE_2D, miplevel, image->internalFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
}

if ( width == 1 && height == 1 )
{
Expand All @@ -711,11 +720,16 @@ static void Upload32( byte * const *mipmaps, qboolean customMip, image_t *image,
if ( customMip && level < MAX_MIP_LEVELS && mipmaps[level] )
{
data = mipmaps[level];
doLightScale = (qboolean)!upload->noLightScale;
processData = qtrue;
}
else
else if ( !openglMipMaps )
{
R_MipMap( data, width, height );
processData = qfalse;
}
else // openglMipMaps
{
processData = qfalse;
}

width = max(width >> 1, 1);
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ cvar_t *r_overBrightBits;

cvar_t *r_debugSurface;
cvar_t *r_simpleMipMaps;
cvar_t *r_openglMipMaps;

cvar_t *r_showImages;

Expand Down Expand Up @@ -620,6 +621,16 @@ static void GLimp_InitExtensions(void) {
}
}

static void GLimp_InitOpenGLVersion(void) {
glConfig.glVersion = QGL_VERSION_1_0;

if (strncmp(glConfig.version_string, "1.4", 3) >= 0)
{
glConfig.glVersion = QGL_VERSION_1_4;
Com_Printf("...OpenGL 1.4 available");
}
}

/*
** InitOpenGL
**
Expand Down Expand Up @@ -649,6 +660,8 @@ static void InitOpenGL(void) {
// stubbed or broken drivers may have reported 0...
glConfig.maxTextureSize = max(0, glConfig.maxTextureSize);

GLimp_InitOpenGLVersion();

// initialize extensions
GLimp_InitExtensions();

Expand Down Expand Up @@ -1078,6 +1091,7 @@ void R_Register( void )
r_aspectratio = ri.Cvar_Get("r_aspectratio", "-1", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH); // screen resolutions
r_customaspect = ri.Cvar_Get("r_customaspect", "1", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH);
r_simpleMipMaps = ri.Cvar_Get("r_simpleMipMaps", "1", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH);
r_openglMipMaps = ri.Cvar_Get("r_openglMipMaps", "1", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH);
r_vertexLight = ri.Cvar_Get("r_vertexLight", "0", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH);
r_uiFullScreen = ri.Cvar_Get( "r_uifullscreen", "0", 0);
r_subdivisions = ri.Cvar_Get("r_subdivisions", "4", CVAR_ARCHIVE | CVAR_GLOBAL | CVAR_LATCH);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ extern cvar_t *r_overBrightBits;

extern cvar_t *r_debugSurface;
extern cvar_t *r_simpleMipMaps;
extern cvar_t *r_openglMipMaps;

extern cvar_t *r_showImages;
extern cvar_t *r_debugSort;
Expand Down

0 comments on commit a8e4f68

Please sign in to comment.