Skip to content

Commit

Permalink
renderer: implement sRGB in GLSL
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jan 11, 2020
1 parent 806755e commit 05e08ce
Show file tree
Hide file tree
Showing 22 changed files with 459 additions and 83 deletions.
1 change: 1 addition & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ set(GLSLSOURCELIST
${ENGINE_DIR}/renderer/glsl_source/blurY_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/colorSpace_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/contrast_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/contrast_vp.glsl
Expand Down
14 changes: 14 additions & 0 deletions src/common/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Compiler.h"
#include "Math.h"

#define convertFromSRGB( v ) v <= 0.04045f ? v * (1.0f / 12.92f) : pow((v + 0.055f) * (1.0f / 1.055f), 2.4f)

namespace Color {

/*
Expand Down Expand Up @@ -256,6 +258,18 @@ class BasicColor
alpha = v;
}

CONSTEXPR_FUNCTION_RELAXED component_type ConvertFromSRGB( component_type v ) NOEXCEPT
{
return convertFromSRGB( v );
}

CONSTEXPR_FUNCTION_RELAXED void ConvertFromSRGB() NOEXCEPT
{
red = ConvertFromSRGB( red );
green = ConvertFromSRGB( green );
blue = ConvertFromSRGB( blue );
}

CONSTEXPR_FUNCTION_RELAXED BasicColor& operator*=( float factor ) NOEXCEPT
{
*this = *this * factor;
Expand Down
51 changes: 45 additions & 6 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ static std::string GenEngineConstants() {
AddConst( str, "r_RimExponent", r_rimExponent->value );
}

if ( r_cheapSRGB.Get() )
{
AddDefine( str, "r_cheapSRGB", 1 );
}

if ( r_showLightTiles->integer )
{
AddDefine( str, "r_showLightTiles", 1 );
Expand Down Expand Up @@ -1574,6 +1579,7 @@ void GLShader_generic2D::SetShaderProgramUniforms( shaderProgram_t *shaderProgra
GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_LightFactor( this ),
u_LinearizeTexture( this ),
u_TextureMatrix( this ),
u_ViewOrigin( this ),
u_ViewUp( this ),
Expand Down Expand Up @@ -1602,6 +1608,11 @@ void GLShader_generic::BuildShaderVertexLibNames( std::string& vertexInlines )
vertexInlines += "vertexSimple vertexSkinning vertexAnimation vertexSprite ";
}

void GLShader_generic::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "colorSpace";
}

void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
Expand All @@ -1612,6 +1623,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
GLShader( "lightMapping",
ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
u_LightFactors( this ),
u_LinearizeTextures( this ),
u_TextureMatrix( this ),
u_SpecularExponent( this ),
u_ColorModulate( this ),
Expand Down Expand Up @@ -1651,7 +1663,7 @@ void GLShader_lightMapping::BuildShaderVertexLibNames( std::string& vertexInline

void GLShader_lightMapping::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "computeLight reliefMapping";
fragmentInlines += "colorSpace computeLight reliefMapping";
}

void GLShader_lightMapping::BuildShaderCompileMacros( std::string& /*compileMacros*/ )
Expand All @@ -1677,6 +1689,7 @@ void GLShader_lightMapping::SetShaderProgramUniforms( shaderProgram_t *shaderPro

GLShader_forwardLighting_omniXYZ::GLShader_forwardLighting_omniXYZ( GLShaderManager *manager ):
GLShader("forwardLighting_omniXYZ", "forwardLighting", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager),
u_LinearizeTextures( this ),
u_TextureMatrix( this ),
u_SpecularExponent( this ),
u_AlphaThreshold( this ),
Expand Down Expand Up @@ -1713,7 +1726,7 @@ void GLShader_forwardLighting_omniXYZ::BuildShaderVertexLibNames( std::string& v

void GLShader_forwardLighting_omniXYZ::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "computeLight reliefMapping";
fragmentInlines += "colorSpace computeLight reliefMapping";
}

void GLShader_forwardLighting_omniXYZ::BuildShaderCompileMacros( std::string& /*compileMacros*/ )
Expand All @@ -1735,6 +1748,7 @@ void GLShader_forwardLighting_omniXYZ::SetShaderProgramUniforms( shaderProgram_t

GLShader_forwardLighting_projXYZ::GLShader_forwardLighting_projXYZ( GLShaderManager *manager ):
GLShader("forwardLighting_projXYZ", "forwardLighting", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager),
u_LinearizeTextures( this ),
u_TextureMatrix( this ),
u_SpecularExponent( this ),
u_AlphaThreshold( this ),
Expand Down Expand Up @@ -1772,7 +1786,7 @@ void GLShader_forwardLighting_projXYZ::BuildShaderVertexLibNames( std::string& v

void GLShader_forwardLighting_projXYZ::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "computeLight reliefMapping";
fragmentInlines += "colorSpace computeLight reliefMapping";
}

void GLShader_forwardLighting_projXYZ::BuildShaderCompileMacros( std::string& compileMacros )
Expand All @@ -1795,6 +1809,7 @@ void GLShader_forwardLighting_projXYZ::SetShaderProgramUniforms( shaderProgram_t

GLShader_forwardLighting_directionalSun::GLShader_forwardLighting_directionalSun( GLShaderManager *manager ):
GLShader("forwardLighting_directionalSun", "forwardLighting", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager),
u_LinearizeTextures( this ),
u_TextureMatrix( this ),
u_SpecularExponent( this ),
u_AlphaThreshold( this ),
Expand Down Expand Up @@ -1834,7 +1849,7 @@ void GLShader_forwardLighting_directionalSun::BuildShaderVertexLibNames( std::st

void GLShader_forwardLighting_directionalSun::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "computeLight reliefMapping";
fragmentInlines += "colorSpace computeLight reliefMapping";
}

void GLShader_forwardLighting_directionalSun::BuildShaderCompileMacros( std::string& compileMacros )
Expand Down Expand Up @@ -1933,6 +1948,7 @@ void GLShader_reflection::SetShaderProgramUniforms( shaderProgram_t *shaderProgr

GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
GLShader( "skybox", ATTR_POSITION, manager ),
u_LinearizeTexture( this ),
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
Expand All @@ -1942,13 +1958,19 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
{
}

void GLShader_skybox::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "colorSpace";
}

void GLShader_skybox::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
}

GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
GLShader( "fogQuake3", ATTR_POSITION | ATTR_QTANGENT, manager ),
u_LinearizeTexture( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_Color( this ),
Expand All @@ -1968,13 +1990,19 @@ void GLShader_fogQuake3::BuildShaderVertexLibNames( std::string& vertexInlines )
vertexInlines += "vertexSimple vertexSkinning vertexAnimation ";
}

void GLShader_fogQuake3::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "colorSpace";
}

void GLShader_fogQuake3::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
}

GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
GLShader( "fogGlobal", ATTR_POSITION, manager ),
u_LinearizeTexture( this ),
u_ViewOrigin( this ),
u_ViewMatrix( this ),
u_ModelViewProjectionMatrix( this ),
Expand All @@ -1985,6 +2013,11 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
{
}

void GLShader_fogGlobal::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "colorSpace";
}

void GLShader_fogGlobal::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
Expand Down Expand Up @@ -2071,8 +2104,14 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
u_TextureMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_DeformMagnitude( this ),
u_InverseGamma( this )
u_InverseGamma( this ),
u_DelinearizeScreen( this )
{
}

void GLShader_cameraEffects::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "colorSpace";
}

void GLShader_cameraEffects::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
Expand Down Expand Up @@ -2144,7 +2183,7 @@ GLShader_liquid::GLShader_liquid( GLShaderManager *manager ) :

void GLShader_liquid::BuildShaderFragmentLibNames( std::string& fragmentInlines )
{
fragmentInlines += "computeLight reliefMapping";
fragmentInlines += "colorSpace computeLight reliefMapping";
}

void GLShader_liquid::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
Expand Down
61 changes: 60 additions & 1 deletion src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,51 @@ class u_ShadowTexelSize :
}
};

class u_LinearizeTexture :
GLUniform1i
{
public:
u_LinearizeTexture( GLShader *shader ) :
GLUniform1i( shader, "u_LinearizeTexture" )
{
}

void SetUniform_LinearizeTexture( const int value )
{
this->SetValue( value );
}
};

class u_LinearizeTextures :
GLUniform2i
{
public:
u_LinearizeTextures( GLShader *shader ) :
GLUniform2i( shader, "u_LinearizeTextures" )
{
}

void SetUniform_LinearizeTextures( const ivec2_t v )
{
this->SetValue( v );
}
};

class u_DelinearizeScreen :
GLUniform1i
{
public:
u_DelinearizeScreen( GLShader *shader ) :
GLUniform1i( shader, "u_DelinearizeScreen" )
{
}

void SetUniform_DelinearizeScreen( const int value )
{
this->SetValue( value );
}
};

class u_ShadowBlur :
GLUniform1f
{
Expand Down Expand Up @@ -2390,6 +2435,7 @@ class GLShader_generic2D :
class GLShader_generic :
public GLShader,
public u_LightFactor,
public u_LinearizeTexture,
public u_TextureMatrix,
public u_ViewOrigin,
public u_ViewUp,
Expand All @@ -2414,12 +2460,14 @@ class GLShader_generic :
public:
GLShader_generic( GLShaderManager *manager );
void BuildShaderVertexLibNames( std::string& vertexInlines ) override;
void BuildShaderFragmentLibNames( std::string& vertexInlines ) override;
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_lightMapping :
public GLShader,
public u_LightFactors,
public u_LinearizeTextures,
public u_TextureMatrix,
public u_SpecularExponent,
public u_ColorModulate,
Expand Down Expand Up @@ -2460,6 +2508,7 @@ class GLShader_lightMapping :

class GLShader_forwardLighting_omniXYZ :
public GLShader,
public u_LinearizeTextures,
public u_TextureMatrix,
public u_SpecularExponent,
public u_AlphaThreshold,
Expand Down Expand Up @@ -2497,6 +2546,7 @@ class GLShader_forwardLighting_omniXYZ :

class GLShader_forwardLighting_projXYZ :
public GLShader,
public u_LinearizeTextures,
public u_TextureMatrix,
public u_SpecularExponent,
public u_AlphaThreshold,
Expand Down Expand Up @@ -2535,6 +2585,7 @@ class GLShader_forwardLighting_projXYZ :

class GLShader_forwardLighting_directionalSun :
public GLShader,
public u_LinearizeTextures,
public u_TextureMatrix,
public u_SpecularExponent,
public u_AlphaThreshold,
Expand Down Expand Up @@ -2623,6 +2674,7 @@ class GLShader_reflection :

class GLShader_skybox :
public GLShader,
public u_LinearizeTexture,
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
Expand All @@ -2632,11 +2684,13 @@ class GLShader_skybox :
{
public:
GLShader_skybox( GLShaderManager *manager );
void BuildShaderFragmentLibNames( std::string& vertexInlines ) override;
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_fogQuake3 :
public GLShader,
public u_LinearizeTexture,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_Color,
Expand All @@ -2652,11 +2706,13 @@ class GLShader_fogQuake3 :
public:
GLShader_fogQuake3( GLShaderManager *manager );
void BuildShaderVertexLibNames( std::string& vertexInlines ) override;
void BuildShaderFragmentLibNames( std::string& fragmentInlines ) override;
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_fogGlobal :
public GLShader,
public u_LinearizeTexture,
public u_ViewOrigin,
public u_ViewMatrix,
public u_ModelViewProjectionMatrix,
Expand All @@ -2667,6 +2723,7 @@ class GLShader_fogGlobal :
{
public:
GLShader_fogGlobal( GLShaderManager *manager );
void BuildShaderFragmentLibNames( std::string& fragmentInlines ) override;
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

Expand Down Expand Up @@ -2732,11 +2789,13 @@ class GLShader_cameraEffects :
public u_TextureMatrix,
public u_ModelViewProjectionMatrix,
public u_DeformMagnitude,
public u_InverseGamma
public u_InverseGamma,
public u_DelinearizeScreen
{
public:
GLShader_cameraEffects( GLShaderManager *manager );
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
void BuildShaderFragmentLibNames( std::string& vertexInlines ) override;
};

class GLShader_blurX :
Expand Down
Loading

0 comments on commit 05e08ce

Please sign in to comment.