device.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "default.h"
  5. #include "state.h"
  6. #include "accel.h"
  7. namespace embree
  8. {
  9. class BVH4Factory;
  10. class BVH8Factory;
  11. struct TaskArena;
  12. class Device : public State, public MemoryMonitorInterface
  13. {
  14. ALIGNED_CLASS_(16);
  15. public:
  16. /*! allocator that performs unified shared memory allocations */
  17. template<typename T, size_t alignment>
  18. struct allocator
  19. {
  20. typedef T value_type;
  21. typedef T* pointer;
  22. typedef const T* const_pointer;
  23. typedef T& reference;
  24. typedef const T& const_reference;
  25. typedef std::size_t size_type;
  26. typedef std::ptrdiff_t difference_type;
  27. allocator() {}
  28. allocator(Device* device)
  29. : device(device) {}
  30. __forceinline pointer allocate( size_type n ) {
  31. assert(device);
  32. return (pointer) device->malloc(n*sizeof(T),alignment);
  33. }
  34. __forceinline void deallocate( pointer p, size_type n ) {
  35. if (device) device->free(p);
  36. }
  37. __forceinline void construct( pointer p, const_reference val ) {
  38. new (p) T(val);
  39. }
  40. __forceinline void destroy( pointer p ) {
  41. p->~T();
  42. }
  43. Device* device = nullptr;
  44. };
  45. /*! vector class that performs aligned allocations from Device object */
  46. template<typename T>
  47. using vector = vector_t<T,allocator<T,std::alignment_of<T>::value>>;
  48. template<typename T, size_t alignment>
  49. using avector = vector_t<T,allocator<T,alignment>>;
  50. public:
  51. /*! Device construction */
  52. Device (const char* cfg);
  53. /*! Device destruction */
  54. virtual ~Device ();
  55. /*! prints info about the device */
  56. void print();
  57. /*! sets the error code */
  58. void setDeviceErrorCode(RTCError error);
  59. /*! returns and clears the error code */
  60. RTCError getDeviceErrorCode();
  61. /*! sets the error code */
  62. static void setThreadErrorCode(RTCError error);
  63. /*! returns and clears the error code */
  64. static RTCError getThreadErrorCode();
  65. /*! processes error codes, do not call directly */
  66. static void process_error(Device* device, RTCError error, const char* str);
  67. /*! invokes the memory monitor callback */
  68. void memoryMonitor(ssize_t bytes, bool post);
  69. /*! sets the size of the software cache. */
  70. void setCacheSize(size_t bytes);
  71. /*! sets a property */
  72. void setProperty(const RTCDeviceProperty prop, ssize_t val);
  73. /*! gets a property */
  74. ssize_t getProperty(const RTCDeviceProperty prop);
  75. /*! enter device by setting up some global state */
  76. virtual void enter() {}
  77. /*! leave device by setting up some global state */
  78. virtual void leave() {}
  79. /*! buffer allocation */
  80. virtual void* malloc(size_t size, size_t align);
  81. /*! buffer deallocation */
  82. virtual void free(void* ptr);
  83. private:
  84. /*! initializes the tasking system */
  85. void initTaskingSystem(size_t numThreads);
  86. /*! shuts down the tasking system */
  87. void exitTaskingSystem();
  88. std::unique_ptr<TaskArena> arena;
  89. public:
  90. // use tasking system arena to execute func
  91. void execute(bool join, const std::function<void()>& func);
  92. /*! some variables that can be set via rtcSetParameter1i for debugging purposes */
  93. public:
  94. static ssize_t debug_int0;
  95. static ssize_t debug_int1;
  96. static ssize_t debug_int2;
  97. static ssize_t debug_int3;
  98. public:
  99. std::unique_ptr<BVH4Factory> bvh4_factory;
  100. #if defined(EMBREE_TARGET_SIMD8)
  101. std::unique_ptr<BVH8Factory> bvh8_factory;
  102. #endif
  103. };
  104. #if defined(EMBREE_SYCL_SUPPORT)
  105. class DeviceGPU : public Device
  106. {
  107. public:
  108. DeviceGPU(sycl::context sycl_context, const char* cfg);
  109. ~DeviceGPU();
  110. virtual void enter() override;
  111. virtual void leave() override;
  112. virtual void* malloc(size_t size, size_t align) override;
  113. virtual void free(void* ptr) override;
  114. /* set SYCL device */
  115. void setSYCLDevice(const sycl::device sycl_device);
  116. private:
  117. sycl::context gpu_context;
  118. sycl::device gpu_device;
  119. unsigned int gpu_maxWorkGroupSize;
  120. unsigned int gpu_maxComputeUnits;
  121. public:
  122. void* dispatchGlobalsPtr = nullptr;
  123. public:
  124. inline sycl::device &getGPUDevice() { return gpu_device; }
  125. inline sycl::context &getGPUContext() { return gpu_context; }
  126. inline unsigned int getGPUMaxWorkGroupSize() { return gpu_maxWorkGroupSize; }
  127. void init_rthw_level_zero();
  128. void init_rthw_opencl();
  129. };
  130. #endif
  131. struct DeviceEnterLeave
  132. {
  133. DeviceEnterLeave (RTCDevice hdevice);
  134. DeviceEnterLeave (RTCScene hscene);
  135. DeviceEnterLeave (RTCGeometry hgeometry);
  136. DeviceEnterLeave (RTCBuffer hbuffer);
  137. ~DeviceEnterLeave();
  138. private:
  139. Device* device;
  140. };
  141. }