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
qquickrhiitem_intro.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 <QtQuick/QQuickRhiItem>
5#include <rhi/qrhi.h>
6
9{
10public:
11 void initialize(QRhiCommandBuffer *cb) override;
12 void synchronize(QQuickRhiItem *item) override;
13 void render(QRhiCommandBuffer *cb) override;
14
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_angle = 0.0f;
23};
24
26{
29 Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged)
30
31public:
33
34 float angle() const { return m_angle; }
35 void setAngle(float a);
36
39
40private:
41 float m_angle = 0.0f;
42};
43
48
50{
51 if (m_angle == a)
52 return;
53
54 m_angle = a;
56 update();
57}
58
60{
61 ExampleRhiItem *item = static_cast<ExampleRhiItem *>(rhiItem);
62 if (item->angle() != m_angle)
63 m_angle = item->angle();
64}
65
67{
68 QFile f(name);
69 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
70}
71
72static float vertexData[] = {
73 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
74 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
75 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
76};
77
79{
80 if (m_rhi != rhi()) {
81 m_pipeline.reset();
82 m_rhi = rhi();
83 }
84
85 if (!m_pipeline) {
87 m_vbuf->create();
88
90 m_ubuf->create();
91
92 m_srb.reset(m_rhi->newShaderResourceBindings());
93 m_srb->setBindings({
95 });
96 m_srb->create();
97
98 m_pipeline.reset(m_rhi->newGraphicsPipeline());
99 m_pipeline->setShaderStages({
100 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/shaders/color.vert.qsb")) },
101 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/shaders/color.frag.qsb")) }
102 });
103 QRhiVertexInputLayout inputLayout;
104 inputLayout.setBindings({
105 { 5 * sizeof(float) }
106 });
107 inputLayout.setAttributes({
109 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
110 });
111 m_pipeline->setVertexInputLayout(inputLayout);
112 m_pipeline->setShaderResourceBindings(m_srb.get());
113 m_pipeline->setRenderPassDescriptor(renderTarget()->renderPassDescriptor());
114 m_pipeline->create();
115
116 QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch();
117 resourceUpdates->uploadStaticBuffer(m_vbuf.get(), vertexData);
118 cb->resourceUpdate(resourceUpdates);
119 }
120
121 const QSize outputSize = renderTarget()->pixelSize();
122 m_viewProjection = m_rhi->clipSpaceCorrMatrix();
123 m_viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
124 m_viewProjection.translate(0, 0, -4);
125}
126
128{
129 QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch();
130 QMatrix4x4 modelViewProjection = m_viewProjection;
131 modelViewProjection.rotate(m_angle, 0, 1, 0);
132 resourceUpdates->updateDynamicBuffer(m_ubuf.get(), 0, 64, modelViewProjection.constData());
133
134 const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f);
135 cb->beginPass(renderTarget(), clearColor, { 1.0f, 0 }, resourceUpdates);
136
137 cb->setGraphicsPipeline(m_pipeline.get());
138 const QSize outputSize = renderTarget()->pixelSize();
139 cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height()));
140 cb->setShaderResources();
141 const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0);
142 cb->setVertexInput(0, 1, &vbufBinding);
143 cb->draw(3);
144
145 cb->endPass();
146}
void render(QRhiCommandBuffer *cb) override
Called when the backing color buffer's contents needs updating.
void synchronize(QQuickRhiItem *item) override
This function is called on the render thread, if there is one, while the main/GUI thread is blocked.
void initialize(QRhiCommandBuffer *cb) override
Called when the item is initialized for the first time, when the associated texture's size,...
QQuickRhiItemRenderer * createRenderer() override
Reimplement this function to create and return a new instance of a QQuickRhiItemRenderer subclass.
void angleChanged()
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.
void update()
Schedules a call to updatePaintNode() for this item.
\inmodule QtQuick
QRhiRenderTarget * renderTarget() const
\inmodule QtQuick
@ 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
virtual QSize pixelSize() const =0
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
\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 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
static bool initialize()
Definition qctf.cpp:94
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat GLfloat f
GLfloat angle
GLuint name
#define QML_NAMED_ELEMENT(NAME)
static float vertexData[]
static QShader getShader(const QString &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
#define Q_PROPERTY(...)
#define Q_OBJECT
#define signals
#define emit
QGraphicsItem * item