| 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 |
#include <QRandomGenerator> |
| 53 |
|
| 54 |
// Compute shader example. Writes to a storage buffer from a compute shader, |
| 55 |
// then uses the same buffer as vertex buffer in the vertex stage. This would |
| 56 |
// be typical when implementing particles for example. Here we just simply move |
| 57 |
// the positions back and forth along the X axis. |
| 58 |
|
| 59 |
// Note that the example relies on gl_PointSize which is not supported |
| 60 |
// everywhere. So in some cases the points will be of size 1. |
| 61 |
|
| 62 |
struct { |
| 63 |
QVector<QRhiResource *> releasePool; |
| 64 |
QRhiBuffer *sbuf = nullptr; |
| 65 |
QRhiBuffer *computeUniBuf = nullptr; |
| 66 |
QRhiShaderResourceBindings *computeBindings = nullptr; |
| 67 |
QRhiComputePipeline *computePipeline = nullptr; |
| 68 |
QRhiShaderResourceBindings *graphicsBindings = nullptr; |
| 69 |
QRhiGraphicsPipeline *graphicsPipeline = nullptr; |
| 70 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 71 |
float step = 0.2f; |
| 72 |
} d; |
| 73 |
|
| 74 |
// these struct must match the std140 packing rules |
| 75 |
struct Data { |
| 76 |
float pos[2]; |
| 77 |
float dir; |
| 78 |
quint32 pad[1]; |
| 79 |
}; |
| 80 |
struct ComputeUBuf { |
| 81 |
float step; |
| 82 |
quint32 count; |
| 83 |
}; |
| 84 |
|
| 85 |
const int DATA_COUNT = 256 * 128; |
| 86 |
|
| 87 |
const int COMPUTE_UBUF_SIZE = 8; |
| 88 |
|
| 89 |
void Window::customInit() |
| 90 |
{ |
| 91 |
if (!m_r->isFeatureSupported(QRhi::Compute)) |
| 92 |
qFatal("Compute is not supported"); |
| 93 |
|
| 94 |
if (!m_r->isFeatureSupported(QRhi::VertexShaderPointSize)) |
| 95 |
qWarning("Point sizes other than 1 not supported"); |
| 96 |
|
| 97 |
// compute pass |
| 98 |
|
| 99 |
d.sbuf = m_r->newBuffer(QRhiBuffer::Immutable, |
| 100 |
QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer, |
| 101 |
sizeof(Data) * DATA_COUNT); |
| 102 |
d.sbuf->build(); |
| 103 |
d.releasePool << d.sbuf; |
| 104 |
|
| 105 |
d.computeUniBuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, COMPUTE_UBUF_SIZE); |
| 106 |
d.computeUniBuf->build(); |
| 107 |
d.releasePool << d.computeUniBuf; |
| 108 |
|
| 109 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 110 |
|
| 111 |
QByteArray data; |
| 112 |
data.resize(sizeof(Data) * DATA_COUNT); |
| 113 |
Data *p = reinterpret_cast<Data *>(data.data()); |
| 114 |
QRandomGenerator *rgen = QRandomGenerator::global(); |
| 115 |
for (int i = 0; i < DATA_COUNT; ++i) { |
| 116 |
p->pos[0] = rgen->bounded(1000) / 500.0f - 1.0f; |
| 117 |
p->pos[1] = rgen->bounded(1000) / 500.0f - 1.0f; |
| 118 |
p->dir = rgen->bounded(2) ? 1 : -1; |
| 119 |
++p; |
| 120 |
} |
| 121 |
d.initialUpdates->uploadStaticBuffer(d.sbuf, data.constData()); |
| 122 |
|
| 123 |
ComputeUBuf ud; |
| 124 |
ud.step = d.step; |
| 125 |
ud.count = DATA_COUNT; |
| 126 |
d.initialUpdates->updateDynamicBuffer(d.computeUniBuf, 0, COMPUTE_UBUF_SIZE, &ud); |
| 127 |
|
| 128 |
d.computeBindings = m_r->newShaderResourceBindings(); |
| 129 |
d.computeBindings->setBindings({ |
| 130 |
QRhiShaderResourceBinding::bufferLoadStore(0, QRhiShaderResourceBinding::ComputeStage, d.sbuf), |
| 131 |
QRhiShaderResourceBinding::uniformBuffer(1, QRhiShaderResourceBinding::ComputeStage, d.computeUniBuf) |
| 132 |
}); |
| 133 |
d.computeBindings->build(); |
| 134 |
d.releasePool << d.computeBindings; |
| 135 |
|
| 136 |
d.computePipeline = m_r->newComputePipeline(); |
| 137 |
d.computePipeline->setShaderResourceBindings(d.computeBindings); |
| 138 |
d.computePipeline->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/buffer.comp.qsb")) }); |
| 139 |
d.computePipeline->build(); |
| 140 |
d.releasePool << d.computePipeline; |
| 141 |
|
| 142 |
// graphics pass |
| 143 |
|
| 144 |
d.graphicsBindings = m_r->newShaderResourceBindings(); |
| 145 |
d.graphicsBindings->build(); |
| 146 |
d.releasePool << d.graphicsBindings; |
| 147 |
|
| 148 |
d.graphicsPipeline = m_r->newGraphicsPipeline(); |
| 149 |
d.graphicsPipeline->setTopology(QRhiGraphicsPipeline::Points); |
| 150 |
d.graphicsPipeline->setShaderStages({ |
| 151 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/main.vert.qsb")) }, |
| 152 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/main.frag.qsb")) } |
| 153 |
}); |
| 154 |
QRhiVertexInputLayout inputLayout; |
| 155 |
inputLayout.setBindings({ |
| 156 |
{ 2 * sizeof(float) } |
| 157 |
}); |
| 158 |
inputLayout.setAttributes({ |
| 159 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 160 |
}); |
| 161 |
d.graphicsPipeline->setVertexInputLayout(inputLayout); |
| 162 |
d.graphicsPipeline->setShaderResourceBindings(d.graphicsBindings); |
| 163 |
d.graphicsPipeline->setRenderPassDescriptor(m_rp); |
| 164 |
d.graphicsPipeline->build(); |
| 165 |
d.releasePool << d.graphicsPipeline; |
| 166 |
} |
| 167 |
|
| 168 |
void Window::customRelease() |
| 169 |
{ |
| 170 |
qDeleteAll(d.releasePool); |
| 171 |
d.releasePool.clear(); |
| 172 |
} |
| 173 |
|
| 174 |
void Window::customRender() |
| 175 |
{ |
| 176 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 177 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 178 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 179 |
if (d.initialUpdates) { |
| 180 |
u->merge(d.initialUpdates); |
| 181 |
d.initialUpdates->release(); |
| 182 |
d.initialUpdates = nullptr; |
| 183 |
} |
| 184 |
|
| 185 |
#if 0 |
| 186 |
u->updateDynamicBuffer(d.computeUniBuf, 0, sizeof(float), &d.step); |
| 187 |
d.step += 0.01f; |
| 188 |
#endif |
| 189 |
|
| 190 |
// compute pass |
| 191 |
cb->beginComputePass(u); |
| 192 |
cb->setComputePipeline(d.computePipeline); |
| 193 |
cb->setShaderResources(); |
| 194 |
cb->dispatch(DATA_COUNT / 256, 1, 1); |
| 195 |
cb->endComputePass(); |
| 196 |
|
| 197 |
// graphics pass |
| 198 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); |
| 199 |
cb->setGraphicsPipeline(d.graphicsPipeline); |
| 200 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 201 |
QRhiCommandBuffer::VertexInput vbufBinding(d.sbuf, 0); |
| 202 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 203 |
cb->draw(DATA_COUNT); |
| 204 |
cb->endPass(); |
| 205 |
} |