From 680b0215fcd8781e894079ffb4140da04dffe326 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sat, 26 Feb 2022 21:49:32 +0000 Subject: [PATCH] Adds depth bias to PipelineState and its management to CDeviceCommandContext. Refs #2368 This was SVN commit r26495. --- source/renderer/TerrainOverlay.cpp | 17 ++++++----------- source/renderer/backend/PipelineState.cpp | 3 +++ source/renderer/backend/PipelineState.h | 3 +++ .../backend/gl/DeviceCommandContext.cpp | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/source/renderer/TerrainOverlay.cpp b/source/renderer/TerrainOverlay.cpp index 75d05a4bad..5bd634970e 100644 --- a/source/renderer/TerrainOverlay.cpp +++ b/source/renderer/TerrainOverlay.cpp @@ -124,14 +124,6 @@ void TerrainOverlay::RenderBeforeWater( UNUSED2(deviceCommandContext); #warning TODO: implement TerrainOverlay::RenderOverlays for GLES #else - // To ensure that outlines are drawn on top of the terrain correctly (and - // don't Z-fight and flicker nastily), draw them as QUADS with the LINE - // PolygonMode, and use PolygonOffset to pull them towards the camera. - // (See e.g. http://www.opengl.org/resources/faq/technical/polygonoffset.htm) - glPolygonOffset(-1.f, -1.f); - //glEnable(GL_POLYGON_OFFSET_LINE); - glEnable(GL_POLYGON_OFFSET_FILL); - StartRender(); ssize_t min_i, min_j, max_i, max_j; @@ -149,9 +141,6 @@ void TerrainOverlay::RenderBeforeWater( ProcessTile(deviceCommandContext, m_i, m_j); EndRender(); - - //glDisable(GL_POLYGON_OFFSET_LINE); - glDisable(GL_POLYGON_OFFSET_FILL); #endif } @@ -226,6 +215,12 @@ void TerrainOverlay::RenderTile( Renderer::Backend::BlendOp::ADD; pipelineStateDesc.rasterizationState.cullMode = drawHidden ? Renderer::Backend::CullMode::NONE : Renderer::Backend::CullMode::BACK; + // To ensure that outlines are drawn on top of the terrain correctly (and + // don't Z-fight and flicker nastily), use detph bias to pull them towards + // the camera. + pipelineStateDesc.rasterizationState.depthBiasEnabled = true; + pipelineStateDesc.rasterizationState.depthBiasConstantFactor = -1.0f; + pipelineStateDesc.rasterizationState.depthBiasSlopeFactor = -1.0f; overlayTech->BeginPass(); deviceCommandContext->SetGraphicsPipelineState(pipelineStateDesc); diff --git a/source/renderer/backend/PipelineState.cpp b/source/renderer/backend/PipelineState.cpp index b1cc0d8411..162dc357b2 100644 --- a/source/renderer/backend/PipelineState.cpp +++ b/source/renderer/backend/PipelineState.cpp @@ -57,6 +57,9 @@ GraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc() desc.rasterizationState.polygonMode = PolygonMode::FILL; desc.rasterizationState.cullMode = CullMode::BACK; desc.rasterizationState.frontFace = FrontFace::COUNTER_CLOCKWISE; + desc.rasterizationState.depthBiasEnabled = false; + desc.rasterizationState.depthBiasConstantFactor = 0.0f; + desc.rasterizationState.depthBiasSlopeFactor = 0.0f; return desc; } diff --git a/source/renderer/backend/PipelineState.h b/source/renderer/backend/PipelineState.h index d14dc48430..6a4e08cb1a 100644 --- a/source/renderer/backend/PipelineState.h +++ b/source/renderer/backend/PipelineState.h @@ -154,6 +154,9 @@ struct RasterizationStateDesc PolygonMode polygonMode; CullMode cullMode; FrontFace frontFace; + bool depthBiasEnabled; + float depthBiasConstantFactor; + float depthBiasSlopeFactor; }; // TODO: Add a shader program to the graphics pipeline state. diff --git a/source/renderer/backend/gl/DeviceCommandContext.cpp b/source/renderer/backend/gl/DeviceCommandContext.cpp index 1d0407ba6b..a5f4291f33 100644 --- a/source/renderer/backend/gl/DeviceCommandContext.cpp +++ b/source/renderer/backend/gl/DeviceCommandContext.cpp @@ -590,6 +590,25 @@ void CDeviceCommandContext::SetGraphicsPipelineStateImpl( glFrontFace(GL_CCW); } +#if !CONFIG2_GLES + if (force || + currentRasterizationStateDesc.depthBiasEnabled != nextRasterizationStateDesc.depthBiasEnabled) + { + if (nextRasterizationStateDesc.depthBiasEnabled) + glEnable(GL_POLYGON_OFFSET_FILL); + else + glDisable(GL_POLYGON_OFFSET_FILL); + } + if (force || + currentRasterizationStateDesc.depthBiasConstantFactor != nextRasterizationStateDesc.depthBiasConstantFactor || + currentRasterizationStateDesc.depthBiasSlopeFactor != nextRasterizationStateDesc.depthBiasSlopeFactor) + { + glPolygonOffset( + nextRasterizationStateDesc.depthBiasSlopeFactor, + nextRasterizationStateDesc.depthBiasConstantFactor); + } +#endif + m_GraphicsPipelineStateDesc = pipelineStateDesc; }