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