Debian Codegraph @

tests/manual/rhi/computeimage/computeimage.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 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 // Compute shader example with image load/store. The texture sampled in the
54 // fragment shader is generated by the compute shader.
55
56 struct {
57 QVector<QRhiResource *> releasePool;
58
59 QRhiTexture *texIn = nullptr;
60 QRhiTexture *texOut = nullptr;
61 QRhiBuffer *computeUBuf = nullptr;
62 QRhiShaderResourceBindings *computeBindings = nullptr;
63 QRhiComputePipeline *computePipeline = nullptr;
64
65 QRhiBuffer *vbuf = nullptr;
66 QRhiBuffer *ibuf = nullptr;
67 QRhiBuffer *ubuf = nullptr;
68 QRhiSampler *sampler = nullptr;
69 QRhiShaderResourceBindings *srb = nullptr;
70 QRhiGraphicsPipeline *ps = nullptr;
71
72 QRhiResourceUpdateBatch *initialUpdates = nullptr;
73 QSize imageSize;
74 QMatrix4x4 winProj;
75 float factor = 1.0f;
76 } d;
77
78 static float quadVertexData[] =
79 { // Y up, CCW
80 -0.5f, 0.5f, 0.0f, 0.0f,
81 -0.5f, -0.5f, 0.0f, 1.0f,
82 0.5f, -0.5f, 1.0f, 1.0f,
83 0.5f, 0.5f, 1.0f, 0.0f
84 };
85
86 static quint16 quadIndexData[] =
87 {
88 0, 1, 2, 0, 2, 3
89 };
90
91 void Window::customInit()
92 {
93 if (!m_r->isFeatureSupported(QRhi::Compute))
94 qFatal("Compute is not supported");
95
96 d.initialUpdates = m_r->nextResourceUpdateBatch();
97
98 // compute pass
99
100 const QImage image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888);
101 d.imageSize = image.size();
102 d.texIn = m_r->newTexture(QRhiTexture::RGBA8, d.imageSize, 1, QRhiTexture::UsedWithLoadStore);
103 d.texIn->build();
104 d.releasePool << d.texIn;
105
106 d.texOut = m_r->newTexture(QRhiTexture::RGBA8, d.imageSize, 1, QRhiTexture::UsedWithLoadStore);
107 d.texOut->build();
108 d.releasePool << d.texOut;
109
110 d.initialUpdates->uploadTexture(d.texIn, image);
111
112 d.computeUBuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 4);
113 d.computeUBuf->build();
114 d.releasePool << d.computeUBuf;
115
116 d.computeBindings = m_r->newShaderResourceBindings();
117 d.computeBindings->setBindings({
118 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf),
119 QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texIn, 0),
120 QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texOut, 0)
121 });
122 d.computeBindings->build();
123 d.releasePool << d.computeBindings;
124
125 d.computePipeline = m_r->newComputePipeline();
126 d.computePipeline->setShaderResourceBindings(d.computeBindings);
127 d.computePipeline->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/image.comp.qsb")) });
128 d.computePipeline->build();
129 d.releasePool << d.computePipeline;
130
131 // graphics pass
132
133 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData));
134 d.vbuf->build();
135 d.releasePool << d.vbuf;
136
137 d.initialUpdates->uploadStaticBuffer(d.vbuf, quadVertexData);
138
139 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData));
140 d.ibuf->build();
141 d.releasePool << d.ibuf;
142
143 d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData);
144
145 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
146 d.ubuf->build();
147 d.releasePool << d.ubuf;
148
149 qint32 flip = 0; // regardless of isYUpInFramebuffer() since the input is not flipped so the end result is good for GL too
150 d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
151
152 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
153 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
154 d.releasePool << d.sampler;
155 d.sampler->build();
156
157 d.srb = m_r->newShaderResourceBindings();
158 d.releasePool << d.srb;
159 d.srb->setBindings({
160 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
161 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.texOut, d.sampler)
162 });
163 d.srb->build();
164
165 d.ps = m_r->newGraphicsPipeline();
166 d.releasePool << d.ps;
167 d.ps->setShaderStages({
168 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
169 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
170 });
171 QRhiVertexInputLayout inputLayout;
172 inputLayout.setBindings({
173 { 4 * sizeof(float) }
174 });
175 inputLayout.setAttributes({
176 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
177 { 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) }
178 });
179 d.ps->setVertexInputLayout(inputLayout);
180 d.ps->setShaderResourceBindings(d.srb);
181 d.ps->setRenderPassDescriptor(m_rp);
182 d.ps->build();
183 }
184
185 void Window::customRelease()
186 {
187 qDeleteAll(d.releasePool);
188 d.releasePool.clear();
189 }
190
191 void Window::customRender()
192 {
193 const QSize outputSizeInPixels = m_sc->currentPixelSize();
194 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
195 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
196 if (d.initialUpdates) {
197 u->merge(d.initialUpdates);
198 d.initialUpdates->release();
199 d.initialUpdates = nullptr;
200 }
201
202 if (d.winProj != m_proj) {
203 d.winProj = m_proj;
204 QMatrix4x4 mvp = m_proj;
205 mvp.scale(2.5f);
206 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
207 }
208
209 u->updateDynamicBuffer(d.computeUBuf, 0, 4, &d.factor);
210 d.factor += 0.1f;
211 if (d.factor >= 50.0f)
212 d.factor = 1.0f;
213
214 cb->beginComputePass(u);
215 cb->setComputePipeline(d.computePipeline);
216 cb->setShaderResources();
217 cb->dispatch(d.imageSize.width() / 16, d.imageSize.height() / 16, 1);
218 cb->endComputePass();
219
220 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 });
221 cb->setGraphicsPipeline(d.ps);
222 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
223 cb->setShaderResources();
224 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
225 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
226 cb->drawIndexed(6);
227 cb->endPass();
228 }