NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DLLImports.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/DLLImports.hpp"
29 #include "base/String.hpp"
30 
31 using namespace FW;
32 
33 //------------------------------------------------------------------------
34 
35 static bool s_inited = false;
36 static char s_cudaDLLName[1024] = "nvcuda.dll";
37 
38 //------------------------------------------------------------------------
39 
40 static struct
41 {
42  const char* name;
43  HMODULE handle;
44 }
45 s_importDLLs[] =
46 {
47  { s_cudaDLLName, NULL },
48  { "kernel32.dll", NULL },
49  { "winmm.dll", NULL },
50  { "shlwapi.dll", NULL },
51 };
52 
53 //------------------------------------------------------------------------
54 
55 #define FW_DLL_IMPORT_RETV(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
56 #define FW_DLL_IMPORT_VOID(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
57 #define FW_DLL_DECLARE_RETV(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
58 #define FW_DLL_DECLARE_VOID(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
59 #define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
60 #define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) static RET (CALL *s_ ## NAME)PARAMS = NULL;
61 #include "base/DLLImports.inl"
62 #undef FW_DLL_IMPORT_RETV
63 #undef FW_DLL_IMPORT_VOID
64 #undef FW_DLL_DECLARE_RETV
65 #undef FW_DLL_DECLARE_VOID
66 #undef FW_DLL_IMPORT_CUDA
67 #undef FW_DLL_IMPORT_CUV2
68 
69 //------------------------------------------------------------------------
70 
71 static const struct
72 {
73  const char* name;
74  void** ptr;
75 }
76 s_importFuncs[] =
77 {
78 #define FW_DLL_IMPORT_RETV(RET, CALL, NAME, PARAMS, PASS) { #NAME, (void**)&s_ ## NAME },
79 #define FW_DLL_IMPORT_VOID(RET, CALL, NAME, PARAMS, PASS) { #NAME, (void**)&s_ ## NAME },
80 #define FW_DLL_DECLARE_RETV(RET, CALL, NAME, PARAMS, PASS) { #NAME, (void**)&s_ ## NAME },
81 #define FW_DLL_DECLARE_VOID(RET, CALL, NAME, PARAMS, PASS) { #NAME, (void**)&s_ ## NAME },
82 #define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) { #NAME, (void**)&s_ ## NAME },
83 #define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) { #NAME "_v2", (void**)&s_ ## NAME }, { #NAME, (void**)&s_ ## NAME },
84 #include "base/DLLImports.inl"
85 #undef FW_DLL_IMPORT_RETV
86 #undef FW_DLL_IMPORT_VOID
87 #undef FW_DLL_DECLARE_RETV
88 #undef FW_DLL_DECLARE_VOID
89 #undef FW_DLL_IMPORT_CUDA
90 #undef FW_DLL_IMPORT_CUV2
91 };
92 
93 //------------------------------------------------------------------------
94 
96 {
97  FW_ASSERT(!s_inited);
98  FW_ASSERT(name.getLength() + 1 <= (int)FW_ARRAY_SIZE(s_cudaDLLName));
99  memcpy(s_cudaDLLName, name.getPtr(), name.getLength() + 1);
100 }
101 
102 //------------------------------------------------------------------------
103 
105 {
106  if (s_inited)
107  return;
108  s_inited = true;
109 
110  for (int i = 0; i < (int)FW_ARRAY_SIZE(s_importDLLs); i++)
111  {
112  s_importDLLs[i].handle = LoadLibrary(s_importDLLs[i].name);
113  if (s_importDLLs[i].handle)
114  for (int j = 0; j < (int)FW_ARRAY_SIZE(s_importFuncs); j++)
115  if (!*s_importFuncs[j].ptr)
116  *s_importFuncs[j].ptr = (void*)GetProcAddress(s_importDLLs[i].handle, s_importFuncs[j].name);
117  }
118 }
119 
120 //------------------------------------------------------------------------
121 
123 {
124  initDLLImports();
125  for (int i = 0; i < (int)FW_ARRAY_SIZE(s_importFuncs); i++)
126  if (!*s_importFuncs[i].ptr)
127  *s_importFuncs[i].ptr = (void*)wglGetProcAddress(s_importFuncs[i].name);
128 }
129 
130 //------------------------------------------------------------------------
131 
133 {
134  if (!s_inited)
135  return;
136  s_inited = false;
137 
138  for (int i = 0; i < (int)FW_ARRAY_SIZE(s_importDLLs); i++)
139  {
140  if (s_importDLLs[i].handle)
141  FreeLibrary(s_importDLLs[i].handle);
142  s_importDLLs[i].handle = NULL;
143  }
144 
145  for (int i = 0; i < (int)FW_ARRAY_SIZE(s_importFuncs); i++)
146  *s_importFuncs[i].ptr = NULL;
147 }
148 
149 //------------------------------------------------------------------------
150 
151 #define FW_DLL_IMPORT_RETV(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); return s_ ## NAME PASS; }
152 #define FW_DLL_IMPORT_VOID(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); s_ ## NAME PASS; }
153 #define FW_DLL_DECLARE_RETV(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); return s_ ## NAME PASS; }
154 #define FW_DLL_DECLARE_VOID(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); s_ ## NAME PASS; }
155 #if (FW_USE_CUDA)
156 # define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); return s_ ## NAME PASS; }
157 # define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { if (!s_inited) initDLLImports(); if (!s_ ## NAME) fail("Failed to import " #NAME "()!"); return s_ ## NAME PASS; }
158 #else
159 # define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { fail(#NAME "(): Built without FW_USE_CUDA!"); return s_ ## NAME PASS; }
160 # define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) RET CALL NAME PARAMS { fail(#NAME "(): Built without FW_USE_CUDA!"); return s_ ## NAME PASS; }
161 #endif
162 #include "base/DLLImports.inl"
163 #undef FW_DLL_IMPORT_RETV
164 #undef FW_DLL_IMPORT_VOID
165 #undef FW_DLL_DECLARE_RETV
166 #undef FW_DLL_DECLARE_VOID
167 #undef FW_DLL_IMPORT_CUDA
168 #undef FW_DLL_IMPORT_CUV2
169 
170 //------------------------------------------------------------------------
171 
172 #define FW_DLL_IMPORT_RETV(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
173 #define FW_DLL_IMPORT_VOID(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
174 #define FW_DLL_DECLARE_RETV(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
175 #define FW_DLL_DECLARE_VOID(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
176 #if (FW_USE_CUDA)
177 # define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
178 # define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { if (!s_inited) initDLLImports(); return (s_ ## NAME != NULL); }
179 #else
180 # define FW_DLL_IMPORT_CUDA(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { return false; }
181 # define FW_DLL_IMPORT_CUV2(RET, CALL, NAME, PARAMS, PASS) bool isAvailable_ ## NAME(void) { return false; }
182 #endif
183 #include "base/DLLImports.inl"
184 #undef FW_DLL_IMPORT_RETV
185 #undef FW_DLL_IMPORT_VOID
186 #undef FW_DLL_DECLARE_RETV
187 #undef FW_DLL_DECLARE_VOID
188 #undef FW_DLL_IMPORT_CUDA
189 #undef FW_DLL_IMPORT_CUV2
190 
191 //------------------------------------------------------------------------
#define NULL
Definition: Defs.hpp:39
const char * getPtr(void) const
Definition: String.hpp:51
const char * name
Definition: DLLImports.cpp:42
void ** ptr
Definition: DLLImports.cpp:74
void initGLImports(void)
Definition: DLLImports.cpp:122
void deinitDLLImports(void)
Definition: DLLImports.cpp:132
void setCudaDLLName(const String &name)
Definition: DLLImports.cpp:95
#define FW_ASSERT(X)
Definition: Defs.hpp:67
int getLength(void) const
Definition: String.hpp:49
#define FW_ARRAY_SIZE(X)
Definition: Defs.hpp:79
HMODULE handle
Definition: DLLImports.cpp:43
void initDLLImports(void)
Definition: DLLImports.cpp:104