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