-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (156 loc) · 5.96 KB
/
main.cpp
File metadata and controls
195 lines (156 loc) · 5.96 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
//
#include "nbl/video/surface/CSurfaceVulkan.h"
#include "../common/BasicMultiQueueApplication.hpp"
namespace nbl::examples
{
// Virtual Inheritance because apps might end up doing diamond inheritance
class WindowedApplication : public virtual BasicMultiQueueApplication
{
using base_t = BasicMultiQueueApplication;
public:
using base_t::base_t;
// We inherit from an application that tries to find Graphics and Compute queues
// because applications with presentable images often want to perform Graphics family operations
virtual bool isComputeOnly() const {return false;}
virtual video::IAPIConnection::SFeatures getAPIFeaturesToEnable() override
{
auto retval = base_t::getAPIFeaturesToEnable();
// We only support one swapchain mode, surface, the other one is Display which we have not implemented yet.
retval.swapchainMode = video::E_SWAPCHAIN_MODE::ESM_SURFACE;
return retval;
}
// New function, we neeed to know about surfaces to create ahead of time
virtual core::vector<video::SPhysicalDeviceFilter::SurfaceCompatibility> getSurfaces() const = 0;
// We have a very simple heuristic, the device must be able to render to all windows!
// (want to make something more complex? you're on your own!)
virtual void filterDevices(core::set<video::IPhysicalDevice*>& physicalDevices) const
{
base_t::filterDevices(physicalDevices);
video::SPhysicalDeviceFilter deviceFilter = {};
auto surfaces = getSurfaces();
deviceFilter.requiredSurfaceCompatibilities = {surfaces};
return deviceFilter(physicalDevices);
}
virtual bool onAppInitialized(core::smart_refctd_ptr<system::ISystem>&& system) override
{
// Remember to call the base class initialization!
if (!base_t::onAppInitialized(std::move(system)))
return false;
#ifdef _NBL_PLATFORM_WINDOWS_
m_winMgr = nbl::ui::IWindowManagerWin32::create();
#else
#error "Unimplemented!"
#endif
return true;
}
core::smart_refctd_ptr<ui::IWindowManager> m_winMgr;
};
// Before we get onto creating a window, we need to discuss how Nabla handles input, clipboards and cursor control
class IWindowClosedCallback : public virtual nbl::ui::IWindow::IEventCallback
{
public:
IWindowClosedCallback() : m_gotWindowClosedMsg(false) {}
// unless you create a separate callback per window, both will "trip" this condition
bool windowGotClosed() const {return m_gotWindowClosedMsg;}
private:
bool onWindowClosed_impl() override
{
m_gotWindowClosedMsg = true;
return true;
}
bool m_gotWindowClosedMsg;
};
// We inherit from an application that tries to find Graphics and Compute queues
// because applications with presentable images often want to perform Graphics family operations
// Virtual Inheritance because apps might end up doing diamond inheritance
class SingleNonResizableWindowApplication : public virtual WindowedApplication
{
using base_t = WindowedApplication;
public:
using base_t::base_t;
virtual bool onAppInitialized(core::smart_refctd_ptr<system::ISystem>&& system) override
{
// Remember to call the base class initialization!
if (!base_t::onAppInitialized(std::move(system)))
return false;
m_window = m_winMgr->createWindow(getWindowCreationParams());
m_surface = video::CSurfaceVulkanWin32::create(core::smart_refctd_ptr(m_api),core::smart_refctd_ptr_static_cast<ui::IWindowWin32>(m_window));
return true;
}
virtual core::vector<video::SPhysicalDeviceFilter::SurfaceCompatibility> getSurfaces() const
{
return {{m_surface.get()/*,EQF_NONE*/}};
}
virtual bool keepRunning() override
{
if (!m_window || reinterpret_cast<const IWindowClosedCallback*>(m_window->getEventCallback())->windowGotClosed())
return false;
return true;
}
protected:
virtual ui::IWindow::SCreationParams getWindowCreationParams() const
{
ui::IWindow::SCreationParams params = {};
params.callback = core::make_smart_refctd_ptr<IWindowClosedCallback>();
params.width = 640;
params.height = 480;
params.x = 32;
params.y = 32;
params.flags = ui::IWindow::ECF_NONE;
params.windowCaption = "SingleNonResizableWindowApplication";
return params;
}
core::smart_refctd_ptr<ui::IWindow> m_window;
core::smart_refctd_ptr<video::ISurfaceVulkan> m_surface;
};
}
#include "nbl/video/CVulkanSwapchain.h"
using namespace nbl;
using namespace core;
using namespace system;
using namespace ui;
class HelloSwapchainApp final : public examples::SingleNonResizableWindowApplication
{
using base_t = examples::SingleNonResizableWindowApplication;
using clock_t = std::chrono::steady_clock;
public:
using base_t::base_t;
bool onAppInitialized(smart_refctd_ptr<ISystem>&& system) override
{
// Remember to call the base class initialization!
if (!base_t::onAppInitialized(std::move(system)))
return false;
// Help the CI a bit by providing a timeout option
// TODO: @Hazardu maybe we should make a unified argument parser/handler for all examples?
if (base_t::argv.size()>=3 && argv[1]=="-timeout_seconds")
timeout = std::chrono::seconds(std::atoi(argv[2].c_str()));
start = clock_t::now();
return true;
}
// We do a very simple thing, and just keep on clearing the swapchain image to red and present
void workLoopBody() override
{
}
//
bool keepRunning() override
{
if (duration_cast<decltype(timeout)>(clock_t::now()-start)>timeout)
return false;
return base_t::keepRunning();
}
protected:
virtual IWindow::SCreationParams getWindowCreationParams() const
{
auto retval = base_t::getWindowCreationParams();
retval.windowCaption = "HelloSwapchainApp";
return retval;
}
// Just like in the HelloUI app we add a timeout
std::chrono::seconds timeout = std::chrono::seconds(0x7fffFFFFu);
clock_t::time_point start;
};
// define an entry point as always!
NBL_MAIN_FUNC(HelloSwapchainApp)