1 | #include "vulkan-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 | #include "logger.hpp"
|
---|
7 |
|
---|
8 | #include "vulkan-utils.hpp"
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | VulkanGame::VulkanGame() {
|
---|
13 | gui = nullptr;
|
---|
14 | window = nullptr;
|
---|
15 | }
|
---|
16 |
|
---|
17 | VulkanGame::~VulkanGame() {
|
---|
18 | }
|
---|
19 |
|
---|
20 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
21 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
22 |
|
---|
23 | cout << "Vulkan Game" << endl;
|
---|
24 |
|
---|
25 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
---|
26 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
---|
27 | SDL_GetVersion(&sdlVersion);
|
---|
28 |
|
---|
29 | // TODO: Refactor the logger api to be more flexible,
|
---|
30 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
31 | restart_gl_log();
|
---|
32 | gl_log("starting SDL\n%s.%s.%s",
|
---|
33 | to_string(sdlVersion.major).c_str(),
|
---|
34 | to_string(sdlVersion.minor).c_str(),
|
---|
35 | to_string(sdlVersion.patch).c_str());
|
---|
36 |
|
---|
37 | open_log();
|
---|
38 | get_log() << "starting SDL" << endl;
|
---|
39 | get_log() <<
|
---|
40 | (int)sdlVersion.major << "." <<
|
---|
41 | (int)sdlVersion.minor << "." <<
|
---|
42 | (int)sdlVersion.patch << endl;
|
---|
43 |
|
---|
44 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
45 | return;
|
---|
46 | }
|
---|
47 |
|
---|
48 | initVulkan();
|
---|
49 | mainLoop();
|
---|
50 | cleanup();
|
---|
51 |
|
---|
52 | close_log();
|
---|
53 | }
|
---|
54 |
|
---|
55 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
56 | // amount of things initialized here keeps growing
|
---|
57 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
58 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
59 | gui = new GameGui_SDL();
|
---|
60 |
|
---|
61 | if (gui->init() == RTWO_ERROR) {
|
---|
62 | // TODO: Also print these sorts of errors to the log
|
---|
63 | cout << "UI library could not be initialized!" << endl;
|
---|
64 | cout << gui->getError() << endl;
|
---|
65 | return RTWO_ERROR;
|
---|
66 | }
|
---|
67 |
|
---|
68 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
69 | if (window == nullptr) {
|
---|
70 | cout << "Window could not be created!" << endl;
|
---|
71 | cout << gui->getError() << endl;
|
---|
72 | return RTWO_ERROR;
|
---|
73 | }
|
---|
74 |
|
---|
75 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
76 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
77 |
|
---|
78 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
---|
79 | if (renderer == nullptr) {
|
---|
80 | cout << "Renderer could not be created!" << endl;
|
---|
81 | cout << gui->getError() << endl;
|
---|
82 | return RTWO_ERROR;
|
---|
83 | }
|
---|
84 |
|
---|
85 | return RTWO_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void VulkanGame::initVulkan() {
|
---|
89 | const vector<const char*> validationLayers = {
|
---|
90 | "VK_LAYER_KHRONOS_validation"
|
---|
91 | };
|
---|
92 | const vector<const char*> deviceExtensions = {
|
---|
93 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
94 | };
|
---|
95 |
|
---|
96 | createVulkanInstance(validationLayers);
|
---|
97 | setupDebugMessenger();
|
---|
98 | createVulkanSurface();
|
---|
99 | pickPhysicalDevice(deviceExtensions);
|
---|
100 | }
|
---|
101 |
|
---|
102 | void VulkanGame::mainLoop() {
|
---|
103 | UIEvent e;
|
---|
104 | bool quit = false;
|
---|
105 |
|
---|
106 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
---|
107 |
|
---|
108 | while (!quit) {
|
---|
109 | gui->processEvents();
|
---|
110 |
|
---|
111 | while (gui->pollEvent(&e)) {
|
---|
112 | switch(e.type) {
|
---|
113 | case UI_EVENT_QUIT:
|
---|
114 | cout << "Quit event detected" << endl;
|
---|
115 | quit = true;
|
---|
116 | break;
|
---|
117 | case UI_EVENT_WINDOW:
|
---|
118 | cout << "Window event detected" << endl;
|
---|
119 | // Currently unused
|
---|
120 | break;
|
---|
121 | case UI_EVENT_KEY:
|
---|
122 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
123 | quit = true;
|
---|
124 | } else {
|
---|
125 | cout << "Key event detected" << endl;
|
---|
126 | }
|
---|
127 | break;
|
---|
128 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
129 | cout << "Mouse button down event detected" << endl;
|
---|
130 | break;
|
---|
131 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
132 | cout << "Mouse button up event detected" << endl;
|
---|
133 | break;
|
---|
134 | case UI_EVENT_MOUSEMOTION:
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | SDL_RenderClear(renderer);
|
---|
142 | SDL_RenderPresent(renderer);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void VulkanGame::cleanup() {
|
---|
147 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
148 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
149 | }
|
---|
150 | vkDestroyInstance(instance, nullptr);
|
---|
151 |
|
---|
152 | SDL_DestroyRenderer(renderer);
|
---|
153 | renderer = nullptr;
|
---|
154 |
|
---|
155 | gui->destroyWindow();
|
---|
156 | gui->shutdown();
|
---|
157 | delete gui;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
---|
161 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
---|
162 | throw runtime_error("validation layers requested, but not available!");
|
---|
163 | }
|
---|
164 |
|
---|
165 | VkApplicationInfo appInfo = {};
|
---|
166 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
167 | appInfo.pApplicationName = "Vulkan Game";
|
---|
168 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
169 | appInfo.pEngineName = "No Engine";
|
---|
170 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
171 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
172 |
|
---|
173 | VkInstanceCreateInfo createInfo = {};
|
---|
174 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
175 | createInfo.pApplicationInfo = &appInfo;
|
---|
176 |
|
---|
177 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
178 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
179 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
180 | }
|
---|
181 |
|
---|
182 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
183 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
184 |
|
---|
185 | cout << endl << "Extensions:" << endl;
|
---|
186 | for (const char* extensionName : extensions) {
|
---|
187 | cout << extensionName << endl;
|
---|
188 | }
|
---|
189 | cout << endl;
|
---|
190 |
|
---|
191 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
192 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
193 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
194 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
195 |
|
---|
196 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
197 | createInfo.pNext = &debugCreateInfo;
|
---|
198 | } else {
|
---|
199 | createInfo.enabledLayerCount = 0;
|
---|
200 |
|
---|
201 | createInfo.pNext = nullptr;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
205 | throw runtime_error("failed to create instance!");
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | void VulkanGame::setupDebugMessenger() {
|
---|
210 | if (!ENABLE_VALIDATION_LAYERS) return;
|
---|
211 |
|
---|
212 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
213 | populateDebugMessengerCreateInfo(createInfo);
|
---|
214 |
|
---|
215 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
216 | throw runtime_error("failed to set up debug messenger!");
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
221 | createInfo = {};
|
---|
222 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
223 | createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
---|
224 | createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
---|
225 | createInfo.pfnUserCallback = debugCallback;
|
---|
226 | }
|
---|
227 |
|
---|
228 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
---|
229 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
230 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
231 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
232 | void* pUserData) {
|
---|
233 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
234 |
|
---|
235 | return VK_FALSE;
|
---|
236 | }
|
---|
237 |
|
---|
238 | void VulkanGame::createVulkanSurface() {
|
---|
239 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
240 | throw runtime_error("failed to create window surface!");
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
---|
245 | uint32_t deviceCount = 0;
|
---|
246 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
247 |
|
---|
248 | if (deviceCount == 0) {
|
---|
249 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
250 | }
|
---|
251 |
|
---|
252 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
253 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
254 |
|
---|
255 | cout << endl << "Graphics cards:" << endl;
|
---|
256 | for (const VkPhysicalDevice& device : devices) {
|
---|
257 | if (isDeviceSuitable(device, deviceExtensions)) {
|
---|
258 | physicalDevice = device;
|
---|
259 | break;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | cout << endl;
|
---|
263 |
|
---|
264 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
265 | throw runtime_error("failed to find a suitable GPU!");
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device, const vector<const char*>& deviceExtensions) {
|
---|
270 | VkPhysicalDeviceProperties deviceProperties;
|
---|
271 | vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
---|
272 |
|
---|
273 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
274 |
|
---|
275 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(device, surface);
|
---|
276 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(device, deviceExtensions);
|
---|
277 | bool swapChainAdequate = false;
|
---|
278 |
|
---|
279 | if (extensionsSupported) {
|
---|
280 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(device, surface);
|
---|
281 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
282 | }
|
---|
283 |
|
---|
284 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
285 | vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
|
---|
286 |
|
---|
287 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
288 | }
|
---|