LoadVulkanFunctions.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "pch.h"
  2. namespace unity
  3. {
  4. namespace webrtc
  5. {
  6. // todo(kazuki):: fix workaround
  7. #pragma clang diagnostic push
  8. #pragma clang diagnostic ignored "-Wmacro-redefined"
  9. #define EXPORTED_VULKAN_FUNCTION(func) PFN_##func func;
  10. #define GLOBAL_VULKAN_FUNCTION(func) PFN_##func func;
  11. #define INSTANCE_VULKAN_FUNCTION(func) PFN_##func func;
  12. #define DEVICE_VULKAN_FUNCTION(func) PFN_##func func;
  13. #include "ListOfVulkanFunctions.inl"
  14. static LIBRARY_TYPE s_vulkanLibrary = nullptr;
  15. bool LoadVulkanFunctions(UnityVulkanInstance& instance)
  16. {
  17. if (!LoadVulkanLibrary(s_vulkanLibrary))
  18. {
  19. RTC_LOG(LS_ERROR) << "Failed loading vulkan library";
  20. return false;
  21. }
  22. if (!LoadExportedVulkanFunction(s_vulkanLibrary))
  23. {
  24. RTC_LOG(LS_ERROR) << "Failed loading vulkan exported function";
  25. return false;
  26. }
  27. if (!LoadInstanceVulkanFunction(instance.instance))
  28. {
  29. RTC_LOG(LS_ERROR) << "Failed loading vulkan instance function";
  30. return false;
  31. }
  32. if (!LoadDeviceVulkanFunction(instance.device))
  33. {
  34. RTC_LOG(LS_ERROR) << "Failed loading vulkan device function";
  35. return false;
  36. }
  37. return true;
  38. }
  39. bool LoadVulkanLibrary(LIBRARY_TYPE& library)
  40. {
  41. // Keep the logic similar to Unity internals at VKApiFunctions.cpp
  42. #if UNITY_WIN
  43. library = LoadLibrary("vulkan-1.dll");
  44. #elif UNITY_ANDROID
  45. library = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
  46. #elif UNITY_LINUX
  47. library = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL);
  48. #else
  49. #error Unsupported Platform
  50. #endif
  51. if (library == nullptr)
  52. return false;
  53. return true;
  54. }
  55. bool LoadExportedVulkanFunction(LIBRARY_TYPE const& library)
  56. {
  57. #if UNITY_WIN
  58. #define LoadFunction GetProcAddress
  59. #elif UNITY_ANDROID || UNITY_LINUX
  60. #define LoadFunction dlsym
  61. #endif
  62. #define EXPORTED_VULKAN_FUNCTION( name ) \
  63. name = (PFN_##name)LoadFunction( library, #name ); \
  64. if (name == nullptr) { \
  65. return false; \
  66. }
  67. #include "ListOfVulkanFunctions.inl"
  68. return true;
  69. }
  70. bool LoadGlobalVulkanFunction()
  71. {
  72. #define GLOBAL_VULKAN_FUNCTION( name ) \
  73. name = (PFN_##name)vkGetInstanceProcAddr( nullptr, #name ); \
  74. if (name == nullptr) { \
  75. return false; \
  76. }
  77. #include "ListOfVulkanFunctions.inl"
  78. return true;
  79. }
  80. bool LoadInstanceVulkanFunction(VkInstance instance)
  81. {
  82. #define INSTANCE_VULKAN_FUNCTION( name ) \
  83. name = (PFN_##name)vkGetInstanceProcAddr( instance, #name ); \
  84. if (name == nullptr) { \
  85. return false; \
  86. }
  87. #include "ListOfVulkanFunctions.inl"
  88. return true;
  89. }
  90. bool LoadDeviceVulkanFunction(VkDevice device)
  91. {
  92. #define DEVICE_VULKAN_FUNCTION( name ) \
  93. name = (PFN_##name)vkGetDeviceProcAddr( device, #name ); \
  94. if (name == nullptr) { \
  95. return false; \
  96. }
  97. #include "ListOfVulkanFunctions.inl"
  98. return true;
  99. }
  100. #pragma clang diagnostic pop
  101. } // namespace webrtc
  102. } // namespace unity