QtBase  v6.3.1
msaarenderbuffer.cpp
Go to the documentation of this file.
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 {
78  QRhiBuffer *vbuf = nullptr;
79  QRhiBuffer *ibuf = nullptr;
80  QRhiBuffer *ubuf = nullptr;
81  QRhiRenderBuffer *rb = nullptr;
84  QRhiTexture *tex = nullptr;
85  QRhiSampler *sampler = nullptr;
86  QRhiBuffer *triUbuf = nullptr;
93  float triRot = 0;
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->create();
101  d.releasePool << d.vbuf;
102 
103  d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
104  d.ibuf->create();
105  d.releasePool << d.ibuf;
106 
108  d.ubuf->create();
109  d.releasePool << d.ubuf;
110 
111  d.rb = m_r->newRenderBuffer(QRhiRenderBuffer::Color, QSize(512, 512), 4); // 4x MSAA
112  d.rb->create();
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->create();
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
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->create();
133 
135  d.releasePool << d.triUbuf;
136  d.triUbuf->create();
137 
138  d.triSrb = m_r->newShaderResourceBindings();
139  d.releasePool << d.triSrb;
140  d.triSrb->setBindings({
142  });
143  d.triSrb->create();
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({
158  { 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) }
159  });
160  d.triPs->setVertexInputLayout(inputLayout);
161  d.triPs->setShaderResourceBindings(d.triSrb);
162  d.triPs->setRenderPassDescriptor(d.rtRp);
163  d.triPs->create();
164 
167  d.releasePool << d.sampler;
168  d.sampler->create();
169 
171  d.releasePool << d.srb;
172  d.srb->setBindings({
175  });
176  d.srb->create();
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({
189  { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
190  });
191  d.ps->setVertexInputLayout(inputLayout);
192  d.ps->setShaderResourceBindings(d.srb);
193  d.ps->setRenderPassDescriptor(m_rp);
194  d.ps->create();
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 
212 {
213  qDeleteAll(d.releasePool);
214  d.releasePool.clear();
215 }
216 
218 {
219  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
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, quint32(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 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
static QColor fromRgbF(float r, float g, float b, float a=1.0)
Definition: qcolor.cpp:2424
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:61
void rotate(float angle, const QVector3D &vector)
void scale(const QVector3D &vector)
Definition: qmatrix4x4.cpp:803
const float * constData() const
Definition: qmatrix4x4.h:183
@ Immutable
Definition: qrhi_p.h:717
@ Dynamic
Definition: qrhi_p.h:719
@ IndexBuffer
Definition: qrhi_p.h:724
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
void setResolveTexture(QRhiTexture *tex)
Definition: qrhi_p.h:476
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
QMatrix4x4 clipSpaceCorrMatrix() const
Definition: qrhi.cpp:6299
bool isYUpInFramebuffer() const
Definition: qrhi.cpp:6238
QRhiShaderResourceBindings * newShaderResourceBindings()
Definition: qrhi.cpp:6543
QRhiRenderBuffer * newRenderBuffer(QRhiRenderBuffer::Type type, const QSize &pixelSize, int sampleCount=1, QRhiRenderBuffer::Flags flags={}, QRhiTexture::Format backingFormatHint=QRhiTexture::UnknownFormat)
Definition: qrhi.cpp:6589
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, int size)
Definition: qrhi.cpp:6562
QRhiSampler * newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter, QRhiSampler::Filter mipmapMode, QRhiSampler::AddressMode addressU, QRhiSampler::AddressMode addressV, QRhiSampler::AddressMode addressW=QRhiSampler::Repeat)
Definition: qrhi.cpp:6679
QRhiTextureRenderTarget * newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc, QRhiTextureRenderTarget::Flags flags={})
Definition: qrhi.cpp:6696
QRhiGraphicsPipeline * newGraphicsPipeline()
Definition: qrhi.cpp:6520
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition: qrhi.cpp:6609
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
@ ClampToEdge
Definition: qrhi_p.h:884
static QRhiShaderResourceBinding sampledTexture(int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
Definition: qrhi.cpp:3374
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition: qrhi.cpp:3268
@ RenderTarget
Definition: qrhi_p.h:766
void setColorAttachments(std::initializer_list< QRhiColorAttachment > list)
Definition: qrhi_p.h:504
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Definition: qrhi_p.h:257
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Definition: qrhi_p.h:268
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr int width() const noexcept
Definition: qsize.h:157
qreal opacity
The opacity of the window in the windowing system.
Definition: qwindow.h:132
std::unique_ptr< QRhiSwapChain > m_sc
Definition: window.h:89
std::unique_ptr< QRhiRenderPassDescriptor > m_rp
Definition: window.h:91
virtual void customRender()
QColor m_clearColor
Definition: examplefw.h:176
QMatrix4x4 m_proj
Definition: window.h:94
virtual void customInit()
QRhi * m_r
Definition: examplefw.h:161
qDeleteAll(list.begin(), list.end())
QShader getShader(const QString &name)
Definition: examplefw.h:86
QRhiRenderBuffer * rb
QRhiBuffer * ibuf
struct @915 d
QRhiShaderResourceBindings * triSrb
QRhiBuffer * ubuf
QList< QRhiResource * > releasePool
float triRot
QRhiTextureRenderTarget * rt
QRhiRenderPassDescriptor * rtRp
QRhiTexture * tex
QRhiGraphicsPipeline * triPs
QMatrix4x4 triBaseMvp
QRhiBuffer * triUbuf
QRhiShaderResourceBindings * srb
QRhiBuffer * vbuf
QMatrix4x4 winProj
QRhiGraphicsPipeline * ps
QRhiResourceUpdateBatch * initialUpdates
unsigned int quint32
Definition: qglobal.h:288
unsigned short quint16
Definition: qglobal.h:286
int qint32
Definition: qglobal.h:287
GLuint sampler
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)