Checks.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef SAMPLES_COMMON_CHECKS_HPP__
  2. #define SAMPLES_COMMON_CHECKS_HPP__
  3. #include <dw/gl/GL.h>
  4. #include <cuda_runtime.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <stdexcept>
  8. //------------------------------------------------------------------------------
  9. // Functions
  10. //------------------------------------------------------------------------------
  11. //------------------------------------------------------------------------------
  12. // print GL error with location
  13. inline void getGLError(int line, const char *file, const char *function)
  14. {
  15. GLenum error = glGetError();
  16. if (error != GL_NO_ERROR) {
  17. std::cerr << file << " in function " << function << " in line " << line << ": glError: 0x"
  18. << std::hex << error << std::dec << std::endl;
  19. exit(1);
  20. }
  21. }
  22. #ifndef __PRETTY_FUNCTION__
  23. #define __PRETTY_FUNCTION__ __FUNCTION__
  24. #endif
  25. // macro to easily check for GL errors
  26. #define CHECK_GL_ERROR() getGLError(__LINE__, __FILE__, __PRETTY_FUNCTION__);
  27. //------------------------------------------------------------------------------
  28. // macro to easily check for dw errors
  29. #define CHECK_DW_ERROR(x) { \
  30. dwStatus result = x; \
  31. if(result!=DW_SUCCESS) \
  32. throw std::runtime_error(std::string("DW Error ") \
  33. + dwGetStatusName(result) \
  34. + std::string(" executing DW function:\n " #x) \
  35. + std::string("\n at " __FILE__ ":") + std::to_string(__LINE__)); \
  36. };
  37. #define CHECK_DW_ERROR_MSG(x,description) { \
  38. dwStatus result = x; \
  39. if(result!=DW_SUCCESS) \
  40. throw std::runtime_error(std::string("DW Error ") \
  41. + dwGetStatusName(result) \
  42. + std::string(" executing DW function:\n " #x) \
  43. + std::string("\n at " __FILE__ ":") \
  44. + std::to_string(__LINE__) \
  45. + std::string(" -> ") + description); \
  46. };
  47. //------------------------------------------------------------------------------
  48. // macro to easily check for cuda errors
  49. #define CHECK_CUDA_ERROR(x) { \
  50. x; \
  51. auto result = cudaGetLastError(); \
  52. if(result != cudaSuccess) \
  53. throw std::runtime_error(std::string("CUDA Error ") \
  54. + cudaGetErrorString(result) \
  55. + std::string(" executing CUDA function:\n " #x) \
  56. + std::string("\n at " __FILE__ ":") + std::to_string(__LINE__)); \
  57. };
  58. #endif // SAMPLES_COMMON_CHECKS_HPP__