blob: 5132bebaef5e0e67890895a536e09ff95389038d [file] [log] [blame]
Brian Paulfbd8f211999-11-11 01:22:25 +00001/*
2 * Mesa 3-D graphics library
Brian Paul767e15a2004-11-27 03:51:11 +00003 * Version: 6.3
Brian Paulfbd8f211999-11-11 01:22:25 +00004 *
Brian Paul767e15a2004-11-27 03:51:11 +00005 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
Brian Paulfbd8f211999-11-11 01:22:25 +00006 *
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 Paul7e1161b1999-11-25 18:17:04 +000026/*
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 Paul9f943992000-01-28 19:03:33 +000033 *
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 Paul4e9676f2002-06-29 19:48:15 +000038 * NOTE: There are no dependencies on Mesa in this code.
Brian Paulab0c8862001-01-23 23:35:47 +000039 *
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 Paul4e9676f2002-06-29 19:48:15 +000043 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
Brian Paul54f3aab2002-10-02 01:51:44 +000044 * 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 Paul7e1161b1999-11-25 18:17:04 +000049 */
50
51
52
Brian Paul3c27be32000-02-10 21:27:48 +000053#include "glheader.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000054#include "glapi.h"
Brian Paul0c239fc1999-12-16 12:38:11 +000055#include "glapioffsets.h"
56#include "glapitable.h"
Brian Paul9f943992000-01-28 19:03:33 +000057#include "glthread.h"
Brian Paulbb72d321999-12-16 17:31:59 +000058
Brian Paul3c257e12001-03-28 17:19:58 +000059/***** BEGIN NO-OP DISPATCH *****/
60
61static GLboolean WarnFlag = GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000062static _glapi_warning_func warning_func;
Brian Paul3c257e12001-03-28 17:19:58 +000063
Ian Romanick25fe93f2005-04-13 20:59:15 +000064static void init_glapi_relocs(void);
65
66static _glapi_proc generate_entrypoint(GLuint functionOffset);
67static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
Brian Paul4e9676f2002-06-29 19:48:15 +000068
69/*
70 * Enable/disable printing of warning messages.
71 */
Adam Jackson791ce022004-12-15 23:14:29 +000072PUBLIC void
Brian Paul3c257e12001-03-28 17:19:58 +000073_glapi_noop_enable_warnings(GLboolean enable)
74{
75 WarnFlag = enable;
76}
77
Brian Paul4e9676f2002-06-29 19:48:15 +000078/*
79 * Register a callback function for reporting errors.
80 */
Adam Jackson791ce022004-12-15 23:14:29 +000081PUBLIC void
Brian Paul4e9676f2002-06-29 19:48:15 +000082_glapi_set_warning_func( _glapi_warning_func func )
83{
84 warning_func = func;
85}
86
Brian Paul3c257e12001-03-28 17:19:58 +000087static GLboolean
88warn(void)
89{
Ben Crossmand8aa5ff2005-04-15 09:13:21 +000090 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
Brian Paul4e9676f2002-06-29 19:48:15 +000091 && warning_func) {
Brian Paul3c257e12001-03-28 17:19:58 +000092 return GL_TRUE;
Brian Paul4e9676f2002-06-29 19:48:15 +000093 }
94 else {
Brian Paul3c257e12001-03-28 17:19:58 +000095 return GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000096 }
Brian Paul3c257e12001-03-28 17:19:58 +000097}
98
99
100#define KEYWORD1 static
Daniel Borca722cb892004-01-07 12:37:09 +0000101#define KEYWORD2 GLAPIENTRY
Brian Paul3c257e12001-03-28 17:19:58 +0000102#define NAME(func) NoOp##func
103
Brian Paul4e9676f2002-06-29 19:48:15 +0000104#define F NULL
Brian Paul3c257e12001-03-28 17:19:58 +0000105
Brian Paul5849e3d2004-11-05 18:32:02 +0000106#define DISPATCH(func, args, msg) \
107 if (warn()) { \
108 warning_func(NULL, "GL User Error: called without context: %s", #func); \
Brian Paul3c257e12001-03-28 17:19:58 +0000109 }
110
Brian Paul5849e3d2004-11-05 18:32:02 +0000111#define RETURN_DISPATCH(func, args, msg) \
112 if (warn()) { \
113 warning_func(NULL, "GL User Error: called without context: %s", #func); \
114 } \
Brian Paul3c257e12001-03-28 17:19:58 +0000115 return 0
116
117#define DISPATCH_TABLE_NAME __glapi_noop_table
Brian Paul767e15a2004-11-27 03:51:11 +0000118#define UNUSED_TABLE_NAME __unused_noop_functions
Brian Paul3c257e12001-03-28 17:19:58 +0000119
Brian Paul767e15a2004-11-27 03:51:11 +0000120#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
Brian Paul3c257e12001-03-28 17:19:58 +0000121
Brian Paula760ccf2004-12-03 15:24:34 +0000122static GLint NoOpUnused(void)
Brian Paul3c257e12001-03-28 17:19:58 +0000123{
124 if (warn()) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000125 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
Brian Paul3c257e12001-03-28 17:19:58 +0000126 }
127 return 0;
128}
129
130#include "glapitemp.h"
131
132/***** END NO-OP DISPATCH *****/
133
134
135
136/***** BEGIN THREAD-SAFE DISPATCH *****/
Brian Paul3c257e12001-03-28 17:19:58 +0000137
138#if defined(THREADS)
139
Ian Romanick25fe93f2005-04-13 20:59:15 +0000140#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
146static __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 Romanick8e77da1c2004-06-29 19:08:20 +0000162/**
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 Romanickd14d1032004-07-02 00:01:09 +0000181 * 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 Romanick10b3bf62004-07-05 22:42:14 +0000185 * \c GL_TRUE, is used to determine whether or not the application is
Ian Romanickd14d1032004-07-02 00:01:09 +0000186 * multithreaded.
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000187 */
188/*@{*/
Ian Romanickd14d1032004-07-02 00:01:09 +0000189static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000190_glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
191static _glthread_TSD RealDispatchTSD; /**< only when using override */
192static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
193/*@}*/
Brian Paul3c257e12001-03-28 17:19:58 +0000194
Ian Romanick25fe93f2005-04-13 20:59:15 +0000195#endif /* defined(GLX_USE_TLS) */
Brian Paul3c257e12001-03-28 17:19:58 +0000196
Brian Paul3c257e12001-03-28 17:19:58 +0000197#define DISPATCH_TABLE_NAME __glapi_threadsafe_table
Brian Paul767e15a2004-11-27 03:51:11 +0000198#define UNUSED_TABLE_NAME __unused_threadsafe_functions
Brian Paul3c257e12001-03-28 17:19:58 +0000199
Brian Paul767e15a2004-11-27 03:51:11 +0000200#define TABLE_ENTRY(name) (_glapi_proc) gl##name
Brian Paul3c257e12001-03-28 17:19:58 +0000201
Brian Paula760ccf2004-12-03 15:24:34 +0000202static GLint glUnused(void)
Brian Paul3c257e12001-03-28 17:19:58 +0000203{
204 return 0;
205}
206
207#include "glapitemp.h"
208
209#endif
210
211/***** END THREAD-SAFE DISPATCH *****/
212
213
Ian Romanick25fe93f2005-04-13 20:59:15 +0000214#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/*@{*/
231PUBLIC const struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table;
232PUBLIC const struct _glapi_table *_glapi_DispatchTSD = NULL;
233PUBLIC const void *_glapi_Context = NULL;
234/*@}*/
235
236#else
Brian Paul3c257e12001-03-28 17:19:58 +0000237
Adam Jackson791ce022004-12-15 23:14:29 +0000238PUBLIC struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table;
Ian Romanickd14d1032004-07-02 00:01:09 +0000239#if defined( THREADS )
Adam Jackson791ce022004-12-15 23:14:29 +0000240PUBLIC struct _glapi_table *_glapi_DispatchTSD = (struct _glapi_table *) __glapi_noop_table;
Ian Romanickd14d1032004-07-02 00:01:09 +0000241#endif
Adam Jackson791ce022004-12-15 23:14:29 +0000242PUBLIC struct _glapi_table *_glapi_RealDispatch = (struct _glapi_table *) __glapi_noop_table;
Brian Paul0f710251999-12-15 15:02:30 +0000243
Brian Paul8f91fb61999-12-17 14:51:28 +0000244/* Used when thread safety disabled */
Adam Jackson791ce022004-12-15 23:14:29 +0000245PUBLIC void *_glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000246
Ian Romanick25fe93f2005-04-13 20:59:15 +0000247#endif /* defined(GLX_USE_TLS) */
248
Brian Paul0f710251999-12-15 15:02:30 +0000249
Brian Paulab0c8862001-01-23 23:35:47 +0000250static GLboolean DispatchOverride = GL_FALSE;
251
252
Brian Paul54f3aab2002-10-02 01:51:44 +0000253
Brian Paul767e15a2004-11-27 03:51:11 +0000254/**
255 * strdup() is actually not a standard ANSI C or POSIX routine.
Brian Paul3c257e12001-03-28 17:19:58 +0000256 * Irix will not define it if ANSI mode is in effect.
257 */
258static char *
259str_dup(const char *str)
Randy Frankd7361e12000-03-27 21:13:58 +0000260{
Brian Paulfffb8092000-03-29 18:46:11 +0000261 char *copy;
262 copy = (char*) malloc(strlen(str) + 1);
263 if (!copy)
264 return NULL;
265 strcpy(copy, str);
266 return copy;
Randy Frankd7361e12000-03-27 21:13:58 +0000267}
268
Brian Paul7fb54ae1999-11-19 22:33:50 +0000269
Brian Paulbb72d321999-12-16 17:31:59 +0000270
Brian Paul767e15a2004-11-27 03:51:11 +0000271/**
Brian Paulbb72d321999-12-16 17:31:59 +0000272 * We should call this periodically from a function such as glXMakeCurrent
Brian Paul5104b4d2002-03-07 21:50:41 +0000273 * in order to test if multiple threads are being used.
Brian Paulbb72d321999-12-16 17:31:59 +0000274 */
Adam Jackson791ce022004-12-15 23:14:29 +0000275PUBLIC void
Brian Paulbb72d321999-12-16 17:31:59 +0000276_glapi_check_multithread(void)
277{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000278#if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000279 if (!ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000280 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 Romanickd14d1032004-07-02 00:01:09 +0000287 ThreadSafe = GL_TRUE;
Brian Paulbb72d321999-12-16 17:31:59 +0000288 _glapi_set_dispatch(NULL);
289 }
290 }
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000291 else if (!_glapi_get_dispatch()) {
292 /* make sure that this thread's dispatch pointer isn't null */
293 _glapi_set_dispatch(NULL);
294 }
Brian Paulbb72d321999-12-16 17:31:59 +0000295#endif
296}
297
298
299
Brian Paul767e15a2004-11-27 03:51:11 +0000300/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000301 * 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 Jackson791ce022004-12-15 23:14:29 +0000305PUBLIC void
Brian Paulf9b97d92000-01-28 20:17:42 +0000306_glapi_set_context(void *context)
Brian Paul8f91fb61999-12-17 14:51:28 +0000307{
Brian Paul767e15a2004-11-27 03:51:11 +0000308 (void) __unused_noop_functions; /* silence a warning */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000309#if defined(GLX_USE_TLS)
310 _glapi_tls_Context = context;
311#elif defined(THREADS)
Brian Paul767e15a2004-11-27 03:51:11 +0000312 (void) __unused_threadsafe_functions; /* silence a warning */
Brian Paul3c27be32000-02-10 21:27:48 +0000313 _glthread_SetTSD(&ContextTSD, context);
Ian Romanickd14d1032004-07-02 00:01:09 +0000314 _glapi_Context = (ThreadSafe) ? NULL : context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000315#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000316 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000317#endif
318}
319
320
321
Brian Paul767e15a2004-11-27 03:51:11 +0000322/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000323 * 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 Jackson791ce022004-12-15 23:14:29 +0000327PUBLIC void *
Brian Paulf9b97d92000-01-28 20:17:42 +0000328_glapi_get_context(void)
Brian Paul8f91fb61999-12-17 14:51:28 +0000329{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000330#if defined(GLX_USE_TLS)
331 return _glapi_tls_Context;
332#elif defined(THREADS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000333 if (ThreadSafe) {
Brian Paul8f91fb61999-12-17 14:51:28 +0000334 return _glthread_GetTSD(&ContextTSD);
335 }
336 else {
Brian Paulf9b97d92000-01-28 20:17:42 +0000337 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000338 }
339#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000340 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000341#endif
342}
343
344
345
Brian Paul767e15a2004-11-27 03:51:11 +0000346/**
Brian Paul7fb54ae1999-11-19 22:33:50 +0000347 * Set the global or per-thread dispatch table pointer.
348 */
Adam Jackson791ce022004-12-15 23:14:29 +0000349PUBLIC void
Brian Paul7fb54ae1999-11-19 22:33:50 +0000350_glapi_set_dispatch(struct _glapi_table *dispatch)
351{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000352#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 Romanick6c5402b2004-07-05 22:40:54 +0000359 if (!dispatch) {
360 /* use the no-op functions */
361 dispatch = (struct _glapi_table *) __glapi_noop_table;
362 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000363#ifdef DEBUG
Ian Romanick6c5402b2004-07-05 22:40:54 +0000364 else {
Brian Paul7fb54ae1999-11-19 22:33:50 +0000365 _glapi_check_table(dispatch);
366 }
367#endif
368
Ian Romanick25fe93f2005-04-13 20:59:15 +0000369#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 Paulab0c8862001-01-23 23:35:47 +0000378 if (DispatchOverride) {
Ian Romanick6c5402b2004-07-05 22:40:54 +0000379 _glthread_SetTSD(&RealDispatchTSD, (void *) dispatch);
Ian Romanickd14d1032004-07-02 00:01:09 +0000380 if (ThreadSafe)
Brian Paul3c257e12001-03-28 17:19:58 +0000381 _glapi_RealDispatch = (struct _glapi_table*) __glapi_threadsafe_table;
Brian Paul3a71d052000-09-05 20:17:37 +0000382 else
Brian Paulab0c8862001-01-23 23:35:47 +0000383 _glapi_RealDispatch = dispatch;
Brian Paul3b18a362000-09-26 15:27:20 +0000384 }
385 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000386 /* normal operation */
Ian Romanick6c5402b2004-07-05 22:40:54 +0000387 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
Ian Romanickd14d1032004-07-02 00:01:09 +0000388 if (ThreadSafe) {
389 _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table;
390 _glapi_DispatchTSD = NULL;
391 }
392 else {
Ian Romanick6c5402b2004-07-05 22:40:54 +0000393 _glapi_Dispatch = dispatch;
Ian Romanickd14d1032004-07-02 00:01:09 +0000394 _glapi_DispatchTSD = dispatch;
395 }
Brian Paul3a71d052000-09-05 20:17:37 +0000396 }
Brian Paul3a71d052000-09-05 20:17:37 +0000397#else /*THREADS*/
Brian Paulab0c8862001-01-23 23:35:47 +0000398 if (DispatchOverride) {
399 _glapi_RealDispatch = dispatch;
400 }
401 else {
402 _glapi_Dispatch = dispatch;
403 }
Brian Paul3a71d052000-09-05 20:17:37 +0000404#endif /*THREADS*/
Brian Paul7fb54ae1999-11-19 22:33:50 +0000405}
406
407
Brian Paulbb72d321999-12-16 17:31:59 +0000408
Brian Paul767e15a2004-11-27 03:51:11 +0000409/**
Brian Paulbb72d321999-12-16 17:31:59 +0000410 * Return pointer to current dispatch table for calling thread.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000411 */
Adam Jackson791ce022004-12-15 23:14:29 +0000412PUBLIC struct _glapi_table *
Brian Paul7fb54ae1999-11-19 22:33:50 +0000413_glapi_get_dispatch(void)
414{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000415#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 Romanickd14d1032004-07-02 00:01:09 +0000422 if (ThreadSafe) {
Brian Paulab0c8862001-01-23 23:35:47 +0000423 if (DispatchOverride) {
424 return (struct _glapi_table *) _glthread_GetTSD(&RealDispatchTSD);
425 }
426 else {
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000427 return (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD);
Brian Paulab0c8862001-01-23 23:35:47 +0000428 }
Brian Paulbb72d321999-12-16 17:31:59 +0000429 }
Brian Paul590d3471999-12-17 12:20:23 +0000430 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000431 if (DispatchOverride) {
432 assert(_glapi_RealDispatch);
433 return _glapi_RealDispatch;
434 }
435 else {
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000436 assert(_glapi_DispatchTSD);
437 return _glapi_DispatchTSD;
Brian Paulab0c8862001-01-23 23:35:47 +0000438 }
Brian Paul590d3471999-12-17 12:20:23 +0000439 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000440#else
Brian Paulc2319b42000-01-17 19:28:31 +0000441 return _glapi_Dispatch;
Brian Paul7fb54ae1999-11-19 22:33:50 +0000442#endif
443}
444
445
Brian Paulab0c8862001-01-23 23:35:47 +0000446/*
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 Jackson791ce022004-12-15 23:14:29 +0000468PUBLIC int
Brian Paulab0c8862001-01-23 23:35:47 +0000469_glapi_begin_dispatch_override(struct _glapi_table *override)
Brian Paul3a71d052000-09-05 20:17:37 +0000470{
Brian Paulab0c8862001-01-23 23:35:47 +0000471 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 Paul3a71d052000-09-05 20:17:37 +0000477
Ian Romanick25fe93f2005-04-13 20:59:15 +0000478#if defined(GLX_USE_TLS)
479 _glthread_SetTSD(&_gl_DispatchTSD, (void *) override);
480 _glapi_tls_Dispatch = override;
481#elif defined(THREADS)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000482 _glthread_SetTSD(&_gl_DispatchTSD, (void *) override);
Ian Romanickd14d1032004-07-02 00:01:09 +0000483 if ( ThreadSafe ) {
Brian Paul3c257e12001-03-28 17:19:58 +0000484 _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table;
Ian Romanickd14d1032004-07-02 00:01:09 +0000485 _glapi_DispatchTSD = NULL;
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000486 }
487 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000488 _glapi_Dispatch = override;
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000489 _glapi_DispatchTSD = override;
490 }
Brian Paulab0c8862001-01-23 23:35:47 +0000491#else
492 _glapi_Dispatch = override;
493#endif
494 return 1;
495}
496
497
Adam Jackson791ce022004-12-15 23:14:29 +0000498PUBLIC void
Brian Paulab0c8862001-01-23 23:35:47 +0000499_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 Romanick25fe93f2005-04-13 20:59:15 +0000506#if defined(GLX_USE_TLS)
507 _glapi_tls_RealDispatch = NULL;
508#else
509# if defined(THREADS)
Brian Paulab0c8862001-01-23 23:35:47 +0000510 _glthread_SetTSD(&RealDispatchTSD, NULL);
Ian Romanick25fe93f2005-04-13 20:59:15 +0000511# endif
Brian Paulab0c8862001-01-23 23:35:47 +0000512 _glapi_RealDispatch = NULL;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000513#endif
Brian Paulab0c8862001-01-23 23:35:47 +0000514}
515
516
Adam Jackson791ce022004-12-15 23:14:29 +0000517PUBLIC struct _glapi_table *
Brian Paulab0c8862001-01-23 23:35:47 +0000518_glapi_get_override_dispatch(int layer)
519{
520 if (layer == 0) {
521 return _glapi_get_dispatch();
Brian Paul3a71d052000-09-05 20:17:37 +0000522 }
523 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000524 if (DispatchOverride) {
Ian Romanick25fe93f2005-04-13 20:59:15 +0000525#if defined(GLX_USE_TLS)
526 return (struct _glapi_table *) _glapi_tls_Dispatch;
527#elif defined(THREADS)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000528 return (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD);
Brian Paul3a71d052000-09-05 20:17:37 +0000529#else
Brian Paulab0c8862001-01-23 23:35:47 +0000530 return _glapi_Dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000531#endif
Brian Paulab0c8862001-01-23 23:35:47 +0000532 }
533 else {
534 return NULL;
535 }
536 }
Brian Paul3a71d052000-09-05 20:17:37 +0000537}
Brian Paulab0c8862001-01-23 23:35:47 +0000538
Brian Paul3a71d052000-09-05 20:17:37 +0000539
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000540#if !defined( USE_X86_ASM )
Ian Romanick78677992004-05-27 00:05:13 +0000541#define NEED_FUNCTION_POINTER
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000542#endif
Ian Romanick78677992004-05-27 00:05:13 +0000543
Brian Paul54f3aab2002-10-02 01:51:44 +0000544/* The code in this file is auto-generated with Python */
Brian Paulb5fd8862001-11-18 22:48:11 +0000545#include "glprocs.h"
Brian Paul7fb54ae1999-11-19 22:33:50 +0000546
Brian Paul959f8022000-03-19 01:10:11 +0000547
Brian Paul767e15a2004-11-27 03:51:11 +0000548/**
549 * Search the table of static entrypoint functions for the named function
550 * and return the corresponding glprocs_table_t entry.
551 */
Ian Romanick78677992004-05-27 00:05:13 +0000552static const glprocs_table_t *
553find_entry( const char * n )
554{
Brian Paula760ccf2004-12-03 15:24:34 +0000555 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000556
Brian Paula760ccf2004-12-03 15:24:34 +0000557 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000558 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 Paul767e15a2004-11-27 03:51:11 +0000568
569/**
Brian Paul959f8022000-03-19 01:10:11 +0000570 * Return dispatch table offset of the named static (built-in) function.
571 * Return -1 if function not found.
572 */
573static GLint
574get_static_proc_offset(const char *funcName)
575{
Ian Romanick78677992004-05-27 00:05:13 +0000576 const glprocs_table_t * const f = find_entry( funcName );
577
578 if ( f != NULL ) {
579 return f->Offset;
Brian Paul959f8022000-03-19 01:10:11 +0000580 }
581 return -1;
582}
583
584
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000585#ifdef USE_X86_ASM
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000586
Ian Romanick25fe93f2005-04-13 20:59:15 +0000587#if defined( GLX_USE_TLS )
588extern GLubyte gl_dispatch_functions_start[];
589extern GLubyte gl_dispatch_functions_end[];
590#else
591extern const GLubyte gl_dispatch_functions_start[];
592#endif
593
594# if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000595# define X86_DISPATCH_FUNCTION_SIZE 32
596# else
597# define X86_DISPATCH_FUNCTION_SIZE 16
598# endif
599
600
Brian Paul767e15a2004-11-27 03:51:11 +0000601/**
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000602 * Return dispatch function address the named static (built-in) function.
603 * Return NULL if function not found.
604 */
Brian Paul209bd3a2004-11-27 04:02:32 +0000605static const _glapi_proc
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000606get_static_proc_address(const char *funcName)
607{
608 const glprocs_table_t * const f = find_entry( funcName );
609
610 if ( f != NULL ) {
Brian Paul209bd3a2004-11-27 04:02:32 +0000611 return (_glapi_proc) (gl_dispatch_functions_start
612 + (X86_DISPATCH_FUNCTION_SIZE * f->Offset));
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000613 }
614 else {
615 return NULL;
616 }
617}
618
619#else
620
621
Brian Paul767e15a2004-11-27 03:51:11 +0000622/**
623 * Return pointer to the named static (built-in) function.
624 * \return NULL if function not found.
Brian Paul959f8022000-03-19 01:10:11 +0000625 */
Brian Paul767e15a2004-11-27 03:51:11 +0000626static const _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000627get_static_proc_address(const char *funcName)
628{
Ian Romanick78677992004-05-27 00:05:13 +0000629 const glprocs_table_t * const f = find_entry( funcName );
630 return ( f != NULL ) ? f->Address : NULL;
631}
632
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000633#endif /* USE_X86_ASM */
634
Ian Romanick78677992004-05-27 00:05:13 +0000635
Brian Paul767e15a2004-11-27 03:51:11 +0000636/**
637 * Return the name of the function at the given offset in the dispatch
638 * table. For debugging only.
639 */
Ian Romanick78677992004-05-27 00:05:13 +0000640static const char *
641get_static_proc_name( GLuint offset )
642{
Brian Paula760ccf2004-12-03 15:24:34 +0000643 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000644
Brian Paula760ccf2004-12-03 15:24:34 +0000645 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000646 if (static_functions[i].Offset == offset) {
647 return gl_string_table + static_functions[i].Name_offset;
Brian Paul9c7ca852000-10-19 20:13:12 +0000648 }
649 }
650 return NULL;
Brian Paul959f8022000-03-19 01:10:11 +0000651}
652
653
654
655/**********************************************************************
656 * Extension function management.
657 */
658
Brian Paul54f3aab2002-10-02 01:51:44 +0000659/*
660 * Number of extension functions which we can dynamically add at runtime.
661 */
662#define MAX_EXTENSION_FUNCS 300
Brian Paul959f8022000-03-19 01:10:11 +0000663
Brian Paul54f3aab2002-10-02 01:51:44 +0000664
665/*
Brian Paul767e15a2004-11-27 03:51:11 +0000666 * The dispatch table size (number of entries) is the size of the
Brian Paul54f3aab2002-10-02 01:51:44 +0000667 * _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 Paul959f8022000-03-19 01:10:11 +0000673
Brian Paul767e15a2004-11-27 03:51:11 +0000674struct name_address_offset {
675 const char *Name;
676 _glapi_proc Address;
677 GLuint Offset;
678};
679
680
Brian Paul959f8022000-03-19 01:10:11 +0000681static struct name_address_offset ExtEntryTable[MAX_EXTENSION_FUNCS];
682static GLuint NumExtEntryPoints = 0;
683
davem694a497e62001-06-06 22:55:28 +0000684#ifdef USE_SPARC_ASM
685extern void __glapi_sparc_icache_flush(unsigned int *);
686#endif
Brian Paul959f8022000-03-19 01:10:11 +0000687
Brian Paul767e15a2004-11-27 03:51:11 +0000688/**
Brian Paul959f8022000-03-19 01:10:11 +0000689 * Generate a dispatch function (entrypoint) which jumps through
690 * the given slot number (offset) in the current dispatch table.
691 * We need assembly language in order to accomplish this.
692 */
Brian Paul767e15a2004-11-27 03:51:11 +0000693static _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000694generate_entrypoint(GLuint functionOffset)
695{
696#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000697 /* 32 is chosen as something of a magic offset. For x86, the dispatch
698 * at offset 32 is the first one where the offset in the
699 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
Brian Paul959f8022000-03-19 01:10:11 +0000700 */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000701 const GLubyte * const template_func = gl_dispatch_functions_start
702 + (X86_DISPATCH_FUNCTION_SIZE * 32);
703 GLubyte * const code = (GLubyte *) malloc( X86_DISPATCH_FUNCTION_SIZE );
Brian Paul959f8022000-03-19 01:10:11 +0000704
Ian Romanick25fe93f2005-04-13 20:59:15 +0000705
706 if ( code != NULL ) {
707 (void) memcpy( code, template_func, X86_DISPATCH_FUNCTION_SIZE );
708 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
Brian Paul959f8022000-03-19 01:10:11 +0000709 }
Ian Romanick25fe93f2005-04-13 20:59:15 +0000710
Brian Paul767e15a2004-11-27 03:51:11 +0000711 return (_glapi_proc) code;
davem69775355a2001-06-05 23:54:00 +0000712#elif defined(USE_SPARC_ASM)
713
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000714#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000715 static const unsigned int insn_template[] = {
716 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
717 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
718 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
719 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
720 0x8528b020, /* sllx %g2, 32, %g2 */
721 0xc2584002, /* ldx [%g1 + %g2], %g1 */
722 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
723 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
724 0xc6584002, /* ldx [%g1 + %g2], %g3 */
725 0x81c0c000, /* jmpl %g3, %g0 */
726 0x01000000 /* nop */
727 };
728#else
729 static const unsigned int insn_template[] = {
730 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
731 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
732 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
733 0x81c0c000, /* jmpl %g3, %g0 */
734 0x01000000 /* nop */
735 };
736#endif
Brian Pauldec2a4d2002-10-29 15:03:14 +0000737 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
davem69775355a2001-06-05 23:54:00 +0000738 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
739 if (code) {
740 memcpy(code, insn_template, sizeof(insn_template));
741
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000742#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000743 code[0] |= (glapi_addr >> (32 + 10));
744 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
davem694a497e62001-06-06 22:55:28 +0000745 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000746 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
747 code[3] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000748 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000749 code[6] |= ((functionOffset * 8) >> 10);
750 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000751 __glapi_sparc_icache_flush(&code[6]);
davem69775355a2001-06-05 23:54:00 +0000752#else
753 code[0] |= (glapi_addr >> 10);
754 code[1] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000755 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000756 code[2] |= (functionOffset * 4);
davem694a497e62001-06-06 22:55:28 +0000757 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000758#endif
759 }
Brian Paul767e15a2004-11-27 03:51:11 +0000760 return (_glapi_proc) code;
Brian Paul959f8022000-03-19 01:10:11 +0000761#else
Brian Paula6c423d2004-08-25 15:59:48 +0000762 (void) functionOffset;
Brian Paul959f8022000-03-19 01:10:11 +0000763 return NULL;
Brian Paul54f3aab2002-10-02 01:51:44 +0000764#endif /* USE_*_ASM */
Brian Paul959f8022000-03-19 01:10:11 +0000765}
766
767
Brian Paul767e15a2004-11-27 03:51:11 +0000768/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000769 * This function inserts a new dispatch offset into the assembly language
770 * stub that was generated with the preceeding function.
771 */
772static void
Brian Paul767e15a2004-11-27 03:51:11 +0000773fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
Brian Paul54f3aab2002-10-02 01:51:44 +0000774{
775#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000776 GLubyte * const code = (GLubyte *) entrypoint;
Brian Paul54f3aab2002-10-02 01:51:44 +0000777
Ian Romanick25fe93f2005-04-13 20:59:15 +0000778
779#if X86_DISPATCH_FUNCTION_SIZE == 32
780 *((unsigned int *)(code + 11)) = 4 * offset;
781 *((unsigned int *)(code + 22)) = 4 * offset;
782#elif X86_DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
783 *((unsigned int *)(code + 8)) = 4 * offset;
784#elif X86_DISPATCH_FUNCTION_SIZE == 16
785 *((unsigned int *)(code + 7)) = 4 * offset;
786#else
787# error Invalid X86_DISPATCH_FUNCTION_SIZE!
788#endif
Brian Paul54f3aab2002-10-02 01:51:44 +0000789
790#elif defined(USE_SPARC_ASM)
791
792 /* XXX this hasn't been tested! */
793 unsigned int *code = (unsigned int *) entrypoint;
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000794#ifdef __arch64__
Brian Paul54f3aab2002-10-02 01:51:44 +0000795 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
796 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
797 code[6] |= ((offset * 8) >> 10);
798 code[7] |= ((offset * 8) & ((1 << 10) - 1));
799 __glapi_sparc_icache_flush(&code[6]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000800#else /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000801 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
Brian Paul944ea202002-10-17 16:29:17 +0000802 code[2] |= (offset * 4);
Brian Paul54f3aab2002-10-02 01:51:44 +0000803 __glapi_sparc_icache_flush(&code[2]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000804#endif /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000805
Brian Paula6c423d2004-08-25 15:59:48 +0000806#else
807
808 /* an unimplemented architecture */
809 (void) entrypoint;
810 (void) offset;
811
Brian Paul54f3aab2002-10-02 01:51:44 +0000812#endif /* USE_*_ASM */
813}
814
Brian Paul959f8022000-03-19 01:10:11 +0000815
Brian Paul767e15a2004-11-27 03:51:11 +0000816/**
Brian Paul959f8022000-03-19 01:10:11 +0000817 * Add a new extension function entrypoint.
818 * Return: GL_TRUE = success or GL_FALSE = failure
819 */
Adam Jackson791ce022004-12-15 23:14:29 +0000820PUBLIC GLboolean
Brian Paul959f8022000-03-19 01:10:11 +0000821_glapi_add_entrypoint(const char *funcName, GLuint offset)
822{
Brian Paul8ad10762002-10-11 17:41:03 +0000823 /* trivial rejection test */
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000824#ifdef MANGLE
825 if (!funcName || funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
Brian Paula6c423d2004-08-25 15:59:48 +0000826 return GL_FALSE;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000827#else
Brian Paul8ad10762002-10-11 17:41:03 +0000828 if (!funcName || funcName[0] != 'g' || funcName[1] != 'l')
829 return GL_FALSE;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000830#endif
Brian Paul8ad10762002-10-11 17:41:03 +0000831
Brian Paul959f8022000-03-19 01:10:11 +0000832 /* first check if the named function is already statically present */
833 {
834 GLint index = get_static_proc_offset(funcName);
835 if (index >= 0) {
Brian Paulb51b0a82001-03-07 05:06:11 +0000836 return (GLboolean) ((GLuint) index == offset); /* bad offset! */
Brian Paul959f8022000-03-19 01:10:11 +0000837 }
838 }
839
Brian Paul54f3aab2002-10-02 01:51:44 +0000840 /* See if this function has already been dynamically added */
Brian Paul959f8022000-03-19 01:10:11 +0000841 {
Brian Paul959f8022000-03-19 01:10:11 +0000842 GLuint i;
843 for (i = 0; i < NumExtEntryPoints; i++) {
844 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
Brian Paul54f3aab2002-10-02 01:51:44 +0000845 /* function already registered */
Brian Paul959f8022000-03-19 01:10:11 +0000846 if (ExtEntryTable[i].Offset == offset) {
847 return GL_TRUE; /* offsets match */
848 }
Brian Paule4fcea22003-09-19 15:38:15 +0000849 else if (ExtEntryTable[i].Offset == (GLuint) ~0
Brian Paul54f3aab2002-10-02 01:51:44 +0000850 && offset < DISPATCH_TABLE_SIZE) {
851 /* need to patch-up the dispatch code */
Brian Paule4fcea22003-09-19 15:38:15 +0000852 if (offset != (GLuint) ~0) {
Brian Paul54f3aab2002-10-02 01:51:44 +0000853 fill_in_entrypoint_offset(ExtEntryTable[i].Address, offset);
854 ExtEntryTable[i].Offset = offset;
855 }
856 return GL_TRUE;
857 }
Brian Paul959f8022000-03-19 01:10:11 +0000858 else {
859 return GL_FALSE; /* bad offset! */
860 }
861 }
862 }
Brian Paul959f8022000-03-19 01:10:11 +0000863 }
864
Brian Paul8ad10762002-10-11 17:41:03 +0000865 /* This is a new function, try to add it. */
866 if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS ||
867 offset >= DISPATCH_TABLE_SIZE) {
868 /* No space left */
869 return GL_FALSE;
870 }
871 else {
Brian Paul767e15a2004-11-27 03:51:11 +0000872 _glapi_proc entrypoint = generate_entrypoint(offset);
Brian Paul8ad10762002-10-11 17:41:03 +0000873 if (!entrypoint)
874 return GL_FALSE; /* couldn't generate assembly */
875
876 /* OK! */
877 ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
878 ExtEntryTable[NumExtEntryPoints].Offset = offset;
879 ExtEntryTable[NumExtEntryPoints].Address = entrypoint;
880 NumExtEntryPoints++;
881
882 return GL_TRUE; /* success */
883 }
884
885 /* should never get here, silence compiler warnings */
Brian Paul959f8022000-03-19 01:10:11 +0000886 return GL_FALSE;
887}
888
889
Brian Paul767e15a2004-11-27 03:51:11 +0000890/**
Brian Paul959f8022000-03-19 01:10:11 +0000891 * Return offset of entrypoint for named function within dispatch table.
892 */
Adam Jackson791ce022004-12-15 23:14:29 +0000893PUBLIC GLint
Brian Paul959f8022000-03-19 01:10:11 +0000894_glapi_get_proc_offset(const char *funcName)
895{
896 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000897 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000898 for (i = 0; i < NumExtEntryPoints; i++) {
899 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
900 return ExtEntryTable[i].Offset;
901 }
902 }
903
904 /* search static functions */
905 return get_static_proc_offset(funcName);
906}
907
908
909
Brian Paul767e15a2004-11-27 03:51:11 +0000910/**
911 * Return pointer to the named function. If the function name isn't found
912 * in the name of static functions, try generating a new API entrypoint on
913 * the fly with assembly language.
Brian Paul959f8022000-03-19 01:10:11 +0000914 */
Keith Whitwell5cef1a12005-04-28 13:16:23 +0000915PUBLIC _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000916_glapi_get_proc_address(const char *funcName)
917{
Brian Paulb51b0a82001-03-07 05:06:11 +0000918 GLuint i;
Brian Paula6ed6f42003-08-27 14:48:16 +0000919
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000920#ifdef MANGLE
921 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
922 return NULL;
923#else
Brian Paula6ed6f42003-08-27 14:48:16 +0000924 if (funcName[0] != 'g' || funcName[1] != 'l')
925 return NULL;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000926#endif
Brian Paula6ed6f42003-08-27 14:48:16 +0000927
928 /* search extension functions first */
Brian Paul959f8022000-03-19 01:10:11 +0000929 for (i = 0; i < NumExtEntryPoints; i++) {
930 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
931 return ExtEntryTable[i].Address;
932 }
933 }
934
935 /* search static functions */
Brian Paul54f3aab2002-10-02 01:51:44 +0000936 {
Brian Paul767e15a2004-11-27 03:51:11 +0000937 const _glapi_proc func = get_static_proc_address(funcName);
Brian Paul54f3aab2002-10-02 01:51:44 +0000938 if (func)
939 return func;
940 }
Brian Paul959f8022000-03-19 01:10:11 +0000941
Brian Paul54f3aab2002-10-02 01:51:44 +0000942 /* generate new entrypoint - use a temporary dispatch offset of
943 * ~0 (i.e. -1). Later, when the driver calls _glapi_add_entrypoint()
944 * we'll put in the proper offset. If that never happens, and the
945 * user calls this function, he'll segfault. That's what you get
946 * when you try calling a GL function that doesn't really exist.
947 */
948 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
Brian Paul767e15a2004-11-27 03:51:11 +0000949 _glapi_proc entrypoint = generate_entrypoint(~0);
Brian Paul54f3aab2002-10-02 01:51:44 +0000950 if (!entrypoint)
951 return GL_FALSE;
952
953 ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
954 ExtEntryTable[NumExtEntryPoints].Offset = ~0;
955 ExtEntryTable[NumExtEntryPoints].Address = entrypoint;
956 NumExtEntryPoints++;
957
958 return entrypoint;
959 }
960 else {
961 /* no space for new functions! */
962 return NULL;
963 }
964}
Brian Paul959f8022000-03-19 01:10:11 +0000965
966
967
Brian Paul767e15a2004-11-27 03:51:11 +0000968/**
Brian Paul959f8022000-03-19 01:10:11 +0000969 * Return the name of the function at the given dispatch offset.
970 * This is only intended for debugging.
971 */
Adam Jackson791ce022004-12-15 23:14:29 +0000972PUBLIC const char *
Brian Paul959f8022000-03-19 01:10:11 +0000973_glapi_get_proc_name(GLuint offset)
974{
Brian Paul959f8022000-03-19 01:10:11 +0000975 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000976 const char * n;
Brian Paul54f3aab2002-10-02 01:51:44 +0000977
978 /* search built-in functions */
Ian Romanick78677992004-05-27 00:05:13 +0000979 n = get_static_proc_name(offset);
980 if ( n != NULL ) {
981 return n;
Brian Paul959f8022000-03-19 01:10:11 +0000982 }
983
984 /* search added extension functions */
985 for (i = 0; i < NumExtEntryPoints; i++) {
986 if (ExtEntryTable[i].Offset == offset) {
987 return ExtEntryTable[i].Name;
988 }
989 }
990 return NULL;
991}
992
993
994
Brian Paul767e15a2004-11-27 03:51:11 +0000995/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000996 * Return size of dispatch table struct as number of functions (or
997 * slots).
998 */
Adam Jackson791ce022004-12-15 23:14:29 +0000999PUBLIC GLuint
Brian Paul54f3aab2002-10-02 01:51:44 +00001000_glapi_get_dispatch_table_size(void)
1001{
1002 return DISPATCH_TABLE_SIZE;
1003}
1004
1005
1006
Brian Paul767e15a2004-11-27 03:51:11 +00001007/**
Brian Paul54f3aab2002-10-02 01:51:44 +00001008 * Get API dispatcher version string.
1009 */
Adam Jackson791ce022004-12-15 23:14:29 +00001010PUBLIC const char *
Brian Paul54f3aab2002-10-02 01:51:44 +00001011_glapi_get_version(void)
1012{
1013 return "20021001"; /* YYYYMMDD */
1014}
1015
1016
1017
Brian Paul767e15a2004-11-27 03:51:11 +00001018/**
Brian Paul959f8022000-03-19 01:10:11 +00001019 * Make sure there are no NULL pointers in the given dispatch table.
Brian Paul5104b4d2002-03-07 21:50:41 +00001020 * Intended for debugging purposes.
Brian Paul959f8022000-03-19 01:10:11 +00001021 */
Adam Jackson791ce022004-12-15 23:14:29 +00001022PUBLIC void
Brian Paul959f8022000-03-19 01:10:11 +00001023_glapi_check_table(const struct _glapi_table *table)
1024{
Brian Paul5104b4d2002-03-07 21:50:41 +00001025#ifdef DEBUG
Brian Paul959f8022000-03-19 01:10:11 +00001026 const GLuint entries = _glapi_get_dispatch_table_size();
1027 const void **tab = (const void **) table;
1028 GLuint i;
1029 for (i = 1; i < entries; i++) {
1030 assert(tab[i]);
1031 }
1032
Brian Paul959f8022000-03-19 01:10:11 +00001033 /* Do some spot checks to be sure that the dispatch table
1034 * slots are assigned correctly.
1035 */
1036 {
1037 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
1038 char *BeginFunc = (char*) &table->Begin;
1039 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
1040 assert(BeginOffset == _gloffset_Begin);
1041 assert(BeginOffset == offset);
1042 }
1043 {
1044 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
1045 char *viewportFunc = (char*) &table->Viewport;
1046 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
1047 assert(viewportOffset == _gloffset_Viewport);
1048 assert(viewportOffset == offset);
1049 }
1050 {
1051 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
1052 char *VertexPointerFunc = (char*) &table->VertexPointer;
1053 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
1054 assert(VertexPointerOffset == _gloffset_VertexPointer);
1055 assert(VertexPointerOffset == offset);
1056 }
1057 {
1058 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
1059 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
1060 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
1061 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
1062 assert(ResetMinMaxOffset == offset);
1063 }
1064 {
1065 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
1066 char *blendColorFunc = (char*) &table->BlendColor;
1067 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
1068 assert(blendColorOffset == _gloffset_BlendColor);
1069 assert(blendColorOffset == offset);
1070 }
1071 {
1072 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
1073 char *istextureFunc = (char*) &table->IsTextureEXT;
1074 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
1075 assert(istextureOffset == _gloffset_IsTextureEXT);
1076 assert(istextureOffset == offset);
1077 }
Brian Paula14cbff2000-10-27 18:31:21 +00001078 {
1079 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
1080 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1081 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1082 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1083 assert(secondaryColor3fOffset == offset);
Brian Paul767e15a2004-11-27 03:51:11 +00001084 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
Brian Paula14cbff2000-10-27 18:31:21 +00001085 }
Brian Paul91d6f122002-05-29 15:23:16 +00001086 {
1087 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1088 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1089 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1090 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1091 assert(pointParameterivOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001092 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
Brian Paul91d6f122002-05-29 15:23:16 +00001093 }
Brian Paul54f3aab2002-10-02 01:51:44 +00001094 {
1095 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1096 char *setFenceFunc = (char*) &table->SetFenceNV;
1097 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1098 assert(setFenceOffset == _gloffset_SetFenceNV);
1099 assert(setFenceOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001100 assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc) &glSetFenceNV);
Brian Paul54f3aab2002-10-02 01:51:44 +00001101 }
Brian Paula6c423d2004-08-25 15:59:48 +00001102#else
1103 (void) table;
Brian Paul959f8022000-03-19 01:10:11 +00001104#endif
1105}
Ian Romanick25fe93f2005-04-13 20:59:15 +00001106
1107
1108/**
1109 * Perform platform-specific GL API entry-point fixups.
1110 *
1111 *
1112 */
1113static void
1114init_glapi_relocs( void )
1115{
1116#if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1117 extern void * _x86_get_dispatch(void);
1118 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1119 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1120
1121
1122 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1123 (void) memcpy( curr_func, get_disp, 6 );
1124 curr_func += X86_DISPATCH_FUNCTION_SIZE;
1125 }
1126#endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1127}