Skip to content

renderer: implement complete overBright in GLSL and high-precision frame-buffer #1050

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

Merged
merged 2 commits into from
Mar 12, 2024
Merged
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
9 changes: 8 additions & 1 deletion src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,7 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
u_ModelMatrix( this ),
u_ProjectionMatrixTranspose( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_ColorModulate( this ),
u_Color( this ),
u_Bones( this ),
Expand Down Expand Up @@ -1653,6 +1654,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_ReliefDepthScale( this ),
Expand Down Expand Up @@ -1973,6 +1975,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_VertexInterpolation( this ),
GLDeformStage( this ),
GLCompileMacro_USE_ALPHA_TESTING( this )
Expand All @@ -1989,6 +1992,7 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
GLShader( "fogQuake3", ATTR_POSITION | ATTR_QTANGENT, manager ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_Color( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
Expand Down Expand Up @@ -2017,6 +2021,7 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
u_ViewMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_UnprojectMatrix( this ),
u_InverseLightFactor( this ),
u_Color( this ),
u_FogDistanceVector( this ),
u_FogDepthVector( this )
Expand Down Expand Up @@ -2094,7 +2099,8 @@ void GLShader_portal::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )

GLShader_contrast::GLShader_contrast( GLShaderManager *manager ) :
GLShader( "contrast", ATTR_POSITION, manager ),
u_ModelViewProjectionMatrix( this )
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this )
{
}

Expand All @@ -2108,6 +2114,7 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
u_ColorModulate( this ),
u_TextureMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LightFactor( this ),
u_DeformMagnitude( this ),
u_InverseGamma( this )
{
Expand Down
39 changes: 38 additions & 1 deletion src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,36 @@ class GLCompileMacro_USE_ALPHA_TESTING :
}
};

class u_LightFactor :
GLUniform1f
{
public:
u_LightFactor( GLShader *shader ) :
GLUniform1f( shader, "u_LightFactor" )
{
}

void SetUniform_LightFactor( const float lightFactor )
{
this->SetValue( lightFactor );
}
};

class u_InverseLightFactor :
GLUniform1f
{
public:
u_InverseLightFactor( GLShader *shader ) :
GLUniform1f( shader, "u_InverseLightFactor" )
{
}

void SetUniform_InverseLightFactor( const float inverseLightFactor )
{
this->SetValue( inverseLightFactor );
}
};

class u_TextureMatrix :
GLUniformMatrix4f
{
Expand Down Expand Up @@ -2270,6 +2300,7 @@ class GLShader_generic :
public u_ModelMatrix,
public u_ProjectionMatrixTranspose,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_ColorModulate,
public u_Color,
public u_Bones,
Expand Down Expand Up @@ -2300,6 +2331,7 @@ class GLShader_lightMapping :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_Bones,
public u_VertexInterpolation,
public u_ReliefDepthScale,
Expand Down Expand Up @@ -2502,6 +2534,7 @@ class GLShader_skybox :
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_VertexInterpolation,
public GLDeformStage,
public GLCompileMacro_USE_ALPHA_TESTING
Expand All @@ -2515,6 +2548,7 @@ class GLShader_fogQuake3 :
public GLShader,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_Color,
public u_Bones,
public u_VertexInterpolation,
Expand All @@ -2537,6 +2571,7 @@ class GLShader_fogGlobal :
public u_ViewMatrix,
public u_ModelViewProjectionMatrix,
public u_UnprojectMatrix,
public u_InverseLightFactor,
public u_Color,
public u_FogDistanceVector,
public u_FogDepthVector
Expand Down Expand Up @@ -2595,7 +2630,8 @@ class GLShader_portal :

class GLShader_contrast :
public GLShader,
public u_ModelViewProjectionMatrix
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor
{
public:
GLShader_contrast( GLShaderManager *manager );
Expand All @@ -2607,6 +2643,7 @@ class GLShader_cameraEffects :
public u_ColorModulate,
public u_TextureMatrix,
public u_ModelViewProjectionMatrix,
public u_LightFactor,
public u_DeformMagnitude,
public u_InverseGamma
{
Expand Down
8 changes: 6 additions & 2 deletions src/engine/renderer/glsl_source/cameraEffects_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

uniform sampler2D u_CurrentMap;
uniform sampler3D u_ColorMap;

uniform float u_LightFactor;
uniform vec4 u_ColorModulate;
uniform float u_InverseGamma;

Expand All @@ -36,9 +38,11 @@ void main()
// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 st = gl_FragCoord.st / r_FBufSize;

vec4 original = clamp(texture2D(u_CurrentMap, st), 0.0, 1.0);
vec4 color = texture2D(u_CurrentMap, st);

color.rgb *= u_LightFactor;

vec4 color = original;
color = clamp(color, 0.0, 1.0);

// apply color grading
vec3 colCoord = color.rgb * 15.0 / 16.0 + 0.5 / 16.0;
Expand Down
4 changes: 4 additions & 0 deletions src/engine/renderer/glsl_source/contrast_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

uniform sampler2D u_ColorMap;

uniform float u_InverseLightFactor;

const vec4 LUMINANCE_VECTOR = vec4(0.2125, 0.7154, 0.0721, 0.0);

#if __VERSION__ > 120
Expand Down Expand Up @@ -57,5 +59,7 @@ void main()
color += f(texture2D(u_ColorMap, st + vec2(1.0, 1.0) * scale));
color *= 0.25;

color.rgb *= u_InverseLightFactor;

outputColor = color;
}
8 changes: 7 additions & 1 deletion src/engine/renderer/glsl_source/fogGlobal_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

uniform sampler2D u_ColorMap; // fog texture
uniform sampler2D u_DepthMap;

uniform float u_InverseLightFactor;
uniform vec3 u_ViewOrigin;
uniform vec4 u_FogDistanceVector;
uniform vec4 u_FogDepthVector;
Expand Down Expand Up @@ -52,5 +54,9 @@ void main()
// st.s = vertexDistanceToCamera;
st.t = 1.0;

outputColor = u_Color * texture2D(u_ColorMap, st);
vec4 color = texture2D(u_ColorMap, st);

color.rgb *= u_InverseLightFactor;

outputColor = u_Color * color;
}
4 changes: 4 additions & 0 deletions src/engine/renderer/glsl_source/fogQuake3_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

uniform sampler2D u_ColorMap;

uniform float u_InverseLightFactor;
IN(smooth) vec3 var_Position;
IN(smooth) vec2 var_TexCoords;
IN(smooth) vec4 var_Color;
Expand All @@ -35,6 +36,9 @@ void main()
vec4 color = texture2D(u_ColorMap, var_TexCoords);

color *= var_Color;

color.rgb *= u_InverseLightFactor;

outputColor = color;

#if 0
Expand Down
9 changes: 9 additions & 0 deletions src/engine/renderer/glsl_source/generic_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
uniform sampler2D u_ColorMap;
uniform float u_AlphaThreshold;

#if !defined(GENERIC_2D)
uniform float u_InverseLightFactor;
#endif

IN(smooth) vec2 var_TexCoords;
IN(smooth) vec4 var_Color;

Expand Down Expand Up @@ -54,6 +58,11 @@ void main()
#endif

color *= var_Color;

#if !defined(GENERIC_2D) && !defined(USE_DEPTH_FADE)
color.rgb *= u_InverseLightFactor;
#endif

outputColor = color;

#if defined(GENERIC_2D)
Expand Down
19 changes: 18 additions & 1 deletion src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uniform sampler2D u_MaterialMap;
uniform sampler2D u_GlowMap;

uniform float u_AlphaThreshold;
uniform float u_InverseLightFactor;
uniform vec3 u_ViewOrigin;

IN(smooth) vec3 var_Position;
Expand Down Expand Up @@ -172,9 +173,25 @@ void main()
color.rgb += 0.7 * emission;
#endif

/* HACK: use sign to know if there is a light or not, and
then if it will receive overbright multiplication or not. */
if ( u_InverseLightFactor > 0 )
{
color.rgb *= u_InverseLightFactor;
}

#if defined(r_glowMapping)
// Blend glow map.
color.rgb += texture2D(u_GlowMap, texCoords).rgb;
vec3 glow = texture2D(u_GlowMap, texCoords).rgb;

/* HACK: use sign to know if there is a light or not, and
then if it will receive overbright multiplication or not. */
if ( u_InverseLightFactor < 0 )
{
glow *= - u_InverseLightFactor;
}

color.rgb += glow;
#endif

outputColor = color;
Expand Down
4 changes: 4 additions & 0 deletions src/engine/renderer/glsl_source/skybox_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
const float radiusWorld = 4096.0; // Value used by quake 3 skybox code

uniform samplerCube u_ColorMap;

uniform float u_InverseLightFactor;
uniform sampler2D u_CloudMap;

uniform bool u_UseCloudMap;
Expand Down Expand Up @@ -73,5 +75,7 @@ void main()
}
#endif

color.rgb *= u_InverseLightFactor;

outputColor = color;
}
9 changes: 9 additions & 0 deletions src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,9 @@ void RB_RenderGlobalFog()

gl_fogGlobalShader->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin ); // world space

// u_InverseLightFactor
gl_fogGlobalShader->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );

{
fog_t *fog;

Expand Down Expand Up @@ -3067,6 +3070,9 @@ void RB_RenderBloom()

gl_contrastShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );

// u_InverseLightFactor
gl_contrastShader->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );

GL_BindToTMU( 0, tr.currentRenderImage[ backEnd.currentMainFBO ] );

GL_PopMatrix(); // special 1/4th of the screen contrastRenderFBO ortho
Expand Down Expand Up @@ -3292,6 +3298,9 @@ void RB_CameraPostFX()
// enable shader, set arrays
gl_cameraEffectsShader->BindProgram( 0 );

// u_LightFactor
gl_cameraEffectsShader->SetUniform_LightFactor( tr.mapLightFactor );

gl_cameraEffectsShader->SetUniform_ColorModulate( backEnd.viewParms.gradingWeights );
gl_cameraEffectsShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );

Expand Down
Loading