Skip to content

Commit b9df3a1

Browse files
committed
renderer: implement and detect RG images
1 parent aabc223 commit b9df3a1

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

src/engine/renderer/tr_backend.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,17 @@ static GLint GL_ToSRGB( GLint internalFormat, bool isSRGB )
739739
{
740740
switch ( format )
741741
{
742-
/* EXT_texture_sRGB_R8 extension.
743-
See: https://github.com/KhronosGroup/OpenGL-Registry/blob/main/extensions/EXT/EXT_texture_sRGB_R8.txt */
744742
case GL_RED:
743+
case GL_R8:
744+
/* EXT_texture_sRGB_R8 extension.
745+
See: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_sRGB_R8.txt */
745746
ASSERT( glConfig.textureSrgbR8Available );
746747
return GL_SR8_EXT;
748+
case GL_RG8:
749+
/* EXT_texture_sRGB_RG8 extension.
750+
See: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_sRGB_RG8.txt */
751+
ASSERT( glConfig.textureSrgbRG8Available );
752+
return GL_SRG8_EXT;
747753
case GL_RGB:
748754
return GL_SRGB;
749755
case GL_RGBA:

src/engine/renderer/tr_image.cpp

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ class ListImagesCmd : public Cmd::StaticCmd
177177
{ GL_RGBA32UI, { "RGBA32UI", 16 } },
178178
{ GL_ALPHA16F_ARB, { "A16F", 2 } },
179179
{ GL_ALPHA32F_ARB, { "A32F", 4 } },
180+
{ GL_R8, { "R8", 1 } },
180181
{ GL_R16F, { "R16F", 2 } },
181182
{ GL_R32F, { "R32F", 4 } },
182183
{ GL_LUMINANCE_ALPHA16F_ARB, { "LA16F", 4 } },
183184
{ GL_LUMINANCE_ALPHA32F_ARB, { "LA32F", 8 } },
185+
{ GL_RG8, { "RG8", 2 } },
184186
{ GL_RG16F, { "RG16F", 4 } },
185187
{ GL_RG32F, { "RG32F", 8 } },
186188

@@ -946,7 +948,19 @@ void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int
946948
}
947949
else
948950
{
949-
internalFormat = GL_RED;
951+
internalFormat = glConfig.textureSrgbRG8Available ? GL_R8 : GL_RED;
952+
}
953+
}
954+
else if ( image->bits & IF_RG )
955+
{
956+
if ( isSRGB && !glConfig.textureSrgbRG8Available )
957+
{
958+
Log::Warn("red-green image '%s' cannot be loaded as sRGB", image->name );
959+
internalFormat = GL_RGB8;
960+
}
961+
else
962+
{
963+
internalFormat = GL_RG8;
950964
}
951965
}
952966
else if ( image->bits & ( IF_RGBA16F | IF_RGBA32F | IF_TWOCOMP16F | IF_TWOCOMP32F | IF_ONECOMP16F | IF_ONECOMP32F ) )
@@ -1094,31 +1108,59 @@ void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int
10941108

10951109
if ( internalFormat == GL_RGB8 )
10961110
{
1097-
if ( isSRGB && !glConfig.textureSrgbR8Available )
1098-
{
1099-
break;
1100-
}
1101-
11021111
/* Scan the texture for green and blue channels' max values
11031112
and verify if the green and blue channels are being used or not. */
11041113

11051114
c = image->width * image->height;
11061115
scan = dataArray[0];
11071116

1108-
internalFormat = GL_RED;
1117+
internalFormat = glConfig.textureRGAvailable ? GL_R8 : GL_RED;
1118+
1119+
bool hasGreen = false;
1120+
bool hasBlue = false;
11091121

11101122
for ( i = 0; i < c * 4; i += 4 )
11111123
{
1112-
if ( scan[ i + 1 ] != 0 )
1124+
if ( scan[ i + 2 ] != 0 )
11131125
{
1114-
internalFormat = GL_RGB8;
1126+
hasBlue = true;
11151127
break;
11161128
}
11171129

1118-
if ( scan[ i + 2 ] != 0 )
1130+
if ( scan[ i + 1 ] != 0 )
1131+
{
1132+
hasGreen = true;
1133+
1134+
if ( !glConfig.textureRGAvailable )
1135+
{
1136+
break;
1137+
}
1138+
}
1139+
}
1140+
1141+
if ( hasBlue )
1142+
{
1143+
if ( isSRGB && !glConfig.textureSrgbR8Available )
1144+
{
1145+
internalFormat = GL_R8;
1146+
}
1147+
else
11191148
{
11201149
internalFormat = GL_RGB8;
1121-
break;
1150+
}
1151+
}
1152+
else if ( hasGreen )
1153+
{
1154+
if ( glConfig.textureRGAvailable )
1155+
{
1156+
if ( isSRGB && !glConfig.textureSrgbRG8Available )
1157+
{
1158+
internalFormat = GL_RGB8;
1159+
}
1160+
else
1161+
{
1162+
internalFormat = GL_RG8;
1163+
}
11221164
}
11231165
}
11241166
}
@@ -2815,8 +2857,15 @@ void R_CreateBuiltinImages()
28152857

28162858
imageParams.bits = IF_NOPICMIP;
28172859

2860+
if ( glConfig.textureRGAvailable )
2861+
{
2862+
imageParams.bits |= IF_RG;
2863+
}
2864+
28182865
tr.greenImage = R_CreateImage( "_green", ( const byte ** ) &dataPtr, DIMENSION, DIMENSION, 1, imageParams );
28192866

2867+
imageParams.bits = IF_NOPICMIP;
2868+
28202869
// blue
28212870
for ( x = DIMENSION * DIMENSION, out = data; x; --x, out += 4 )
28222871
{

src/engine/renderer/tr_local.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ enum class ssaoMode {
485485
IF_BC5 = BIT( 23 ),
486486
IF_RGBA32UI = BIT( 24 ),
487487
IF_HOMEPATH = BIT( 25 ),
488-
IF_NOALPHA = BIT( 26 )
488+
IF_NOALPHA = BIT( 26 ),
489+
IF_RG = BIT(27)
489490
};
490491

491492
enum class filterType_t

src/engine/renderer/tr_public.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct GLConfig
139139
bool gpuShader5Available;
140140
bool textureGatherAvailable;
141141
bool textureSrgbR8Available;
142+
bool textureSrgbRG8Available;
142143
int maxDrawBuffers;
143144

144145
float maxTextureAnisotropy;

src/engine/sys/sdl_glimp.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ static Cvar::Cvar<bool> r_arb_texture_barrier( "r_arb_texture_barrier",
106106
"Use GL_ARB_texture_barrier if available", Cvar::NONE, true );
107107
static Cvar::Cvar<bool> r_arb_texture_gather( "r_arb_texture_gather",
108108
"Use GL_ARB_texture_gather if available", Cvar::NONE, true );
109+
static Cvar::Cvar<bool> r_arb_texture_rg( "r_arb_texture_rg",
110+
"Use GL_ARB_texture_rg if available", Cvar::NONE, true );
109111
static Cvar::Cvar<bool> r_arb_uniform_buffer_object( "r_arb_uniform_buffer_object",
110112
"Use GL_ARB_uniform_buffer_object if available", Cvar::NONE, true );
111113
static Cvar::Cvar<bool> r_arb_vertex_attrib_binding( "r_arb_vertex_attrib_binding",
@@ -120,10 +122,10 @@ static Cvar::Cvar<bool> r_ext_texture_float( "r_ext_texture_float",
120122
"Use GL_EXT_texture_float if available", Cvar::NONE, true );
121123
static Cvar::Cvar<bool> r_ext_texture_integer( "r_ext_texture_integer",
122124
"Use GL_EXT_texture_integer if available", Cvar::NONE, true );
123-
static Cvar::Cvar<bool> r_ext_texture_rg( "r_ext_texture_rg",
124-
"Use GL_EXT_texture_rg if available", Cvar::NONE, true );
125125
static Cvar::Cvar<bool> r_ext_texture_srgb_r8( "r_ext_texture_srgb_r8",
126126
"Use GL_EXT_texture_sRGB_R8 if available", Cvar::NONE, true );
127+
static Cvar::Cvar<bool> r_ext_texture_srgb_rg8( "r_ext_texture_srgb_rg8",
128+
"Use GL_EXT_texture_sRGB_RG8 if available", Cvar::NONE, true );
127129
static Cvar::Cvar<bool> r_khr_debug( "r_khr_debug",
128130
"Use GL_KHR_debug if available", Cvar::NONE, true );
129131
static Cvar::Cvar<bool> r_khr_shader_subgroup( "r_khr_shader_subgroup",
@@ -2215,6 +2217,9 @@ static void GLimp_InitExtensions()
22152217

22162218
glConfig.textureSrgbR8Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, EXT_texture_sRGB_R8, r_ext_texture_srgb_r8.Get() );
22172219

2220+
// Texture - others
2221+
glConfig.textureSrgbRG8Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, EXT_texture_sRGB_RG8, r_ext_texture_srgb_rg8.Get() );
2222+
22182223
// Texture - others
22192224
glConfig.textureAnisotropyAvailable = false;
22202225
glConfig.textureAnisotropy = 0.0f;

0 commit comments

Comments
 (0)