Debian Codegraph @

tests/manual/rhi/computebuffer/computebuffer.cpp

raw
1 // Copyright (C) 2019 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5 #include <QRandomGenerator>
6
7 // Compute shader example. Writes to a storage buffer from a compute shader,
8 // then uses the same buffer as vertex buffer in the vertex stage. This would
9 // be typical when implementing particles for example. Here we just simply move
10 // the positions back and forth along the X axis.
11
12 // Note that the example relies on gl_PointSize which is not supported
13 // everywhere. So in some cases the points will be of size 1.
14
15 struct {
16 QList<QRhiResource *> releasePool;
17 QRhiBuffer *sbuf = nullptr;
18 QRhiBuffer *computeUniBuf = nullptr;
19 QRhiShaderResourceBindings *computeBindings = nullptr;
20 QRhiComputePipeline *computePipeline = nullptr;
21 QRhiShaderResourceBindings *graphicsBindings = nullptr;
22 QRhiGraphicsPipeline *graphicsPipeline = nullptr;
23 QRhiResourceUpdateBatch *initialUpdates = nullptr;
24 float step = 0.2f;
25 } d;
26
27 // these struct must match the std140 packing rules
28 struct Data {
29 float pos[2];
30 float dir;
31 quint32 pad[1];
32 };
33 struct ComputeUBuf {
34 float step;
35 quint32 count;
36 };
37
38 const int DATA_COUNT = 256 * 128;
39
40 const int COMPUTE_UBUF_SIZE = 8;
41
42 void Window::customInit()
43 {
44 if (!m_r->isFeatureSupported(QRhi::Compute))
45 qFatal("Compute is not supported");
46
47 if (!m_r->isFeatureSupported(QRhi::VertexShaderPointSize))
48 qWarning("Point sizes other than 1 not supported");
49
50 // compute pass
51
52 d.sbuf = m_r->newBuffer(QRhiBuffer::Immutable,
53 QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
54 sizeof(Data) * DATA_COUNT);
55 d.sbuf->create();
56 d.releasePool << d.sbuf;
57
58 d.computeUniBuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, COMPUTE_UBUF_SIZE);
59 d.computeUniBuf->create();
60 d.releasePool << d.computeUniBuf;
61
62 d.initialUpdates = m_r->nextResourceUpdateBatch();
63
64 QByteArray data;
65 data.resize(sizeof(Data) * DATA_COUNT);
66 Data *p = reinterpret_cast<Data *>(data.data());
67 QRandomGenerator *rgen = QRandomGenerator::global();
68 for (int i = 0; i < DATA_COUNT; ++i) {
69 p->pos[0] = rgen->bounded(1000) / 500.0f - 1.0f;
70 p->pos[1] = rgen->bounded(1000) / 500.0f - 1.0f;
71 p->dir = rgen->bounded(2) ? 1 : -1;
72 ++p;
73 }
74 d.initialUpdates->uploadStaticBuffer(d.sbuf, data.constData());
75
76 ComputeUBuf ud;
77 ud.step = d.step;
78 ud.count = DATA_COUNT;
79 d.initialUpdates->updateDynamicBuffer(d.computeUniBuf, 0, COMPUTE_UBUF_SIZE, &ud);
80
81 d.computeBindings = m_r->newShaderResourceBindings();
82 d.computeBindings->setBindings({
83 QRhiShaderResourceBinding::bufferLoadStore(0, QRhiShaderResourceBinding::ComputeStage, d.sbuf),
84 QRhiShaderResourceBinding::uniformBuffer(1, QRhiShaderResourceBinding::ComputeStage, d.computeUniBuf)
85 });
86 d.computeBindings->create();
87 d.releasePool << d.computeBindings;
88
89 d.computePipeline = m_r->newComputePipeline();
90 d.computePipeline->setShaderResourceBindings(d.computeBindings);
91 d.computePipeline->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/buffer.comp.qsb")) });
92 d.computePipeline->create();
93 d.releasePool << d.computePipeline;
94
95 // graphics pass
96
97 d.graphicsBindings = m_r->newShaderResourceBindings();
98 d.graphicsBindings->create();
99 d.releasePool << d.graphicsBindings;
100
101 d.graphicsPipeline = m_r->newGraphicsPipeline();
102 d.graphicsPipeline->setTopology(QRhiGraphicsPipeline::Points);
103 d.graphicsPipeline->setShaderStages({
104 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/main.vert.qsb")) },
105 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/main.frag.qsb")) }
106 });
107 QRhiVertexInputLayout inputLayout;
108 inputLayout.setBindings({
109 { 2 * sizeof(float) }
110 });
111 inputLayout.setAttributes({
112 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
113 });
114 d.graphicsPipeline->setVertexInputLayout(inputLayout);
115 d.graphicsPipeline->setShaderResourceBindings(d.graphicsBindings);
116 d.graphicsPipeline->setRenderPassDescriptor(m_rp);
117 d.graphicsPipeline->create();
118 d.releasePool << d.graphicsPipeline;
119 }
120
121 void Window::customRelease()
122 {
123 qDeleteAll(d.releasePool);
124 d.releasePool.clear();
125 }
126
127 void Window::customRender()
128 {
129 const QSize outputSizeInPixels = m_sc->currentPixelSize();
130 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
131 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
132 if (d.initialUpdates) {
133 u->merge(d.initialUpdates);
134 d.initialUpdates->release();
135 d.initialUpdates = nullptr;
136 }
137
138 #if 0
139 u->updateDynamicBuffer(d.computeUniBuf, 0, sizeof(float), &d.step);
140 d.step += 0.01f;
141 #endif
142
143 // compute pass
144 cb->beginComputePass(u);
145 cb->setComputePipeline(d.computePipeline);
146 cb->setShaderResources();
147 cb->dispatch(DATA_COUNT / 256, 1, 1);
148 cb->endComputePass();
149
150 // graphics pass
151 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 });
152 cb->setGraphicsPipeline(d.graphicsPipeline);
153 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
154 QRhiCommandBuffer::VertexInput vbufBinding(d.sbuf, 0);
155 cb->setVertexInput(0, 1, &vbufBinding);
156 cb->draw(DATA_COUNT);
157 cb->endPass();
158 }