Skip to content

Commit

Permalink
OpenGL2: Remove sRGB support, replace with gamma cvars.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileTheory committed Apr 16, 2014
1 parent 880a7c6 commit 4fe69cb
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 230 deletions.
9 changes: 7 additions & 2 deletions code/renderergl2/glsl/calclevels4x_fp.glsl
Expand Up @@ -9,10 +9,15 @@ const vec3 LUMINANCE_VECTOR = vec3(0.2125, 0.7154, 0.0721); //vec3(0.299, 0.5

vec3 GetValues(vec2 offset, vec3 current)
{
vec3 minAvgMax;
vec2 tc = var_TexCoords + u_InvTexRes * offset; minAvgMax = texture2D(u_TextureMap, tc).rgb;
vec2 tc = var_TexCoords + u_InvTexRes * offset;
vec3 minAvgMax = texture2D(u_TextureMap, tc).rgb;

#ifdef FIRST_PASS

#if defined(r_framebufferGamma)
minAvgMax = pow(minAvgMax, vec3(r_framebufferGamma));
#endif

float lumi = max(dot(LUMINANCE_VECTOR, minAvgMax), 0.000001);
float loglumi = clamp(log2(lumi), -10.0, 10.0);
minAvgMax = vec3(loglumi * 0.05 + 0.5);
Expand Down
113 changes: 71 additions & 42 deletions code/renderergl2/glsl/lightall_fp.glsl
Expand Up @@ -65,10 +65,6 @@ varying vec3 var_ViewDir;
#endif
#endif

#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
varying vec3 var_LightColor;
#endif

#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
varying vec4 var_LightDir;
#endif
Expand Down Expand Up @@ -329,9 +325,9 @@ mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )

void main()
{
vec3 viewDir;
vec3 viewDir, lightColor, ambientColor;
vec3 L, N, E, H;
float NL, NH, NE, EH;
float NL, NH, NE, EH, attenuation;

#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_VERT_TANGENT_SPACE)
Expand All @@ -352,16 +348,10 @@ void main()
#endif

#if defined(USE_LIGHTMAP)
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw);
vec3 lightColor = lightSample.rgb;
vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
#if defined(RGBM_LIGHTMAP)
lightColor *= lightSample.a;
lightmapColor.rgb *= lightmapColor.a;
#endif
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
vec3 ambientColor = u_AmbientLight;
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
vec3 lightColor = var_LightColor;
#endif

vec2 texCoords = var_TexCoords.xy;
Expand All @@ -375,12 +365,22 @@ void main()
#endif

vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
#if defined(USE_GAMMA2_TEXTURES)
diffuse.rgb *= diffuse.rgb;
#endif


#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHTMAP)
lightColor = lightmapColor.rgb * var_Color.rgb;
ambientColor = vec3(0.0);
attenuation = 1.0;
#elif defined(USE_LIGHT_VECTOR)
lightColor = u_DirectedLight * var_Color.rgb;
ambientColor = u_AmbientLight * var_Color.rgb;
attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
#elif defined(USE_LIGHT_VERTEX)
lightColor = var_Color.rgb;
ambientColor = vec3(0.0);
attenuation = 1.0;
#endif

#if defined(USE_NORMALMAP)
#if defined(SWIZZLE_NORMALMAP)
N.xy = texture2D(u_NormalMap, texCoords).ag - vec2(0.5);
Expand Down Expand Up @@ -416,8 +416,13 @@ void main()
#endif
#endif

#if defined(r_lightGamma)
lightColor = pow(lightColor, vec3(r_lightGamma));
ambientColor = pow(ambientColor, vec3(r_lightGamma));
#endif

#if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
vec3 ambientColor = lightColor;
ambientColor = lightColor;
float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);

// Scale the incoming light to compensate for the baked-in light angle
Expand All @@ -426,7 +431,7 @@ void main()

// Recover any unused light as ambient, in case attenuation is over 4x or
// light is below the surface
ambientColor -= lightColor * surfNL;
ambientColor = clamp(ambientColor - lightColor * surfNL, 0.0, 1.0);
#endif

vec3 reflectance;
Expand All @@ -436,15 +441,17 @@ void main()

#if defined(USE_SPECULARMAP)
vec4 specular = texture2D(u_SpecularMap, texCoords);
#if defined(USE_GAMMA2_TEXTURES)
specular.rgb *= specular.rgb;
#endif
#else
vec4 specular = vec4(1.0);
#endif

specular *= u_SpecularScale;

#if defined(r_materialGamma)
diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
specular.rgb = pow(specular.rgb, vec3(r_materialGamma));
#endif

float gloss = specular.a;
float shininess = exp2(gloss * 13.0);

Expand Down Expand Up @@ -482,7 +489,7 @@ void main()
#endif
#endif

gl_FragColor.rgb = lightColor * reflectance * NL;
gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL);

#if 0
vec3 aSpecular = EnvironmentBRDF(gloss, NE, specular.rgb);
Expand All @@ -506,19 +513,23 @@ void main()

// parallax corrected cubemap (cheaper trick)
// from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
R += u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;

vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;

#if defined(USE_LIGHTMAP)
cubeLightColor *= lightSample.rgb;
#elif defined (USE_LIGHT_VERTEX)
cubeLightColor *= var_LightColor;
#else
cubeLightColor *= lightColor * NL + ambientColor;
#endif
// normalize cubemap based on lowest mip (~diffuse)
// multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation
//vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, 6.0).rgb, 0.5 / 255.0);
//cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721));

#if defined(r_framebufferGamma)
cubeLightColor = pow(cubeLightColor, vec3(r_framebufferGamma));
#endif

// multiply cubemap values by lighting
// not technically correct, but helps make reflections look less unnatural
//cubeLightColor *= lightColor * (attenuation * NL) + ambientColor;

//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb * u_EnableTextures.w;
gl_FragColor.rgb += cubeLightColor * reflectance;
#endif

Expand All @@ -541,25 +552,43 @@ void main()
reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess);
reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess);

lightColor = u_PrimaryLightColor;
lightColor = u_PrimaryLightColor * var_Color.rgb;

// enable when point lights are supported as primary lights
//lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
#if defined(r_lightGamma)
lightColor = pow(lightColor, vec3(r_lightGamma));
#endif

#if defined(USE_SHADOWMAP)
lightColor *= shadowValue;
#endif

// enable when point lights are supported as primary lights
//lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);

gl_FragColor.rgb += lightColor * reflectance * NL2;
#endif

gl_FragColor.a = diffuse.a;
#else
gl_FragColor = diffuse;
lightColor = var_Color.rgb;

#if defined(USE_LIGHTMAP)
gl_FragColor.rgb *= lightColor;
lightColor *= lightmapColor.rgb;
#endif

#if defined(r_lightGamma)
lightColor = pow(lightColor, vec3(r_lightGamma));
#endif

#if defined(r_materialGamma)
diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
#endif

gl_FragColor.rgb = diffuse.rgb * lightColor;

#endif

#if defined(r_framebufferGamma)
gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / r_framebufferGamma));
#endif

gl_FragColor *= var_Color;
gl_FragColor.a = diffuse.a * var_Color.a;
}
8 changes: 0 additions & 8 deletions code/renderergl2/glsl/lightall_vp.glsl
Expand Up @@ -83,10 +83,6 @@ varying vec3 var_ViewDir;
#endif
#endif

#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
varying vec3 var_LightColor;
#endif

#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
varying vec4 var_LightDir;
#endif
Expand Down Expand Up @@ -216,10 +212,6 @@ void main()
#endif

var_Color = u_VertColor * attr_Color + u_BaseColor;
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
var_LightColor = var_Color.rgb;
var_Color.rgb = vec3(1.0);
#endif

#if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT)
float sqrLightDist = dot(L, L);
Expand Down
9 changes: 9 additions & 0 deletions code/renderergl2/glsl/tonemap_fp.glsl
Expand Up @@ -32,6 +32,11 @@ vec3 FilmicTonemap(vec3 x)
void main()
{
vec4 color = texture2D(u_TextureMap, var_TexCoords) * u_Color;

#if defined(r_framebufferGamma)
color.rgb = pow(color.rgb, vec3(r_framebufferGamma));
#endif

vec3 minAvgMax = texture2D(u_LevelsMap, var_TexCoords).rgb;
vec3 logMinAvgMaxLum = clamp(minAvgMax * 20.0 - 10.0, -u_AutoExposureMinMax.y, -u_AutoExposureMinMax.x);

Expand All @@ -44,5 +49,9 @@ void main()
vec3 fWhite = 1.0 / FilmicTonemap(vec3(u_ToneMinAvgMaxLinear.z - u_ToneMinAvgMaxLinear.x));
color.rgb = FilmicTonemap(color.rgb) * fWhite;

#if defined(r_tonemapGamma)
color.rgb = pow(color.rgb, vec3(1.0 / r_tonemapGamma));
#endif

gl_FragColor = clamp(color, 0.0, 1.0);
}
46 changes: 0 additions & 46 deletions code/renderergl2/tr_extensions.c
Expand Up @@ -593,52 +593,6 @@ void GLimp_InitExtraExtensions()
ri.Printf(PRINT_ALL, result[2], extension);
}

// GL_EXT_texture_sRGB
extension = "GL_EXT_texture_sRGB";
glRefConfig.textureSrgb = qfalse;
if (GLimp_HaveExtension(extension))
{
if (r_srgb->integer)
glRefConfig.textureSrgb = qtrue;

ri.Printf(PRINT_ALL, result[glRefConfig.textureSrgb], extension);
}
else
{
ri.Printf(PRINT_ALL, result[2], extension);
}

// GL_EXT_framebuffer_sRGB
extension = "GL_EXT_framebuffer_sRGB";
glRefConfig.framebufferSrgb = qfalse;
if (GLimp_HaveExtension(extension))
{
if (r_srgb->integer)
glRefConfig.framebufferSrgb = qtrue;

ri.Printf(PRINT_ALL, result[glRefConfig.framebufferSrgb], extension);
}
else
{
ri.Printf(PRINT_ALL, result[2], extension);
}

// GL_EXT_texture_sRGB_decode
extension = "GL_EXT_texture_sRGB_decode";
glRefConfig.textureSrgbDecode = qfalse;
if (GLimp_HaveExtension(extension))
{
if (r_srgb->integer)
glRefConfig.textureSrgbDecode = qtrue;

ri.Printf(PRINT_ALL, result[glRefConfig.textureSrgbDecode], extension);
}
else
{
ri.Printf(PRINT_ALL, result[2], extension);
}


glRefConfig.textureCompression = TCR_NONE;

// GL_EXT_texture_compression_latc
Expand Down
12 changes: 12 additions & 0 deletions code/renderergl2/tr_glsl.c
Expand Up @@ -321,6 +321,18 @@ static void GLSL_GetShaderHeader( GLenum shaderType, const GLcharARB *extra, cha
Q_strcat(dest, size,
va("#ifndef r_FBufScale\n#define r_FBufScale vec2(%f, %f)\n#endif\n", fbufWidthScale, fbufHeightScale));

if (r_materialGamma->value != 1.0f)
Q_strcat(dest, size, va("#ifndef r_materialGamma\n#define r_materialGamma %f\n#endif\n", r_materialGamma->value));

if (r_lightGamma->value != 1.0f)
Q_strcat(dest, size, va("#ifndef r_lightGamma\n#define r_lightGamma %f\n#endif\n", r_lightGamma->value));

if (r_framebufferGamma->value != 1.0f)
Q_strcat(dest, size, va("#ifndef r_framebufferGamma\n#define r_framebufferGamma %f\n#endif\n", r_framebufferGamma->value));

if (r_tonemapGamma->value != 1.0f)
Q_strcat(dest, size, va("#ifndef r_tonemapGamma\n#define r_tonemapGamma %f\n#endif\n", r_tonemapGamma->value));

if (extra)
{
Q_strcat(dest, size, extra);
Expand Down

0 comments on commit 4fe69cb

Please sign in to comment.