95edbc9071ed5ce940ce2229dda1b025ddf2c326
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * This file manages the OpenGL API dispatch layer.
28 * The dispatch table (struct _glapi_table) is basically just a list
29 * of function pointers.
30 * There are functions to set/get the current dispatch table for the
31 * current thread and to manage registration/dispatch of dynamically
32 * added extension functions.
33 *
34 * It's intended that this file and the other glapi*.[ch] files are
35 * flexible enough to be reused in several places: XFree86, DRI-
36 * based libGL.so, and perhaps the SGI SI.
37 *
38 * NOTE: There are no dependencies on Mesa in this code.
39 *
40 * Versions (API changes):
41 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
42 * 2001/01/16 - added dispatch override feature for Mesa 3.5
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.
49 */
50
51
52
53 #include "glheader.h"
54 #include "glapi.h"
55 #include "glapioffsets.h"
56 #include "glapitable.h"
57
58 /***** BEGIN NO-OP DISPATCH *****/
59
60 static GLboolean WarnFlag = GL_FALSE;
61 static _glapi_warning_func warning_func;
62
63 #if defined(PTHREADS) || defined(GLX_USE_TLS)
64 static void init_glapi_relocs(void);
65 #endif
66
67 static _glapi_proc generate_entrypoint(GLuint functionOffset);
68 static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
69
70 /*
71 * Enable/disable printing of warning messages.
72 */
73 PUBLIC void
74 _glapi_noop_enable_warnings(GLboolean enable)
75 {
76 WarnFlag = enable;
77 }
78
79 /*
80 * Register a callback function for reporting errors.
81 */
82 PUBLIC void
83 _glapi_set_warning_func( _glapi_warning_func func )
84 {
85 warning_func = func;
86 }
87
88 static GLboolean
89 warn(void)
90 {
91 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
92 && warning_func) {
93 return GL_TRUE;
94 }
95 else {
96 return GL_FALSE;
97 }
98 }
99
100
101 #define KEYWORD1 static
102 #define KEYWORD1_ALT static
103 #define KEYWORD2 GLAPIENTRY
104 #define NAME(func) NoOp##func
105
106 #define F NULL
107
108 #define DISPATCH(func, args, msg) \
109 if (warn()) { \
110 warning_func(NULL, "GL User Error: called without context: %s", #func); \
111 }
112
113 #define RETURN_DISPATCH(func, args, msg) \
114 if (warn()) { \
115 warning_func(NULL, "GL User Error: called without context: %s", #func); \
116 } \
117 return 0
118
119 #define DISPATCH_TABLE_NAME __glapi_noop_table
120 #define UNUSED_TABLE_NAME __unused_noop_functions
121
122 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
123
124 static GLint NoOpUnused(void)
125 {
126 if (warn()) {
127 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
128 }
129 return 0;
130 }
131
132 #include "glapitemp.h"
133
134 /***** END NO-OP DISPATCH *****/
135
136
137
138 /**
139 * \name Current dispatch and current context control variables
140 *
141 * Depending on whether or not multithreading is support, and the type of
142 * support available, several variables are used to store the current context
143 * pointer and the current dispatch table pointer. In the non-threaded case,
144 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
145 * purpose.
146 *
147 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
148 * \c _glapi_Context will be \c NULL if an application is detected as being
149 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
150 * and \c _glapi_Context just like the case without any threading support.
151 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
152 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
153 * static dispatch functions access these variables via \c _glapi_get_dispatch
154 * and \c _glapi_get_context.
155 *
156 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
157 * possible for the original thread to be setting it at the same instant a new
158 * thread, perhaps running on a different processor, is clearing it. Because
159 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
160 * used to determine whether or not the application is multithreaded.
161 *
162 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
163 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
164 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
165 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
166 * between TLS enabled loaders and non-TLS DRI drivers.
167 */
168 /*@{*/
169 #if defined(GLX_USE_TLS)
170
171 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
172 __attribute__((tls_model("initial-exec")))
173 = (struct _glapi_table *) __glapi_noop_table;
174
175 PUBLIC __thread void * _glapi_tls_Context
176 __attribute__((tls_model("initial-exec")));
177
178 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
179 PUBLIC const void *_glapi_Context = NULL;
180
181 #else
182
183 #if defined(THREADS)
184
185 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
186 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
187 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
188
189 #if defined(WIN32_THREADS)
190 void FreeTSD(_glthread_TSD *p);
191 void FreeAllTSD(void)
192 {
193 FreeTSD(&_gl_DispatchTSD);
194 FreeTSD(&ContextTSD);
195 }
196 #endif /* defined(WIN32_THREADS) */
197
198 #endif /* defined(THREADS) */
199
200 PUBLIC struct _glapi_table *_glapi_Dispatch =
201 (struct _glapi_table *) __glapi_noop_table;
202 PUBLIC void *_glapi_Context = NULL;
203
204 #endif /* defined(GLX_USE_TLS) */
205 /*@}*/
206
207
208 /**
209 * strdup() is actually not a standard ANSI C or POSIX routine.
210 * Irix will not define it if ANSI mode is in effect.
211 */
212 static char *
213 str_dup(const char *str)
214 {
215 char *copy;
216 copy = (char*) malloc(strlen(str) + 1);
217 if (!copy)
218 return NULL;
219 strcpy(copy, str);
220 return copy;
221 }
222
223
224
225 /**
226 * We should call this periodically from a function such as glXMakeCurrent
227 * in order to test if multiple threads are being used.
228 */
229 void
230 _glapi_check_multithread(void)
231 {
232 #if defined(THREADS) && !defined(GLX_USE_TLS)
233 if (!ThreadSafe) {
234 static unsigned long knownID;
235 static GLboolean firstCall = GL_TRUE;
236 if (firstCall) {
237 knownID = _glthread_GetID();
238 firstCall = GL_FALSE;
239 }
240 else if (knownID != _glthread_GetID()) {
241 ThreadSafe = GL_TRUE;
242 _glapi_set_dispatch(NULL);
243 }
244 }
245 else if (!_glapi_get_dispatch()) {
246 /* make sure that this thread's dispatch pointer isn't null */
247 _glapi_set_dispatch(NULL);
248 }
249 #endif
250 }
251
252
253
254 /**
255 * Set the current context pointer for this thread.
256 * The context pointer is an opaque type which should be cast to
257 * void from the real context pointer type.
258 */
259 PUBLIC void
260 _glapi_set_context(void *context)
261 {
262 (void) __unused_noop_functions; /* silence a warning */
263 #if defined(GLX_USE_TLS)
264 _glapi_tls_Context = context;
265 #elif defined(THREADS)
266 _glthread_SetTSD(&ContextTSD, context);
267 _glapi_Context = (ThreadSafe) ? NULL : context;
268 #else
269 _glapi_Context = context;
270 #endif
271 }
272
273
274
275 /**
276 * Get the current context pointer for this thread.
277 * The context pointer is an opaque type which should be cast from
278 * void to the real context pointer type.
279 */
280 PUBLIC void *
281 _glapi_get_context(void)
282 {
283 #if defined(GLX_USE_TLS)
284 return _glapi_tls_Context;
285 #elif defined(THREADS)
286 if (ThreadSafe) {
287 return _glthread_GetTSD(&ContextTSD);
288 }
289 else {
290 return _glapi_Context;
291 }
292 #else
293 return _glapi_Context;
294 #endif
295 }
296
297
298
299 /**
300 * Set the global or per-thread dispatch table pointer.
301 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
302 * table (__glapi_noop_table).
303 */
304 PUBLIC void
305 _glapi_set_dispatch(struct _glapi_table *dispatch)
306 {
307 #if defined(PTHREADS) || defined(GLX_USE_TLS)
308 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
309 pthread_once( & once_control, init_glapi_relocs );
310 #endif
311
312 if (!dispatch) {
313 /* use the no-op functions */
314 dispatch = (struct _glapi_table *) __glapi_noop_table;
315 }
316 #ifdef DEBUG
317 else {
318 _glapi_check_table(dispatch);
319 }
320 #endif
321
322 #if defined(GLX_USE_TLS)
323 _glapi_tls_Dispatch = dispatch;
324 #elif defined(THREADS)
325 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
326 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
327 #else /*THREADS*/
328 _glapi_Dispatch = dispatch;
329 #endif /*THREADS*/
330 }
331
332
333
334 /**
335 * Return pointer to current dispatch table for calling thread.
336 */
337 PUBLIC struct _glapi_table *
338 _glapi_get_dispatch(void)
339 {
340 struct _glapi_table * api;
341 #if defined(GLX_USE_TLS)
342 api = _glapi_tls_Dispatch;
343 #elif defined(THREADS)
344 api = (ThreadSafe)
345 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
346 : _glapi_Dispatch;
347 #else
348 api = _glapi_Dispatch;
349 #endif
350 return api;
351 }
352
353
354
355 /***
356 *** The rest of this file is pretty much concerned with GetProcAddress
357 *** functionality.
358 ***/
359
360 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
361 # define DISPATCH_FUNCTION_SIZE 16
362 #elif defined(USE_X86_ASM)
363 # if defined(THREADS) && !defined(GLX_USE_TLS)
364 # define DISPATCH_FUNCTION_SIZE 32
365 # else
366 # define DISPATCH_FUNCTION_SIZE 16
367 # endif
368 #endif
369
370 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
371 # define NEED_FUNCTION_POINTER
372 #endif
373
374 /* The code in this file is auto-generated with Python */
375 #include "glprocs.h"
376
377
378 /**
379 * Search the table of static entrypoint functions for the named function
380 * and return the corresponding glprocs_table_t entry.
381 */
382 static const glprocs_table_t *
383 find_entry( const char * n )
384 {
385 GLuint i;
386 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
387 const char *testName = gl_string_table + static_functions[i].Name_offset;
388 if (strcmp(testName, n) == 0) {
389 return &static_functions[i];
390 }
391 }
392 return NULL;
393 }
394
395
396 /**
397 * Return dispatch table offset of the named static (built-in) function.
398 * Return -1 if function not found.
399 */
400 static GLint
401 get_static_proc_offset(const char *funcName)
402 {
403 const glprocs_table_t * const f = find_entry( funcName );
404 if (f) {
405 return f->Offset;
406 }
407 return -1;
408 }
409
410
411 #if !defined(XFree86Server) && !defined(XGLServer)
412 #ifdef USE_X86_ASM
413
414 #if defined( GLX_USE_TLS )
415 extern GLubyte gl_dispatch_functions_start[];
416 extern GLubyte gl_dispatch_functions_end[];
417 #else
418 extern const GLubyte gl_dispatch_functions_start[];
419 #endif
420
421 #endif /* USE_X86_ASM */
422
423
424 /**
425 * Return dispatch function address for the named static (built-in) function.
426 * Return NULL if function not found.
427 */
428 static _glapi_proc
429 get_static_proc_address(const char *funcName)
430 {
431 const glprocs_table_t * const f = find_entry( funcName );
432 if (f) {
433 #ifdef DISPATCH_FUNCTION_SIZE
434 return (_glapi_proc) (gl_dispatch_functions_start
435 + (DISPATCH_FUNCTION_SIZE * f->Offset));
436 #else
437 return f->Address;
438 #endif
439 }
440 else {
441 return NULL;
442 }
443 }
444
445 #endif /* !defined(XFree86Server) && !defined(XGLServer) */
446
447
448
449 /**
450 * Return the name of the function at the given offset in the dispatch
451 * table. For debugging only.
452 */
453 static const char *
454 get_static_proc_name( GLuint offset )
455 {
456 GLuint i;
457 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
458 if (static_functions[i].Offset == offset) {
459 return gl_string_table + static_functions[i].Name_offset;
460 }
461 }
462 return NULL;
463 }
464
465
466
467 /**********************************************************************
468 * Extension function management.
469 */
470
471 /*
472 * Number of extension functions which we can dynamically add at runtime.
473 */
474 #define MAX_EXTENSION_FUNCS 300
475
476
477 /*
478 * The dispatch table size (number of entries) is the size of the
479 * _glapi_table struct plus the number of dynamic entries we can add.
480 * The extra slots can be filled in by DRI drivers that register new extension
481 * functions.
482 */
483 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
484
485
486 /**
487 * Track information about a function added to the GL API.
488 */
489 struct _glapi_function {
490 /**
491 * Name of the function.
492 */
493 const char * name;
494
495
496 /**
497 * Text string that describes the types of the parameters passed to the
498 * named function. Parameter types are converted to characters using the
499 * following rules:
500 * - 'i' for \c GLint, \c GLuint, and \c GLenum
501 * - 'p' for any pointer type
502 * - 'f' for \c GLfloat and \c GLclampf
503 * - 'd' for \c GLdouble and \c GLclampd
504 */
505 const char * parameter_signature;
506
507
508 /**
509 * Offset in the dispatch table where the pointer to the real function is
510 * located. If the driver has not requested that the named function be
511 * added to the dispatch table, this will have the value ~0.
512 */
513 unsigned dispatch_offset;
514
515
516 /**
517 * Pointer to the dispatch stub for the named function.
518 *
519 * \todo
520 * The semantic of this field should be changed slightly. Currently, it
521 * is always expected to be non-\c NULL. However, it would be better to
522 * only allocate the entry-point stub when the application requests the
523 * function via \c glXGetProcAddress. This would save memory for all the
524 * functions that the driver exports but that the application never wants
525 * to call.
526 */
527 _glapi_proc dispatch_stub;
528 };
529
530
531 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
532 static GLuint NumExtEntryPoints = 0;
533
534 #ifdef USE_SPARC_ASM
535 extern void __glapi_sparc_icache_flush(unsigned int *);
536 #endif
537
538 /**
539 * Generate a dispatch function (entrypoint) which jumps through
540 * the given slot number (offset) in the current dispatch table.
541 * We need assembly language in order to accomplish this.
542 */
543 static _glapi_proc
544 generate_entrypoint(GLuint functionOffset)
545 {
546 #if defined(USE_X86_ASM)
547 /* 32 is chosen as something of a magic offset. For x86, the dispatch
548 * at offset 32 is the first one where the offset in the
549 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
550 */
551 const GLubyte * const template_func = gl_dispatch_functions_start
552 + (DISPATCH_FUNCTION_SIZE * 32);
553 GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
554
555
556 if ( code != NULL ) {
557 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
558 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
559 }
560
561 return (_glapi_proc) code;
562 #elif defined(USE_SPARC_ASM)
563
564 #ifdef __arch64__
565 static const unsigned int insn_template[] = {
566 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
567 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
568 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
569 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
570 0x8528b020, /* sllx %g2, 32, %g2 */
571 0xc2584002, /* ldx [%g1 + %g2], %g1 */
572 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
573 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
574 0xc6584002, /* ldx [%g1 + %g2], %g3 */
575 0x81c0c000, /* jmpl %g3, %g0 */
576 0x01000000 /* nop */
577 };
578 #else
579 static const unsigned int insn_template[] = {
580 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
581 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
582 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
583 0x81c0c000, /* jmpl %g3, %g0 */
584 0x01000000 /* nop */
585 };
586 #endif /* __arch64__ */
587 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
588 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
589 if (code) {
590 memcpy(code, insn_template, sizeof(insn_template));
591
592 #ifdef __arch64__
593 code[0] |= (glapi_addr >> (32 + 10));
594 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
595 __glapi_sparc_icache_flush(&code[0]);
596 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
597 code[3] |= (glapi_addr & ((1 << 10) - 1));
598 __glapi_sparc_icache_flush(&code[2]);
599 code[6] |= ((functionOffset * 8) >> 10);
600 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
601 __glapi_sparc_icache_flush(&code[6]);
602 #else
603 code[0] |= (glapi_addr >> 10);
604 code[1] |= (glapi_addr & ((1 << 10) - 1));
605 __glapi_sparc_icache_flush(&code[0]);
606 code[2] |= (functionOffset * 4);
607 __glapi_sparc_icache_flush(&code[2]);
608 #endif /* __arch64__ */
609 }
610 return (_glapi_proc) code;
611 #else
612 (void) functionOffset;
613 return NULL;
614 #endif /* USE_*_ASM */
615 }
616
617
618 /**
619 * This function inserts a new dispatch offset into the assembly language
620 * stub that was generated with the preceeding function.
621 */
622 static void
623 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
624 {
625 #if defined(USE_X86_ASM)
626 GLubyte * const code = (GLubyte *) entrypoint;
627
628 #if DISPATCH_FUNCTION_SIZE == 32
629 *((unsigned int *)(code + 11)) = 4 * offset;
630 *((unsigned int *)(code + 22)) = 4 * offset;
631 #elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
632 *((unsigned int *)(code + 8)) = 4 * offset;
633 #elif DISPATCH_FUNCTION_SIZE == 16
634 *((unsigned int *)(code + 7)) = 4 * offset;
635 #else
636 # error Invalid DISPATCH_FUNCTION_SIZE!
637 #endif
638
639 #elif defined(USE_SPARC_ASM)
640
641 /* XXX this hasn't been tested! */
642 unsigned int *code = (unsigned int *) entrypoint;
643 #ifdef __arch64__
644 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
645 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
646 code[6] |= ((offset * 8) >> 10);
647 code[7] |= ((offset * 8) & ((1 << 10) - 1));
648 __glapi_sparc_icache_flush(&code[6]);
649 #else /* __arch64__ */
650 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
651 code[2] |= (offset * 4);
652 __glapi_sparc_icache_flush(&code[2]);
653 #endif /* __arch64__ */
654
655 #else
656
657 /* an unimplemented architecture */
658 (void) entrypoint;
659 (void) offset;
660
661 #endif /* USE_*_ASM */
662 }
663
664
665 /**
666 * Generate new entrypoint
667 *
668 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
669 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
670 * never happens, and the user calls this function, he'll segfault. That's
671 * what you get when you try calling a GL function that doesn't really exist.
672 *
673 * \param funcName Name of the function to create an entry-point for.
674 *
675 * \sa _glapi_add_entrypoint
676 */
677
678 static struct _glapi_function *
679 add_function_name( const char * funcName )
680 {
681 struct _glapi_function * entry = NULL;
682
683 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
684 _glapi_proc entrypoint = generate_entrypoint(~0);
685 if (entrypoint != NULL) {
686 entry = & ExtEntryTable[NumExtEntryPoints];
687
688 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
689 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
690 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
691 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
692 NumExtEntryPoints++;
693 }
694 }
695
696 return entry;
697 }
698
699
700 /**
701 * Fill-in the dispatch stub for the named function.
702 *
703 * This function is intended to be called by a hardware driver. When called,
704 * a dispatch stub may be created created for the function. A pointer to this
705 * dispatch function will be returned by glXGetProcAddress.
706 *
707 * \param function_names Array of pointers to function names that should
708 * share a common dispatch offset.
709 * \param parameter_signature String representing the types of the parameters
710 * passed to the named function. Parameter types
711 * are converted to characters using the following
712 * rules:
713 * - 'i' for \c GLint, \c GLuint, and \c GLenum
714 * - 'p' for any pointer type
715 * - 'f' for \c GLfloat and \c GLclampf
716 * - 'd' for \c GLdouble and \c GLclampd
717 *
718 * \returns
719 * The offset in the dispatch table of the named function. A pointer to the
720 * driver's implementation of the named function should be stored at
721 * \c dispatch_table[\c offset].
722 *
723 * \sa glXGetProcAddress
724 *
725 * \warning
726 * This function can only handle up to 8 names at a time. As far as I know,
727 * the maximum number of names ever associated with an existing GL function is
728 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
729 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
730 * too painful of a limitation.
731 *
732 * \todo
733 * Determine whether or not \c parameter_signature should be allowed to be
734 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
735 * pass in an empty string.
736 *
737 * \todo
738 * Determine if code should be added to reject function names that start with
739 * 'glX'.
740 *
741 * \bug
742 * Add code to compare \c parameter_signature with the parameter signature of
743 * a static function. In order to do that, we need to find a way to \b get
744 * the parameter signature of a static function.
745 */
746
747 PUBLIC int
748 _glapi_add_dispatch( const char * const * function_names,
749 const char * parameter_signature )
750 {
751 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
752 const char * const real_sig = (parameter_signature != NULL)
753 ? parameter_signature : "";
754 struct _glapi_function * entry[8];
755 GLboolean is_static[8];
756 unsigned i;
757 unsigned j;
758 int offset = ~0;
759 int new_offset;
760
761
762 (void) memset( is_static, 0, sizeof( is_static ) );
763 (void) memset( entry, 0, sizeof( entry ) );
764
765 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
766 /* Do some trivial validation on the name of the function.
767 */
768
769 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
770 return GL_FALSE;
771
772 /* Determine if the named function already exists. If the function does
773 * exist, it must have the same parameter signature as the function
774 * being added.
775 */
776
777 new_offset = get_static_proc_offset(function_names[i]);
778 if (new_offset >= 0) {
779 /* FIXME: Make sure the parameter signatures match! How do we get
780 * FIXME: the parameter signature for static functions?
781 */
782
783 if ( (offset != ~0) && (new_offset != offset) ) {
784 return -1;
785 }
786
787 is_static[i] = GL_TRUE;
788 offset = new_offset;
789 }
790
791
792 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
793 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
794 /* The offset may be ~0 if the function name was added by
795 * glXGetProcAddress but never filled in by the driver.
796 */
797
798 if (ExtEntryTable[j].dispatch_offset != ~0) {
799 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
800 != 0) {
801 return -1;
802 }
803
804 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
805 return -1;
806 }
807
808 offset = ExtEntryTable[j].dispatch_offset;
809 }
810
811 entry[i] = & ExtEntryTable[j];
812 break;
813 }
814 }
815 }
816
817 if (offset == ~0) {
818 offset = next_dynamic_offset;
819 next_dynamic_offset++;
820 }
821
822 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
823 if (! is_static[i] ) {
824 if (entry[i] == NULL) {
825 entry[i] = add_function_name( function_names[i] );
826 if (entry[i] == NULL) {
827 /* FIXME: Possible memory leak here.
828 */
829 return -1;
830 }
831 }
832
833 entry[i]->parameter_signature = str_dup(real_sig);
834 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
835 entry[i]->dispatch_offset = offset;
836 }
837 }
838
839 return offset;
840 }
841
842
843 /**
844 * Return offset of entrypoint for named function within dispatch table.
845 */
846 PUBLIC GLint
847 _glapi_get_proc_offset(const char *funcName)
848 {
849 /* search extension functions first */
850 GLuint i;
851 for (i = 0; i < NumExtEntryPoints; i++) {
852 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
853 return ExtEntryTable[i].dispatch_offset;
854 }
855 }
856 /* search static functions */
857 return get_static_proc_offset(funcName);
858 }
859
860
861
862 /**
863 * Return pointer to the named function. If the function name isn't found
864 * in the name of static functions, try generating a new API entrypoint on
865 * the fly with assembly language.
866 */
867 _glapi_proc
868 _glapi_get_proc_address(const char *funcName)
869 {
870 struct _glapi_function * entry;
871 GLuint i;
872
873 #ifdef MANGLE
874 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
875 return NULL;
876 #else
877 if (funcName[0] != 'g' || funcName[1] != 'l')
878 return NULL;
879 #endif
880
881 /* search extension functions first */
882 for (i = 0; i < NumExtEntryPoints; i++) {
883 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
884 return ExtEntryTable[i].dispatch_stub;
885 }
886 }
887
888 #if !defined( XFree86Server ) && !defined( XGLServer )
889 /* search static functions */
890 {
891 const _glapi_proc func = get_static_proc_address(funcName);
892 if (func)
893 return func;
894 }
895 #endif /* !defined( XFree86Server ) */
896
897 entry = add_function_name(funcName);
898 return (entry == NULL) ? NULL : entry->dispatch_stub;
899 }
900
901
902
903 /**
904 * Return the name of the function at the given dispatch offset.
905 * This is only intended for debugging.
906 */
907 const char *
908 _glapi_get_proc_name(GLuint offset)
909 {
910 GLuint i;
911 const char * n;
912
913 /* search built-in functions */
914 n = get_static_proc_name(offset);
915 if ( n != NULL ) {
916 return n;
917 }
918
919 /* search added extension functions */
920 for (i = 0; i < NumExtEntryPoints; i++) {
921 if (ExtEntryTable[i].dispatch_offset == offset) {
922 return ExtEntryTable[i].name;
923 }
924 }
925 return NULL;
926 }
927
928
929
930 /**
931 * Return size of dispatch table struct as number of functions (or
932 * slots).
933 */
934 PUBLIC GLuint
935 _glapi_get_dispatch_table_size(void)
936 {
937 return DISPATCH_TABLE_SIZE;
938 }
939
940
941
942 /**
943 * Make sure there are no NULL pointers in the given dispatch table.
944 * Intended for debugging purposes.
945 */
946 void
947 _glapi_check_table(const struct _glapi_table *table)
948 {
949 #ifdef DEBUG
950 const GLuint entries = _glapi_get_dispatch_table_size();
951 const void **tab = (const void **) table;
952 GLuint i;
953 for (i = 1; i < entries; i++) {
954 assert(tab[i]);
955 }
956
957 /* Do some spot checks to be sure that the dispatch table
958 * slots are assigned correctly.
959 */
960 {
961 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
962 char *BeginFunc = (char*) &table->Begin;
963 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
964 assert(BeginOffset == _gloffset_Begin);
965 assert(BeginOffset == offset);
966 }
967 {
968 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
969 char *viewportFunc = (char*) &table->Viewport;
970 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
971 assert(viewportOffset == _gloffset_Viewport);
972 assert(viewportOffset == offset);
973 }
974 {
975 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
976 char *VertexPointerFunc = (char*) &table->VertexPointer;
977 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
978 assert(VertexPointerOffset == _gloffset_VertexPointer);
979 assert(VertexPointerOffset == offset);
980 }
981 {
982 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
983 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
984 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
985 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
986 assert(ResetMinMaxOffset == offset);
987 }
988 {
989 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
990 char *blendColorFunc = (char*) &table->BlendColor;
991 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
992 assert(blendColorOffset == _gloffset_BlendColor);
993 assert(blendColorOffset == offset);
994 }
995 {
996 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
997 char *istextureFunc = (char*) &table->IsTextureEXT;
998 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
999 assert(istextureOffset == _gloffset_IsTextureEXT);
1000 assert(istextureOffset == offset);
1001 }
1002 {
1003 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
1004 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1005 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1006 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1007 assert(secondaryColor3fOffset == offset);
1008 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
1009 }
1010 {
1011 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1012 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1013 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1014 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1015 assert(pointParameterivOffset == offset);
1016 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
1017 }
1018 {
1019 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1020 char *setFenceFunc = (char*) &table->SetFenceNV;
1021 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1022 assert(setFenceOffset == _gloffset_SetFenceNV);
1023 assert(setFenceOffset == offset);
1024 }
1025 #else
1026 (void) table;
1027 #endif
1028 }
1029
1030
1031 #if defined(PTHREADS) || defined(GLX_USE_TLS)
1032 /**
1033 * Perform platform-specific GL API entry-point fixups.
1034 *
1035 *
1036 */
1037 static void
1038 init_glapi_relocs( void )
1039 {
1040 #if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1041 extern void * _x86_get_dispatch(void);
1042 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1043 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1044
1045
1046 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1047 (void) memcpy( curr_func, get_disp, 6 );
1048 curr_func += DISPATCH_FUNCTION_SIZE;
1049 }
1050 #endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1051 }
1052 #endif