Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Mesa 3-D graphics library |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 4 | * Version: 4.1 |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 5 | * |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2002 Brian Paul All Rights Reserved. |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included |
| 16 | * in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 22 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | |
Brian Paul | 7e1161b | 1999-11-25 18:17:04 +0000 | [diff] [blame] | 27 | /* |
| 28 | * This file manages the OpenGL API dispatch layer. |
| 29 | * The dispatch table (struct _glapi_table) is basically just a list |
| 30 | * of function pointers. |
| 31 | * There are functions to set/get the current dispatch table for the |
| 32 | * current thread and to manage registration/dispatch of dynamically |
| 33 | * added extension functions. |
Brian Paul | 9f94399 | 2000-01-28 19:03:33 +0000 | [diff] [blame] | 34 | * |
| 35 | * It's intended that this file and the other glapi*.[ch] files are |
| 36 | * flexible enough to be reused in several places: XFree86, DRI- |
| 37 | * based libGL.so, and perhaps the SGI SI. |
| 38 | * |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 39 | * NOTE: There are no dependencies on Mesa in this code. |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 40 | * |
| 41 | * Versions (API changes): |
| 42 | * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0 |
| 43 | * 2001/01/16 - added dispatch override feature for Mesa 3.5 |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 44 | * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1. |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 45 | * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints |
| 46 | * itself (using offset ~0). _glapi_add_entrypoint() can be |
| 47 | * called afterward and it'll fill in the correct dispatch |
| 48 | * offset. This allows DRI libGL to avoid probing for DRI |
| 49 | * drivers! No changes to the public glapi interface. |
Brian Paul | 7e1161b | 1999-11-25 18:17:04 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | |
| 53 | |
Brian Paul | 3c27be3 | 2000-02-10 21:27:48 +0000 | [diff] [blame] | 54 | #include "glheader.h" |
Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 55 | #include "glapi.h" |
Brian Paul | 0c239fc | 1999-12-16 12:38:11 +0000 | [diff] [blame] | 56 | #include "glapioffsets.h" |
| 57 | #include "glapitable.h" |
Brian Paul | 9f94399 | 2000-01-28 19:03:33 +0000 | [diff] [blame] | 58 | #include "glthread.h" |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 59 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 60 | /***** BEGIN NO-OP DISPATCH *****/ |
| 61 | |
| 62 | static GLboolean WarnFlag = GL_FALSE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 63 | static _glapi_warning_func warning_func; |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 64 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Enable/disable printing of warning messages. |
| 68 | */ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 69 | void |
| 70 | _glapi_noop_enable_warnings(GLboolean enable) |
| 71 | { |
| 72 | WarnFlag = enable; |
| 73 | } |
| 74 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Register a callback function for reporting errors. |
| 77 | */ |
| 78 | void |
| 79 | _glapi_set_warning_func( _glapi_warning_func func ) |
| 80 | { |
| 81 | warning_func = func; |
| 82 | } |
| 83 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 84 | static GLboolean |
| 85 | warn(void) |
| 86 | { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 87 | if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) |
| 88 | && warning_func) { |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 89 | return GL_TRUE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 90 | } |
| 91 | else { |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 92 | return GL_FALSE; |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 93 | } |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | #define KEYWORD1 static |
| 98 | #define KEYWORD2 |
| 99 | #define NAME(func) NoOp##func |
| 100 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 101 | #define F NULL |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 102 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 103 | #define DISPATCH(func, args, msg) \ |
| 104 | if (warn()) { \ |
| 105 | warning_func(NULL, "GL User Error: called without context:"); \ |
| 106 | warning_func msg; \ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 109 | #define RETURN_DISPATCH(func, args, msg) \ |
| 110 | if (warn()) { \ |
| 111 | warning_func(NULL, "GL User Error: called without context:"); \ |
| 112 | warning_func msg; \ |
| 113 | } \ |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 114 | return 0 |
| 115 | |
| 116 | #define DISPATCH_TABLE_NAME __glapi_noop_table |
| 117 | #define UNUSED_TABLE_NAME __usused_noop_functions |
| 118 | |
| 119 | #define TABLE_ENTRY(name) (void *) NoOp##name |
| 120 | |
| 121 | static int NoOpUnused(void) |
| 122 | { |
| 123 | if (warn()) { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | #include "glapitemp.h" |
| 130 | |
| 131 | /***** END NO-OP DISPATCH *****/ |
| 132 | |
| 133 | |
| 134 | |
| 135 | /***** BEGIN THREAD-SAFE DISPATCH *****/ |
| 136 | /* if we support thread-safety, build a special dispatch table for use |
| 137 | * in thread-safety mode (ThreadSafe == GL_TRUE). Each entry in the |
| 138 | * dispatch table will call _glthread_GetTSD() to get the actual dispatch |
| 139 | * table bound to the current thread, then jump through that table. |
| 140 | */ |
| 141 | |
| 142 | #if defined(THREADS) |
| 143 | |
| 144 | static GLboolean ThreadSafe = GL_FALSE; /* In thread-safe mode? */ |
| 145 | static _glthread_TSD DispatchTSD; /* Per-thread dispatch pointer */ |
| 146 | static _glthread_TSD RealDispatchTSD; /* only when using override */ |
| 147 | static _glthread_TSD ContextTSD; /* Per-thread context pointer */ |
| 148 | |
| 149 | |
| 150 | #define KEYWORD1 static |
| 151 | #define KEYWORD2 GLAPIENTRY |
| 152 | #define NAME(func) _ts_##func |
| 153 | |
| 154 | #define DISPATCH(FUNC, ARGS, MESSAGE) \ |
| 155 | struct _glapi_table *dispatch; \ |
| 156 | dispatch = (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); \ |
| 157 | if (!dispatch) \ |
| 158 | dispatch = (struct _glapi_table *) __glapi_noop_table; \ |
| 159 | (dispatch->FUNC) ARGS |
| 160 | |
| 161 | #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \ |
| 162 | struct _glapi_table *dispatch; \ |
| 163 | dispatch = (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); \ |
| 164 | if (!dispatch) \ |
| 165 | dispatch = (struct _glapi_table *) __glapi_noop_table; \ |
| 166 | return (dispatch->FUNC) ARGS |
| 167 | |
| 168 | #define DISPATCH_TABLE_NAME __glapi_threadsafe_table |
| 169 | #define UNUSED_TABLE_NAME __usused_threadsafe_functions |
| 170 | |
| 171 | #define TABLE_ENTRY(name) (void *) _ts_##name |
| 172 | |
| 173 | static int _ts_Unused(void) |
| 174 | { |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | #include "glapitemp.h" |
| 179 | |
| 180 | #endif |
| 181 | |
| 182 | /***** END THREAD-SAFE DISPATCH *****/ |
| 183 | |
| 184 | |
| 185 | |
Brian Paul | 8ceb5c3 | 2000-02-24 22:14:04 +0000 | [diff] [blame] | 186 | struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table; |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 187 | struct _glapi_table *_glapi_RealDispatch = (struct _glapi_table *) __glapi_noop_table; |
Brian Paul | 0f71025 | 1999-12-15 15:02:30 +0000 | [diff] [blame] | 188 | |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 189 | /* Used when thread safety disabled */ |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 190 | void *_glapi_Context = NULL; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 191 | |
Brian Paul | 0f71025 | 1999-12-15 15:02:30 +0000 | [diff] [blame] | 192 | |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 193 | static GLboolean DispatchOverride = GL_FALSE; |
| 194 | |
| 195 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 196 | |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 197 | /* strdup() is actually not a standard ANSI C or POSIX routine. |
| 198 | * Irix will not define it if ANSI mode is in effect. |
| 199 | */ |
| 200 | static char * |
| 201 | str_dup(const char *str) |
Randy Frank | d7361e1 | 2000-03-27 21:13:58 +0000 | [diff] [blame] | 202 | { |
Brian Paul | fffb809 | 2000-03-29 18:46:11 +0000 | [diff] [blame] | 203 | char *copy; |
| 204 | copy = (char*) malloc(strlen(str) + 1); |
| 205 | if (!copy) |
| 206 | return NULL; |
| 207 | strcpy(copy, str); |
| 208 | return copy; |
Randy Frank | d7361e1 | 2000-03-27 21:13:58 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 211 | |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * We should call this periodically from a function such as glXMakeCurrent |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 215 | * in order to test if multiple threads are being used. |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 216 | */ |
| 217 | void |
| 218 | _glapi_check_multithread(void) |
| 219 | { |
| 220 | #if defined(THREADS) |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 221 | if (!ThreadSafe) { |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 222 | static unsigned long knownID; |
| 223 | static GLboolean firstCall = GL_TRUE; |
| 224 | if (firstCall) { |
| 225 | knownID = _glthread_GetID(); |
| 226 | firstCall = GL_FALSE; |
| 227 | } |
| 228 | else if (knownID != _glthread_GetID()) { |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 229 | ThreadSafe = GL_TRUE; |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 232 | if (ThreadSafe) { |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 233 | /* make sure that this thread's dispatch pointer isn't null */ |
| 234 | if (!_glapi_get_dispatch()) { |
| 235 | _glapi_set_dispatch(NULL); |
| 236 | } |
| 237 | } |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | /* |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 244 | * Set the current context pointer for this thread. |
| 245 | * The context pointer is an opaque type which should be cast to |
| 246 | * void from the real context pointer type. |
| 247 | */ |
| 248 | void |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 249 | _glapi_set_context(void *context) |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 250 | { |
| 251 | #if defined(THREADS) |
Brian Paul | 3c27be3 | 2000-02-10 21:27:48 +0000 | [diff] [blame] | 252 | _glthread_SetTSD(&ContextTSD, context); |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 253 | if (ThreadSafe) |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 254 | _glapi_Context = NULL; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 255 | else |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 256 | _glapi_Context = context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 257 | #else |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 258 | _glapi_Context = context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 259 | #endif |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | * Get the current context pointer for this thread. |
| 266 | * The context pointer is an opaque type which should be cast from |
| 267 | * void to the real context pointer type. |
| 268 | */ |
| 269 | void * |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 270 | _glapi_get_context(void) |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 271 | { |
| 272 | #if defined(THREADS) |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 273 | if (ThreadSafe) { |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 274 | return _glthread_GetTSD(&ContextTSD); |
| 275 | } |
| 276 | else { |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 277 | return _glapi_Context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 278 | } |
| 279 | #else |
Brian Paul | f9b97d9 | 2000-01-28 20:17:42 +0000 | [diff] [blame] | 280 | return _glapi_Context; |
Brian Paul | 8f91fb6 | 1999-12-17 14:51:28 +0000 | [diff] [blame] | 281 | #endif |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | /* |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 287 | * Set the global or per-thread dispatch table pointer. |
| 288 | */ |
| 289 | void |
| 290 | _glapi_set_dispatch(struct _glapi_table *dispatch) |
| 291 | { |
| 292 | if (!dispatch) { |
| 293 | /* use the no-op functions */ |
Brian Paul | 8ceb5c3 | 2000-02-24 22:14:04 +0000 | [diff] [blame] | 294 | dispatch = (struct _glapi_table *) __glapi_noop_table; |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 295 | } |
| 296 | #ifdef DEBUG |
| 297 | else { |
| 298 | _glapi_check_table(dispatch); |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | #if defined(THREADS) |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 303 | if (DispatchOverride) { |
| 304 | _glthread_SetTSD(&RealDispatchTSD, (void *) dispatch); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 305 | if (ThreadSafe) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 306 | _glapi_RealDispatch = (struct _glapi_table*) __glapi_threadsafe_table; |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 307 | else |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 308 | _glapi_RealDispatch = dispatch; |
Brian Paul | 3b18a36 | 2000-09-26 15:27:20 +0000 | [diff] [blame] | 309 | } |
| 310 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 311 | /* normal operation */ |
Brian Paul | 3b18a36 | 2000-09-26 15:27:20 +0000 | [diff] [blame] | 312 | _glthread_SetTSD(&DispatchTSD, (void *) dispatch); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 313 | if (ThreadSafe) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 314 | _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 315 | else |
| 316 | _glapi_Dispatch = dispatch; |
| 317 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 318 | #else /*THREADS*/ |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 319 | if (DispatchOverride) { |
| 320 | _glapi_RealDispatch = dispatch; |
| 321 | } |
| 322 | else { |
| 323 | _glapi_Dispatch = dispatch; |
| 324 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 325 | #endif /*THREADS*/ |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 329 | |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 330 | /* |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 331 | * Return pointer to current dispatch table for calling thread. |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 332 | */ |
| 333 | struct _glapi_table * |
| 334 | _glapi_get_dispatch(void) |
| 335 | { |
| 336 | #if defined(THREADS) |
Brian Paul | 26e14d2 | 2000-01-05 04:36:17 +0000 | [diff] [blame] | 337 | if (ThreadSafe) { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 338 | if (DispatchOverride) { |
| 339 | return (struct _glapi_table *) _glthread_GetTSD(&RealDispatchTSD); |
| 340 | } |
| 341 | else { |
| 342 | return (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); |
| 343 | } |
Brian Paul | bb72d32 | 1999-12-16 17:31:59 +0000 | [diff] [blame] | 344 | } |
Brian Paul | 590d347 | 1999-12-17 12:20:23 +0000 | [diff] [blame] | 345 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 346 | if (DispatchOverride) { |
| 347 | assert(_glapi_RealDispatch); |
| 348 | return _glapi_RealDispatch; |
| 349 | } |
| 350 | else { |
| 351 | assert(_glapi_Dispatch); |
| 352 | return _glapi_Dispatch; |
| 353 | } |
Brian Paul | 590d347 | 1999-12-17 12:20:23 +0000 | [diff] [blame] | 354 | } |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 355 | #else |
Brian Paul | c2319b4 | 2000-01-17 19:28:31 +0000 | [diff] [blame] | 356 | return _glapi_Dispatch; |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 357 | #endif |
| 358 | } |
| 359 | |
| 360 | |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 361 | /* |
| 362 | * Notes on dispatch overrride: |
| 363 | * |
| 364 | * Dispatch override allows an external agent to hook into the GL dispatch |
| 365 | * mechanism before execution goes into the core rendering library. For |
| 366 | * example, a trace mechanism would insert itself as an overrider, print |
| 367 | * logging info for each GL function, then dispatch to the real GL function. |
| 368 | * |
| 369 | * libGLS (GL Stream library) is another agent that might use override. |
| 370 | * |
| 371 | * We don't allow more than one layer of overriding at this time. |
| 372 | * In the future we may allow nested/layered override. In that case |
| 373 | * _glapi_begin_dispatch_override() will return an override layer, |
| 374 | * _glapi_end_dispatch_override(layer) will remove an override layer |
| 375 | * and _glapi_get_override_dispatch(layer) will return the dispatch |
| 376 | * table for a given override layer. layer = 0 will be the "real" |
| 377 | * dispatch table. |
| 378 | */ |
| 379 | |
| 380 | /* |
| 381 | * Return: dispatch override layer number. |
| 382 | */ |
| 383 | int |
| 384 | _glapi_begin_dispatch_override(struct _glapi_table *override) |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 385 | { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 386 | struct _glapi_table *real = _glapi_get_dispatch(); |
| 387 | |
| 388 | assert(!DispatchOverride); /* can't nest at this time */ |
| 389 | DispatchOverride = GL_TRUE; |
| 390 | |
| 391 | _glapi_set_dispatch(real); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 392 | |
| 393 | #if defined(THREADS) |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 394 | _glthread_SetTSD(&DispatchTSD, (void *) override); |
| 395 | if (ThreadSafe) |
Brian Paul | 3c257e1 | 2001-03-28 17:19:58 +0000 | [diff] [blame] | 396 | _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 397 | else |
| 398 | _glapi_Dispatch = override; |
| 399 | #else |
| 400 | _glapi_Dispatch = override; |
| 401 | #endif |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void |
| 407 | _glapi_end_dispatch_override(int layer) |
| 408 | { |
| 409 | struct _glapi_table *real = _glapi_get_dispatch(); |
| 410 | (void) layer; |
| 411 | DispatchOverride = GL_FALSE; |
| 412 | _glapi_set_dispatch(real); |
| 413 | /* the rest of this isn't needed, just play it safe */ |
| 414 | #if defined(THREADS) |
| 415 | _glthread_SetTSD(&RealDispatchTSD, NULL); |
| 416 | #endif |
| 417 | _glapi_RealDispatch = NULL; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | struct _glapi_table * |
| 422 | _glapi_get_override_dispatch(int layer) |
| 423 | { |
| 424 | if (layer == 0) { |
| 425 | return _glapi_get_dispatch(); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 426 | } |
| 427 | else { |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 428 | if (DispatchOverride) { |
| 429 | #if defined(THREADS) |
| 430 | return (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 431 | #else |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 432 | return _glapi_Dispatch; |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 433 | #endif |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 434 | } |
| 435 | else { |
| 436 | return NULL; |
| 437 | } |
| 438 | } |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 439 | } |
Brian Paul | ab0c886 | 2001-01-23 23:35:47 +0000 | [diff] [blame] | 440 | |
Brian Paul | 3a71d05 | 2000-09-05 20:17:37 +0000 | [diff] [blame] | 441 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 442 | struct name_address_offset { |
| 443 | const char *Name; |
| 444 | GLvoid *Address; |
| 445 | GLuint Offset; |
| 446 | }; |
| 447 | |
Brian Paul | 0c239fc | 1999-12-16 12:38:11 +0000 | [diff] [blame] | 448 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 449 | /* The code in this file is auto-generated with Python */ |
Brian Paul | b5fd886 | 2001-11-18 22:48:11 +0000 | [diff] [blame] | 450 | #include "glprocs.h" |
Brian Paul | 7fb54ae | 1999-11-19 22:33:50 +0000 | [diff] [blame] | 451 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 452 | |
| 453 | |
| 454 | /* |
| 455 | * Return dispatch table offset of the named static (built-in) function. |
| 456 | * Return -1 if function not found. |
| 457 | */ |
| 458 | static GLint |
| 459 | get_static_proc_offset(const char *funcName) |
| 460 | { |
| 461 | GLuint i; |
| 462 | for (i = 0; static_functions[i].Name; i++) { |
| 463 | if (strcmp(static_functions[i].Name, funcName) == 0) { |
Brian Paul | 94666c4 | 2001-09-14 02:48:53 +0000 | [diff] [blame] | 464 | return static_functions[i].Offset; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /* |
| 472 | * Return dispatch function address the named static (built-in) function. |
| 473 | * Return NULL if function not found. |
| 474 | */ |
| 475 | static GLvoid * |
| 476 | get_static_proc_address(const char *funcName) |
| 477 | { |
Brian Paul | 9c7ca85 | 2000-10-19 20:13:12 +0000 | [diff] [blame] | 478 | GLint i; |
| 479 | for (i = 0; static_functions[i].Name; i++) { |
| 480 | if (strcmp(static_functions[i].Name, funcName) == 0) { |
| 481 | return static_functions[i].Address; |
| 482 | } |
| 483 | } |
| 484 | return NULL; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | |
| 489 | /********************************************************************** |
| 490 | * Extension function management. |
| 491 | */ |
| 492 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 493 | /* |
| 494 | * Number of extension functions which we can dynamically add at runtime. |
| 495 | */ |
| 496 | #define MAX_EXTENSION_FUNCS 300 |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 497 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * The disptach table size (number of entries) is the sizeof the |
| 501 | * _glapi_table struct plus the number of dynamic entries we can add. |
| 502 | * The extra slots can be filled in by DRI drivers that register new extension |
| 503 | * functions. |
| 504 | */ |
| 505 | #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS) |
| 506 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 507 | |
| 508 | static struct name_address_offset ExtEntryTable[MAX_EXTENSION_FUNCS]; |
| 509 | static GLuint NumExtEntryPoints = 0; |
| 510 | |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 511 | #ifdef USE_SPARC_ASM |
| 512 | extern void __glapi_sparc_icache_flush(unsigned int *); |
| 513 | #endif |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * Generate a dispatch function (entrypoint) which jumps through |
| 517 | * the given slot number (offset) in the current dispatch table. |
| 518 | * We need assembly language in order to accomplish this. |
| 519 | */ |
| 520 | static void * |
| 521 | generate_entrypoint(GLuint functionOffset) |
| 522 | { |
| 523 | #if defined(USE_X86_ASM) |
| 524 | /* |
| 525 | * This x86 code contributed by Josh Vanderhoof. |
| 526 | * |
| 527 | * 0: a1 10 32 54 76 movl __glapi_Dispatch,%eax |
| 528 | * 00 01 02 03 04 |
| 529 | * 5: 85 c0 testl %eax,%eax |
| 530 | * 05 06 |
| 531 | * 7: 74 06 je f <entrypoint+0xf> |
| 532 | * 07 08 |
| 533 | * 9: ff a0 10 32 54 76 jmp *0x76543210(%eax) |
| 534 | * 09 0a 0b 0c 0d 0e |
| 535 | * f: e8 fc ff ff ff call __glapi_get_dispatch |
| 536 | * 0f 10 11 12 13 |
| 537 | * 14: ff a0 10 32 54 76 jmp *0x76543210(%eax) |
| 538 | * 14 15 16 17 18 19 |
| 539 | */ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 540 | static const unsigned char insn_template[] = { |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 541 | 0xa1, 0x00, 0x00, 0x00, 0x00, |
| 542 | 0x85, 0xc0, |
| 543 | 0x74, 0x06, |
| 544 | 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00, |
| 545 | 0xe8, 0x00, 0x00, 0x00, 0x00, |
| 546 | 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00 |
| 547 | }; |
Brian Paul | dec2a4d | 2002-10-29 15:03:14 +0000 | [diff] [blame] | 548 | unsigned char *code = (unsigned char *) malloc(sizeof(insn_template)); |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 549 | unsigned int next_insn; |
| 550 | if (code) { |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 551 | memcpy(code, insn_template, sizeof(insn_template)); |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 552 | |
| 553 | *(unsigned int *)(code + 0x01) = (unsigned int)&_glapi_Dispatch; |
| 554 | *(unsigned int *)(code + 0x0b) = (unsigned int)functionOffset * 4; |
| 555 | next_insn = (unsigned int)(code + 0x14); |
| 556 | *(unsigned int *)(code + 0x10) = (unsigned int)_glapi_get_dispatch - next_insn; |
| 557 | *(unsigned int *)(code + 0x16) = (unsigned int)functionOffset * 4; |
| 558 | } |
| 559 | return code; |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 560 | #elif defined(USE_SPARC_ASM) |
| 561 | |
| 562 | #ifdef __sparc_v9__ |
| 563 | static const unsigned int insn_template[] = { |
| 564 | 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */ |
| 565 | 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */ |
| 566 | 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */ |
| 567 | 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */ |
| 568 | 0x8528b020, /* sllx %g2, 32, %g2 */ |
| 569 | 0xc2584002, /* ldx [%g1 + %g2], %g1 */ |
| 570 | 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */ |
| 571 | 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */ |
| 572 | 0xc6584002, /* ldx [%g1 + %g2], %g3 */ |
| 573 | 0x81c0c000, /* jmpl %g3, %g0 */ |
| 574 | 0x01000000 /* nop */ |
| 575 | }; |
| 576 | #else |
| 577 | static const unsigned int insn_template[] = { |
| 578 | 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */ |
| 579 | 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */ |
| 580 | 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */ |
| 581 | 0x81c0c000, /* jmpl %g3, %g0 */ |
| 582 | 0x01000000 /* nop */ |
| 583 | }; |
| 584 | #endif |
Brian Paul | dec2a4d | 2002-10-29 15:03:14 +0000 | [diff] [blame] | 585 | unsigned int *code = (unsigned int *) malloc(sizeof(insn_template)); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 586 | unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch; |
| 587 | if (code) { |
| 588 | memcpy(code, insn_template, sizeof(insn_template)); |
| 589 | |
| 590 | #ifdef __sparc_v9__ |
| 591 | code[0] |= (glapi_addr >> (32 + 10)); |
| 592 | code[1] |= ((glapi_addr & 0xffffffff) >> 10); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 593 | __glapi_sparc_icache_flush(&code[0]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 594 | code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1)); |
| 595 | code[3] |= (glapi_addr & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 596 | __glapi_sparc_icache_flush(&code[2]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 597 | code[6] |= ((functionOffset * 8) >> 10); |
| 598 | code[7] |= ((functionOffset * 8) & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 599 | __glapi_sparc_icache_flush(&code[6]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 600 | #else |
| 601 | code[0] |= (glapi_addr >> 10); |
| 602 | code[1] |= (glapi_addr & ((1 << 10) - 1)); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 603 | __glapi_sparc_icache_flush(&code[0]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 604 | code[2] |= (functionOffset * 4); |
davem69 | 4a497e6 | 2001-06-06 22:55:28 +0000 | [diff] [blame] | 605 | __glapi_sparc_icache_flush(&code[2]); |
davem69 | 775355a | 2001-06-05 23:54:00 +0000 | [diff] [blame] | 606 | #endif |
| 607 | } |
| 608 | return code; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 609 | #else |
| 610 | return NULL; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 611 | #endif /* USE_*_ASM */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 615 | /* |
| 616 | * This function inserts a new dispatch offset into the assembly language |
| 617 | * stub that was generated with the preceeding function. |
| 618 | */ |
| 619 | static void |
| 620 | fill_in_entrypoint_offset(void *entrypoint, GLuint offset) |
| 621 | { |
| 622 | #if defined(USE_X86_ASM) |
| 623 | |
| 624 | unsigned char *code = (unsigned char *) entrypoint; |
| 625 | *(unsigned int *)(code + 0x0b) = offset * 4; |
| 626 | *(unsigned int *)(code + 0x16) = offset * 4; |
| 627 | |
| 628 | #elif defined(USE_SPARC_ASM) |
| 629 | |
| 630 | /* XXX this hasn't been tested! */ |
| 631 | unsigned int *code = (unsigned int *) entrypoint; |
| 632 | #ifdef __sparc_v9__ |
| 633 | code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */ |
| 634 | code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */ |
| 635 | code[6] |= ((offset * 8) >> 10); |
| 636 | code[7] |= ((offset * 8) & ((1 << 10) - 1)); |
| 637 | __glapi_sparc_icache_flush(&code[6]); |
| 638 | #else /* __sparc_v9__ */ |
| 639 | code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */ |
Brian Paul | 944ea20 | 2002-10-17 16:29:17 +0000 | [diff] [blame] | 640 | code[2] |= (offset * 4); |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 641 | __glapi_sparc_icache_flush(&code[2]); |
| 642 | #endif /* __sparc_v9__ */ |
| 643 | |
| 644 | #endif /* USE_*_ASM */ |
| 645 | } |
| 646 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * Add a new extension function entrypoint. |
| 650 | * Return: GL_TRUE = success or GL_FALSE = failure |
| 651 | */ |
| 652 | GLboolean |
| 653 | _glapi_add_entrypoint(const char *funcName, GLuint offset) |
| 654 | { |
Brian Paul | 8ad1076 | 2002-10-11 17:41:03 +0000 | [diff] [blame] | 655 | /* trivial rejection test */ |
| 656 | if (!funcName || funcName[0] != 'g' || funcName[1] != 'l') |
| 657 | return GL_FALSE; |
| 658 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 659 | /* first check if the named function is already statically present */ |
| 660 | { |
| 661 | GLint index = get_static_proc_offset(funcName); |
| 662 | if (index >= 0) { |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 663 | return (GLboolean) ((GLuint) index == offset); /* bad offset! */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 667 | /* See if this function has already been dynamically added */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 668 | { |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 669 | GLuint i; |
| 670 | for (i = 0; i < NumExtEntryPoints; i++) { |
| 671 | if (strcmp(ExtEntryTable[i].Name, funcName) == 0) { |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 672 | /* function already registered */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 673 | if (ExtEntryTable[i].Offset == offset) { |
| 674 | return GL_TRUE; /* offsets match */ |
| 675 | } |
Brian Paul | e4fcea2 | 2003-09-19 15:38:15 +0000 | [diff] [blame^] | 676 | else if (ExtEntryTable[i].Offset == (GLuint) ~0 |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 677 | && offset < DISPATCH_TABLE_SIZE) { |
| 678 | /* need to patch-up the dispatch code */ |
Brian Paul | e4fcea2 | 2003-09-19 15:38:15 +0000 | [diff] [blame^] | 679 | if (offset != (GLuint) ~0) { |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 680 | fill_in_entrypoint_offset(ExtEntryTable[i].Address, offset); |
| 681 | ExtEntryTable[i].Offset = offset; |
| 682 | } |
| 683 | return GL_TRUE; |
| 684 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 685 | else { |
| 686 | return GL_FALSE; /* bad offset! */ |
| 687 | } |
| 688 | } |
| 689 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Brian Paul | 8ad1076 | 2002-10-11 17:41:03 +0000 | [diff] [blame] | 692 | /* This is a new function, try to add it. */ |
| 693 | if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS || |
| 694 | offset >= DISPATCH_TABLE_SIZE) { |
| 695 | /* No space left */ |
| 696 | return GL_FALSE; |
| 697 | } |
| 698 | else { |
| 699 | void *entrypoint = generate_entrypoint(offset); |
| 700 | if (!entrypoint) |
| 701 | return GL_FALSE; /* couldn't generate assembly */ |
| 702 | |
| 703 | /* OK! */ |
| 704 | ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName); |
| 705 | ExtEntryTable[NumExtEntryPoints].Offset = offset; |
| 706 | ExtEntryTable[NumExtEntryPoints].Address = entrypoint; |
| 707 | NumExtEntryPoints++; |
| 708 | |
| 709 | return GL_TRUE; /* success */ |
| 710 | } |
| 711 | |
| 712 | /* should never get here, silence compiler warnings */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 713 | return GL_FALSE; |
| 714 | } |
| 715 | |
| 716 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 717 | /* |
| 718 | * Return offset of entrypoint for named function within dispatch table. |
| 719 | */ |
| 720 | GLint |
| 721 | _glapi_get_proc_offset(const char *funcName) |
| 722 | { |
| 723 | /* search extension functions first */ |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 724 | GLuint i; |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 725 | for (i = 0; i < NumExtEntryPoints; i++) { |
| 726 | if (strcmp(ExtEntryTable[i].Name, funcName) == 0) { |
| 727 | return ExtEntryTable[i].Offset; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* search static functions */ |
| 732 | return get_static_proc_offset(funcName); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | |
| 737 | /* |
| 738 | * Return entrypoint for named function. |
| 739 | */ |
| 740 | const GLvoid * |
| 741 | _glapi_get_proc_address(const char *funcName) |
| 742 | { |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 743 | GLuint i; |
Brian Paul | a6ed6f4 | 2003-08-27 14:48:16 +0000 | [diff] [blame] | 744 | |
| 745 | if (funcName[0] != 'g' || funcName[1] != 'l') |
| 746 | return NULL; |
| 747 | |
| 748 | /* search extension functions first */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 749 | for (i = 0; i < NumExtEntryPoints; i++) { |
| 750 | if (strcmp(ExtEntryTable[i].Name, funcName) == 0) { |
| 751 | return ExtEntryTable[i].Address; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* search static functions */ |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 756 | { |
| 757 | const GLvoid *func = get_static_proc_address(funcName); |
| 758 | if (func) |
| 759 | return func; |
| 760 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 761 | |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 762 | /* generate new entrypoint - use a temporary dispatch offset of |
| 763 | * ~0 (i.e. -1). Later, when the driver calls _glapi_add_entrypoint() |
| 764 | * we'll put in the proper offset. If that never happens, and the |
| 765 | * user calls this function, he'll segfault. That's what you get |
| 766 | * when you try calling a GL function that doesn't really exist. |
| 767 | */ |
| 768 | if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) { |
| 769 | GLvoid *entrypoint = generate_entrypoint(~0); |
| 770 | if (!entrypoint) |
| 771 | return GL_FALSE; |
| 772 | |
| 773 | ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName); |
| 774 | ExtEntryTable[NumExtEntryPoints].Offset = ~0; |
| 775 | ExtEntryTable[NumExtEntryPoints].Address = entrypoint; |
| 776 | NumExtEntryPoints++; |
| 777 | |
| 778 | return entrypoint; |
| 779 | } |
| 780 | else { |
| 781 | /* no space for new functions! */ |
| 782 | return NULL; |
| 783 | } |
| 784 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 785 | |
| 786 | |
| 787 | |
| 788 | /* |
| 789 | * Return the name of the function at the given dispatch offset. |
| 790 | * This is only intended for debugging. |
| 791 | */ |
| 792 | const char * |
| 793 | _glapi_get_proc_name(GLuint offset) |
| 794 | { |
| 795 | const GLuint n = sizeof(static_functions) / sizeof(struct name_address_offset); |
| 796 | GLuint i; |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 797 | |
| 798 | /* search built-in functions */ |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 799 | for (i = 0; i < n; i++) { |
| 800 | if (static_functions[i].Offset == offset) |
| 801 | return static_functions[i].Name; |
| 802 | } |
| 803 | |
| 804 | /* search added extension functions */ |
| 805 | for (i = 0; i < NumExtEntryPoints; i++) { |
| 806 | if (ExtEntryTable[i].Offset == offset) { |
| 807 | return ExtEntryTable[i].Name; |
| 808 | } |
| 809 | } |
| 810 | return NULL; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | |
| 815 | /* |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 816 | * Return size of dispatch table struct as number of functions (or |
| 817 | * slots). |
| 818 | */ |
| 819 | GLuint |
| 820 | _glapi_get_dispatch_table_size(void) |
| 821 | { |
| 822 | return DISPATCH_TABLE_SIZE; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | |
| 827 | /* |
| 828 | * Get API dispatcher version string. |
| 829 | */ |
| 830 | const char * |
| 831 | _glapi_get_version(void) |
| 832 | { |
| 833 | return "20021001"; /* YYYYMMDD */ |
| 834 | } |
| 835 | |
| 836 | |
| 837 | |
| 838 | /* |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 839 | * Make sure there are no NULL pointers in the given dispatch table. |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 840 | * Intended for debugging purposes. |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 841 | */ |
| 842 | void |
| 843 | _glapi_check_table(const struct _glapi_table *table) |
| 844 | { |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 845 | #ifdef DEBUG |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 846 | const GLuint entries = _glapi_get_dispatch_table_size(); |
| 847 | const void **tab = (const void **) table; |
| 848 | GLuint i; |
| 849 | for (i = 1; i < entries; i++) { |
| 850 | assert(tab[i]); |
| 851 | } |
| 852 | |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 853 | /* Do some spot checks to be sure that the dispatch table |
| 854 | * slots are assigned correctly. |
| 855 | */ |
| 856 | { |
| 857 | GLuint BeginOffset = _glapi_get_proc_offset("glBegin"); |
| 858 | char *BeginFunc = (char*) &table->Begin; |
| 859 | GLuint offset = (BeginFunc - (char *) table) / sizeof(void *); |
| 860 | assert(BeginOffset == _gloffset_Begin); |
| 861 | assert(BeginOffset == offset); |
| 862 | } |
| 863 | { |
| 864 | GLuint viewportOffset = _glapi_get_proc_offset("glViewport"); |
| 865 | char *viewportFunc = (char*) &table->Viewport; |
| 866 | GLuint offset = (viewportFunc - (char *) table) / sizeof(void *); |
| 867 | assert(viewportOffset == _gloffset_Viewport); |
| 868 | assert(viewportOffset == offset); |
| 869 | } |
| 870 | { |
| 871 | GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer"); |
| 872 | char *VertexPointerFunc = (char*) &table->VertexPointer; |
| 873 | GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *); |
| 874 | assert(VertexPointerOffset == _gloffset_VertexPointer); |
| 875 | assert(VertexPointerOffset == offset); |
| 876 | } |
| 877 | { |
| 878 | GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax"); |
| 879 | char *ResetMinMaxFunc = (char*) &table->ResetMinmax; |
| 880 | GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *); |
| 881 | assert(ResetMinMaxOffset == _gloffset_ResetMinmax); |
| 882 | assert(ResetMinMaxOffset == offset); |
| 883 | } |
| 884 | { |
| 885 | GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor"); |
| 886 | char *blendColorFunc = (char*) &table->BlendColor; |
| 887 | GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *); |
| 888 | assert(blendColorOffset == _gloffset_BlendColor); |
| 889 | assert(blendColorOffset == offset); |
| 890 | } |
| 891 | { |
| 892 | GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT"); |
| 893 | char *istextureFunc = (char*) &table->IsTextureEXT; |
| 894 | GLuint offset = (istextureFunc - (char *) table) / sizeof(void *); |
| 895 | assert(istextureOffset == _gloffset_IsTextureEXT); |
| 896 | assert(istextureOffset == offset); |
| 897 | } |
Brian Paul | a14cbff | 2000-10-27 18:31:21 +0000 | [diff] [blame] | 898 | { |
| 899 | GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT"); |
| 900 | char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT; |
| 901 | GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *); |
| 902 | assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT); |
| 903 | assert(secondaryColor3fOffset == offset); |
| 904 | assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (void *) &glSecondaryColor3fEXT); |
| 905 | } |
Brian Paul | 91d6f12 | 2002-05-29 15:23:16 +0000 | [diff] [blame] | 906 | { |
| 907 | GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV"); |
| 908 | char *pointParameterivFunc = (char*) &table->PointParameterivNV; |
| 909 | GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *); |
| 910 | assert(pointParameterivOffset == _gloffset_PointParameterivNV); |
| 911 | assert(pointParameterivOffset == offset); |
| 912 | assert(_glapi_get_proc_address("glPointParameterivNV") == (void *) &glPointParameterivNV); |
| 913 | } |
Brian Paul | 54f3aab | 2002-10-02 01:51:44 +0000 | [diff] [blame] | 914 | { |
| 915 | GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV"); |
| 916 | char *setFenceFunc = (char*) &table->SetFenceNV; |
| 917 | GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *); |
| 918 | assert(setFenceOffset == _gloffset_SetFenceNV); |
| 919 | assert(setFenceOffset == offset); |
| 920 | assert(_glapi_get_proc_address("glSetFenceNV") == (void *) &glSetFenceNV); |
| 921 | } |
Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 922 | #endif |
| 923 | } |