123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #include "pch.h"
- #include "GpuMemoryBuffer.h"
- #include "GraphicsDevice.h"
- #if SUPPORT_D3D11 && SUPPORT_D3D12
- #include "D3D11/D3D11GraphicsDevice.h"
- #include "D3D12/D3D12GraphicsDevice.h"
- #endif
- #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES
- #include "OpenGL/OpenGLGraphicsDevice.h"
- #endif
- #if SUPPORT_VULKAN
- #include "UnityVulkanInterfaceFunctions.h"
- #include "Vulkan/VulkanGraphicsDevice.h"
- #endif
- #if SUPPORT_METAL
- #include "Metal/MetalDevice.h"
- #include "Metal/MetalGraphicsDevice.h"
- #endif
- namespace unity
- {
- namespace webrtc
- {
- class NullDevice : public IGraphicsDevice
- {
- public:
- NullDevice(UnityGfxRenderer renderer)
- : IGraphicsDevice(renderer, nullptr)
- {
- }
- virtual ~NullDevice() override { }
- bool InitV() override { return true; }
- void ShutdownV() override { }
- ITexture2D*
- CreateDefaultTextureV(uint32_t width, uint32_t height, UnityRenderingExtTextureFormat textureFormat) override
- {
- RTC_NOTREACHED();
- return nullptr;
- }
- void* GetEncodeDevicePtrV() override
- {
- RTC_NOTREACHED();
- return nullptr;
- }
- bool CopyResourceV(ITexture2D* dest, ITexture2D* src) override
- {
- RTC_NOTREACHED();
- return true;
- }
- bool CopyResourceFromNativeV(ITexture2D* dest, NativeTexPtr nativeTexturePtr) override
- {
- RTC_NOTREACHED();
- return true;
- }
- UnityGfxRenderer GetGfxRenderer() const override { return m_gfxRenderer; }
- std::unique_ptr<GpuMemoryBufferHandle> Map(ITexture2D* texture) override
- {
- RTC_NOTREACHED();
- return nullptr;
- }
- bool WaitSync(const ITexture2D* texture, uint64_t nsTimeout = 0) override
- {
- RTC_NOTREACHED();
- return true;
- }
- bool ResetSync(const ITexture2D* texture) override
- {
- RTC_NOTREACHED();
- return true;
- }
- bool WaitIdleForTest() override
- {
- RTC_NOTREACHED();
- return true;
- }
- // Required for software encoding
- ITexture2D*
- CreateCPUReadTextureV(uint32_t width, uint32_t height, UnityRenderingExtTextureFormat textureFormat) override
- {
- RTC_NOTREACHED();
- return nullptr;
- }
- rtc::scoped_refptr<::webrtc::I420Buffer> ConvertRGBToI420(ITexture2D* tex) override
- {
- RTC_NOTREACHED();
- return nullptr;
- }
- #if CUDA_PLATFORM
- bool IsCudaSupport() override { return false; }
- CUcontext GetCUcontext() override
- {
- RTC_NOTREACHED();
- return 0;
- }
- NV_ENC_BUFFER_FORMAT GetEncodeBufferFormat() override
- {
- RTC_NOTREACHED();
- return NV_ENC_BUFFER_FORMAT_UNDEFINED;
- }
- #endif
- };
- GraphicsDevice& GraphicsDevice::GetInstance()
- {
- static GraphicsDevice device;
- return device;
- }
- IGraphicsDevice* GraphicsDevice::Init(IUnityInterfaces* unityInterfaces, ProfilerMarkerFactory* profiler)
- {
- const UnityGfxRenderer rendererType = unityInterfaces->Get<IUnityGraphics>()->GetRenderer();
- switch (rendererType)
- {
- #if SUPPORT_D3D11
- case kUnityGfxRendererD3D11:
- {
- IUnityGraphicsD3D11* deviceInterface = unityInterfaces->Get<IUnityGraphicsD3D11>();
- return Init(rendererType, deviceInterface->GetDevice(), deviceInterface, profiler);
- }
- #endif
- #if SUPPORT_D3D12
- case kUnityGfxRendererD3D12:
- {
- IUnityGraphicsD3D12v5* deviceInterface = unityInterfaces->Get<IUnityGraphicsD3D12v5>();
- return Init(rendererType, deviceInterface->GetDevice(), deviceInterface, profiler);
- }
- #endif
- #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES || UNITY_WIN || UNITY_OSX
- case kUnityGfxRendererOpenGLES20:
- case kUnityGfxRendererOpenGLES30:
- case kUnityGfxRendererOpenGLCore:
- {
- return Init(rendererType, nullptr, nullptr, profiler);
- }
- #endif
- #if SUPPORT_VULKAN
- case kUnityGfxRendererVulkan:
- {
- UnityGraphicsVulkan* deviceInterface = UnityGraphicsVulkan::Get(unityInterfaces).release();
- UnityVulkanInstance vulkan = deviceInterface->Instance();
- return Init(
- rendererType, reinterpret_cast<void*>(&vulkan), reinterpret_cast<void*>(deviceInterface), profiler);
- }
- #endif
- #if SUPPORT_METAL
- case kUnityGfxRendererMetal:
- {
- std::unique_ptr<MetalDevice> device = MetalDevice::Create(unityInterfaces->Get<IUnityGraphicsMetal>());
- return Init(rendererType, device.release(), nullptr, profiler);
- break;
- }
- #endif
- default:
- {
- return nullptr;
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------
- IGraphicsDevice* GraphicsDevice::Init(
- const UnityGfxRenderer renderer, void* device, void* unityInterface, ProfilerMarkerFactory* profiler)
- {
- IGraphicsDevice* pDevice = nullptr;
- switch (renderer)
- {
- #if SUPPORT_D3D11
- case kUnityGfxRendererD3D11:
- {
- RTC_DCHECK(device);
- pDevice = new D3D11GraphicsDevice(static_cast<ID3D11Device*>(device), renderer, profiler);
- break;
- }
- #endif
- #if SUPPORT_D3D12
- case kUnityGfxRendererD3D12:
- {
- RTC_DCHECK(device);
- pDevice = new D3D12GraphicsDevice(
- static_cast<ID3D12Device*>(device),
- static_cast<IUnityGraphicsD3D12v5*>(unityInterface),
- renderer,
- profiler);
- break;
- }
- #endif
- #if SUPPORT_OPENGL_CORE || SUPPORT_OPENGL_ES || UNITY_WIN || UNITY_OSX
- case kUnityGfxRendererOpenGLES20:
- case kUnityGfxRendererOpenGLES30:
- case kUnityGfxRendererOpenGLCore:
- {
- #if UNITY_WIN || UNITY_OSX
- pDevice = new NullDevice(renderer);
- #else
- pDevice = new OpenGLGraphicsDevice(renderer, profiler);
- #endif
- break;
- }
- #endif
- #if SUPPORT_VULKAN
- case kUnityGfxRendererVulkan:
- {
- RTC_DCHECK(device);
- const UnityVulkanInstance* vulkan = static_cast<const UnityVulkanInstance*>(device);
- pDevice = new VulkanGraphicsDevice(
- static_cast<UnityGraphicsVulkan*>(unityInterface),
- vulkan->instance,
- vulkan->physicalDevice,
- vulkan->device,
- vulkan->graphicsQueue,
- vulkan->queueFamilyIndex,
- renderer,
- profiler);
- break;
- }
- #endif
- #if SUPPORT_METAL
- case kUnityGfxRendererMetal:
- {
- RTC_DCHECK(device);
- MetalDevice* metalDevice = static_cast<MetalDevice*>(device);
- pDevice = new MetalGraphicsDevice(metalDevice, renderer, profiler);
- break;
- }
- #endif
- default:
- {
- DebugError("Unsupported Unity Renderer: %d", renderer);
- return nullptr;
- }
- }
- return pDevice;
- }
- //---------------------------------------------------------------------------------------------------------------------
- GraphicsDevice::GraphicsDevice() { }
- } // end namespace webrtc
- } // end namespace unity
|