| 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 |
|
| 6 |
// Compute shader example with image load/store. The texture sampled in the |
| 7 |
// fragment shader is generated by the compute shader. |
| 8 |
|
| 9 |
struct { |
| 10 |
QList<QRhiResource *> releasePool; |
| 11 |
|
| 12 |
QRhiTexture *texIn = nullptr; |
| 13 |
QRhiTexture *texOut = nullptr; |
| 14 |
QRhiBuffer *computeUBuf = nullptr; |
| 15 |
QRhiShaderResourceBindings *computeBindings = nullptr; |
| 16 |
QRhiComputePipeline *computePipeline = nullptr; |
| 17 |
|
| 18 |
QRhiBuffer *vbuf = nullptr; |
| 19 |
QRhiBuffer *ibuf = nullptr; |
| 20 |
QRhiBuffer *ubuf = nullptr; |
| 21 |
QRhiSampler *sampler = nullptr; |
| 22 |
QRhiShaderResourceBindings *srb = nullptr; |
| 23 |
QRhiGraphicsPipeline *ps = nullptr; |
| 24 |
|
| 25 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 26 |
QSize imageSize; |
| 27 |
QMatrix4x4 winProj; |
| 28 |
float factor = 1.0f; |
| 29 |
} d; |
| 30 |
|
| 31 |
static float quadVertexData[] = |
| 32 |
{ // Y up, CCW |
| 33 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 34 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 35 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 36 |
0.5f, 0.5f, 1.0f, 0.0f |
| 37 |
}; |
| 38 |
|
| 39 |
static quint16 quadIndexData[] = |
| 40 |
{ |
| 41 |
0, 1, 2, 0, 2, 3 |
| 42 |
}; |
| 43 |
|
| 44 |
void Window::customInit() |
| 45 |
{ |
| 46 |
if (!m_r->isFeatureSupported(QRhi::Compute)) |
| 47 |
qFatal("Compute is not supported"); |
| 48 |
|
| 49 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 50 |
|
| 51 |
// compute pass |
| 52 |
|
| 53 |
const QImage image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888); |
| 54 |
d.imageSize = image.size(); |
| 55 |
d.texIn = m_r->newTexture(QRhiTexture::RGBA8, d.imageSize, 1, QRhiTexture::UsedWithLoadStore); |
| 56 |
d.texIn->create(); |
| 57 |
d.releasePool << d.texIn; |
| 58 |
|
| 59 |
d.texOut = m_r->newTexture(QRhiTexture::RGBA8, d.imageSize, 1, QRhiTexture::UsedWithLoadStore); |
| 60 |
d.texOut->create(); |
| 61 |
d.releasePool << d.texOut; |
| 62 |
|
| 63 |
d.initialUpdates->uploadTexture(d.texIn, image); |
| 64 |
|
| 65 |
d.computeUBuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 4); |
| 66 |
d.computeUBuf->create(); |
| 67 |
d.releasePool << d.computeUBuf; |
| 68 |
|
| 69 |
d.computeBindings = m_r->newShaderResourceBindings(); |
| 70 |
d.computeBindings->setBindings({ |
| 71 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf), |
| 72 |
QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texIn, 0), |
| 73 |
QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texOut, 0) |
| 74 |
}); |
| 75 |
d.computeBindings->create(); |
| 76 |
d.releasePool << d.computeBindings; |
| 77 |
|
| 78 |
d.computePipeline = m_r->newComputePipeline(); |
| 79 |
d.computePipeline->setShaderResourceBindings(d.computeBindings); |
| 80 |
d.computePipeline->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/image.comp.qsb")) }); |
| 81 |
d.computePipeline->create(); |
| 82 |
d.releasePool << d.computePipeline; |
| 83 |
|
| 84 |
// graphics pass |
| 85 |
|
| 86 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData)); |
| 87 |
d.vbuf->create(); |
| 88 |
d.releasePool << d.vbuf; |
| 89 |
|
| 90 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, quadVertexData); |
| 91 |
|
| 92 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData)); |
| 93 |
d.ibuf->create(); |
| 94 |
d.releasePool << d.ibuf; |
| 95 |
|
| 96 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData); |
| 97 |
|
| 98 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 99 |
d.ubuf->create(); |
| 100 |
d.releasePool << d.ubuf; |
| 101 |
|
| 102 |
qint32 flip = 0; // regardless of isYUpInFramebuffer() since the input is not flipped so the end result is good for GL too |
| 103 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 104 |
|
| 105 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 106 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 107 |
d.releasePool << d.sampler; |
| 108 |
d.sampler->create(); |
| 109 |
|
| 110 |
d.srb = m_r->newShaderResourceBindings(); |
| 111 |
d.releasePool << d.srb; |
| 112 |
d.srb->setBindings({ |
| 113 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 114 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.texOut, d.sampler) |
| 115 |
}); |
| 116 |
d.srb->create(); |
| 117 |
|
| 118 |
d.ps = m_r->newGraphicsPipeline(); |
| 119 |
d.releasePool << d.ps; |
| 120 |
d.ps->setShaderStages({ |
| 121 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 122 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 123 |
}); |
| 124 |
QRhiVertexInputLayout inputLayout; |
| 125 |
inputLayout.setBindings({ |
| 126 |
{ 4 * sizeof(float) } |
| 127 |
}); |
| 128 |
inputLayout.setAttributes({ |
| 129 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 130 |
{ 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) } |
| 131 |
}); |
| 132 |
d.ps->setVertexInputLayout(inputLayout); |
| 133 |
d.ps->setShaderResourceBindings(d.srb); |
| 134 |
d.ps->setRenderPassDescriptor(m_rp); |
| 135 |
d.ps->create(); |
| 136 |
} |
| 137 |
|
| 138 |
void Window::customRelease() |
| 139 |
{ |
| 140 |
qDeleteAll(d.releasePool); |
| 141 |
d.releasePool.clear(); |
| 142 |
} |
| 143 |
|
| 144 |
void Window::customRender() |
| 145 |
{ |
| 146 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 147 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 148 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 149 |
if (d.initialUpdates) { |
| 150 |
u->merge(d.initialUpdates); |
| 151 |
d.initialUpdates->release(); |
| 152 |
d.initialUpdates = nullptr; |
| 153 |
} |
| 154 |
|
| 155 |
if (d.winProj != m_proj) { |
| 156 |
d.winProj = m_proj; |
| 157 |
QMatrix4x4 mvp = m_proj; |
| 158 |
mvp.scale(2.5f); |
| 159 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 160 |
} |
| 161 |
|
| 162 |
u->updateDynamicBuffer(d.computeUBuf, 0, 4, &d.factor); |
| 163 |
d.factor += 0.1f; |
| 164 |
if (d.factor >= 50.0f) |
| 165 |
d.factor = 1.0f; |
| 166 |
|
| 167 |
cb->beginComputePass(u); |
| 168 |
cb->setComputePipeline(d.computePipeline); |
| 169 |
cb->setShaderResources(); |
| 170 |
cb->dispatch(d.imageSize.width() / 16, d.imageSize.height() / 16, 1); |
| 171 |
cb->endComputePass(); |
| 172 |
|
| 173 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); |
| 174 |
cb->setGraphicsPipeline(d.ps); |
| 175 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 176 |
cb->setShaderResources(); |
| 177 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 178 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 179 |
cb->drawIndexed(6); |
| 180 |
cb->endPass(); |
| 181 |
} |