| 1 |
// Copyright (C) 2018 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "texturedcuberenderer.h" |
| 5 |
#include <QFile> |
| 6 |
#include <rhi/qshader.h> |
| 7 |
|
| 8 |
#include "../shared/cube.h" |
| 9 |
|
| 10 |
const bool MIPMAP = true; |
| 11 |
const bool AUTOGENMIPMAP = true; |
| 12 |
|
| 13 |
static QShader getShader(const QString &name) |
| 14 |
{ |
| 15 |
QFile f(name); |
| 16 |
if (f.open(QIODevice::ReadOnly)) |
| 17 |
return QShader::fromSerialized(f.readAll()); |
| 18 |
|
| 19 |
return QShader(); |
| 20 |
} |
| 21 |
|
| 22 |
void TexturedCubeRenderer::initResources(QRhiRenderPassDescriptor *rp) |
| 23 |
{ |
| 24 |
m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 25 |
m_vbuf->setName(QByteArrayLiteral("Cube vbuf (textured)")); |
| 26 |
m_vbuf->create(); |
| 27 |
m_vbufReady = false; |
| 28 |
|
| 29 |
m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4); |
| 30 |
m_ubuf->setName(QByteArrayLiteral("Cube ubuf (textured)")); |
| 31 |
m_ubuf->create(); |
| 32 |
|
| 33 |
m_image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888); |
| 34 |
QRhiTexture::Flags texFlags; |
| 35 |
if (MIPMAP) |
| 36 |
texFlags |= QRhiTexture::MipMapped; |
| 37 |
if (AUTOGENMIPMAP) |
| 38 |
texFlags |= QRhiTexture::UsedWithGenerateMips; |
| 39 |
m_tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(m_image.width(), m_image.height()), 1, texFlags); |
| 40 |
m_tex->setName(QByteArrayLiteral("Qt texture")); |
| 41 |
m_tex->create(); |
| 42 |
|
| 43 |
m_sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, MIPMAP ? QRhiSampler::Linear : QRhiSampler::None, |
| 44 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 45 |
m_sampler->create(); |
| 46 |
|
| 47 |
m_srb = m_r->newShaderResourceBindings(); |
| 48 |
m_srb->setBindings({ |
| 49 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf), |
| 50 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, m_tex, m_sampler) |
| 51 |
}); |
| 52 |
m_srb->create(); |
| 53 |
|
| 54 |
m_ps = m_r->newGraphicsPipeline(); |
| 55 |
|
| 56 |
// No blending but the texture has alpha which we do not want to write out. |
| 57 |
// Be nice. (would not matter for an onscreen window but makes a difference |
| 58 |
// when reading back and saving into image files f.ex.) |
| 59 |
QRhiGraphicsPipeline::TargetBlend blend; |
| 60 |
blend.colorWrite = QRhiGraphicsPipeline::R | QRhiGraphicsPipeline::G | QRhiGraphicsPipeline::B; |
| 61 |
m_ps->setTargetBlends({ blend }); |
| 62 |
|
| 63 |
m_ps->setDepthTest(true); |
| 64 |
m_ps->setDepthWrite(true); |
| 65 |
m_ps->setDepthOp(QRhiGraphicsPipeline::Less); |
| 66 |
|
| 67 |
m_ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 68 |
m_ps->setFrontFace(QRhiGraphicsPipeline::CCW); |
| 69 |
|
| 70 |
m_ps->setSampleCount(m_sampleCount); |
| 71 |
|
| 72 |
QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); |
| 73 |
Q_ASSERT(vs.isValid()); |
| 74 |
QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); |
| 75 |
Q_ASSERT(fs.isValid()); |
| 76 |
m_ps->setShaderStages({ |
| 77 |
{ QRhiShaderStage::Vertex, vs }, |
| 78 |
{ QRhiShaderStage::Fragment, fs } |
| 79 |
}); |
| 80 |
|
| 81 |
QRhiVertexInputLayout inputLayout; |
| 82 |
inputLayout.setBindings({ |
| 83 |
{ 3 * sizeof(float) }, |
| 84 |
{ 2 * sizeof(float) } |
| 85 |
}); |
| 86 |
inputLayout.setAttributes({ |
| 87 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 88 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 } |
| 89 |
}); |
| 90 |
|
| 91 |
m_ps->setVertexInputLayout(inputLayout); |
| 92 |
m_ps->setShaderResourceBindings(m_srb); |
| 93 |
m_ps->setRenderPassDescriptor(rp); |
| 94 |
|
| 95 |
m_ps->create(); |
| 96 |
} |
| 97 |
|
| 98 |
void TexturedCubeRenderer::resize(const QSize &pixelSize) |
| 99 |
{ |
| 100 |
m_proj = m_r->clipSpaceCorrMatrix(); |
| 101 |
m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f); |
| 102 |
m_proj.translate(0, 0, -4); |
| 103 |
} |
| 104 |
|
| 105 |
void TexturedCubeRenderer::releaseResources() |
| 106 |
{ |
| 107 |
delete m_ps; |
| 108 |
m_ps = nullptr; |
| 109 |
|
| 110 |
delete m_srb; |
| 111 |
m_srb = nullptr; |
| 112 |
|
| 113 |
delete m_sampler; |
| 114 |
m_sampler = nullptr; |
| 115 |
|
| 116 |
delete m_tex; |
| 117 |
m_tex = nullptr; |
| 118 |
|
| 119 |
delete m_ubuf; |
| 120 |
m_ubuf = nullptr; |
| 121 |
|
| 122 |
delete m_vbuf; |
| 123 |
m_vbuf = nullptr; |
| 124 |
} |
| 125 |
|
| 126 |
void TexturedCubeRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) |
| 127 |
{ |
| 128 |
if (!m_vbufReady) { |
| 129 |
m_vbufReady = true; |
| 130 |
resourceUpdates->uploadStaticBuffer(m_vbuf, cube); |
| 131 |
qint32 flip = 0; |
| 132 |
resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &flip); |
| 133 |
} |
| 134 |
|
| 135 |
if (!m_image.isNull()) { |
| 136 |
if (MIPMAP) { |
| 137 |
QVarLengthArray<QRhiTextureUploadEntry, 16> descEntries; |
| 138 |
if (!AUTOGENMIPMAP) { |
| 139 |
// the ghetto mipmap generator... |
| 140 |
for (int i = 0, ie = m_r->mipLevelsForSize(m_image.size()); i != ie; ++i) { |
| 141 |
QImage image = m_image.scaled(m_r->sizeForMipLevel(i, m_image.size())); |
| 142 |
descEntries.append({ 0, i, QRhiTextureSubresourceUploadDescription(image) }); |
| 143 |
} |
| 144 |
} else { |
| 145 |
descEntries.append({ 0, 0, QRhiTextureSubresourceUploadDescription(m_image) }); |
| 146 |
} |
| 147 |
QRhiTextureUploadDescription desc; |
| 148 |
desc.setEntries(descEntries.cbegin(), descEntries.cend()); |
| 149 |
resourceUpdates->uploadTexture(m_tex, desc); |
| 150 |
if (AUTOGENMIPMAP) |
| 151 |
resourceUpdates->generateMips(m_tex); |
| 152 |
} else { |
| 153 |
resourceUpdates->uploadTexture(m_tex, m_image); |
| 154 |
} |
| 155 |
m_image = QImage(); |
| 156 |
} |
| 157 |
|
| 158 |
m_rotation += 1.0f; |
| 159 |
QMatrix4x4 mvp = m_proj; |
| 160 |
mvp.translate(m_translation); |
| 161 |
mvp.scale(0.5f); |
| 162 |
mvp.rotate(m_rotation, 0, 1, 0); |
| 163 |
resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData()); |
| 164 |
} |
| 165 |
|
| 166 |
void TexturedCubeRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels) |
| 167 |
{ |
| 168 |
cb->setGraphicsPipeline(m_ps); |
| 169 |
cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); |
| 170 |
cb->setShaderResources(); |
| 171 |
const QRhiCommandBuffer::VertexInput vbufBindings[] = { |
| 172 |
{ m_vbuf, 0 }, |
| 173 |
{ m_vbuf, quint32(36 * 3 * sizeof(float)) } |
| 174 |
}; |
| 175 |
cb->setVertexInput(0, 2, vbufBindings); |
| 176 |
cb->draw(36); |
| 177 |
} |