From 9452c963c8ab732946f835e5b5a0510c6fed49ee Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Fri, 28 Jun 2019 10:56:04 +0200 Subject: [PATCH] rhi: Add a test for instancing Draws 1024 cubes at random x,y positions with varying color with a single instanced draw call. Change-Id: I8737503acf23866ad4734b1d88753415a3b93445 Reviewed-by: Lars Knoll --- tests/manual/rhi/instancing/buildshaders.bat | 2 + tests/manual/rhi/instancing/inst.frag | 9 + tests/manual/rhi/instancing/inst.frag.qsb | Bin 0 -> 936 bytes tests/manual/rhi/instancing/inst.vert | 25 +++ tests/manual/rhi/instancing/inst.vert.qsb | Bin 0 -> 1792 bytes tests/manual/rhi/instancing/instancing.cpp | 175 +++++++++++++++++++ tests/manual/rhi/instancing/instancing.pro | 8 + tests/manual/rhi/instancing/instancing.qrc | 6 + tests/manual/rhi/rhi.pro | 3 +- 9 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tests/manual/rhi/instancing/buildshaders.bat create mode 100644 tests/manual/rhi/instancing/inst.frag create mode 100644 tests/manual/rhi/instancing/inst.frag.qsb create mode 100644 tests/manual/rhi/instancing/inst.vert create mode 100644 tests/manual/rhi/instancing/inst.vert.qsb create mode 100644 tests/manual/rhi/instancing/instancing.cpp create mode 100644 tests/manual/rhi/instancing/instancing.pro create mode 100644 tests/manual/rhi/instancing/instancing.qrc diff --git a/tests/manual/rhi/instancing/buildshaders.bat b/tests/manual/rhi/instancing/buildshaders.bat new file mode 100644 index 0000000000..9053a1c54a --- /dev/null +++ b/tests/manual/rhi/instancing/buildshaders.bat @@ -0,0 +1,2 @@ +qsb --glsl "330,300 es" --hlsl 50 --msl 12 inst.vert -o inst.vert.qsb +qsb --glsl "330,300 es" --hlsl 50 --msl 12 inst.frag -o inst.frag.qsb diff --git a/tests/manual/rhi/instancing/inst.frag b/tests/manual/rhi/instancing/inst.frag new file mode 100644 index 0000000000..719fd79ff5 --- /dev/null +++ b/tests/manual/rhi/instancing/inst.frag @@ -0,0 +1,9 @@ +#version 440 + +layout(location = 0) in vec3 vColor; +layout(location = 0) out vec4 fragColor; + +void main() +{ + fragColor = vec4(vColor, 1.0); +} diff --git a/tests/manual/rhi/instancing/inst.frag.qsb b/tests/manual/rhi/instancing/inst.frag.qsb new file mode 100644 index 0000000000000000000000000000000000000000..f19a2f0a7130c49a350e927e2242778199b2b9af GIT binary patch literal 936 zcmV;Z16TY200_@`oW)gJPa8)R9%BRXkfh|IRti!zk@H|w( zxhV{+y`IDvgY_tk;=e8-mcH*xoCnfhWb887-huHD#%IiVow2Hk7w)PLk~s|ht<6Q( zG58$=xntmR^A>W8vJ^?fUP!!={~yGA`#X78Sv&SRhB}+8p5eYU?$i;Zx}y-XT|m&%X5IjM#5RJ zt5B)R@Wb-`T|zx8BleTVao{^ zKY{%;wP3|y6*w_7wum=rT_p_dIoX%VeumD>$$o~;iP$f&o-*3tb@b=~oM3`-F^cgo zAlOT*6#EMK{|@Ms(b8h9fXxzCDqzP4~&pZ*J1{0;M}{=;==8b)>s=&*^*q8(x#|QmKvCH%!H{ zh~?W%#S)x!pxS3s$i5eQsWbs(>_&&LrJp9}WjNn*Ji-6>oK>i_xwfsE(tel)Qmpi3 z>V+;mPN6zn<3l`Az2H~;YjoE*&l1p3;Oi>^@uV5nylF-QWiKTo%OX}XlqnNMk_q5MWLvp0Y05R KKVJcZKlC&I1lUjj literal 0 HcmV?d00001 diff --git a/tests/manual/rhi/instancing/inst.vert b/tests/manual/rhi/instancing/inst.vert new file mode 100644 index 0000000000..3e1b117d58 --- /dev/null +++ b/tests/manual/rhi/instancing/inst.vert @@ -0,0 +1,25 @@ +#version 440 + +layout(location = 0) in vec4 position; + +// Instanced attributes to variate the translation and color of the cube +layout(location = 1) in vec3 instTranslate; +layout(location = 2) in vec3 instColor; + +layout(location = 0) out vec3 vColor; + +out gl_PerVertex { vec4 gl_Position; }; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; +} ubuf; + +void main() +{ + vColor = instColor; + mat4 t = mat4(1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + instTranslate.x, instTranslate.y, instTranslate.z, 1); + gl_Position = ubuf.mvp * t * position; +} diff --git a/tests/manual/rhi/instancing/inst.vert.qsb b/tests/manual/rhi/instancing/inst.vert.qsb new file mode 100644 index 0000000000000000000000000000000000000000..de05eae80fa14a16a1cd23080a0727d931d7e99d GIT binary patch literal 1792 zcmV+b2mkm002LH?ob4D*Zxcs!{F!7SkPto!Nz;L(=^9*PJAtBv&_Y^Ks;ERHLZ~US z*7iCq*z2&nb^|n0h19BtUW&N!NBRTW|ImA{J@m+>Qs0|-G(GsoTT1wK#V9B}a6fbS5D&tZH8W0H1cBgK8=+jT+(~mH|g>i`dJd;MdI#%F0JL~Xjg1xV_oM%|4zYToHhS9ff ztL58)TKkOf(Z`Up$-}z8ppWv!sm8EgH^}c7Q>a%{6xVf@F@yPm*J}jmRF?IGQ+ob? zhw&$j*Q66^7`q)9f630uk?Ll+O77Q8dS(b)ZwNB^5n;z6GAR+0y!hT+U0*HvL9?`Q z2}_9`0c-I=+EK6!pDc`y<%lX6p?FTfn1fMe=wC*}`xmTJ;MVyOMj2Tt2})XZO!)X+FoGqp#D7J8PQZzq0TuK37A9Mh-%4$Jq5yJw$Z)| zFqcu6_MuNH>SF0B3TV+9{0`<4C)(tiTr<77J{`QrjF zAJl)6c=HEcPSO-pM)=o&$&oC^c#Pt_OgTPGdAbJNBLrUu ziIXC4GR7x}KQD48@rvgR@toFpCJC>2-X)$Xjc1zhYhVT1{vPG~m^KuR6FiI*h_(m%}f;k<)AiqFrNjTxua zv|Ie6A?*z6hq~5^F7rAFY?m3hfKk~#M-V90Zj!(MDWGdMhWxN_L1myF#d!=UQ}(m8I} zfP$M0#~TNi2h5Vuww?gaR5usC%&P_NxRPmb>Ll@1qI`qXCyBSLco(=jDZrS1*J*Oh zmRTVCrBxuHtvv8606b1i)MHjk9*U<$ahFC{`&l!ynVonq9b(vvvisZdcU zNb|Il6l^px)+NXSKhJ{#yh4|E4MVHYENv#LLjRa?+(x_CwE5MJ9a!xeTr}EF{hATt zeW%mBh_BLvYmi_cDv{%D%!d-RxsX~)sEby+3xV6wYJ*tNl5!2Qr8BDP;G3I0-}W$( zZn<#({*tCE;`8RFauJ#KsCV3m8H0F$oE5;uvfc}9Gqymis3NSDBvxf{$W|>VYx?5; z6FMtrrm_xeb92)Vu$vulp~xFu&}g|Lgma>IfC1huhz1&381$Rch+~;Q+m&_!RMKj> z6f5UJI#5Odrko@fHqjDGTc^c~n`mT0tinqPMP53n&e5$m)H-Mg%N=DrJLq{XimLL@ ziKsDoh*!h<&sG{T#A3t3-EPbA1BiINtm5r;zo~VshY;352+@EV8nDQ58Nb?0tv#q0Wh?H2xY-=`%O`Nn7W zR&L+E^J)2jZV0(5`!qtxQH^w@ZpUr$vo^8wlg)^B{UTq#t3`8H?daHTwYS>V-_!>! zQO1#Xq~2SiIuE4Bx%8g+H(P!ydC%|k+NR23Q9wa)P?{(LrN1}y^6=YJ8eDBfFjpZA zGrO#l<2y^VO!l&B6^rcKp$9r=w1c&LN<@MqQd<5`BAIkdY}1G|;U3;WvGg^+Xcvht ivunhzdqp<*>n!zKc- + +// Instanced draw example. When running with OpenGL, at least 3.3 or ES 3.0 is +// needed. + +const int INSTANCE_COUNT = 1024; + +struct { + QVector releasePool; + + QRhiBuffer *vbuf = nullptr; + QRhiBuffer *instBuf = nullptr; + QRhiBuffer *ubuf = nullptr; + QRhiShaderResourceBindings *srb = nullptr; + QRhiGraphicsPipeline *ps = nullptr; + + QRhiResourceUpdateBatch *initialUpdates = nullptr; + QMatrix4x4 winProj; +} d; + +void Window::customInit() +{ + if (!m_r->isFeatureSupported(QRhi::Instancing)) + qFatal("Instanced drawing is not supported"); + + d.initialUpdates = m_r->nextResourceUpdateBatch(); + + d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); + d.vbuf->build(); + d.releasePool << d.vbuf; + + d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); + + // translation + color (vec3 + vec3), interleaved, for each instance + d.instBuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, INSTANCE_COUNT * 6 * sizeof(float)); + d.instBuf->build(); + d.releasePool << d.instBuf; + + d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 12); + d.ubuf->build(); + d.releasePool << d.ubuf; + + d.srb = m_r->newShaderResourceBindings(); + d.releasePool << d.srb; + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, d.ubuf) + }); + d.srb->build(); + + d.ps = m_r->newGraphicsPipeline(); + d.releasePool << d.ps; + d.ps->setShaderStages({ + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/inst.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/inst.frag.qsb")) } + }); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 3 * sizeof(float) }, // cube vertices + { 6 * sizeof(float), QRhiVertexInputBinding::PerInstance } // per-instance translation and color + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float3, 0 }, // position + { 1, 1, QRhiVertexInputAttribute::Float3, 0 }, // instTranslate + { 1, 2, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) } // instColor + }); + d.ps->setVertexInputLayout(inputLayout); + d.ps->setShaderResourceBindings(d.srb); + d.ps->setRenderPassDescriptor(m_rp); + d.ps->build(); + + QByteArray instData; + instData.resize(INSTANCE_COUNT * 6 * sizeof(float)); + float *p = reinterpret_cast(instData.data()); + QRandomGenerator *rgen = QRandomGenerator::global(); + for (int i = 0; i < INSTANCE_COUNT; ++i) { + // translation + *p++ = rgen->bounded(8000) / 100.0f - 40.0f; + *p++ = rgen->bounded(8000) / 100.0f - 40.0f; + *p++ = 0.0f; + // color + *p++ = i / float(INSTANCE_COUNT); + *p++ = 0.0f; + *p++ = 0.0f; + } + d.initialUpdates->uploadStaticBuffer(d.instBuf, instData.constData()); +} + +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 = m_r->nextResourceUpdateBatch(); + if (d.initialUpdates) { + u->merge(d.initialUpdates); + d.initialUpdates->release(); + d.initialUpdates = nullptr; + } + + if (d.winProj != m_proj) { + d.winProj = m_proj; + QMatrix4x4 mvp = m_proj; + mvp.scale(0.05f); + u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); + } + + cb->beginPass(m_sc->currentFrameRenderTarget(), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f), { 1.0f, 0 }, u); + cb->setGraphicsPipeline(d.ps); + cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBinding[] = { + { d.vbuf, 0 }, + { d.instBuf, 0 } + }; + cb->setVertexInput(0, 2, vbufBinding); + cb->draw(36, INSTANCE_COUNT); + cb->endPass(); +} diff --git a/tests/manual/rhi/instancing/instancing.pro b/tests/manual/rhi/instancing/instancing.pro new file mode 100644 index 0000000000..416c79f28a --- /dev/null +++ b/tests/manual/rhi/instancing/instancing.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +QT += gui-private + +SOURCES = \ + instancing.cpp + +RESOURCES = instancing.qrc diff --git a/tests/manual/rhi/instancing/instancing.qrc b/tests/manual/rhi/instancing/instancing.qrc new file mode 100644 index 0000000000..db21a9d8aa --- /dev/null +++ b/tests/manual/rhi/instancing/instancing.qrc @@ -0,0 +1,6 @@ + + + inst.vert.qsb + inst.frag.qsb + + diff --git a/tests/manual/rhi/rhi.pro b/tests/manual/rhi/rhi.pro index a06d973efe..d3661ff169 100644 --- a/tests/manual/rhi/rhi.pro +++ b/tests/manual/rhi/rhi.pro @@ -16,7 +16,8 @@ SUBDIRS += \ mrt \ shadowmap \ computebuffer \ - computeimage + computeimage \ + instancing qtConfig(widgets) { SUBDIRS += \