| 1 |
// Copyright (C) 2021 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "examplewidget.h" |
| 5 |
#include "../shared/cube.h" |
| 6 |
|
| 7 |
#include <QFile> |
| 8 |
|
| 9 |
#include <QPainter> |
| 10 |
#include <QtGui/qquaternion.h> |
| 11 |
|
| 12 |
static const QSize CUBE_TEX_SIZE(512, 512); |
| 13 |
|
| 14 |
ExampleRhiWidget::ExampleRhiWidget(QWidget *parent, Qt::WindowFlags f) |
| 15 |
: QRhiWidget(parent, f) |
| 16 |
{ |
| 17 |
setDebugLayer(true); |
| 18 |
} |
| 19 |
|
| 20 |
void ExampleRhiWidget::initialize(QRhi *rhi, QRhiTexture *outputTexture) |
| 21 |
{ |
| 22 |
if (m_rhi != rhi) { |
| 23 |
m_rt.reset(); |
| 24 |
m_rp.reset(); |
| 25 |
m_ds.reset(); |
| 26 |
scene.vbuf.reset(); |
| 27 |
} else if (m_output != outputTexture) { |
| 28 |
m_rt.reset(); |
| 29 |
m_rp.reset(); |
| 30 |
} |
| 31 |
|
| 32 |
m_rhi = rhi; |
| 33 |
m_output = outputTexture; |
| 34 |
|
| 35 |
if (!m_ds) { |
| 36 |
m_ds.reset(m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, m_output->pixelSize())); |
| 37 |
m_ds->create(); |
| 38 |
} else if (m_ds->pixelSize() != m_output->pixelSize()) { |
| 39 |
m_ds->setPixelSize(m_output->pixelSize()); |
| 40 |
m_ds->create(); |
| 41 |
} |
| 42 |
|
| 43 |
if (!m_rt) { |
| 44 |
m_rt.reset(m_rhi->newTextureRenderTarget({ { m_output }, m_ds.data() })); |
| 45 |
m_rp.reset(m_rt->newCompatibleRenderPassDescriptor()); |
| 46 |
m_rt->setRenderPassDescriptor(m_rp.data()); |
| 47 |
m_rt->create(); |
| 48 |
} |
| 49 |
|
| 50 |
if (!scene.vbuf) { |
| 51 |
initScene(); |
| 52 |
updateCubeTexture(); |
| 53 |
} |
| 54 |
|
| 55 |
const QSize outputSize = m_output->pixelSize(); |
| 56 |
scene.mvp = m_rhi->clipSpaceCorrMatrix(); |
| 57 |
scene.mvp.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 58 |
scene.mvp.translate(0, 0, -4); |
| 59 |
updateMvp(); |
| 60 |
} |
| 61 |
|
| 62 |
void ExampleRhiWidget::updateMvp() |
| 63 |
{ |
| 64 |
QMatrix4x4 mvp = scene.mvp * QMatrix4x4(QQuaternion::fromEulerAngles(QVector3D(30, itemData.cubeRotation, 0)).toRotationMatrix()); |
| 65 |
if (!scene.resourceUpdates) |
| 66 |
scene.resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 67 |
scene.resourceUpdates->updateDynamicBuffer(scene.ubuf.data(), 0, 64, mvp.constData()); |
| 68 |
} |
| 69 |
|
| 70 |
void ExampleRhiWidget::updateCubeTexture() |
| 71 |
{ |
| 72 |
QImage image(CUBE_TEX_SIZE, QImage::Format_RGBA8888); |
| 73 |
const QRect r(QPoint(0, 0), CUBE_TEX_SIZE); |
| 74 |
QPainter p(&image); |
| 75 |
p.fillRect(r, QGradient::DeepBlue); |
| 76 |
QFont font; |
| 77 |
font.setPointSize(24); |
| 78 |
p.setFont(font); |
| 79 |
p.drawText(r, itemData.cubeText); |
| 80 |
p.end(); |
| 81 |
|
| 82 |
if (!scene.resourceUpdates) |
| 83 |
scene.resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 84 |
scene.resourceUpdates->uploadTexture(scene.cubeTex.data(), image); |
| 85 |
} |
| 86 |
|
| 87 |
static QShader getShader(const QString &name) |
| 88 |
{ |
| 89 |
QFile f(name); |
| 90 |
if (f.open(QIODevice::ReadOnly)) |
| 91 |
return QShader::fromSerialized(f.readAll()); |
| 92 |
|
| 93 |
return QShader(); |
| 94 |
} |
| 95 |
|
| 96 |
void ExampleRhiWidget::initScene() |
| 97 |
{ |
| 98 |
scene.vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube))); |
| 99 |
scene.vbuf->create(); |
| 100 |
|
| 101 |
scene.resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 102 |
scene.resourceUpdates->uploadStaticBuffer(scene.vbuf.data(), cube); |
| 103 |
|
| 104 |
scene.ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68)); |
| 105 |
scene.ubuf->create(); |
| 106 |
|
| 107 |
const qint32 flip = 0; |
| 108 |
scene.resourceUpdates->updateDynamicBuffer(scene.ubuf.data(), 64, 4, &flip); |
| 109 |
|
| 110 |
scene.cubeTex.reset(m_rhi->newTexture(QRhiTexture::RGBA8, CUBE_TEX_SIZE)); |
| 111 |
scene.cubeTex->create(); |
| 112 |
|
| 113 |
scene.sampler.reset(m_rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 114 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge)); |
| 115 |
scene.sampler->create(); |
| 116 |
|
| 117 |
scene.srb.reset(m_rhi->newShaderResourceBindings()); |
| 118 |
scene.srb->setBindings({ |
| 119 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, scene.ubuf.data()), |
| 120 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, scene.cubeTex.data(), scene.sampler.data()) |
| 121 |
}); |
| 122 |
scene.srb->create(); |
| 123 |
|
| 124 |
scene.ps.reset(m_rhi->newGraphicsPipeline()); |
| 125 |
scene.ps->setDepthTest(true); |
| 126 |
scene.ps->setDepthWrite(true); |
| 127 |
scene.ps->setDepthOp(QRhiGraphicsPipeline::Less); |
| 128 |
scene.ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 129 |
scene.ps->setFrontFace(QRhiGraphicsPipeline::CCW); |
| 130 |
QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); |
| 131 |
Q_ASSERT(vs.isValid()); |
| 132 |
QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); |
| 133 |
Q_ASSERT(fs.isValid()); |
| 134 |
scene.ps->setShaderStages({ |
| 135 |
{ QRhiShaderStage::Vertex, vs }, |
| 136 |
{ QRhiShaderStage::Fragment, fs } |
| 137 |
}); |
| 138 |
QRhiVertexInputLayout inputLayout; |
| 139 |
inputLayout.setBindings({ |
| 140 |
{ 3 * sizeof(float) }, |
| 141 |
{ 2 * sizeof(float) } |
| 142 |
}); |
| 143 |
inputLayout.setAttributes({ |
| 144 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 145 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 } |
| 146 |
}); |
| 147 |
scene.ps->setVertexInputLayout(inputLayout); |
| 148 |
scene.ps->setShaderResourceBindings(scene.srb.data()); |
| 149 |
scene.ps->setRenderPassDescriptor(m_rp.data()); |
| 150 |
scene.ps->create(); |
| 151 |
} |
| 152 |
|
| 153 |
void ExampleRhiWidget::render(QRhiCommandBuffer *cb) |
| 154 |
{ |
| 155 |
if (itemData.cubeRotationDirty) { |
| 156 |
itemData.cubeRotationDirty = false; |
| 157 |
updateMvp(); |
| 158 |
} |
| 159 |
|
| 160 |
if (itemData.cubeTextDirty) { |
| 161 |
itemData.cubeTextDirty = false; |
| 162 |
updateCubeTexture(); |
| 163 |
} |
| 164 |
|
| 165 |
QRhiResourceUpdateBatch *rub = scene.resourceUpdates; |
| 166 |
if (rub) |
| 167 |
scene.resourceUpdates = nullptr; |
| 168 |
|
| 169 |
const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f); |
| 170 |
|
| 171 |
cb->beginPass(m_rt.data(), clearColor, { 1.0f, 0 }, rub); |
| 172 |
|
| 173 |
cb->setGraphicsPipeline(scene.ps.data()); |
| 174 |
const QSize outputSize = m_output->pixelSize(); |
| 175 |
cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height())); |
| 176 |
cb->setShaderResources(); |
| 177 |
const QRhiCommandBuffer::VertexInput vbufBindings[] = { |
| 178 |
{ scene.vbuf.data(), 0 }, |
| 179 |
{ scene.vbuf.data(), quint32(36 * 3 * sizeof(float)) } |
| 180 |
}; |
| 181 |
cb->setVertexInput(0, 2, vbufBindings); |
| 182 |
cb->draw(36); |
| 183 |
|
| 184 |
cb->endPass(); |
| 185 |
} |