2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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.
55 #include "glapioffsets.h"
56 #include "glapitable.h"
59 /***** BEGIN NO-OP DISPATCH *****/
61 static GLboolean WarnFlag
= GL_FALSE
;
62 static _glapi_warning_func warning_func
;
64 static void init_glapi_relocs(void);
66 static _glapi_proc
generate_entrypoint(GLuint functionOffset
);
67 static void fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
);
70 * Enable/disable printing of warning messages.
73 _glapi_noop_enable_warnings(GLboolean enable
)
79 * Register a callback function for reporting errors.
82 _glapi_set_warning_func( _glapi_warning_func func
)
90 if ((WarnFlag
|| getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
100 #define KEYWORD1 static
101 #define KEYWORD2 GLAPIENTRY
102 #define NAME(func) NoOp##func
106 #define DISPATCH(func, args, msg) \
108 warning_func(NULL, "GL User Error: called without context: %s", #func); \
111 #define RETURN_DISPATCH(func, args, msg) \
113 warning_func(NULL, "GL User Error: called without context: %s", #func); \
117 #define DISPATCH_TABLE_NAME __glapi_noop_table
118 #define UNUSED_TABLE_NAME __unused_noop_functions
120 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
122 static GLint
NoOpUnused(void)
125 warning_func(NULL
, "GL User Error: calling extension function without a current context\n");
130 #include "glapitemp.h"
132 /***** END NO-OP DISPATCH *****/
136 /***** BEGIN THREAD-SAFE DISPATCH *****/
140 #if defined(GLX_USE_TLS)
142 __thread
struct _glapi_table
* _glapi_tls_Dispatch
143 __attribute__((tls_model("initial-exec")))
144 = (struct _glapi_table
*) __glapi_noop_table
;
146 static __thread
struct _glapi_table
* _glapi_tls_RealDispatch
147 __attribute__((tls_model("initial-exec")))
148 = (struct _glapi_table
*) __glapi_noop_table
;
150 __thread
void * _glapi_tls_Context
151 __attribute__((tls_model("initial-exec")));
154 * Legacy per-thread dispatch pointer. This is only needed to support
155 * non-TLS DRI drivers.
158 _glthread_TSD _gl_DispatchTSD
;
163 * \name Multi-threaded control support variables
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.
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
181 * There is a race condition in setting \c _glapi_DispatchTSD to \c NULL.
182 * It is possible for the original thread to be setting it at the same instant
183 * a new thread, perhaps running on a different processor, is clearing it.
184 * Because of that, \c ThreadSafe, which can only ever be changed to
185 * \c GL_TRUE, is used to determine whether or not the application is
189 static GLboolean ThreadSafe
= GL_FALSE
; /**< In thread-safe mode? */
190 _glthread_TSD _gl_DispatchTSD
; /**< Per-thread dispatch pointer */
191 static _glthread_TSD RealDispatchTSD
; /**< only when using override */
192 static _glthread_TSD ContextTSD
; /**< Per-thread context pointer */
195 #endif /* defined(GLX_USE_TLS) */
197 #define DISPATCH_TABLE_NAME __glapi_threadsafe_table
198 #define UNUSED_TABLE_NAME __unused_threadsafe_functions
200 #define TABLE_ENTRY(name) (_glapi_proc) gl##name
202 static GLint
glUnused(void)
207 #include "glapitemp.h"
211 /***** END THREAD-SAFE DISPATCH *****/
214 #if defined(GLX_USE_TLS)
217 * \name Old dispatch pointers
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.
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.
231 PUBLIC
const struct _glapi_table
*_glapi_Dispatch
= (struct _glapi_table
*) __glapi_threadsafe_table
;
232 PUBLIC
const struct _glapi_table
*_glapi_DispatchTSD
= NULL
;
233 PUBLIC
const void *_glapi_Context
= NULL
;
238 PUBLIC
struct _glapi_table
*_glapi_Dispatch
= (struct _glapi_table
*) __glapi_noop_table
;
239 #if defined( THREADS )
240 PUBLIC
struct _glapi_table
*_glapi_DispatchTSD
= (struct _glapi_table
*) __glapi_noop_table
;
242 PUBLIC
struct _glapi_table
*_glapi_RealDispatch
= (struct _glapi_table
*) __glapi_noop_table
;
244 /* Used when thread safety disabled */
245 PUBLIC
void *_glapi_Context
= NULL
;
247 #endif /* defined(GLX_USE_TLS) */
250 static GLboolean DispatchOverride
= GL_FALSE
;
255 * strdup() is actually not a standard ANSI C or POSIX routine.
256 * Irix will not define it if ANSI mode is in effect.
259 str_dup(const char *str
)
262 copy
= (char*) malloc(strlen(str
) + 1);
272 * We should call this periodically from a function such as glXMakeCurrent
273 * in order to test if multiple threads are being used.
276 _glapi_check_multithread(void)
278 #if defined(THREADS) && !defined(GLX_USE_TLS)
280 static unsigned long knownID
;
281 static GLboolean firstCall
= GL_TRUE
;
283 knownID
= _glthread_GetID();
284 firstCall
= GL_FALSE
;
286 else if (knownID
!= _glthread_GetID()) {
287 ThreadSafe
= GL_TRUE
;
288 _glapi_set_dispatch(NULL
);
291 else if (!_glapi_get_dispatch()) {
292 /* make sure that this thread's dispatch pointer isn't null */
293 _glapi_set_dispatch(NULL
);
301 * Set the current context pointer for this thread.
302 * The context pointer is an opaque type which should be cast to
303 * void from the real context pointer type.
306 _glapi_set_context(void *context
)
308 (void) __unused_noop_functions
; /* silence a warning */
309 #if defined(GLX_USE_TLS)
310 _glapi_tls_Context
= context
;
311 #elif defined(THREADS)
312 (void) __unused_threadsafe_functions
; /* silence a warning */
313 _glthread_SetTSD(&ContextTSD
, context
);
314 _glapi_Context
= (ThreadSafe
) ? NULL
: context
;
316 _glapi_Context
= context
;
323 * Get the current context pointer for this thread.
324 * The context pointer is an opaque type which should be cast from
325 * void to the real context pointer type.
328 _glapi_get_context(void)
330 #if defined(GLX_USE_TLS)
331 return _glapi_tls_Context
;
332 #elif defined(THREADS)
334 return _glthread_GetTSD(&ContextTSD
);
337 return _glapi_Context
;
340 return _glapi_Context
;
347 * Set the global or per-thread dispatch table pointer.
350 _glapi_set_dispatch(struct _glapi_table
*dispatch
)
352 #if defined(PTHREADS) || defined(GLX_USE_TLS)
353 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
356 pthread_once( & once_control
, init_glapi_relocs
);
360 /* use the no-op functions */
361 dispatch
= (struct _glapi_table
*) __glapi_noop_table
;
365 _glapi_check_table(dispatch
);
369 #if defined(GLX_USE_TLS)
370 if (DispatchOverride
) {
371 _glapi_tls_RealDispatch
= dispatch
;
374 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) dispatch
);
375 _glapi_tls_Dispatch
= dispatch
;
377 #elif defined(THREADS)
378 if (DispatchOverride
) {
379 _glthread_SetTSD(&RealDispatchTSD
, (void *) dispatch
);
381 _glapi_RealDispatch
= (struct _glapi_table
*) __glapi_threadsafe_table
;
383 _glapi_RealDispatch
= dispatch
;
386 /* normal operation */
387 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) dispatch
);
389 _glapi_Dispatch
= (struct _glapi_table
*) __glapi_threadsafe_table
;
390 _glapi_DispatchTSD
= NULL
;
393 _glapi_Dispatch
= dispatch
;
394 _glapi_DispatchTSD
= dispatch
;
398 if (DispatchOverride
) {
399 _glapi_RealDispatch
= dispatch
;
402 _glapi_Dispatch
= dispatch
;
410 * Return pointer to current dispatch table for calling thread.
412 PUBLIC
struct _glapi_table
*
413 _glapi_get_dispatch(void)
415 #if defined(GLX_USE_TLS)
416 struct _glapi_table
* api
= (DispatchOverride
)
417 ? _glapi_tls_RealDispatch
: _glapi_tls_Dispatch
;
419 assert( api
!= NULL
);
421 #elif defined(THREADS)
423 if (DispatchOverride
) {
424 return (struct _glapi_table
*) _glthread_GetTSD(&RealDispatchTSD
);
427 return (struct _glapi_table
*) _glthread_GetTSD(&_gl_DispatchTSD
);
431 if (DispatchOverride
) {
432 assert(_glapi_RealDispatch
);
433 return _glapi_RealDispatch
;
436 assert(_glapi_DispatchTSD
);
437 return _glapi_DispatchTSD
;
441 return _glapi_Dispatch
;
447 * Notes on dispatch overrride:
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.
454 * libGLS (GL Stream library) is another agent that might use override.
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"
466 * Return: dispatch override layer number.
469 _glapi_begin_dispatch_override(struct _glapi_table
*override
)
471 struct _glapi_table
*real
= _glapi_get_dispatch();
473 assert(!DispatchOverride
); /* can't nest at this time */
474 DispatchOverride
= GL_TRUE
;
476 _glapi_set_dispatch(real
);
478 #if defined(GLX_USE_TLS)
479 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) override
);
480 _glapi_tls_Dispatch
= override
;
481 #elif defined(THREADS)
482 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) override
);
484 _glapi_Dispatch
= (struct _glapi_table
*) __glapi_threadsafe_table
;
485 _glapi_DispatchTSD
= NULL
;
488 _glapi_Dispatch
= override
;
489 _glapi_DispatchTSD
= override
;
492 _glapi_Dispatch
= override
;
499 _glapi_end_dispatch_override(int layer
)
501 struct _glapi_table
*real
= _glapi_get_dispatch();
503 DispatchOverride
= GL_FALSE
;
504 _glapi_set_dispatch(real
);
505 /* the rest of this isn't needed, just play it safe */
506 #if defined(GLX_USE_TLS)
507 _glapi_tls_RealDispatch
= NULL
;
509 # if defined(THREADS)
510 _glthread_SetTSD(&RealDispatchTSD
, NULL
);
512 _glapi_RealDispatch
= NULL
;
517 PUBLIC
struct _glapi_table
*
518 _glapi_get_override_dispatch(int layer
)
521 return _glapi_get_dispatch();
524 if (DispatchOverride
) {
525 #if defined(GLX_USE_TLS)
526 return (struct _glapi_table
*) _glapi_tls_Dispatch
;
527 #elif defined(THREADS)
528 return (struct _glapi_table
*) _glthread_GetTSD(&_gl_DispatchTSD
);
530 return _glapi_Dispatch
;
540 #if !defined( USE_X86_ASM )
541 #define NEED_FUNCTION_POINTER
544 /* The code in this file is auto-generated with Python */
549 * Search the table of static entrypoint functions for the named function
550 * and return the corresponding glprocs_table_t entry.
552 static const glprocs_table_t
*
553 find_entry( const char * n
)
557 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
558 const char * test_name
;
560 test_name
= gl_string_table
+ static_functions
[i
].Name_offset
;
561 if (strcmp(test_name
, n
) == 0) {
562 return & static_functions
[i
];
570 * Return dispatch table offset of the named static (built-in) function.
571 * Return -1 if function not found.
574 get_static_proc_offset(const char *funcName
)
576 const glprocs_table_t
* const f
= find_entry( funcName
);
587 #if defined( GLX_USE_TLS )
588 extern GLubyte gl_dispatch_functions_start
[];
589 extern GLubyte gl_dispatch_functions_end
[];
591 extern const GLubyte gl_dispatch_functions_start
[];
594 # if defined(THREADS) && !defined(GLX_USE_TLS)
595 # define X86_DISPATCH_FUNCTION_SIZE 32
597 # define X86_DISPATCH_FUNCTION_SIZE 16
602 * Return dispatch function address the named static (built-in) function.
603 * Return NULL if function not found.
605 static const _glapi_proc
606 get_static_proc_address(const char *funcName
)
608 const glprocs_table_t
* const f
= find_entry( funcName
);
611 return (_glapi_proc
) (gl_dispatch_functions_start
612 + (X86_DISPATCH_FUNCTION_SIZE
* f
->Offset
));
623 * Return pointer to the named static (built-in) function.
624 * \return NULL if function not found.
626 static const _glapi_proc
627 get_static_proc_address(const char *funcName
)
629 const glprocs_table_t
* const f
= find_entry( funcName
);
630 return ( f
!= NULL
) ? f
->Address
: NULL
;
633 #endif /* USE_X86_ASM */
637 * Return the name of the function at the given offset in the dispatch
638 * table. For debugging only.
641 get_static_proc_name( GLuint offset
)
645 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
646 if (static_functions
[i
].Offset
== offset
) {
647 return gl_string_table
+ static_functions
[i
].Name_offset
;
655 /**********************************************************************
656 * Extension function management.
660 * Number of extension functions which we can dynamically add at runtime.
662 #define MAX_EXTENSION_FUNCS 300
666 * The dispatch table size (number of entries) is the size of the
667 * _glapi_table struct plus the number of dynamic entries we can add.
668 * The extra slots can be filled in by DRI drivers that register new extension
671 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
674 struct name_address_offset
{
681 static struct name_address_offset ExtEntryTable
[MAX_EXTENSION_FUNCS
];
682 static GLuint NumExtEntryPoints
= 0;
685 extern void __glapi_sparc_icache_flush(unsigned int *);
689 * 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.
694 generate_entrypoint(GLuint functionOffset
)
696 #if defined(USE_X86_ASM)
697 /* 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.
701 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
);
706 if ( code
!= NULL
) {
707 (void) memcpy( code
, template_func
, X86_DISPATCH_FUNCTION_SIZE
);
708 fill_in_entrypoint_offset( (_glapi_proc
) code
, functionOffset
);
711 return (_glapi_proc
) code
;
712 #elif defined(USE_SPARC_ASM)
715 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 */
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 */
737 unsigned int *code
= (unsigned int *) malloc(sizeof(insn_template
));
738 unsigned long glapi_addr
= (unsigned long) &_glapi_Dispatch
;
740 memcpy(code
, insn_template
, sizeof(insn_template
));
743 code
[0] |= (glapi_addr
>> (32 + 10));
744 code
[1] |= ((glapi_addr
& 0xffffffff) >> 10);
745 __glapi_sparc_icache_flush(&code
[0]);
746 code
[2] |= ((glapi_addr
>> 32) & ((1 << 10) - 1));
747 code
[3] |= (glapi_addr
& ((1 << 10) - 1));
748 __glapi_sparc_icache_flush(&code
[2]);
749 code
[6] |= ((functionOffset
* 8) >> 10);
750 code
[7] |= ((functionOffset
* 8) & ((1 << 10) - 1));
751 __glapi_sparc_icache_flush(&code
[6]);
753 code
[0] |= (glapi_addr
>> 10);
754 code
[1] |= (glapi_addr
& ((1 << 10) - 1));
755 __glapi_sparc_icache_flush(&code
[0]);
756 code
[2] |= (functionOffset
* 4);
757 __glapi_sparc_icache_flush(&code
[2]);
760 return (_glapi_proc
) code
;
762 (void) functionOffset
;
764 #endif /* USE_*_ASM */
769 * This function inserts a new dispatch offset into the assembly language
770 * stub that was generated with the preceeding function.
773 fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
)
775 #if defined(USE_X86_ASM)
776 GLubyte
* const code
= (GLubyte
*) entrypoint
;
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
;
787 # error Invalid X86_DISPATCH_FUNCTION_SIZE!
790 #elif defined(USE_SPARC_ASM)
792 /* XXX this hasn't been tested! */
793 unsigned int *code
= (unsigned int *) entrypoint
;
795 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]);
800 #else /* __arch64__ */
801 code
[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
802 code
[2] |= (offset
* 4);
803 __glapi_sparc_icache_flush(&code
[2]);
804 #endif /* __arch64__ */
808 /* an unimplemented architecture */
812 #endif /* USE_*_ASM */
817 * Add a new extension function entrypoint.
818 * Return: GL_TRUE = success or GL_FALSE = failure
821 _glapi_add_entrypoint(const char *funcName
, GLuint offset
)
823 /* trivial rejection test */
825 if (!funcName
|| funcName
[0] != 'm' || funcName
[1] != 'g' || funcName
[2] != 'l')
828 if (!funcName
|| funcName
[0] != 'g' || funcName
[1] != 'l')
832 /* first check if the named function is already statically present */
834 GLint index
= get_static_proc_offset(funcName
);
836 return (GLboolean
) ((GLuint
) index
== offset
); /* bad offset! */
840 /* See if this function has already been dynamically added */
843 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
844 if (strcmp(ExtEntryTable
[i
].Name
, funcName
) == 0) {
845 /* function already registered */
846 if (ExtEntryTable
[i
].Offset
== offset
) {
847 return GL_TRUE
; /* offsets match */
849 else if (ExtEntryTable
[i
].Offset
== (GLuint
) ~0
850 && offset
< DISPATCH_TABLE_SIZE
) {
851 /* need to patch-up the dispatch code */
852 if (offset
!= (GLuint
) ~0) {
853 fill_in_entrypoint_offset(ExtEntryTable
[i
].Address
, offset
);
854 ExtEntryTable
[i
].Offset
= offset
;
859 return GL_FALSE
; /* bad offset! */
865 /* This is a new function, try to add it. */
866 if (NumExtEntryPoints
>= MAX_EXTENSION_FUNCS
||
867 offset
>= DISPATCH_TABLE_SIZE
) {
872 _glapi_proc entrypoint
= generate_entrypoint(offset
);
874 return GL_FALSE
; /* couldn't generate assembly */
877 ExtEntryTable
[NumExtEntryPoints
].Name
= str_dup(funcName
);
878 ExtEntryTable
[NumExtEntryPoints
].Offset
= offset
;
879 ExtEntryTable
[NumExtEntryPoints
].Address
= entrypoint
;
882 return GL_TRUE
; /* success */
885 /* should never get here, silence compiler warnings */
891 * Return offset of entrypoint for named function within dispatch table.
894 _glapi_get_proc_offset(const char *funcName
)
896 /* search extension functions first */
898 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
899 if (strcmp(ExtEntryTable
[i
].Name
, funcName
) == 0) {
900 return ExtEntryTable
[i
].Offset
;
904 /* search static functions */
905 return get_static_proc_offset(funcName
);
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.
916 _glapi_get_proc_address(const char *funcName
)
921 if (funcName
[0] != 'm' || funcName
[1] != 'g' || funcName
[2] != 'l')
924 if (funcName
[0] != 'g' || funcName
[1] != 'l')
928 /* search extension functions first */
929 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
930 if (strcmp(ExtEntryTable
[i
].Name
, funcName
) == 0) {
931 return ExtEntryTable
[i
].Address
;
935 /* search static functions */
937 const _glapi_proc func
= get_static_proc_address(funcName
);
942 /* 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.
948 if (NumExtEntryPoints
< MAX_EXTENSION_FUNCS
) {
949 _glapi_proc entrypoint
= generate_entrypoint(~0);
953 ExtEntryTable
[NumExtEntryPoints
].Name
= str_dup(funcName
);
954 ExtEntryTable
[NumExtEntryPoints
].Offset
= ~0;
955 ExtEntryTable
[NumExtEntryPoints
].Address
= entrypoint
;
961 /* no space for new functions! */
969 * Return the name of the function at the given dispatch offset.
970 * This is only intended for debugging.
973 _glapi_get_proc_name(GLuint offset
)
978 /* search built-in functions */
979 n
= get_static_proc_name(offset
);
984 /* search added extension functions */
985 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
986 if (ExtEntryTable
[i
].Offset
== offset
) {
987 return ExtEntryTable
[i
].Name
;
996 * Return size of dispatch table struct as number of functions (or
1000 _glapi_get_dispatch_table_size(void)
1002 return DISPATCH_TABLE_SIZE
;
1008 * Get API dispatcher version string.
1011 _glapi_get_version(void)
1013 return "20021001"; /* YYYYMMDD */
1019 * Make sure there are no NULL pointers in the given dispatch table.
1020 * Intended for debugging purposes.
1023 _glapi_check_table(const struct _glapi_table
*table
)
1026 const GLuint entries
= _glapi_get_dispatch_table_size();
1027 const void **tab
= (const void **) table
;
1029 for (i
= 1; i
< entries
; i
++) {
1033 /* Do some spot checks to be sure that the dispatch table
1034 * slots are assigned correctly.
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
);
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
);
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
);
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
);
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
);
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
);
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
);
1084 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc
) &glSecondaryColor3fEXT
);
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
);
1092 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc
) &glPointParameterivNV
);
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
);
1100 assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc
) &glSetFenceNV
);
1109 * Perform platform-specific GL API entry-point fixups.
1114 init_glapi_relocs( void )
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
;
1122 while ( curr_func
!= (GLubyte
*) gl_dispatch_functions_end
) {
1123 (void) memcpy( curr_func
, get_disp
, 6 );
1124 curr_func
+= X86_DISPATCH_FUNCTION_SIZE
;
1126 #endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */