Skip to content

Commit 6324ce6

Browse files
committed
Merge branch 'master' into for-0.56.0/sync
2 parents 74e26bb + a247f5d commit 6324ce6

File tree

14 files changed

+106
-85
lines changed

14 files changed

+106
-85
lines changed

src/engine/renderer/GeometryOptimiser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial( world_t* world, bspSur
570570
VectorCopy( ( ( srfGeneric_t* ) surface->data )->origin, srf.origin );
571571
srf.radius = ( ( srfGeneric_t* ) surface->data )->radius;
572572

573-
materialSystem.GenerateMaterial( &srf );
573+
materialSystem.GenerateMaterial( &srf, world->globalFog );
574574

575575
static const float MAX_NORMAL_SURFACE_DISTANCE = 65536.0f * sqrtf( 2.0f );
576576
if ( VectorLength( ( ( srfGeneric_t* ) surface->data )->bounds[0] ) > MAX_NORMAL_SURFACE_DISTANCE

src/engine/renderer/Material.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,8 @@ void BindShaderFog( Material* material ) {
10911091

10921092
// all fogging distance is based on world Z units
10931093
vec4_t fogDistanceVector;
1094-
vec3_t local;
1095-
VectorSubtract( backEnd.orientation.origin, backEnd.viewParms.orientation.origin, local );
1096-
fogDistanceVector[0] = -backEnd.orientation.modelViewMatrix[2];
1097-
fogDistanceVector[1] = -backEnd.orientation.modelViewMatrix[6];
1098-
fogDistanceVector[2] = -backEnd.orientation.modelViewMatrix[10];
1099-
fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.orientation.axis[0] );
1094+
VectorCopy( backEnd.viewParms.orientation.axis[ 0 ], fogDistanceVector );
1095+
fogDistanceVector[ 3 ] = -DotProduct( fogDistanceVector, backEnd.viewParms.orientation.origin );
11001096

11011097
// scale the fog vectors based on the fog's thickness
11021098
VectorScale( fogDistanceVector, fog->tcScale, fogDistanceVector );
@@ -1106,15 +1102,9 @@ void BindShaderFog( Material* material ) {
11061102
float eyeT;
11071103
vec4_t fogDepthVector;
11081104
if ( fog->hasSurface ) {
1109-
fogDepthVector[0] = fog->surface[0] * backEnd.orientation.axis[0][0] +
1110-
fog->surface[1] * backEnd.orientation.axis[0][1] + fog->surface[2] * backEnd.orientation.axis[0][2];
1111-
fogDepthVector[1] = fog->surface[0] * backEnd.orientation.axis[1][0] +
1112-
fog->surface[1] * backEnd.orientation.axis[1][1] + fog->surface[2] * backEnd.orientation.axis[1][2];
1113-
fogDepthVector[2] = fog->surface[0] * backEnd.orientation.axis[2][0] +
1114-
fog->surface[1] * backEnd.orientation.axis[2][1] + fog->surface[2] * backEnd.orientation.axis[2][2];
1115-
fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.orientation.origin, fog->surface );
1116-
1117-
eyeT = DotProduct( backEnd.orientation.viewOrigin, fogDepthVector ) + fogDepthVector[3];
1105+
VectorCopy( fog->surface, fogDepthVector );
1106+
fogDepthVector[ 3 ] = -fog->surface[ 3 ];
1107+
eyeT = DotProduct( backEnd.viewParms.orientation.origin, fogDepthVector ) + fogDepthVector[ 3 ];
11181108
} else {
11191109
Vector4Set( fogDepthVector, 0, 0, 0, 1 );
11201110
eyeT = 1; // non-surface fog always has eye inside
@@ -1487,7 +1477,7 @@ void MaterialSystem::ProcessStage( MaterialSurface* surface, shaderStage_t* pSta
14871477
/* This will only generate a material itself
14881478
A material represents a distinct global OpenGL state (e. g. blend function, depth test, depth write etc.)
14891479
Materials can have a dependency on other materials to make sure that consecutive stages are rendered in the proper order */
1490-
void MaterialSystem::GenerateMaterial( MaterialSurface* surface ) {
1480+
void MaterialSystem::GenerateMaterial( MaterialSurface* surface, int globalFog ) {
14911481
uint32_t stage = 0;
14921482
uint32_t previousMaterialID = 0;
14931483

@@ -1508,7 +1498,7 @@ void MaterialSystem::GenerateMaterial( MaterialSurface* surface ) {
15081498
surface->stages++;
15091499
}
15101500

1511-
if ( !surface->shader->noFog && surface->fog >= 1 ) {
1501+
if ( !surface->shader->noFog && surface->fog >= 1 && surface->fog != globalFog ) {
15121502
uint32_t unused;
15131503
ProcessStage( surface, surface->shader->fogShader->stages, surface->shader->fogShader, packIDs, stage, unused, true );
15141504

@@ -2010,6 +2000,11 @@ void MaterialSystem::RenderMaterials( const shaderSort_t fromSort, const shaderS
20102000
}
20112001
}
20122002

2003+
// All material packs currently render depth-writing shaders.
2004+
// If we were to render depth fade or anything else sampling u_DepthMap with the material system,
2005+
// we would need to track this on a per-material basis.
2006+
backEnd.dirtyDepthBuffer = true;
2007+
20132008
GL_BindVAO( backEnd.defaultVAO );
20142009

20152010
// Draw the skybox here because we skipped R_AddWorldSurfaces()

src/engine/renderer/Material.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class MaterialSystem {
379379
const bool mayUseVertexOverbright, const bool vertexLit, const bool fullbright );
380380
void ProcessStage( MaterialSurface* surface, shaderStage_t* pStage, shader_t* shader, uint32_t* packIDs, uint32_t& stage,
381381
uint32_t& previousMaterialID, bool skipStageSync = false );
382-
void GenerateMaterial( MaterialSurface* surface );
382+
void GenerateMaterial( MaterialSurface* surface, int globalFog );
383383
void GenerateWorldMaterialsBuffer();
384384
void GenerateWorldCommandBuffer( std::vector<MaterialSurface>& surfaces );
385385
void GeneratePortalBoundingSpheres();

src/engine/renderer/gl_shader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ void GLShader::PostProcessUniforms() {
21392139
auto iterNext = FindUniformForOffset( uniformQueue, std140Size );
21402140
if ( iterNext == uniformQueue.end() ) {
21412141
// add 1 unit of padding
2142-
ASSERT( !( *iterNext )->_components ); // array WriteToBuffer impls don't handle padding correctly
2142+
ASSERT( !_materialSystemUniforms.back()->_components); // array WriteToBuffer impls don't handle padding correctly
21432143
++std140Size;
21442144
++_materialSystemUniforms.back()->_std430Size;
21452145
} else {
@@ -2639,7 +2639,8 @@ GLShader_fogGlobal::GLShader_fogGlobal() :
26392639
u_UnprojectMatrix( this ),
26402640
u_Color_Float( this ),
26412641
u_Color_Uint( this ),
2642-
u_FogDistanceVector( this )
2642+
u_ViewOrigin( this ),
2643+
u_FogDensity( this )
26432644
{
26442645
}
26452646

src/engine/renderer/gl_shader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3481,7 +3481,8 @@ class GLShader_fogGlobal :
34813481
public u_UnprojectMatrix,
34823482
public u_Color_Float,
34833483
public u_Color_Uint,
3484-
public u_FogDistanceVector
3484+
public u_ViewOrigin,
3485+
public u_FogDensity
34853486
{
34863487
public:
34873488
GLShader_fogGlobal();

src/engine/renderer/glsl_source/fogGlobal_fp.glsl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ uniform sampler2D u_DepthMap;
2929

3030
uniform colorPack u_Color;
3131

32-
uniform vec4 u_FogDistanceVector;
32+
uniform vec3 u_ViewOrigin;
33+
uniform float u_FogDensity;
3334
uniform mat4 u_UnprojectMatrix;
3435

35-
#if __VERSION__ > 120
36-
out vec4 outputColor;
37-
#else
38-
#define outputColor gl_FragColor
39-
#endif
36+
DECLARE_OUTPUT(vec4)
4037

4138
void main()
4239
{
@@ -48,9 +45,8 @@ void main()
4845
vec4 P = u_UnprojectMatrix * vec4(gl_FragCoord.xy, depth, 1.0);
4946
P.xyz /= P.w;
5047

51-
// calculate the length in fog (t is always 0 if eye is in fog)
52-
st.s = dot(P.xyz, u_FogDistanceVector.xyz) + u_FogDistanceVector.w;
53-
// st.s = vertexDistanceToCamera;
48+
// calculate the length in fog (t is always 1 if eye is in fog)
49+
st.s = distance(u_ViewOrigin, P.xyz) * u_FogDensity;
5450
st.t = 1.0;
5551

5652
vec4 color = texture2D(u_ColorMap, st);

src/engine/renderer/glsl_source/fogQuake3_fp.glsl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626

2727
uniform sampler2D u_FogMap;
2828

29-
IN(smooth) vec3 var_Position;
3029
IN(smooth) vec2 var_TexCoords;
3130
IN(smooth) vec4 var_Color;
3231

@@ -41,8 +40,4 @@ void main()
4140
color *= var_Color;
4241

4342
outputColor = color;
44-
45-
#if 0
46-
outputColor = vec4(vec3(1.0, 0.0, 0.0), color.a);
47-
#endif
4843
}

src/engine/renderer/glsl_source/fogQuake3_vp.glsl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ uniform colorPack u_ColorGlobal;
3434
uniform mat4 u_ModelMatrix;
3535
uniform mat4 u_ModelViewProjectionMatrix;
3636

37-
uniform vec4 u_FogDistanceVector;
38-
uniform vec4 u_FogDepthVector;
37+
uniform vec4 u_FogDistanceVector; // view axis, scaled by fog density
38+
uniform vec4 u_FogDepthVector; // fog plane
3939
uniform float u_FogEyeT;
4040

41-
OUT(smooth) vec3 var_Position;
41+
// var_TexCoords.s is distance from viewer to vertex dotted with view axis
42+
// var_TexCoords.t is the fraction of the viewer-to-vertex ray which is inside fog
4243
OUT(smooth) vec2 var_TexCoords;
4344
OUT(smooth) vec4 var_Color;
4445

@@ -71,7 +72,8 @@ void main()
7172
gl_Position = u_ModelViewProjectionMatrix * position;
7273

7374
// transform position into world space
74-
var_Position = (u_ModelMatrix * position).xyz;
75+
position = u_ModelMatrix * position;
76+
position.xyz /= position.w;
7577

7678
// calculate the length in fog
7779
float s = dot(position.xyz, u_FogDistanceVector.xyz) + u_FogDistanceVector.w;

src/engine/renderer/tr_backend.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,25 @@ void RB_RunVisTests( )
12101210
}
12111211
}
12121212

1213+
void RB_PrepareForSamplingDepthMap()
1214+
{
1215+
if ( !glConfig.textureBarrierAvailable )
1216+
{
1217+
return;
1218+
}
1219+
1220+
if ( !backEnd.dirtyDepthBuffer )
1221+
{
1222+
return;
1223+
}
1224+
1225+
// Flush depth buffer to make sure it is available for reading in the depth fade
1226+
// GLSL - prevents https://github.com/DaemonEngine/Daemon/issues/1676
1227+
glTextureBarrier();
1228+
1229+
backEnd.dirtyDepthBuffer = false;
1230+
}
1231+
12131232
static void RenderDepthTiles()
12141233
{
12151234
GL_State( GLS_DEPTHTEST_DISABLE );
@@ -1227,6 +1246,11 @@ static void RenderDepthTiles()
12271246
return;
12281247
}
12291248

1249+
if ( glConfig.usingBindlessTextures )
1250+
{
1251+
RB_PrepareForSamplingDepthMap();
1252+
}
1253+
12301254
// 1st step
12311255
R_BindFBO( tr.depthtile1FBO );
12321256
GL_Viewport( 0, 0, tr.depthtile1FBO->width, tr.depthtile1FBO->height );
@@ -1363,6 +1387,8 @@ void RB_RenderGlobalFog()
13631387

13641388
GLIMP_LOGCOMMENT( "--- RB_RenderGlobalFog ---" );
13651389

1390+
RB_PrepareForSamplingDepthMap();
1391+
13661392
GL_Cull( cullType_t::CT_TWO_SIDED );
13671393

13681394
gl_fogGlobalShader->BindProgram( 0 );
@@ -1377,20 +1403,8 @@ void RB_RenderGlobalFog()
13771403

13781404
GL_State( GLS_DEPTHTEST_DISABLE | GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
13791405

1380-
// all fogging distance is based on world Z units
1381-
vec4_t fogDistanceVector;
1382-
vec3_t local;
1383-
VectorSubtract( backEnd.orientation.origin, backEnd.viewParms.orientation.origin, local );
1384-
fogDistanceVector[ 0 ] = -backEnd.orientation.modelViewMatrix[ 2 ];
1385-
fogDistanceVector[ 1 ] = -backEnd.orientation.modelViewMatrix[ 6 ];
1386-
fogDistanceVector[ 2 ] = -backEnd.orientation.modelViewMatrix[ 10 ];
1387-
fogDistanceVector[ 3 ] = DotProduct( local, backEnd.viewParms.orientation.axis[ 0 ] );
1388-
1389-
// scale the fog vectors based on the fog's thickness
1390-
VectorScale( fogDistanceVector, fog->tcScale, fogDistanceVector );
1391-
fogDistanceVector[ 3 ] *= fog->tcScale;
1392-
1393-
gl_fogGlobalShader->SetUniform_FogDistanceVector( fogDistanceVector );
1406+
gl_fogGlobalShader->SetUniform_FogDensity( fog->tcScale );
1407+
gl_fogGlobalShader->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin );
13941408
SetUniform_Color( gl_fogGlobalShader, fog->color );
13951409
}
13961410

@@ -1508,6 +1522,8 @@ void RB_RenderMotionBlur()
15081522

15091523
GLIMP_LOGCOMMENT( "--- RB_RenderMotionBlur ---" );
15101524

1525+
RB_PrepareForSamplingDepthMap();
1526+
15111527
GL_State( GLS_DEPTHTEST_DISABLE );
15121528
GL_Cull( cullType_t::CT_TWO_SIDED );
15131529

@@ -1546,12 +1562,7 @@ void RB_RenderSSAO()
15461562

15471563
GLIMP_LOGCOMMENT( "--- RB_RenderSSAO ---" );
15481564

1549-
// Assume depth is dirty since we just rendered depth pass and everything opaque
1550-
if ( glConfig.textureBarrierAvailable )
1551-
{
1552-
glTextureBarrier();
1553-
backEnd.dirtyDepthBuffer = false;
1554-
}
1565+
RB_PrepareForSamplingDepthMap();
15551566

15561567
GL_State( GLS_DEPTHTEST_DISABLE | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO );
15571568
GL_Cull( cullType_t::CT_TWO_SIDED );
@@ -2718,9 +2729,6 @@ static void RB_RenderView( bool depthPass )
27182729
// draw everything that is translucent
27192730
if ( glConfig.usingMaterialSystem ) {
27202731
materialSystem.RenderMaterials( shaderSort_t::SS_ENVIRONMENT_NOFOG, shaderSort_t::SS_POST_PROCESS, backEnd.viewParms.viewID );
2721-
2722-
// HACK: assume surfaces with depth fade don't use the material system
2723-
backEnd.dirtyDepthBuffer = true;
27242732
}
27252733
RB_RenderDrawSurfaces( shaderSort_t::SS_ENVIRONMENT_NOFOG, shaderSort_t::SS_POST_PROCESS, DRAWSURFACES_ALL );
27262734

src/engine/renderer/tr_bsp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,12 @@ static void R_LoadFogs( lump_t *l, lump_t *brushesLump, lump_t *sidesLump )
33243324
out->fogParms = shader->fogParms;
33253325

33263326
out->color = Color::Adapt( shader->fogParms.color );
3327+
3328+
if ( tr.worldLinearizeTexture )
3329+
{
3330+
out->color = out->color.ConvertFromSRGB();
3331+
}
3332+
33273333
out->color *= tr.identityLight;
33283334

33293335
out->color.SetAlpha( 1 );

0 commit comments

Comments
 (0)