Skip to content

Commit 49064fc

Browse files
committed
Add a workaround for AMD/Mesa driver bug with bindless on depth target
1 parent f89e5c5 commit 49064fc

File tree

8 files changed

+54
-11
lines changed

8 files changed

+54
-11
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ static std::string GenVertexHeader() {
575575
"#define OUT(mode) varying\n";
576576
}
577577

578+
if ( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA ) {
579+
AddDefine( str, "DRIVER_AMD_MESA", 1 );
580+
}
581+
578582
if ( glConfig.shaderDrawParametersAvailable ) {
579583
str += "OUT(flat) int in_drawID;\n";
580584
str += "OUT(flat) int in_baseInstance;\n";
@@ -612,6 +616,10 @@ static std::string GenFragmentHeader() {
612616
"#define DECLARE_OUTPUT(type) /* empty*/\n";
613617
}
614618

619+
if ( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA ) {
620+
AddDefine( str, "DRIVER_AMD_MESA", 1 );
621+
}
622+
615623
if ( glConfig.usingBindlessTextures ) {
616624
str += "layout(bindless_sampler) uniform;\n";
617625
}
@@ -640,6 +648,10 @@ static std::string GenFragmentHeader() {
640648
static std::string GenComputeHeader() {
641649
std::string str;
642650

651+
if ( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA ) {
652+
AddDefine( str, "DRIVER_AMD_MESA", 1 );
653+
}
654+
643655
// Compute shader compatibility defines
644656
if ( glConfig.usingMaterialSystem ) {
645657
AddDefine( str, "MAX_VIEWS", MAX_VIEWS );
@@ -2214,6 +2226,13 @@ std::vector<GLUniform*> GLShaderManager::ProcessUniforms( const GLUniform::Updat
22142226
for ( GLUniform* uniform : uniforms ) {
22152227
if ( uniform->_updateType >= minType && uniform->_updateType <= maxType
22162228
&& ( !uniform->_isTexture || !skipTextures ) ) {
2229+
2230+
if ( uniform->_isTexture && uniform->_AMDSkipBindless
2231+
&& ( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA )
2232+
&& workaround_glDriver_amd_mesa_skipBindlessDepthTarget.Get() ){
2233+
continue;
2234+
}
2235+
22172236
tmp.emplace_back( uniform );
22182237
}
22192238
}

src/engine/renderer/gl_shader.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class GLUniform {
347347
const UpdateType _updateType;
348348
const int _components;
349349
const bool _isTexture;
350+
const bool _AMDSkipBindless;
350351

351352
protected:
352353
GLShader* _shader;
@@ -355,7 +356,7 @@ class GLUniform {
355356

356357
GLUniform( GLShader* shader, const char* name, const char* type, const GLuint std430Size, const GLuint std430Alignment,
357358
const UpdateType updateType, const int components = 0,
358-
const bool isTexture = false ) :
359+
const bool isTexture = false, const bool AMDSkipBindless = false ) :
359360
_name( name ),
360361
_type( type ),
361362
_std430BaseSize( std430Size ),
@@ -364,6 +365,7 @@ class GLUniform {
364365
_updateType( updateType ),
365366
_components( components ),
366367
_isTexture( isTexture ),
368+
_AMDSkipBindless( AMDSkipBindless ),
367369
_shader( shader ) {
368370
_shader->RegisterUniform( this );
369371
}
@@ -522,9 +524,9 @@ class GLShaderManager {
522524

523525
class GLUniformSampler : protected GLUniform {
524526
protected:
525-
GLUniformSampler( GLShader* shader, const char* name, const char* type, const UpdateType updateType ) :
527+
GLUniformSampler( GLShader* shader, const char* name, const char* type, const UpdateType updateType, const bool AMDSkipBindless = false ) :
526528
GLUniform( shader, name, type, glConfig.bindlessTexturesAvailable ? 2 : 1,
527-
glConfig.bindlessTexturesAvailable ? 2 : 1, updateType, 0, true ) {
529+
glConfig.bindlessTexturesAvailable ? 2 : 1, updateType, 0, true, AMDSkipBindless ) {
528530
}
529531

530532
inline GLint GetLocation() {
@@ -550,6 +552,12 @@ class GLUniformSampler : protected GLUniform {
550552
currentValueBindless = value;
551553

552554
if ( glConfig.usingBindlessTextures ) {
555+
if ( _AMDSkipBindless
556+
&& ( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA )
557+
&& workaround_glDriver_amd_mesa_skipBindlessDepthTarget.Get() ) {
558+
return;
559+
}
560+
553561
if ( _shader->UseMaterialSystem() && _updateType == TEXDATA_OR_PUSH ) {
554562
return;
555563
}
@@ -579,8 +587,8 @@ class GLUniformSampler : protected GLUniform {
579587

580588
class GLUniformSampler2D : protected GLUniformSampler {
581589
protected:
582-
GLUniformSampler2D( GLShader* shader, const char* name, const UpdateType updateType ) :
583-
GLUniformSampler( shader, name, "sampler2D", updateType ) {
590+
GLUniformSampler2D( GLShader* shader, const char* name, const UpdateType updateType, const bool AMDSkipBindless = false ) :
591+
GLUniformSampler( shader, name, "sampler2D", updateType, AMDSkipBindless ) {
584592
}
585593
};
586594

@@ -1743,7 +1751,7 @@ class u_DepthMap :
17431751
GLUniformSampler2D {
17441752
public:
17451753
u_DepthMap( GLShader* shader ) :
1746-
GLUniformSampler2D( shader, "u_DepthMap", CONST ) {
1754+
GLUniformSampler2D( shader, "u_DepthMap", CONST, true ) {
17471755
}
17481756

17491757
void SetUniform_DepthMapBindless( GLuint64 bindlessHandle ) {

src/engine/renderer/glsl_source/depthReduction_cp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4141
// Keep this to 8x8 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
4242
layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
4343

44-
layout(binding = 0) uniform sampler2D u_DepthMap;
44+
uniform sampler2D u_DepthMap;
4545
layout(r32f, binding = 1) uniform readonly image2D depthImageIn;
4646
layout(r32f, binding = 2) uniform writeonly image2D depthImageOut;
4747

src/engine/renderer/glsl_source/material_fp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6060
sampler2D u_PortalMap = sampler2D( u_PortalMap_initial );
6161
#endif // !LIQUID_GLSL
6262

63-
#if defined(DEPTHMAP_GLSL)
63+
#if defined(DEPTHMAP_GLSL) && !defined(DRIVER_AMD_MESA)
6464
sampler2D u_DepthMap = sampler2D( u_DepthMap_initial );
6565
#endif // !DEPTHMAP_GLSL
6666

src/engine/renderer/tr_backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ GLuint64 GL_BindToTMU( int unit, image_t *image )
178178
image = tr.defaultImage;
179179
}
180180

181-
if ( glConfig.usingBindlessTextures ) {
181+
if ( glConfig.usingBindlessTextures && !( image->bits & IF_AMD_SKIP_BINDLESS ) ) {
182182
return tr.textureManager.BindTexture( 0, image->texture );
183183
}
184184

src/engine/renderer/tr_image.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,15 @@ image_t *R_CreateImage( const char *name, const byte **pic, int width, int heigh
14711471
image->height = height;
14721472

14731473
image->bits = imageParams.bits;
1474+
1475+
const bool skipAMDBindless =
1476+
( glConfig.driverVendor == glDriverVendor_t::ATI || glConfig.driverVendor == glDriverVendor_t::MESA )
1477+
&& workaround_glDriver_amd_mesa_skipBindlessDepthTarget.Get();
1478+
1479+
if ( !skipAMDBindless ) {
1480+
image->bits &= ~IF_AMD_SKIP_BINDLESS;
1481+
}
1482+
14741483
image->filterType = imageParams.filterType;
14751484
image->wrapType = imageParams.wrapType;
14761485

@@ -2556,7 +2565,7 @@ static void R_CreateCurrentRenderImage()
25562565
tr.currentRenderImage[1] = R_CreateImage( "_currentRender[1]", nullptr, width, height, 1, imageParams );
25572566

25582567
imageParams = {};
2559-
imageParams.bits = IF_NOPICMIP | IF_PACKED_DEPTH24_STENCIL8;
2568+
imageParams.bits = IF_NOPICMIP | IF_PACKED_DEPTH24_STENCIL8 | IF_AMD_SKIP_BINDLESS;
25602569
imageParams.filterType = filterType_t::FT_NEAREST;
25612570
imageParams.wrapType = wrapTypeEnum_t::WT_CLAMP;
25622571

src/engine/renderer/tr_local.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ enum class ssaoMode {
483483
IF_BC4 = BIT( 22 ),
484484
IF_BC5 = BIT( 23 ),
485485
IF_RGBA32UI = BIT( 24 ),
486-
IF_HOMEPATH = BIT( 25 )
486+
IF_HOMEPATH = BIT( 25 ),
487+
IF_AMD_SKIP_BINDLESS = BIT( 26 )
487488
};
488489

489490
enum class filterType_t
@@ -2773,6 +2774,8 @@ enum
27732774

27742775
extern cvar_t *r_evsmPostProcess;
27752776

2777+
extern Cvar::Cvar<bool> workaround_glDriver_amd_mesa_skipBindlessDepthTarget;
2778+
27762779
//====================================================================
27772780

27782781
/* r_checkGLErrors values:

src/engine/sys/sdl_glimp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ static Cvar::Cvar<bool> workaround_glDriver_amd_oglp_disableBindlessTexture(
135135
"workaround.glDriver.amd.oglp.disableBindlessTexture",
136136
"Disable ARB_bindless_texture on AMD OGLP driver",
137137
Cvar::NONE, true );
138+
Cvar::Cvar<bool> workaround_glDriver_amd_mesa_skipBindlessDepthTarget(
139+
"workaround.glDriver.amd.mesa.skipBindlessDepthTarget",
140+
"Disable bindless depth target on AMD and Mesa drivers",
141+
Cvar::NONE, true );
138142
static Cvar::Cvar<bool> workaround_glDriver_mesa_ati_rv300_useFloatVertex(
139143
"workaround.glDriver.mesa.ati.rv300.useFloatVertex",
140144
"Use float vertex instead of supported-but-slower half-float vertex on Mesa driver on ATI RV300 hardware",

0 commit comments

Comments
 (0)