|
| 1 | +#include <iostream> |
| 2 | +#include <stdio.h> |
| 3 | + |
| 4 | +#include "vkvg.h" |
| 5 | +#include "vkh.h" |
| 6 | +#include "vkh_phyinfo.h" |
| 7 | + |
| 8 | +#include <GLFW/glfw3.h> |
| 9 | + |
| 10 | +#include "SampleApp.hpp" |
| 11 | +#include "VkvgTest.hpp" |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +bool SampleApp::try_get_phyinfo(VkhPhyInfo *phys, uint32_t phyCount, VkPhysicalDeviceType gpuType, VkhPhyInfo *phy) { |
| 16 | + for (uint32_t i = 0; i < phyCount; i++) { |
| 17 | + if (phys[i]->properties.deviceType == gpuType) { |
| 18 | + *phy = phys[i]; |
| 19 | + return true; |
| 20 | + } |
| 21 | + } |
| 22 | + return false; |
| 23 | +} |
| 24 | + |
| 25 | +static void glfw_error_callback(int error, const char *description) { |
| 26 | + fprintf(stderr, "vkengine: GLFW error %d: %s\n", error, description); |
| 27 | +} |
| 28 | +void SampleApp::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { |
| 29 | + if (action != GLFW_PRESS) |
| 30 | + return; |
| 31 | + switch (key) { |
| 32 | + case GLFW_KEY_SPACE: |
| 33 | + //paused = !paused; |
| 34 | + break; |
| 35 | + case GLFW_KEY_ESCAPE: |
| 36 | + glfwSetWindowShouldClose(window, GLFW_TRUE); |
| 37 | + break; |
| 38 | + } |
| 39 | +} |
| 40 | +static void char_callback(GLFWwindow *window, uint32_t c) {} |
| 41 | +static void mouse_move_callback(GLFWwindow *window, double x, double y) {} |
| 42 | +static void scroll_callback(GLFWwindow *window, double x, double y) {} |
| 43 | +static void mouse_button_callback(GLFWwindow *window, int but, int state, int modif) {} |
| 44 | + |
| 45 | + |
| 46 | +void SampleApp::Init() { |
| 47 | + glfwSetErrorCallback(glfw_error_callback); |
| 48 | + |
| 49 | + if (!glfwInit()) { |
| 50 | + perror("glfwInit failed"); |
| 51 | + exit(-1); |
| 52 | + } |
| 53 | + |
| 54 | + if (!glfwVulkanSupported()) { |
| 55 | + perror("glfwVulkanSupported return false."); |
| 56 | + exit(-1); |
| 57 | + } |
| 58 | + const char *enabledLayers[10]; |
| 59 | + const char *enabledExts[10]; |
| 60 | + uint32_t enabledExtsCount = 0, enabledLayersCount = 0, phyCount = 0; |
| 61 | + |
| 62 | + vkh_layers_check_init(); |
| 63 | +#ifdef VKVG_USE_VALIDATION |
| 64 | + if (vkh_layer_is_present("VK_LAYER_KHRONOS_validation")) |
| 65 | + enabledLayers[enabledLayersCount++] = "VK_LAYER_KHRONOS_validation"; |
| 66 | +#endif |
| 67 | +#ifdef VKVG_USE_MESA_OVERLAY |
| 68 | + if (vkh_layer_is_present("VK_LAYER_MESA_overlay")) |
| 69 | + enabledLayers[enabledLayersCount++] = "VK_LAYER_MESA_overlay"; |
| 70 | +#endif |
| 71 | + |
| 72 | +#ifdef VKVG_USE_RENDERDOC |
| 73 | + if (vkh_layer_is_present("VK_LAYER_RENDERDOC_Capture")) |
| 74 | + enabledLayers[enabledLayersCount++] = "VK_LAYER_RENDERDOC_Capture"; |
| 75 | +#endif |
| 76 | + vkh_layers_check_release(); |
| 77 | + |
| 78 | + uint32_t glfwReqExtsCount = 0; |
| 79 | + const char **gflwExts = glfwGetRequiredInstanceExtensions(&glfwReqExtsCount); |
| 80 | + |
| 81 | + vkvg_get_required_instance_extensions(enabledExts, &enabledExtsCount); |
| 82 | + |
| 83 | + for (uint32_t i = 0; i < glfwReqExtsCount; i++) |
| 84 | + enabledExts[i + enabledExtsCount] = gflwExts[i]; |
| 85 | + |
| 86 | + enabledExtsCount += glfwReqExtsCount; |
| 87 | + |
| 88 | + app = vkh_app_create(1, 2, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts); |
| 89 | + |
| 90 | +#if defined(DEBUG) && defined(VKVG_DBG_UTILS) |
| 91 | + uint32_t severity = 0; |
| 92 | + for(const uint32_t& s : logSeverity) |
| 93 | + severity += pow(16, s); |
| 94 | + |
| 95 | + vkh_app_enable_debug_messenger(app, |
| 96 | + (VkDebugUtilsMessageTypeFlagBitsEXT)logType, |
| 97 | + (VkDebugUtilsMessageSeverityFlagBitsEXT)severity, |
| 98 | + NULL); |
| 99 | +#endif |
| 100 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 101 | + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); |
| 102 | + glfwWindowHint(GLFW_FLOATING, GLFW_FALSE); |
| 103 | + glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); |
| 104 | + |
| 105 | + win = glfwCreateWindow((int)width, (int)height, "Window Title", NULL, NULL); |
| 106 | + |
| 107 | + glfwCreateWindowSurface(vkh_app_get_inst(app), win, NULL, &vkSurf); |
| 108 | + |
| 109 | + VkhPhyInfo *phys = vkh_app_get_phyinfos(app, &phyCount, vkSurf); |
| 110 | + |
| 111 | + if (listGpus) { |
| 112 | + std::cout << "Available GPU's:" << std::endl; |
| 113 | + std::cout << "================" << std::endl; |
| 114 | + for (uint32_t i = 0; i < phyCount; i++) { |
| 115 | + std::cout << "\t" << i << ": " << phys[i]->properties.deviceName << std::endl; |
| 116 | + } |
| 117 | + vkh_app_free_phyinfos(phyCount, phys); |
| 118 | + vkDestroySurfaceKHR(vkh_app_get_inst(app), vkSurf, NULL); |
| 119 | + glfwDestroyWindow(win); |
| 120 | + vkh_app_destroy(app); |
| 121 | + glfwTerminate(); |
| 122 | + exit(0); |
| 123 | + } |
| 124 | + |
| 125 | + VkhPhyInfo pi = 0; |
| 126 | + if (gpuIndex > 0 && gpuIndex < phyCount) { |
| 127 | + pi = phys[gpuIndex]; |
| 128 | + } else { |
| 129 | + if (!try_get_phyinfo(phys, phyCount, preferedGPU, &pi) && |
| 130 | + !try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU, &pi) && |
| 131 | + !try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU, &pi)) |
| 132 | + pi = phys[0]; |
| 133 | + } |
| 134 | + assert(pi && "No vulkan physical device found."); |
| 135 | + |
| 136 | + memory_properties = pi->memProps; |
| 137 | + gpu_props = pi->properties; |
| 138 | + |
| 139 | + uint32_t qCount = 0; |
| 140 | + float qPriorities[] = {0.0}; |
| 141 | + |
| 142 | + VkDeviceQueueCreateInfo pQueueInfos[] = {{}, {}, {}}; |
| 143 | + if (vkh_phyinfo_create_presentable_queues(pi, 1, qPriorities, &pQueueInfos[qCount])) |
| 144 | + qCount++; |
| 145 | + /*if (vkh_phyinfo_create_compute_queues (pi, 1, qPriorities, &pQueueInfos[qCount])) |
| 146 | + qCount++; |
| 147 | + if (vkh_phyinfo_create_transfer_queues (pi, 1, qPriorities, &pQueueInfos[qCount])) |
| 148 | + qCount++;*/ |
| 149 | + |
| 150 | + enabledExtsCount = 0; |
| 151 | + |
| 152 | + if (vkvg_get_required_device_extensions(pi->phy, enabledExts, &enabledExtsCount) != VKVG_STATUS_SUCCESS) { |
| 153 | + perror("vkvg_get_required_device_extensions failed, enable log for details.\n"); |
| 154 | + exit(-1); |
| 155 | + } |
| 156 | + TRY_LOAD_DEVICE_EXT(VK_KHR_swapchain) |
| 157 | + |
| 158 | + VkPhysicalDeviceFeatures enabledFeatures{}; |
| 159 | + const void *pNext = vkvg_get_device_requirements(&enabledFeatures); |
| 160 | + |
| 161 | + VkDeviceCreateInfo device_info = {.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 162 | + .pNext = pNext, |
| 163 | + .queueCreateInfoCount = qCount, |
| 164 | + .pQueueCreateInfos = (VkDeviceQueueCreateInfo *)&pQueueInfos, |
| 165 | + .enabledExtensionCount = enabledExtsCount, |
| 166 | + .ppEnabledExtensionNames = enabledExts, |
| 167 | + .pEnabledFeatures = &enabledFeatures, |
| 168 | + }; |
| 169 | + |
| 170 | + vkhDev = vkh_device_create(app, pi, &device_info); |
| 171 | + presentQIndex = (uint32_t)pi->pQueue; |
| 172 | + renderer = |
| 173 | + vkh_presenter_create(vkhDev, presentQIndex, vkSurf, width, height, VK_FORMAT_B8G8R8A8_SRGB, presentMode); |
| 174 | + |
| 175 | + |
| 176 | + vkh_app_free_phyinfos(phyCount, phys); |
| 177 | + |
| 178 | + glfwSetKeyCallback(win, key_callback); |
| 179 | + glfwSetMouseButtonCallback(win, mouse_button_callback); |
| 180 | + glfwSetCursorPosCallback(win, mouse_move_callback); |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | +void SampleApp::Run() { |
| 185 | + uint32_t curIter = 0; |
| 186 | + |
| 187 | + if (VkvgTest::tests.empty()) |
| 188 | + return; |
| 189 | + |
| 190 | + int testIdx = 0; |
| 191 | + auto it = VkvgTest::tests.begin(); |
| 192 | + VkvgTest* curTest = NULL; |
| 193 | + |
| 194 | + if (testsToRun.empty()) { |
| 195 | + curTest = (*it); |
| 196 | + } else { |
| 197 | + curTest = VkvgTest::tests[testsToRun[testIdx]]; |
| 198 | + } |
| 199 | + |
| 200 | + glfwSetWindowTitle(win, curTest->name.c_str()); |
| 201 | + curTest->initTest(this); |
| 202 | + |
| 203 | + while (!glfwWindowShouldClose(win)) { |
| 204 | + glfwPollEvents(); |
| 205 | + |
| 206 | + curTest->performTest(); |
| 207 | + |
| 208 | + if (!vkh_presenter_draw(renderer)) { |
| 209 | + vkh_presenter_get_size(renderer, &width, &height); |
| 210 | + curTest->cleanTest(); |
| 211 | + curTest->initTest(this); |
| 212 | + continue; |
| 213 | + } |
| 214 | + |
| 215 | + if (iterations > 0) { |
| 216 | + if (++curIter == iterations) { |
| 217 | + testIdx++; |
| 218 | + curTest->cleanTest(); |
| 219 | + if (testsToRun.empty()) { |
| 220 | + if (++it == VkvgTest::tests.end()){ |
| 221 | + curTest = NULL; |
| 222 | + break; |
| 223 | + } |
| 224 | + curTest = (*it); |
| 225 | + } else { |
| 226 | + if (testIdx == testsToRun.size()) { |
| 227 | + curTest = NULL; |
| 228 | + break; |
| 229 | + } |
| 230 | + curTest = VkvgTest::tests[testsToRun[testIdx]]; |
| 231 | + } |
| 232 | + |
| 233 | + curIter = 0; |
| 234 | + glfwSetWindowTitle(win, curTest->name.c_str()); |
| 235 | + curTest->initTest(this); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + if (curTest != NULL) |
| 240 | + curTest->cleanTest(); |
| 241 | +} |
| 242 | + |
| 243 | +void SampleApp::CleanUp() { |
| 244 | + vkDeviceWaitIdle(vkh_device_get_vkdev(vkhDev)); |
| 245 | + vkh_presenter_destroy(renderer); |
| 246 | + vkDestroySurfaceKHR(vkh_app_get_inst(app), vkSurf, NULL); |
| 247 | + |
| 248 | + vkh_device_destroy(vkhDev); |
| 249 | + glfwDestroyWindow(win); |
| 250 | + vkh_app_destroy(app); |
| 251 | + |
| 252 | + glfwTerminate(); |
| 253 | +} |
0 commit comments