Skip to content

Commit

Permalink
renderer: rewrite the GLSL extension enablement
Browse files Browse the repository at this point in the history
- trust GLimp_InitExtensions and deduplicate the code
- unify the way to tell an extension is available
  • Loading branch information
illwieckz committed Jun 8, 2024
1 parent 5cf0f16 commit 289c75e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
57 changes: 28 additions & 29 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,18 @@ static std::string BuildDeformSteps( deformStage_t *deforms, int numDeforms )
return steps;
}

static void addExtension( std::string &str, bool available, int minGlslVersion, const std::string &name ) {
if ( !available )
{
return;
}

static void addExtension( std::string &str, int enabled, int minGlslVersion,
int supported, const char *name ) {
if( !enabled ) {
// extension disabled by user
} else if( glConfig2.shadingLanguageVersion >= minGlslVersion ) {
// the extension is available in the core language
str += Str::Format( "#define HAVE_%s 1\n", name );
} else if( supported ) {
// extension has to be explicitly enabled
if( glConfig2.shadingLanguageVersion < minGlslVersion )
{
str += Str::Format( "#extension GL_%s : require\n", name );
str += Str::Format( "#define HAVE_%s 1\n", name );
} else {
// extension is not supported
}

str += Str::Format( "#define HAVE_%s 1\n", name );
}

static void AddConst( std::string& str, const std::string& name, int value )
Expand All @@ -413,14 +410,25 @@ static std::string GenVersionDeclaration() {
glConfig2.shadingLanguageVersion >= 150 ? (glConfig2.glCoreProfile ? "core" : "compatibility") : "");

// add supported GLSL extensions
addExtension( str, glConfig2.textureGatherAvailable, 400,
GLEW_ARB_texture_gather, "ARB_texture_gather" );
addExtension( str, r_ext_gpu_shader4->integer, 130,
GLEW_EXT_gpu_shader4, "EXT_gpu_shader4" );
addExtension( str, r_arb_gpu_shader5->integer, 400,
GLEW_ARB_gpu_shader5, "ARB_gpu_shader5" );
addExtension( str, r_arb_uniform_buffer_object->integer, 140,
GLEW_ARB_uniform_buffer_object, "ARB_uniform_buffer_object" );
struct extension_t {
bool available;
int minGlslVersion;
std::string name;
};

const std::vector<extension_t> extensions = {
{ glConfig2.gpuShader4Available, 130, "EXT_gpu_shader4" },
{ glConfig2.gpuShader5Available, 400, "ARB_gpu_shader5" },
{ glConfig2.textureGatherAvailable, 400, "ARB_texture_gather" },
{ glConfig2.textureIntegerAvailable, 0, "EXT_texture_integer" },
{ glConfig2.textureRGAvailable, 0, "ARB_texture_rg" },
{ glConfig2.uniformBufferObjectAvailable, 140, "ARB_uniform_buffer_object" },
};

for ( const auto& extension : extensions )
{
addExtension( str, extension.available, extension.minGlslVersion, extension.name );
}

return str;
}
Expand Down Expand Up @@ -749,15 +757,6 @@ std::string GLShaderManager::BuildGPUShaderText( Str::StringRef mainShaderNa
std::string env;
env.reserve( 1024 ); // Might help, just an estimate.

if ( glConfig2.textureRGAvailable )
AddDefine( env, "TEXTURE_RG", 1 );

if ( glConfig2.uniformBufferObjectAvailable )
AddDefine( env, "UNIFORM_BUFFER_OBJECT", 1 );

if ( glConfig2.textureIntegerAvailable )
AddDefine( env, "TEXTURE_INTEGER", 1 );

AddDefine( env, "r_AmbientScale", r_ambientScale->value );
AddDefine( env, "r_SpecularScale", r_specularScale->value );
AddDefine( env, "r_zNear", r_znear->value );
Expand Down
8 changes: 4 additions & 4 deletions src/engine/renderer/glsl_source/computeLight_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ uniform samplerCube u_EnvironmentMap1;
uniform float u_EnvironmentInterpolation;
#endif // USE_REFLECTIVE_SPECULAR

#ifdef HAVE_ARB_uniform_buffer_object
#if defined(HAVE_ARB_uniform_buffer_object)
struct light {
vec4 center_radius;
vec4 color_type;
Expand Down Expand Up @@ -146,7 +146,7 @@ void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightCol
#endif // !USE_PHYSICAL_MAPPING
}

#if defined(TEXTURE_INTEGER) && defined(r_highPrecisionRendering)
#if defined(HAVE_EXT_texture_integer) && defined(r_highPrecisionRendering)
const int lightsPerLayer = 16;
uniform usampler3D u_LightTiles;
#define idxs_t uvec4
Expand All @@ -158,7 +158,7 @@ int nextIdx( inout idxs_t idxs ) {
idxs = idxs >> 2;
return int( tmp.x + tmp.y + tmp.z + tmp.w );
}
#else // !TEXTURE INTEGER
#else // !HAVE_EXT_texture_integer
const int lightsPerLayer = 4;
uniform sampler3D u_LightTiles;
#define idxs_t vec4
Expand All @@ -171,7 +171,7 @@ int nextIdx( inout idxs_t idxs ) {
tmp -= 4.0 * idxs;
return int( dot( tmp, vec4( 64.0, 16.0, 4.0, 1.0 ) ) );
}
#endif // TEXTURE_INTEGER
#endif // HAVE_EXT_texture_integer

const int numLayers = MAX_REF_LIGHTS / 256;

Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/glsl_source/debugShadowMap_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* debugShadowMap_fp.glsl */

/* swizzle one- and two-component textures to RG */
#ifdef TEXTURE_RG
#ifdef HAVE_ARB_texture_rg
# define SWIZ1 r
# define SWIZ2 rg
#else
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/glsl_source/forwardLighting_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* forwardLighting_fp.glsl */

/* swizzle one- and two-component textures to RG */
#ifdef TEXTURE_RG
#ifdef HAVE_ARB_texture_rg
# define SWIZ1 r
# define SWIZ2 rg
#else
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/lighttile_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct light {
float angle;
};

#ifdef HAVE_ARB_uniform_buffer_object
#if defined(HAVE_ARB_uniform_buffer_object)
layout(std140) uniform u_Lights {
vec4 lightvec[ MAX_REF_LIGHTS * 3 ];
};
Expand Down Expand Up @@ -88,7 +88,7 @@ uniform vec3 u_zFar;

const int numLayers = MAX_REF_LIGHTS / 256;

#if defined(TEXTURE_INTEGER) && defined(r_highPrecisionRendering)
#if defined(HAVE_EXT_texture_integer) && defined(r_highPrecisionRendering)
#define idxs_t uvec4
#define idx_initializer uvec4(3)
DECLARE_OUTPUT(uvec4)
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/glsl_source/shadowFill_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ IN(smooth) vec4 var_Color;

DECLARE_OUTPUT(vec4)

#ifdef TEXTURE_RG
#ifdef HAVE_ARB_texture_rg
# define SWIZ1 r
# define SWIZ2 rg
#else
Expand Down
1 change: 1 addition & 0 deletions src/engine/renderer/tr_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct glconfig2_t
bool textureIntegerAvailable;
bool textureRGAvailable;
bool gpuShader4Available;
bool gpuShader5Available;
bool textureGatherAvailable;
int maxDrawBuffers;

Expand Down
3 changes: 3 additions & 0 deletions src/engine/sys/sdl_glimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,9 @@ static void GLimp_InitExtensions()
// made required in OpenGL 3.0
glConfig2.gpuShader4Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, EXT_gpu_shader4, r_ext_gpu_shader4->value );

// made required in OpenGL 4.0
glConfig2.gpuShader5Available = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, ARB_gpu_shader5, r_arb_gpu_shader5->value );

// made required in OpenGL 3.0
// GL_EXT_texture_integer can be used in shaders only if GL_EXT_gpu_shader4 is also available
glConfig2.textureIntegerAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, EXT_texture_integer, r_ext_texture_integer->value )
Expand Down

0 comments on commit 289c75e

Please sign in to comment.