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
main.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//! [0]
5#include <QGuiApplication>
6#include <QImage>
7#include <QFile>
8#include <rhi/qrhi.h>
9
10int main(int argc, char **argv)
11{
12 QGuiApplication app(argc, argv);
13
14#if QT_CONFIG(vulkan)
15 QVulkanInstance inst;
16#endif
17 std::unique_ptr<QRhi> rhi;
18#if defined(Q_OS_WIN)
19 QRhiD3D12InitParams params;
20 rhi.reset(QRhi::create(QRhi::D3D12, &params));
21#elif QT_CONFIG(metal)
22 QRhiMetalInitParams params;
23 rhi.reset(QRhi::create(QRhi::Metal, &params));
24#elif QT_CONFIG(vulkan)
25 inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions());
26 if (inst.create()) {
27 QRhiVulkanInitParams params;
28 params.inst = &inst;
29 rhi.reset(QRhi::create(QRhi::Vulkan, &params));
30 } else {
31 qFatal("Failed to create Vulkan instance");
32 }
33#endif
34 if (rhi)
35 qDebug() << rhi->backendName() << rhi->driverInfo();
36 else
37 qFatal("Failed to initialize RHI");
38
39 float rotation = 0.0f;
40 float opacity = 1.0f;
41 int opacityDir = 1;
42
43 std::unique_ptr<QRhiTexture> tex(rhi->newTexture(QRhiTexture::RGBA8,
44 QSize(1280, 720),
45 1,
46 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource));
47 tex->create();
48 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget({ tex.get() }));
49 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
50 rt->setRenderPassDescriptor(rp.get());
51 rt->create();
52
53 QMatrix4x4 viewProjection = rhi->clipSpaceCorrMatrix();
54 viewProjection.perspective(45.0f, 1280 / 720.f, 0.01f, 1000.0f);
55 viewProjection.translate(0, 0, -4);
56
57 static float vertexData[] = { // Y up, CCW
58 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
59 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
60 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
61 };
62
63 std::unique_ptr<QRhiBuffer> vbuf(rhi->newBuffer(QRhiBuffer::Immutable,
64 QRhiBuffer::VertexBuffer,
65 sizeof(vertexData)));
66 vbuf->create();
67
68 std::unique_ptr<QRhiBuffer> ubuf(rhi->newBuffer(QRhiBuffer::Dynamic,
69 QRhiBuffer::UniformBuffer,
70 64 + 4));
71 ubuf->create();
72
73 std::unique_ptr<QRhiShaderResourceBindings> srb(rhi->newShaderResourceBindings());
74 srb->setBindings({
75 QRhiShaderResourceBinding::uniformBuffer(0,
76 QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
77 ubuf.get())
78 });
79 srb->create();
80
81 std::unique_ptr<QRhiGraphicsPipeline> ps(rhi->newGraphicsPipeline());
82 QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
83 premulAlphaBlend.enable = true;
84 ps->setTargetBlends({ premulAlphaBlend });
85 static auto getShader = [](const QString &name) {
86 QFile f(name);
87 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
88 };
89 ps->setShaderStages({
90 { QRhiShaderStage::Vertex, getShader(QLatin1String("color.vert.qsb")) },
91 { QRhiShaderStage::Fragment, getShader(QLatin1String("color.frag.qsb")) }
92 });
93 QRhiVertexInputLayout inputLayout;
94 inputLayout.setBindings({
95 { 5 * sizeof(float) }
96 });
97 inputLayout.setAttributes({
98 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
99 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
100 });
101 ps->setVertexInputLayout(inputLayout);
102 ps->setShaderResourceBindings(srb.get());
103 ps->setRenderPassDescriptor(rp.get());
104 ps->create();
105
106 QRhiCommandBuffer *cb;
107 for (int frame = 0; frame < 20; ++frame) {
108 rhi->beginOffscreenFrame(&cb);
109
110 QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch();
111 if (frame == 0)
112 u->uploadStaticBuffer(vbuf.get(), vertexData);
113
114 QMatrix4x4 mvp = viewProjection;
115 mvp.rotate(rotation, 0, 1, 0);
116 u->updateDynamicBuffer(ubuf.get(), 0, 64, mvp.constData());
117 rotation += 5.0f;
118
119 u->updateDynamicBuffer(ubuf.get(), 64, 4, &opacity);
120 opacity += opacityDir * 0.2f;
121 if (opacity < 0.0f || opacity > 1.0f) {
122 opacityDir *= -1;
123 opacity = qBound(0.0f, opacity, 1.0f);
124 }
125
126 cb->beginPass(rt.get(), Qt::green, { 1.0f, 0 }, u);
127 cb->setGraphicsPipeline(ps.get());
128 cb->setViewport({ 0, 0, 1280, 720 });
129 cb->setShaderResources();
130 const QRhiCommandBuffer::VertexInput vbufBinding(vbuf.get(), 0);
131 cb->setVertexInput(0, 1, &vbufBinding);
132 cb->draw(3);
133 QRhiReadbackResult readbackResult;
134 u = rhi->nextResourceUpdateBatch();
135 u->readBackTexture({ tex.get() }, &readbackResult);
136 cb->endPass(u);
137
138 rhi->endOffscreenFrame();
139
140 QImage image(reinterpret_cast<const uchar *>(readbackResult.data.constData()),
141 readbackResult.pixelSize.width(),
142 readbackResult.pixelSize.height(),
143 QImage::Format_RGBA8888_Premultiplied);
144 if (rhi->isYUpInFramebuffer())
145 image.flip();
146 image.save(QString::asprintf("frame%d.png", frame));
147 }
148
149 return 0;
150}
151//! [0]
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:322
int main(int argc, char *argv[])
[ctor_close]