| 1 |
// Copyright (C) 2018 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
#include <qmath.h> |
| 6 |
|
| 7 |
QByteArray loadHdr(const QString &fn, QSize *size) |
| 8 |
{ |
| 9 |
QFile f(fn); |
| 10 |
if (!f.open(QIODevice::ReadOnly)) { |
| 11 |
qWarning("Failed to open %s", qPrintable(fn)); |
| 12 |
return QByteArray(); |
| 13 |
} |
| 14 |
|
| 15 |
char sig[256]; |
| 16 |
f.read(sig, 11); |
| 17 |
if (strncmp(sig, "#?RADIANCE\n", 11)) |
| 18 |
return QByteArray(); |
| 19 |
|
| 20 |
QByteArray buf = f.readAll(); |
| 21 |
const char *p = buf.constData(); |
| 22 |
const char *pEnd = p + buf.size(); |
| 23 |
|
| 24 |
// Process lines until the empty one. |
| 25 |
QByteArray line; |
| 26 |
while (p < pEnd) { |
| 27 |
char c = *p++; |
| 28 |
if (c == '\n') { |
| 29 |
if (line.isEmpty()) |
| 30 |
break; |
| 31 |
if (line.startsWith(QByteArrayLiteral("FORMAT="))) { |
| 32 |
const QByteArray format = line.mid(7).trimmed(); |
| 33 |
if (format != QByteArrayLiteral("32-bit_rle_rgbe")) { |
| 34 |
qWarning("HDR format '%s' is not supported", format.constData()); |
| 35 |
return QByteArray(); |
| 36 |
} |
| 37 |
} |
| 38 |
line.clear(); |
| 39 |
} else { |
| 40 |
line.append(c); |
| 41 |
} |
| 42 |
} |
| 43 |
if (p == pEnd) { |
| 44 |
qWarning("Malformed HDR image data at property strings"); |
| 45 |
return QByteArray(); |
| 46 |
} |
| 47 |
|
| 48 |
// Get the resolution string. |
| 49 |
while (p < pEnd) { |
| 50 |
char c = *p++; |
| 51 |
if (c == '\n') |
| 52 |
break; |
| 53 |
line.append(c); |
| 54 |
} |
| 55 |
if (p == pEnd) { |
| 56 |
qWarning("Malformed HDR image data at resolution string"); |
| 57 |
return QByteArray(); |
| 58 |
} |
| 59 |
|
| 60 |
int w = 0, h = 0; |
| 61 |
// We only care about the standard orientation. |
| 62 |
if (!sscanf(line.constData(), "-Y %d +X %d", &h, &w)) { |
| 63 |
qWarning("Unsupported HDR resolution string '%s'", line.constData()); |
| 64 |
return QByteArray(); |
| 65 |
} |
| 66 |
if (w <= 0 || h <= 0) { |
| 67 |
qWarning("Invalid HDR resolution"); |
| 68 |
return QByteArray(); |
| 69 |
} |
| 70 |
|
| 71 |
// output is RGBA32F |
| 72 |
const int blockSize = 4 * sizeof(float); |
| 73 |
QByteArray data; |
| 74 |
data.resize(w * h * blockSize); |
| 75 |
|
| 76 |
typedef unsigned char RGBE[4]; |
| 77 |
RGBE *scanline = new RGBE[w]; |
| 78 |
|
| 79 |
for (int y = 0; y < h; ++y) { |
| 80 |
if (pEnd - p < 4) { |
| 81 |
qWarning("Unexpected end of HDR data"); |
| 82 |
delete[] scanline; |
| 83 |
return QByteArray(); |
| 84 |
} |
| 85 |
|
| 86 |
scanline[0][0] = *p++; |
| 87 |
scanline[0][1] = *p++; |
| 88 |
scanline[0][2] = *p++; |
| 89 |
scanline[0][3] = *p++; |
| 90 |
|
| 91 |
if (scanline[0][0] == 2 && scanline[0][1] == 2 && scanline[0][2] < 128) { |
| 92 |
// new rle, the first pixel was a dummy |
| 93 |
for (int channel = 0; channel < 4; ++channel) { |
| 94 |
for (int x = 0; x < w && p < pEnd; ) { |
| 95 |
unsigned char c = *p++; |
| 96 |
if (c > 128) { // run |
| 97 |
if (p < pEnd) { |
| 98 |
int repCount = c & 127; |
| 99 |
c = *p++; |
| 100 |
while (repCount--) |
| 101 |
scanline[x++][channel] = c; |
| 102 |
} |
| 103 |
} else { // not a run |
| 104 |
while (c-- && p < pEnd) |
| 105 |
scanline[x++][channel] = *p++; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} else { |
| 110 |
// old rle |
| 111 |
scanline[0][0] = 2; |
| 112 |
int bitshift = 0; |
| 113 |
int x = 1; |
| 114 |
while (x < w && pEnd - p >= 4) { |
| 115 |
scanline[x][0] = *p++; |
| 116 |
scanline[x][1] = *p++; |
| 117 |
scanline[x][2] = *p++; |
| 118 |
scanline[x][3] = *p++; |
| 119 |
|
| 120 |
if (scanline[x][0] == 1 && scanline[x][1] == 1 && scanline[x][2] == 1) { // run |
| 121 |
int repCount = scanline[x][3] << bitshift; |
| 122 |
while (repCount--) { |
| 123 |
memcpy(scanline[x], scanline[x - 1], 4); |
| 124 |
++x; |
| 125 |
} |
| 126 |
bitshift += 8; |
| 127 |
} else { // not a run |
| 128 |
++x; |
| 129 |
bitshift = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// adjust for -Y orientation |
| 135 |
float *fp = reinterpret_cast<float *>(data.data() + (h - 1 - y) * blockSize * w); |
| 136 |
for (int x = 0; x < w; ++x) { |
| 137 |
float d = qPow(2.0f, float(scanline[x][3]) - 128.0f); |
| 138 |
// r, g, b, a |
| 139 |
*fp++ = scanline[x][0] / 256.0f * d; |
| 140 |
*fp++ = scanline[x][1] / 256.0f * d; |
| 141 |
*fp++ = scanline[x][2] / 256.0f * d; |
| 142 |
*fp++ = 1.0f; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
delete[] scanline; |
| 147 |
|
| 148 |
if (size) |
| 149 |
*size = QSize(w, h); |
| 150 |
|
| 151 |
return data; |
| 152 |
} |
| 153 |
|
| 154 |
static float vertexData[] = |
| 155 |
{ // Y up, CCW |
| 156 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 157 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 158 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 159 |
0.5f, 0.5f, 1.0f, 0.0f |
| 160 |
}; |
| 161 |
|
| 162 |
static quint16 indexData[] = |
| 163 |
{ |
| 164 |
0, 1, 2, 0, 2, 3 |
| 165 |
}; |
| 166 |
|
| 167 |
struct { |
| 168 |
QList<QRhiResource *> releasePool; |
| 169 |
QRhiBuffer *vbuf = nullptr; |
| 170 |
QRhiBuffer *ibuf = nullptr; |
| 171 |
QRhiBuffer *ubuf = nullptr; |
| 172 |
QRhiTexture *tex = nullptr; |
| 173 |
QRhiSampler *sampler = nullptr; |
| 174 |
QRhiShaderResourceBindings *srb = nullptr; |
| 175 |
QRhiGraphicsPipeline *ps = nullptr; |
| 176 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 177 |
QMatrix4x4 winProj; |
| 178 |
} d; |
| 179 |
|
| 180 |
void Window::customInit() |
| 181 |
{ |
| 182 |
if (!m_r->isTextureFormatSupported(QRhiTexture::RGBA32F)) |
| 183 |
qWarning("RGBA32F texture format is not supported"); |
| 184 |
|
| 185 |
QSize size; |
| 186 |
QByteArray floatData = loadHdr(QLatin1String(":/OpenfootageNET_fieldairport-512.hdr"), &size); |
| 187 |
Q_ASSERT(!floatData.isEmpty()); |
| 188 |
qDebug() << size; |
| 189 |
|
| 190 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); |
| 191 |
d.vbuf->create(); |
| 192 |
d.releasePool << d.vbuf; |
| 193 |
|
| 194 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData)); |
| 195 |
d.ibuf->create(); |
| 196 |
d.releasePool << d.ibuf; |
| 197 |
|
| 198 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 199 |
d.ubuf->create(); |
| 200 |
d.releasePool << d.ubuf; |
| 201 |
|
| 202 |
d.tex = m_r->newTexture(QRhiTexture::RGBA32F, size); |
| 203 |
d.releasePool << d.tex; |
| 204 |
d.tex->create(); |
| 205 |
|
| 206 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 207 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 208 |
d.releasePool << d.sampler; |
| 209 |
d.sampler->create(); |
| 210 |
|
| 211 |
d.srb = m_r->newShaderResourceBindings(); |
| 212 |
d.releasePool << d.srb; |
| 213 |
d.srb->setBindings({ |
| 214 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 215 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler) |
| 216 |
}); |
| 217 |
d.srb->create(); |
| 218 |
|
| 219 |
d.ps = m_r->newGraphicsPipeline(); |
| 220 |
d.releasePool << d.ps; |
| 221 |
d.ps->setShaderStages({ |
| 222 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 223 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 224 |
}); |
| 225 |
QRhiVertexInputLayout inputLayout; |
| 226 |
inputLayout.setBindings({ |
| 227 |
{ 4 * sizeof(float) } |
| 228 |
}); |
| 229 |
inputLayout.setAttributes({ |
| 230 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 231 |
{ 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) } |
| 232 |
}); |
| 233 |
d.ps->setVertexInputLayout(inputLayout); |
| 234 |
d.ps->setShaderResourceBindings(d.srb); |
| 235 |
d.ps->setRenderPassDescriptor(m_rp); |
| 236 |
d.ps->create(); |
| 237 |
|
| 238 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 239 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, vertexData); |
| 240 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData); |
| 241 |
|
| 242 |
qint32 flip = 1; |
| 243 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 244 |
|
| 245 |
QRhiTextureUploadDescription desc({ 0, 0, { floatData.constData(), quint32(floatData.size()) } }); |
| 246 |
d.initialUpdates->uploadTexture(d.tex, desc); |
| 247 |
} |
| 248 |
|
| 249 |
void Window::customRelease() |
| 250 |
{ |
| 251 |
qDeleteAll(d.releasePool); |
| 252 |
d.releasePool.clear(); |
| 253 |
} |
| 254 |
|
| 255 |
void Window::customRender() |
| 256 |
{ |
| 257 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 258 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 259 |
if (d.initialUpdates) { |
| 260 |
u->merge(d.initialUpdates); |
| 261 |
d.initialUpdates->release(); |
| 262 |
d.initialUpdates = nullptr; |
| 263 |
} |
| 264 |
|
| 265 |
if (d.winProj != m_proj) { |
| 266 |
d.winProj = m_proj; |
| 267 |
QMatrix4x4 mvp = m_proj; |
| 268 |
mvp.scale(2.5f); |
| 269 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 270 |
} |
| 271 |
|
| 272 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 273 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 274 |
cb->setGraphicsPipeline(d.ps); |
| 275 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 276 |
cb->setShaderResources(); |
| 277 |
const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 278 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 279 |
cb->drawIndexed(6); |
| 280 |
cb->endPass(); |
| 281 |
} |