From 2c639aea76226d0971f6995b2a4a5132a8a3de50 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 24 Oct 2022 15:23:09 +0200 Subject: [PATCH] rhi: Add a manual test for simple stencil-based outline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Interesting on its own just because it exercises stencil testing, unlike any of the other existing manual tests. In addition it serves as a base example for how outlines could be done, it is one possible approach at least. (render with stencil write, then render again slightly scaled up with a solid color with testing against the stencil buffer content) Change-Id: I0c845a9004136f229cab037f6f0aab2f772bdd76 Reviewed-by: Christian Strømme --- tests/manual/rhi/CMakeLists.txt | 1 + tests/manual/rhi/shared/examplefw.h | 1 - .../manual/rhi/stenciloutline/CMakeLists.txt | 19 +++ .../rhi/stenciloutline/buildshaders.bat | 2 + tests/manual/rhi/stenciloutline/material.frag | 13 ++ .../rhi/stenciloutline/material.frag.qsb | Bin 0 -> 897 bytes tests/manual/rhi/stenciloutline/material.vert | 13 ++ .../rhi/stenciloutline/material.vert.qsb | Bin 0 -> 978 bytes .../rhi/stenciloutline/stenciloutline.cpp | 154 ++++++++++++++++++ 9 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 tests/manual/rhi/stenciloutline/CMakeLists.txt create mode 100644 tests/manual/rhi/stenciloutline/buildshaders.bat create mode 100644 tests/manual/rhi/stenciloutline/material.frag create mode 100644 tests/manual/rhi/stenciloutline/material.frag.qsb create mode 100644 tests/manual/rhi/stenciloutline/material.vert create mode 100644 tests/manual/rhi/stenciloutline/material.vert.qsb create mode 100644 tests/manual/rhi/stenciloutline/stenciloutline.cpp diff --git a/tests/manual/rhi/CMakeLists.txt b/tests/manual/rhi/CMakeLists.txt index 2abb28ebbd..c160a78aff 100644 --- a/tests/manual/rhi/CMakeLists.txt +++ b/tests/manual/rhi/CMakeLists.txt @@ -29,6 +29,7 @@ add_subdirectory(texturearray) add_subdirectory(polygonmode) add_subdirectory(tessellation) add_subdirectory(geometryshader) +add_subdirectory(stenciloutline) if(QT_FEATURE_widgets) add_subdirectory(rhiwidget) endif() diff --git a/tests/manual/rhi/shared/examplefw.h b/tests/manual/rhi/shared/examplefw.h index 4f49084537..45f46b8e5a 100644 --- a/tests/manual/rhi/shared/examplefw.h +++ b/tests/manual/rhi/shared/examplefw.h @@ -259,7 +259,6 @@ void Window::init() // now onto the backend-independent init m_sc = m_r->newSwapChain(); - // allow depth-stencil, although we do not actually enable depth test/write for the triangle m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(), // no need to set the size here, due to UsedWithSwapChainOnly sampleCount, diff --git a/tests/manual/rhi/stenciloutline/CMakeLists.txt b/tests/manual/rhi/stenciloutline/CMakeLists.txt new file mode 100644 index 0000000000..fbd2631482 --- /dev/null +++ b/tests/manual/rhi/stenciloutline/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_internal_add_manual_test(stenciloutline + GUI + SOURCES + stenciloutline.cpp + LIBRARIES + Qt::Gui + Qt::GuiPrivate +) + +qt_internal_add_resource(stenciloutline "stenciloutline" + PREFIX + "/" + FILES + "material.frag.qsb" + "material.vert.qsb" +) diff --git a/tests/manual/rhi/stenciloutline/buildshaders.bat b/tests/manual/rhi/stenciloutline/buildshaders.bat new file mode 100644 index 0000000000..fc274eeec2 --- /dev/null +++ b/tests/manual/rhi/stenciloutline/buildshaders.bat @@ -0,0 +1,2 @@ +qsb --glsl "100 es,120,150" --hlsl 50 --msl 12 material.vert -o material.vert.qsb +qsb --glsl "100 es,120,150" --hlsl 50 --msl 12 material.frag -o material.frag.qsb diff --git a/tests/manual/rhi/stenciloutline/material.frag b/tests/manual/rhi/stenciloutline/material.frag new file mode 100644 index 0000000000..8fc52e014d --- /dev/null +++ b/tests/manual/rhi/stenciloutline/material.frag @@ -0,0 +1,13 @@ +#version 440 + +layout(location = 0) out vec4 fragColor; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; + vec3 color; +}; + +void main() +{ + fragColor = vec4(color, 1.0); +} diff --git a/tests/manual/rhi/stenciloutline/material.frag.qsb b/tests/manual/rhi/stenciloutline/material.frag.qsb new file mode 100644 index 0000000000000000000000000000000000000000..1c238b51caeb866a1d0113ee46a00b7c2254d44d GIT binary patch literal 897 zcmV-{1AhDf00~rhoXu8SPuxZnK6`;J1`-k)XkMaek*Zy_+8S7@s02bp6xs@@X^411 z$TD7gak92ozHE^YQs4WYm;QwQfc~|3N-GQ2VSuZ{icgbw0+_-CSM!4IRXmpR zg#k{ba!cG6>Q(>kn3d3Ttf$5vpnX@kP>)}Uy(Bip-xJ??*{52ca(BNz{TB6ULV3H1 z!(WJy#?SDRV(&{m%DE$amj(YBeP)@5#-M_&2-c`ztAbI@m#9LAHPP`avQON0k;$Cw z{f!wOa~5dJeIa8gPa1!&P2D*0qfo7zrZI_K$7k!@ADvH>H;(Ld`v&ZX2pUNmXHKfl zGS9d)6pp}72P&9Ow%MAx&ZcsraTISGSLmLFz8A#-`73LEfXFoRhl+T0dy-)^wqC^c z=((NRC+4Zzp(KMYdiOce*J{0Q_As4c)LOcLa{N}dv|;|gXuhk^{Jo^P^j~QOL}h1mA_N5JMxp%jk{-NULlTtu>$)IR^aP1xEK&a z3tK_|iS4|NJTGz6u0zF`61i=%6BDVq4SKp|l*mqfN3vdPQ^Y%Z`NOMYYd?;X#QG^q zCs~>|T?@eqN5|G{J07{Isk5_E73rEH!hchf8mYYDEZ$JmBzKXQ#9*Zj~KXG2##3Eb2kTlkYR_Rn^W5>NbK_zivZ z&v)^{C4M+kp&hsh9*(Ox>b7rz;?Rq`E9YCey%I#+Oyc{oV>&tEpsv|FJ=Kx47r0?1 zRHFk=>hZU#3$&|p&rI?k~U%UDmXJhshTSRGh4WwjHsApkJ;18MJD7FDxw zm=DN(m^7Qr zF;ljvc^#hMm`~X3IC=9nx)SM1maa@XjJ^*~qk96;m&t#Y?2Wu> z#Wgx)RJC*^vb#@lmnrT(#9f8=P4YAH4NJy6OkGyMtE~1biX*Ns!U_w^VO|3Jc`7v4 zV-23?Xb;EFBrg!Bs~|gMzo2k*9ZU>I`Oc`*P@&qiNIp+G#8@PZ5@9T=zUXlo``7P9qwNxEZqMUax4=UM*6p) z!M;9#=bEai#={(TiT@?i?^2C72;*VY)~A;yhe4xID3F}jqKh7v)}Z!n4vR^ zLYL)t<-g5gkfb7x_-)s9oKz;6FL@{HIj6qPFl{WJ@>X<(6psYL?@F0+BW$hPKAW%urrKBmm`Z>onoK%y=Sz~chSEAK>R1mY2-Ks z?q@6?7WeJ%f%`ue_kVNlcfSUA5q$&Fcz(BY&gDZN0DD2AaCdkToQhNiNv-1=ge3mU z3)_Fh3Gek|Na3uBFdo9}@Il-EGw$`$K-PS>t}|0+ca)LJuVB`!>x7{|Z7F<;YA4bv zY*_`Mq8>ea`uwH$Fp1ODdm0TgNn8sKfc)a|tA~e&&mXxObc+9BCMN>eRo&r7FD+^# z(*|g^^gh#zFzeS0ku6iQy6VRyyl(15S{w>-8h8TN24#>#Si6n9Ncwb*$!WzDNbMaf zK^xhc#>d%w)E;##0+}UIA;`!+YWSJ7{tT4fY>LS5XWf9`3j^8id+@W|7o9yP*QX-v z{(_Ij?|X11QW2f4qO4n_8ZnOz{=b zCl1^3@%U>~bL++zI}x1axO!DHvA94-)5w#PE#Mr17^(bj;kki*eMeQ)`uJ;N0^;1| zuSYubc%pUrM?pk|V?s@gb?>lwvnnoqC<-5o@h=RcKbVCMPwX3A(N2y30n|YtJG*)1 A$p8QV literal 0 HcmV?d00001 diff --git a/tests/manual/rhi/stenciloutline/stenciloutline.cpp b/tests/manual/rhi/stenciloutline/stenciloutline.cpp new file mode 100644 index 0000000000..bdf7f217b1 --- /dev/null +++ b/tests/manual/rhi/stenciloutline/stenciloutline.cpp @@ -0,0 +1,154 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "../shared/examplefw.h" +#include "../shared/cube.h" + +static const quint32 UBUF_SIZE = 76; +static const float OUTLINE_SIZE = 1.05f; + +struct { + QList releasePool; + + QRhiBuffer *vbuf; + QRhiBuffer *ubuf; + QRhiShaderResourceBindings *srb; + QRhiGraphicsPipeline *psPass1; + QRhiGraphicsPipeline *psPass2; + float rot = 0.0f; + + QRhiResourceUpdateBatch *initialUpdates = nullptr; +} d; + +void Window::customInit() +{ + d.initialUpdates = m_r->nextResourceUpdateBatch(); + + d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); + d.vbuf->create(); + d.releasePool << d.vbuf; + + d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); + + d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 2 * m_r->ubufAligned(UBUF_SIZE)); + d.ubuf->create(); + d.releasePool << d.ubuf; + + d.srb = m_r->newShaderResourceBindings(); + d.releasePool << d.srb; + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, UBUF_SIZE) + }); + d.srb->create(); + + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 3 * sizeof(float) }, + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float3, 0 }, + }); + + const QRhiShaderStage stages[] = { + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/material.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/material.frag.qsb")) } + }; + + d.psPass1 = m_r->newGraphicsPipeline(); + d.releasePool << d.psPass1; + d.psPass1->setShaderStages(stages, stages + 2); + d.psPass1->setFlags(QRhiGraphicsPipeline::UsesStencilRef); + d.psPass1->setDepthTest(true); + d.psPass1->setDepthWrite(true); + QRhiGraphicsPipeline::StencilOpState stencilPass1 = { QRhiGraphicsPipeline::Keep, + QRhiGraphicsPipeline::Keep, + QRhiGraphicsPipeline::Replace, + QRhiGraphicsPipeline::Always }; + d.psPass1->setStencilFront(stencilPass1); + d.psPass1->setCullMode(QRhiGraphicsPipeline::Back); + //d.psPass1->setStencilBack(stencilPass1); + d.psPass1->setStencilTest(true); + d.psPass1->setVertexInputLayout(inputLayout); + d.psPass1->setShaderResourceBindings(d.srb); + d.psPass1->setRenderPassDescriptor(m_rp); + d.psPass1->create(); + + d.psPass2 = m_r->newGraphicsPipeline(); + d.releasePool << d.psPass2; + d.psPass2->setShaderStages(stages, stages + 2); + d.psPass2->setFlags(QRhiGraphicsPipeline::UsesStencilRef); + d.psPass2->setDepthTest(false); + d.psPass2->setDepthWrite(false); + QRhiGraphicsPipeline::StencilOpState stencilPass2 = { QRhiGraphicsPipeline::Keep, + QRhiGraphicsPipeline::Keep, + QRhiGraphicsPipeline::Replace, + QRhiGraphicsPipeline::NotEqual }; + d.psPass2->setStencilFront(stencilPass2); + d.psPass2->setCullMode(QRhiGraphicsPipeline::Back); + //d.psPass2->setStencilBack(stencilPass2); + d.psPass2->setStencilTest(true); + d.psPass2->setStencilWriteMask(0); + d.psPass2->setVertexInputLayout(inputLayout); + d.psPass2->setShaderResourceBindings(d.srb); + d.psPass2->setRenderPassDescriptor(m_rp); + d.psPass2->create(); +} + +void Window::customRelease() +{ + qDeleteAll(d.releasePool); + d.releasePool.clear(); +} + +void Window::customRender() +{ + const QSize outputSizeInPixels = m_sc->currentPixelSize(); + QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); + QRhiResourceUpdateBatch *u = nullptr; + if (d.initialUpdates) { + u = d.initialUpdates; + d.initialUpdates = nullptr; + } + + char *p = d.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); + QMatrix4x4 mvp = m_proj; + mvp.rotate(d.rot, 1, 1, 0); + mvp.scale(0.5f); + + memcpy(p, mvp.constData(), 64); + const float color[] = { 1.0f, 0.0f, 0.0f }; + memcpy(p + 64, color, 3 * sizeof(float)); + + static const size_t pass2Ofs = m_r->ubufAligned(UBUF_SIZE); + mvp.scale(OUTLINE_SIZE); + memcpy(p + pass2Ofs, mvp.constData(), 64); + const float pass2Color[] = { 0.0f, 0.0f, 1.0f }; + memcpy(p + pass2Ofs + 64, pass2Color, 3 * sizeof(float)); + + d.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); + + const QRhiCommandBuffer::VertexInput vbufBinding[] = { + { d.vbuf, 0 }, + }; + + cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u, QRhiCommandBuffer::DoNotTrackResourcesForCompute); + + cb->setGraphicsPipeline(d.psPass1); + cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); + QRhiCommandBuffer::DynamicOffset pass1DynOffset = { 0, 0 }; + cb->setShaderResources(d.srb, 1, &pass1DynOffset); + cb->setVertexInput(0, 1, vbufBinding); + cb->setStencilRef(1); + cb->draw(36); + + cb->setGraphicsPipeline(d.psPass2); + QRhiCommandBuffer::DynamicOffset pass2DynOffset = { 0, m_r->ubufAligned(UBUF_SIZE) }; + cb->setShaderResources(d.srb, 1, &pass2DynOffset); + cb->setVertexInput(0, 1, vbufBinding); + cb->setStencilRef(1); + cb->draw(36); + + cb->endPass(); + + d.rot += 1; +}