cuda-helpers.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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",
  26. func);
  27. }
  28. return func_ptr;
  29. }
  30. typedef struct cuda_function {
  31. ptrdiff_t offset;
  32. const char *name;
  33. } cuda_function;
  34. static const cuda_function cuda_functions[] = {
  35. {offsetof(CudaFunctions, cuInit), "cuInit"},
  36. {offsetof(CudaFunctions, cuDeviceGetCount), "cuDeviceGetCount"},
  37. {offsetof(CudaFunctions, cuDeviceGet), "cuDeviceGet"},
  38. {offsetof(CudaFunctions, cuDeviceGetAttribute), "cuDeviceGetAttribute"},
  39. {offsetof(CudaFunctions, cuCtxCreate), "cuCtxCreate_v2"},
  40. {offsetof(CudaFunctions, cuCtxDestroy), "cuCtxDestroy_v2"},
  41. {offsetof(CudaFunctions, cuCtxPushCurrent), "cuCtxPushCurrent_v2"},
  42. {offsetof(CudaFunctions, cuCtxPopCurrent), "cuCtxPopCurrent_v2"},
  43. {offsetof(CudaFunctions, cuArray3DCreate), "cuArray3DCreate_v2"},
  44. {offsetof(CudaFunctions, cuArrayDestroy), "cuArrayDestroy"},
  45. {offsetof(CudaFunctions, cuMemcpy2D), "cuMemcpy2D_v2"},
  46. {offsetof(CudaFunctions, cuGetErrorName), "cuGetErrorName"},
  47. {offsetof(CudaFunctions, cuGetErrorString), "cuGetErrorString"},
  48. {offsetof(CudaFunctions, cuMemHostRegister), "cuMemHostRegister_v2"},
  49. {offsetof(CudaFunctions, cuMemHostUnregister), "cuMemHostUnregister"},
  50. #ifndef _WIN32
  51. {offsetof(CudaFunctions, cuGLGetDevices), "cuGLGetDevices_v2"},
  52. {offsetof(CudaFunctions, cuGraphicsGLRegisterImage),
  53. "cuGraphicsGLRegisterImage"},
  54. {offsetof(CudaFunctions, cuGraphicsUnregisterResource),
  55. "cuGraphicsUnregisterResource"},
  56. {offsetof(CudaFunctions, cuGraphicsMapResources),
  57. "cuGraphicsMapResources"},
  58. {offsetof(CudaFunctions, cuGraphicsUnmapResources),
  59. "cuGraphicsUnmapResources"},
  60. {offsetof(CudaFunctions, cuGraphicsSubResourceGetMappedArray),
  61. "cuGraphicsSubResourceGetMappedArray"},
  62. #endif
  63. };
  64. static const size_t num_cuda_funcs =
  65. sizeof(cuda_functions) / sizeof(cuda_function);
  66. static bool init_cuda_internal(obs_encoder_t *encoder)
  67. {
  68. static bool initialized = false;
  69. static bool success = false;
  70. if (initialized)
  71. return success;
  72. initialized = true;
  73. if (!load_cuda_lib()) {
  74. obs_encoder_set_last_error(encoder,
  75. "Loading CUDA library failed.");
  76. return false;
  77. }
  78. cu = bzalloc(sizeof(CudaFunctions));
  79. for (size_t idx = 0; idx < num_cuda_funcs; idx++) {
  80. const cuda_function func = cuda_functions[idx];
  81. void *fptr = load_cuda_func(func.name);
  82. if (!fptr) {
  83. blog(LOG_ERROR,
  84. "[obs-nvenc] Failed to find CUDA function: %s",
  85. func.name);
  86. obs_encoder_set_last_error(
  87. encoder, "Loading CUDA functions failed.");
  88. return false;
  89. }
  90. *(uintptr_t *)((uintptr_t)cu + func.offset) = (uintptr_t)fptr;
  91. }
  92. success = true;
  93. return true;
  94. }
  95. bool cuda_get_error_desc(CUresult res, const char **name, const char **desc)
  96. {
  97. if (cu->cuGetErrorName(res, name) != CUDA_SUCCESS ||
  98. cu->cuGetErrorString(res, desc) != CUDA_SUCCESS)
  99. return false;
  100. return true;
  101. }
  102. bool cuda_error_check(struct nvenc_data *enc, CUresult res, const char *func,
  103. const char *call)
  104. {
  105. if (res == CUDA_SUCCESS)
  106. return true;
  107. struct dstr message = {0};
  108. const char *name, *desc;
  109. if (cuda_get_error_desc(res, &name, &desc)) {
  110. dstr_printf(&message,
  111. "%s: CUDA call \"%s\" failed with %s (%d): %s",
  112. func, call, name, res, desc);
  113. } else {
  114. dstr_printf(&message, "%s: CUDA call \"%s\" failed with %d",
  115. func, call, res);
  116. }
  117. error("%s", message.array);
  118. obs_encoder_set_last_error(enc->encoder, message.array);
  119. dstr_free(&message);
  120. return false;
  121. }
  122. bool init_cuda(obs_encoder_t *encoder)
  123. {
  124. bool success;
  125. pthread_mutex_lock(&init_mutex);
  126. success = init_cuda_internal(encoder);
  127. pthread_mutex_unlock(&init_mutex);
  128. return success;
  129. }
  130. void obs_cuda_load(void)
  131. {
  132. pthread_mutex_init(&init_mutex, NULL);
  133. }
  134. void obs_cuda_unload(void)
  135. {
  136. bfree(cu);
  137. pthread_mutex_destroy(&init_mutex);
  138. }