[99d44b2] | 1 | #include "vulkan-game.hpp"
|
---|
[850e84c] | 2 |
|
---|
[0df3c9a] | 3 | #include <iostream>
|
---|
| 4 |
|
---|
[5edbd58] | 5 | #include "consts.hpp"
|
---|
[c559904] | 6 | #include "logger.hpp"
|
---|
[5edbd58] | 7 |
|
---|
[c1d9b2a] | 8 | #include "vulkan-utils.hpp"
|
---|
| 9 |
|
---|
[0df3c9a] | 10 | using namespace std;
|
---|
| 11 |
|
---|
[99d44b2] | 12 | VulkanGame::VulkanGame() {
|
---|
[0df3c9a] | 13 | gui = nullptr;
|
---|
| 14 | window = nullptr;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[99d44b2] | 17 | VulkanGame::~VulkanGame() {
|
---|
[0df3c9a] | 18 | }
|
---|
| 19 |
|
---|
[b6e60b4] | 20 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
[2e77b3f] | 21 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
| 22 |
|
---|
| 23 | cout << "Vulkan Game" << endl;
|
---|
| 24 |
|
---|
[c559904] | 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 |
|
---|
[5edbd58] | 44 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
[0df3c9a] | 45 | return;
|
---|
| 46 | }
|
---|
[b6e60b4] | 47 |
|
---|
[0df3c9a] | 48 | initVulkan();
|
---|
| 49 | mainLoop();
|
---|
| 50 | cleanup();
|
---|
[c559904] | 51 |
|
---|
| 52 | close_log();
|
---|
[0df3c9a] | 53 | }
|
---|
| 54 |
|
---|
[c559904] | 55 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
| 56 | // amount of things initialized here keeps growing
|
---|
[b6e60b4] | 57 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
[c559904] | 58 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
[0df3c9a] | 59 | gui = new GameGui_SDL();
|
---|
| 60 |
|
---|
[b6e60b4] | 61 | if (gui->init() == RTWO_ERROR) {
|
---|
[c559904] | 62 | // TODO: Also print these sorts of errors to the log
|
---|
[0df3c9a] | 63 | cout << "UI library could not be initialized!" << endl;
|
---|
[b6e60b4] | 64 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 65 | return RTWO_ERROR;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[b6e60b4] | 68 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
[0df3c9a] | 69 | if (window == nullptr) {
|
---|
| 70 | cout << "Window could not be created!" << endl;
|
---|
[ed7c953] | 71 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 72 | return RTWO_ERROR;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[b6e60b4] | 75 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
[a6f6833] | 76 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
[b6e60b4] | 77 |
|
---|
[c1d9b2a] | 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 |
|
---|
[0df3c9a] | 85 | return RTWO_SUCCESS;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[99d44b2] | 88 | void VulkanGame::initVulkan() {
|
---|
[c1d9b2a] | 89 | const vector<const char*> validationLayers = {
|
---|
| 90 | "VK_LAYER_KHRONOS_validation"
|
---|
| 91 | };
|
---|
[fe5c3ba] | 92 | const vector<const char*> deviceExtensions = {
|
---|
| 93 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
| 94 | };
|
---|
[c1d9b2a] | 95 |
|
---|
| 96 | createVulkanInstance(validationLayers);
|
---|
| 97 | setupDebugMessenger();
|
---|
[90a424f] | 98 | createVulkanSurface();
|
---|
[fe5c3ba] | 99 | pickPhysicalDevice(deviceExtensions);
|
---|
[0df3c9a] | 100 | }
|
---|
| 101 |
|
---|
[99d44b2] | 102 | void VulkanGame::mainLoop() {
|
---|
[f6521fb] | 103 | UIEvent e;
|
---|
[0df3c9a] | 104 | bool quit = false;
|
---|
| 105 |
|
---|
[c1d9b2a] | 106 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
---|
| 107 |
|
---|
[0df3c9a] | 108 | while (!quit) {
|
---|
[27c40ce] | 109 | gui->processEvents();
|
---|
| 110 |
|
---|
[f6521fb] | 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;
|
---|
[c61323a] | 136 | default:
|
---|
| 137 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
[0df3c9a] | 138 | }
|
---|
| 139 | }
|
---|
[c1d9b2a] | 140 |
|
---|
| 141 | SDL_RenderClear(renderer);
|
---|
| 142 | SDL_RenderPresent(renderer);
|
---|
[0df3c9a] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[99d44b2] | 146 | void VulkanGame::cleanup() {
|
---|
[c1d9b2a] | 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 |
|
---|
[b6e60b4] | 155 | gui->destroyWindow();
|
---|
| 156 | gui->shutdown();
|
---|
[0df3c9a] | 157 | delete gui;
|
---|
[c1d9b2a] | 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 | }
|
---|
[90a424f] | 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 |
|
---|
[fe5c3ba] | 244 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 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) {
|
---|
[fe5c3ba] | 257 | if (isDeviceSuitable(device, deviceExtensions)) {
|
---|
[90a424f] | 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 |
|
---|
[fe5c3ba] | 269 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device, const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 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 | } |
---|