Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 3 | * Version: 6.3 |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 4 | * |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 Brian Paul All Rights Reserved. |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | |
Brian Paul | 7e1161b | 1999-11-25 18:17:04 +0000 | [diff] [blame] | 26 | /* |
| 27 | * This file manages the OpenGL API dispatch layer. |
| 28 | * The dispatch table (struct _glapi_table) is basically just a list |
| 29 | * of function pointers. |
| 30 | * There are functions to set/get the current dispatch table for the |
| 31 | * current thread and to manage registration/dispatch of dynamically |
| 32 | * added extension functions. |
Brian Paul | 9f94399 | 2000-01-28 19:03:33 +0000 | [diff] [blame] | 33 | * |
| 34 | * It's intended that this file and the other glapi*.[ch] files are |
| 35 | * flexible enough to be reused in several places: XFree86, DRI- |
| 36 | * based libGL.so, and perhaps the SGI SI. |
| 37 | * |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 38 | * NOTE: There are no dependencies on Mesa in this code. |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 39 | * |
| 40 | * Versions (API changes): |
| 41 | * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0 |
| 42 | * 2001/01/16 - added dispatch override feature for Mesa 3.5 |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 43 | * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1. |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 44 | * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints |
| 45 | * itself (using offset ~0). _glapi_add_entrypoint() can be |
| 46 | * called afterward and it'll fill in the correct dispatch |
| 47 | * offset. This allows DRI libGL to avoid probing for DRI |
| 48 | * drivers! No changes to the public glapi interface. |
Brian Paul | 7e1161b | 1999-11-25 18:17:04 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | |
| 52 | |
Brian Paul | 3c27be3 | 2000-02-10 21:27:48 +0000 | [diff] [blame] | 53 | #include "glheader.h" |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 54 | #include "glapi.h" |
Brian Paul | 0c239fc | 1999-12-16 12:38:11 +0000 | [diff] [blame] | 55 | #include "glapioffsets.h" |
| 56 | #include "glapitable.h" |
Brian Paul | 9f94399 | 2000-01-28 19:03:33 +0000 | [diff] [blame] | 57 | #include "glthread.h" |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 58 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 59 | /***** BEGIN NO-OP DISPATCH *****/ |
| 60 | |
| 61 | static GLboolean WarnFlag = GL_FALSE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 62 | static _glapi_warning_func warning_func; |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 63 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 64 | static void init_glapi_relocs(void); |
| 65 | |
| 66 | static _glapi_proc generate_entrypoint(GLuint functionOffset); |
| 67 | static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset); |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Enable/disable printing of warning messages. |
| 71 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 72 | PUBLIC void |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 73 | _glapi_noop_enable_warnings(GLboolean enable) |
| 74 | { |
| 75 | WarnFlag = enable; |
| 76 | } |
| 77 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 78 | /* |
| 79 | * Register a callback function for reporting errors. |
| 80 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 81 | PUBLIC void |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 82 | _glapi_set_warning_func( _glapi_warning_func func ) |
| 83 | { |
| 84 | warning_func = func; |
| 85 | } |
| 86 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 87 | static GLboolean |
| 88 | warn(void) |
| 89 | { |
Ben Crossman | d8aa5ff | 2005-04-15 09:13:21 +0000 | [diff] [blame] | 90 | if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 91 | && warning_func) { |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 92 | return GL_TRUE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 93 | } |
| 94 | else { |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 95 | return GL_FALSE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 96 | } |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | #define KEYWORD1 static |
Daniel Borca | 722cb89 | 2004-01-07 12:37:09 +0000 | [diff] [blame] | 101 | #define KEYWORD2 GLAPIENTRY |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 102 | #define NAME(func) NoOp##func |
| 103 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 104 | #define F NULL |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 105 | |
Brian Paul | 5849e3d | 2004-11-05 18:32:02 +0000 | [diff] [blame] | 106 | #define DISPATCH(func, args, msg) \ |
| 107 | if (warn()) { \ |
| 108 | warning_func(NULL, "GL User Error: called without context: %s", #func); \ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Brian Paul | 5849e3d | 2004-11-05 18:32:02 +0000 | [diff] [blame] | 111 | #define RETURN_DISPATCH(func, args, msg) \ |
| 112 | if (warn()) { \ |
| 113 | warning_func(NULL, "GL User Error: called without context: %s", #func); \ |
| 114 | } \ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 115 | return 0 |
| 116 | |
| 117 | #define DISPATCH_TABLE_NAME __glapi_noop_table |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 118 | #define UNUSED_TABLE_NAME __unused_noop_functions |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 119 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 120 | #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 121 | |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 122 | static GLint NoOpUnused(void) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 123 | { |
| 124 | if (warn()) { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 125 | warning_func(NULL, "GL User Error: calling extension function without a current context\n"); |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | #include "glapitemp.h" |
| 131 | |
| 132 | /***** END NO-OP DISPATCH *****/ |
| 133 | |
| 134 | |
| 135 | |
| 136 | /***** BEGIN THREAD-SAFE DISPATCH *****/ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 137 | |
| 138 | #if defined(THREADS) |
| 139 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 140 | #if defined(GLX_USE_TLS) |
| 141 | |
| 142 | __thread struct _glapi_table * _glapi_tls_Dispatch |
| 143 | __attribute__((tls_model("initial-exec"))) |
| 144 | = (struct _glapi_table *) __glapi_noop_table; |
| 145 | |
| 146 | static __thread struct _glapi_table * _glapi_tls_RealDispatch |
| 147 | __attribute__((tls_model("initial-exec"))) |
| 148 | = (struct _glapi_table *) __glapi_noop_table; |
| 149 | |
| 150 | __thread void * _glapi_tls_Context |
| 151 | __attribute__((tls_model("initial-exec"))); |
| 152 | |
| 153 | /** |
| 154 | * Legacy per-thread dispatch pointer. This is only needed to support |
| 155 | * non-TLS DRI drivers. |
| 156 | */ |
| 157 | |
| 158 | _glthread_TSD _gl_DispatchTSD; |
| 159 | |
| 160 | #else |
| 161 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 162 | /** |
| 163 | * \name Multi-threaded control support variables |
| 164 | * |
| 165 | * If thread-safety is supported, there are two potential mechanisms that can |
| 166 | * be used. The old-style mechanism would set \c _glapi_Dispatch to a special |
| 167 | * thread-safe dispatch table. These dispatch routines would call |
| 168 | * \c _glapi_get_dispatch to get the actual dispatch pointer. In this |
| 169 | * setup \c _glapi_Dispatch could never be \c NULL. This dual layered |
| 170 | * dispatch setup performed great for single-threaded apps, but didn't |
| 171 | * perform well for multithreaded apps. |
| 172 | * |
| 173 | * In the new mechansim, there are two variables. The first is |
| 174 | * \c _glapi_DispatchTSD. In the single-threaded case, this variable points |
| 175 | * to the dispatch table. In the multi-threaded case, this variable is |
| 176 | * \c NULL, and thread-specific variable \c _gl_DispatchTSD points to the |
| 177 | * actual dispatch table. \c _glapi_DispatchTSD is used to signal to the |
| 178 | * static dispatch functions to call \c _glapi_get_dispatch to get the real |
| 179 | * dispatch table. |
| 180 | * |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 181 | * There is a race condition in setting \c _glapi_DispatchTSD to \c NULL. |
| 182 | * It is possible for the original thread to be setting it at the same instant |
| 183 | * a new thread, perhaps running on a different processor, is clearing it. |
| 184 | * Because of that, \c ThreadSafe, which can only ever be changed to |
Ian Romanick | 10b3bf6 | 2004-07-05 22:42:14 +0000 | [diff] [blame] | 185 | * \c GL_TRUE, is used to determine whether or not the application is |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 186 | * multithreaded. |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 187 | */ |
| 188 | /*@{*/ |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 189 | static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */ |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 190 | _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */ |
| 191 | static _glthread_TSD RealDispatchTSD; /**< only when using override */ |
| 192 | static _glthread_TSD ContextTSD; /**< Per-thread context pointer */ |
| 193 | /*@}*/ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 194 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 195 | #endif /* defined(GLX_USE_TLS) */ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 196 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 197 | #define DISPATCH_TABLE_NAME __glapi_threadsafe_table |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 198 | #define UNUSED_TABLE_NAME __unused_threadsafe_functions |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 199 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 200 | #define TABLE_ENTRY(name) (_glapi_proc) gl##name |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 201 | |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 202 | static GLint glUnused(void) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 203 | { |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | #include "glapitemp.h" |
| 208 | |
| 209 | #endif |
| 210 | |
| 211 | /***** END THREAD-SAFE DISPATCH *****/ |
| 212 | |
| 213 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 214 | #if defined(GLX_USE_TLS) |
| 215 | |
| 216 | /** |
| 217 | * \name Old dispatch pointers |
| 218 | * |
| 219 | * Very old DRI based drivers assume that \c _glapi_Dispatch will never be |
| 220 | * \c NULL. Becuase of that, special "thread-safe" dispatch functions are |
| 221 | * needed here. Slightly more recent drivers detect the multi-threaded case |
| 222 | * by \c _glapi_DispatchTSD being \c NULL. |
| 223 | * |
| 224 | * \deprecated |
| 225 | * |
| 226 | * \warning |
| 227 | * \c _glapi_RealDispatch does not exist in TLS builds. I don't think it was |
| 228 | * ever used outside libGL.so, so this should be safe. |
| 229 | */ |
| 230 | /*@{*/ |
| 231 | PUBLIC const struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; |
| 232 | PUBLIC const struct _glapi_table *_glapi_DispatchTSD = NULL; |
| 233 | PUBLIC const void *_glapi_Context = NULL; |
| 234 | /*@}*/ |
| 235 | |
| 236 | #else |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 237 | |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 238 | PUBLIC struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table; |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 239 | #if defined( THREADS ) |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 240 | PUBLIC struct _glapi_table *_glapi_DispatchTSD = (struct _glapi_table *) __glapi_noop_table; |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 241 | #endif |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 242 | PUBLIC struct _glapi_table *_glapi_RealDispatch = (struct _glapi_table *) __glapi_noop_table; |
Brian Paul | 0f71025 | 1999-12-15 15:02:30 +0000 | [diff] [blame] | 243 | |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 244 | /* Used when thread safety disabled */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 245 | PUBLIC void *_glapi_Context = NULL; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 246 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 247 | #endif /* defined(GLX_USE_TLS) */ |
| 248 | |
Brian Paul | 0f71025 | 1999-12-15 15:02:30 +0000 | [diff] [blame] | 249 | |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 250 | static GLboolean DispatchOverride = GL_FALSE; |
| 251 | |
| 252 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 253 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 254 | /** |
| 255 | * strdup() is actually not a standard ANSI C or POSIX routine. |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 256 | * Irix will not define it if ANSI mode is in effect. |
| 257 | */ |
| 258 | static char * |
| 259 | str_dup(const char *str) |
Randy Frank | d7361e1 | 2000-03-27 21:13:58 +0000 | [diff] [blame] | 260 | { |
Brian Paul | fffb809 | 2000-03-29 18:46:11 +0000 | [diff] [blame] | 261 | char *copy; |
| 262 | copy = (char*) malloc(strlen(str) + 1); |
| 263 | if (!copy) |
| 264 | return NULL; |
| 265 | strcpy(copy, str); |
| 266 | return copy; |
Randy Frank | d7361e1 | 2000-03-27 21:13:58 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 269 | |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 270 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 271 | /** |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 272 | * We should call this periodically from a function such as glXMakeCurrent |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 273 | * in order to test if multiple threads are being used. |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 274 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 275 | PUBLIC void |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 276 | _glapi_check_multithread(void) |
| 277 | { |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 278 | #if defined(THREADS) && !defined(GLX_USE_TLS) |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 279 | if (!ThreadSafe) { |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 280 | static unsigned long knownID; |
| 281 | static GLboolean firstCall = GL_TRUE; |
| 282 | if (firstCall) { |
| 283 | knownID = _glthread_GetID(); |
| 284 | firstCall = GL_FALSE; |
| 285 | } |
| 286 | else if (knownID != _glthread_GetID()) { |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 287 | ThreadSafe = GL_TRUE; |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 288 | _glapi_set_dispatch(NULL); |
| 289 | } |
| 290 | } |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 291 | else if (!_glapi_get_dispatch()) { |
| 292 | /* make sure that this thread's dispatch pointer isn't null */ |
| 293 | _glapi_set_dispatch(NULL); |
| 294 | } |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 295 | #endif |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 300 | /** |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 301 | * Set the current context pointer for this thread. |
| 302 | * The context pointer is an opaque type which should be cast to |
| 303 | * void from the real context pointer type. |
| 304 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 305 | PUBLIC void |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 306 | _glapi_set_context(void *context) |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 307 | { |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 308 | (void) __unused_noop_functions; /* silence a warning */ |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 309 | #if defined(GLX_USE_TLS) |
| 310 | _glapi_tls_Context = context; |
| 311 | #elif defined(THREADS) |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 312 | (void) __unused_threadsafe_functions; /* silence a warning */ |
Brian Paul | 3c27be3 | 2000-02-10 21:27:48 +0000 | [diff] [blame] | 313 | _glthread_SetTSD(&ContextTSD, context); |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 314 | _glapi_Context = (ThreadSafe) ? NULL : context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 315 | #else |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 316 | _glapi_Context = context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 317 | #endif |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 322 | /** |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 323 | * Get the current context pointer for this thread. |
| 324 | * The context pointer is an opaque type which should be cast from |
| 325 | * void to the real context pointer type. |
| 326 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 327 | PUBLIC void * |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 328 | _glapi_get_context(void) |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 329 | { |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 330 | #if defined(GLX_USE_TLS) |
| 331 | return _glapi_tls_Context; |
| 332 | #elif defined(THREADS) |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 333 | if (ThreadSafe) { |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 334 | return _glthread_GetTSD(&ContextTSD); |
| 335 | } |
| 336 | else { |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 337 | return _glapi_Context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 338 | } |
| 339 | #else |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 340 | return _glapi_Context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 341 | #endif |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 346 | /** |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 347 | * Set the global or per-thread dispatch table pointer. |
| 348 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 349 | PUBLIC void |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 350 | _glapi_set_dispatch(struct _glapi_table *dispatch) |
| 351 | { |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 352 | #if defined(PTHREADS) || defined(GLX_USE_TLS) |
| 353 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 354 | |
| 355 | |
| 356 | pthread_once( & once_control, init_glapi_relocs ); |
| 357 | #endif |
| 358 | |
Ian Romanick | 6c5402b | 2004-07-05 22:40:54 +0000 | [diff] [blame] | 359 | if (!dispatch) { |
| 360 | /* use the no-op functions */ |
| 361 | dispatch = (struct _glapi_table *) __glapi_noop_table; |
| 362 | } |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 363 | #ifdef DEBUG |
Ian Romanick | 6c5402b | 2004-07-05 22:40:54 +0000 | [diff] [blame] | 364 | else { |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 365 | _glapi_check_table(dispatch); |
| 366 | } |
| 367 | #endif |
| 368 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 369 | #if defined(GLX_USE_TLS) |
| 370 | if (DispatchOverride) { |
| 371 | _glapi_tls_RealDispatch = dispatch; |
| 372 | } |
| 373 | else { |
| 374 | _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch); |
| 375 | _glapi_tls_Dispatch = dispatch; |
| 376 | } |
| 377 | #elif defined(THREADS) |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 378 | if (DispatchOverride) { |
Ian Romanick | 6c5402b | 2004-07-05 22:40:54 +0000 | [diff] [blame] | 379 | _glthread_SetTSD(&RealDispatchTSD, (void *) dispatch); |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 380 | if (ThreadSafe) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 381 | _glapi_RealDispatch = (struct _glapi_table*) __glapi_threadsafe_table; |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 382 | else |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 383 | _glapi_RealDispatch = dispatch; |
Brian Paul | 3b18a36 | 2000-09-26 15:27:20 +0000 | [diff] [blame] | 384 | } |
| 385 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 386 | /* normal operation */ |
Ian Romanick | 6c5402b | 2004-07-05 22:40:54 +0000 | [diff] [blame] | 387 | _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch); |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 388 | if (ThreadSafe) { |
| 389 | _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; |
| 390 | _glapi_DispatchTSD = NULL; |
| 391 | } |
| 392 | else { |
Ian Romanick | 6c5402b | 2004-07-05 22:40:54 +0000 | [diff] [blame] | 393 | _glapi_Dispatch = dispatch; |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 394 | _glapi_DispatchTSD = dispatch; |
| 395 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 396 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 397 | #else /*THREADS*/ |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 398 | if (DispatchOverride) { |
| 399 | _glapi_RealDispatch = dispatch; |
| 400 | } |
| 401 | else { |
| 402 | _glapi_Dispatch = dispatch; |
| 403 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 404 | #endif /*THREADS*/ |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 408 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 409 | /** |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 410 | * Return pointer to current dispatch table for calling thread. |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 411 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 412 | PUBLIC struct _glapi_table * |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 413 | _glapi_get_dispatch(void) |
| 414 | { |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 415 | #if defined(GLX_USE_TLS) |
| 416 | struct _glapi_table * api = (DispatchOverride) |
| 417 | ? _glapi_tls_RealDispatch : _glapi_tls_Dispatch; |
| 418 | |
| 419 | assert( api != NULL ); |
| 420 | return api; |
| 421 | #elif defined(THREADS) |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 422 | if (ThreadSafe) { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 423 | if (DispatchOverride) { |
| 424 | return (struct _glapi_table *) _glthread_GetTSD(&RealDispatchTSD); |
| 425 | } |
| 426 | else { |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 427 | return (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD); |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 428 | } |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 429 | } |
Brian Paul | 590d347 | 1999-12-17 12:20:23 +0000 | [diff] [blame] | 430 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 431 | if (DispatchOverride) { |
| 432 | assert(_glapi_RealDispatch); |
| 433 | return _glapi_RealDispatch; |
| 434 | } |
| 435 | else { |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 436 | assert(_glapi_DispatchTSD); |
| 437 | return _glapi_DispatchTSD; |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 438 | } |
Brian Paul | 590d347 | 1999-12-17 12:20:23 +0000 | [diff] [blame] | 439 | } |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 440 | #else |
Brian Paul | c2319b4 | 2000-01-17 19:28:31 +0000 | [diff] [blame] | 441 | return _glapi_Dispatch; |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 442 | #endif |
| 443 | } |
| 444 | |
| 445 | |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 446 | /* |
| 447 | * Notes on dispatch overrride: |
| 448 | * |
| 449 | * Dispatch override allows an external agent to hook into the GL dispatch |
| 450 | * mechanism before execution goes into the core rendering library. For |
| 451 | * example, a trace mechanism would insert itself as an overrider, print |
| 452 | * logging info for each GL function, then dispatch to the real GL function. |
| 453 | * |
| 454 | * libGLS (GL Stream library) is another agent that might use override. |
| 455 | * |
| 456 | * We don't allow more than one layer of overriding at this time. |
| 457 | * In the future we may allow nested/layered override. In that case |
| 458 | * _glapi_begin_dispatch_override() will return an override layer, |
| 459 | * _glapi_end_dispatch_override(layer) will remove an override layer |
| 460 | * and _glapi_get_override_dispatch(layer) will return the dispatch |
| 461 | * table for a given override layer. layer = 0 will be the "real" |
| 462 | * dispatch table. |
| 463 | */ |
| 464 | |
| 465 | /* |
| 466 | * Return: dispatch override layer number. |
| 467 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 468 | PUBLIC int |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 469 | _glapi_begin_dispatch_override(struct _glapi_table *override) |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 470 | { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 471 | struct _glapi_table *real = _glapi_get_dispatch(); |
| 472 | |
| 473 | assert(!DispatchOverride); /* can't nest at this time */ |
| 474 | DispatchOverride = GL_TRUE; |
| 475 | |
| 476 | _glapi_set_dispatch(real); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 477 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 478 | #if defined(GLX_USE_TLS) |
| 479 | _glthread_SetTSD(&_gl_DispatchTSD, (void *) override); |
| 480 | _glapi_tls_Dispatch = override; |
| 481 | #elif defined(THREADS) |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 482 | _glthread_SetTSD(&_gl_DispatchTSD, (void *) override); |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 483 | if ( ThreadSafe ) { |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 484 | _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; |
Ian Romanick | d14d103 | 2004-07-02 00:01:09 +0000 | [diff] [blame] | 485 | _glapi_DispatchTSD = NULL; |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 486 | } |
| 487 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 488 | _glapi_Dispatch = override; |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 489 | _glapi_DispatchTSD = override; |
| 490 | } |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 491 | #else |
| 492 | _glapi_Dispatch = override; |
| 493 | #endif |
| 494 | return 1; |
| 495 | } |
| 496 | |
| 497 | |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 498 | PUBLIC void |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 499 | _glapi_end_dispatch_override(int layer) |
| 500 | { |
| 501 | struct _glapi_table *real = _glapi_get_dispatch(); |
| 502 | (void) layer; |
| 503 | DispatchOverride = GL_FALSE; |
| 504 | _glapi_set_dispatch(real); |
| 505 | /* the rest of this isn't needed, just play it safe */ |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 506 | #if defined(GLX_USE_TLS) |
| 507 | _glapi_tls_RealDispatch = NULL; |
| 508 | #else |
| 509 | # if defined(THREADS) |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 510 | _glthread_SetTSD(&RealDispatchTSD, NULL); |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 511 | # endif |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 512 | _glapi_RealDispatch = NULL; |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 513 | #endif |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 517 | PUBLIC struct _glapi_table * |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 518 | _glapi_get_override_dispatch(int layer) |
| 519 | { |
| 520 | if (layer == 0) { |
| 521 | return _glapi_get_dispatch(); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 522 | } |
| 523 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 524 | if (DispatchOverride) { |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 525 | #if defined(GLX_USE_TLS) |
| 526 | return (struct _glapi_table *) _glapi_tls_Dispatch; |
| 527 | #elif defined(THREADS) |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 528 | return (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 529 | #else |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 530 | return _glapi_Dispatch; |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 531 | #endif |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 532 | } |
| 533 | else { |
| 534 | return NULL; |
| 535 | } |
| 536 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 537 | } |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 538 | |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 539 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 540 | #if !defined( USE_X86_ASM ) |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 541 | #define NEED_FUNCTION_POINTER |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 542 | #endif |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 543 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 544 | /* The code in this file is auto-generated with Python */ |
Brian Paul | b5fd886 | 2001-11-18 22:48:11 +0000 | [diff] [blame] | 545 | #include "glprocs.h" |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 546 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 547 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 548 | /** |
| 549 | * Search the table of static entrypoint functions for the named function |
| 550 | * and return the corresponding glprocs_table_t entry. |
| 551 | */ |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 552 | static const glprocs_table_t * |
| 553 | find_entry( const char * n ) |
| 554 | { |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 555 | GLuint i; |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 556 | |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 557 | for (i = 0; static_functions[i].Name_offset >= 0; i++) { |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 558 | const char * test_name; |
| 559 | |
| 560 | test_name = gl_string_table + static_functions[i].Name_offset; |
| 561 | if (strcmp(test_name, n) == 0) { |
| 562 | return & static_functions[i]; |
| 563 | } |
| 564 | } |
| 565 | return NULL; |
| 566 | } |
| 567 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 568 | |
| 569 | /** |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 570 | * Return dispatch table offset of the named static (built-in) function. |
| 571 | * Return -1 if function not found. |
| 572 | */ |
| 573 | static GLint |
| 574 | get_static_proc_offset(const char *funcName) |
| 575 | { |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 576 | const glprocs_table_t * const f = find_entry( funcName ); |
| 577 | |
| 578 | if ( f != NULL ) { |
| 579 | return f->Offset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 580 | } |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 585 | #ifdef USE_X86_ASM |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 586 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 587 | #if defined( GLX_USE_TLS ) |
| 588 | extern GLubyte gl_dispatch_functions_start[]; |
| 589 | extern GLubyte gl_dispatch_functions_end[]; |
| 590 | #else |
| 591 | extern const GLubyte gl_dispatch_functions_start[]; |
| 592 | #endif |
| 593 | |
| 594 | # if defined(THREADS) && !defined(GLX_USE_TLS) |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 595 | # define X86_DISPATCH_FUNCTION_SIZE 32 |
| 596 | # else |
| 597 | # define X86_DISPATCH_FUNCTION_SIZE 16 |
| 598 | # endif |
| 599 | |
| 600 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 601 | /** |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 602 | * Return dispatch function address the named static (built-in) function. |
| 603 | * Return NULL if function not found. |
| 604 | */ |
Brian Paul | 209bd3a | 2004-11-27 04:02:32 +0000 | [diff] [blame] | 605 | static const _glapi_proc |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 606 | get_static_proc_address(const char *funcName) |
| 607 | { |
| 608 | const glprocs_table_t * const f = find_entry( funcName ); |
| 609 | |
| 610 | if ( f != NULL ) { |
Brian Paul | 209bd3a | 2004-11-27 04:02:32 +0000 | [diff] [blame] | 611 | return (_glapi_proc) (gl_dispatch_functions_start |
| 612 | + (X86_DISPATCH_FUNCTION_SIZE * f->Offset)); |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 613 | } |
| 614 | else { |
| 615 | return NULL; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | #else |
| 620 | |
| 621 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 622 | /** |
| 623 | * Return pointer to the named static (built-in) function. |
| 624 | * \return NULL if function not found. |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 625 | */ |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 626 | static const _glapi_proc |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 627 | get_static_proc_address(const char *funcName) |
| 628 | { |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 629 | const glprocs_table_t * const f = find_entry( funcName ); |
| 630 | return ( f != NULL ) ? f->Address : NULL; |
| 631 | } |
| 632 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 633 | #endif /* USE_X86_ASM */ |
| 634 | |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 635 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 636 | /** |
| 637 | * Return the name of the function at the given offset in the dispatch |
| 638 | * table. For debugging only. |
| 639 | */ |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 640 | static const char * |
| 641 | get_static_proc_name( GLuint offset ) |
| 642 | { |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 643 | GLuint i; |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 644 | |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 645 | for (i = 0; static_functions[i].Name_offset >= 0; i++) { |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 646 | if (static_functions[i].Offset == offset) { |
| 647 | return gl_string_table + static_functions[i].Name_offset; |
Brian Paul | 9c7ca85 | 2000-10-19 20:13:12 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | return NULL; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
| 654 | |
| 655 | /********************************************************************** |
| 656 | * Extension function management. |
| 657 | */ |
| 658 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 659 | /* |
| 660 | * Number of extension functions which we can dynamically add at runtime. |
| 661 | */ |
| 662 | #define MAX_EXTENSION_FUNCS 300 |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 663 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 664 | |
| 665 | /* |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 666 | * The dispatch table size (number of entries) is the size of the |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 667 | * _glapi_table struct plus the number of dynamic entries we can add. |
| 668 | * The extra slots can be filled in by DRI drivers that register new extension |
| 669 | * functions. |
| 670 | */ |
| 671 | #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS) |
| 672 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 673 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 674 | /** |
| 675 | * Track information about a function added to the GL API. |
| 676 | */ |
| 677 | struct _glapi_function { |
| 678 | /** |
| 679 | * Name of the function. |
| 680 | */ |
| 681 | const char * name; |
| 682 | |
| 683 | |
| 684 | /** |
| 685 | * Text string that describes the types of the parameters passed to the |
| 686 | * named function. Parameter types are converted to characters using the |
| 687 | * following rules: |
| 688 | * - 'i' for \c GLint, \c GLuint, and \c GLenum |
| 689 | * - 'p' for any pointer type |
| 690 | * - 'f' for \c GLfloat and \c GLclampf |
| 691 | * - 'd' for \c GLdouble and \c GLclampd |
| 692 | */ |
| 693 | const char * parameter_signature; |
| 694 | |
| 695 | |
| 696 | /** |
| 697 | * Offset in the dispatch table where the pointer to the real function is |
| 698 | * located. If the driver has not requested that the named function be |
| 699 | * added to the dispatch table, this will have the value ~0. |
| 700 | */ |
| 701 | unsigned dispatch_offset; |
| 702 | |
| 703 | |
| 704 | /** |
| 705 | * Pointer to the dispatch stub for the named function. |
| 706 | * |
| 707 | * \todo |
| 708 | * The semantic of this field should be changed slightly. Currently, it |
| 709 | * is always expected to be non-\c NULL. However, it would be better to |
| 710 | * only allocate the entry-point stub when the application requests the |
| 711 | * function via \c glXGetProcAddress. This would save memory for all the |
| 712 | * functions that the driver exports but that the application never wants |
| 713 | * to call. |
| 714 | */ |
| 715 | _glapi_proc dispatch_stub; |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
| 718 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 719 | static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS]; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 720 | static GLuint NumExtEntryPoints = 0; |
| 721 | |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 722 | #ifdef USE_SPARC_ASM |
| 723 | extern void __glapi_sparc_icache_flush(unsigned int *); |
| 724 | #endif |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 725 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 726 | /** |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 727 | * Generate a dispatch function (entrypoint) which jumps through |
| 728 | * the given slot number (offset) in the current dispatch table. |
| 729 | * We need assembly language in order to accomplish this. |
| 730 | */ |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 731 | static _glapi_proc |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 732 | generate_entrypoint(GLuint functionOffset) |
| 733 | { |
| 734 | #if defined(USE_X86_ASM) |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 735 | /* 32 is chosen as something of a magic offset. For x86, the dispatch |
| 736 | * at offset 32 is the first one where the offset in the |
| 737 | * "jmp OFFSET*4(%eax)" can't be encoded in a single byte. |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 738 | */ |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 739 | const GLubyte * const template_func = gl_dispatch_functions_start |
| 740 | + (X86_DISPATCH_FUNCTION_SIZE * 32); |
| 741 | GLubyte * const code = (GLubyte *) malloc( X86_DISPATCH_FUNCTION_SIZE ); |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 742 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 743 | |
| 744 | if ( code != NULL ) { |
| 745 | (void) memcpy( code, template_func, X86_DISPATCH_FUNCTION_SIZE ); |
| 746 | fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset ); |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 747 | } |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 748 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 749 | return (_glapi_proc) code; |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 750 | #elif defined(USE_SPARC_ASM) |
| 751 | |
Ian Romanick | 9f23a3a | 2005-07-28 00:11:10 +0000 | [diff] [blame] | 752 | #ifdef __arch64__ |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 753 | static const unsigned int insn_template[] = { |
| 754 | 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */ |
| 755 | 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */ |
| 756 | 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */ |
| 757 | 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */ |
| 758 | 0x8528b020, /* sllx %g2, 32, %g2 */ |
| 759 | 0xc2584002, /* ldx [%g1 + %g2], %g1 */ |
| 760 | 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */ |
| 761 | 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */ |
| 762 | 0xc6584002, /* ldx [%g1 + %g2], %g3 */ |
| 763 | 0x81c0c000, /* jmpl %g3, %g0 */ |
| 764 | 0x01000000 /* nop */ |
| 765 | }; |
| 766 | #else |
| 767 | static const unsigned int insn_template[] = { |
| 768 | 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */ |
| 769 | 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */ |
| 770 | 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */ |
| 771 | 0x81c0c000, /* jmpl %g3, %g0 */ |
| 772 | 0x01000000 /* nop */ |
| 773 | }; |
| 774 | #endif |
Brian Paul | dec2a4d | 2002-10-29 15:03:14 +0000 | [diff] [blame] | 775 | unsigned int *code = (unsigned int *) malloc(sizeof(insn_template)); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 776 | unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch; |
| 777 | if (code) { |
| 778 | memcpy(code, insn_template, sizeof(insn_template)); |
| 779 | |
Ian Romanick | 9f23a3a | 2005-07-28 00:11:10 +0000 | [diff] [blame] | 780 | #ifdef __arch64__ |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 781 | code[0] |= (glapi_addr >> (32 + 10)); |
| 782 | code[1] |= ((glapi_addr & 0xffffffff) >> 10); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 783 | __glapi_sparc_icache_flush(&code[0]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 784 | code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1)); |
| 785 | code[3] |= (glapi_addr & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 786 | __glapi_sparc_icache_flush(&code[2]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 787 | code[6] |= ((functionOffset * 8) >> 10); |
| 788 | code[7] |= ((functionOffset * 8) & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 789 | __glapi_sparc_icache_flush(&code[6]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 790 | #else |
| 791 | code[0] |= (glapi_addr >> 10); |
| 792 | code[1] |= (glapi_addr & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 793 | __glapi_sparc_icache_flush(&code[0]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 794 | code[2] |= (functionOffset * 4); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 795 | __glapi_sparc_icache_flush(&code[2]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 796 | #endif |
| 797 | } |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 798 | return (_glapi_proc) code; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 799 | #else |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 800 | (void) functionOffset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 801 | return NULL; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 802 | #endif /* USE_*_ASM */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 806 | /** |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 807 | * This function inserts a new dispatch offset into the assembly language |
| 808 | * stub that was generated with the preceeding function. |
| 809 | */ |
| 810 | static void |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 811 | fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset) |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 812 | { |
| 813 | #if defined(USE_X86_ASM) |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 814 | GLubyte * const code = (GLubyte *) entrypoint; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 815 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 816 | |
| 817 | #if X86_DISPATCH_FUNCTION_SIZE == 32 |
| 818 | *((unsigned int *)(code + 11)) = 4 * offset; |
| 819 | *((unsigned int *)(code + 22)) = 4 * offset; |
| 820 | #elif X86_DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS ) |
| 821 | *((unsigned int *)(code + 8)) = 4 * offset; |
| 822 | #elif X86_DISPATCH_FUNCTION_SIZE == 16 |
| 823 | *((unsigned int *)(code + 7)) = 4 * offset; |
| 824 | #else |
| 825 | # error Invalid X86_DISPATCH_FUNCTION_SIZE! |
| 826 | #endif |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 827 | |
| 828 | #elif defined(USE_SPARC_ASM) |
| 829 | |
| 830 | /* XXX this hasn't been tested! */ |
| 831 | unsigned int *code = (unsigned int *) entrypoint; |
Ian Romanick | 9f23a3a | 2005-07-28 00:11:10 +0000 | [diff] [blame] | 832 | #ifdef __arch64__ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 833 | code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */ |
| 834 | code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */ |
| 835 | code[6] |= ((offset * 8) >> 10); |
| 836 | code[7] |= ((offset * 8) & ((1 << 10) - 1)); |
| 837 | __glapi_sparc_icache_flush(&code[6]); |
Ian Romanick | 9f23a3a | 2005-07-28 00:11:10 +0000 | [diff] [blame] | 838 | #else /* __arch64__ */ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 839 | code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */ |
Brian Paul | 944ea20 | 2002-10-17 16:29:17 +0000 | [diff] [blame] | 840 | code[2] |= (offset * 4); |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 841 | __glapi_sparc_icache_flush(&code[2]); |
Ian Romanick | 9f23a3a | 2005-07-28 00:11:10 +0000 | [diff] [blame] | 842 | #endif /* __arch64__ */ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 843 | |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 844 | #else |
| 845 | |
| 846 | /* an unimplemented architecture */ |
| 847 | (void) entrypoint; |
| 848 | (void) offset; |
| 849 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 850 | #endif /* USE_*_ASM */ |
| 851 | } |
| 852 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 853 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 854 | /** |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 855 | * Generate new entrypoint |
| 856 | * |
| 857 | * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver |
| 858 | * calls \c _glapi_add_dispatch we'll put in the proper offset. If that |
| 859 | * never happens, and the user calls this function, he'll segfault. That's |
| 860 | * what you get when you try calling a GL function that doesn't really exist. |
| 861 | * |
| 862 | * \param funcName Name of the function to create an entry-point for. |
| 863 | * |
| 864 | * \sa _glapi_add_entrypoint |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 865 | */ |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 866 | |
| 867 | static struct _glapi_function * |
| 868 | add_function_name( const char * funcName ) |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 869 | { |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 870 | struct _glapi_function * entry = NULL; |
| 871 | |
| 872 | if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) { |
| 873 | _glapi_proc entrypoint = generate_entrypoint(~0); |
| 874 | if (entrypoint != NULL) { |
| 875 | entry = & ExtEntryTable[NumExtEntryPoints]; |
| 876 | |
| 877 | ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName); |
| 878 | ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL; |
| 879 | ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0; |
| 880 | ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint; |
| 881 | NumExtEntryPoints++; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return entry; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | /** |
| 890 | * Fill-in the dispatch stub for the named function. |
| 891 | * |
| 892 | * This function is intended to be called by a hardware driver. When called, |
| 893 | * a dispatch stub may be created created for the function. A pointer to this |
| 894 | * dispatch function will be returned by glXGetProcAddress. |
| 895 | * |
| 896 | * \param function_names Array of pointers to function names that should |
| 897 | * share a common dispatch offset. |
| 898 | * \param parameter_signature String representing the types of the parameters |
| 899 | * passed to the named function. Parameter types |
| 900 | * are converted to characters using the following |
| 901 | * rules: |
| 902 | * - 'i' for \c GLint, \c GLuint, and \c GLenum |
| 903 | * - 'p' for any pointer type |
| 904 | * - 'f' for \c GLfloat and \c GLclampf |
| 905 | * - 'd' for \c GLdouble and \c GLclampd |
| 906 | * |
| 907 | * \returns |
| 908 | * The offset in the dispatch table of the named function. A pointer to the |
| 909 | * driver's implementation of the named function should be stored at |
| 910 | * \c dispatch_table[\c offset]. |
| 911 | * |
| 912 | * \sa glXGetProcAddress |
| 913 | * |
| 914 | * \warning |
| 915 | * This function can only handle up to 8 names at a time. As far as I know, |
| 916 | * the maximum number of names ever associated with an existing GL function is |
| 917 | * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT, |
| 918 | * \c glPointParameterfARB, and \c glPointParameterf), so this should not be |
| 919 | * too painful of a limitation. |
| 920 | * |
| 921 | * \todo |
| 922 | * Determine whether or not \c parameter_signature should be allowed to be |
| 923 | * \c NULL. It doesn't seem like much of a hardship for drivers to have to |
| 924 | * pass in an empty string. |
| 925 | * |
| 926 | * \todo |
| 927 | * Determine if code should be added to reject function names that start with |
| 928 | * 'glX'. |
| 929 | * |
| 930 | * \bug |
| 931 | * Add code to compare \c parameter_signature with the parameter signature of |
| 932 | * a static function. In order to do that, we need to find a way to \b get |
| 933 | * the parameter signature of a static function. |
| 934 | */ |
| 935 | |
| 936 | PUBLIC int |
| 937 | _glapi_add_dispatch( const char * const * function_names, |
| 938 | const char * parameter_signature ) |
| 939 | { |
| 940 | static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC; |
| 941 | const char * const real_sig = (parameter_signature != NULL) |
| 942 | ? parameter_signature : ""; |
| 943 | struct _glapi_function * entry[8]; |
| 944 | GLboolean is_static[8]; |
| 945 | unsigned i; |
| 946 | unsigned j; |
| 947 | int offset = ~0; |
| 948 | int new_offset; |
| 949 | |
| 950 | |
| 951 | (void) memset( is_static, 0, sizeof( is_static ) ); |
| 952 | (void) memset( entry, 0, sizeof( entry ) ); |
| 953 | |
| 954 | for ( i = 0 ; function_names[i] != NULL ; i++ ) { |
| 955 | /* Do some trivial validation on the name of the function. |
| 956 | */ |
| 957 | |
Brian Paul | f7b4e0d | 2004-04-23 20:33:07 +0000 | [diff] [blame] | 958 | #ifdef MANGLE |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 959 | if (!function_names[i] || function_names[i][0] != 'm' || function_names[i][1] != 'g' || function_names[i][2] != 'l') |
| 960 | return GL_FALSE; |
Brian Paul | f7b4e0d | 2004-04-23 20:33:07 +0000 | [diff] [blame] | 961 | #else |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 962 | if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l') |
| 963 | return GL_FALSE; |
Brian Paul | f7b4e0d | 2004-04-23 20:33:07 +0000 | [diff] [blame] | 964 | #endif |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 965 | |
| 966 | |
| 967 | /* Determine if the named function already exists. If the function does |
| 968 | * exist, it must have the same parameter signature as the function |
| 969 | * being added. |
| 970 | */ |
Brian Paul | 8ad1076 | 2002-10-11 17:41:03 +0000 | [diff] [blame] | 971 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 972 | new_offset = get_static_proc_offset(function_names[i]); |
| 973 | if (new_offset >= 0) { |
| 974 | /* FIXME: Make sure the parameter signatures match! How do we get |
| 975 | * FIXME: the parameter signature for static functions? |
| 976 | */ |
| 977 | |
| 978 | if ( (offset != ~0) && (new_offset != offset) ) { |
| 979 | return -1; |
| 980 | } |
| 981 | |
| 982 | is_static[i] = GL_TRUE; |
| 983 | offset = new_offset; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | for ( j = 0 ; j < NumExtEntryPoints ; j++ ) { |
| 988 | if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) { |
| 989 | /* The offset may be ~0 if the function name was added by |
| 990 | * glXGetProcAddress but never filled in by the driver. |
| 991 | */ |
| 992 | |
| 993 | if (ExtEntryTable[j].dispatch_offset != ~0) { |
| 994 | if (strcmp(real_sig, ExtEntryTable[j].parameter_signature) |
| 995 | != 0) { |
| 996 | return -1; |
| 997 | } |
| 998 | |
| 999 | if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) { |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
| 1003 | offset = ExtEntryTable[j].dispatch_offset; |
| 1004 | } |
| 1005 | |
| 1006 | entry[i] = & ExtEntryTable[j]; |
| 1007 | break; |
| 1008 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1012 | |
| 1013 | if (offset == ~0) { |
| 1014 | offset = next_dynamic_offset; |
| 1015 | next_dynamic_offset++; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | for ( i = 0 ; function_names[i] != NULL ; i++ ) { |
| 1020 | if (! is_static[i] ) { |
| 1021 | if (entry[i] == NULL) { |
| 1022 | entry[i] = add_function_name( function_names[i] ); |
| 1023 | if (entry[i] == NULL) { |
| 1024 | /* FIXME: Possible memory leak here. |
| 1025 | */ |
| 1026 | return -1; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | entry[i]->parameter_signature = str_dup(real_sig); |
| 1032 | fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset); |
| 1033 | entry[i]->dispatch_offset = offset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1034 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1035 | } |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1036 | |
| 1037 | return offset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1041 | /** |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1042 | * Return offset of entrypoint for named function within dispatch table. |
| 1043 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 1044 | PUBLIC GLint |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1045 | _glapi_get_proc_offset(const char *funcName) |
| 1046 | { |
| 1047 | /* search extension functions first */ |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 1048 | GLuint i; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1049 | for (i = 0; i < NumExtEntryPoints; i++) { |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1050 | if (strcmp(ExtEntryTable[i].name, funcName) == 0) { |
| 1051 | return ExtEntryTable[i].dispatch_offset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | /* search static functions */ |
| 1056 | return get_static_proc_offset(funcName); |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1061 | /** |
| 1062 | * Return pointer to the named function. If the function name isn't found |
| 1063 | * in the name of static functions, try generating a new API entrypoint on |
| 1064 | * the fly with assembly language. |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1065 | */ |
Keith Whitwell | 5cef1a1 | 2005-04-28 13:16:23 +0000 | [diff] [blame] | 1066 | PUBLIC _glapi_proc |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1067 | _glapi_get_proc_address(const char *funcName) |
| 1068 | { |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1069 | struct _glapi_function * entry; |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 1070 | GLuint i; |
Brian Paul | a6ed6f4 | 2003-08-27 14:48:16 +0000 | [diff] [blame] | 1071 | |
Brian Paul | f7b4e0d | 2004-04-23 20:33:07 +0000 | [diff] [blame] | 1072 | #ifdef MANGLE |
| 1073 | if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l') |
| 1074 | return NULL; |
| 1075 | #else |
Brian Paul | a6ed6f4 | 2003-08-27 14:48:16 +0000 | [diff] [blame] | 1076 | if (funcName[0] != 'g' || funcName[1] != 'l') |
| 1077 | return NULL; |
Brian Paul | f7b4e0d | 2004-04-23 20:33:07 +0000 | [diff] [blame] | 1078 | #endif |
Brian Paul | a6ed6f4 | 2003-08-27 14:48:16 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* search extension functions first */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1081 | for (i = 0; i < NumExtEntryPoints; i++) { |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1082 | if (strcmp(ExtEntryTable[i].name, funcName) == 0) { |
| 1083 | return ExtEntryTable[i].dispatch_stub; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* search static functions */ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1088 | { |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1089 | const _glapi_proc func = get_static_proc_address(funcName); |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1090 | if (func) |
| 1091 | return func; |
| 1092 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1093 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1094 | entry = add_function_name(funcName); |
| 1095 | return (entry == NULL) ? NULL : entry->dispatch_stub; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1096 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1097 | |
| 1098 | |
| 1099 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1100 | /** |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1101 | * Return the name of the function at the given dispatch offset. |
| 1102 | * This is only intended for debugging. |
| 1103 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 1104 | PUBLIC const char * |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1105 | _glapi_get_proc_name(GLuint offset) |
| 1106 | { |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1107 | GLuint i; |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 1108 | const char * n; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1109 | |
| 1110 | /* search built-in functions */ |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 1111 | n = get_static_proc_name(offset); |
| 1112 | if ( n != NULL ) { |
| 1113 | return n; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /* search added extension functions */ |
| 1117 | for (i = 0; i < NumExtEntryPoints; i++) { |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame^] | 1118 | if (ExtEntryTable[i].dispatch_offset == offset) { |
| 1119 | return ExtEntryTable[i].name; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1127 | /** |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1128 | * Return size of dispatch table struct as number of functions (or |
| 1129 | * slots). |
| 1130 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 1131 | PUBLIC GLuint |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1132 | _glapi_get_dispatch_table_size(void) |
| 1133 | { |
| 1134 | return DISPATCH_TABLE_SIZE; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1139 | /** |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1140 | * Get API dispatcher version string. |
| 1141 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 1142 | PUBLIC const char * |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1143 | _glapi_get_version(void) |
| 1144 | { |
| 1145 | return "20021001"; /* YYYYMMDD */ |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1150 | /** |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1151 | * Make sure there are no NULL pointers in the given dispatch table. |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 1152 | * Intended for debugging purposes. |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1153 | */ |
Adam Jackson | 791ce02 | 2004-12-15 23:14:29 +0000 | [diff] [blame] | 1154 | PUBLIC void |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1155 | _glapi_check_table(const struct _glapi_table *table) |
| 1156 | { |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 1157 | #ifdef DEBUG |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1158 | const GLuint entries = _glapi_get_dispatch_table_size(); |
| 1159 | const void **tab = (const void **) table; |
| 1160 | GLuint i; |
| 1161 | for (i = 1; i < entries; i++) { |
| 1162 | assert(tab[i]); |
| 1163 | } |
| 1164 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1165 | /* Do some spot checks to be sure that the dispatch table |
| 1166 | * slots are assigned correctly. |
| 1167 | */ |
| 1168 | { |
| 1169 | GLuint BeginOffset = _glapi_get_proc_offset("glBegin"); |
| 1170 | char *BeginFunc = (char*) &table->Begin; |
| 1171 | GLuint offset = (BeginFunc - (char *) table) / sizeof(void *); |
| 1172 | assert(BeginOffset == _gloffset_Begin); |
| 1173 | assert(BeginOffset == offset); |
| 1174 | } |
| 1175 | { |
| 1176 | GLuint viewportOffset = _glapi_get_proc_offset("glViewport"); |
| 1177 | char *viewportFunc = (char*) &table->Viewport; |
| 1178 | GLuint offset = (viewportFunc - (char *) table) / sizeof(void *); |
| 1179 | assert(viewportOffset == _gloffset_Viewport); |
| 1180 | assert(viewportOffset == offset); |
| 1181 | } |
| 1182 | { |
| 1183 | GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer"); |
| 1184 | char *VertexPointerFunc = (char*) &table->VertexPointer; |
| 1185 | GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *); |
| 1186 | assert(VertexPointerOffset == _gloffset_VertexPointer); |
| 1187 | assert(VertexPointerOffset == offset); |
| 1188 | } |
| 1189 | { |
| 1190 | GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax"); |
| 1191 | char *ResetMinMaxFunc = (char*) &table->ResetMinmax; |
| 1192 | GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *); |
| 1193 | assert(ResetMinMaxOffset == _gloffset_ResetMinmax); |
| 1194 | assert(ResetMinMaxOffset == offset); |
| 1195 | } |
| 1196 | { |
| 1197 | GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor"); |
| 1198 | char *blendColorFunc = (char*) &table->BlendColor; |
| 1199 | GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *); |
| 1200 | assert(blendColorOffset == _gloffset_BlendColor); |
| 1201 | assert(blendColorOffset == offset); |
| 1202 | } |
| 1203 | { |
| 1204 | GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT"); |
| 1205 | char *istextureFunc = (char*) &table->IsTextureEXT; |
| 1206 | GLuint offset = (istextureFunc - (char *) table) / sizeof(void *); |
| 1207 | assert(istextureOffset == _gloffset_IsTextureEXT); |
| 1208 | assert(istextureOffset == offset); |
| 1209 | } |
Brian Paul | a14cbff | 2000-10-27 18:31:21 +0000 | [diff] [blame] | 1210 | { |
| 1211 | GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT"); |
| 1212 | char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT; |
| 1213 | GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *); |
| 1214 | assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT); |
| 1215 | assert(secondaryColor3fOffset == offset); |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 1216 | assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT); |
Brian Paul | a14cbff | 2000-10-27 18:31:21 +0000 | [diff] [blame] | 1217 | } |
Brian Paul | 91d6f12 | 2002-05-29 15:23:16 +0000 | [diff] [blame] | 1218 | { |
| 1219 | GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV"); |
| 1220 | char *pointParameterivFunc = (char*) &table->PointParameterivNV; |
| 1221 | GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *); |
| 1222 | assert(pointParameterivOffset == _gloffset_PointParameterivNV); |
| 1223 | assert(pointParameterivOffset == offset); |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 1224 | assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV); |
Brian Paul | 91d6f12 | 2002-05-29 15:23:16 +0000 | [diff] [blame] | 1225 | } |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1226 | { |
| 1227 | GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV"); |
| 1228 | char *setFenceFunc = (char*) &table->SetFenceNV; |
| 1229 | GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *); |
| 1230 | assert(setFenceOffset == _gloffset_SetFenceNV); |
| 1231 | assert(setFenceOffset == offset); |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 1232 | assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc) &glSetFenceNV); |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 1233 | } |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 1234 | #else |
| 1235 | (void) table; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1236 | #endif |
| 1237 | } |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 1238 | |
| 1239 | |
| 1240 | /** |
| 1241 | * Perform platform-specific GL API entry-point fixups. |
| 1242 | * |
| 1243 | * |
| 1244 | */ |
| 1245 | static void |
| 1246 | init_glapi_relocs( void ) |
| 1247 | { |
| 1248 | #if defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) |
| 1249 | extern void * _x86_get_dispatch(void); |
| 1250 | const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch; |
| 1251 | GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start; |
| 1252 | |
| 1253 | |
| 1254 | while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) { |
| 1255 | (void) memcpy( curr_func, get_disp, 6 ); |
| 1256 | curr_func += X86_DISPATCH_FUNCTION_SIZE; |
| 1257 | } |
| 1258 | #endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */ |
| 1259 | } |