vk_error_print.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Danil, 2021+ Vulkan shader launcher, self https://github.com/danilw/vulkan-shadertoy-launcher
  2. // The MIT License
  3. #ifndef vk_utils_printf_H
  4. #define vk_utils_printf_H
  5. #include <vulkan/vulkan.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <errno.h>
  11. #ifdef _GNUC
  12. #define ATTR_UNUSED __attribute__((format(printf, 3, 4)))
  13. #else
  14. #define ATTR_UNUSED
  15. #endif
  16. enum vk_error_type
  17. {
  18. VK_ERROR_SUCCESS = 0,
  19. VK_ERROR_VKRESULT,
  20. VK_ERROR_VKRESULT_WARNING,
  21. VK_ERROR_ERRNO,
  22. };
  23. typedef struct vk_error_data
  24. {
  25. enum vk_error_type type;
  26. union {
  27. VkResult vkresult;
  28. int err_no;
  29. };
  30. const char *file;
  31. unsigned int line;
  32. } vk_error_data;
  33. typedef struct vk_error
  34. {
  35. struct vk_error_data error;
  36. struct vk_error_data sub_error; /*
  37. * Used in cases where error is e.g. "VK_INCOMPLETE", and it is due to
  38. * another error.
  39. */
  40. } vk_error;
  41. #define VK_ERROR_NONE (struct vk_error){ .error = { .type = VK_ERROR_SUCCESS,}, .sub_error = { .type = VK_ERROR_SUCCESS,}, }
  42. #define vk_error_set_vkresult(es, e) vk_error_data_set_vkresult(&(es)->error, (e), __FILE__, __LINE__)
  43. #define vk_error_set_errno(es, e) vk_error_data_set_errno (&(es)->error, (e), __FILE__, __LINE__)
  44. #define vk_error_sub_set_vkresult(es, e) vk_error_data_set_vkresult(&(es)->sub_error, (e), __FILE__, __LINE__)
  45. #define vk_error_sub_set_errno(es, e) vk_error_data_set_errno (&(es)->sub_error, (e), __FILE__, __LINE__)
  46. #define vk_error_merge(es, os) \
  47. do { \
  48. if (vk_error_data_merge(&(es)->error, &(os)->error)) \
  49. (es)->sub_error = (os)->sub_error; \
  50. } while (0)
  51. #define vk_error_sub_merge(es, os) vk_error_data_merge(&(es)->sub_error, &(os)->error)
  52. void vk_error_data_set_vkresult(struct vk_error_data *error, VkResult vkresult, const char *file, unsigned int line);
  53. void vk_error_data_set_errno(struct vk_error_data *error, int err_no, const char *file, unsigned int line);
  54. bool vk_error_data_merge(struct vk_error_data *error, struct vk_error_data *other);
  55. bool vk_error_is_success(struct vk_error *error);
  56. bool vk_error_is_warning(struct vk_error *error);
  57. bool vk_error_is_error(struct vk_error *error);
  58. #if defined(VK_USE_PLATFORM_WIN32_KHR)
  59. void win_error(char *iout, char *iout2);
  60. #endif
  61. #define vk_error_printf(es, ...) vk_error_fprintf(stdout, (es), __VA_ARGS__)
  62. void vk_error_fprintf(FILE *fout, struct vk_error *error, const char *fmt, ...) ATTR_UNUSED;
  63. const char *vk_VkPhysicalDeviceType_string(VkPhysicalDeviceType type);
  64. #endif