Skip to content

Commit 118d826

Browse files
committed
tr_backend: fix FXAA sampling by using GL_LINEAR on currentRender
- fix FXAA by using GL_LINEAR on currentRender, - restore GL_NEAREST after that to not break other effects.
1 parent c519f51 commit 118d826

6 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/engine/renderer/GLUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ struct GLConfig
140140
bool bufferStorageAvailable;
141141
bool uniformBufferObjectAvailable;
142142
bool mapBufferRangeAvailable;
143+
bool samplerObjectsAvailable;
143144
bool syncAvailable;
144145
bool textureBarrierAvailable;
145146
bool halfFloatVertexAvailable;

src/engine/renderer/glsl_source/fxaa_fp.glsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4646

4747
#insert fxaa3_11_fp
4848

49+
#if defined(HAVE_ARB_bindless_texture)
50+
uniform sampler2D u_ColorMap_linear;
51+
#define u_ColorMap u_ColorMap_linear
52+
#else
4953
uniform sampler2D u_ColorMap;
54+
#endif
5055

5156
#if __VERSION__ > 120
5257
out vec4 outputColor;

src/engine/renderer/tr_backend.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ void RB_RenderSSAO()
16031603

16041604
void RB_FXAA()
16051605
{
1606-
if ( !r_FXAA.Get() || !gl_fxaaShader )
1606+
if ( !r_FXAA.Get() || !gl_fxaaShader || !glConfig.samplerObjectsAvailable )
16071607
{
16081608
return;
16091609
}
@@ -1625,11 +1625,42 @@ void RB_FXAA()
16251625
GL_BindToTMU( 0, tr.currentRenderImage[backEnd.currentMainFBO] )
16261626
);
16271627

1628+
// FXAA expects GL_LINEAR for the sampling to work.
1629+
GLuint64 handle = 0;
1630+
1631+
if ( glConfig.usingBindlessTextures )
1632+
{
1633+
// Set a handler.
1634+
GLuint texture = tr.currentRenderImage[backEnd.currentMainFBO]->texnum;
1635+
handle = glGetTextureSamplerHandleARB( texture, tr.linearSampler );
1636+
glMakeTextureHandleResidentARB( handle );
1637+
GLuint program = gl_fxaaShader->GetProgram()->id;
1638+
GLint location = glGetUniformLocation( program, "u_ColorMap_linear" );
1639+
glUniformHandleui64ARB( location, handle );
1640+
}
1641+
else
1642+
{
1643+
// Bind a sampler.
1644+
glBindSampler( 0, tr.linearSampler );
1645+
}
1646+
16281647
// This shader is run last, so let it render to screen.
16291648
R_BindNullFBO();
16301649

16311650
Tess_InstantScreenSpaceQuad();
16321651

1652+
// Make sure we didn't break other effects expecting GL_NEAREST.
1653+
if ( glConfig.usingBindlessTextures )
1654+
{
1655+
// Unset the handler.
1656+
glMakeTextureHandleNonResidentARB( handle );
1657+
}
1658+
else
1659+
{
1660+
// Unbind the sampler.
1661+
glBindSampler( 0, 0 );
1662+
}
1663+
16331664
GL_CheckErrors();
16341665
}
16351666

@@ -1697,7 +1728,7 @@ void RB_CameraPostFX() {
16971728
GL_BindToTMU( 0, tr.currentRenderImage[backEnd.currentMainFBO] )
16981729
);
16991730

1700-
if ( r_FXAA.Get() && gl_fxaaShader )
1731+
if ( r_FXAA.Get() && gl_fxaaShader && glConfig.samplerObjectsAvailable )
17011732
{
17021733
// Swap main FBOs.
17031734
backEnd.currentMainFBO = 1 - backEnd.currentMainFBO;
@@ -3848,6 +3879,12 @@ void R_ShutdownBackend()
38483879
glDisableVertexAttribArray( i );
38493880
}
38503881
glState.vertexAttribsState = 0;
3882+
3883+
if ( tr.linearSampler )
3884+
{
3885+
glDeleteSamplers( 1, &tr.linearSampler );
3886+
tr.linearSampler = 0;
3887+
}
38513888
}
38523889

38533890
const RenderCommand *EndOfListCommand::ExecuteSelf( ) const

src/engine/renderer/tr_local.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,8 @@ enum
25952595
float inverseSawToothTable[ FUNCTABLE_SIZE ];
25962596

25972597
scissorState_t scissor;
2598+
2599+
GLuint linearSampler;
25982600
};
25992601

26002602
extern const matrix_t quakeToOpenGLMatrix;

src/engine/renderer/tr_shade.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ static void GLSL_InitGPUShadersOrError()
222222

223223
GL_CheckErrors();
224224

225+
bool requireLinearSampler = false;
226+
225227
gl_shaderManager.InitDriverInfo();
226228

227229
/* It must be done before GenerateBuiltinHeaders() because glConfig.realtimeLighting
@@ -374,6 +376,15 @@ static void GLSL_InitGPUShadersOrError()
374376
gl_shaderManager.LoadShader( gl_fxaaShader );
375377

376378
gl_fxaaShader->MarkProgramForBuilding();
379+
380+
requireLinearSampler = true;
381+
}
382+
383+
if ( requireLinearSampler && !tr.linearSampler )
384+
{
385+
glGenSamplers( 1, &tr.linearSampler );
386+
glSamplerParameteri( tr.linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ;
387+
glSamplerParameteri( tr.linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
377388
}
378389

379390
gl_shaderManager.PostProcessGlobalUniforms();

src/engine/sys/sdl_glimp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ static Cvar::Cvar<bool> r_arb_multi_draw_indirect( "r_arb_multi_draw_indirect",
9090
"Use GL_ARB_multi_draw_indirect if available", Cvar::NONE, true );
9191
static Cvar::Cvar<bool> r_arb_program_interface_query( "r_arb_program_interface_query",
9292
"Load GL_ARB_program_interface_query if available", Cvar::NONE, true );
93+
static Cvar::Cvar<bool> r_arb_sampler_objects( "r_arb_sampler_objects",
94+
"Use GL_ARB_sampler_objects if available", Cvar::NONE, true );
9395
static Cvar::Cvar<bool> r_arb_shader_draw_parameters( "r_arb_shader_draw_parameters",
9496
"Use GL_ARB_shader_draw_parameters if available", Cvar::NONE, true );
9597
static Cvar::Cvar<bool> r_arb_shader_atomic_counters( "r_arb_shader_atomic_counters",
@@ -2018,6 +2020,7 @@ static void GLimp_InitExtensions()
20182020
Cvar::Latch( r_arb_internalformat_query2 );
20192021
Cvar::Latch( r_arb_map_buffer_range );
20202022
Cvar::Latch( r_arb_multi_draw_indirect );
2023+
Cvar::Latch( r_arb_sampler_objects );
20212024
Cvar::Latch( r_arb_shader_atomic_counters );
20222025
Cvar::Latch( r_arb_shader_atomic_counter_ops );
20232026
Cvar::Latch( r_arb_shader_draw_parameters );
@@ -2374,6 +2377,9 @@ static void GLimp_InitExtensions()
23742377
// made required in OpenGL 3.0
23752378
glConfig.mapBufferRangeAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, ARB_map_buffer_range, r_arb_map_buffer_range.Get() );
23762379

2380+
// made required in OpenGL 3.3
2381+
glConfig.samplerObjectsAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, ARB_sampler_objects, r_arb_sampler_objects.Get() );
2382+
23772383
// made required in OpenGL 3.2
23782384
glConfig.syncAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_CORE, ARB_sync, r_arb_sync.Get() );
23792385

0 commit comments

Comments
 (0)