Debian Codegraph @

tests/manual/rhi/multiview/multiview.cpp

raw
1 // Copyright (C) 2023 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5
6 // Multiview rendering. Renders the same geometry (a triangle) with two
7 // different transforms into two layers of a texture array object in a *single*
8 // draw call. (NB under the hood it is at the hardware/driver's discretion what
9 // happens; it may very well map to some simple looping and still drawing
10 // twice, whereas with modern hardware it can be expected to be implemented
11 // more efficiently, but that's hidden from us)
12
13 // Toggle this to exercise 4x MSAA for the texture array that is the render
14 // target of the multiview render pass. The elements written by the multiview
15 // render pass get resolved to a non-multisample texture array at the end of
16 // the pass.
17 static bool MSAA = false;
18
19 static float quadVertexData[] =
20 { // Y up, CCW
21 -0.5f, 0.5f, 0.0f, 0.0f,
22 -0.5f, -0.5f, 0.0f, 1.0f,
23 0.5f, -0.5f, 1.0f, 1.0f,
24 0.5f, 0.5f, 1.0f, 0.0f
25 };
26
27 static quint16 quadIndexData[] =
28 {
29 0, 1, 2, 0, 2, 3
30 };
31
32 static float triangleData[] =
33 { // Y up, CCW
34 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
35 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
36 0.5f, -0.5f, 0.0f, 0.0f, 1.0f
37 };
38
39 static const int INSTANCE_COUNT = 5;
40
41 static float instanceData[INSTANCE_COUNT * 3] =
42 {
43 0.4f, 0.0f, 0.0f,
44 0.2f, 0.0f, 0.1f,
45 0.0f, 0.0f, 0.2f,
46 -0.2f, 0.0f, 0.3f,
47 -0.4f, 0.0f, 0.4f
48 };
49
50 struct {
51 QList<QRhiResource *> releasePool;
52 QRhiBuffer *vbuf = nullptr;
53 QRhiBuffer *instanceBuf = nullptr;
54 QRhiBuffer *ibuf = nullptr;
55 QRhiBuffer *ubuf = nullptr;
56 QRhiTextureRenderTarget *rt = nullptr;
57 QRhiRenderPassDescriptor *rtRp = nullptr;
58 QRhiSampler *sampler = nullptr;
59 QRhiGraphicsPipeline *ps = nullptr;
60 QRhiResourceUpdateBatch *initialUpdates = nullptr;
61 QMatrix4x4 winProj;
62 QRhiTexture *tex = nullptr;
63 QRhiTexture *resolveTex = nullptr; // only if MSAA is true
64 QRhiTexture *ds = nullptr;
65 QRhiShaderResourceBindings *srb[2] = {};
66
67 QRhiBuffer *triUbuf = nullptr;
68 QRhiShaderResourceBindings *triSrb = nullptr;
69 QRhiGraphicsPipeline *triPs = nullptr;
70 QMatrix4x4 triBaseMvp;
71 } d;
72
73 void Window::customInit()
74 {
75 if (!m_r->isFeatureSupported(QRhi::MultiView))
76 qFatal("Multiview is not supported");
77
78 int sampleCount = 1;
79 if (MSAA) {
80 qDebug("Using 4x MSAA for the multiview render pass");
81 sampleCount = 4;
82 }
83
84 // texture array with 2 elements, e.g. 0 is left eye, 1 is right
85 d.tex = m_r->newTextureArray(QRhiTexture::RGBA8, 2, QSize(512, 512), sampleCount, QRhiTexture::RenderTarget);
86 d.releasePool << d.tex;
87 d.tex->create();
88
89 if (MSAA) {
90 d.resolveTex = m_r->newTextureArray(QRhiTexture::RGBA8, 2, QSize(512, 512), 1, QRhiTexture::RenderTarget);
91 d.releasePool << d.resolveTex;
92 d.resolveTex->create();
93 }
94
95 // Have a depth-stencil buffer, just to exercise it, the triangles will be
96 // rendered with depth test/write enabled. The catch here is that we must
97 // use a texture array for depth/stencil as well, so QRhiRenderBuffer is
98 // not an option anymore.
99 d.ds = m_r->newTextureArray(QRhiTexture::D24S8, 2, QSize(512, 512), sampleCount, QRhiTexture::RenderTarget);
100 d.releasePool << d.ds;
101 d.ds->create();
102
103 // set up the multiview render target
104 QRhiColorAttachment multiViewAtt(d.tex);
105 // using array elements 0 and 1
106 multiViewAtt.setLayer(0);
107 multiViewAtt.setMultiViewCount(2); // the view count must be set both on the render target and the pipeline
108
109 // On-screen we work with a non-MSAA texture array, so the fragment shader
110 // does not need to deal with sampler2DMSArray, but can use sampler2DArray
111 // regardless of using multisampling or not. This means using an extra
112 // non-MSAA 2D texture array into which both array elements get resolved at
113 // the end of the multiview render pass.
114 QRhiTexture *textureForOnscreenView = d.tex;
115 if (MSAA) {
116 multiViewAtt.setResolveTexture(d.resolveTex);
117 textureForOnscreenView = d.resolveTex;
118 }
119
120 QRhiTextureRenderTargetDescription rtDesc(multiViewAtt);
121 rtDesc.setDepthTexture(d.ds);
122
123 d.rt = m_r->newTextureRenderTarget(rtDesc);
124 d.releasePool << d.rt;
125 d.rtRp = d.rt->newCompatibleRenderPassDescriptor();
126 d.releasePool << d.rtRp;
127 d.rt->setRenderPassDescriptor(d.rtRp);
128 d.rt->create();
129
130 // vertex buffer used by both passes
131 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData) + sizeof(triangleData));
132 d.vbuf->create();
133 d.releasePool << d.vbuf;
134
135 // data for the instanced translation attribute
136 d.instanceBuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(instanceData));
137 d.instanceBuf->create();
138 d.releasePool << d.instanceBuf;
139
140 // resources for the on-screen visualizer
141 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData));
142 d.ibuf->create();
143 d.releasePool << d.ibuf;
144
145 const int oneRoundedUniformBlockSize = m_r->ubufAligned(72);
146 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, oneRoundedUniformBlockSize * 2);
147 d.ubuf->create();
148 d.releasePool << d.ubuf;
149
150 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
151 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
152 d.releasePool << d.sampler;
153 d.sampler->create();
154
155 // two srbs, just for the quad positioning on-screen
156 for (int i = 0; i < 2; ++i) {
157 QRhiShaderResourceBindings *srb = m_r->newShaderResourceBindings();
158 d.releasePool << srb;
159 srb->setBindings({
160 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
161 d.ubuf, i * oneRoundedUniformBlockSize, 72),
162 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage,
163 textureForOnscreenView, d.sampler)
164 });
165 srb->create();
166 d.srb[i] = srb;
167 }
168
169 d.ps = m_r->newGraphicsPipeline();
170 d.releasePool << d.ps;
171 d.ps->setShaderStages({
172 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
173 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
174 });
175 QRhiVertexInputLayout inputLayout;
176 inputLayout.setBindings({
177 { 4 * sizeof(float) }
178 });
179 inputLayout.setAttributes({
180 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
181 { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
182 });
183 d.ps->setVertexInputLayout(inputLayout);
184 d.ps->setShaderResourceBindings(d.srb[0]); // all of them are layout-compatible
185 d.ps->setRenderPassDescriptor(m_rp);
186 d.ps->create();
187
188 d.initialUpdates = m_r->nextResourceUpdateBatch();
189 d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData);
190 d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(triangleData), triangleData);
191 d.initialUpdates->uploadStaticBuffer(d.instanceBuf, instanceData);
192 d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData);
193
194 qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
195 for (int i = 0; i < 2; ++i) {
196 d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 64, 4, &flip);
197 float layer = i;
198 d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 68, 4, &layer);
199 }
200
201 // create resources for the multiview render pass
202 d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 128); // mat4 mvp[2]
203 d.releasePool << d.triUbuf;
204 d.triUbuf->create();
205
206 d.triSrb = m_r->newShaderResourceBindings();
207 d.releasePool << d.triSrb;
208 d.triSrb->setBindings({
209 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf)
210 });
211 d.triSrb->create();
212
213 d.triPs = m_r->newGraphicsPipeline();
214 d.releasePool << d.triPs;
215 d.triPs->setShaderStages({
216 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/multiview.vert.qsb")) },
217 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/multiview.frag.qsb")) }
218 });
219 d.triPs->setMultiViewCount(2); // the view count must be set both on the render target and the pipeline
220 inputLayout.setBindings({
221 { 5 * sizeof(float) },
222 { 3 * sizeof(float), QRhiVertexInputBinding::PerInstance }
223 });
224 inputLayout.setAttributes({
225 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
226 { 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) },
227 { 1, 2, QRhiVertexInputAttribute::Float3, 0 }
228 });
229 d.triPs->setDepthTest(true);
230 d.triPs->setDepthWrite(true);
231 d.triPs->setSampleCount(sampleCount);
232 d.triPs->setVertexInputLayout(inputLayout);
233 d.triPs->setShaderResourceBindings(d.triSrb);
234 d.triPs->setRenderPassDescriptor(d.rtRp);
235 d.triPs->create();
236
237 d.triBaseMvp = m_r->clipSpaceCorrMatrix();
238 d.triBaseMvp.perspective(45.0f, d.rt->pixelSize().width() / float(d.rt->pixelSize().height()), 0.01f, 1000.0f);
239 d.triBaseMvp.translate(0, 0, -2);
240 }
241
242 void Window::customRelease()
243 {
244 qDeleteAll(d.releasePool);
245 d.releasePool.clear();
246 }
247
248 void Window::customRender()
249 {
250 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
251 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
252 if (d.initialUpdates) {
253 u->merge(d.initialUpdates);
254 d.initialUpdates->release();
255 d.initialUpdates = nullptr;
256 }
257
258 QMatrix4x4 triMvp = d.triBaseMvp;
259 // let's say this is the left eye, make the triangle point left for now
260 triMvp.rotate(90, 0, 0, 1);
261 u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
262 triMvp = d.triBaseMvp;
263 // right for the right eye
264 triMvp.rotate(270, 0, 0, 1);
265 u->updateDynamicBuffer(d.triUbuf, 64, 64, triMvp.constData());
266
267 cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u);
268 cb->setGraphicsPipeline(d.triPs);
269 cb->setViewport({ 0, 0, float(d.rt->pixelSize().width()), float(d.rt->pixelSize().height()) });
270 cb->setShaderResources();
271 const QRhiCommandBuffer::VertexInput multiViewPassVbufBindings[] = {
272 { d.vbuf, quint32(sizeof(quadVertexData)) },
273 { d.instanceBuf, 0 }
274 };
275 cb->setVertexInput(0, 2, multiViewPassVbufBindings);
276 cb->draw(3, INSTANCE_COUNT);
277 cb->endPass();
278
279 // "blit" the two texture layers on-screen just to visualize the contents
280 u = m_r->nextResourceUpdateBatch();
281 if (d.winProj != m_proj) {
282 d.winProj = m_proj;
283 const int oneRoundedUniformBlockSize = m_r->ubufAligned(72);
284 for (int i = 0; i < 2; ++i) {
285 QMatrix4x4 mvp = m_proj;
286 mvp.translate(0, 0, 1);
287 if (i == 0)
288 mvp.translate(-1.0f, 0, 0);
289 else
290 mvp.translate(1.0f, 0, 0);
291 u->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize, 64, mvp.constData());
292 }
293 }
294 const QSize outputSizeInPixels = m_sc->currentPixelSize();
295 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
296 cb->setGraphicsPipeline(d.ps);
297 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
298 const QRhiCommandBuffer::VertexInput quadPassVBufBindings[] = { { d.vbuf, 0 } };
299 cb->setVertexInput(0, 1, quadPassVBufBindings, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
300 for (int i = 0; i < 2; ++i) {
301 cb->setShaderResources(d.srb[i]);
302 cb->drawIndexed(6);
303 }
304 cb->endPass();
305 }