GraphicsDevice.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "pch.h"
  2. #include "GpuMemoryBuffer.h"
  3. #include "GraphicsDevice.h"
  4. #if SUPPORT_D3D11 && SUPPORT_D3D12
  5. #include "D3D11/D3D11GraphicsDevice.h"
  6. #include "D3D12/D3D12GraphicsDevice.h"
  7. #endif
  8. #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES
  9. #include "OpenGL/OpenGLGraphicsDevice.h"
  10. #endif
  11. #if SUPPORT_VULKAN
  12. #include "UnityVulkanInterfaceFunctions.h"
  13. #include "Vulkan/VulkanGraphicsDevice.h"
  14. #endif
  15. #if SUPPORT_METAL
  16. #include "Metal/MetalDevice.h"
  17. #include "Metal/MetalGraphicsDevice.h"
  18. #endif
  19. namespace unity
  20. {
  21. namespace webrtc
  22. {
  23. class NullDevice : public IGraphicsDevice
  24. {
  25. public:
  26. NullDevice(UnityGfxRenderer renderer)
  27. : IGraphicsDevice(renderer, nullptr)
  28. {
  29. }
  30. virtual ~NullDevice() override { }
  31. bool InitV() override { return true; }
  32. void ShutdownV() override { }
  33. ITexture2D*
  34. CreateDefaultTextureV(uint32_t width, uint32_t height, UnityRenderingExtTextureFormat textureFormat) override
  35. {
  36. RTC_NOTREACHED();
  37. return nullptr;
  38. }
  39. void* GetEncodeDevicePtrV() override
  40. {
  41. RTC_NOTREACHED();
  42. return nullptr;
  43. }
  44. bool CopyResourceV(ITexture2D* dest, ITexture2D* src) override
  45. {
  46. RTC_NOTREACHED();
  47. return true;
  48. }
  49. bool CopyResourceFromNativeV(ITexture2D* dest, NativeTexPtr nativeTexturePtr) override
  50. {
  51. RTC_NOTREACHED();
  52. return true;
  53. }
  54. UnityGfxRenderer GetGfxRenderer() const override { return m_gfxRenderer; }
  55. std::unique_ptr<GpuMemoryBufferHandle> Map(ITexture2D* texture) override
  56. {
  57. RTC_NOTREACHED();
  58. return nullptr;
  59. }
  60. bool WaitSync(const ITexture2D* texture, uint64_t nsTimeout = 0) override
  61. {
  62. RTC_NOTREACHED();
  63. return true;
  64. }
  65. bool ResetSync(const ITexture2D* texture) override
  66. {
  67. RTC_NOTREACHED();
  68. return true;
  69. }
  70. bool WaitIdleForTest() override
  71. {
  72. RTC_NOTREACHED();
  73. return true;
  74. }
  75. // Required for software encoding
  76. ITexture2D*
  77. CreateCPUReadTextureV(uint32_t width, uint32_t height, UnityRenderingExtTextureFormat textureFormat) override
  78. {
  79. RTC_NOTREACHED();
  80. return nullptr;
  81. }
  82. rtc::scoped_refptr<::webrtc::I420Buffer> ConvertRGBToI420(ITexture2D* tex) override
  83. {
  84. RTC_NOTREACHED();
  85. return nullptr;
  86. }
  87. #if CUDA_PLATFORM
  88. bool IsCudaSupport() override { return false; }
  89. CUcontext GetCUcontext() override
  90. {
  91. RTC_NOTREACHED();
  92. return 0;
  93. }
  94. NV_ENC_BUFFER_FORMAT GetEncodeBufferFormat() override
  95. {
  96. RTC_NOTREACHED();
  97. return NV_ENC_BUFFER_FORMAT_UNDEFINED;
  98. }
  99. #endif
  100. };
  101. GraphicsDevice& GraphicsDevice::GetInstance()
  102. {
  103. static GraphicsDevice device;
  104. return device;
  105. }
  106. IGraphicsDevice* GraphicsDevice::Init(IUnityInterfaces* unityInterfaces, ProfilerMarkerFactory* profiler)
  107. {
  108. const UnityGfxRenderer rendererType = unityInterfaces->Get<IUnityGraphics>()->GetRenderer();
  109. switch (rendererType)
  110. {
  111. #if SUPPORT_D3D11
  112. case kUnityGfxRendererD3D11:
  113. {
  114. IUnityGraphicsD3D11* deviceInterface = unityInterfaces->Get<IUnityGraphicsD3D11>();
  115. return Init(rendererType, deviceInterface->GetDevice(), deviceInterface, profiler);
  116. }
  117. #endif
  118. #if SUPPORT_D3D12
  119. case kUnityGfxRendererD3D12:
  120. {
  121. IUnityGraphicsD3D12v5* deviceInterface = unityInterfaces->Get<IUnityGraphicsD3D12v5>();
  122. return Init(rendererType, deviceInterface->GetDevice(), deviceInterface, profiler);
  123. }
  124. #endif
  125. #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES || UNITY_WIN || UNITY_OSX
  126. case kUnityGfxRendererOpenGLES20:
  127. case kUnityGfxRendererOpenGLES30:
  128. case kUnityGfxRendererOpenGLCore:
  129. {
  130. return Init(rendererType, nullptr, nullptr, profiler);
  131. }
  132. #endif
  133. #if SUPPORT_VULKAN
  134. case kUnityGfxRendererVulkan:
  135. {
  136. UnityGraphicsVulkan* deviceInterface = UnityGraphicsVulkan::Get(unityInterfaces).release();
  137. UnityVulkanInstance vulkan = deviceInterface->Instance();
  138. return Init(
  139. rendererType, reinterpret_cast<void*>(&vulkan), reinterpret_cast<void*>(deviceInterface), profiler);
  140. }
  141. #endif
  142. #if SUPPORT_METAL
  143. case kUnityGfxRendererMetal:
  144. {
  145. std::unique_ptr<MetalDevice> device = MetalDevice::Create(unityInterfaces->Get<IUnityGraphicsMetal>());
  146. return Init(rendererType, device.release(), nullptr, profiler);
  147. break;
  148. }
  149. #endif
  150. default:
  151. {
  152. return nullptr;
  153. }
  154. }
  155. }
  156. //---------------------------------------------------------------------------------------------------------------------
  157. IGraphicsDevice* GraphicsDevice::Init(
  158. const UnityGfxRenderer renderer, void* device, void* unityInterface, ProfilerMarkerFactory* profiler)
  159. {
  160. IGraphicsDevice* pDevice = nullptr;
  161. switch (renderer)
  162. {
  163. #if SUPPORT_D3D11
  164. case kUnityGfxRendererD3D11:
  165. {
  166. RTC_DCHECK(device);
  167. pDevice = new D3D11GraphicsDevice(static_cast<ID3D11Device*>(device), renderer, profiler);
  168. break;
  169. }
  170. #endif
  171. #if SUPPORT_D3D12
  172. case kUnityGfxRendererD3D12:
  173. {
  174. RTC_DCHECK(device);
  175. pDevice = new D3D12GraphicsDevice(
  176. static_cast<ID3D12Device*>(device),
  177. static_cast<IUnityGraphicsD3D12v5*>(unityInterface),
  178. renderer,
  179. profiler);
  180. break;
  181. }
  182. #endif
  183. #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES || UNITY_WIN || UNITY_OSX
  184. case kUnityGfxRendererOpenGLES20:
  185. case kUnityGfxRendererOpenGLES30:
  186. case kUnityGfxRendererOpenGLCore:
  187. {
  188. #if UNITY_WIN || UNITY_OSX
  189. pDevice = new NullDevice(renderer);
  190. #else
  191. pDevice = new OpenGLGraphicsDevice(renderer, profiler);
  192. #endif
  193. break;
  194. }
  195. #endif
  196. #if SUPPORT_VULKAN
  197. case kUnityGfxRendererVulkan:
  198. {
  199. RTC_DCHECK(device);
  200. const UnityVulkanInstance* vulkan = static_cast<const UnityVulkanInstance*>(device);
  201. pDevice = new VulkanGraphicsDevice(
  202. static_cast<UnityGraphicsVulkan*>(unityInterface),
  203. vulkan->instance,
  204. vulkan->physicalDevice,
  205. vulkan->device,
  206. vulkan->graphicsQueue,
  207. vulkan->queueFamilyIndex,
  208. renderer,
  209. profiler);
  210. break;
  211. }
  212. #endif
  213. #if SUPPORT_METAL
  214. case kUnityGfxRendererMetal:
  215. {
  216. RTC_DCHECK(device);
  217. MetalDevice* metalDevice = static_cast<MetalDevice*>(device);
  218. pDevice = new MetalGraphicsDevice(metalDevice, renderer, profiler);
  219. break;
  220. }
  221. #endif
  222. default:
  223. {
  224. DebugError("Unsupported Unity Renderer: %d", renderer);
  225. return nullptr;
  226. }
  227. }
  228. return pDevice;
  229. }
  230. //---------------------------------------------------------------------------------------------------------------------
  231. GraphicsDevice::GraphicsDevice() { }
  232. } // end namespace webrtc
  233. } // end namespace unity