Changeset b6127d2 in opengl-game
- Timestamp:
- Jul 3, 2019, 2:24:05 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 7dcd925
- Parents:
- ab65f84
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
README.txt
rab65f84 rb6127d2 62 62 Download the vulkan sdk (make sure VULKAN_SDK_PATH in the makefile points to it) 63 63 64 source setup-env.sh into your current shell 65 64 66 make vulkcangame && ./vulkangame 65 67 -
vulkan-game.cpp
rab65f84 rb6127d2 20 20 using namespace std; 21 21 using namespace glm; 22 23 bool checkValidationLayerSupport(); 24 vector<const char*> getRequiredExtensions(SDL_Window* window); 22 25 23 26 const int SCREEN_WIDTH = 800; … … 34 37 #endif 35 38 39 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( 40 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, 41 VkDebugUtilsMessageTypeFlagsEXT messageType, 42 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, 43 void* pUserData) { 44 cerr << "validation layer: " << pCallbackData->pMessage << endl; 45 46 return VK_FALSE; 47 } 48 49 VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, 50 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, 51 const VkAllocationCallbacks* pAllocator, 52 VkDebugUtilsMessengerEXT* pDebugMessenger) { 53 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr( 54 instance, "vkCreateDebugUtilsMessengerEXT"); 55 56 if (func != nullptr) { 57 return func(instance, pCreateInfo, pAllocator, pDebugMessenger); 58 } else { 59 return VK_ERROR_EXTENSION_NOT_PRESENT; 60 } 61 } 62 36 63 class VulkanGame { 37 64 public: … … 50 77 51 78 VkInstance instance; 79 VkDebugUtilsMessengerEXT debugMessenger; 52 80 53 81 // both SDL and GLFW create window functions return NULL on failure … … 77 105 void initVulkan() { 78 106 createInstance(); 107 setupDebugMessenger(); 79 108 } 80 109 81 110 void createInstance() { 111 if (enableValidationLayers && !checkValidationLayerSupport()) { 112 throw runtime_error("validation layers requested, but not available!"); 113 } 114 82 115 VkApplicationInfo appInfo = {}; 83 116 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; … … 92 125 createInfo.pApplicationInfo = &appInfo; 93 126 94 uint32_t extensionCount; 95 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr); 96 97 vector<const char*> extensionNames(extensionCount); 98 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensionNames.data()); 99 100 createInfo.enabledExtensionCount = extensionCount; 101 createInfo.ppEnabledExtensionNames = extensionNames.data(); 102 createInfo.enabledLayerCount = 0; 127 auto extensions = getRequiredExtensions(window); 128 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); 129 createInfo.ppEnabledExtensionNames = extensions.data(); 130 131 if (enableValidationLayers) { 132 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); 133 createInfo.ppEnabledLayerNames = validationLayers.data(); 134 } else { 135 createInfo.enabledLayerCount = 0; 136 } 103 137 104 138 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { 105 139 throw runtime_error("failed to create instance!"); 140 } 141 } 142 143 void setupDebugMessenger() { 144 if (!enableValidationLayers) return; 145 146 VkDebugUtilsMessengerCreateInfoEXT createInfo = {}; 147 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; 148 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; 149 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; 150 createInfo.pfnUserCallback = debugCallback; 151 createInfo.pUserData = nullptr; 152 153 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) { 154 throw runtime_error("failed to setup debug messenger!"); 106 155 } 107 156 } … … 156 205 }; 157 206 207 vector<const char*> getRequiredExtensions(SDL_Window* window) { 208 uint32_t extensionCount; 209 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr); 210 211 vector<const char*> extensions(extensionCount); 212 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data()); 213 214 if (enableValidationLayers) { 215 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); 216 } 217 218 return extensions; 219 } 220 221 bool checkValidationLayerSupport() { 222 uint32_t layerCount; 223 vkEnumerateInstanceLayerProperties(&layerCount, nullptr); 224 225 vector<VkLayerProperties> availableLayers(layerCount); 226 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); 227 228 for (const char* layerName : validationLayers) { 229 bool layerFound = false; 230 231 for (const auto& layerProperties : availableLayers) { 232 if (strcmp(layerName, layerProperties.layerName) == 0) { 233 layerFound = true; 234 break; 235 } 236 } 237 238 if (!layerFound) { 239 return false; 240 } 241 } 242 243 return true; 244 } 245 158 246 int main() { 159 247 … … 163 251 cout << "DEBUGGING IS ON" << endl; 164 252 #endif 165 166 /*167 glfwInit();168 169 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);170 GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);171 172 uint32_t extensionCount = 0;173 vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);174 175 cout << extensionCount << " extensions supported" << endl;176 */177 178 /*179 while(!glfwWindowShouldClose(window)) {180 glfwPollEvents();181 }182 183 glfwDestroyWindow(window);184 185 glfwTerminate();186 */187 188 253 /* 189 254 mat4 matrix;
Note:
See TracChangeset
for help on using the changeset viewer.