VulkanGraphicsDevice.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include <IUnityGraphicsVulkan.h>
  3. #include <api/video/i420_buffer.h>
  4. #include <memory>
  5. #include <vulkan/vulkan.h>
  6. #include "PlatformBase.h"
  7. #if CUDA_PLATFORM
  8. #include "GraphicsDevice/Cuda/CudaContext.h"
  9. #endif
  10. #include "GraphicsDevice/IGraphicsDevice.h"
  11. namespace unity
  12. {
  13. namespace webrtc
  14. {
  15. using namespace ::webrtc;
  16. class UnityGraphicsVulkan;
  17. class VulkanGraphicsDevice : public IGraphicsDevice
  18. {
  19. public:
  20. VulkanGraphicsDevice(
  21. UnityGraphicsVulkan* unityVulkan,
  22. const VkInstance instance,
  23. const VkPhysicalDevice physicalDevice,
  24. const VkDevice device,
  25. const VkQueue graphicsQueue,
  26. const uint32_t queueFamilyIndex,
  27. UnityGfxRenderer renderer,
  28. ProfilerMarkerFactory* profiler);
  29. ~VulkanGraphicsDevice() override = default;
  30. bool InitV() override;
  31. void ShutdownV() override;
  32. inline void* GetEncodeDevicePtrV() override;
  33. ITexture2D* CreateDefaultTextureV(
  34. const uint32_t w, const uint32_t h, UnityRenderingExtTextureFormat textureFormat) override;
  35. ITexture2D*
  36. CreateCPUReadTextureV(uint32_t width, uint32_t height, UnityRenderingExtTextureFormat textureFormat) override;
  37. std::unique_ptr<UnityVulkanImage> AccessTexture(void* ptr) const;
  38. bool CopyResourceV(ITexture2D* dest, ITexture2D* src) override;
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. /// <param name="dest"></param>
  43. /// <param name="nativeTexturePtr"> a pointer of UnityVulkanImage </param>
  44. /// <returns></returns>
  45. bool CopyResourceFromNativeV(ITexture2D* dest, void* nativeTexturePtr) override;
  46. std::unique_ptr<GpuMemoryBufferHandle> Map(ITexture2D* texture) override;
  47. bool WaitSync(const ITexture2D* texture, uint64_t nsTimeout = 0) override;
  48. bool ResetSync(const ITexture2D* texture) override;
  49. bool WaitIdleForTest() override;
  50. rtc::scoped_refptr<I420Buffer> ConvertRGBToI420(ITexture2D* tex) override;
  51. #if CUDA_PLATFORM
  52. bool IsCudaSupport() override { return m_isCudaSupport; }
  53. CUcontext GetCUcontext() override { return m_cudaContext.GetContext(); }
  54. NV_ENC_BUFFER_FORMAT GetEncodeBufferFormat() override { return NV_ENC_BUFFER_FORMAT_ARGB; }
  55. #endif
  56. private:
  57. VkResult CreateCommandPool();
  58. UnityGraphicsVulkan* m_unityVulkan;
  59. VkPhysicalDevice m_physicalDevice;
  60. VkDevice m_device;
  61. VkQueue m_graphicsQueue;
  62. VkCommandPool m_commandPool;
  63. uint32_t m_queueFamilyIndex;
  64. VkAllocationCallbacks* m_allocator;
  65. const UnityProfilerMarkerDesc* m_maker;
  66. #if CUDA_PLATFORM
  67. bool InitCudaContext();
  68. VkInstance m_instance;
  69. CudaContext m_cudaContext;
  70. bool m_isCudaSupport;
  71. #endif
  72. };
  73. void* VulkanGraphicsDevice::GetEncodeDevicePtrV()
  74. {
  75. #if CUDA_PLATFORM
  76. return reinterpret_cast<void*>(m_cudaContext.GetContext());
  77. #else
  78. return nullptr;
  79. #endif
  80. }
  81. } // end namespace webrtc
  82. } // end namespace unity