Changeset 8b823e7 in opengl-game


Ignore:
Timestamp:
Jan 24, 2021, 11:38:43 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
d8cf709
Parents:
ce9dc9f
Message:

Create an error-checking macro to check Vulkan function results, which will print a custom message, the error code, and the line number for all results besides VK_SUCCESS, and update the imgui error-checking callback with similar functionality.

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.cpp

    rce9dc9f r8b823e7  
    66#include <stdexcept>
    77
    8 #include <stdlib.h>         // abort (only used in check_vk_result)
    9 
    108#include <SDL2/SDL_vulkan.h>
    119
     
    2018static bool g_SwapChainRebuild = false;
    2119
    22 static void check_vk_result(VkResult err) {
    23    if (err == 0) {
     20static void check_imgui_vk_result(VkResult res) {
     21   if (res == VK_SUCCESS) {
    2422      return;
    2523   }
    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();
    2931   }
    3032}
     
    3739      g_SwapChainRebuild = true;
    3840      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!");
    4950
    5051   // START OF NEW CODE
     
    5253   // before the render loop ever starts. I should change this
    5354
    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
    5658   VkCommandBufferBeginInfo info = {};
    5759   info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    5860   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!");
    6164
    6265   VkRenderPassBeginInfo renderPassInfo = {};
     
    8184   vkCmdEndRenderPass(commandBuffers[imageIndex]);
    8285
    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!");
    8688
    8789   // END OF NEW CODE
     
    101103   submitInfo.pSignalSemaphores = signalSemaphores;
    102104
    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!");
    106107}
    107108
     
    196197      pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes);
    197198      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");
    199202   }
    200203
     
    225228   init_info.MinImageCount = swapChainMinImageCount;
    226229   init_info.ImageCount = swapChainImageCount;
    227    init_info.CheckVkResultFn = check_vk_result;
     230   init_info.CheckVkResultFn = check_imgui_vk_result;
    228231   ImGui_ImplVulkan_Init(&init_info, renderPass);
    229232
  • vulkan-game.cpp

    rce9dc9f r8b823e7  
    3838 */
    3939
    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)
     40static void check_imgui_vk_result(VkResult res) {
     41   if (res == VK_SUCCESS) {
    4342      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   }
    4752}
    4853
     
    251256   init_info.MinImageCount = this->swapChainMinImageCount;
    252257   init_info.ImageCount = this->swapChainImageCount;
    253    init_info.CheckVkResultFn = check_vk_result;
     258   init_info.CheckVkResultFn = check_imgui_vk_result;
    254259   ImGui_ImplVulkan_Init(&init_info, this->renderPass);
    255260
     
    261266   // Upload Fonts
    262267   {
    263       VkResult err;
    264 
    265268      VkCommandBuffer command_buffer;
    266269
     
    271274      info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    272275      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!");
    275279
    276280      //err = vkResetCommandPool(this->device, command_pool, 0); // Probably not really needed here since the command pool is never used before this
     
    279283      begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    280284      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!");
    283287
    284288      ImGui_ImplVulkan_CreateFontsTexture(command_buffer);
     
    288292      end_info.commandBufferCount = 1;
    289293      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!");
    294300
    295301      if (vkDeviceWaitIdle(this->device) != VK_SUCCESS) {
     
    300306
    301307      // 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!");
    304310   }
    305311
  • vulkan-utils.cpp

    rce9dc9f r8b823e7  
    88#define STB_IMAGE_IMPLEMENTATION
    99#include "stb_image.h" // TODO: Probably switch to SDL_image
     10
     11string 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}
    1044
    1145bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
  • vulkan-utils.hpp

    rce9dc9f r8b823e7  
    33
    44#include <optional>
     5#include <sstream>
     6#include <stdexcept>
    57#include <string>
    68#include <vector>
     
    3234class VulkanUtils {
    3335   public:
     36      static string resultString(VkResult result);
     37
    3438      static bool checkValidationLayerSupport(const vector<const char*> &validationLayers);
    3539
     
    136140}
    137141
     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
    138157#endif // _VULKAN_UTILS_H
Note: See TracChangeset for help on using the changeset viewer.