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
150 lines (114 loc) · 3.84 KB
/
Renderer.cpp
File metadata and controls
150 lines (114 loc) · 3.84 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
#include "Renderer.hpp"
#include <etna/GlobalContext.hpp>
#include <etna/Etna.hpp>
#include <etna/RenderTargetStates.hpp>
#include <etna/PipelineManager.hpp>
#include <etna/Profiling.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 = "model_bakery_renderer",
.applicationVersion = VK_MAKE_VERSION(0, 1, 0),
.instanceExtensions = instanceExtensions,
.deviceExtensions = deviceExtensions,
.features = vk::PhysicalDeviceFeatures2{.features = {}},
.physicalDeviceIndexOverride = {},
.numFramesInFlight = 2,
});
}
void Renderer::initFrameDelivery(vk::UniqueSurfaceKHR a_surface, ResolutionProvider res_provider)
{
resolutionProvider = std::move(res_provider);
auto& ctx = etna::get_context();
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 = useVsync,
});
resolution = {w, h};
worldRenderer = std::make_unique<WorldRenderer>();
worldRenderer->allocateResources(resolution);
worldRenderer->loadShaders();
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 model_bakery_renderer_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;
auto currentCmdBuf = commandManager->acquireNext();
etna::begin_frame();
auto nextSwapchainImage = window->acquireNext();
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);
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;
}
if (!nextSwapchainImage && resolutionProvider() != glm::uvec2{0, 0})
{
auto [w, h] = window->recreateSwapchain(etna::Window::DesiredProperties{
.resolution = {resolution.x, resolution.y},
.vsync = useVsync,
});
ETNA_VERIFY((resolution == glm::uvec2{w, h}));
}
etna::end_frame();
}
Renderer::~Renderer()
{
ETNA_CHECK_VK_RESULT(etna::get_context().getDevice().waitIdle());
}