Debian Codegraph @

examples/widgets/rhi/cuberhiwidget/examplewidget.cpp

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