Debian Codegraph @

tests/manual/rhi/mrt/mrt.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "../shared/examplefw.h"
52
53 // Exercises MRT, by attaching 4 textures to a single QRhiTextureRenderTarget.
54 // The fragment shader outputs to all four targets.
55 // The textures are then used to render 4 quads, so their contents can be confirmed.
56
57 const int ATTCOUNT = 4;
58
59 static float quadVertexData[] =
60 { // Y up, CCW
61 -0.5f, 0.5f, 0.0f, 0.0f,
62 -0.5f, -0.5f, 0.0f, 1.0f,
63 0.5f, -0.5f, 1.0f, 1.0f,
64 0.5f, 0.5f, 1.0f, 0.0f
65 };
66
67 static quint16 quadIndexData[] =
68 {
69 0, 1, 2, 0, 2, 3
70 };
71
72 static float triangleData[] =
73 { // Y up, CCW
74 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
75 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
76 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
77 };
78
79 struct {
80 QVector<QRhiResource *> releasePool;
81 QRhiBuffer *vbuf = nullptr;
82 QRhiBuffer *ibuf = nullptr;
83 QRhiBuffer *ubuf = nullptr;
84 QRhiTextureRenderTarget *rt = nullptr;
85 QRhiRenderPassDescriptor *rtRp = nullptr;
86 QRhiSampler *sampler = nullptr;
87 QRhiGraphicsPipeline *ps = nullptr;
88 QRhiResourceUpdateBatch *initialUpdates = nullptr;
89 QMatrix4x4 winProj;
90 struct PerColorBuffer {
91 QRhiTexture *tex = nullptr;
92 QRhiShaderResourceBindings *srb = nullptr;
93 };
94 PerColorBuffer colData[ATTCOUNT];
95
96 QRhiBuffer *triUbuf = nullptr;
97 QRhiShaderResourceBindings *triSrb = nullptr;
98 QRhiGraphicsPipeline *triPs = nullptr;
99 float triRot = 0;
100 QMatrix4x4 triBaseMvp;
101 } d;
102
103 void Window::customInit()
104 {
105 qDebug("Max color attachments: %d", m_r->resourceLimit(QRhi::MaxColorAttachments));
106 if (m_r->resourceLimit(QRhi::MaxColorAttachments) < 4)
107 qWarning("MRT is not supported");
108
109 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData) + sizeof(triangleData));
110 d.vbuf->build();
111 d.releasePool << d.vbuf;
112
113 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData));
114 d.ibuf->build();
115 d.releasePool << d.ibuf;
116
117 const int oneRoundedUniformBlockSize = m_r->ubufAligned(68);
118 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, oneRoundedUniformBlockSize * ATTCOUNT);
119 d.ubuf->build();
120 d.releasePool << d.ubuf;
121
122 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
123 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
124 d.releasePool << d.sampler;
125 d.sampler->build();
126
127 for (int i = 0; i < ATTCOUNT; ++i) {
128 QRhiTexture *tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
129 d.releasePool << tex;
130 tex->build();
131
132 QRhiShaderResourceBindings *srb = m_r->newShaderResourceBindings();
133 d.releasePool << srb;
134 srb->setBindings({
135 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
136 d.ubuf, i * oneRoundedUniformBlockSize, 68),
137 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, tex, d.sampler)
138 });
139 srb->build();
140
141 d.colData[i].tex = tex;
142 d.colData[i].srb = srb;
143 }
144
145 QRhiTextureRenderTargetDescription rtDesc;
146 QRhiColorAttachment att[ATTCOUNT];
147 for (int i = 0; i < ATTCOUNT; ++i)
148 att[i] = QRhiColorAttachment(d.colData[i].tex);
149 rtDesc.setColorAttachments(att, att + ATTCOUNT);
150 d.rt = m_r->newTextureRenderTarget(rtDesc);
151 d.releasePool << d.rt;
152 d.rtRp = d.rt->newCompatibleRenderPassDescriptor();
153 d.releasePool << d.rtRp;
154 d.rt->setRenderPassDescriptor(d.rtRp);
155 d.rt->build();
156
157 d.ps = m_r->newGraphicsPipeline();
158 d.releasePool << d.ps;
159 d.ps->setShaderStages({
160 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
161 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
162 });
163 QRhiVertexInputLayout inputLayout;
164 inputLayout.setBindings({
165 { 4 * sizeof(float) }
166 });
167 inputLayout.setAttributes({
168 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
169 { 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) }
170 });
171 d.ps->setVertexInputLayout(inputLayout);
172 d.ps->setShaderResourceBindings(d.colData[0].srb); // all of them are layout-compatible
173 d.ps->setRenderPassDescriptor(m_rp);
174 d.ps->build();
175
176 d.initialUpdates = m_r->nextResourceUpdateBatch();
177 d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData);
178 d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(triangleData), triangleData);
179 d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData);
180
181 qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
182 for (int i = 0; i < ATTCOUNT; ++i)
183 d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 64, 4, &flip);
184
185 // triangle
186 d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
187 d.releasePool << d.triUbuf;
188 d.triUbuf->build();
189
190 d.triSrb = m_r->newShaderResourceBindings();
191 d.releasePool << d.triSrb;
192 d.triSrb->setBindings({
193 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf)
194 });
195 d.triSrb->build();
196
197 d.triPs = m_r->newGraphicsPipeline();
198 d.releasePool << d.triPs;
199 d.triPs->setShaderStages({
200 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/mrt.vert.qsb")) },
201 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/mrt.frag.qsb")) }
202 });
203
204 QRhiGraphicsPipeline::TargetBlend blends[ATTCOUNT]; // defaults to blending == false
205 d.triPs->setTargetBlends(blends, blends + ATTCOUNT);
206
207 inputLayout.setBindings({
208 { 5 * sizeof(float) }
209 });
210 inputLayout.setAttributes({
211 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
212 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
213 });
214 d.triPs->setVertexInputLayout(inputLayout);
215 d.triPs->setShaderResourceBindings(d.triSrb);
216 d.triPs->setRenderPassDescriptor(d.rtRp);
217 d.triPs->build();
218
219 d.triBaseMvp = m_r->clipSpaceCorrMatrix();
220 d.triBaseMvp.perspective(45.0f, d.rt->pixelSize().width() / float(d.rt->pixelSize().height()), 0.01f, 1000.0f);
221 d.triBaseMvp.translate(0, 0, -2);
222 float opacity = 1.0f;
223 d.initialUpdates->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity);
224 }
225
226 void Window::customRelease()
227 {
228 qDeleteAll(d.releasePool);
229 d.releasePool.clear();
230 }
231
232 void Window::customRender()
233 {
234 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
235 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
236 if (d.initialUpdates) {
237 u->merge(d.initialUpdates);
238 d.initialUpdates->release();
239 d.initialUpdates = nullptr;
240 }
241
242 QMatrix4x4 triMvp = d.triBaseMvp;
243 triMvp.rotate(d.triRot, 0, 1, 0);
244 d.triRot += 1;
245 u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
246
247 cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u);
248 cb->setGraphicsPipeline(d.triPs);
249 cb->setViewport({ 0, 0, float(d.rt->pixelSize().width()), float(d.rt->pixelSize().height()) });
250 cb->setShaderResources();
251 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, sizeof(quadVertexData));
252 cb->setVertexInput(0, 1, &vbufBinding);
253 cb->draw(3);
254 cb->endPass();
255
256 u = m_r->nextResourceUpdateBatch();
257 if (d.winProj != m_proj) {
258 d.winProj = m_proj;
259 const int oneRoundedUniformBlockSize = m_r->ubufAligned(68);
260 for (int i = 0; i < ATTCOUNT; ++i) {
261 QMatrix4x4 mvp = m_proj;
262 switch (i) {
263 case 0:
264 mvp.translate(-2.0f, 0, 0);
265 break;
266 case 1:
267 mvp.translate(-0.8f, 0, 0);
268 break;
269 case 2:
270 mvp.translate(0.4f, 0, 0);
271 break;
272 case 3:
273 mvp.translate(1.6f, 0, 0);
274 break;
275 default:
276 Q_UNREACHABLE();
277 break;
278 }
279 u->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize, 64, mvp.constData());
280 }
281 }
282
283 const QSize outputSizeInPixels = m_sc->currentPixelSize();
284 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
285 cb->setGraphicsPipeline(d.ps);
286 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
287 vbufBinding.second = 0;
288 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
289 for (int i = 0; i < ATTCOUNT; ++i) {
290 cb->setShaderResources(d.colData[i].srb);
291 cb->drawIndexed(6);
292 }
293 cb->endPass();
294 }