cuda-helpers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "obs-nvenc.h"
  2. #include "nvenc-internal.h"
  3. #include "cuda-helpers.h"
  4. #include <util/platform.h>
  5. #include <util/threading.h>
  6. #include <util/config-file.h>
  7. #include <util/dstr.h>
  8. #include <util/pipe.h>
  9. static void *cuda_lib = NULL;
  10. static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
  11. CudaFunctions *cu = NULL;
  12. bool load_cuda_lib(void)
  13. {
  14. #ifdef _WIN32
  15. cuda_lib = os_dlopen("nvcuda.dll");
  16. #else
  17. cuda_lib = os_dlopen("libcuda.so.1");
  18. #endif
  19. return cuda_lib != NULL;
  20. }
  21. static void *load_cuda_func(const char *func)
  22. {
  23. void *func_ptr = os_dlsym(cuda_lib, func);
  24. if (!func_ptr) {
  25. blog(LOG_ERROR, "[obs-nvenc] Could not load function: %s", func);
  26. }
  27. return func_ptr;
  28. }
  29. typedef struct cuda_function {
  30. ptrdiff_t offset;
  31. const char *name;
  32. } cuda_function;
  33. static const cuda_function cuda_functions[] = {
  34. {offsetof(CudaFunctions, cuInit), "cuInit"},
  35. {offsetof(CudaFunctions, cuDeviceGetCount), "cuDeviceGetCount"},
  36. {offsetof(CudaFunctions, cuDeviceGet), "cuDeviceGet"},
  37. {offsetof(CudaFunctions, cuDeviceGetAttribute), "cuDeviceGetAttribute"},
  38. {offsetof(CudaFunctions, cuCtxCreate), "cuCtxCreate_v2"},
  39. {offsetof(CudaFunctions, cuCtxDestroy), "cuCtxDestroy_v2"},
  40. {offsetof(CudaFunctions, cuCtxPushCurrent), "cuCtxPushCurrent_v2"},
  41. {offsetof(CudaFunctions, cuCtxPopCurrent), "cuCtxPopCurrent_v2"},
  42. {offsetof(CudaFunctions, cuArray3DCreate), "cuArray3DCreate_v2"},
  43. {offsetof(CudaFunctions, cuArrayDestroy), "cuArrayDestroy"},
  44. {offsetof(CudaFunctions, cuMemcpy2D), "cuMemcpy2D_v2"},
  45. {offsetof(CudaFunctions, cuGetErrorName), "cuGetErrorName"},
  46. {offsetof(CudaFunctions, cuGetErrorString), "cuGetErrorString"},
  47. {offsetof(CudaFunctions, cuMemHostRegister), "cuMemHostRegister_v2"},
  48. {offsetof(CudaFunctions, cuMemHostUnregister), "cuMemHostUnregister"},
  49. #ifndef _WIN32
  50. {offsetof(CudaFunctions, cuGLGetDevices), "cuGLGetDevices_v2"},
  51. {offsetof(CudaFunctions, cuGraphicsGLRegisterImage), "cuGraphicsGLRegisterImage"},
  52. {offsetof(CudaFunctions, cuGraphicsUnregisterResource), "cuGraphicsUnregisterResource"},
  53. {offsetof(CudaFunctions, cuGraphicsMapResources), "cuGraphicsMapResources"},
  54. {offsetof(CudaFunctions, cuGraphicsUnmapResources), "cuGraphicsUnmapResources"},
  55. {offsetof(CudaFunctions, cuGraphicsSubResourceGetMappedArray), "cuGraphicsSubResourceGetMappedArray"},
  56. #endif
  57. };
  58. static const size_t num_cuda_funcs = sizeof(cuda_functions) / sizeof(cuda_function);
  59. static bool init_cuda_internal(obs_encoder_t *encoder)
  60. {
  61. static bool initialized = false;
  62. static bool success = false;
  63. if (initialized)
  64. return success;
  65. initialized = true;
  66. if (!load_cuda_lib()) {
  67. obs_encoder_set_last_error(encoder, "Loading CUDA library failed.");
  68. return false;
  69. }
  70. cu = bzalloc(sizeof(CudaFunctions));
  71. for (size_t idx = 0; idx < num_cuda_funcs; idx++) {
  72. const cuda_function func = cuda_functions[idx];
  73. void *fptr = load_cuda_func(func.name);
  74. if (!fptr) {
  75. blog(LOG_ERROR, "[obs-nvenc] Failed to find CUDA function: %s", func.name);
  76. obs_encoder_set_last_error(encoder, "Loading CUDA functions failed.");
  77. return false;
  78. }
  79. *(uintptr_t *)((uintptr_t)cu + func.offset) = (uintptr_t)fptr;
  80. }
  81. success = true;
  82. return true;
  83. }
  84. bool cuda_get_error_desc(CUresult res, const char **name, const char **desc)
  85. {
  86. if (cu->cuGetErrorName(res, name) != CUDA_SUCCESS || cu->cuGetErrorString(res, desc) != CUDA_SUCCESS)
  87. return false;
  88. return true;
  89. }
  90. bool cuda_error_check(struct nvenc_data *enc, CUresult res, const char *func, const char *call)
  91. {
  92. if (res == CUDA_SUCCESS)
  93. return true;
  94. struct dstr message = {0};
  95. const char *name, *desc;
  96. if (cuda_get_error_desc(res, &name, &desc)) {
  97. dstr_printf(&message, "%s: CUDA call \"%s\" failed with %s (%d): %s", func, call, name, res, desc);
  98. } else {
  99. dstr_printf(&message, "%s: CUDA call \"%s\" failed with %d", func, call, res);
  100. }
  101. error("%s", message.array);
  102. obs_encoder_set_last_error(enc->encoder, message.array);
  103. dstr_free(&message);
  104. return false;
  105. }
  106. bool init_cuda(obs_encoder_t *encoder)
  107. {
  108. bool success;
  109. pthread_mutex_lock(&init_mutex);
  110. success = init_cuda_internal(encoder);
  111. pthread_mutex_unlock(&init_mutex);
  112. return success;
  113. }
  114. void obs_cuda_load(void)
  115. {
  116. pthread_mutex_init(&init_mutex, NULL);
  117. }
  118. void obs_cuda_unload(void)
  119. {
  120. bfree(cu);
  121. pthread_mutex_destroy(&init_mutex);
  122. }