39c15be7eb7320e1016b1a28e7dff0f66b7dc589
[platform/upstream/gstreamer.git] / sys / nvcodec / gstcudanvrtc.c
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstcudanvrtc.h"
25
26 GST_DEBUG_CATEGORY_STATIC (gst_cuda_nvrtc_debug);
27 #define GST_CAT_DEFAULT gst_cuda_nvrtc_debug
28
29 static void
30 _init_debug (void)
31 {
32   static volatile gsize once_init = 0;
33
34   if (g_once_init_enter (&once_init)) {
35
36     GST_DEBUG_CATEGORY_INIT (gst_cuda_nvrtc_debug, "cudanvrtc", 0,
37         "CUDA runtime compiler");
38     g_once_init_leave (&once_init, 1);
39   }
40 }
41
42 gchar *
43 gst_cuda_nvrtc_compile (const gchar * source)
44 {
45   nvrtcProgram prog;
46   nvrtcResult ret;
47   CUresult curet;
48   const gchar *opts[] = { "--gpu-architecture=compute_30" };
49   gsize ptx_size;
50   gchar *ptx = NULL;
51   int driverVersion;
52
53   g_return_val_if_fail (source != NULL, FALSE);
54
55   _init_debug ();
56
57   GST_TRACE ("CUDA kernel source \n%s", source);
58
59   curet = CuDriverGetVersion (&driverVersion);
60   if (curet != CUDA_SUCCESS) {
61     GST_ERROR ("Failed to query CUDA Driver version, ret %d", curet);
62     return NULL;
63   }
64
65   GST_DEBUG ("CUDA Driver Version %d.%d", driverVersion / 1000,
66       (driverVersion % 1000) / 10);
67
68   ret = NvrtcCreateProgram (&prog, source, NULL, 0, NULL, NULL);
69   if (ret != NVRTC_SUCCESS) {
70     GST_ERROR ("couldn't create nvrtc program, ret %d", ret);
71     return NULL;
72   }
73
74   /* Starting from CUDA 11, the lowest supported architecture is 5.2 */
75   if (driverVersion >= 11000)
76     opts[0] = "--gpu-architecture=compute_52";
77
78   ret = NvrtcCompileProgram (prog, 1, opts);
79   if (ret != NVRTC_SUCCESS) {
80     gsize log_size;
81
82     GST_ERROR ("couldn't compile nvrtc program, ret %d", ret);
83     if (NvrtcGetProgramLogSize (prog, &log_size) == NVRTC_SUCCESS &&
84         log_size > 0) {
85       gchar *compile_log = g_alloca (log_size);
86       if (NvrtcGetProgramLog (prog, compile_log) == NVRTC_SUCCESS) {
87         GST_ERROR ("nvrtc compile log %s", compile_log);
88       }
89     }
90
91     goto error;
92   }
93
94   ret = NvrtcGetPTXSize (prog, &ptx_size);
95   if (ret != NVRTC_SUCCESS) {
96     GST_ERROR ("unknown ptx size, ret %d", ret);
97
98     goto error;
99   }
100
101   ptx = g_malloc0 (ptx_size);
102   ret = NvrtcGetPTX (prog, ptx);
103   if (ret != NVRTC_SUCCESS) {
104     GST_ERROR ("couldn't get ptx, ret %d", ret);
105     g_free (ptx);
106
107     goto error;
108   }
109
110   NvrtcDestroyProgram (&prog);
111
112   GST_TRACE ("compiled CUDA PTX %s\n", ptx);
113
114   return ptx;
115
116 error:
117   NvrtcDestroyProgram (&prog);
118
119   return NULL;
120 }