forked from Mrkol/graphics-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
194 lines (151 loc) · 5.34 KB
/
Renderer.cpp
File metadata and controls
194 lines (151 loc) · 5.34 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
#include "Renderer.hpp"
#include <etna/GlobalContext.hpp>
#include <etna/Etna.hpp>
#include <etna/RenderTargetStates.hpp>
#include <etna/PipelineManager.hpp>
#include <etna/Profiling.hpp>
#include <imgui.h>
#include <gui/ImGuiRenderer.hpp>
Renderer::Renderer(glm::uvec2 res)
: resolution{res}
{
}
void Renderer::initVulkan(std::span<const char*> instance_extensions)
{
std::vector<const char*> instanceExtensions;
for (auto ext : instance_extensions)
instanceExtensions.push_back(ext);
std::vector<const char*> deviceExtensions;
deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
etna::initialize(etna::InitParams{
.applicationName = "ShadowmapSample",
.applicationVersion = VK_MAKE_VERSION(0, 1, 0),
.instanceExtensions = instanceExtensions,
.deviceExtensions = deviceExtensions,
.features = vk::PhysicalDeviceFeatures2{.features = {}},
// Replace with an index if etna detects your preferred GPU incorrectly
.physicalDeviceIndexOverride = {},
// How much frames we buffer on the GPU without waiting for their completion on the CPU
.numFramesInFlight = 2,
});
}
void Renderer::initFrameDelivery(vk::UniqueSurfaceKHR a_surface, ResolutionProvider res_provider)
{
auto& ctx = etna::get_context();
resolutionProvider = std::move(res_provider);
commandManager = ctx.createPerFrameCmdMgr();
window = ctx.createWindow(etna::Window::CreateInfo{
.surface = std::move(a_surface),
});
auto [w, h] = window->recreateSwapchain(etna::Window::DesiredProperties{
.resolution = {resolution.x, resolution.y},
.vsync = true,
});
resolution = {w, h};
worldRenderer = std::make_unique<WorldRenderer>();
worldRenderer->allocateResources(resolution);
worldRenderer->loadShaders();
worldRenderer->setupPipelines(window->getCurrentFormat());
guiRenderer = std::make_unique<ImGuiRenderer>(window->getCurrentFormat());
}
void Renderer::recreateSwapchain(glm::uvec2 res)
{
auto& ctx = etna::get_context();
ETNA_CHECK_VK_RESULT(ctx.getDevice().waitIdle());
auto [w, h] = window->recreateSwapchain(etna::Window::DesiredProperties{
.resolution = {res.x, res.y},
.vsync = true,
});
resolution = {w, h};
// Most resources depend on the current resolution, so we recreate them.
worldRenderer->allocateResources(resolution);
// Format of the swapchain CAN change on android
worldRenderer->setupPipelines(window->getCurrentFormat());
}
void Renderer::loadScene(std::filesystem::path path)
{
worldRenderer->loadScene(path);
}
void Renderer::debugInput(const Keyboard& kb)
{
worldRenderer->debugInput(kb);
if (kb[KeyboardKey::kB] == ButtonState::Falling)
{
const int retval = std::system("cd " GRAPHICS_COURSE_ROOT "/build"
" && cmake --build . --target shadowmap_shaders");
if (retval != 0)
spdlog::warn("Shader recompilation returned a non-zero return code!");
else
{
ETNA_CHECK_VK_RESULT(etna::get_context().getDevice().waitIdle());
etna::reload_shaders();
spdlog::info("Successfully reloaded shaders!");
}
}
}
void Renderer::update(const FramePacket& packet)
{
worldRenderer->update(packet);
}
void Renderer::drawFrame()
{
ZoneScoped;
{
ZoneScopedN("drawGui");
guiRenderer->nextFrame();
ImGui::NewFrame();
worldRenderer->drawGui();
ImGui::Render();
}
auto currentCmdBuf = commandManager->acquireNext();
// TODO: this makes literally 0 sense here, rename/refactor,
// it doesn't actually begin anything, just resets descriptor pools
etna::begin_frame();
auto nextSwapchainImage = window->acquireNext();
// NOTE: here, we skip frames when the window is in the process of being
// re-sized. This is not mandatory, it is possible to submit frames to a
// "sub-optimal" swap chain and still get something drawn while resizing,
// but only on some platforms (not windows+nvidia, sadly).
if (nextSwapchainImage)
{
auto [image, view, availableSem, readyForPresentSem] = *nextSwapchainImage;
ETNA_CHECK_VK_RESULT(currentCmdBuf.begin(vk::CommandBufferBeginInfo{}));
{
ETNA_PROFILE_GPU(currentCmdBuf, renderFrame);
worldRenderer->renderWorld(currentCmdBuf, image, view);
{
ImDrawData* pDrawData = ImGui::GetDrawData();
guiRenderer->render(
currentCmdBuf, {{0, 0}, {resolution.x, resolution.y}}, image, view, pDrawData);
}
etna::set_state(
currentCmdBuf,
image,
vk::PipelineStageFlagBits2::eColorAttachmentOutput,
{},
vk::ImageLayout::ePresentSrcKHR,
vk::ImageAspectFlagBits::eColor);
etna::flush_barriers(currentCmdBuf);
ETNA_READ_BACK_GPU_PROFILING(currentCmdBuf);
}
ETNA_CHECK_VK_RESULT(currentCmdBuf.end());
auto renderingDone = commandManager->submit(
std::move(currentCmdBuf), std::move(availableSem), std::move(readyForPresentSem));
const bool presented = window->present(std::move(renderingDone), view);
if (!presented)
nextSwapchainImage = std::nullopt;
}
etna::end_frame();
if (!nextSwapchainImage)
{
auto res = resolutionProvider();
// On windows, we get 0,0 while the window is minimized and
// must skip frames until the window is un-minimized again
if (res.x != 0 && res.y != 0)
recreateSwapchain(res);
}
}
Renderer::~Renderer()
{
ETNA_CHECK_VK_RESULT(etna::get_context().getDevice().waitIdle());
}