summaryrefslogtreecommitdiffstats
path: root/tests/manual/rhi/multiview/multiview.cpp
blob: a89821eb8eb170c7fb91f11ef293b8bbfe4b9154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "../shared/examplefw.h"

// Multiview rendering. Renders the same geometry (a triangle) with two
// different transforms into two layers of a texture array object in a *single*
// draw call. (NB under the hood it is at the hardware/driver's discretion what
// happens; it may very well map to some simple looping and still drawing
// twice, whereas with modern hardware it can be expected to be implemented
// more efficiently, but that's hidden from us)

// Toggle this to exercise 4x MSAA for the texture array that is the render
// target of the multiview render pass. The elements written by the multiview
// render pass get resolved to a non-multisample texture array at the end of
// the pass.
static bool MSAA = false;

static float quadVertexData[] =
{ // Y up, CCW
  -0.5f,   0.5f,   0.0f, 0.0f,
  -0.5f,  -0.5f,   0.0f, 1.0f,
  0.5f,   -0.5f,   1.0f, 1.0f,
  0.5f,   0.5f,    1.0f, 0.0f
};

static quint16 quadIndexData[] =
{
    0, 1, 2, 0, 2, 3
};

static float triangleData[] =
{ // Y up, CCW
     0.0f,   0.5f,   1.0f, 0.0f, 0.0f,
    -0.5f,  -0.5f,   0.0f, 1.0f, 0.0f,
     0.5f,  -0.5f,   0.0f, 0.0f, 1.0f
};

static const int INSTANCE_COUNT = 5;

static float instanceData[INSTANCE_COUNT * 3] =
{
    0.4f,  0.0f, 0.0f,
    0.2f,  0.0f, 0.1f,
    0.0f,  0.0f, 0.2f,
    -0.2f, 0.0f, 0.3f,
    -0.4f, 0.0f, 0.4f
};

struct {
    QList<QRhiResource *> releasePool;
    QRhiBuffer *vbuf = nullptr;
    QRhiBuffer *instanceBuf = nullptr;
    QRhiBuffer *ibuf = nullptr;
    QRhiBuffer *ubuf = nullptr;
    QRhiTextureRenderTarget *rt = nullptr;
    QRhiRenderPassDescriptor *rtRp = nullptr;
    QRhiSampler *sampler = nullptr;
    QRhiGraphicsPipeline *ps = nullptr;
    QRhiResourceUpdateBatch *initialUpdates = nullptr;
    QMatrix4x4 winProj;
    QRhiTexture *tex = nullptr;
    QRhiTexture *resolveTex = nullptr; // only if MSAA is true
    QRhiTexture *ds = nullptr;
    QRhiShaderResourceBindings *srb[2] = {};

    QRhiBuffer *triUbuf = nullptr;
    QRhiShaderResourceBindings *triSrb = nullptr;
    QRhiGraphicsPipeline *triPs = nullptr;
    QMatrix4x4 triBaseMvp;
} d;

void Window::customInit()
{
    if (!m_r->isFeatureSupported(QRhi::MultiView))
        qFatal("Multiview is not supported");

    int sampleCount = 1;
    if (MSAA) {
        qDebug("Using 4x MSAA for the multiview render pass");
        sampleCount = 4;
    }

    // texture array with 2 elements, e.g. 0 is left eye, 1 is right
    d.tex = m_r->newTextureArray(QRhiTexture::RGBA8, 2, QSize(512, 512), sampleCount, QRhiTexture::RenderTarget);
    d.releasePool << d.tex;
    d.tex->create();

    if (MSAA) {
        d.resolveTex = m_r->newTextureArray(QRhiTexture::RGBA8, 2, QSize(512, 512), 1, QRhiTexture::RenderTarget);
        d.releasePool << d.resolveTex;
        d.resolveTex->create();
    }

    // Have a depth-stencil buffer, just to exercise it, the triangles will be
    // rendered with depth test/write enabled. The catch here is that we must
    // use a texture array for depth/stencil as well, so QRhiRenderBuffer is
    // not an option anymore.
    d.ds = m_r->newTextureArray(QRhiTexture::D24S8, 2, QSize(512, 512), sampleCount, QRhiTexture::RenderTarget);
    d.releasePool << d.ds;
    d.ds->create();

    // set up the multiview render target
    QRhiColorAttachment multiViewAtt(d.tex);
    // using array elements 0 and 1
    multiViewAtt.setLayer(0);
    multiViewAtt.setMultiViewCount(2); // the view count must be set both on the render target and the pipeline

    // On-screen we work with a non-MSAA texture array, so the fragment shader
    // does not need to deal with sampler2DMSArray, but can use sampler2DArray
    // regardless of using multisampling or not. This means using an extra
    // non-MSAA 2D texture array into which both array elements get resolved at
    // the end of the multiview render pass.
    QRhiTexture *textureForOnscreenView = d.tex;
    if (MSAA) {
        multiViewAtt.setResolveTexture(d.resolveTex);
        textureForOnscreenView = d.resolveTex;
    }

    QRhiTextureRenderTargetDescription rtDesc(multiViewAtt);
    rtDesc.setDepthTexture(d.ds);

    d.rt = m_r->newTextureRenderTarget(rtDesc);
    d.releasePool << d.rt;
    d.rtRp = d.rt->newCompatibleRenderPassDescriptor();
    d.releasePool << d.rtRp;
    d.rt->setRenderPassDescriptor(d.rtRp);
    d.rt->create();

    // vertex buffer used by both passes
    d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData) + sizeof(triangleData));
    d.vbuf->create();
    d.releasePool << d.vbuf;

    // data for the instanced translation attribute
    d.instanceBuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(instanceData));
    d.instanceBuf->create();
    d.releasePool << d.instanceBuf;

    // resources for the on-screen visualizer
    d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData));
    d.ibuf->create();
    d.releasePool << d.ibuf;

    const int oneRoundedUniformBlockSize = m_r->ubufAligned(72);
    d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, oneRoundedUniformBlockSize * 2);
    d.ubuf->create();
    d.releasePool << d.ubuf;

    d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
                                QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
    d.releasePool << d.sampler;
    d.sampler->create();

    // two srbs, just for the quad positioning on-screen
    for (int i = 0; i < 2; ++i) {
        QRhiShaderResourceBindings *srb = m_r->newShaderResourceBindings();
        d.releasePool << srb;
        srb->setBindings({
            QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
                                                     d.ubuf, i * oneRoundedUniformBlockSize, 72),
            QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage,
                                                      textureForOnscreenView, d.sampler)
        });
        srb->create();
        d.srb[i] = srb;
    }

    d.ps = m_r->newGraphicsPipeline();
    d.releasePool << d.ps;
    d.ps->setShaderStages({
        { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
        { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
    });
    QRhiVertexInputLayout inputLayout;
    inputLayout.setBindings({
        { 4 * sizeof(float) }
    });
    inputLayout.setAttributes({
        { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
        { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
    });
    d.ps->setVertexInputLayout(inputLayout);
    d.ps->setShaderResourceBindings(d.srb[0]); // all of them are layout-compatible
    d.ps->setRenderPassDescriptor(m_rp);
    d.ps->create();

    d.initialUpdates = m_r->nextResourceUpdateBatch();
    d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData);
    d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(triangleData), triangleData);
    d.initialUpdates->uploadStaticBuffer(d.instanceBuf, instanceData);
    d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData);

    qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
    for (int i = 0; i < 2; ++i) {
        d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 64, 4, &flip);
        float layer = i;
        d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 68, 4, &layer);
    }

    // create resources for the multiview render pass
    d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 128); // mat4 mvp[2]
    d.releasePool << d.triUbuf;
    d.triUbuf->create();

    d.triSrb = m_r->newShaderResourceBindings();
    d.releasePool << d.triSrb;
    d.triSrb->setBindings({
        QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf)
    });
    d.triSrb->create();

    d.triPs = m_r->newGraphicsPipeline();
    d.releasePool << d.triPs;
    d.triPs->setShaderStages({
        { QRhiShaderStage::Vertex, getShader(QLatin1String(":/multiview.vert.qsb")) },
        { QRhiShaderStage::Fragment, getShader(QLatin1String(":/multiview.frag.qsb")) }
    });
    d.triPs->setMultiViewCount(2); // the view count must be set both on the render target and the pipeline
    inputLayout.setBindings({
        { 5 * sizeof(float) },
        { 3 * sizeof(float), QRhiVertexInputBinding::PerInstance }
    });
    inputLayout.setAttributes({
        { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
        { 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) },
        { 1, 2, QRhiVertexInputAttribute::Float3, 0 }
    });
    d.triPs->setDepthTest(true);
    d.triPs->setDepthWrite(true);
    d.triPs->setSampleCount(sampleCount);
    d.triPs->setVertexInputLayout(inputLayout);
    d.triPs->setShaderResourceBindings(d.triSrb);
    d.triPs->setRenderPassDescriptor(d.rtRp);
    d.triPs->create();

    d.triBaseMvp = m_r->clipSpaceCorrMatrix();
    d.triBaseMvp.perspective(45.0f, d.rt->pixelSize().width() / float(d.rt->pixelSize().height()), 0.01f, 1000.0f);
    d.triBaseMvp.translate(0, 0, -2);
}

void Window::customRelease()
{
    qDeleteAll(d.releasePool);
    d.releasePool.clear();
}

void Window::customRender()
{
    QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
    QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
    if (d.initialUpdates) {
        u->merge(d.initialUpdates);
        d.initialUpdates->release();
        d.initialUpdates = nullptr;
    }

    QMatrix4x4 triMvp = d.triBaseMvp;
    // let's say this is the left eye, make the triangle point left for now
    triMvp.rotate(90, 0, 0, 1);
    u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
    triMvp = d.triBaseMvp;
    // right for the right eye
    triMvp.rotate(270, 0, 0, 1);
    u->updateDynamicBuffer(d.triUbuf, 64, 64, triMvp.constData());

    cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u);
    cb->setGraphicsPipeline(d.triPs);
    cb->setViewport({ 0, 0, float(d.rt->pixelSize().width()), float(d.rt->pixelSize().height()) });
    cb->setShaderResources();
    const QRhiCommandBuffer::VertexInput multiViewPassVbufBindings[] = {
        { d.vbuf, quint32(sizeof(quadVertexData)) },
        { d.instanceBuf, 0 }
    };
    cb->setVertexInput(0, 2, multiViewPassVbufBindings);
    cb->draw(3, INSTANCE_COUNT);
    cb->endPass();

    // "blit" the two texture layers on-screen just to visualize the contents
    u = m_r->nextResourceUpdateBatch();
    if (d.winProj != m_proj) {
        d.winProj = m_proj;
        const int oneRoundedUniformBlockSize = m_r->ubufAligned(72);
        for (int i = 0; i < 2; ++i) {
            QMatrix4x4 mvp = m_proj;
            mvp.translate(0, 0, 1);
            if (i == 0)
                mvp.translate(-1.0f, 0, 0);
            else
                mvp.translate(1.0f, 0, 0);
            u->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize, 64, mvp.constData());
        }
    }
    const QSize outputSizeInPixels = m_sc->currentPixelSize();
    cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
    cb->setGraphicsPipeline(d.ps);
    cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
    const QRhiCommandBuffer::VertexInput quadPassVBufBindings[] = { { d.vbuf, 0 } };
    cb->setVertexInput(0, 1, quadPassVBufBindings, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
    for (int i = 0; i < 2; ++i) {
        cb->setShaderResources(d.srb[i]);
        cb->drawIndexed(6);
    }
    cb->endPass();
}