Skip to content

Commit d29eb7b

Browse files
committed
renderer: implement and detect RG images
1 parent b364dcf commit d29eb7b

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

src/engine/renderer/tr_backend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,12 @@ static GLint GL_ToSRGB( GLint internalFormat, bool isSRGB )
742742
/* EXT_texture_sRGB_R8 extension.
743743
See: https://github.com/KhronosGroup/OpenGL-Registry/blob/main/extensions/EXT/EXT_texture_sRGB_R8.txt */
744744
case GL_RED:
745+
case GL_R8:
745746
ASSERT( glConfig.textureSrgbR8Available );
746747
return GL_SR8_EXT;
748+
case GL_RG:
749+
ASSERT( glConfig.textureSrgbRG8Available );
750+
return GL_SRG8_EXT;
747751
case GL_RGB:
748752
return GL_SRGB;
749753
case GL_RGBA:

src/engine/renderer/tr_image.cpp

Lines changed: 59 additions & 10 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,6 +948,18 @@ void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int
946948
internalFormat = GL_RED;
947949
}
948950
}
951+
else if ( image->bits & IF_RG )
952+
{
953+
if ( isSRGB && !glConfig.textureSrgbRG8Available )
954+
{
955+
Log::Warn("red-green image '%s' cannot be loaded as sRGB", image->name );
956+
internalFormat = GL_RGB8;
957+
}
958+
else
959+
{
960+
internalFormat = GL_RG8;
961+
}
962+
}
949963
else if ( image->bits & ( IF_RGBA16F | IF_RGBA32F | IF_TWOCOMP16F | IF_TWOCOMP32F | IF_ONECOMP16F | IF_ONECOMP32F ) )
950964
{
951965
if( !glConfig.textureFloatAvailable ) {
@@ -1091,31 +1105,59 @@ void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int
10911105

10921106
if ( internalFormat == GL_RGB8 )
10931107
{
1094-
if ( isSRGB && !glConfig.textureSrgbR8Available )
1095-
{
1096-
break;
1097-
}
1098-
10991108
/* Scan the texture for green and blue channels' max values
11001109
and verify if the green and blue channels are being used or not. */
11011110

11021111
c = image->width * image->height;
11031112
scan = dataArray[0];
11041113

1105-
internalFormat = GL_RED;
1114+
internalFormat = glConfig.textureRGAvailable ? GL_R8 : GL_RED;
1115+
1116+
bool hasGreen = false;
1117+
bool hasBlue = false;
11061118

11071119
for ( i = 0; i < c * 4; i += 4 )
11081120
{
1109-
if ( scan[ i + 1 ] != 0 )
1121+
if ( scan[ i + 2 ] != 0 )
11101122
{
1111-
internalFormat = GL_RGB8;
1123+
hasBlue = true;
11121124
break;
11131125
}
11141126

1115-
if ( scan[ i + 2 ] != 0 )
1127+
if ( scan[ i + 1 ] != 0 )
1128+
{
1129+
hasGreen = true;
1130+
1131+
if ( !glConfig.textureRGAvailable )
1132+
{
1133+
break;
1134+
}
1135+
}
1136+
}
1137+
1138+
if ( hasBlue )
1139+
{
1140+
if ( isSRGB && !glConfig.textureSrgbR8Available )
1141+
{
1142+
internalFormat = GL_R8;
1143+
}
1144+
else
11161145
{
11171146
internalFormat = GL_RGB8;
1118-
break;
1147+
}
1148+
}
1149+
else if ( hasGreen )
1150+
{
1151+
if ( glConfig.textureRGAvailable )
1152+
{
1153+
if ( isSRGB && !glConfig.textureSrgbRG8Available )
1154+
{
1155+
internalFormat = GL_RGB8;
1156+
}
1157+
else
1158+
{
1159+
internalFormat = GL_RG8;
1160+
}
11191161
}
11201162
}
11211163
}
@@ -2812,8 +2854,15 @@ void R_CreateBuiltinImages()
28122854

28132855
imageParams.bits = IF_NOPICMIP;
28142856

2857+
if ( glConfig.textureRGAvailable )
2858+
{
2859+
imageParams.bits |= IF_RG;
2860+
}
2861+
28152862
tr.greenImage = R_CreateImage( "_green", ( const byte ** ) &dataPtr, DIMENSION, DIMENSION, 1, imageParams );
28162863

2864+
imageParams.bits = IF_NOPICMIP;
2865+
28172866
// blue
28182867
for ( x = DIMENSION * DIMENSION, out = data; x; --x, out += 4 )
28192868
{

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: 10 additions & 0 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",
@@ -124,6 +126,8 @@ static Cvar::Cvar<bool> r_ext_texture_rg( "r_ext_texture_rg",
124126
"Use GL_EXT_texture_rg if available", Cvar::NONE, true );
125127
static Cvar::Cvar<bool> r_ext_texture_srgb_r8( "r_ext_texture_srgb_r8",
126128
"Use GL_EXT_texture_sRGB_R8 if available", Cvar::NONE, true );
129+
static Cvar::Cvar<bool> r_ext_texture_srgb_rg8( "r_ext_texture_srgb_rg8",
130+
"Use GL_EXT_texture_sRGB_RG8 if available", Cvar::NONE, true );
127131
static Cvar::Cvar<bool> r_khr_debug( "r_khr_debug",
128132
"Use GL_KHR_debug if available", Cvar::NONE, true );
129133
static Cvar::Cvar<bool> r_khr_shader_subgroup( "r_khr_shader_subgroup",
@@ -2118,6 +2122,9 @@ static void GLimp_InitExtensions()
21182122
// made required in OpenGL 3.0
21192123
glConfig.textureFloatAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, ARB_texture_float, r_ext_texture_float.Get() );
21202124

2125+
// made required in OpenGL 3.0
2126+
glConfig.textureRGAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, ARB_texture_rg, r_ext_texture_rg.Get() );
2127+
21212128
bool gpuShader4Enabled = r_ext_gpu_shader4.Get();
21222129

21232130
if ( gpuShader4Enabled
@@ -2215,6 +2222,9 @@ static void GLimp_InitExtensions()
22152222

22162223
glConfig.textureSrgbR8Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, EXT_texture_sRGB_R8, r_ext_texture_srgb_r8.Get() );
22172224

2225+
// Texture - others
2226+
glConfig.textureSrgbRG8Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, EXT_texture_sRGB_RG8, r_ext_texture_srgb_rg8.Get() );
2227+
22182228
// Texture - others
22192229
glConfig.textureAnisotropyAvailable = false;
22202230
glConfig.textureAnisotropy = 0.0f;

0 commit comments

Comments
 (0)