Changeset 8b823e7 in opengl-game for sdl-game.cpp


Ignore:
Timestamp:
Jan 24, 2021, 11:38:43 PM (4 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.