Changeset 8b823e7 in opengl-game
- Timestamp:
- Jan 24, 2021, 11:38:43 PM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- d8cf709
- Parents:
- ce9dc9f
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sdl-game.cpp
rce9dc9f r8b823e7 6 6 #include <stdexcept> 7 7 8 #include <stdlib.h> // abort (only used in check_vk_result)9 10 8 #include <SDL2/SDL_vulkan.h> 11 9 … … 20 18 static bool g_SwapChainRebuild = false; 21 19 22 static void check_ vk_result(VkResult err) {23 if ( err == 0) {20 static void check_imgui_vk_result(VkResult res) { 21 if (res == VK_SUCCESS) { 24 22 return; 25 23 } 26 fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err); 27 if (err < 0) { 28 abort(); 24 25 ostringstream oss; 26 oss << "[imgui] Vulkan error! VkResult is \"" << VulkanUtils::resultString(res) << "\"" << __LINE__; 27 if (res < 0) { 28 throw runtime_error("Fatal: " + oss.str()); 29 } else { 30 cerr << oss.str(); 29 31 } 30 32 } … … 37 39 g_SwapChainRebuild = true; 38 40 return; 39 } else if (result != VK_SUCCESS) { 40 throw runtime_error("failed to acquire swap chain image!"); 41 } 42 43 if (vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()) != VK_SUCCESS) { 44 throw runtime_error("failed waiting for fence!"); 45 } 46 if (vkResetFences(device, 1, &inFlightFences[imageIndex]) != VK_SUCCESS) { 47 throw runtime_error("failed to reset fence!"); 48 } 41 } else { 42 VKUTIL_CHECK_RESULT(result, "failed to acquire swap chain image!"); 43 } 44 45 VKUTIL_CHECK_RESULT(vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()), 46 "failed waiting for fence!"); 47 48 VKUTIL_CHECK_RESULT(vkResetFences(device, 1, &inFlightFences[imageIndex]), 49 "failed to reset fence!"); 49 50 50 51 // START OF NEW CODE … … 52 53 // before the render loop ever starts. I should change this 53 54 54 result = vkResetCommandPool(device, commandPools[imageIndex], 0); 55 check_vk_result(result); 55 VKUTIL_CHECK_RESULT(vkResetCommandPool(device, commandPools[imageIndex], 0), 56 "failed to reset command pool!"); 57 56 58 VkCommandBufferBeginInfo info = {}; 57 59 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 58 60 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 59 result = vkBeginCommandBuffer(commandBuffers[imageIndex], &info); 60 check_vk_result(result); 61 62 VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers[imageIndex], &info), 63 "failed to begin recording command buffer!"); 61 64 62 65 VkRenderPassBeginInfo renderPassInfo = {}; … … 81 84 vkCmdEndRenderPass(commandBuffers[imageIndex]); 82 85 83 if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS) { 84 throw runtime_error("failed to record command buffer!"); 85 } 86 VKUTIL_CHECK_RESULT(vkEndCommandBuffer(commandBuffers[imageIndex]), 87 "failed to record command buffer!"); 86 88 87 89 // END OF NEW CODE … … 101 103 submitInfo.pSignalSemaphores = signalSemaphores; 102 104 103 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) { 104 throw runtime_error("failed to submit draw command buffer!"); 105 } 105 VKUTIL_CHECK_RESULT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]), 106 "failed to submit draw command buffer!"); 106 107 } 107 108 … … 196 197 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes); 197 198 pool_info.pPoolSizes = pool_sizes; 198 check_vk_result(vkCreateDescriptorPool(device, &pool_info, nullptr, &descriptorPool)); 199 200 VKUTIL_CHECK_RESULT(vkCreateDescriptorPool(device, &pool_info, nullptr, &descriptorPool), 201 "failed to create descriptor pool"); 199 202 } 200 203 … … 225 228 init_info.MinImageCount = swapChainMinImageCount; 226 229 init_info.ImageCount = swapChainImageCount; 227 init_info.CheckVkResultFn = check_ vk_result;230 init_info.CheckVkResultFn = check_imgui_vk_result; 228 231 ImGui_ImplVulkan_Init(&init_info, renderPass); 229 232 -
vulkan-game.cpp
rce9dc9f r8b823e7 38 38 */ 39 39 40 // Put in here to use for IMGUI, but I might use something similar in other places as well 41 static void check_vk_result(VkResult result) { 42 if (result == VK_SUCCESS) 40 static void check_imgui_vk_result(VkResult res) { 41 if (res == VK_SUCCESS) { 43 42 return; 44 fprintf(stderr, "[vulkan] Error: VkResult = %d\n", result); 45 if (result < 0) 46 abort(); 43 } 44 45 ostringstream oss; 46 oss << "[imgui] Vulkan error! VkResult is \"" << VulkanUtils::resultString(res) << "\"" << __LINE__; 47 if (res < 0) { 48 throw runtime_error("Fatal: " + oss.str()); 49 } else { 50 cerr << oss.str(); 51 } 47 52 } 48 53 … … 251 256 init_info.MinImageCount = this->swapChainMinImageCount; 252 257 init_info.ImageCount = this->swapChainImageCount; 253 init_info.CheckVkResultFn = check_ vk_result;258 init_info.CheckVkResultFn = check_imgui_vk_result; 254 259 ImGui_ImplVulkan_Init(&init_info, this->renderPass); 255 260 … … 261 266 // Upload Fonts 262 267 { 263 VkResult err;264 265 268 VkCommandBuffer command_buffer; 266 269 … … 271 274 info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 272 275 info.commandBufferCount = 1; 273 err = vkAllocateCommandBuffers(this->device, &info, &command_buffer); 274 check_vk_result(err); 276 277 VKUTIL_CHECK_RESULT(vkAllocateCommandBuffers(this->device, &info, &command_buffer), 278 "failed to allocate command buffers!"); 275 279 276 280 //err = vkResetCommandPool(this->device, command_pool, 0); // Probably not really needed here since the command pool is never used before this … … 279 283 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 280 284 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 281 err = vkBeginCommandBuffer(command_buffer, &begin_info);282 check_vk_result(err);285 VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(command_buffer, &begin_info), 286 "failed to begin recording command buffer!"); 283 287 284 288 ImGui_ImplVulkan_CreateFontsTexture(command_buffer); … … 288 292 end_info.commandBufferCount = 1; 289 293 end_info.pCommandBuffers = &command_buffer; 290 err = vkEndCommandBuffer(command_buffer); 291 check_vk_result(err); 292 err = vkQueueSubmit(this->graphicsQueue, 1, &end_info, VK_NULL_HANDLE); 293 check_vk_result(err); 294 295 VKUTIL_CHECK_RESULT(vkEndCommandBuffer(command_buffer), 296 "failed to record command buffer!"); 297 298 VKUTIL_CHECK_RESULT(vkQueueSubmit(this->graphicsQueue, 1, &end_info, VK_NULL_HANDLE), 299 "failed to submit draw command buffer!"); 294 300 295 301 if (vkDeviceWaitIdle(this->device) != VK_SUCCESS) { … … 300 306 301 307 // This should make the command pool reusable for later 302 err = vkResetCommandPool(this->device, resourceCommandPool, 0);303 check_vk_result(err);308 VKUTIL_CHECK_RESULT(vkResetCommandPool(this->device, resourceCommandPool, 0), 309 "failed to reset command pool!"); 304 310 } 305 311 -
vulkan-utils.cpp
rce9dc9f r8b823e7 8 8 #define STB_IMAGE_IMPLEMENTATION 9 9 #include "stb_image.h" // TODO: Probably switch to SDL_image 10 11 string VulkanUtils::resultString(VkResult result) { 12 #define STR(r) case VK_ ##r: return #r 13 14 switch (result) { 15 STR(NOT_READY); 16 STR(TIMEOUT); 17 STR(EVENT_SET); 18 STR(EVENT_RESET); 19 STR(INCOMPLETE); 20 STR(ERROR_OUT_OF_HOST_MEMORY); 21 STR(ERROR_OUT_OF_DEVICE_MEMORY); 22 STR(ERROR_INITIALIZATION_FAILED); 23 STR(ERROR_DEVICE_LOST); 24 STR(ERROR_MEMORY_MAP_FAILED); 25 STR(ERROR_LAYER_NOT_PRESENT); 26 STR(ERROR_EXTENSION_NOT_PRESENT); 27 STR(ERROR_FEATURE_NOT_PRESENT); 28 STR(ERROR_INCOMPATIBLE_DRIVER); 29 STR(ERROR_TOO_MANY_OBJECTS); 30 STR(ERROR_FORMAT_NOT_SUPPORTED); 31 STR(ERROR_SURFACE_LOST_KHR); 32 STR(ERROR_NATIVE_WINDOW_IN_USE_KHR); 33 STR(SUBOPTIMAL_KHR); 34 STR(ERROR_OUT_OF_DATE_KHR); 35 STR(ERROR_INCOMPATIBLE_DISPLAY_KHR); 36 STR(ERROR_VALIDATION_FAILED_EXT); 37 STR(ERROR_INVALID_SHADER_NV); 38 default: 39 return "UNKNOWN_ERROR"; 40 } 41 42 #undef STR 43 } 10 44 11 45 bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) { -
vulkan-utils.hpp
rce9dc9f r8b823e7 3 3 4 4 #include <optional> 5 #include <sstream> 6 #include <stdexcept> 5 7 #include <string> 6 8 #include <vector> … … 32 34 class VulkanUtils { 33 35 public: 36 static string resultString(VkResult result); 37 34 38 static bool checkValidationLayerSupport(const vector<const char*> &validationLayers); 35 39 … … 136 140 } 137 141 142 #define VKUTIL_CHECK_RESULT(f, msg) { \ 143 VkResult res = (f); \ 144 \ 145 if (res != VK_SUCCESS) { \ 146 ostringstream oss; \ 147 oss << msg << " VkResult is \"" << VulkanUtils::resultString(res) << "\" in " << __FILE__ << " at line " << __LINE__;\ 148 \ 149 if (res < 0) { \ 150 throw runtime_error("Fatal: " + oss.str()); \ 151 } else { \ 152 cerr << oss.str(); \ 153 } \ 154 } \ 155 } 156 138 157 #endif // _VULKAN_UTILS_H
Note:
See TracChangeset
for help on using the changeset viewer.