blob: c11760cf126b7e3dfe6f179981f00f9fd60a17fb [file] [log] [blame]
Brian Paulfbd8f211999-11-11 01:22:25 +00001/*
2 * Mesa 3-D graphics library
Brian Paul767e15a2004-11-27 03:51:11 +00003 * Version: 6.3
Brian Paulfbd8f211999-11-11 01:22:25 +00004 *
Brian Paul767e15a2004-11-27 03:51:11 +00005 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
Brian Paulfbd8f211999-11-11 01:22:25 +00006 *
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
Brian Paul7e1161b1999-11-25 18:17:04 +000026/*
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.
Brian Paul9f943992000-01-28 19:03:33 +000033 *
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 *
Brian Paul4e9676f2002-06-29 19:48:15 +000038 * NOTE: There are no dependencies on Mesa in this code.
Brian Paulab0c8862001-01-23 23:35:47 +000039 *
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
Brian Paul4e9676f2002-06-29 19:48:15 +000043 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
Brian Paul54f3aab2002-10-02 01:51:44 +000044 * 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.
Brian Paul7e1161b1999-11-25 18:17:04 +000049 */
50
51
52
Brian Paul3c27be32000-02-10 21:27:48 +000053#include "glheader.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000054#include "glapi.h"
Brian Paul0c239fc1999-12-16 12:38:11 +000055#include "glapioffsets.h"
56#include "glapitable.h"
Brian Paul9f943992000-01-28 19:03:33 +000057#include "glthread.h"
Brian Paulbb72d321999-12-16 17:31:59 +000058
Brian Paul3c257e12001-03-28 17:19:58 +000059/***** BEGIN NO-OP DISPATCH *****/
60
61static GLboolean WarnFlag = GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000062static _glapi_warning_func warning_func;
Brian Paul3c257e12001-03-28 17:19:58 +000063
Ian Romanick25fe93f2005-04-13 20:59:15 +000064static void init_glapi_relocs(void);
65
66static _glapi_proc generate_entrypoint(GLuint functionOffset);
67static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
Brian Paul4e9676f2002-06-29 19:48:15 +000068
69/*
70 * Enable/disable printing of warning messages.
71 */
Adam Jackson791ce022004-12-15 23:14:29 +000072PUBLIC void
Brian Paul3c257e12001-03-28 17:19:58 +000073_glapi_noop_enable_warnings(GLboolean enable)
74{
75 WarnFlag = enable;
76}
77
Brian Paul4e9676f2002-06-29 19:48:15 +000078/*
79 * Register a callback function for reporting errors.
80 */
Adam Jackson791ce022004-12-15 23:14:29 +000081PUBLIC void
Brian Paul4e9676f2002-06-29 19:48:15 +000082_glapi_set_warning_func( _glapi_warning_func func )
83{
84 warning_func = func;
85}
86
Brian Paul3c257e12001-03-28 17:19:58 +000087static GLboolean
88warn(void)
89{
Ben Crossmand8aa5ff2005-04-15 09:13:21 +000090 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
Brian Paul4e9676f2002-06-29 19:48:15 +000091 && warning_func) {
Brian Paul3c257e12001-03-28 17:19:58 +000092 return GL_TRUE;
Brian Paul4e9676f2002-06-29 19:48:15 +000093 }
94 else {
Brian Paul3c257e12001-03-28 17:19:58 +000095 return GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000096 }
Brian Paul3c257e12001-03-28 17:19:58 +000097}
98
99
100#define KEYWORD1 static
Daniel Borca722cb892004-01-07 12:37:09 +0000101#define KEYWORD2 GLAPIENTRY
Brian Paul3c257e12001-03-28 17:19:58 +0000102#define NAME(func) NoOp##func
103
Brian Paul4e9676f2002-06-29 19:48:15 +0000104#define F NULL
Brian Paul3c257e12001-03-28 17:19:58 +0000105
Brian Paul5849e3d2004-11-05 18:32:02 +0000106#define DISPATCH(func, args, msg) \
107 if (warn()) { \
108 warning_func(NULL, "GL User Error: called without context: %s", #func); \
Brian Paul3c257e12001-03-28 17:19:58 +0000109 }
110
Brian Paul5849e3d2004-11-05 18:32:02 +0000111#define RETURN_DISPATCH(func, args, msg) \
112 if (warn()) { \
113 warning_func(NULL, "GL User Error: called without context: %s", #func); \
114 } \
Brian Paul3c257e12001-03-28 17:19:58 +0000115 return 0
116
117#define DISPATCH_TABLE_NAME __glapi_noop_table
Brian Paul767e15a2004-11-27 03:51:11 +0000118#define UNUSED_TABLE_NAME __unused_noop_functions
Brian Paul3c257e12001-03-28 17:19:58 +0000119
Brian Paul767e15a2004-11-27 03:51:11 +0000120#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
Brian Paul3c257e12001-03-28 17:19:58 +0000121
Brian Paula760ccf2004-12-03 15:24:34 +0000122static GLint NoOpUnused(void)
Brian Paul3c257e12001-03-28 17:19:58 +0000123{
124 if (warn()) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000125 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
Brian Paul3c257e12001-03-28 17:19:58 +0000126 }
127 return 0;
128}
129
130#include "glapitemp.h"
131
132/***** END NO-OP DISPATCH *****/
133
134
135
Ian Romanick967b0062005-08-10 23:54:15 +0000136/**
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
169PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
170 __attribute__((tls_model("initial-exec")))
171 = (struct _glapi_table *) __glapi_noop_table;
172
173PUBLIC __thread void * _glapi_tls_Context
174 __attribute__((tls_model("initial-exec")));
175
176PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
177PUBLIC const void *_glapi_Context = NULL;
178
179#else
Brian Paul3c257e12001-03-28 17:19:58 +0000180
181#if defined(THREADS)
182
Ian Romanickd14d1032004-07-02 00:01:09 +0000183static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000184_glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000185static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
Brian Paul3c257e12001-03-28 17:19:58 +0000186
Ian Romanick967b0062005-08-10 23:54:15 +0000187#endif /* defined(THREADS) */
Brian Paul3c257e12001-03-28 17:19:58 +0000188
Ian Romanick967b0062005-08-10 23:54:15 +0000189PUBLIC struct _glapi_table *_glapi_Dispatch =
190 (struct _glapi_table *) __glapi_noop_table;
Adam Jackson791ce022004-12-15 23:14:29 +0000191PUBLIC void *_glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000192
Ian Romanick25fe93f2005-04-13 20:59:15 +0000193#endif /* defined(GLX_USE_TLS) */
Ian Romanick967b0062005-08-10 23:54:15 +0000194/*@}*/
Brian Paulab0c8862001-01-23 23:35:47 +0000195
Brian Paul54f3aab2002-10-02 01:51:44 +0000196
Brian Paul767e15a2004-11-27 03:51:11 +0000197/**
198 * strdup() is actually not a standard ANSI C or POSIX routine.
Brian Paul3c257e12001-03-28 17:19:58 +0000199 * Irix will not define it if ANSI mode is in effect.
200 */
201static char *
202str_dup(const char *str)
Randy Frankd7361e12000-03-27 21:13:58 +0000203{
Brian Paulfffb8092000-03-29 18:46:11 +0000204 char *copy;
205 copy = (char*) malloc(strlen(str) + 1);
206 if (!copy)
207 return NULL;
208 strcpy(copy, str);
209 return copy;
Randy Frankd7361e12000-03-27 21:13:58 +0000210}
211
Brian Paul7fb54ae1999-11-19 22:33:50 +0000212
Brian Paulbb72d321999-12-16 17:31:59 +0000213
Brian Paul767e15a2004-11-27 03:51:11 +0000214/**
Brian Paulbb72d321999-12-16 17:31:59 +0000215 * We should call this periodically from a function such as glXMakeCurrent
Brian Paul5104b4d2002-03-07 21:50:41 +0000216 * in order to test if multiple threads are being used.
Brian Paulbb72d321999-12-16 17:31:59 +0000217 */
Ian Romanick967b0062005-08-10 23:54:15 +0000218void
Brian Paulbb72d321999-12-16 17:31:59 +0000219_glapi_check_multithread(void)
220{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000221#if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000222 if (!ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000223 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()) {
Ian Romanickd14d1032004-07-02 00:01:09 +0000230 ThreadSafe = GL_TRUE;
Brian Paulbb72d321999-12-16 17:31:59 +0000231 _glapi_set_dispatch(NULL);
232 }
233 }
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000234 else if (!_glapi_get_dispatch()) {
235 /* make sure that this thread's dispatch pointer isn't null */
236 _glapi_set_dispatch(NULL);
237 }
Brian Paulbb72d321999-12-16 17:31:59 +0000238#endif
239}
240
241
242
Brian Paul767e15a2004-11-27 03:51:11 +0000243/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000244 * 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 */
Adam Jackson791ce022004-12-15 23:14:29 +0000248PUBLIC void
Brian Paulf9b97d92000-01-28 20:17:42 +0000249_glapi_set_context(void *context)
Brian Paul8f91fb61999-12-17 14:51:28 +0000250{
Brian Paul767e15a2004-11-27 03:51:11 +0000251 (void) __unused_noop_functions; /* silence a warning */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000252#if defined(GLX_USE_TLS)
253 _glapi_tls_Context = context;
254#elif defined(THREADS)
Brian Paul3c27be32000-02-10 21:27:48 +0000255 _glthread_SetTSD(&ContextTSD, context);
Ian Romanickd14d1032004-07-02 00:01:09 +0000256 _glapi_Context = (ThreadSafe) ? NULL : context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000257#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000258 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000259#endif
260}
261
262
263
Brian Paul767e15a2004-11-27 03:51:11 +0000264/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000265 * 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 */
Adam Jackson791ce022004-12-15 23:14:29 +0000269PUBLIC void *
Brian Paulf9b97d92000-01-28 20:17:42 +0000270_glapi_get_context(void)
Brian Paul8f91fb61999-12-17 14:51:28 +0000271{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000272#if defined(GLX_USE_TLS)
273 return _glapi_tls_Context;
274#elif defined(THREADS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000275 if (ThreadSafe) {
Brian Paul8f91fb61999-12-17 14:51:28 +0000276 return _glthread_GetTSD(&ContextTSD);
277 }
278 else {
Brian Paulf9b97d92000-01-28 20:17:42 +0000279 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000280 }
281#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000282 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000283#endif
284}
285
286
287
Brian Paul767e15a2004-11-27 03:51:11 +0000288/**
Brian Paul7fb54ae1999-11-19 22:33:50 +0000289 * Set the global or per-thread dispatch table pointer.
290 */
Adam Jackson791ce022004-12-15 23:14:29 +0000291PUBLIC void
Brian Paul7fb54ae1999-11-19 22:33:50 +0000292_glapi_set_dispatch(struct _glapi_table *dispatch)
293{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000294#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
Ian Romanick6c5402b2004-07-05 22:40:54 +0000301 if (!dispatch) {
302 /* use the no-op functions */
303 dispatch = (struct _glapi_table *) __glapi_noop_table;
304 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000305#ifdef DEBUG
Ian Romanick6c5402b2004-07-05 22:40:54 +0000306 else {
Brian Paul7fb54ae1999-11-19 22:33:50 +0000307 _glapi_check_table(dispatch);
308 }
309#endif
310
Ian Romanick25fe93f2005-04-13 20:59:15 +0000311#if defined(GLX_USE_TLS)
Ian Romanick967b0062005-08-10 23:54:15 +0000312 _glapi_tls_Dispatch = dispatch;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000313#elif defined(THREADS)
Ian Romanick967b0062005-08-10 23:54:15 +0000314 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
315 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000316#else /*THREADS*/
Ian Romanick967b0062005-08-10 23:54:15 +0000317 _glapi_Dispatch = dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000318#endif /*THREADS*/
Brian Paul7fb54ae1999-11-19 22:33:50 +0000319}
320
321
Brian Paulbb72d321999-12-16 17:31:59 +0000322
Brian Paul767e15a2004-11-27 03:51:11 +0000323/**
Brian Paulbb72d321999-12-16 17:31:59 +0000324 * Return pointer to current dispatch table for calling thread.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000325 */
Adam Jackson791ce022004-12-15 23:14:29 +0000326PUBLIC struct _glapi_table *
Brian Paul7fb54ae1999-11-19 22:33:50 +0000327_glapi_get_dispatch(void)
328{
Ian Romanick967b0062005-08-10 23:54:15 +0000329 struct _glapi_table * api;
330
Ian Romanick25fe93f2005-04-13 20:59:15 +0000331#if defined(GLX_USE_TLS)
Ian Romanick967b0062005-08-10 23:54:15 +0000332 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
Ian Romanick25fe93f2005-04-13 20:59:15 +0000340
341 assert( api != NULL );
342 return api;
Brian Paul3a71d052000-09-05 20:17:37 +0000343}
Brian Paulab0c8862001-01-23 23:35:47 +0000344
Brian Paul3a71d052000-09-05 20:17:37 +0000345
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000346#if !defined( USE_X86_ASM )
Ian Romanick78677992004-05-27 00:05:13 +0000347#define NEED_FUNCTION_POINTER
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000348#endif
Ian Romanick78677992004-05-27 00:05:13 +0000349
Brian Paul54f3aab2002-10-02 01:51:44 +0000350/* The code in this file is auto-generated with Python */
Brian Paulb5fd8862001-11-18 22:48:11 +0000351#include "glprocs.h"
Brian Paul7fb54ae1999-11-19 22:33:50 +0000352
Brian Paul959f8022000-03-19 01:10:11 +0000353
Brian Paul767e15a2004-11-27 03:51:11 +0000354/**
355 * Search the table of static entrypoint functions for the named function
356 * and return the corresponding glprocs_table_t entry.
357 */
Ian Romanick78677992004-05-27 00:05:13 +0000358static const glprocs_table_t *
359find_entry( const char * n )
360{
Brian Paula760ccf2004-12-03 15:24:34 +0000361 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000362
Brian Paula760ccf2004-12-03 15:24:34 +0000363 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000364 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
Brian Paul767e15a2004-11-27 03:51:11 +0000374
375/**
Brian Paul959f8022000-03-19 01:10:11 +0000376 * Return dispatch table offset of the named static (built-in) function.
377 * Return -1 if function not found.
378 */
379static GLint
380get_static_proc_offset(const char *funcName)
381{
Ian Romanick78677992004-05-27 00:05:13 +0000382 const glprocs_table_t * const f = find_entry( funcName );
383
384 if ( f != NULL ) {
385 return f->Offset;
Brian Paul959f8022000-03-19 01:10:11 +0000386 }
387 return -1;
388}
389
390
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000391#ifdef USE_X86_ASM
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000392
Ian Romanick25fe93f2005-04-13 20:59:15 +0000393#if defined( GLX_USE_TLS )
394extern GLubyte gl_dispatch_functions_start[];
395extern GLubyte gl_dispatch_functions_end[];
396#else
397extern const GLubyte gl_dispatch_functions_start[];
398#endif
399
400# if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000401# define X86_DISPATCH_FUNCTION_SIZE 32
402# else
403# define X86_DISPATCH_FUNCTION_SIZE 16
404# endif
405
406
Brian Paul767e15a2004-11-27 03:51:11 +0000407/**
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000408 * Return dispatch function address the named static (built-in) function.
409 * Return NULL if function not found.
410 */
Brian Paul209bd3a2004-11-27 04:02:32 +0000411static const _glapi_proc
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000412get_static_proc_address(const char *funcName)
413{
414 const glprocs_table_t * const f = find_entry( funcName );
415
416 if ( f != NULL ) {
Brian Paul209bd3a2004-11-27 04:02:32 +0000417 return (_glapi_proc) (gl_dispatch_functions_start
418 + (X86_DISPATCH_FUNCTION_SIZE * f->Offset));
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000419 }
420 else {
421 return NULL;
422 }
423}
424
425#else
426
427
Brian Paul767e15a2004-11-27 03:51:11 +0000428/**
429 * Return pointer to the named static (built-in) function.
430 * \return NULL if function not found.
Brian Paul959f8022000-03-19 01:10:11 +0000431 */
Brian Paul767e15a2004-11-27 03:51:11 +0000432static const _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000433get_static_proc_address(const char *funcName)
434{
Ian Romanick78677992004-05-27 00:05:13 +0000435 const glprocs_table_t * const f = find_entry( funcName );
436 return ( f != NULL ) ? f->Address : NULL;
437}
438
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000439#endif /* USE_X86_ASM */
440
Ian Romanick78677992004-05-27 00:05:13 +0000441
Brian Paul767e15a2004-11-27 03:51:11 +0000442/**
443 * Return the name of the function at the given offset in the dispatch
444 * table. For debugging only.
445 */
Ian Romanick78677992004-05-27 00:05:13 +0000446static const char *
447get_static_proc_name( GLuint offset )
448{
Brian Paula760ccf2004-12-03 15:24:34 +0000449 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000450
Brian Paula760ccf2004-12-03 15:24:34 +0000451 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000452 if (static_functions[i].Offset == offset) {
453 return gl_string_table + static_functions[i].Name_offset;
Brian Paul9c7ca852000-10-19 20:13:12 +0000454 }
455 }
456 return NULL;
Brian Paul959f8022000-03-19 01:10:11 +0000457}
458
459
460
461/**********************************************************************
462 * Extension function management.
463 */
464
Brian Paul54f3aab2002-10-02 01:51:44 +0000465/*
466 * Number of extension functions which we can dynamically add at runtime.
467 */
468#define MAX_EXTENSION_FUNCS 300
Brian Paul959f8022000-03-19 01:10:11 +0000469
Brian Paul54f3aab2002-10-02 01:51:44 +0000470
471/*
Brian Paul767e15a2004-11-27 03:51:11 +0000472 * The dispatch table size (number of entries) is the size of the
Brian Paul54f3aab2002-10-02 01:51:44 +0000473 * _glapi_table struct plus the number of dynamic entries we can add.
474 * The extra slots can be filled in by DRI drivers that register new extension
475 * functions.
476 */
477#define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
478
Brian Paul959f8022000-03-19 01:10:11 +0000479
Ian Romanick1585c232005-07-28 00:29:51 +0000480/**
481 * Track information about a function added to the GL API.
482 */
483struct _glapi_function {
484 /**
485 * Name of the function.
486 */
487 const char * name;
488
489
490 /**
491 * Text string that describes the types of the parameters passed to the
492 * named function. Parameter types are converted to characters using the
493 * following rules:
494 * - 'i' for \c GLint, \c GLuint, and \c GLenum
495 * - 'p' for any pointer type
496 * - 'f' for \c GLfloat and \c GLclampf
497 * - 'd' for \c GLdouble and \c GLclampd
498 */
499 const char * parameter_signature;
500
501
502 /**
503 * Offset in the dispatch table where the pointer to the real function is
504 * located. If the driver has not requested that the named function be
505 * added to the dispatch table, this will have the value ~0.
506 */
507 unsigned dispatch_offset;
508
509
510 /**
511 * Pointer to the dispatch stub for the named function.
512 *
513 * \todo
514 * The semantic of this field should be changed slightly. Currently, it
515 * is always expected to be non-\c NULL. However, it would be better to
516 * only allocate the entry-point stub when the application requests the
517 * function via \c glXGetProcAddress. This would save memory for all the
518 * functions that the driver exports but that the application never wants
519 * to call.
520 */
521 _glapi_proc dispatch_stub;
Brian Paul767e15a2004-11-27 03:51:11 +0000522};
523
524
Ian Romanick1585c232005-07-28 00:29:51 +0000525static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
Brian Paul959f8022000-03-19 01:10:11 +0000526static GLuint NumExtEntryPoints = 0;
527
davem694a497e62001-06-06 22:55:28 +0000528#ifdef USE_SPARC_ASM
529extern void __glapi_sparc_icache_flush(unsigned int *);
530#endif
Brian Paul959f8022000-03-19 01:10:11 +0000531
Brian Paul767e15a2004-11-27 03:51:11 +0000532/**
Brian Paul959f8022000-03-19 01:10:11 +0000533 * Generate a dispatch function (entrypoint) which jumps through
534 * the given slot number (offset) in the current dispatch table.
535 * We need assembly language in order to accomplish this.
536 */
Brian Paul767e15a2004-11-27 03:51:11 +0000537static _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000538generate_entrypoint(GLuint functionOffset)
539{
540#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000541 /* 32 is chosen as something of a magic offset. For x86, the dispatch
542 * at offset 32 is the first one where the offset in the
543 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
Brian Paul959f8022000-03-19 01:10:11 +0000544 */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000545 const GLubyte * const template_func = gl_dispatch_functions_start
546 + (X86_DISPATCH_FUNCTION_SIZE * 32);
547 GLubyte * const code = (GLubyte *) malloc( X86_DISPATCH_FUNCTION_SIZE );
Brian Paul959f8022000-03-19 01:10:11 +0000548
Ian Romanick25fe93f2005-04-13 20:59:15 +0000549
550 if ( code != NULL ) {
551 (void) memcpy( code, template_func, X86_DISPATCH_FUNCTION_SIZE );
552 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
Brian Paul959f8022000-03-19 01:10:11 +0000553 }
Ian Romanick25fe93f2005-04-13 20:59:15 +0000554
Brian Paul767e15a2004-11-27 03:51:11 +0000555 return (_glapi_proc) code;
davem69775355a2001-06-05 23:54:00 +0000556#elif defined(USE_SPARC_ASM)
557
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000558#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000559 static const unsigned int insn_template[] = {
560 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
561 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
562 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
563 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
564 0x8528b020, /* sllx %g2, 32, %g2 */
565 0xc2584002, /* ldx [%g1 + %g2], %g1 */
566 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
567 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
568 0xc6584002, /* ldx [%g1 + %g2], %g3 */
569 0x81c0c000, /* jmpl %g3, %g0 */
570 0x01000000 /* nop */
571 };
572#else
573 static const unsigned int insn_template[] = {
574 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
575 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
576 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
577 0x81c0c000, /* jmpl %g3, %g0 */
578 0x01000000 /* nop */
579 };
580#endif
Brian Pauldec2a4d2002-10-29 15:03:14 +0000581 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
davem69775355a2001-06-05 23:54:00 +0000582 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
583 if (code) {
584 memcpy(code, insn_template, sizeof(insn_template));
585
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000586#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000587 code[0] |= (glapi_addr >> (32 + 10));
588 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
davem694a497e62001-06-06 22:55:28 +0000589 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000590 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
591 code[3] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000592 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000593 code[6] |= ((functionOffset * 8) >> 10);
594 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000595 __glapi_sparc_icache_flush(&code[6]);
davem69775355a2001-06-05 23:54:00 +0000596#else
597 code[0] |= (glapi_addr >> 10);
598 code[1] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000599 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000600 code[2] |= (functionOffset * 4);
davem694a497e62001-06-06 22:55:28 +0000601 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000602#endif
603 }
Brian Paul767e15a2004-11-27 03:51:11 +0000604 return (_glapi_proc) code;
Brian Paul959f8022000-03-19 01:10:11 +0000605#else
Brian Paula6c423d2004-08-25 15:59:48 +0000606 (void) functionOffset;
Brian Paul959f8022000-03-19 01:10:11 +0000607 return NULL;
Brian Paul54f3aab2002-10-02 01:51:44 +0000608#endif /* USE_*_ASM */
Brian Paul959f8022000-03-19 01:10:11 +0000609}
610
611
Brian Paul767e15a2004-11-27 03:51:11 +0000612/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000613 * This function inserts a new dispatch offset into the assembly language
614 * stub that was generated with the preceeding function.
615 */
616static void
Brian Paul767e15a2004-11-27 03:51:11 +0000617fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
Brian Paul54f3aab2002-10-02 01:51:44 +0000618{
619#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000620 GLubyte * const code = (GLubyte *) entrypoint;
Brian Paul54f3aab2002-10-02 01:51:44 +0000621
Ian Romanick25fe93f2005-04-13 20:59:15 +0000622
623#if X86_DISPATCH_FUNCTION_SIZE == 32
624 *((unsigned int *)(code + 11)) = 4 * offset;
625 *((unsigned int *)(code + 22)) = 4 * offset;
626#elif X86_DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
627 *((unsigned int *)(code + 8)) = 4 * offset;
628#elif X86_DISPATCH_FUNCTION_SIZE == 16
629 *((unsigned int *)(code + 7)) = 4 * offset;
630#else
631# error Invalid X86_DISPATCH_FUNCTION_SIZE!
632#endif
Brian Paul54f3aab2002-10-02 01:51:44 +0000633
634#elif defined(USE_SPARC_ASM)
635
636 /* XXX this hasn't been tested! */
637 unsigned int *code = (unsigned int *) entrypoint;
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000638#ifdef __arch64__
Brian Paul54f3aab2002-10-02 01:51:44 +0000639 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
640 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
641 code[6] |= ((offset * 8) >> 10);
642 code[7] |= ((offset * 8) & ((1 << 10) - 1));
643 __glapi_sparc_icache_flush(&code[6]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000644#else /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000645 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
Brian Paul944ea202002-10-17 16:29:17 +0000646 code[2] |= (offset * 4);
Brian Paul54f3aab2002-10-02 01:51:44 +0000647 __glapi_sparc_icache_flush(&code[2]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000648#endif /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000649
Brian Paula6c423d2004-08-25 15:59:48 +0000650#else
651
652 /* an unimplemented architecture */
653 (void) entrypoint;
654 (void) offset;
655
Brian Paul54f3aab2002-10-02 01:51:44 +0000656#endif /* USE_*_ASM */
657}
658
Brian Paul959f8022000-03-19 01:10:11 +0000659
Brian Paul767e15a2004-11-27 03:51:11 +0000660/**
Ian Romanick1585c232005-07-28 00:29:51 +0000661 * Generate new entrypoint
662 *
663 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
664 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
665 * never happens, and the user calls this function, he'll segfault. That's
666 * what you get when you try calling a GL function that doesn't really exist.
667 *
668 * \param funcName Name of the function to create an entry-point for.
669 *
670 * \sa _glapi_add_entrypoint
Brian Paul959f8022000-03-19 01:10:11 +0000671 */
Ian Romanick1585c232005-07-28 00:29:51 +0000672
673static struct _glapi_function *
674add_function_name( const char * funcName )
Brian Paul959f8022000-03-19 01:10:11 +0000675{
Ian Romanick1585c232005-07-28 00:29:51 +0000676 struct _glapi_function * entry = NULL;
677
678 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
679 _glapi_proc entrypoint = generate_entrypoint(~0);
680 if (entrypoint != NULL) {
681 entry = & ExtEntryTable[NumExtEntryPoints];
682
683 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
684 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
685 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
686 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
687 NumExtEntryPoints++;
688 }
689 }
690
691 return entry;
692}
693
694
695/**
696 * Fill-in the dispatch stub for the named function.
697 *
698 * This function is intended to be called by a hardware driver. When called,
699 * a dispatch stub may be created created for the function. A pointer to this
700 * dispatch function will be returned by glXGetProcAddress.
701 *
702 * \param function_names Array of pointers to function names that should
703 * share a common dispatch offset.
704 * \param parameter_signature String representing the types of the parameters
705 * passed to the named function. Parameter types
706 * are converted to characters using the following
707 * rules:
708 * - 'i' for \c GLint, \c GLuint, and \c GLenum
709 * - 'p' for any pointer type
710 * - 'f' for \c GLfloat and \c GLclampf
711 * - 'd' for \c GLdouble and \c GLclampd
712 *
713 * \returns
714 * The offset in the dispatch table of the named function. A pointer to the
715 * driver's implementation of the named function should be stored at
716 * \c dispatch_table[\c offset].
717 *
718 * \sa glXGetProcAddress
719 *
720 * \warning
721 * This function can only handle up to 8 names at a time. As far as I know,
722 * the maximum number of names ever associated with an existing GL function is
723 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
724 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
725 * too painful of a limitation.
726 *
727 * \todo
728 * Determine whether or not \c parameter_signature should be allowed to be
729 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
730 * pass in an empty string.
731 *
732 * \todo
733 * Determine if code should be added to reject function names that start with
734 * 'glX'.
735 *
736 * \bug
737 * Add code to compare \c parameter_signature with the parameter signature of
738 * a static function. In order to do that, we need to find a way to \b get
739 * the parameter signature of a static function.
740 */
741
742PUBLIC int
743_glapi_add_dispatch( const char * const * function_names,
744 const char * parameter_signature )
745{
746 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
747 const char * const real_sig = (parameter_signature != NULL)
748 ? parameter_signature : "";
749 struct _glapi_function * entry[8];
750 GLboolean is_static[8];
751 unsigned i;
752 unsigned j;
753 int offset = ~0;
754 int new_offset;
755
756
757 (void) memset( is_static, 0, sizeof( is_static ) );
758 (void) memset( entry, 0, sizeof( entry ) );
759
760 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
761 /* Do some trivial validation on the name of the function.
762 */
763
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000764#ifdef MANGLE
Ian Romanick1585c232005-07-28 00:29:51 +0000765 if (!function_names[i] || function_names[i][0] != 'm' || function_names[i][1] != 'g' || function_names[i][2] != 'l')
766 return GL_FALSE;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000767#else
Ian Romanick1585c232005-07-28 00:29:51 +0000768 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
769 return GL_FALSE;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000770#endif
Ian Romanick1585c232005-07-28 00:29:51 +0000771
772
773 /* Determine if the named function already exists. If the function does
774 * exist, it must have the same parameter signature as the function
775 * being added.
776 */
Brian Paul8ad10762002-10-11 17:41:03 +0000777
Ian Romanick1585c232005-07-28 00:29:51 +0000778 new_offset = get_static_proc_offset(function_names[i]);
779 if (new_offset >= 0) {
780 /* FIXME: Make sure the parameter signatures match! How do we get
781 * FIXME: the parameter signature for static functions?
782 */
783
784 if ( (offset != ~0) && (new_offset != offset) ) {
785 return -1;
786 }
787
788 is_static[i] = GL_TRUE;
789 offset = new_offset;
790 }
791
792
793 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
794 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
795 /* The offset may be ~0 if the function name was added by
796 * glXGetProcAddress but never filled in by the driver.
797 */
798
799 if (ExtEntryTable[j].dispatch_offset != ~0) {
800 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
801 != 0) {
802 return -1;
803 }
804
805 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
806 return -1;
807 }
808
809 offset = ExtEntryTable[j].dispatch_offset;
810 }
811
812 entry[i] = & ExtEntryTable[j];
813 break;
814 }
Brian Paul959f8022000-03-19 01:10:11 +0000815 }
816 }
817
Ian Romanick1585c232005-07-28 00:29:51 +0000818
819 if (offset == ~0) {
820 offset = next_dynamic_offset;
821 next_dynamic_offset++;
822 }
823
824
825 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
826 if (! is_static[i] ) {
827 if (entry[i] == NULL) {
828 entry[i] = add_function_name( function_names[i] );
829 if (entry[i] == NULL) {
830 /* FIXME: Possible memory leak here.
831 */
832 return -1;
833 }
834 }
835
836
837 entry[i]->parameter_signature = str_dup(real_sig);
838 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
839 entry[i]->dispatch_offset = offset;
Brian Paul959f8022000-03-19 01:10:11 +0000840 }
Brian Paul959f8022000-03-19 01:10:11 +0000841 }
Ian Romanick1585c232005-07-28 00:29:51 +0000842
843 return offset;
Brian Paul959f8022000-03-19 01:10:11 +0000844}
845
846
Brian Paul767e15a2004-11-27 03:51:11 +0000847/**
Brian Paul959f8022000-03-19 01:10:11 +0000848 * Return offset of entrypoint for named function within dispatch table.
849 */
Adam Jackson791ce022004-12-15 23:14:29 +0000850PUBLIC GLint
Brian Paul959f8022000-03-19 01:10:11 +0000851_glapi_get_proc_offset(const char *funcName)
852{
853 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000854 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000855 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000856 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
857 return ExtEntryTable[i].dispatch_offset;
Brian Paul959f8022000-03-19 01:10:11 +0000858 }
859 }
860
861 /* search static functions */
862 return get_static_proc_offset(funcName);
863}
864
865
866
Brian Paul767e15a2004-11-27 03:51:11 +0000867/**
868 * Return pointer to the named function. If the function name isn't found
869 * in the name of static functions, try generating a new API entrypoint on
870 * the fly with assembly language.
Brian Paul959f8022000-03-19 01:10:11 +0000871 */
Ian Romanick967b0062005-08-10 23:54:15 +0000872_glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000873_glapi_get_proc_address(const char *funcName)
874{
Ian Romanick1585c232005-07-28 00:29:51 +0000875 struct _glapi_function * entry;
Brian Paulb51b0a82001-03-07 05:06:11 +0000876 GLuint i;
Brian Paula6ed6f42003-08-27 14:48:16 +0000877
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000878#ifdef MANGLE
879 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
880 return NULL;
881#else
Brian Paula6ed6f42003-08-27 14:48:16 +0000882 if (funcName[0] != 'g' || funcName[1] != 'l')
883 return NULL;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000884#endif
Brian Paula6ed6f42003-08-27 14:48:16 +0000885
886 /* search extension functions first */
Brian Paul959f8022000-03-19 01:10:11 +0000887 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000888 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
889 return ExtEntryTable[i].dispatch_stub;
Brian Paul959f8022000-03-19 01:10:11 +0000890 }
891 }
892
893 /* search static functions */
Brian Paul54f3aab2002-10-02 01:51:44 +0000894 {
Brian Paul767e15a2004-11-27 03:51:11 +0000895 const _glapi_proc func = get_static_proc_address(funcName);
Brian Paul54f3aab2002-10-02 01:51:44 +0000896 if (func)
897 return func;
898 }
Brian Paul959f8022000-03-19 01:10:11 +0000899
Ian Romanick1585c232005-07-28 00:29:51 +0000900 entry = add_function_name(funcName);
901 return (entry == NULL) ? NULL : entry->dispatch_stub;
Brian Paul54f3aab2002-10-02 01:51:44 +0000902}
Brian Paul959f8022000-03-19 01:10:11 +0000903
904
905
Brian Paul767e15a2004-11-27 03:51:11 +0000906/**
Brian Paul959f8022000-03-19 01:10:11 +0000907 * Return the name of the function at the given dispatch offset.
908 * This is only intended for debugging.
909 */
Ian Romanick967b0062005-08-10 23:54:15 +0000910const char *
Brian Paul959f8022000-03-19 01:10:11 +0000911_glapi_get_proc_name(GLuint offset)
912{
Brian Paul959f8022000-03-19 01:10:11 +0000913 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000914 const char * n;
Brian Paul54f3aab2002-10-02 01:51:44 +0000915
916 /* search built-in functions */
Ian Romanick78677992004-05-27 00:05:13 +0000917 n = get_static_proc_name(offset);
918 if ( n != NULL ) {
919 return n;
Brian Paul959f8022000-03-19 01:10:11 +0000920 }
921
922 /* search added extension functions */
923 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000924 if (ExtEntryTable[i].dispatch_offset == offset) {
925 return ExtEntryTable[i].name;
Brian Paul959f8022000-03-19 01:10:11 +0000926 }
927 }
928 return NULL;
929}
930
931
932
Brian Paul767e15a2004-11-27 03:51:11 +0000933/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000934 * Return size of dispatch table struct as number of functions (or
935 * slots).
936 */
Adam Jackson791ce022004-12-15 23:14:29 +0000937PUBLIC GLuint
Brian Paul54f3aab2002-10-02 01:51:44 +0000938_glapi_get_dispatch_table_size(void)
939{
940 return DISPATCH_TABLE_SIZE;
941}
942
943
944
Brian Paul767e15a2004-11-27 03:51:11 +0000945/**
Brian Paul959f8022000-03-19 01:10:11 +0000946 * Make sure there are no NULL pointers in the given dispatch table.
Brian Paul5104b4d2002-03-07 21:50:41 +0000947 * Intended for debugging purposes.
Brian Paul959f8022000-03-19 01:10:11 +0000948 */
Ian Romanick967b0062005-08-10 23:54:15 +0000949void
Brian Paul959f8022000-03-19 01:10:11 +0000950_glapi_check_table(const struct _glapi_table *table)
951{
Brian Paul5104b4d2002-03-07 21:50:41 +0000952#ifdef DEBUG
Brian Paul959f8022000-03-19 01:10:11 +0000953 const GLuint entries = _glapi_get_dispatch_table_size();
954 const void **tab = (const void **) table;
955 GLuint i;
956 for (i = 1; i < entries; i++) {
957 assert(tab[i]);
958 }
959
Brian Paul959f8022000-03-19 01:10:11 +0000960 /* Do some spot checks to be sure that the dispatch table
961 * slots are assigned correctly.
962 */
963 {
964 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
965 char *BeginFunc = (char*) &table->Begin;
966 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
967 assert(BeginOffset == _gloffset_Begin);
968 assert(BeginOffset == offset);
969 }
970 {
971 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
972 char *viewportFunc = (char*) &table->Viewport;
973 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
974 assert(viewportOffset == _gloffset_Viewport);
975 assert(viewportOffset == offset);
976 }
977 {
978 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
979 char *VertexPointerFunc = (char*) &table->VertexPointer;
980 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
981 assert(VertexPointerOffset == _gloffset_VertexPointer);
982 assert(VertexPointerOffset == offset);
983 }
984 {
985 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
986 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
987 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
988 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
989 assert(ResetMinMaxOffset == offset);
990 }
991 {
992 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
993 char *blendColorFunc = (char*) &table->BlendColor;
994 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
995 assert(blendColorOffset == _gloffset_BlendColor);
996 assert(blendColorOffset == offset);
997 }
998 {
999 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
1000 char *istextureFunc = (char*) &table->IsTextureEXT;
1001 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
1002 assert(istextureOffset == _gloffset_IsTextureEXT);
1003 assert(istextureOffset == offset);
1004 }
Brian Paula14cbff2000-10-27 18:31:21 +00001005 {
1006 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
1007 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1008 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1009 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1010 assert(secondaryColor3fOffset == offset);
Brian Paul767e15a2004-11-27 03:51:11 +00001011 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
Brian Paula14cbff2000-10-27 18:31:21 +00001012 }
Brian Paul91d6f122002-05-29 15:23:16 +00001013 {
1014 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1015 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1016 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1017 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1018 assert(pointParameterivOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001019 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
Brian Paul91d6f122002-05-29 15:23:16 +00001020 }
Brian Paul54f3aab2002-10-02 01:51:44 +00001021 {
1022 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1023 char *setFenceFunc = (char*) &table->SetFenceNV;
1024 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1025 assert(setFenceOffset == _gloffset_SetFenceNV);
1026 assert(setFenceOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001027 assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc) &glSetFenceNV);
Brian Paul54f3aab2002-10-02 01:51:44 +00001028 }
Brian Paula6c423d2004-08-25 15:59:48 +00001029#else
1030 (void) table;
Brian Paul959f8022000-03-19 01:10:11 +00001031#endif
1032}
Ian Romanick25fe93f2005-04-13 20:59:15 +00001033
1034
1035/**
1036 * Perform platform-specific GL API entry-point fixups.
1037 *
1038 *
1039 */
1040static void
1041init_glapi_relocs( void )
1042{
1043#if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1044 extern void * _x86_get_dispatch(void);
1045 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1046 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1047
1048
1049 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1050 (void) memcpy( curr_func, get_disp, 6 );
1051 curr_func += X86_DISPATCH_FUNCTION_SIZE;
1052 }
1053#endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1054}