Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
rhiwidgetintro.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QRhiWidget>
5#include <QFile>
6#include <rhi/qrhi.h>
7
8//![0]
9class ExampleRhiWidget : public QRhiWidget
10{
11public:
12 ExampleRhiWidget(QWidget *parent = nullptr) : QRhiWidget(parent) { }
13 void initialize(QRhiCommandBuffer *cb) override;
14 void render(QRhiCommandBuffer *cb) override;
15private:
16 QRhi *m_rhi = nullptr;
17 std::unique_ptr<QRhiBuffer> m_vbuf;
18 std::unique_ptr<QRhiBuffer> m_ubuf;
19 std::unique_ptr<QRhiShaderResourceBindings> m_srb;
20 std::unique_ptr<QRhiGraphicsPipeline> m_pipeline;
21 QMatrix4x4 m_viewProjection;
22 float m_rotation = 0.0f;
23};
24
25float vertexData[] = {
26 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
27 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
28 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
29};
30
31QShader getShader(const QString &name)
32{
33 QFile f(name);
34 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
35}
36
37void ExampleRhiWidget::initialize(QRhiCommandBuffer *cb)
38{
39 if (m_rhi != rhi()) {
40 m_pipeline.reset();
41 m_rhi = rhi();
42 }
43
44 if (!m_pipeline) {
45 m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)));
46 m_vbuf->create();
47
48 m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64));
49 m_ubuf->create();
50
51 m_srb.reset(m_rhi->newShaderResourceBindings());
52 m_srb->setBindings({
53 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, m_ubuf.get()),
54 });
55 m_srb->create();
56
57 m_pipeline.reset(m_rhi->newGraphicsPipeline());
58 m_pipeline->setShaderStages({
59 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/shader_assets/color.vert.qsb")) },
60 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/shader_assets/color.frag.qsb")) }
61 });
62 QRhiVertexInputLayout inputLayout;
63 inputLayout.setBindings({
64 { 5 * sizeof(float) }
65 });
66 inputLayout.setAttributes({
67 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
68 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
69 });
70 m_pipeline->setVertexInputLayout(inputLayout);
71 m_pipeline->setShaderResourceBindings(m_srb.get());
72 m_pipeline->setRenderPassDescriptor(renderTarget()->renderPassDescriptor());
73 m_pipeline->create();
74
75 QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch();
76 resourceUpdates->uploadStaticBuffer(m_vbuf.get(), vertexData);
77 cb->resourceUpdate(resourceUpdates);
78 }
79
80 const QSize outputSize = colorTexture()->pixelSize();
81 m_viewProjection = m_rhi->clipSpaceCorrMatrix();
82 m_viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
83 m_viewProjection.translate(0, 0, -4);
84}
85
86void ExampleRhiWidget::render(QRhiCommandBuffer *cb)
87{
88 QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch();
89 m_rotation += 1.0f;
90 QMatrix4x4 modelViewProjection = m_viewProjection;
91 modelViewProjection.rotate(m_rotation, 0, 1, 0);
92 resourceUpdates->updateDynamicBuffer(m_ubuf.get(), 0, 64, modelViewProjection.constData());
93
94 const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f);
95 cb->beginPass(renderTarget(), clearColor, { 1.0f, 0 }, resourceUpdates);
96
97 cb->setGraphicsPipeline(m_pipeline.get());
98 const QSize outputSize = colorTexture()->pixelSize();
99 cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height()));
100 cb->setShaderResources();
101 const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0);
102 cb->setVertexInput(0, 1, &vbufBinding);
103 cb->draw(3);
104
105 cb->endPass();
106
107 update();
108}
109//![0]
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:322
float vertexData[]
QShader getShader(const QString &name)