Skip to content

Commit 1e71dae

Browse files
committed
Copy depth buffer if texture barrier unavailable
If the texture barrier GL extension is enabled, make a read-only copy of the depth buffer to use for u_DepthMap sampling, thereby avoiding texture feedback loops in another way. Configurable by setting r_readonlyDepthBuffer 0 (skip the copy even if there is no texture barrier - useful for low-performance systems) or r_readonlyDepthBuffer 2 (always make the copy - useful for testing that code path). Fixes #1783 (broken depth fade on Apple Silicon).
1 parent 6c51397 commit 1e71dae

File tree

8 files changed

+69
-7
lines changed

8 files changed

+69
-7
lines changed

src/engine/renderer/Material.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ void BindShaderGeneric3D( Material* material ) {
904904
gl_genericShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
905905
gl_genericShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
906906

907-
gl_genericShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.currentDepthImage ) );
907+
gl_genericShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.depthSamplerImage ) );
908908

909909
// u_DeformGen
910910
gl_genericShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
@@ -1080,7 +1080,7 @@ void BindShaderLiquid( Material* material ) {
10801080
gl_liquidShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
10811081

10821082
// depth texture
1083-
gl_liquidShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 2, tr.currentDepthImage ) );
1083+
gl_liquidShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 2, tr.depthSamplerImage ) );
10841084

10851085
// bind u_PortalMap
10861086
gl_liquidShaderMaterial->SetUniform_PortalMapBindless( GL_BindToTMU( 1, tr.portalRenderImage ) );

src/engine/renderer/tr_backend.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,11 @@ void RB_RunVisTests( )
12131213

12141214
void RB_PrepareForSamplingDepthMap()
12151215
{
1216+
if ( glConfig.usingReadonlyDepth )
1217+
{
1218+
return;
1219+
}
1220+
12161221
if ( !glConfig.textureBarrierAvailable )
12171222
{
12181223
return;
@@ -1413,7 +1418,7 @@ void RB_RenderGlobalFog()
14131418

14141419
// bind u_DepthMap
14151420
gl_fogGlobalShader->SetUniform_DepthMapBindless(
1416-
GL_BindToTMU( 1, tr.currentDepthImage )
1421+
GL_BindToTMU( 1, tr.depthSamplerImage )
14171422
);
14181423

14191424
Tess_InstantScreenSpaceQuad();
@@ -1535,7 +1540,7 @@ void RB_RenderMotionBlur()
15351540
gl_motionblurShader->SetUniform_blurVec(tr.refdef.blurVec);
15361541

15371542
gl_motionblurShader->SetUniform_DepthMapBindless(
1538-
GL_BindToTMU( 1, tr.currentDepthImage )
1543+
GL_BindToTMU( 1, tr.depthSamplerImage )
15391544
);
15401545

15411546
Tess_InstantScreenSpaceQuad();
@@ -1586,7 +1591,7 @@ void RB_RenderSSAO()
15861591
gl_ssaoShader->SetUniform_UnprojectionParams( unprojectionParams );
15871592

15881593
gl_ssaoShader->SetUniform_DepthMapBindless(
1589-
GL_BindToTMU( 0, tr.currentDepthImage )
1594+
GL_BindToTMU( 0, tr.depthSamplerImage )
15901595
);
15911596

15921597
Tess_InstantScreenSpaceQuad();
@@ -2699,6 +2704,17 @@ static void RB_RenderView( bool depthPass )
26992704
return;
27002705
}
27012706

2707+
if ( glConfig.usingReadonlyDepth )
2708+
{
2709+
FBO_t *currentDrawFBO = glState.currentFBO;
2710+
R_BindFBO( GL_READ_FRAMEBUFFER, currentDrawFBO );
2711+
R_BindFBO( GL_DRAW_FRAMEBUFFER, tr.readonlyDepthFBO );
2712+
int x0 = backEnd.viewParms.viewportX, x1 = x0 + backEnd.viewParms.viewportWidth;
2713+
int y0 = backEnd.viewParms.viewportY, y1 = y0 + backEnd.viewParms.viewportHeight;
2714+
GL_fboShim.glBlitFramebuffer( x0, y0, x1, y1, x0, y0, x1, y1, GL_DEPTH_BUFFER_BIT, GL_NEAREST );
2715+
R_BindFBO( currentDrawFBO );
2716+
}
2717+
27022718
if( tr.refdef.blurVec[0] != 0.0f ||
27032719
tr.refdef.blurVec[1] != 0.0f ||
27042720
tr.refdef.blurVec[2] != 0.0f )

src/engine/renderer/tr_fbo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ void R_InitFBOs()
263263
R_AttachFBOTexturePackedDepthStencil( tr.currentDepthImage->texnum );
264264
R_CheckFBO( tr.mainFBO[1] );
265265

266+
if ( glConfig.usingReadonlyDepth )
267+
{
268+
tr.readonlyDepthFBO = R_CreateFBO( "_depthReadonly", width, height );
269+
R_BindFBO( tr.readonlyDepthFBO );
270+
GL_fboShim.glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tr.depthSamplerImage->texnum, 0 );
271+
glConfig.usingReadonlyDepth = R_CheckFBO( tr.readonlyDepthFBO );
272+
}
273+
266274
if ( glConfig.realtimeLighting )
267275
{
268276
/* It's only required to create frame buffers only used by the

src/engine/renderer/tr_image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,12 @@ static void R_CreateCurrentRenderImage()
24732473

24742474
tr.currentDepthImage = R_CreateImage( "*currentDepth", nullptr, width, height, 1, imageParams );
24752475

2476+
if ( glConfig.usingReadonlyDepth )
2477+
{
2478+
imageParams.bits = IF_NOPICMIP | IF_DEPTH24;
2479+
tr.depthSamplerImage = R_CreateImage( "*readonlyDepth", nullptr, width, height, 1, imageParams );
2480+
}
2481+
24762482
if ( glConfig.usingMaterialSystem ) {
24772483
materialSystem.GenerateDepthImages( width, height, imageParams );
24782484
}

src/engine/renderer/tr_init.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
9292
Cvar::Cvar<bool> r_overbrightIgnoreMapSettings("r_overbrightIgnoreMapSettings", "force usage of r_overbrightDefaultClamp / r_overbrightDefaultExponent, ignoring worldspawn", Cvar::NONE, false);
9393
Cvar::Range<Cvar::Cvar<int>> r_lightMode("r_lightMode", "lighting mode: 0: fullbright (cheat), 1: vertex light, 2: grid light (cheat), 3: light map", Cvar::NONE, Util::ordinal(lightMode_t::MAP), Util::ordinal(lightMode_t::FULLBRIGHT), Util::ordinal(lightMode_t::MAP));
9494
Cvar::Cvar<bool> r_colorGrading( "r_colorGrading", "Use color grading", Cvar::NONE, true );
95+
static Cvar::Range<Cvar::Cvar<int>> r_readonlyDepthBuffer(
96+
"r_readonlyDepthBuffer", "sample depth from a copy of the depth texture: 0 = no (unsafe), 1 = if necessary depending on GL features, 2 = yes",
97+
Cvar::NONE, 1, 0, 2);
9598
Cvar::Cvar<bool> r_preferBindlessTextures( "r_preferBindlessTextures", "use bindless textures even when material system is off", Cvar::NONE, false );
9699
Cvar::Cvar<bool> r_materialSystem( "r_materialSystem", "Use Material System", Cvar::NONE, false );
97100
Cvar::Cvar<bool> r_gpuFrustumCulling( "r_gpuFrustumCulling", "Use frustum culling on the GPU for the Material System", Cvar::NONE, true );
@@ -1193,6 +1196,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
11931196

11941197
Cvar::Latch( r_realtimeLighting );
11951198
Cvar::Latch( r_realtimeLightLayers );
1199+
Cvar::Latch( r_readonlyDepthBuffer );
11961200
Cvar::Latch( r_preferBindlessTextures );
11971201
Cvar::Latch( r_materialSystem );
11981202

@@ -1407,12 +1411,37 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
14071411
backEndData[ 1 ] = nullptr;
14081412
}
14091413

1414+
switch ( r_readonlyDepthBuffer.Get() )
1415+
{
1416+
case 0:
1417+
glConfig.usingReadonlyDepth = false;
1418+
break;
1419+
case 1:
1420+
glConfig.usingReadonlyDepth = !glConfig.textureBarrierAvailable;
1421+
break;
1422+
case 2:
1423+
glConfig.usingReadonlyDepth = true;
1424+
break;
1425+
}
1426+
1427+
if ( glConfig.usingReadonlyDepth && !r_depthShaders.Get() )
1428+
{
1429+
Log::Warn( "Disabling read-only depth buffer because depth pre-pass is disabled" );
1430+
glConfig.usingReadonlyDepth = false;
1431+
}
1432+
14101433
R_ToggleSmpFrame();
14111434

14121435
R_InitImages();
14131436

14141437
R_InitFBOs();
14151438

1439+
// This is here in case creating the depth-only FBO failed.
1440+
if ( !glConfig.usingReadonlyDepth )
1441+
{
1442+
tr.depthSamplerImage = tr.currentDepthImage;
1443+
}
1444+
14161445
R_InitVBOs();
14171446

14181447
R_InitShaders();

src/engine/renderer/tr_local.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,7 @@ enum
24542454
image_t *bloomRenderFBOImage[ 2 ];
24552455
image_t *currentRenderImage[ 2 ];
24562456
image_t *currentDepthImage;
2457+
image_t *depthSamplerImage;
24572458
image_t *depthtile1RenderImage;
24582459
image_t *depthtile2RenderImage;
24592460
image_t *lighttileRenderImage;
@@ -2465,6 +2466,7 @@ enum
24652466

24662467
// framebuffer objects
24672468
FBO_t *mainFBO[ 2 ];
2469+
FBO_t *readonlyDepthFBO;
24682470
FBO_t *depthtile1FBO;
24692471
FBO_t *depthtile2FBO;
24702472
FBO_t *lighttileFBO;

src/engine/renderer/tr_public.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct GLConfig
111111
bool computeShaderAvailable;
112112
bool bindlessTexturesAvailable; // do the driver/hardware support it
113113
bool usingBindlessTextures; // are we using them right now
114+
bool usingReadonlyDepth;
114115
bool shaderDrawParametersAvailable;
115116
bool SSBOAvailable;
116117
bool multiDrawIndirectAvailable;

src/engine/renderer/tr_shade.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ void Render_generic3D( shaderStage_t *pStage )
951951
if ( needDepthMap )
952952
{
953953
gl_genericShader->SetUniform_DepthMapBindless(
954-
GL_BindToTMU( 1, tr.currentDepthImage )
954+
GL_BindToTMU( 1, tr.depthSamplerImage )
955955
);
956956
}
957957

@@ -1550,7 +1550,7 @@ void Render_liquid( shaderStage_t *pStage )
15501550
gl_liquidShader->SetUniform_PortalMapBindless( GL_BindToTMU( 1, tr.portalRenderImage ) );
15511551

15521552
// depth texture
1553-
gl_liquidShader->SetUniform_DepthMapBindless( GL_BindToTMU( 2, tr.currentDepthImage ) );
1553+
gl_liquidShader->SetUniform_DepthMapBindless( GL_BindToTMU( 2, tr.depthSamplerImage ) );
15541554

15551555
// bind u_HeightMap u_depthScale u_reliefOffsetBias
15561556
if ( pStage->enableReliefMapping )

0 commit comments

Comments
 (0)