Debian Codegraph @

tests/manual/rhi/msaarenderbuffer/msaarenderbuffer.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5
6 // Uses a multisample renderbuffer (whatever that may be on a given backend) to
7 // render to and then resolves the samples into a non-multisample texture.
8
9 static float vertexData[] =
10 { // Y up, CCW
11 -0.5f, 0.5f, 0.0f, 0.0f,
12 -0.5f, -0.5f, 0.0f, 1.0f,
13 0.5f, -0.5f, 1.0f, 1.0f,
14 0.5f, 0.5f, 1.0f, 0.0f
15 };
16
17 static quint16 indexData[] =
18 {
19 0, 1, 2, 0, 2, 3
20 };
21
22 static float triangleData[] =
23 { // Y up, CCW
24 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
25 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
26 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
27 };
28
29 struct {
30 QList<QRhiResource *> releasePool;
31 QRhiBuffer *vbuf = nullptr;
32 QRhiBuffer *ibuf = nullptr;
33 QRhiBuffer *ubuf = nullptr;
34 QRhiRenderBuffer *rb = nullptr;
35 QRhiTextureRenderTarget *rt = nullptr;
36 QRhiRenderPassDescriptor *rtRp = nullptr;
37 QRhiTexture *tex = nullptr;
38 QRhiSampler *sampler = nullptr;
39 QRhiBuffer *triUbuf = nullptr;
40 QRhiShaderResourceBindings *triSrb = nullptr;
41 QRhiGraphicsPipeline *triPs = nullptr;
42 QRhiShaderResourceBindings *srb = nullptr;
43 QRhiGraphicsPipeline *ps = nullptr;
44 QRhiResourceUpdateBatch *initialUpdates = nullptr;
45 QMatrix4x4 triBaseMvp;
46 float triRot = 0;
47 QMatrix4x4 winProj;
48 } d;
49
50 void Window::customInit()
51 {
52 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData) + sizeof(triangleData));
53 d.vbuf->create();
54 d.releasePool << d.vbuf;
55
56 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
57 d.ibuf->create();
58 d.releasePool << d.ibuf;
59
60 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
61 d.ubuf->create();
62 d.releasePool << d.ubuf;
63
64 d.rb = m_r->newRenderBuffer(QRhiRenderBuffer::Color, QSize(512, 512), 4); // 4x MSAA
65 d.rb->create();
66 d.releasePool << d.rb;
67
68 // the non-msaa texture that will be the destination in the resolve
69 d.tex = m_r->newTexture(QRhiTexture::RGBA8, d.rb->pixelSize(), 1, QRhiTexture::RenderTarget);
70 d.releasePool << d.tex;
71 d.tex->create();
72
73 // rb is multisample, instead of writing out the msaa data into it,
74 // resolve into d.tex at the end of each render pass
75 QRhiTextureRenderTargetDescription rtDesc;
76 QRhiColorAttachment rtAtt(d.rb);
77 rtAtt.setResolveTexture(d.tex);
78 rtDesc.setColorAttachments({ rtAtt });
79
80 d.rt = m_r->newTextureRenderTarget(rtDesc);
81 d.releasePool << d.rt;
82 d.rtRp = d.rt->newCompatibleRenderPassDescriptor();
83 d.releasePool << d.rtRp;
84 d.rt->setRenderPassDescriptor(d.rtRp);
85 d.rt->create();
86
87 d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
88 d.releasePool << d.triUbuf;
89 d.triUbuf->create();
90
91 d.triSrb = m_r->newShaderResourceBindings();
92 d.releasePool << d.triSrb;
93 d.triSrb->setBindings({
94 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf)
95 });
96 d.triSrb->create();
97
98 d.triPs = m_r->newGraphicsPipeline();
99 d.releasePool << d.triPs;
100 d.triPs->setSampleCount(4); // must match the render target
101 d.triPs->setShaderStages({
102 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/color.vert.qsb")) },
103 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/color.frag.qsb")) }
104 });
105 QRhiVertexInputLayout inputLayout;
106 inputLayout.setBindings({
107 { 5 * sizeof(float) }
108 });
109 inputLayout.setAttributes({
110 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
111 { 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) }
112 });
113 d.triPs->setVertexInputLayout(inputLayout);
114 d.triPs->setShaderResourceBindings(d.triSrb);
115 d.triPs->setRenderPassDescriptor(d.rtRp);
116 d.triPs->create();
117
118 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
119 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
120 d.releasePool << d.sampler;
121 d.sampler->create();
122
123 d.srb = m_r->newShaderResourceBindings();
124 d.releasePool << d.srb;
125 d.srb->setBindings({
126 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
127 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
128 });
129 d.srb->create();
130
131 d.ps = m_r->newGraphicsPipeline();
132 d.releasePool << d.ps;
133 d.ps->setShaderStages({
134 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
135 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
136 });
137 inputLayout.setBindings({
138 { 4 * sizeof(float) }
139 });
140 inputLayout.setAttributes({
141 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
142 { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
143 });
144 d.ps->setVertexInputLayout(inputLayout);
145 d.ps->setShaderResourceBindings(d.srb);
146 d.ps->setRenderPassDescriptor(m_rp);
147 d.ps->create();
148
149 d.initialUpdates = m_r->nextResourceUpdateBatch();
150 d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(vertexData), vertexData);
151 d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(vertexData), sizeof(triangleData), triangleData);
152 d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData);
153
154 d.triBaseMvp = m_r->clipSpaceCorrMatrix();
155 d.triBaseMvp.perspective(45.0f, d.rb->pixelSize().width() / float(d.rb->pixelSize().height()), 0.01f, 1000.0f);
156 d.triBaseMvp.translate(0, 0, -2);
157 float opacity = 1.0f;
158 d.initialUpdates->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity);
159
160 qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
161 d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
162 }
163
164 void Window::customRelease()
165 {
166 qDeleteAll(d.releasePool);
167 d.releasePool.clear();
168 }
169
170 void Window::customRender()
171 {
172 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
173 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
174 if (d.initialUpdates) {
175 u->merge(d.initialUpdates);
176 d.initialUpdates->release();
177 d.initialUpdates = nullptr;
178 }
179
180 QMatrix4x4 triMvp = d.triBaseMvp;
181 triMvp.rotate(d.triRot, 0, 1, 0);
182 d.triRot += 1;
183 u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
184
185 if (d.winProj != m_proj) {
186 d.winProj = m_proj;
187 QMatrix4x4 mvp = m_proj;
188 mvp.scale(2.5f);
189 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
190 }
191
192 // offscreen (triangle, msaa)
193 cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u);
194 cb->setGraphicsPipeline(d.triPs);
195 cb->setViewport({ 0, 0, float(d.rb->pixelSize().width()), float(d.rb->pixelSize().height()) });
196 cb->setShaderResources();
197 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, quint32(sizeof(vertexData)));
198 cb->setVertexInput(0, 1, &vbufBinding);
199 cb->draw(3);
200 cb->endPass();
201
202 // onscreen (quad)
203 const QSize outputSizeInPixels = m_sc->currentPixelSize();
204 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 });
205 cb->setGraphicsPipeline(d.ps);
206 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
207 cb->setShaderResources();
208 vbufBinding.second = 0;
209 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
210 cb->drawIndexed(6);
211 cb->endPass();
212 }