2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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.
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.
38 * NOTE: There are no dependencies on Mesa in this code.
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
43 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
44 * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints
45 * itself (using offset ~0). _glapi_add_entrypoint() can be
46 * called afterward and it'll fill in the correct dispatch
47 * offset. This allows DRI libGL to avoid probing for DRI
48 * drivers! No changes to the public glapi interface.
53 #ifdef HAVE_DIX_CONFIG_H
55 #include <dix-config.h>
60 #include "main/glheader.h"
64 #include "main/compiler.h"
73 #include "glapioffsets.h"
74 #include "glapitable.h"
77 /***** BEGIN NO-OP DISPATCH *****/
79 static GLboolean WarnFlag
= GL_FALSE
;
80 static _glapi_warning_func warning_func
;
83 * Enable/disable printing of warning messages.
86 _glapi_noop_enable_warnings(GLboolean enable
)
92 * Register a callback function for reporting errors.
95 _glapi_set_warning_func( _glapi_warning_func func
)
103 #if !defined(_WIN32_WCE)
104 if ((WarnFlag
|| getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
117 #define KEYWORD1 static
118 #define KEYWORD1_ALT static
119 #define KEYWORD2 GLAPIENTRY
120 #define NAME(func) NoOp##func
124 #define DISPATCH(func, args, msg) \
126 warning_func(NULL, "GL User Error: called without context: %s", #func); \
129 #define RETURN_DISPATCH(func, args, msg) \
131 warning_func(NULL, "GL User Error: called without context: %s", #func); \
135 #define DISPATCH_TABLE_NAME __glapi_noop_table
136 #define UNUSED_TABLE_NAME __unused_noop_functions
138 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
140 static GLint
NoOpUnused(void)
143 warning_func(NULL
, "GL User Error: calling extension function without a current context\n");
148 #include "glapitemp.h"
150 /***** END NO-OP DISPATCH *****/
155 * \name Current dispatch and current context control variables
157 * Depending on whether or not multithreading is support, and the type of
158 * support available, several variables are used to store the current context
159 * pointer and the current dispatch table pointer. In the non-threaded case,
160 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
163 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
164 * \c _glapi_Context will be \c NULL if an application is detected as being
165 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
166 * and \c _glapi_Context just like the case without any threading support.
167 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
168 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
169 * static dispatch functions access these variables via \c _glapi_get_dispatch
170 * and \c _glapi_get_context.
172 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
173 * possible for the original thread to be setting it at the same instant a new
174 * thread, perhaps running on a different processor, is clearing it. Because
175 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
176 * used to determine whether or not the application is multithreaded.
178 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
179 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
180 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
181 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
182 * between TLS enabled loaders and non-TLS DRI drivers.
185 #if defined(GLX_USE_TLS)
187 PUBLIC __thread
struct _glapi_table
* _glapi_tls_Dispatch
188 __attribute__((tls_model("initial-exec")))
189 = (struct _glapi_table
*) __glapi_noop_table
;
191 PUBLIC __thread
void * _glapi_tls_Context
192 __attribute__((tls_model("initial-exec")));
194 PUBLIC
const struct _glapi_table
*_glapi_Dispatch
= NULL
;
195 PUBLIC
const void *_glapi_Context
= NULL
;
201 _glthread_DECLARE_STATIC_MUTEX(ThreadCheckMutex
);
202 static GLboolean ThreadSafe
= GL_FALSE
; /**< In thread-safe mode? */
203 _glthread_TSD _gl_DispatchTSD
; /**< Per-thread dispatch pointer */
204 static _glthread_TSD ContextTSD
; /**< Per-thread context pointer */
206 #if defined(WIN32_THREADS)
207 void FreeTSD(_glthread_TSD
*p
);
208 void FreeAllTSD(void)
210 FreeTSD(&_gl_DispatchTSD
);
211 FreeTSD(&ContextTSD
);
213 #endif /* defined(WIN32_THREADS) */
215 #endif /* defined(THREADS) */
217 PUBLIC
struct _glapi_table
*_glapi_Dispatch
=
218 (struct _glapi_table
*) __glapi_noop_table
;
219 PUBLIC
void *_glapi_Context
= NULL
;
221 #endif /* defined(GLX_USE_TLS) */
228 * We should call this periodically from a function such as glXMakeCurrent
229 * in order to test if multiple threads are being used.
232 _glapi_check_multithread(void)
234 #if defined(THREADS) && !defined(GLX_USE_TLS)
235 static unsigned long knownID
;
236 static GLboolean firstCall
= GL_TRUE
;
241 _glthread_LOCK_MUTEX(ThreadCheckMutex
);
243 knownID
= _glthread_GetID();
244 firstCall
= GL_FALSE
;
246 else if (knownID
!= _glthread_GetID()) {
247 ThreadSafe
= GL_TRUE
;
248 _glapi_set_dispatch(NULL
);
249 _glapi_set_context(NULL
);
251 _glthread_UNLOCK_MUTEX(ThreadCheckMutex
);
258 * Set the current context pointer for this thread.
259 * The context pointer is an opaque type which should be cast to
260 * void from the real context pointer type.
263 _glapi_set_context(void *context
)
265 (void) __unused_noop_functions
; /* silence a warning */
266 #if defined(GLX_USE_TLS)
267 _glapi_tls_Context
= context
;
268 #elif defined(THREADS)
269 _glthread_SetTSD(&ContextTSD
, context
);
270 _glapi_Context
= (ThreadSafe
) ? NULL
: context
;
272 _glapi_Context
= context
;
279 * Get the current context pointer for this thread.
280 * The context pointer is an opaque type which should be cast from
281 * void to the real context pointer type.
284 _glapi_get_context(void)
286 #if defined(GLX_USE_TLS)
287 return _glapi_tls_Context
;
288 #elif defined(THREADS)
290 return _glthread_GetTSD(&ContextTSD
);
293 return _glapi_Context
;
296 return _glapi_Context
;
302 #if defined( GLX_USE_TLS )
303 extern GLubyte gl_dispatch_functions_start
[];
304 extern GLubyte gl_dispatch_functions_end
[];
306 extern const GLubyte gl_dispatch_functions_start
[];
309 #endif /* USE_X86_ASM */
312 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
313 # define DISPATCH_FUNCTION_SIZE 16
314 #elif defined(USE_X86_ASM)
315 # if defined(THREADS) && !defined(GLX_USE_TLS)
316 # define DISPATCH_FUNCTION_SIZE 32
318 # define DISPATCH_FUNCTION_SIZE 16
324 extern unsigned int __glapi_sparc_tls_stub
;
326 extern unsigned int __glapi_sparc_pthread_stub
;
330 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
331 # define NEED_FUNCTION_POINTER
334 #if defined(PTHREADS) || defined(GLX_USE_TLS)
336 * Perform platform-specific GL API entry-point fixups.
339 init_glapi_relocs( void )
341 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
342 extern unsigned long _x86_get_dispatch(void);
343 char run_time_patch
[] = {
344 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
346 GLuint
*offset
= (GLuint
*) &run_time_patch
[2]; /* 32-bits for x86/32 */
347 const GLubyte
* const get_disp
= (const GLubyte
*) run_time_patch
;
348 GLubyte
* curr_func
= (GLubyte
*) gl_dispatch_functions_start
;
350 *offset
= _x86_get_dispatch();
351 while ( curr_func
!= (GLubyte
*) gl_dispatch_functions_end
) {
352 (void) memcpy( curr_func
, get_disp
, sizeof(run_time_patch
));
353 curr_func
+= DISPATCH_FUNCTION_SIZE
;
357 extern void __glapi_sparc_icache_flush(unsigned int *);
358 static const unsigned int template[] = {
360 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
361 0x8730e00a, /* srl %g3, 10, %g3 */
362 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
364 0xc259c002, /* ldx [%g7 + %g2], %g1 */
365 0xc2584003, /* ldx [%g1 + %g3], %g1 */
367 0xc201c002, /* ld [%g7 + %g2], %g1 */
368 0xc2004003, /* ld [%g1 + %g3], %g1 */
370 0x81c04000, /* jmp %g1 */
371 0x01000000, /* nop */
374 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
375 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
376 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
377 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
378 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
379 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
380 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
382 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
383 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
384 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
386 0x80a06000, /* --> cmp %g1, 0 */
387 0x02800005, /* --> be +4*5 */
388 0x01000000, /* --> nop */
390 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
392 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
394 0x81c04000, /* --> jmp %g1 */
395 0x01000000, /* --> nop */
397 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
399 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
401 0xa0100003, /* --> mov %g3, %l0 */
402 0x40000000, /* --> call _glapi_get_dispatch */
403 0x01000000, /* --> nop */
404 0x82100008, /* --> mov %o0, %g1 */
405 0x86100010, /* --> mov %l0, %g3 */
406 0x10bffff7, /* --> ba -4*9 */
407 0x81e80000, /* --> restore */
411 extern unsigned long __glapi_sparc_get_dispatch(void);
412 unsigned int *code
= &__glapi_sparc_tls_stub
;
413 unsigned long dispatch
= __glapi_sparc_get_dispatch();
415 unsigned int *code
= &__glapi_sparc_pthread_stub
;
416 unsigned long dispatch
= (unsigned long) &_glapi_Dispatch
;
417 unsigned long call_dest
= (unsigned long ) &_glapi_get_dispatch
;
421 #if defined(GLX_USE_TLS)
422 code
[0] = template[0] | (dispatch
>> 10);
423 code
[1] = template[1];
424 __glapi_sparc_icache_flush(&code
[0]);
425 code
[2] = template[2] | (dispatch
& 0x3ff);
426 code
[3] = template[3];
427 __glapi_sparc_icache_flush(&code
[2]);
428 code
[4] = template[4];
429 code
[5] = template[5];
430 __glapi_sparc_icache_flush(&code
[4]);
431 code
[6] = template[6];
432 __glapi_sparc_icache_flush(&code
[6]);
434 #if defined(__arch64__)
435 code
[0] = template[0] | (dispatch
>> (32 + 10));
436 code
[1] = template[1] | ((dispatch
& 0xffffffff) >> 10);
437 __glapi_sparc_icache_flush(&code
[0]);
438 code
[2] = template[2] | ((dispatch
>> 32) & 0x3ff);
439 code
[3] = template[3];
440 __glapi_sparc_icache_flush(&code
[2]);
441 code
[4] = template[4];
442 code
[5] = template[5];
443 __glapi_sparc_icache_flush(&code
[4]);
444 code
[6] = template[6] | (dispatch
& 0x3ff);
447 code
[0] = template[0] | (dispatch
>> 10);
448 code
[1] = template[1];
449 __glapi_sparc_icache_flush(&code
[0]);
450 code
[2] = template[2] | (dispatch
& 0x3ff);
453 code
[idx
+ 0] = template[idx
+ 0];
454 __glapi_sparc_icache_flush(&code
[idx
- 1]);
455 code
[idx
+ 1] = template[idx
+ 1];
456 code
[idx
+ 2] = template[idx
+ 2];
457 __glapi_sparc_icache_flush(&code
[idx
+ 1]);
458 code
[idx
+ 3] = template[idx
+ 3];
459 code
[idx
+ 4] = template[idx
+ 4];
460 __glapi_sparc_icache_flush(&code
[idx
+ 3]);
461 code
[idx
+ 5] = template[idx
+ 5];
462 code
[idx
+ 6] = template[idx
+ 6];
463 __glapi_sparc_icache_flush(&code
[idx
+ 5]);
464 code
[idx
+ 7] = template[idx
+ 7];
465 code
[idx
+ 8] = template[idx
+ 8] |
466 (((call_dest
- ((unsigned long) &code
[idx
+ 8]))
468 __glapi_sparc_icache_flush(&code
[idx
+ 7]);
469 code
[idx
+ 9] = template[idx
+ 9];
470 code
[idx
+ 10] = template[idx
+ 10];
471 __glapi_sparc_icache_flush(&code
[idx
+ 9]);
472 code
[idx
+ 11] = template[idx
+ 11];
473 code
[idx
+ 12] = template[idx
+ 12];
474 __glapi_sparc_icache_flush(&code
[idx
+ 11]);
475 code
[idx
+ 13] = template[idx
+ 13];
476 __glapi_sparc_icache_flush(&code
[idx
+ 13]);
480 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
484 * Set the global or per-thread dispatch table pointer.
485 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
486 * table (__glapi_noop_table).
489 _glapi_set_dispatch(struct _glapi_table
*dispatch
)
491 #if defined(PTHREADS) || defined(GLX_USE_TLS)
492 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
493 pthread_once( & once_control
, init_glapi_relocs
);
497 /* use the no-op functions */
498 dispatch
= (struct _glapi_table
*) __glapi_noop_table
;
502 _glapi_check_table(dispatch
);
506 #if defined(GLX_USE_TLS)
507 _glapi_tls_Dispatch
= dispatch
;
508 #elif defined(THREADS)
509 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) dispatch
);
510 _glapi_Dispatch
= (ThreadSafe
) ? NULL
: dispatch
;
512 _glapi_Dispatch
= dispatch
;
519 * Return pointer to current dispatch table for calling thread.
521 PUBLIC
struct _glapi_table
*
522 _glapi_get_dispatch(void)
524 struct _glapi_table
* api
;
525 #if defined(GLX_USE_TLS)
526 api
= _glapi_tls_Dispatch
;
527 #elif defined(THREADS)
529 ? (struct _glapi_table
*) _glthread_GetTSD(&_gl_DispatchTSD
)
532 api
= _glapi_Dispatch
;
541 * The dispatch table size (number of entries) is the size of the
542 * _glapi_table struct plus the number of dynamic entries we can add.
543 * The extra slots can be filled in by DRI drivers that register new extension
546 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
550 * Return size of dispatch table struct as number of functions (or
554 _glapi_get_dispatch_table_size(void)
556 return DISPATCH_TABLE_SIZE
;
561 * Make sure there are no NULL pointers in the given dispatch table.
562 * Intended for debugging purposes.
565 _glapi_check_table(const struct _glapi_table
*table
)
568 const GLuint entries
= _glapi_get_dispatch_table_size();
569 const void **tab
= (const void **) table
;
571 for (i
= 1; i
< entries
; i
++) {
575 /* Do some spot checks to be sure that the dispatch table
576 * slots are assigned correctly.
579 GLuint BeginOffset
= _glapi_get_proc_offset("glBegin");
580 char *BeginFunc
= (char*) &table
->Begin
;
581 GLuint offset
= (BeginFunc
- (char *) table
) / sizeof(void *);
582 assert(BeginOffset
== _gloffset_Begin
);
583 assert(BeginOffset
== offset
);
586 GLuint viewportOffset
= _glapi_get_proc_offset("glViewport");
587 char *viewportFunc
= (char*) &table
->Viewport
;
588 GLuint offset
= (viewportFunc
- (char *) table
) / sizeof(void *);
589 assert(viewportOffset
== _gloffset_Viewport
);
590 assert(viewportOffset
== offset
);
593 GLuint VertexPointerOffset
= _glapi_get_proc_offset("glVertexPointer");
594 char *VertexPointerFunc
= (char*) &table
->VertexPointer
;
595 GLuint offset
= (VertexPointerFunc
- (char *) table
) / sizeof(void *);
596 assert(VertexPointerOffset
== _gloffset_VertexPointer
);
597 assert(VertexPointerOffset
== offset
);
600 GLuint ResetMinMaxOffset
= _glapi_get_proc_offset("glResetMinmax");
601 char *ResetMinMaxFunc
= (char*) &table
->ResetMinmax
;
602 GLuint offset
= (ResetMinMaxFunc
- (char *) table
) / sizeof(void *);
603 assert(ResetMinMaxOffset
== _gloffset_ResetMinmax
);
604 assert(ResetMinMaxOffset
== offset
);
607 GLuint blendColorOffset
= _glapi_get_proc_offset("glBlendColor");
608 char *blendColorFunc
= (char*) &table
->BlendColor
;
609 GLuint offset
= (blendColorFunc
- (char *) table
) / sizeof(void *);
610 assert(blendColorOffset
== _gloffset_BlendColor
);
611 assert(blendColorOffset
== offset
);
614 GLuint secondaryColor3fOffset
= _glapi_get_proc_offset("glSecondaryColor3fEXT");
615 char *secondaryColor3fFunc
= (char*) &table
->SecondaryColor3fEXT
;
616 GLuint offset
= (secondaryColor3fFunc
- (char *) table
) / sizeof(void *);
617 assert(secondaryColor3fOffset
== _gloffset_SecondaryColor3fEXT
);
618 assert(secondaryColor3fOffset
== offset
);
621 GLuint pointParameterivOffset
= _glapi_get_proc_offset("glPointParameterivNV");
622 char *pointParameterivFunc
= (char*) &table
->PointParameterivNV
;
623 GLuint offset
= (pointParameterivFunc
- (char *) table
) / sizeof(void *);
624 assert(pointParameterivOffset
== _gloffset_PointParameterivNV
);
625 assert(pointParameterivOffset
== offset
);
628 GLuint setFenceOffset
= _glapi_get_proc_offset("glSetFenceNV");
629 char *setFenceFunc
= (char*) &table
->SetFenceNV
;
630 GLuint offset
= (setFenceFunc
- (char *) table
) / sizeof(void *);
631 assert(setFenceOffset
== _gloffset_SetFenceNV
);
632 assert(setFenceOffset
== offset
);