-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtest_swapchain_qt.cpp
114 lines (96 loc) · 3.59 KB
/
test_swapchain_qt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <QApplication>
#include <QPushButton>
#include <QMainWindow>
#include <luisa/core/clock.h>
#include <luisa/core/logging.h>
#include <luisa/runtime/context.h>
#include <luisa/runtime/device.h>
#include <luisa/runtime/stream.h>
#include <luisa/runtime/event.h>
#include <luisa/runtime/swapchain.h>
#include <luisa/dsl/syntax.h>
#include <luisa/gui/window.h>
#include <luisa/gui/framerate.h>
using namespace luisa;
using namespace luisa::compute;
class Canvas : public QWidget {
public:
[[nodiscard]] QPaintEngine *paintEngine() const override { return nullptr; }
public:
explicit Canvas(QWidget *parent) noexcept : QWidget{parent} {
setAttribute(Qt::WA_NativeWindow);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_DontCreateNativeAncestors);
setAutoFillBackground(true);
}
};
int main(int argc, char *argv[]) {
Context context{argv[0]};
if (argc <= 1) {
LUISA_INFO("Usage: {} <backend>. <backend>: cuda, dx, cpu, metal", argv[0]);
exit(1);
}
Device device = context.create_device(argv[1]);
static constexpr auto width = 1280u;
static constexpr auto height = 720u;
static constexpr auto resolution = make_uint2(width, height);
auto draw = device.compile<2>([](ImageFloat image, Float time) noexcept {
auto p = dispatch_id().xy();
auto uv = make_float2(p) / make_float2(resolution) * 2.0f - 1.0f;
auto color = def(make_float4());
Constant<float> scales{pi, luisa::exp(1.f), luisa::sqrt(2.f)};
for (auto i = 0u; i < 3u; i++) {
color[i] = cos(time * scales[i] + uv.y * 11.f +
sin(-time * scales[2u - i] + uv.x * 7.f) * 4.f) *
.5f +
.5f;
}
color[3] = 1.0f;
image.write(p, color);
});
Stream stream = device.create_stream(StreamTag::GRAPHICS);
auto image = device.create_image<float>(PixelStorage::BYTE4, resolution);
QApplication app{argc, argv};
QMainWindow window;
window.setFixedSize(width, height);
window.setWindowTitle("Display");
window.setAutoFillBackground(true);
Canvas canvas{&window};
canvas.setFixedSize(window.contentsRect().size());
canvas.move(window.contentsRect().topLeft());
QWidget overlay{&window};
overlay.setFixedSize(window.contentsRect().size() / 2);
overlay.move(window.contentsRect().center() - overlay.rect().center());
overlay.setAutoFillBackground(true);
QPushButton button{"Quit", &overlay};
button.move(overlay.contentsRect().center() - button.rect().center());
QObject::connect(&button, &QPushButton::clicked, [&] {
window.setVisible(false);
});
auto swapchain = device.create_swapchain(
stream,
SwapchainOption{
.display = 0u,
.window = static_cast<uint64_t>(canvas.winId()),
.size = make_uint2(width, height),
.wants_hdr = false,
.wants_vsync = false,
.back_buffer_count = 2,
});
window.show();
Clock clk;
Framerate framerate;
while (window.isVisible()) {
QApplication::processEvents();
auto time = static_cast<float>(clk.toc() * 1e-3);
stream << draw(image, time).dispatch(resolution)
<< swapchain.present(image);
framerate.record();
auto title = luisa::format("Display - {:.2f} fps", framerate.report());
window.setWindowTitle(title.c_str());
}
stream << synchronize();
QApplication::quit();
}