| 1 |
// Copyright (C) 2020 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
// An advanced version of floattexture. Instead of RGBA32F, we use RGBA16F, and |
| 5 |
// also generate the floating point data from rgba with compute. Then there's a |
| 6 |
// compute pass using the BSDF prefiltering taken from Qt Quick 3D, which |
| 7 |
// generates all the mip levels. |
| 8 |
|
| 9 |
// Why do we animate the scale of the quad rendered to the window? To have |
| 10 |
// different mip levels used, to prove that all of them are generated |
| 11 |
// correctly, without artifacts (which would occur if memory barriers were not |
| 12 |
// correctly generated by QRhi). For full verification use RenderDoc or similar. |
| 13 |
|
| 14 |
#include "../shared/examplefw.h" |
| 15 |
#include <qmath.h> |
| 16 |
|
| 17 |
static float vertexData[] = |
| 18 |
{ // Y up, CCW |
| 19 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 20 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 21 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 22 |
0.5f, 0.5f, 1.0f, 0.0f |
| 23 |
}; |
| 24 |
|
| 25 |
static quint16 indexData[] = |
| 26 |
{ |
| 27 |
0, 1, 2, 0, 2, 3 |
| 28 |
}; |
| 29 |
|
| 30 |
static const int MAX_MIP_LEVELS = 20; |
| 31 |
|
| 32 |
struct { |
| 33 |
QList<QRhiResource *> releasePool; |
| 34 |
|
| 35 |
QRhiBuffer *vbuf = nullptr; |
| 36 |
QRhiBuffer *ibuf = nullptr; |
| 37 |
QRhiBuffer *ubuf = nullptr; |
| 38 |
QRhiTexture *texRgba = nullptr; |
| 39 |
QRhiTexture *texFloat16 = nullptr; |
| 40 |
QRhiSampler *sampler = nullptr; |
| 41 |
QRhiShaderResourceBindings *srb = nullptr; |
| 42 |
QRhiGraphicsPipeline *ps = nullptr; |
| 43 |
|
| 44 |
QRhiBuffer *computeUBuf_load = nullptr; |
| 45 |
QRhiShaderResourceBindings *computeBindings_load = nullptr; |
| 46 |
QRhiComputePipeline *computePipeline_load = nullptr; |
| 47 |
QRhiBuffer *computeUBuf_prefilter = nullptr; |
| 48 |
QRhiShaderResourceBindings *computeBindings_prefilter[MAX_MIP_LEVELS]; |
| 49 |
QRhiComputePipeline *computePipeline_prefilter = nullptr; |
| 50 |
|
| 51 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 52 |
bool computeDone = false; |
| 53 |
int mipCount; |
| 54 |
int prefilterUBufElemSize; |
| 55 |
quint32 prefilterNumWorkGroups[MAX_MIP_LEVELS][3]; |
| 56 |
float scale = 2.5f; |
| 57 |
int scale_dir = -1; |
| 58 |
} d; |
| 59 |
|
| 60 |
void recordUploadThenFilterFloat16TextureWithCompute(QRhiCommandBuffer *cb) |
| 61 |
{ |
| 62 |
const int w = d.texRgba->pixelSize().width() / 16; |
| 63 |
const int h = d.texRgba->pixelSize().height() / 16; |
| 64 |
|
| 65 |
cb->beginComputePass(); |
| 66 |
|
| 67 |
cb->setComputePipeline(d.computePipeline_load); |
| 68 |
cb->setShaderResources(); |
| 69 |
cb->dispatch(w, h, 1); |
| 70 |
|
| 71 |
cb->setComputePipeline(d.computePipeline_prefilter); |
| 72 |
for (int level = 1; level < d.mipCount; ++level) { |
| 73 |
const int i = level - 1; |
| 74 |
const int mipW = d.prefilterNumWorkGroups[i][0]; |
| 75 |
const int mipH = d.prefilterNumWorkGroups[i][1]; |
| 76 |
QPair<int, quint32> dynamicOffset = { 0, quint32(d.prefilterUBufElemSize * i) }; |
| 77 |
cb->setShaderResources(d.computeBindings_prefilter[i], 1, &dynamicOffset); |
| 78 |
cb->dispatch(mipW, mipH, 1); |
| 79 |
} |
| 80 |
|
| 81 |
cb->endComputePass(); |
| 82 |
} |
| 83 |
|
| 84 |
void Window::customInit() |
| 85 |
{ |
| 86 |
if (!m_r->isFeatureSupported(QRhi::Compute)) |
| 87 |
qFatal("Compute is not supported"); |
| 88 |
|
| 89 |
if (!m_r->isTextureFormatSupported(QRhiTexture::RGBA16F)) |
| 90 |
qFatal("RGBA16F texture format is not supported"); |
| 91 |
|
| 92 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 93 |
|
| 94 |
// load rgba8 image data |
| 95 |
|
| 96 |
QImage image; |
| 97 |
image.load(QLatin1String(":/qt256.png")); |
| 98 |
image = image.convertToFormat(QImage::Format_RGBA8888); |
| 99 |
Q_ASSERT(!image.isNull()); |
| 100 |
d.texRgba = m_r->newTexture(QRhiTexture::RGBA8, image.size(), 1, QRhiTexture::UsedWithLoadStore); |
| 101 |
d.texRgba->create(); |
| 102 |
d.releasePool << d.texRgba; |
| 103 |
|
| 104 |
d.initialUpdates->uploadTexture(d.texRgba, image); |
| 105 |
|
| 106 |
d.mipCount = m_r->mipLevelsForSize(image.size()); |
| 107 |
Q_ASSERT(d.mipCount <= MAX_MIP_LEVELS); |
| 108 |
|
| 109 |
d.texFloat16 = m_r->newTexture(QRhiTexture::RGBA16F, image.size(), 1, QRhiTexture::UsedWithLoadStore | QRhiTexture::MipMapped); |
| 110 |
d.releasePool << d.texFloat16; |
| 111 |
d.texFloat16->create(); |
| 112 |
|
| 113 |
// compute |
| 114 |
|
| 115 |
d.computeUBuf_load = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 12); |
| 116 |
d.computeUBuf_load->create(); |
| 117 |
d.releasePool << d.computeUBuf_load; |
| 118 |
|
| 119 |
quint32 numWorkGroups[3] = { quint32(image.width()), quint32(image.height()), 0 }; |
| 120 |
d.initialUpdates->updateDynamicBuffer(d.computeUBuf_load, 0, 12, numWorkGroups); |
| 121 |
|
| 122 |
d.computeBindings_load = m_r->newShaderResourceBindings(); |
| 123 |
d.computeBindings_load->setBindings({ |
| 124 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf_load), |
| 125 |
QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texRgba, 0), |
| 126 |
QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, 0) |
| 127 |
}); |
| 128 |
d.computeBindings_load->create(); |
| 129 |
d.releasePool << d.computeBindings_load; |
| 130 |
|
| 131 |
d.computePipeline_load = m_r->newComputePipeline(); |
| 132 |
d.computePipeline_load->setShaderResourceBindings(d.computeBindings_load); |
| 133 |
d.computePipeline_load->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/load.comp.qsb")) }); |
| 134 |
d.computePipeline_load->create(); |
| 135 |
d.releasePool << d.computePipeline_load; |
| 136 |
|
| 137 |
d.prefilterUBufElemSize = m_r->ubufAligned(12); |
| 138 |
d.computeUBuf_prefilter = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, d.prefilterUBufElemSize * d.mipCount); |
| 139 |
d.computeUBuf_prefilter->create(); |
| 140 |
d.releasePool << d.computeUBuf_prefilter; |
| 141 |
|
| 142 |
int mipW = image.width() >> 1; |
| 143 |
int mipH = image.height() >> 1; |
| 144 |
for (int level = 1; level < d.mipCount; ++level) { |
| 145 |
const int i = level - 1; |
| 146 |
d.prefilterNumWorkGroups[i][0] = quint32(mipW); |
| 147 |
d.prefilterNumWorkGroups[i][1] = quint32(mipH); |
| 148 |
d.prefilterNumWorkGroups[i][2] = 0; |
| 149 |
d.initialUpdates->updateDynamicBuffer(d.computeUBuf_prefilter, d.prefilterUBufElemSize * i, 12, d.prefilterNumWorkGroups[i]); |
| 150 |
mipW = mipW > 2 ? mipW >> 1 : 1; |
| 151 |
mipH = mipH > 2 ? mipH >> 1 : 1; |
| 152 |
|
| 153 |
d.computeBindings_prefilter[i] = m_r->newShaderResourceBindings(); |
| 154 |
d.computeBindings_prefilter[i]->setBindings({ |
| 155 |
QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf_prefilter, 12), |
| 156 |
QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, level - 1), |
| 157 |
QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, level) |
| 158 |
}); |
| 159 |
d.computeBindings_prefilter[i]->create(); |
| 160 |
d.releasePool << d.computeBindings_prefilter[i]; |
| 161 |
} |
| 162 |
|
| 163 |
d.computePipeline_prefilter = m_r->newComputePipeline(); |
| 164 |
d.computePipeline_prefilter->setShaderResourceBindings(d.computeBindings_prefilter[0]); // just need a layout compatible one |
| 165 |
d.computePipeline_prefilter->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/prefilter.comp.qsb")) }); |
| 166 |
d.computePipeline_prefilter->create(); |
| 167 |
d.releasePool << d.computePipeline_prefilter; |
| 168 |
|
| 169 |
// graphics |
| 170 |
|
| 171 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); |
| 172 |
d.vbuf->create(); |
| 173 |
d.releasePool << d.vbuf; |
| 174 |
|
| 175 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData)); |
| 176 |
d.ibuf->create(); |
| 177 |
d.releasePool << d.ibuf; |
| 178 |
|
| 179 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 180 |
d.ubuf->create(); |
| 181 |
d.releasePool << d.ubuf; |
| 182 |
|
| 183 |
// enable mipmaps |
| 184 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear, |
| 185 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 186 |
d.releasePool << d.sampler; |
| 187 |
d.sampler->create(); |
| 188 |
|
| 189 |
d.srb = m_r->newShaderResourceBindings(); |
| 190 |
d.releasePool << d.srb; |
| 191 |
d.srb->setBindings({ |
| 192 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 193 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.texFloat16, d.sampler) |
| 194 |
}); |
| 195 |
d.srb->create(); |
| 196 |
|
| 197 |
d.ps = m_r->newGraphicsPipeline(); |
| 198 |
d.releasePool << d.ps; |
| 199 |
d.ps->setShaderStages({ |
| 200 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 201 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 202 |
}); |
| 203 |
QRhiVertexInputLayout inputLayout; |
| 204 |
inputLayout.setBindings({ |
| 205 |
{ 4 * sizeof(float) } |
| 206 |
}); |
| 207 |
inputLayout.setAttributes({ |
| 208 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 209 |
{ 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) } |
| 210 |
}); |
| 211 |
d.ps->setVertexInputLayout(inputLayout); |
| 212 |
d.ps->setShaderResourceBindings(d.srb); |
| 213 |
d.ps->setRenderPassDescriptor(m_rp); |
| 214 |
d.ps->create(); |
| 215 |
|
| 216 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, vertexData); |
| 217 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData); |
| 218 |
|
| 219 |
qint32 flip = 0; |
| 220 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 221 |
} |
| 222 |
|
| 223 |
void Window::customRelease() |
| 224 |
{ |
| 225 |
qDeleteAll(d.releasePool); |
| 226 |
d.releasePool.clear(); |
| 227 |
} |
| 228 |
|
| 229 |
void Window::customRender() |
| 230 |
{ |
| 231 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 232 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 233 |
if (d.initialUpdates) { |
| 234 |
u->merge(d.initialUpdates); |
| 235 |
d.initialUpdates->release(); |
| 236 |
d.initialUpdates = nullptr; |
| 237 |
} |
| 238 |
|
| 239 |
QMatrix4x4 mvp = m_proj; |
| 240 |
mvp.scale(d.scale); |
| 241 |
d.scale += d.scale_dir * 0.01f; |
| 242 |
if (qFuzzyIsNull(d.scale) || d.scale >= 2.5f) |
| 243 |
d.scale_dir *= -1; |
| 244 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 245 |
|
| 246 |
cb->resourceUpdate(u); |
| 247 |
|
| 248 |
// If not yet done, then do a compute pass that uploads level 0, doing an |
| 249 |
// rgba8 -> float16 conversion. Follow that with another compute pass to do |
| 250 |
// the filtering and generate all the mip levels. |
| 251 |
if (!d.computeDone) { |
| 252 |
recordUploadThenFilterFloat16TextureWithCompute(cb); |
| 253 |
d.computeDone = true; |
| 254 |
} |
| 255 |
|
| 256 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 257 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); |
| 258 |
cb->setGraphicsPipeline(d.ps); |
| 259 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 260 |
cb->setShaderResources(); |
| 261 |
const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 262 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 263 |
cb->drawIndexed(6); |
| 264 |
cb->endPass(); |
| 265 |
} |