EmbreeDevice.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Vladimir Fonov <[email protected]>
  4. // 2013 Alec Jacobson <[email protected]>
  5. // 2014 Christian Schüller <[email protected]>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla Public License
  8. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. // obtain one at http://mozilla.org/MPL/2.0/.
  10. //
  11. #ifndef IGL_EMBREE_EMBREE_DEVICE_H
  12. #define IGL_EMBREE_EMBREE_DEVICE_H
  13. #include <embree4/rtcore.h>
  14. #include <iostream>
  15. namespace igl
  16. {
  17. namespace embree
  18. {
  19. /// keep track of embree device instance
  20. struct EmbreeDevice
  21. {
  22. RTCDevice embree_device;
  23. int embree_device_cntr;
  24. static EmbreeDevice & instance()
  25. {
  26. static EmbreeDevice s;
  27. return s;
  28. } // instance
  29. EmbreeDevice(const EmbreeDevice &) = delete;
  30. EmbreeDevice & operator = (const EmbreeDevice &) = delete;
  31. static RTCDevice get_device(const char *config=nullptr)
  32. {
  33. return instance().get(config);
  34. }
  35. static void release_device(void)
  36. {
  37. instance().release();
  38. }
  39. private:
  40. EmbreeDevice():embree_device(nullptr),embree_device_cntr(0) {}
  41. ~EmbreeDevice()
  42. {
  43. if(embree_device)
  44. rtcReleaseDevice(embree_device);
  45. }
  46. RTCDevice get(const char *config=nullptr)
  47. {
  48. if(!embree_device)
  49. {
  50. embree_device = rtcNewDevice (config);
  51. if(rtcGetDeviceError (embree_device) != RTC_ERROR_NONE)
  52. {
  53. assert(false && "Embree: An error occurred while initializing embree core!");
  54. #ifdef IGL_VERBOSE
  55. std::cerr << "Embree: An error occurred while initializing embree core!" << std::endl;
  56. }
  57. else
  58. {
  59. std::cerr << "Embree: core initialized." << std::endl;
  60. #endif
  61. }
  62. }
  63. ++embree_device_cntr;
  64. return embree_device;
  65. }
  66. void release()
  67. {
  68. if(!--embree_device_cntr) {
  69. rtcReleaseDevice (embree_device);
  70. embree_device = nullptr;
  71. #ifdef IGL_VERBOSE
  72. std::cerr << "Embree: core released." << std::endl;
  73. #endif
  74. }
  75. }
  76. };
  77. }
  78. }
  79. #endif // IGL_EMBREE_EMBREE_DEVICE_H