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 |
|
---|
93 | createVulkanInstance(validationLayers);
|
---|
94 | setupDebugMessenger();
|
---|
95 | createVulkanSurface();
|
---|
96 | pickPhysicalDevice();
|
---|
97 | }
|
---|
98 |
|
---|
99 | void VulkanGame::mainLoop() {
|
---|
100 | UIEvent e;
|
---|
101 | bool quit = false;
|
---|
102 |
|
---|
103 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
---|
104 |
|
---|
105 | while (!quit) {
|
---|
106 | gui->processEvents();
|
---|
107 |
|
---|
108 | while (gui->pollEvent(&e)) {
|
---|
109 | switch(e.type) {
|
---|
110 | case UI_EVENT_QUIT:
|
---|
111 | cout << "Quit event detected" << endl;
|
---|
112 | quit = true;
|
---|
113 | break;
|
---|
114 | case UI_EVENT_WINDOW:
|
---|
115 | cout << "Window event detected" << endl;
|
---|
116 | // Currently unused
|
---|
117 | break;
|
---|
118 | case UI_EVENT_KEY:
|
---|
119 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
120 | quit = true;
|
---|
121 | } else {
|
---|
122 | cout << "Key event detected" << endl;
|
---|
123 | }
|
---|
124 | break;
|
---|
125 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
126 | cout << "Mouse button down event detected" << endl;
|
---|
127 | break;
|
---|
128 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
129 | cout << "Mouse button up event detected" << endl;
|
---|
130 | break;
|
---|
131 | case UI_EVENT_MOUSEMOTION:
|
---|
132 | break;
|
---|
133 | default:
|
---|
134 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | SDL_RenderClear(renderer);
|
---|
139 | SDL_RenderPresent(renderer);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | void VulkanGame::cleanup() {
|
---|
144 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
145 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
146 | }
|
---|
147 | vkDestroyInstance(instance, nullptr);
|
---|
148 |
|
---|
149 | SDL_DestroyRenderer(renderer);
|
---|
150 | renderer = nullptr;
|
---|
151 |
|
---|
152 | gui->destroyWindow();
|
---|
153 | gui->shutdown();
|
---|
154 | delete gui;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
---|
158 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
---|
159 | throw runtime_error("validation layers requested, but not available!");
|
---|
160 | }
|
---|
161 |
|
---|
162 | VkApplicationInfo appInfo = {};
|
---|
163 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
164 | appInfo.pApplicationName = "Vulkan Game";
|
---|
165 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
166 | appInfo.pEngineName = "No Engine";
|
---|
167 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
168 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
169 |
|
---|
170 | VkInstanceCreateInfo createInfo = {};
|
---|
171 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
172 | createInfo.pApplicationInfo = &appInfo;
|
---|
173 |
|
---|
174 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
175 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
176 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
177 | }
|
---|
178 |
|
---|
179 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
180 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
181 |
|
---|
182 | cout << endl << "Extensions:" << endl;
|
---|
183 | for (const char* extensionName : extensions) {
|
---|
184 | cout << extensionName << endl;
|
---|
185 | }
|
---|
186 | cout << endl;
|
---|
187 |
|
---|
188 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
189 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
190 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
191 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
192 |
|
---|
193 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
194 | createInfo.pNext = &debugCreateInfo;
|
---|
195 | } else {
|
---|
196 | createInfo.enabledLayerCount = 0;
|
---|
197 |
|
---|
198 | createInfo.pNext = nullptr;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
202 | throw runtime_error("failed to create instance!");
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void VulkanGame::setupDebugMessenger() {
|
---|
207 | if (!ENABLE_VALIDATION_LAYERS) return;
|
---|
208 |
|
---|
209 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
210 | populateDebugMessengerCreateInfo(createInfo);
|
---|
211 |
|
---|
212 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
213 | throw runtime_error("failed to set up debug messenger!");
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
218 | createInfo = {};
|
---|
219 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
220 | 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;
|
---|
221 | 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;
|
---|
222 | createInfo.pfnUserCallback = debugCallback;
|
---|
223 | }
|
---|
224 |
|
---|
225 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
---|
226 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
227 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
228 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
229 | void* pUserData) {
|
---|
230 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
231 |
|
---|
232 | return VK_FALSE;
|
---|
233 | }
|
---|
234 |
|
---|
235 | void VulkanGame::createVulkanSurface() {
|
---|
236 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
237 | throw runtime_error("failed to create window surface!");
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | void VulkanGame::pickPhysicalDevice() {
|
---|
242 | uint32_t deviceCount = 0;
|
---|
243 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
244 |
|
---|
245 | if (deviceCount == 0) {
|
---|
246 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
247 | }
|
---|
248 |
|
---|
249 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
250 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
251 |
|
---|
252 | cout << endl << "Graphics cards:" << endl;
|
---|
253 | for (const VkPhysicalDevice& device : devices) {
|
---|
254 | if (isDeviceSuitable(device)) {
|
---|
255 | physicalDevice = device;
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | cout << endl;
|
---|
260 |
|
---|
261 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
262 | throw runtime_error("failed to find a suitable GPU!");
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device) {
|
---|
267 | const vector<const char*> deviceExtensions = {
|
---|
268 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
269 | };
|
---|
270 |
|
---|
271 | VkPhysicalDeviceProperties deviceProperties;
|
---|
272 | vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
---|
273 |
|
---|
274 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
275 |
|
---|
276 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(device, surface);
|
---|
277 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(device, deviceExtensions);
|
---|
278 | bool swapChainAdequate = false;
|
---|
279 |
|
---|
280 | if (extensionsSupported) {
|
---|
281 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(device, surface);
|
---|
282 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
283 | }
|
---|
284 |
|
---|
285 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
286 | vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
|
---|
287 |
|
---|
288 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
289 | }
|
---|