diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-02-04 13:52:21 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-02-06 13:06:34 +0100 |
commit | 19510abbffc433c2d6435e558047e55aa5800531 (patch) | |
tree | c62ff91c2f42107622dc8cccd725dafc1b3ba681 | |
parent | 7e82d49cc59f9ac71829ff5a8fa6eb6668469bb5 (diff) |
rhi: Add depth bias and slope scaled depth bias
Beware of the API terminology: GL 'factor' = 'slope scaled depth bias',
GL 'units' = '(constant) depth bias'.
Task-number: QTBUG-81843
Change-Id: I03e3618d007cbf7100add0de4950a6163d788cc7
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
-rw-r--r-- | src/gui/rhi/qrhi_p.h | 8 | ||||
-rw-r--r-- | src/gui/rhi/qrhid3d11.cpp | 2 | ||||
-rw-r--r-- | src/gui/rhi/qrhigles2.cpp | 9 | ||||
-rw-r--r-- | src/gui/rhi/qrhimetal.mm | 13 | ||||
-rw-r--r-- | src/gui/rhi/qrhimetal_p_p.h | 1 | ||||
-rw-r--r-- | src/gui/rhi/qrhivulkan.cpp | 5 |
6 files changed, 37 insertions, 1 deletions
diff --git a/src/gui/rhi/qrhi_p.h b/src/gui/rhi/qrhi_p.h index e32fc0f4a0..d17112a241 100644 --- a/src/gui/rhi/qrhi_p.h +++ b/src/gui/rhi/qrhi_p.h @@ -1155,6 +1155,12 @@ public: float lineWidth() const { return m_lineWidth; } void setLineWidth(float width) { m_lineWidth = width; } + int depthBias() const { return m_depthBias; } + void setDepthBias(int bias) { m_depthBias = bias; } + + float slopeScaledDepthBias() const { return m_slopeScaledDepthBias; } + void setSlopeScaledDepthBias(float bias) { m_slopeScaledDepthBias = bias; } + void setShaderStages(std::initializer_list<QRhiShaderStage> list) { m_shaderStages = list; } template<typename InputIterator> void setShaderStages(InputIterator first, InputIterator last) @@ -1193,6 +1199,8 @@ protected: quint32 m_stencilWriteMask = 0xFF; int m_sampleCount = 1; float m_lineWidth = 1.0f; + int m_depthBias = 0; + float m_slopeScaledDepthBias = 0.0f; QVarLengthArray<QRhiShaderStage, 4> m_shaderStages; QRhiVertexInputLayout m_vertexInputLayout; QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr; diff --git a/src/gui/rhi/qrhid3d11.cpp b/src/gui/rhi/qrhid3d11.cpp index ec86c31719..75b90b6995 100644 --- a/src/gui/rhi/qrhid3d11.cpp +++ b/src/gui/rhi/qrhid3d11.cpp @@ -3467,6 +3467,8 @@ bool QD3D11GraphicsPipeline::build() rastDesc.FillMode = D3D11_FILL_SOLID; rastDesc.CullMode = toD3DCullMode(m_cullMode); rastDesc.FrontCounterClockwise = m_frontFace == CCW; + rastDesc.DepthBias = m_depthBias; + rastDesc.SlopeScaledDepthBias = m_slopeScaledDepthBias; rastDesc.DepthClipEnable = true; rastDesc.ScissorEnable = m_flags.testFlag(UsesScissor); rastDesc.MultisampleEnable = rhiD->effectiveSampleCount(m_sampleCount).Count > 1; diff --git a/src/gui/rhi/qrhigles2.cpp b/src/gui/rhi/qrhigles2.cpp index 2620cc4d7b..62f808ce81 100644 --- a/src/gui/rhi/qrhigles2.cpp +++ b/src/gui/rhi/qrhigles2.cpp @@ -2386,7 +2386,14 @@ void QRhiGles2::executeBindGraphicsPipeline(QRhiGraphicsPipeline *ps) f->glDisable(GL_STENCIL_TEST); } - if (psD->topology() == QRhiGraphicsPipeline::Lines || psD->topology() == QRhiGraphicsPipeline::LineStrip) + if (psD->m_depthBias != 0 || !qFuzzyIsNull(psD->m_slopeScaledDepthBias)) { + f->glPolygonOffset(psD->m_slopeScaledDepthBias, psD->m_depthBias); + f->glEnable(GL_POLYGON_OFFSET_FILL); + } else { + f->glDisable(GL_POLYGON_OFFSET_FILL); + } + + if (psD->m_topology == QRhiGraphicsPipeline::Lines || psD->m_topology == QRhiGraphicsPipeline::LineStrip) f->glLineWidth(psD->m_lineWidth); f->glUseProgram(psD->program); diff --git a/src/gui/rhi/qrhimetal.mm b/src/gui/rhi/qrhimetal.mm index 053061bddf..9e8f1ac096 100644 --- a/src/gui/rhi/qrhimetal.mm +++ b/src/gui/rhi/qrhimetal.mm @@ -298,6 +298,8 @@ struct QMetalGraphicsPipelineData MTLPrimitiveType primitiveType; MTLWinding winding; MTLCullMode cullMode; + float depthBias; + float slopeScaledDepthBias; QMetalShader vs; QMetalShader fs; }; @@ -956,6 +958,14 @@ void QRhiMetal::setGraphicsPipeline(QRhiCommandBuffer *cb, QRhiGraphicsPipeline [cbD->d->currentRenderPassEncoder setFrontFacingWinding: psD->d->winding]; cbD->currentFrontFaceWinding = int(psD->d->winding); } + if (!qFuzzyCompare(psD->d->depthBias, cbD->currentDepthBiasValues.first) + || !qFuzzyCompare(psD->d->slopeScaledDepthBias, cbD->currentDepthBiasValues.second)) + { + [cbD->d->currentRenderPassEncoder setDepthBias: psD->d->depthBias + slopeScale: psD->d->slopeScaledDepthBias + clamp: 0.0f]; + cbD->currentDepthBiasValues = { psD->d->depthBias, psD->d->slopeScaledDepthBias }; + } } psD->lastActiveFrameSlot = currentFrameSlot; @@ -3454,6 +3464,8 @@ bool QMetalGraphicsPipeline::build() d->primitiveType = toMetalPrimitiveType(m_topology); d->winding = m_frontFace == CCW ? MTLWindingCounterClockwise : MTLWindingClockwise; d->cullMode = toMetalCullMode(m_cullMode); + d->depthBias = float(m_depthBias); + d->slopeScaledDepthBias = m_slopeScaledDepthBias; lastActiveFrameSlot = -1; generation += 1; @@ -3602,6 +3614,7 @@ void QMetalCommandBuffer::resetPerPassCachedState() currentIndexFormat = QRhiCommandBuffer::IndexUInt16; currentCullMode = -1; currentFrontFaceWinding = -1; + currentDepthBiasValues = { 0.0f, 0.0f }; d->currentFirstVertexBinding = -1; d->currentVertexInputsBuffers.clear(); diff --git a/src/gui/rhi/qrhimetal_p_p.h b/src/gui/rhi/qrhimetal_p_p.h index 24f3340db1..58e93e2cdb 100644 --- a/src/gui/rhi/qrhimetal_p_p.h +++ b/src/gui/rhi/qrhimetal_p_p.h @@ -288,6 +288,7 @@ struct QMetalCommandBuffer : public QRhiCommandBuffer QRhiCommandBuffer::IndexFormat currentIndexFormat; int currentCullMode; int currentFrontFaceWinding; + QPair<float, float> currentDepthBiasValues; const QRhiNativeHandles *nativeHandles(); void resetState(); diff --git a/src/gui/rhi/qrhivulkan.cpp b/src/gui/rhi/qrhivulkan.cpp index cf6f14368c..e41a321637 100644 --- a/src/gui/rhi/qrhivulkan.cpp +++ b/src/gui/rhi/qrhivulkan.cpp @@ -6208,6 +6208,11 @@ bool QVkGraphicsPipeline::build() rastInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rastInfo.cullMode = toVkCullMode(m_cullMode); rastInfo.frontFace = toVkFrontFace(m_frontFace); + if (m_depthBias != 0 || !qFuzzyIsNull(m_slopeScaledDepthBias)) { + rastInfo.depthBiasEnable = true; + rastInfo.depthBiasConstantFactor = float(m_depthBias); + rastInfo.depthBiasSlopeFactor = m_slopeScaledDepthBias; + } rastInfo.lineWidth = rhiD->hasWideLines ? m_lineWidth : 1.0f; pipelineInfo.pRasterizationState = &rastInfo; |