Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
10{
11public:
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
32{
33 QFile f(name);
34 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
35}
36
38{
39 if (m_rhi != rhi()) {
40 m_pipeline.reset();
41 m_rhi = rhi();
42 }
43
44 if (!m_pipeline) {
46 m_vbuf->create();
47
49 m_ubuf->create();
50
51 m_srb.reset(m_rhi->newShaderResourceBindings());
52 m_srb->setBindings({
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({
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
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}
ExampleRhiWidget(QWidget *parent=nullptr)
void initialize(QRhiCommandBuffer *cb) override
Called when the widget is initialized for the first time, when the associated texture's size,...
void render(QRhiCommandBuffer *cb) override
Called when the widget contents (i.e.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QColor fromRgbF(float r, float g, float b, float a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values,...
Definition qcolor.cpp:2427
\inmodule QtCore
Definition qfile.h:93
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
void rotate(float angle, const QVector3D &vector)
Multiples this matrix by another that rotates coordinates through angle degrees about vector.
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
Multiplies this matrix by another that applies a perspective projection.
void translate(const QVector3D &vector)
Multiplies this matrix by another that translates coordinates by the components of vector.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
@ Immutable
Definition qrhi.h:849
@ Dynamic
Definition qrhi.h:851
@ VertexBuffer
Definition qrhi.h:855
@ UniformBuffer
Definition qrhi.h:857
\inmodule QtGui
Definition qrhi.h:1651
QPair< QRhiBuffer *, quint32 > VertexInput
Synonym for QPair<QRhiBuffer *, quint32>.
Definition qrhi.h:1680
QRhiRenderPassDescriptor * renderPassDescriptor() const
Definition qrhi.h:1164
\inmodule QtGui
Definition qrhi.h:1731
void uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
Enqueues updating a region of a QRhiBuffer buf created with the type QRhiBuffer::Immutable or QRhiBuf...
Definition qrhi.cpp:9011
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition qrhi.cpp:5526
QSize pixelSize() const
Definition qrhi.h:975
\inmodule QtGui
Definition qrhi.h:321
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Sets the bindings from the specified list.
Definition qrhi.h:325
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Sets the attributes from the specified list.
Definition qrhi.h:337
\inmodule QtGui
Definition qrhi.h:85
\inmodule QtWidgets
Definition qrhiwidget.h:19
QRhi * rhi() const
QRhiTexture * colorTexture() const
QRhiRenderTarget * renderTarget() const
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, quint32 size)
Definition qrhi.cpp:10508
QMatrix4x4 clipSpaceCorrMatrix() const
Definition qrhi.cpp:10091
QRhiShaderResourceBindings * newShaderResourceBindings()
Definition qrhi.cpp:10489
QRhiGraphicsPipeline * newGraphicsPipeline()
Definition qrhi.cpp:10466
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition qrhi.cpp:9252
\inmodule QtGui
Definition qshader.h:81
static QShader fromSerialized(const QByteArray &data)
Creates a new QShader instance from the given data.
Definition qshader.cpp:540
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void update()
Updates the widget unless updates are disabled or the widget is hidden.
static bool initialize()
Definition qctf.cpp:94
GLfloat GLfloat f
GLuint name
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
float vertexData[]
QShader getShader(const QString &name)