Skip to content

Commit 2616573

Browse files
committed
renderer: implement sRGB in GLSL
1 parent a6d89da commit 2616573

22 files changed

+390
-12
lines changed

src.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ set(GLSLSOURCELIST
180180
${ENGINE_DIR}/renderer/glsl_source/blur_vp.glsl
181181
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
182182
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_vp.glsl
183+
${ENGINE_DIR}/renderer/glsl_source/colorSpace.glsl
183184
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl
184185
${ENGINE_DIR}/renderer/glsl_source/contrast_fp.glsl
185186
${ENGINE_DIR}/renderer/glsl_source/contrast_vp.glsl

src/common/Color.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
#include "Compiler.h"
3939
#include "Math.h"
4040

41+
#define convertFromSRGB( v ) v <= 0.04045f ? v * (1.0f / 12.92f) : pow((v + 0.055f) * (1.0f / 1.055f), 2.4f)
42+
4143
namespace Color {
4244

4345
/*
@@ -256,6 +258,20 @@ class BasicColor
256258
data_[ 3 ] = v;
257259
}
258260

261+
CONSTEXPR_FUNCTION_RELAXED component_type ConvertFromSRGB( component_type v ) NOEXCEPT
262+
{
263+
float f = float( v ) / 255.0f;
264+
f = convertFromSRGB( f );
265+
return component_type( f * 255 );
266+
}
267+
268+
CONSTEXPR_FUNCTION_RELAXED void ConvertFromSRGB() NOEXCEPT
269+
{
270+
SetRed( ConvertFromSRGB( Red() ) );
271+
SetGreen( ConvertFromSRGB( Green() ) );
272+
SetBlue( ConvertFromSRGB( Blue() ) );
273+
}
274+
259275
CONSTEXPR_FUNCTION_RELAXED BasicColor& operator*=( float factor ) NOEXCEPT
260276
{
261277
*this = *this * factor;

src/engine/renderer/Material.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, boo
157157
Tess_ComputeColor( pStage );
158158
gl_genericShaderMaterial->SetUniform_Color( tess.svars.color );
159159

160+
// u_LinearizeTexture
161+
gl_genericShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );
162+
160163
bool hasDepthFade = pStage->hasDepthFade;
161164
if ( hasDepthFade ) {
162165
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
@@ -190,6 +193,9 @@ void UpdateSurfaceDataLightMapping( uint32_t* materials, shaderStage_t* pStage,
190193
// u_AlphaThreshold
191194
gl_lightMappingShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );
192195

196+
// u_LinearizeTexture
197+
gl_lightMappingShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );
198+
193199
// HeightMap
194200
if ( pStage->enableReliefMapping ) {
195201
float depthScale = RB_EvalExpression( &pStage->depthScaleExp, r_reliefDepthScale->value );
@@ -267,6 +273,9 @@ void UpdateSurfaceDataSkybox( uint32_t* materials, shaderStage_t* pStage, bool,
267273
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );
268274

269275
gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );
276+
277+
// u_LinearizeTexture
278+
gl_skyboxShaderMaterial->SetUniform_LinearizeTexture( pStage->linearizeTexture );
270279
}
271280

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

361370
gl_fogQuake3ShaderMaterial->WriteUniformsToBuffer( materials );
371+
372+
gl_fogQuake3ShaderMaterial->SetUniform_LinearizeTexture( tr.worldLinearizeTexture );
362373
}
363374

364375
/*

src/engine/renderer/gl_shader.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,11 @@ static std::string GenEngineConstants() {
802802
AddConst( str, "r_RimExponent", r_rimExponent->value );
803803
}
804804

805+
if ( r_cheapSRGB.Get() )
806+
{
807+
AddDefine( str, "r_cheapSRGB", 1 );
808+
}
809+
805810
if ( r_showLightTiles->integer )
806811
{
807812
AddDefine( str, "r_showLightTiles", 1 );
@@ -2261,6 +2266,7 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
22612266
u_AlphaThreshold( this ),
22622267
u_ModelMatrix( this ),
22632268
u_ModelViewProjectionMatrix( this ),
2269+
u_LinearizeTexture( this ),
22642270
u_ColorModulateColorGen( this ),
22652271
u_Color( this ),
22662272
u_Bones( this ),
@@ -2293,6 +2299,7 @@ GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
22932299
u_AlphaThreshold( this ),
22942300
u_ModelMatrix( this ),
22952301
u_ModelViewProjectionMatrix( this ),
2302+
u_LinearizeTexture( this ),
22962303
u_ColorModulateColorGen( this ),
22972304
u_Color( this ),
22982305
u_DepthScale( this ),
@@ -2334,6 +2341,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
23342341
u_ViewOrigin( this ),
23352342
u_ModelMatrix( this ),
23362343
u_ModelViewProjectionMatrix( this ),
2344+
u_LinearizeTexture( this ),
23372345
u_Bones( this ),
23382346
u_VertexInterpolation( this ),
23392347
u_ReliefDepthScale( this ),
@@ -2402,6 +2410,7 @@ GLShader_lightMappingMaterial::GLShader_lightMappingMaterial( GLShaderManager* m
24022410
u_ViewOrigin( this ),
24032411
u_ModelMatrix( this ),
24042412
u_ModelViewProjectionMatrix( this ),
2413+
u_LinearizeTexture( this ),
24052414
u_ReliefDepthScale( this ),
24062415
u_ReliefOffsetBias( this ),
24072416
u_NormalScale( this ),
@@ -2712,6 +2721,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
27122721
u_CloudHeight( this ),
27132722
u_UseCloudMap( this ),
27142723
u_AlphaThreshold( this ),
2724+
u_LinearizeTexture( this ),
27152725
u_ModelViewProjectionMatrix( this )
27162726
{
27172727
}
@@ -2730,6 +2740,7 @@ GLShader_skyboxMaterial::GLShader_skyboxMaterial( GLShaderManager* manager ) :
27302740
u_CloudHeight( this ),
27312741
u_UseCloudMap( this ),
27322742
u_AlphaThreshold( this ),
2743+
u_LinearizeTexture( this ),
27332744
u_ModelViewProjectionMatrix( this )
27342745
{}
27352746

@@ -2743,6 +2754,7 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
27432754
u_FogMap( this ),
27442755
u_ModelMatrix( this ),
27452756
u_ModelViewProjectionMatrix( this ),
2757+
u_LinearizeTexture( this ),
27462758
u_ColorGlobal( this ),
27472759
u_Bones( this ),
27482760
u_VertexInterpolation( this ),
@@ -2765,6 +2777,7 @@ GLShader_fogQuake3Material::GLShader_fogQuake3Material( GLShaderManager* manager
27652777
u_FogMap( this ),
27662778
u_ModelMatrix( this ),
27672779
u_ModelViewProjectionMatrix( this ),
2780+
u_LinearizeTexture( this ),
27682781
u_ColorGlobal( this ),
27692782
u_FogDistanceVector( this ),
27702783
u_FogDepthVector( this ),
@@ -2782,6 +2795,7 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
27822795
u_DepthMap( this ),
27832796
u_ModelViewProjectionMatrix( this ),
27842797
u_UnprojectMatrix( this ),
2798+
u_LinearizeTexture( this ),
27852799
u_Color( this ),
27862800
u_FogDistanceVector( this )
27872801
{
@@ -2896,7 +2910,8 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
28962910
u_ColorModulate( this ),
28972911
u_TextureMatrix( this ),
28982912
u_ModelViewProjectionMatrix( this ),
2899-
u_InverseGamma( this )
2913+
u_InverseGamma( this ),
2914+
u_DelinearizeScreen( this )
29002915
{
29012916
}
29022917

src/engine/renderer/gl_shader.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2806,6 +2806,36 @@ class u_ShadowTexelSize :
28062806
}
28072807
};
28082808

2809+
class u_LinearizeTexture :
2810+
GLUniform1i
2811+
{
2812+
public:
2813+
u_LinearizeTexture( GLShader *shader ) :
2814+
GLUniform1i( shader, "u_LinearizeTexture" )
2815+
{
2816+
}
2817+
2818+
void SetUniform_LinearizeTexture( const int value )
2819+
{
2820+
this->SetValue( value );
2821+
}
2822+
};
2823+
2824+
class u_DelinearizeScreen :
2825+
GLUniform1i
2826+
{
2827+
public:
2828+
u_DelinearizeScreen( GLShader *shader ) :
2829+
GLUniform1i( shader, "u_DelinearizeScreen" )
2830+
{
2831+
}
2832+
2833+
void SetUniform_DelinearizeScreen( const int value )
2834+
{
2835+
this->SetValue( value );
2836+
}
2837+
};
2838+
28092839
class u_ShadowBlur :
28102840
GLUniform1f
28112841
{
@@ -3834,6 +3864,7 @@ class GLShader_generic :
38343864
public u_AlphaThreshold,
38353865
public u_ModelMatrix,
38363866
public u_ModelViewProjectionMatrix,
3867+
public u_LinearizeTexture,
38373868
public u_ColorModulateColorGen,
38383869
public u_Color,
38393870
public u_Bones,
@@ -3863,6 +3894,7 @@ class GLShader_genericMaterial :
38633894
public u_AlphaThreshold,
38643895
public u_ModelMatrix,
38653896
public u_ModelViewProjectionMatrix,
3897+
public u_LinearizeTexture,
38663898
public u_ColorModulateColorGen,
38673899
public u_Color,
38683900
public u_DepthScale,
@@ -3901,6 +3933,7 @@ class GLShader_lightMapping :
39013933
public u_ViewOrigin,
39023934
public u_ModelMatrix,
39033935
public u_ModelViewProjectionMatrix,
3936+
public u_LinearizeTexture,
39043937
public u_Bones,
39053938
public u_VertexInterpolation,
39063939
public u_ReliefDepthScale,
@@ -3952,6 +3985,7 @@ class GLShader_lightMappingMaterial :
39523985
public u_ViewOrigin,
39533986
public u_ModelMatrix,
39543987
public u_ModelViewProjectionMatrix,
3988+
public u_LinearizeTexture,
39553989
public u_ReliefDepthScale,
39563990
public u_ReliefOffsetBias,
39573991
public u_NormalScale,
@@ -4195,6 +4229,7 @@ class GLShader_skybox :
41954229
public u_CloudHeight,
41964230
public u_UseCloudMap,
41974231
public u_AlphaThreshold,
4232+
public u_LinearizeTexture,
41984233
public u_ModelViewProjectionMatrix
41994234
{
42004235
public:
@@ -4210,6 +4245,7 @@ class GLShader_skyboxMaterial :
42104245
public u_CloudHeight,
42114246
public u_UseCloudMap,
42124247
public u_AlphaThreshold,
4248+
public u_LinearizeTexture,
42134249
public u_ModelViewProjectionMatrix {
42144250
public:
42154251
GLShader_skyboxMaterial( GLShaderManager* manager );
@@ -4221,6 +4257,7 @@ class GLShader_fogQuake3 :
42214257
public u_FogMap,
42224258
public u_ModelMatrix,
42234259
public u_ModelViewProjectionMatrix,
4260+
public u_LinearizeTexture,
42244261
public u_ColorGlobal,
42254262
public u_Bones,
42264263
public u_VertexInterpolation,
@@ -4241,6 +4278,7 @@ class GLShader_fogQuake3Material :
42414278
public u_FogMap,
42424279
public u_ModelMatrix,
42434280
public u_ModelViewProjectionMatrix,
4281+
public u_LinearizeTexture,
42444282
public u_ColorGlobal,
42454283
public u_FogDistanceVector,
42464284
public u_FogDepthVector,
@@ -4257,6 +4295,7 @@ class GLShader_fogGlobal :
42574295
public u_DepthMap,
42584296
public u_ModelViewProjectionMatrix,
42594297
public u_UnprojectMatrix,
4298+
public u_LinearizeTexture,
42604299
public u_Color,
42614300
public u_FogDistanceVector
42624301
{
@@ -4354,7 +4393,8 @@ class GLShader_cameraEffects :
43544393
public u_ColorModulate,
43554394
public u_TextureMatrix,
43564395
public u_ModelViewProjectionMatrix,
4357-
public u_InverseGamma
4396+
public u_InverseGamma,
4397+
public u_DelinearizeScreen
43584398
{
43594399
public:
43604400
GLShader_cameraEffects( GLShaderManager *manager );

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* cameraEffects_fp.glsl */
2424

25+
#insert colorSpace
26+
2527
uniform sampler2D u_CurrentMap;
2628

29+
uniform bool u_DelinearizeScreen;
2730
#if defined(r_colorGrading)
2831
uniform sampler3D u_ColorMap3D;
2932
#endif
@@ -42,6 +45,8 @@ void main()
4245

4346
vec4 color = texture2D(u_CurrentMap, st);
4447

48+
convertToSRGB(color.rgb, u_DelinearizeScreen);
49+
4550
color = clamp(color, 0.0, 1.0);
4651

4752
#if defined(r_colorGrading)

0 commit comments

Comments
 (0)