| 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 "../shared/cube.h" |
| 6 |
#include "../shared/dds_bc1.h" |
| 7 |
|
| 8 |
struct { |
| 9 |
QRhiBuffer *vbuf = nullptr; |
| 10 |
bool vbufReady = false; |
| 11 |
QRhiBuffer *ubuf = nullptr; |
| 12 |
QRhiTexture *tex = nullptr; |
| 13 |
QRhiSampler *sampler = nullptr; |
| 14 |
QRhiShaderResourceBindings *srb = nullptr; |
| 15 |
QRhiGraphicsPipeline *ps = nullptr; |
| 16 |
|
| 17 |
float rotation = 0; |
| 18 |
|
| 19 |
QByteArrayList compressedData; |
| 20 |
} d; |
| 21 |
|
| 22 |
void Window::customInit() |
| 23 |
{ |
| 24 |
if (!m_r->isTextureFormatSupported(QRhiTexture::BC1)) |
| 25 |
qFatal("This backend does not support BC1"); |
| 26 |
|
| 27 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 28 |
d.vbuf->create(); |
| 29 |
d.vbufReady = false; |
| 30 |
|
| 31 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 32 |
d.ubuf->create(); |
| 33 |
|
| 34 |
QSize imageSize; |
| 35 |
d.compressedData = loadBC1(QLatin1String(":/qt256_bc1_9mips.dds"), &imageSize); |
| 36 |
qDebug() << d.compressedData.count() << imageSize << m_r->mipLevelsForSize(imageSize); |
| 37 |
|
| 38 |
d.tex = m_r->newTexture(QRhiTexture::BC1, imageSize, 1, QRhiTexture::MipMapped); |
| 39 |
d.tex->create(); |
| 40 |
|
| 41 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear, |
| 42 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 43 |
d.sampler->create(); |
| 44 |
|
| 45 |
d.srb = m_r->newShaderResourceBindings(); |
| 46 |
d.srb->setBindings({ |
| 47 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 48 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler) |
| 49 |
}); |
| 50 |
d.srb->create(); |
| 51 |
|
| 52 |
d.ps = m_r->newGraphicsPipeline(); |
| 53 |
|
| 54 |
d.ps->setDepthTest(true); |
| 55 |
d.ps->setDepthWrite(true); |
| 56 |
d.ps->setDepthOp(QRhiGraphicsPipeline::Less); |
| 57 |
|
| 58 |
d.ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 59 |
d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); |
| 60 |
|
| 61 |
const QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); |
| 62 |
if (!vs.isValid()) |
| 63 |
qFatal("Failed to load shader pack (vertex)"); |
| 64 |
const QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); |
| 65 |
if (!fs.isValid()) |
| 66 |
qFatal("Failed to load shader pack (fragment)"); |
| 67 |
|
| 68 |
d.ps->setShaderStages({ |
| 69 |
{ QRhiShaderStage::Vertex, vs }, |
| 70 |
{ QRhiShaderStage::Fragment, fs } |
| 71 |
}); |
| 72 |
|
| 73 |
QRhiVertexInputLayout inputLayout; |
| 74 |
inputLayout.setBindings({ |
| 75 |
{ 3 * sizeof(float) }, |
| 76 |
{ 2 * sizeof(float) } |
| 77 |
}); |
| 78 |
inputLayout.setAttributes({ |
| 79 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 80 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 } |
| 81 |
}); |
| 82 |
|
| 83 |
d.ps->setVertexInputLayout(inputLayout); |
| 84 |
d.ps->setShaderResourceBindings(d.srb); |
| 85 |
d.ps->setRenderPassDescriptor(m_rp); |
| 86 |
|
| 87 |
d.ps->create(); |
| 88 |
} |
| 89 |
|
| 90 |
void Window::customRelease() |
| 91 |
{ |
| 92 |
delete d.ps; |
| 93 |
d.ps = nullptr; |
| 94 |
|
| 95 |
delete d.srb; |
| 96 |
d.srb = nullptr; |
| 97 |
|
| 98 |
delete d.ubuf; |
| 99 |
d.ubuf = nullptr; |
| 100 |
|
| 101 |
delete d.vbuf; |
| 102 |
d.vbuf = nullptr; |
| 103 |
|
| 104 |
delete d.sampler; |
| 105 |
d.sampler = nullptr; |
| 106 |
|
| 107 |
delete d.tex; |
| 108 |
d.tex = nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
void Window::customRender() |
| 112 |
{ |
| 113 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 114 |
if (!d.vbufReady) { |
| 115 |
d.vbufReady = true; |
| 116 |
u->uploadStaticBuffer(d.vbuf, cube); |
| 117 |
qint32 flip = 0; |
| 118 |
u->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 119 |
} |
| 120 |
if (!d.compressedData.isEmpty()) { |
| 121 |
QVarLengthArray<QRhiTextureUploadEntry, 16> descEntries; |
| 122 |
for (int i = 0; i < d.compressedData.count(); ++i) { |
| 123 |
QRhiTextureSubresourceUploadDescription image(d.compressedData[i].constData(), d.compressedData[i].size()); |
| 124 |
descEntries.append({ 0, i, image }); |
| 125 |
} |
| 126 |
QRhiTextureUploadDescription desc; |
| 127 |
desc.setEntries(descEntries.cbegin(), descEntries.cend()); |
| 128 |
u->uploadTexture(d.tex, desc); |
| 129 |
d.compressedData.clear(); |
| 130 |
} |
| 131 |
d.rotation += 1.0f; |
| 132 |
QMatrix4x4 mvp = m_proj; |
| 133 |
mvp.scale(0.5f); |
| 134 |
mvp.rotate(d.rotation, 0, 1, 0); |
| 135 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 136 |
|
| 137 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 138 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 139 |
|
| 140 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 141 |
|
| 142 |
cb->setGraphicsPipeline(d.ps); |
| 143 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 144 |
cb->setShaderResources(); |
| 145 |
const QRhiCommandBuffer::VertexInput vbufBindings[] = { |
| 146 |
{ d.vbuf, 0 }, |
| 147 |
{ d.vbuf, quint32(36 * 3 * sizeof(float)) } |
| 148 |
}; |
| 149 |
cb->setVertexInput(0, 2, vbufBindings); |
| 150 |
cb->draw(36); |
| 151 |
|
| 152 |
cb->endPass(); |
| 153 |
} |