Debian Codegraph @

tests/manual/rhi/msaarenderbuffer/msaarenderbuffer.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 // Uses a multisample renderbuffer (whatever that may be on a given backend) to
54 // render to and then resolves the samples into a non-multisample texture.
55
56 static float vertexData[] =
57 { // Y up, CCW
58 -0.5f, 0.5f, 0.0f, 0.0f,
59 -0.5f, -0.5f, 0.0f, 1.0f,
60 0.5f, -0.5f, 1.0f, 1.0f,
61 0.5f, 0.5f, 1.0f, 0.0f
62 };
63
64 static quint16 indexData[] =
65 {
66 0, 1, 2, 0, 2, 3
67 };
68
69 static float triangleData[] =
70 { // Y up, CCW
71 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
72 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
73 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
74 };
75
76 struct {
77 QVector<QRhiResource *> releasePool;
78 QRhiBuffer *vbuf = nullptr;
79 QRhiBuffer *ibuf = nullptr;
80 QRhiBuffer *ubuf = nullptr;
81 QRhiRenderBuffer *rb = nullptr;
82 QRhiTextureRenderTarget *rt = nullptr;
83 QRhiRenderPassDescriptor *rtRp = nullptr;
84 QRhiTexture *tex = nullptr;
85 QRhiSampler *sampler = nullptr;
86 QRhiBuffer *triUbuf = nullptr;
87 QRhiShaderResourceBindings *triSrb = nullptr;
88 QRhiGraphicsPipeline *triPs = nullptr;
89 QRhiShaderResourceBindings *srb = nullptr;
90 QRhiGraphicsPipeline *ps = nullptr;
91 QRhiResourceUpdateBatch *initialUpdates = nullptr;
92 QMatrix4x4 triBaseMvp;
93 float triRot = 0;
94 QMatrix4x4 winProj;
95 } d;
96
97 void Window::customInit()
98 {
99 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData) + sizeof(triangleData));
100 d.vbuf->build();
101 d.releasePool << d.vbuf;
102
103 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
104 d.ibuf->build();
105 d.releasePool << d.ibuf;
106
107 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
108 d.ubuf->build();
109 d.releasePool << d.ubuf;
110
111 d.rb = m_r->newRenderBuffer(QRhiRenderBuffer::Color, QSize(512, 512), 4); // 4x MSAA
112 d.rb->build();
113 d.releasePool << d.rb;
114
115 // the non-msaa texture that will be the destination in the resolve
116 d.tex = m_r->newTexture(QRhiTexture::RGBA8, d.rb->pixelSize(), 1, QRhiTexture::RenderTarget);
117 d.releasePool << d.tex;
118 d.tex->build();
119
120 // rb is multisample, instead of writing out the msaa data into it,
121 // resolve into d.tex at the end of each render pass
122 QRhiTextureRenderTargetDescription rtDesc;
123 QRhiColorAttachment rtAtt(d.rb);
124 rtAtt.setResolveTexture(d.tex);
125 rtDesc.setColorAttachments({ rtAtt });
126
127 d.rt = m_r->newTextureRenderTarget(rtDesc);
128 d.releasePool << d.rt;
129 d.rtRp = d.rt->newCompatibleRenderPassDescriptor();
130 d.releasePool << d.rtRp;
131 d.rt->setRenderPassDescriptor(d.rtRp);
132 d.rt->build();
133
134 d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
135 d.releasePool << d.triUbuf;
136 d.triUbuf->build();
137
138 d.triSrb = m_r->newShaderResourceBindings();
139 d.releasePool << d.triSrb;
140 d.triSrb->setBindings({
141 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf)
142 });
143 d.triSrb->build();
144
145 d.triPs = m_r->newGraphicsPipeline();
146 d.releasePool << d.triPs;
147 d.triPs->setSampleCount(4); // must match the render target
148 d.triPs->setShaderStages({
149 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/color.vert.qsb")) },
150 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/color.frag.qsb")) }
151 });
152 QRhiVertexInputLayout inputLayout;
153 inputLayout.setBindings({
154 { 5 * sizeof(float) }
155 });
156 inputLayout.setAttributes({
157 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
158 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
159 });
160 d.triPs->setVertexInputLayout(inputLayout);
161 d.triPs->setShaderResourceBindings(d.triSrb);
162 d.triPs->setRenderPassDescriptor(d.rtRp);
163 d.triPs->build();
164
165 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
166 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
167 d.releasePool << d.sampler;
168 d.sampler->build();
169
170 d.srb = m_r->newShaderResourceBindings();
171 d.releasePool << d.srb;
172 d.srb->setBindings({
173 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
174 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
175 });
176 d.srb->build();
177
178 d.ps = m_r->newGraphicsPipeline();
179 d.releasePool << d.ps;
180 d.ps->setShaderStages({
181 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
182 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
183 });
184 inputLayout.setBindings({
185 { 4 * sizeof(float) }
186 });
187 inputLayout.setAttributes({
188 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
189 { 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) }
190 });
191 d.ps->setVertexInputLayout(inputLayout);
192 d.ps->setShaderResourceBindings(d.srb);
193 d.ps->setRenderPassDescriptor(m_rp);
194 d.ps->build();
195
196 d.initialUpdates = m_r->nextResourceUpdateBatch();
197 d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(vertexData), vertexData);
198 d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(vertexData), sizeof(triangleData), triangleData);
199 d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData);
200
201 d.triBaseMvp = m_r->clipSpaceCorrMatrix();
202 d.triBaseMvp.perspective(45.0f, d.rb->pixelSize().width() / float(d.rb->pixelSize().height()), 0.01f, 1000.0f);
203 d.triBaseMvp.translate(0, 0, -2);
204 float opacity = 1.0f;
205 d.initialUpdates->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity);
206
207 qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
208 d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
209 }
210
211 void Window::customRelease()
212 {
213 qDeleteAll(d.releasePool);
214 d.releasePool.clear();
215 }
216
217 void Window::customRender()
218 {
219 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
220 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
221 if (d.initialUpdates) {
222 u->merge(d.initialUpdates);
223 d.initialUpdates->release();
224 d.initialUpdates = nullptr;
225 }
226
227 QMatrix4x4 triMvp = d.triBaseMvp;
228 triMvp.rotate(d.triRot, 0, 1, 0);
229 d.triRot += 1;
230 u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
231
232 if (d.winProj != m_proj) {
233 d.winProj = m_proj;
234 QMatrix4x4 mvp = m_proj;
235 mvp.scale(2.5f);
236 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
237 }
238
239 // offscreen (triangle, msaa)
240 cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u);
241 cb->setGraphicsPipeline(d.triPs);
242 cb->setViewport({ 0, 0, float(d.rb->pixelSize().width()), float(d.rb->pixelSize().height()) });
243 cb->setShaderResources();
244 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, sizeof(vertexData));
245 cb->setVertexInput(0, 1, &vbufBinding);
246 cb->draw(3);
247 cb->endPass();
248
249 // onscreen (quad)
250 const QSize outputSizeInPixels = m_sc->currentPixelSize();
251 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 });
252 cb->setGraphicsPipeline(d.ps);
253 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
254 cb->setShaderResources();
255 vbufBinding.second = 0;
256 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
257 cb->drawIndexed(6);
258 cb->endPass();
259 }