vk_error_print.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Danil, 2021+ Vulkan shader launcher, self https://github.com/danilw/vulkan-shadertoy-launcher
  2. // The MIT License
  3. #include "vk_error_print.h"
  4. void vk_error_data_set_vkresult(struct vk_error_data *error, VkResult vkresult, const char *file, unsigned int line)
  5. {
  6. if (vkresult == 0)
  7. return;
  8. if (error->type != VK_ERROR_SUCCESS && !(error->type == VK_ERROR_VKRESULT_WARNING && vkresult < 0))
  9. return;
  10. *error = (struct vk_error_data){
  11. .type = vkresult < 0?VK_ERROR_VKRESULT:VK_ERROR_VKRESULT_WARNING,
  12. .vkresult = vkresult,
  13. .file = file,
  14. .line = line,
  15. };
  16. }
  17. void vk_error_data_set_errno(struct vk_error_data *error, int err_no, const char *file, unsigned int line)
  18. {
  19. if (err_no == 0)
  20. return;
  21. if (error->type != VK_ERROR_SUCCESS && error->type != VK_ERROR_VKRESULT_WARNING)
  22. return;
  23. *error = (struct vk_error_data){
  24. .type = VK_ERROR_ERRNO,
  25. .err_no = err_no,
  26. .file = file,
  27. .line = line,
  28. };
  29. }
  30. bool vk_error_data_merge(struct vk_error_data *error, struct vk_error_data *other)
  31. {
  32. if (other->type == VK_ERROR_SUCCESS)
  33. return false;
  34. if (error->type != VK_ERROR_SUCCESS && !(error->type == VK_ERROR_VKRESULT_WARNING && (other->type == VK_ERROR_VKRESULT || other->type == VK_ERROR_ERRNO)))
  35. return false;
  36. *error = *other;
  37. return true;
  38. }
  39. bool vk_error_is_success(struct vk_error *error)
  40. {
  41. return error->error.type == VK_ERROR_SUCCESS;
  42. }
  43. bool vk_error_is_warning(struct vk_error *error)
  44. {
  45. return error->error.type == VK_ERROR_VKRESULT_WARNING;
  46. }
  47. bool vk_error_is_error(struct vk_error *error)
  48. {
  49. return !vk_error_is_success(error) && !vk_error_is_warning(error);
  50. }
  51. static const char *VkResult_string(VkResult res)
  52. {
  53. switch (res)
  54. {
  55. case VK_SUCCESS:
  56. return "Success";
  57. case VK_NOT_READY:
  58. return "Not ready";
  59. case VK_TIMEOUT:
  60. return "Timeout";
  61. case VK_EVENT_SET:
  62. return "Event set";
  63. case VK_EVENT_RESET:
  64. return "Event reset";
  65. case VK_INCOMPLETE:
  66. return "Incomplete";
  67. case VK_ERROR_OUT_OF_HOST_MEMORY:
  68. return "Out of host memory";
  69. case VK_ERROR_OUT_OF_DEVICE_MEMORY:
  70. return "Out of device memory";
  71. case VK_ERROR_INITIALIZATION_FAILED:
  72. return "Initialization failed";
  73. case VK_ERROR_DEVICE_LOST:
  74. return "Device lost";
  75. case VK_ERROR_MEMORY_MAP_FAILED:
  76. return "Memory map failed";
  77. case VK_ERROR_LAYER_NOT_PRESENT:
  78. return "Layer not present";
  79. case VK_ERROR_EXTENSION_NOT_PRESENT:
  80. return "Extension not present";
  81. case VK_ERROR_FEATURE_NOT_PRESENT:
  82. return "Feature not present";
  83. case VK_ERROR_INCOMPATIBLE_DRIVER:
  84. return "Incompatible driver";
  85. case VK_ERROR_TOO_MANY_OBJECTS:
  86. return "Too many objects";
  87. case VK_ERROR_FORMAT_NOT_SUPPORTED:
  88. return "Format not supported";
  89. case VK_ERROR_SURFACE_LOST_KHR:
  90. return "Surface lost";
  91. case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
  92. return "Native window is in use";
  93. case VK_SUBOPTIMAL_KHR:
  94. return "Suboptimal";
  95. case VK_ERROR_OUT_OF_DATE_KHR:
  96. return "Surface is out of date";
  97. case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
  98. return "Incompatible display";
  99. case VK_ERROR_VALIDATION_FAILED_EXT:
  100. return "Validation failed";
  101. default:
  102. return "Unrecognized error";
  103. }
  104. }
  105. #if defined(VK_USE_PLATFORM_WIN32_KHR)
  106. void win_error(char *iout, char *iout2){
  107. int msgboxID = MessageBox(
  108. NULL,
  109. (LPCSTR)iout,
  110. (LPCSTR)iout2,
  111. MB_ICONHAND | MB_DEFBUTTON1
  112. );
  113. }
  114. #endif
  115. void print_error(FILE *fout, struct vk_error_data *error_data, const char *prefix)
  116. {
  117. #if defined(VK_USE_PLATFORM_WIN32_KHR)
  118. char iout[512]={0};
  119. char iout2[512]={0};
  120. fprintf(fout, "%s:%u: %s", error_data->file, error_data->line, prefix);
  121. sprintf(iout, "%s:%u: %s", error_data->file, error_data->line, prefix);
  122. switch (error_data->type)
  123. {
  124. case VK_ERROR_VKRESULT_WARNING:
  125. case VK_ERROR_VKRESULT:
  126. fprintf(fout, "%s (VkResult %d)\n", VkResult_string(error_data->vkresult), error_data->vkresult);
  127. sprintf(iout2, "%s (VkResult %d)\n", VkResult_string(error_data->vkresult), error_data->vkresult);
  128. break;
  129. case VK_ERROR_ERRNO:
  130. fprintf(fout, "%s (errno %d)\n", strerror(error_data->err_no), error_data->err_no);
  131. sprintf(iout2, "%s (errno %d)\n", strerror(error_data->err_no), error_data->err_no);
  132. break;
  133. default:
  134. fprintf(fout, "<internal error>\n");
  135. sprintf(iout2, "<internal error>\n");
  136. break;
  137. }
  138. win_error((char*)&iout2,(char*)&iout);
  139. #elif defined(VK_USE_PLATFORM_XCB_KHR)||defined(VK_USE_PLATFORM_WAYLAND_KHR)
  140. fprintf(fout, "%s:%u: %s", error_data->file, error_data->line, prefix);
  141. switch (error_data->type)
  142. {
  143. case VK_ERROR_VKRESULT_WARNING:
  144. case VK_ERROR_VKRESULT:
  145. fprintf(fout, "%s (VkResult %d)\n", VkResult_string(error_data->vkresult), error_data->vkresult);
  146. break;
  147. case VK_ERROR_ERRNO:
  148. fprintf(fout, "%s (errno %d)\n", strerror(error_data->err_no), error_data->err_no);
  149. break;
  150. default:
  151. fprintf(fout, "<internal error>\n");
  152. break;
  153. }
  154. #endif
  155. }
  156. void vk_error_fprintf(FILE *fout, struct vk_error *error, const char *fmt, ...)
  157. {
  158. if (error->error.type == VK_ERROR_SUCCESS)
  159. return;
  160. va_list args;
  161. va_start(args, fmt);
  162. vfprintf(fout, fmt, args);
  163. va_end(args);
  164. print_error(fout, &error->error, "");
  165. if (error->sub_error.type != VK_ERROR_SUCCESS)
  166. print_error(fout, &error->sub_error, " Resulting from this error: ");
  167. }
  168. /* The following functions get a readable string out of the Vulkan standard enums */
  169. const char *vk_VkPhysicalDeviceType_string(VkPhysicalDeviceType type)
  170. {
  171. switch (type)
  172. {
  173. case VK_PHYSICAL_DEVICE_TYPE_OTHER:
  174. return "Neither GPU nor CPU";
  175. case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
  176. return "Integrated GPU";
  177. case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
  178. return "Discrete GPU";
  179. case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
  180. return "Virtual GPU";
  181. case VK_PHYSICAL_DEVICE_TYPE_CPU:
  182. return "CPU";
  183. default:
  184. return "Unrecognized device type";
  185. }
  186. }