Skip to content

WIP: renderer: implement GLSL sRGB support #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ set(GLSLSOURCELIST
${ENGINE_DIR}/renderer/glsl_source/blur_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/colorSpace.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
16 changes: 16 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,20 @@ class BasicColor
data_[ 3 ] = v;
}

CONSTEXPR_FUNCTION_RELAXED component_type ConvertFromSRGB( component_type v ) NOEXCEPT
{
float f = float( v ) / 255.0f;
f = convertFromSRGB( f );
return component_type( f * 255 );
}

CONSTEXPR_FUNCTION_RELAXED void ConvertFromSRGB() NOEXCEPT
{
SetRed( ConvertFromSRGB( Red() ) );
SetGreen( ConvertFromSRGB( Green() ) );
SetBlue( ConvertFromSRGB( Blue() ) );
}

CONSTEXPR_FUNCTION_RELAXED BasicColor& operator*=( float factor ) NOEXCEPT
{
*this = *this * factor;
Expand Down
11 changes: 11 additions & 0 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, boo
Tess_ComputeColor( pStage );
gl_genericShaderMaterial->SetUniform_Color( tess.svars.color );

// u_LinearizeTexture
gl_genericShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );

bool hasDepthFade = pStage->hasDepthFade;
if ( hasDepthFade ) {
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
Expand Down Expand Up @@ -190,6 +193,9 @@ void UpdateSurfaceDataLightMapping( uint32_t* materials, shaderStage_t* pStage,
// u_AlphaThreshold
gl_lightMappingShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );

// u_LinearizeTexture
gl_lightMappingShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );

// HeightMap
if ( pStage->enableReliefMapping ) {
float depthScale = RB_EvalExpression( &pStage->depthScaleExp, r_reliefDepthScale->value );
Expand Down Expand Up @@ -267,6 +273,9 @@ void UpdateSurfaceDataSkybox( uint32_t* materials, shaderStage_t* pStage, bool,
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );

gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );

// u_LinearizeTexture
gl_skyboxShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );
}

void UpdateSurfaceDataScreen( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand Down Expand Up @@ -359,6 +368,8 @@ void UpdateSurfaceDataFog( uint32_t* materials, shaderStage_t* pStage, bool, boo
materials += pStage->bufferOffset;

gl_fogQuake3ShaderMaterial->WriteUniformsToBuffer( materials );

gl_fogQuake3ShaderMaterial->SetUniform_LinearizeTexture( tr.worldLinearizeTexture );
}

/*
Expand Down
17 changes: 16 additions & 1 deletion src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,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 @@ -2261,6 +2266,7 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_ColorModulateColorGen( this ),
u_Color( this ),
u_Bones( this ),
Expand Down Expand Up @@ -2293,6 +2299,7 @@ GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_ColorModulateColorGen( this ),
u_Color( this ),
u_DepthScale( this ),
Expand Down Expand Up @@ -2334,6 +2341,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_ReliefDepthScale( this ),
Expand Down Expand Up @@ -2402,6 +2410,7 @@ GLShader_lightMappingMaterial::GLShader_lightMappingMaterial( GLShaderManager* m
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_ReliefDepthScale( this ),
u_ReliefOffsetBias( this ),
u_NormalScale( this ),
Expand Down Expand Up @@ -2712,6 +2721,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
u_CloudHeight( this ),
u_UseCloudMap( this ),
u_AlphaThreshold( this ),
u_LinearizeTexture( this ),
u_ModelViewProjectionMatrix( this )
{
}
Expand All @@ -2730,6 +2740,7 @@ GLShader_skyboxMaterial::GLShader_skyboxMaterial( GLShaderManager* manager ) :
u_CloudHeight( this ),
u_UseCloudMap( this ),
u_AlphaThreshold( this ),
u_LinearizeTexture( this ),
u_ModelViewProjectionMatrix( this )
{}

Expand All @@ -2743,6 +2754,7 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
u_FogMap( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_ColorGlobal( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
Expand All @@ -2765,6 +2777,7 @@ GLShader_fogQuake3Material::GLShader_fogQuake3Material( GLShaderManager* manager
u_FogMap( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LinearizeTexture( this ),
u_ColorGlobal( this ),
u_FogDistanceVector( this ),
u_FogDepthVector( this ),
Expand All @@ -2782,6 +2795,7 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
u_DepthMap( this ),
u_ModelViewProjectionMatrix( this ),
u_UnprojectMatrix( this ),
u_LinearizeTexture( this ),
u_Color( this ),
u_FogDistanceVector( this )
{
Expand Down Expand Up @@ -2896,7 +2910,8 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
u_ColorModulate( this ),
u_TextureMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseGamma( this )
u_InverseGamma( this ),
u_DelinearizeScreen( this )
{
}

Expand Down
42 changes: 41 additions & 1 deletion src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2806,6 +2806,36 @@ 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_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 @@ -3834,6 +3864,7 @@ class GLShader_generic :
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_ColorModulateColorGen,
public u_Color,
public u_Bones,
Expand Down Expand Up @@ -3863,6 +3894,7 @@ class GLShader_genericMaterial :
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_ColorModulateColorGen,
public u_Color,
public u_DepthScale,
Expand Down Expand Up @@ -3901,6 +3933,7 @@ class GLShader_lightMapping :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_Bones,
public u_VertexInterpolation,
public u_ReliefDepthScale,
Expand Down Expand Up @@ -3952,6 +3985,7 @@ class GLShader_lightMappingMaterial :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_ReliefDepthScale,
public u_ReliefOffsetBias,
public u_NormalScale,
Expand Down Expand Up @@ -4195,6 +4229,7 @@ class GLShader_skybox :
public u_CloudHeight,
public u_UseCloudMap,
public u_AlphaThreshold,
public u_LinearizeTexture,
public u_ModelViewProjectionMatrix
{
public:
Expand All @@ -4210,6 +4245,7 @@ class GLShader_skyboxMaterial :
public u_CloudHeight,
public u_UseCloudMap,
public u_AlphaThreshold,
public u_LinearizeTexture,
public u_ModelViewProjectionMatrix {
public:
GLShader_skyboxMaterial( GLShaderManager* manager );
Expand All @@ -4221,6 +4257,7 @@ class GLShader_fogQuake3 :
public u_FogMap,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_ColorGlobal,
public u_Bones,
public u_VertexInterpolation,
Expand All @@ -4241,6 +4278,7 @@ class GLShader_fogQuake3Material :
public u_FogMap,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_LinearizeTexture,
public u_ColorGlobal,
public u_FogDistanceVector,
public u_FogDepthVector,
Expand All @@ -4257,6 +4295,7 @@ class GLShader_fogGlobal :
public u_DepthMap,
public u_ModelViewProjectionMatrix,
public u_UnprojectMatrix,
public u_LinearizeTexture,
public u_Color,
public u_FogDistanceVector
{
Expand Down Expand Up @@ -4354,7 +4393,8 @@ class GLShader_cameraEffects :
public u_ColorModulate,
public u_TextureMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseGamma
public u_InverseGamma,
public u_DelinearizeScreen
{
public:
GLShader_cameraEffects( GLShaderManager *manager );
Expand Down
5 changes: 5 additions & 0 deletions src/engine/renderer/glsl_source/cameraEffects_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

/* cameraEffects_fp.glsl */

#insert colorSpace

uniform sampler2D u_CurrentMap;

uniform bool u_DelinearizeScreen;
#if defined(r_colorGrading)
uniform sampler3D u_ColorMap3D;
#endif
Expand All @@ -42,6 +45,8 @@ void main()

vec4 color = texture2D(u_CurrentMap, st);

convertToSRGB(color.rgb, u_DelinearizeScreen);

color = clamp(color, 0.0, 1.0);

#if defined(r_colorGrading)
Expand Down
Loading