2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file glapi_getproc.c
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
33 #ifdef HAVE_DIX_CONFIG_H
34 #include <dix-config.h>
35 #include "glapi/mesa.h"
37 #include "main/glheader.h"
38 #include "main/compiler.h"
41 #include "glapi/glapi.h"
42 #include "glapi/glapi_priv.h"
43 #include "glapi/glapitable.h"
44 #include "glapi/glapioffsets.h"
47 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server)
48 # define NEED_FUNCTION_POINTER
51 /* The code in this file is auto-generated with Python */
52 #include "glapi/glprocs.h"
56 * Search the table of static entrypoint functions for the named function
57 * and return the corresponding glprocs_table_t entry.
59 static const glprocs_table_t
*
60 find_entry( const char * n
)
63 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
64 const char *testName
= gl_string_table
+ static_functions
[i
].Name_offset
;
66 /* skip the prefix on the name */
67 if (strcmp(testName
, n
+ 1) == 0)
69 if (strcmp(testName
, n
) == 0)
72 return &static_functions
[i
];
80 * Return dispatch table offset of the named static (built-in) function.
81 * Return -1 if function not found.
84 get_static_proc_offset(const char *funcName
)
86 const glprocs_table_t
* const f
= find_entry( funcName
);
94 #if !defined(XFree86Server)
97 * Return dispatch function address for the named static (built-in) function.
98 * Return NULL if function not found.
101 get_static_proc_address(const char *funcName
)
103 const glprocs_table_t
* const f
= find_entry( funcName
);
105 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
106 return (f
->Address
== NULL
)
107 ? get_entrypoint_address(f
->Offset
)
109 #elif defined(DISPATCH_FUNCTION_SIZE)
110 return get_entrypoint_address(f
->Offset
);
120 #endif /* !defined(XFree86Server) */
125 * Return the name of the function at the given offset in the dispatch
126 * table. For debugging only.
129 get_static_proc_name( GLuint offset
)
132 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
133 if (static_functions
[i
].Offset
== offset
) {
134 return gl_string_table
+ static_functions
[i
].Name_offset
;
142 /**********************************************************************
143 * Extension function management.
148 * Track information about a function added to the GL API.
150 struct _glapi_function
{
152 * Name of the function.
158 * Text string that describes the types of the parameters passed to the
159 * named function. Parameter types are converted to characters using the
161 * - 'i' for \c GLint, \c GLuint, and \c GLenum
162 * - 'p' for any pointer type
163 * - 'f' for \c GLfloat and \c GLclampf
164 * - 'd' for \c GLdouble and \c GLclampd
166 const char * parameter_signature
;
170 * Offset in the dispatch table where the pointer to the real function is
171 * located. If the driver has not requested that the named function be
172 * added to the dispatch table, this will have the value ~0.
174 unsigned dispatch_offset
;
178 * Pointer to the dispatch stub for the named function.
181 * The semantic of this field should be changed slightly. Currently, it
182 * is always expected to be non-\c NULL. However, it would be better to
183 * only allocate the entry-point stub when the application requests the
184 * function via \c glXGetProcAddress. This would save memory for all the
185 * functions that the driver exports but that the application never wants
188 _glapi_proc dispatch_stub
;
193 * Number of extension functions which we can dynamically add at runtime.
195 #define MAX_EXTENSION_FUNCS 300
198 static struct _glapi_function ExtEntryTable
[MAX_EXTENSION_FUNCS
];
199 static GLuint NumExtEntryPoints
= 0;
203 * strdup() is actually not a standard ANSI C or POSIX routine.
204 * Irix will not define it if ANSI mode is in effect.
207 str_dup(const char *str
)
210 copy
= (char*) malloc(strlen(str
) + 1);
219 * Generate new entrypoint
221 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
222 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
223 * never happens, and the user calls this function, he'll segfault. That's
224 * what you get when you try calling a GL function that doesn't really exist.
226 * \param funcName Name of the function to create an entry-point for.
228 * \sa _glapi_add_entrypoint
231 static struct _glapi_function
*
232 add_function_name( const char * funcName
)
234 struct _glapi_function
* entry
= NULL
;
236 if (NumExtEntryPoints
< MAX_EXTENSION_FUNCS
) {
237 _glapi_proc entrypoint
= generate_entrypoint(~0);
238 if (entrypoint
!= NULL
) {
239 entry
= & ExtEntryTable
[NumExtEntryPoints
];
241 ExtEntryTable
[NumExtEntryPoints
].name
= str_dup(funcName
);
242 ExtEntryTable
[NumExtEntryPoints
].parameter_signature
= NULL
;
243 ExtEntryTable
[NumExtEntryPoints
].dispatch_offset
= ~0;
244 ExtEntryTable
[NumExtEntryPoints
].dispatch_stub
= entrypoint
;
254 * Fill-in the dispatch stub for the named function.
256 * This function is intended to be called by a hardware driver. When called,
257 * a dispatch stub may be created created for the function. A pointer to this
258 * dispatch function will be returned by glXGetProcAddress.
260 * \param function_names Array of pointers to function names that should
261 * share a common dispatch offset.
262 * \param parameter_signature String representing the types of the parameters
263 * passed to the named function. Parameter types
264 * are converted to characters using the following
266 * - 'i' for \c GLint, \c GLuint, and \c GLenum
267 * - 'p' for any pointer type
268 * - 'f' for \c GLfloat and \c GLclampf
269 * - 'd' for \c GLdouble and \c GLclampd
272 * The offset in the dispatch table of the named function. A pointer to the
273 * driver's implementation of the named function should be stored at
274 * \c dispatch_table[\c offset]. Return -1 if error/problem.
276 * \sa glXGetProcAddress
279 * This function can only handle up to 8 names at a time. As far as I know,
280 * the maximum number of names ever associated with an existing GL function is
281 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
282 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
283 * too painful of a limitation.
286 * Determine whether or not \c parameter_signature should be allowed to be
287 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
288 * pass in an empty string.
291 * Determine if code should be added to reject function names that start with
295 * Add code to compare \c parameter_signature with the parameter signature of
296 * a static function. In order to do that, we need to find a way to \b get
297 * the parameter signature of a static function.
301 _glapi_add_dispatch( const char * const * function_names
,
302 const char * parameter_signature
)
304 static int next_dynamic_offset
= _gloffset_FIRST_DYNAMIC
;
305 const char * const real_sig
= (parameter_signature
!= NULL
)
306 ? parameter_signature
: "";
307 struct _glapi_function
* entry
[8];
308 GLboolean is_static
[8];
315 (void) memset( is_static
, 0, sizeof( is_static
) );
316 (void) memset( entry
, 0, sizeof( entry
) );
318 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
319 /* Do some trivial validation on the name of the function.
322 if (!function_names
[i
] || function_names
[i
][0] != 'g' || function_names
[i
][1] != 'l')
325 /* Determine if the named function already exists. If the function does
326 * exist, it must have the same parameter signature as the function
330 new_offset
= get_static_proc_offset(function_names
[i
]);
331 if (new_offset
>= 0) {
332 /* FIXME: Make sure the parameter signatures match! How do we get
333 * FIXME: the parameter signature for static functions?
336 if ( (offset
!= ~0) && (new_offset
!= offset
) ) {
340 is_static
[i
] = GL_TRUE
;
345 for ( j
= 0 ; j
< NumExtEntryPoints
; j
++ ) {
346 if (strcmp(ExtEntryTable
[j
].name
, function_names
[i
]) == 0) {
347 /* The offset may be ~0 if the function name was added by
348 * glXGetProcAddress but never filled in by the driver.
351 if (ExtEntryTable
[j
].dispatch_offset
!= ~0) {
352 if (strcmp(real_sig
, ExtEntryTable
[j
].parameter_signature
)
357 if ( (offset
!= ~0) && (ExtEntryTable
[j
].dispatch_offset
!= offset
) ) {
361 offset
= ExtEntryTable
[j
].dispatch_offset
;
364 entry
[i
] = & ExtEntryTable
[j
];
371 offset
= next_dynamic_offset
;
372 next_dynamic_offset
++;
375 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
376 if (! is_static
[i
] ) {
377 if (entry
[i
] == NULL
) {
378 entry
[i
] = add_function_name( function_names
[i
] );
379 if (entry
[i
] == NULL
) {
380 /* FIXME: Possible memory leak here.
386 entry
[i
]->parameter_signature
= str_dup(real_sig
);
387 fill_in_entrypoint_offset(entry
[i
]->dispatch_stub
, offset
);
388 entry
[i
]->dispatch_offset
= offset
;
397 * Return offset of entrypoint for named function within dispatch table.
400 _glapi_get_proc_offset(const char *funcName
)
402 /* search extension functions first */
404 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
405 if (strcmp(ExtEntryTable
[i
].name
, funcName
) == 0) {
406 return ExtEntryTable
[i
].dispatch_offset
;
409 /* search static functions */
410 return get_static_proc_offset(funcName
);
416 * Return pointer to the named function. If the function name isn't found
417 * in the name of static functions, try generating a new API entrypoint on
418 * the fly with assembly language.
421 _glapi_get_proc_address(const char *funcName
)
423 struct _glapi_function
* entry
;
427 /* skip the prefix on the name */
428 if (funcName
[1] != 'g' || funcName
[2] != 'l')
431 if (funcName
[0] != 'g' || funcName
[1] != 'l')
435 /* search extension functions first */
436 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
437 if (strcmp(ExtEntryTable
[i
].name
, funcName
) == 0) {
438 return ExtEntryTable
[i
].dispatch_stub
;
442 #if !defined( XFree86Server )
443 /* search static functions */
445 const _glapi_proc func
= get_static_proc_address(funcName
);
449 #endif /* !defined( XFree86Server ) */
451 entry
= add_function_name(funcName
);
452 return (entry
== NULL
) ? NULL
: entry
->dispatch_stub
;
458 * Return the name of the function at the given dispatch offset.
459 * This is only intended for debugging.
462 _glapi_get_proc_name(GLuint offset
)
467 /* search built-in functions */
468 n
= get_static_proc_name(offset
);
473 /* search added extension functions */
474 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
475 if (ExtEntryTable
[i
].dispatch_offset
== offset
) {
476 return ExtEntryTable
[i
].name
;
484 /**********************************************************************
485 * GL API table functions.
490 * The dispatch table size (number of entries) is the size of the
491 * _glapi_table struct plus the number of dynamic entries we can add.
492 * The extra slots can be filled in by DRI drivers that register new extension
495 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
499 * Return size of dispatch table struct as number of functions (or
503 _glapi_get_dispatch_table_size(void)
505 return DISPATCH_TABLE_SIZE
;
510 * Make sure there are no NULL pointers in the given dispatch table.
511 * Intended for debugging purposes.
514 _glapi_check_table_not_null(const struct _glapi_table
*table
)
516 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
517 const GLuint entries
= _glapi_get_dispatch_table_size();
518 const void **tab
= (const void **) table
;
520 for (i
= 1; i
< entries
; i
++) {
530 * Do some spot checks to be sure that the dispatch table
531 * slots are assigned correctly. For debugging only.
534 _glapi_check_table(const struct _glapi_table
*table
)
536 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
538 GLuint BeginOffset
= _glapi_get_proc_offset("glBegin");
539 char *BeginFunc
= (char*) &table
->Begin
;
540 GLuint offset
= (BeginFunc
- (char *) table
) / sizeof(void *);
541 assert(BeginOffset
== _gloffset_Begin
);
542 assert(BeginOffset
== offset
);
545 GLuint viewportOffset
= _glapi_get_proc_offset("glViewport");
546 char *viewportFunc
= (char*) &table
->Viewport
;
547 GLuint offset
= (viewportFunc
- (char *) table
) / sizeof(void *);
548 assert(viewportOffset
== _gloffset_Viewport
);
549 assert(viewportOffset
== offset
);
552 GLuint VertexPointerOffset
= _glapi_get_proc_offset("glVertexPointer");
553 char *VertexPointerFunc
= (char*) &table
->VertexPointer
;
554 GLuint offset
= (VertexPointerFunc
- (char *) table
) / sizeof(void *);
555 assert(VertexPointerOffset
== _gloffset_VertexPointer
);
556 assert(VertexPointerOffset
== offset
);
559 GLuint ResetMinMaxOffset
= _glapi_get_proc_offset("glResetMinmax");
560 char *ResetMinMaxFunc
= (char*) &table
->ResetMinmax
;
561 GLuint offset
= (ResetMinMaxFunc
- (char *) table
) / sizeof(void *);
562 assert(ResetMinMaxOffset
== _gloffset_ResetMinmax
);
563 assert(ResetMinMaxOffset
== offset
);
566 GLuint blendColorOffset
= _glapi_get_proc_offset("glBlendColor");
567 char *blendColorFunc
= (char*) &table
->BlendColor
;
568 GLuint offset
= (blendColorFunc
- (char *) table
) / sizeof(void *);
569 assert(blendColorOffset
== _gloffset_BlendColor
);
570 assert(blendColorOffset
== offset
);
573 GLuint secondaryColor3fOffset
= _glapi_get_proc_offset("glSecondaryColor3fEXT");
574 char *secondaryColor3fFunc
= (char*) &table
->SecondaryColor3fEXT
;
575 GLuint offset
= (secondaryColor3fFunc
- (char *) table
) / sizeof(void *);
576 assert(secondaryColor3fOffset
== _gloffset_SecondaryColor3fEXT
);
577 assert(secondaryColor3fOffset
== offset
);
580 GLuint pointParameterivOffset
= _glapi_get_proc_offset("glPointParameterivNV");
581 char *pointParameterivFunc
= (char*) &table
->PointParameterivNV
;
582 GLuint offset
= (pointParameterivFunc
- (char *) table
) / sizeof(void *);
583 assert(pointParameterivOffset
== _gloffset_PointParameterivNV
);
584 assert(pointParameterivOffset
== offset
);
587 GLuint setFenceOffset
= _glapi_get_proc_offset("glSetFenceNV");
588 char *setFenceFunc
= (char*) &table
->SetFenceNV
;
589 GLuint offset
= (setFenceFunc
- (char *) table
) / sizeof(void *);
590 assert(setFenceOffset
== _gloffset_SetFenceNV
);
591 assert(setFenceOffset
== offset
);