blob: 2b67b6ab5be2294cda17ad049fce729fa73f6ef9 [file] [log] [blame]
Brian Paulfbd8f211999-11-11 01:22:25 +00001/*
2 * Mesa 3-D graphics library
Brian Paul514a15c2006-03-15 20:56:22 +00003 * Version: 6.5
Brian Paulfbd8f211999-11-11 01:22:25 +00004 *
Brian Paul514a15c2006-03-15 20:56:22 +00005 * Copyright (C) 1999-2006 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 Paulbb72d321999-12-16 17:31:59 +000057
Brian Paul3c257e12001-03-28 17:19:58 +000058/***** BEGIN NO-OP DISPATCH *****/
59
60static GLboolean WarnFlag = GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000061static _glapi_warning_func warning_func;
Brian Paul3c257e12001-03-28 17:19:58 +000062
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +000063#if defined(PTHREADS) || defined(GLX_USE_TLS)
Ian Romanick25fe93f2005-04-13 20:59:15 +000064static void init_glapi_relocs(void);
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +000065#endif
Ian Romanick25fe93f2005-04-13 20:59:15 +000066
67static _glapi_proc generate_entrypoint(GLuint functionOffset);
68static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
Brian Paul4e9676f2002-06-29 19:48:15 +000069
70/*
71 * Enable/disable printing of warning messages.
72 */
Adam Jackson791ce022004-12-15 23:14:29 +000073PUBLIC void
Brian Paul3c257e12001-03-28 17:19:58 +000074_glapi_noop_enable_warnings(GLboolean enable)
75{
76 WarnFlag = enable;
77}
78
Brian Paul4e9676f2002-06-29 19:48:15 +000079/*
80 * Register a callback function for reporting errors.
81 */
Adam Jackson791ce022004-12-15 23:14:29 +000082PUBLIC void
Brian Paul4e9676f2002-06-29 19:48:15 +000083_glapi_set_warning_func( _glapi_warning_func func )
84{
85 warning_func = func;
86}
87
Brian Paul3c257e12001-03-28 17:19:58 +000088static GLboolean
89warn(void)
90{
Ben Crossmand8aa5ff2005-04-15 09:13:21 +000091 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
Brian Paul4e9676f2002-06-29 19:48:15 +000092 && warning_func) {
Brian Paul3c257e12001-03-28 17:19:58 +000093 return GL_TRUE;
Brian Paul4e9676f2002-06-29 19:48:15 +000094 }
95 else {
Brian Paul3c257e12001-03-28 17:19:58 +000096 return GL_FALSE;
Brian Paul4e9676f2002-06-29 19:48:15 +000097 }
Brian Paul3c257e12001-03-28 17:19:58 +000098}
99
100
101#define KEYWORD1 static
Ian Romanick4e4b5f42006-08-22 16:34:38 +0000102#define KEYWORD1_ALT static
Daniel Borca722cb892004-01-07 12:37:09 +0000103#define KEYWORD2 GLAPIENTRY
Brian Paul3c257e12001-03-28 17:19:58 +0000104#define NAME(func) NoOp##func
105
Brian Paul4e9676f2002-06-29 19:48:15 +0000106#define F NULL
Brian Paul3c257e12001-03-28 17:19:58 +0000107
Brian Paul5849e3d2004-11-05 18:32:02 +0000108#define DISPATCH(func, args, msg) \
109 if (warn()) { \
110 warning_func(NULL, "GL User Error: called without context: %s", #func); \
Brian Paul3c257e12001-03-28 17:19:58 +0000111 }
112
Brian Paul5849e3d2004-11-05 18:32:02 +0000113#define RETURN_DISPATCH(func, args, msg) \
114 if (warn()) { \
115 warning_func(NULL, "GL User Error: called without context: %s", #func); \
116 } \
Brian Paul3c257e12001-03-28 17:19:58 +0000117 return 0
118
119#define DISPATCH_TABLE_NAME __glapi_noop_table
Brian Paul767e15a2004-11-27 03:51:11 +0000120#define UNUSED_TABLE_NAME __unused_noop_functions
Brian Paul3c257e12001-03-28 17:19:58 +0000121
Brian Paul767e15a2004-11-27 03:51:11 +0000122#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
Brian Paul3c257e12001-03-28 17:19:58 +0000123
Brian Paula760ccf2004-12-03 15:24:34 +0000124static GLint NoOpUnused(void)
Brian Paul3c257e12001-03-28 17:19:58 +0000125{
126 if (warn()) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000127 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
Brian Paul3c257e12001-03-28 17:19:58 +0000128 }
129 return 0;
130}
131
132#include "glapitemp.h"
133
134/***** END NO-OP DISPATCH *****/
135
136
137
Ian Romanick967b0062005-08-10 23:54:15 +0000138/**
139 * \name Current dispatch and current context control variables
140 *
141 * Depending on whether or not multithreading is support, and the type of
142 * support available, several variables are used to store the current context
143 * pointer and the current dispatch table pointer. In the non-threaded case,
144 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
145 * purpose.
146 *
147 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
148 * \c _glapi_Context will be \c NULL if an application is detected as being
149 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
150 * and \c _glapi_Context just like the case without any threading support.
151 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
152 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
153 * static dispatch functions access these variables via \c _glapi_get_dispatch
154 * and \c _glapi_get_context.
155 *
156 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
157 * possible for the original thread to be setting it at the same instant a new
158 * thread, perhaps running on a different processor, is clearing it. Because
159 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
160 * used to determine whether or not the application is multithreaded.
161 *
162 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
163 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
164 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
165 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
166 * between TLS enabled loaders and non-TLS DRI drivers.
167 */
168/*@{*/
169#if defined(GLX_USE_TLS)
170
171PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
172 __attribute__((tls_model("initial-exec")))
173 = (struct _glapi_table *) __glapi_noop_table;
174
175PUBLIC __thread void * _glapi_tls_Context
176 __attribute__((tls_model("initial-exec")));
177
178PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
179PUBLIC const void *_glapi_Context = NULL;
180
181#else
Brian Paul3c257e12001-03-28 17:19:58 +0000182
183#if defined(THREADS)
184
Ian Romanickd14d1032004-07-02 00:01:09 +0000185static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000186_glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000187static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
Brian Paul3c257e12001-03-28 17:19:58 +0000188
Brian Paul385f23e2006-06-16 14:50:05 +0000189#if defined(WIN32_THREADS)
190void FreeTSD(_glthread_TSD *p);
191void FreeAllTSD(void)
192{
193 FreeTSD(&_gl_DispatchTSD);
194 FreeTSD(&ContextTSD);
195}
196#endif /* defined(WIN32_THREADS) */
197
Ian Romanick967b0062005-08-10 23:54:15 +0000198#endif /* defined(THREADS) */
Brian Paul3c257e12001-03-28 17:19:58 +0000199
Ian Romanick967b0062005-08-10 23:54:15 +0000200PUBLIC struct _glapi_table *_glapi_Dispatch =
201 (struct _glapi_table *) __glapi_noop_table;
Adam Jackson791ce022004-12-15 23:14:29 +0000202PUBLIC void *_glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000203
Ian Romanick25fe93f2005-04-13 20:59:15 +0000204#endif /* defined(GLX_USE_TLS) */
Ian Romanick967b0062005-08-10 23:54:15 +0000205/*@}*/
Brian Paulab0c8862001-01-23 23:35:47 +0000206
Brian Paul54f3aab2002-10-02 01:51:44 +0000207
Brian Paul767e15a2004-11-27 03:51:11 +0000208/**
209 * strdup() is actually not a standard ANSI C or POSIX routine.
Brian Paul3c257e12001-03-28 17:19:58 +0000210 * Irix will not define it if ANSI mode is in effect.
211 */
212static char *
213str_dup(const char *str)
Randy Frankd7361e12000-03-27 21:13:58 +0000214{
Brian Paulfffb8092000-03-29 18:46:11 +0000215 char *copy;
216 copy = (char*) malloc(strlen(str) + 1);
217 if (!copy)
218 return NULL;
219 strcpy(copy, str);
220 return copy;
Randy Frankd7361e12000-03-27 21:13:58 +0000221}
222
Brian Paul7fb54ae1999-11-19 22:33:50 +0000223
Brian Paulbb72d321999-12-16 17:31:59 +0000224
Brian Paul767e15a2004-11-27 03:51:11 +0000225/**
Brian Paulbb72d321999-12-16 17:31:59 +0000226 * We should call this periodically from a function such as glXMakeCurrent
Brian Paul5104b4d2002-03-07 21:50:41 +0000227 * in order to test if multiple threads are being used.
Brian Paulbb72d321999-12-16 17:31:59 +0000228 */
Ian Romanick967b0062005-08-10 23:54:15 +0000229void
Brian Paulbb72d321999-12-16 17:31:59 +0000230_glapi_check_multithread(void)
231{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000232#if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000233 if (!ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000234 static unsigned long knownID;
235 static GLboolean firstCall = GL_TRUE;
236 if (firstCall) {
237 knownID = _glthread_GetID();
238 firstCall = GL_FALSE;
239 }
240 else if (knownID != _glthread_GetID()) {
Ian Romanickd14d1032004-07-02 00:01:09 +0000241 ThreadSafe = GL_TRUE;
Brian Paulbb72d321999-12-16 17:31:59 +0000242 _glapi_set_dispatch(NULL);
243 }
244 }
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000245 else if (!_glapi_get_dispatch()) {
246 /* make sure that this thread's dispatch pointer isn't null */
247 _glapi_set_dispatch(NULL);
248 }
Brian Paulbb72d321999-12-16 17:31:59 +0000249#endif
250}
251
252
253
Brian Paul767e15a2004-11-27 03:51:11 +0000254/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000255 * Set the current context pointer for this thread.
256 * The context pointer is an opaque type which should be cast to
257 * void from the real context pointer type.
258 */
Adam Jackson791ce022004-12-15 23:14:29 +0000259PUBLIC void
Brian Paulf9b97d92000-01-28 20:17:42 +0000260_glapi_set_context(void *context)
Brian Paul8f91fb61999-12-17 14:51:28 +0000261{
Brian Paul767e15a2004-11-27 03:51:11 +0000262 (void) __unused_noop_functions; /* silence a warning */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000263#if defined(GLX_USE_TLS)
264 _glapi_tls_Context = context;
265#elif defined(THREADS)
Brian Paul3c27be32000-02-10 21:27:48 +0000266 _glthread_SetTSD(&ContextTSD, context);
Ian Romanickd14d1032004-07-02 00:01:09 +0000267 _glapi_Context = (ThreadSafe) ? NULL : context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000268#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000269 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000270#endif
271}
272
273
274
Brian Paul767e15a2004-11-27 03:51:11 +0000275/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000276 * Get the current context pointer for this thread.
277 * The context pointer is an opaque type which should be cast from
278 * void to the real context pointer type.
279 */
Adam Jackson791ce022004-12-15 23:14:29 +0000280PUBLIC void *
Brian Paulf9b97d92000-01-28 20:17:42 +0000281_glapi_get_context(void)
Brian Paul8f91fb61999-12-17 14:51:28 +0000282{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000283#if defined(GLX_USE_TLS)
284 return _glapi_tls_Context;
285#elif defined(THREADS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000286 if (ThreadSafe) {
Brian Paul8f91fb61999-12-17 14:51:28 +0000287 return _glthread_GetTSD(&ContextTSD);
288 }
289 else {
Brian Paulf9b97d92000-01-28 20:17:42 +0000290 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000291 }
292#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000293 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000294#endif
295}
296
297
298
Brian Paul767e15a2004-11-27 03:51:11 +0000299/**
Brian Paul7fb54ae1999-11-19 22:33:50 +0000300 * Set the global or per-thread dispatch table pointer.
Brian Paul514a15c2006-03-15 20:56:22 +0000301 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
302 * table (__glapi_noop_table).
Brian Paul7fb54ae1999-11-19 22:33:50 +0000303 */
Adam Jackson791ce022004-12-15 23:14:29 +0000304PUBLIC void
Brian Paul7fb54ae1999-11-19 22:33:50 +0000305_glapi_set_dispatch(struct _glapi_table *dispatch)
306{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000307#if defined(PTHREADS) || defined(GLX_USE_TLS)
308 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000309 pthread_once( & once_control, init_glapi_relocs );
310#endif
311
Ian Romanick6c5402b2004-07-05 22:40:54 +0000312 if (!dispatch) {
313 /* use the no-op functions */
314 dispatch = (struct _glapi_table *) __glapi_noop_table;
315 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000316#ifdef DEBUG
Ian Romanick6c5402b2004-07-05 22:40:54 +0000317 else {
Brian Paul7fb54ae1999-11-19 22:33:50 +0000318 _glapi_check_table(dispatch);
319 }
320#endif
321
Ian Romanick25fe93f2005-04-13 20:59:15 +0000322#if defined(GLX_USE_TLS)
Ian Romanick967b0062005-08-10 23:54:15 +0000323 _glapi_tls_Dispatch = dispatch;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000324#elif defined(THREADS)
Ian Romanick967b0062005-08-10 23:54:15 +0000325 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
326 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000327#else /*THREADS*/
Ian Romanick967b0062005-08-10 23:54:15 +0000328 _glapi_Dispatch = dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000329#endif /*THREADS*/
Brian Paul7fb54ae1999-11-19 22:33:50 +0000330}
331
332
Brian Paulbb72d321999-12-16 17:31:59 +0000333
Brian Paul767e15a2004-11-27 03:51:11 +0000334/**
Brian Paulbb72d321999-12-16 17:31:59 +0000335 * Return pointer to current dispatch table for calling thread.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000336 */
Adam Jackson791ce022004-12-15 23:14:29 +0000337PUBLIC struct _glapi_table *
Brian Paul7fb54ae1999-11-19 22:33:50 +0000338_glapi_get_dispatch(void)
339{
Ian Romanick967b0062005-08-10 23:54:15 +0000340 struct _glapi_table * api;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000341#if defined(GLX_USE_TLS)
Ian Romanick967b0062005-08-10 23:54:15 +0000342 api = _glapi_tls_Dispatch;
343#elif defined(THREADS)
344 api = (ThreadSafe)
345 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
346 : _glapi_Dispatch;
347#else
348 api = _glapi_Dispatch;
349#endif
Ian Romanick25fe93f2005-04-13 20:59:15 +0000350 return api;
Brian Paul3a71d052000-09-05 20:17:37 +0000351}
Brian Paulab0c8862001-01-23 23:35:47 +0000352
Brian Paul3a71d052000-09-05 20:17:37 +0000353
Brian Paul514a15c2006-03-15 20:56:22 +0000354
355/***
356 *** The rest of this file is pretty much concerned with GetProcAddress
357 *** functionality.
358 ***/
359
Ian Romanickd319edf2006-08-22 18:22:20 +0000360#if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
361# define DISPATCH_FUNCTION_SIZE 16
362#elif defined(USE_X86_ASM)
363# if defined(THREADS) && !defined(GLX_USE_TLS)
364# define DISPATCH_FUNCTION_SIZE 32
365# else
366# define DISPATCH_FUNCTION_SIZE 16
367# endif
368#endif
369
370#if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
371# define NEED_FUNCTION_POINTER
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000372#endif
Ian Romanick78677992004-05-27 00:05:13 +0000373
Brian Paul54f3aab2002-10-02 01:51:44 +0000374/* The code in this file is auto-generated with Python */
Brian Paulb5fd8862001-11-18 22:48:11 +0000375#include "glprocs.h"
Brian Paul7fb54ae1999-11-19 22:33:50 +0000376
Brian Paul959f8022000-03-19 01:10:11 +0000377
Brian Paul767e15a2004-11-27 03:51:11 +0000378/**
379 * Search the table of static entrypoint functions for the named function
380 * and return the corresponding glprocs_table_t entry.
381 */
Ian Romanick78677992004-05-27 00:05:13 +0000382static const glprocs_table_t *
383find_entry( const char * n )
384{
Brian Paula760ccf2004-12-03 15:24:34 +0000385 GLuint i;
Brian Paula760ccf2004-12-03 15:24:34 +0000386 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Brian Paul514a15c2006-03-15 20:56:22 +0000387 const char *testName = gl_string_table + static_functions[i].Name_offset;
388 if (strcmp(testName, n) == 0) {
389 return &static_functions[i];
Ian Romanick78677992004-05-27 00:05:13 +0000390 }
391 }
392 return NULL;
393}
394
Brian Paul767e15a2004-11-27 03:51:11 +0000395
396/**
Brian Paul959f8022000-03-19 01:10:11 +0000397 * Return dispatch table offset of the named static (built-in) function.
398 * Return -1 if function not found.
399 */
400static GLint
401get_static_proc_offset(const char *funcName)
402{
Ian Romanick78677992004-05-27 00:05:13 +0000403 const glprocs_table_t * const f = find_entry( funcName );
Brian Paul514a15c2006-03-15 20:56:22 +0000404 if (f) {
Ian Romanick78677992004-05-27 00:05:13 +0000405 return f->Offset;
Brian Paul959f8022000-03-19 01:10:11 +0000406 }
407 return -1;
408}
409
410
Brian Paul514a15c2006-03-15 20:56:22 +0000411#if !defined(XFree86Server) && !defined(XGLServer)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000412#ifdef USE_X86_ASM
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000413
Ian Romanick25fe93f2005-04-13 20:59:15 +0000414#if defined( GLX_USE_TLS )
415extern GLubyte gl_dispatch_functions_start[];
416extern GLubyte gl_dispatch_functions_end[];
417#else
418extern const GLubyte gl_dispatch_functions_start[];
419#endif
420
Brian Paul514a15c2006-03-15 20:56:22 +0000421#endif /* USE_X86_ASM */
422
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000423
Brian Paul767e15a2004-11-27 03:51:11 +0000424/**
Brian Paul514a15c2006-03-15 20:56:22 +0000425 * Return dispatch function address for the named static (built-in) function.
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000426 * Return NULL if function not found.
427 */
Brian Paul09c8e412006-04-13 01:51:25 +0000428static _glapi_proc
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000429get_static_proc_address(const char *funcName)
430{
431 const glprocs_table_t * const f = find_entry( funcName );
Brian Paul514a15c2006-03-15 20:56:22 +0000432 if (f) {
Ian Romanickf3f51bc2006-10-11 22:37:14 +0000433#if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
434 return (f->Address == NULL)
435 ? (_glapi_proc) (gl_dispatch_functions_start
436 + (DISPATCH_FUNCTION_SIZE * f->Offset));
437 : f->Address;
438#elif defined(DISPATCH_FUNCTION_SIZE)
Brian Paul209bd3a2004-11-27 04:02:32 +0000439 return (_glapi_proc) (gl_dispatch_functions_start
Ian Romanickd319edf2006-08-22 18:22:20 +0000440 + (DISPATCH_FUNCTION_SIZE * f->Offset));
Brian Paul514a15c2006-03-15 20:56:22 +0000441#else
442 return f->Address;
443#endif
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000444 }
445 else {
446 return NULL;
447 }
448}
449
Brian Paul514a15c2006-03-15 20:56:22 +0000450#endif /* !defined(XFree86Server) && !defined(XGLServer) */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000451
452
Ian Romanick78677992004-05-27 00:05:13 +0000453
Brian Paul767e15a2004-11-27 03:51:11 +0000454/**
455 * Return the name of the function at the given offset in the dispatch
456 * table. For debugging only.
457 */
Ian Romanick78677992004-05-27 00:05:13 +0000458static const char *
459get_static_proc_name( GLuint offset )
460{
Brian Paula760ccf2004-12-03 15:24:34 +0000461 GLuint i;
Brian Paula760ccf2004-12-03 15:24:34 +0000462 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000463 if (static_functions[i].Offset == offset) {
464 return gl_string_table + static_functions[i].Name_offset;
Brian Paul9c7ca852000-10-19 20:13:12 +0000465 }
466 }
467 return NULL;
Brian Paul959f8022000-03-19 01:10:11 +0000468}
469
470
471
472/**********************************************************************
473 * Extension function management.
474 */
475
Brian Paul54f3aab2002-10-02 01:51:44 +0000476/*
477 * Number of extension functions which we can dynamically add at runtime.
478 */
479#define MAX_EXTENSION_FUNCS 300
Brian Paul959f8022000-03-19 01:10:11 +0000480
Brian Paul54f3aab2002-10-02 01:51:44 +0000481
482/*
Brian Paul767e15a2004-11-27 03:51:11 +0000483 * The dispatch table size (number of entries) is the size of the
Brian Paul54f3aab2002-10-02 01:51:44 +0000484 * _glapi_table struct plus the number of dynamic entries we can add.
485 * The extra slots can be filled in by DRI drivers that register new extension
486 * functions.
487 */
488#define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
489
Brian Paul959f8022000-03-19 01:10:11 +0000490
Ian Romanick1585c232005-07-28 00:29:51 +0000491/**
492 * Track information about a function added to the GL API.
493 */
494struct _glapi_function {
495 /**
496 * Name of the function.
497 */
498 const char * name;
499
500
501 /**
502 * Text string that describes the types of the parameters passed to the
503 * named function. Parameter types are converted to characters using the
504 * following rules:
505 * - 'i' for \c GLint, \c GLuint, and \c GLenum
506 * - 'p' for any pointer type
507 * - 'f' for \c GLfloat and \c GLclampf
508 * - 'd' for \c GLdouble and \c GLclampd
509 */
510 const char * parameter_signature;
511
512
513 /**
514 * Offset in the dispatch table where the pointer to the real function is
515 * located. If the driver has not requested that the named function be
516 * added to the dispatch table, this will have the value ~0.
517 */
518 unsigned dispatch_offset;
519
520
521 /**
522 * Pointer to the dispatch stub for the named function.
523 *
524 * \todo
525 * The semantic of this field should be changed slightly. Currently, it
526 * is always expected to be non-\c NULL. However, it would be better to
527 * only allocate the entry-point stub when the application requests the
528 * function via \c glXGetProcAddress. This would save memory for all the
529 * functions that the driver exports but that the application never wants
530 * to call.
531 */
532 _glapi_proc dispatch_stub;
Brian Paul767e15a2004-11-27 03:51:11 +0000533};
534
535
Ian Romanick1585c232005-07-28 00:29:51 +0000536static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
Brian Paul959f8022000-03-19 01:10:11 +0000537static GLuint NumExtEntryPoints = 0;
538
davem694a497e62001-06-06 22:55:28 +0000539#ifdef USE_SPARC_ASM
540extern void __glapi_sparc_icache_flush(unsigned int *);
541#endif
Brian Paul959f8022000-03-19 01:10:11 +0000542
Brian Paul767e15a2004-11-27 03:51:11 +0000543/**
Brian Paul959f8022000-03-19 01:10:11 +0000544 * Generate a dispatch function (entrypoint) which jumps through
545 * the given slot number (offset) in the current dispatch table.
546 * We need assembly language in order to accomplish this.
547 */
Brian Paul767e15a2004-11-27 03:51:11 +0000548static _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000549generate_entrypoint(GLuint functionOffset)
550{
551#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000552 /* 32 is chosen as something of a magic offset. For x86, the dispatch
553 * at offset 32 is the first one where the offset in the
554 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
Brian Paul959f8022000-03-19 01:10:11 +0000555 */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000556 const GLubyte * const template_func = gl_dispatch_functions_start
Ian Romanickd319edf2006-08-22 18:22:20 +0000557 + (DISPATCH_FUNCTION_SIZE * 32);
558 GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
Brian Paul959f8022000-03-19 01:10:11 +0000559
Ian Romanick25fe93f2005-04-13 20:59:15 +0000560
561 if ( code != NULL ) {
Ian Romanickd319edf2006-08-22 18:22:20 +0000562 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
Ian Romanick25fe93f2005-04-13 20:59:15 +0000563 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
Brian Paul959f8022000-03-19 01:10:11 +0000564 }
Ian Romanick25fe93f2005-04-13 20:59:15 +0000565
Brian Paul767e15a2004-11-27 03:51:11 +0000566 return (_glapi_proc) code;
davem69775355a2001-06-05 23:54:00 +0000567#elif defined(USE_SPARC_ASM)
568
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000569#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000570 static const unsigned int insn_template[] = {
571 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
572 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
573 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
574 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
575 0x8528b020, /* sllx %g2, 32, %g2 */
576 0xc2584002, /* ldx [%g1 + %g2], %g1 */
577 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
578 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
579 0xc6584002, /* ldx [%g1 + %g2], %g3 */
580 0x81c0c000, /* jmpl %g3, %g0 */
581 0x01000000 /* nop */
582 };
583#else
584 static const unsigned int insn_template[] = {
585 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
586 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
587 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
588 0x81c0c000, /* jmpl %g3, %g0 */
589 0x01000000 /* nop */
590 };
Brian Paul514a15c2006-03-15 20:56:22 +0000591#endif /* __arch64__ */
Brian Pauldec2a4d2002-10-29 15:03:14 +0000592 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
davem69775355a2001-06-05 23:54:00 +0000593 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
594 if (code) {
595 memcpy(code, insn_template, sizeof(insn_template));
596
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000597#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000598 code[0] |= (glapi_addr >> (32 + 10));
599 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
davem694a497e62001-06-06 22:55:28 +0000600 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000601 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
602 code[3] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000603 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000604 code[6] |= ((functionOffset * 8) >> 10);
605 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000606 __glapi_sparc_icache_flush(&code[6]);
davem69775355a2001-06-05 23:54:00 +0000607#else
608 code[0] |= (glapi_addr >> 10);
609 code[1] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000610 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000611 code[2] |= (functionOffset * 4);
davem694a497e62001-06-06 22:55:28 +0000612 __glapi_sparc_icache_flush(&code[2]);
Brian Paul514a15c2006-03-15 20:56:22 +0000613#endif /* __arch64__ */
davem69775355a2001-06-05 23:54:00 +0000614 }
Brian Paul767e15a2004-11-27 03:51:11 +0000615 return (_glapi_proc) code;
Brian Paul959f8022000-03-19 01:10:11 +0000616#else
Brian Paula6c423d2004-08-25 15:59:48 +0000617 (void) functionOffset;
Brian Paul959f8022000-03-19 01:10:11 +0000618 return NULL;
Brian Paul54f3aab2002-10-02 01:51:44 +0000619#endif /* USE_*_ASM */
Brian Paul959f8022000-03-19 01:10:11 +0000620}
621
622
Brian Paul767e15a2004-11-27 03:51:11 +0000623/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000624 * This function inserts a new dispatch offset into the assembly language
625 * stub that was generated with the preceeding function.
626 */
627static void
Brian Paul767e15a2004-11-27 03:51:11 +0000628fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
Brian Paul54f3aab2002-10-02 01:51:44 +0000629{
630#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000631 GLubyte * const code = (GLubyte *) entrypoint;
Brian Paul54f3aab2002-10-02 01:51:44 +0000632
Ian Romanickd319edf2006-08-22 18:22:20 +0000633#if DISPATCH_FUNCTION_SIZE == 32
Ian Romanick25fe93f2005-04-13 20:59:15 +0000634 *((unsigned int *)(code + 11)) = 4 * offset;
635 *((unsigned int *)(code + 22)) = 4 * offset;
Ian Romanickd319edf2006-08-22 18:22:20 +0000636#elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
Ian Romanick25fe93f2005-04-13 20:59:15 +0000637 *((unsigned int *)(code + 8)) = 4 * offset;
Ian Romanickd319edf2006-08-22 18:22:20 +0000638#elif DISPATCH_FUNCTION_SIZE == 16
Ian Romanick25fe93f2005-04-13 20:59:15 +0000639 *((unsigned int *)(code + 7)) = 4 * offset;
640#else
Ian Romanickd319edf2006-08-22 18:22:20 +0000641# error Invalid DISPATCH_FUNCTION_SIZE!
Ian Romanick25fe93f2005-04-13 20:59:15 +0000642#endif
Brian Paul54f3aab2002-10-02 01:51:44 +0000643
644#elif defined(USE_SPARC_ASM)
645
646 /* XXX this hasn't been tested! */
647 unsigned int *code = (unsigned int *) entrypoint;
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000648#ifdef __arch64__
Brian Paul54f3aab2002-10-02 01:51:44 +0000649 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
650 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
651 code[6] |= ((offset * 8) >> 10);
652 code[7] |= ((offset * 8) & ((1 << 10) - 1));
653 __glapi_sparc_icache_flush(&code[6]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000654#else /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000655 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
Brian Paul944ea202002-10-17 16:29:17 +0000656 code[2] |= (offset * 4);
Brian Paul54f3aab2002-10-02 01:51:44 +0000657 __glapi_sparc_icache_flush(&code[2]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000658#endif /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000659
Brian Paula6c423d2004-08-25 15:59:48 +0000660#else
661
662 /* an unimplemented architecture */
663 (void) entrypoint;
664 (void) offset;
665
Brian Paul54f3aab2002-10-02 01:51:44 +0000666#endif /* USE_*_ASM */
667}
668
Brian Paul959f8022000-03-19 01:10:11 +0000669
Brian Paul767e15a2004-11-27 03:51:11 +0000670/**
Ian Romanick1585c232005-07-28 00:29:51 +0000671 * Generate new entrypoint
672 *
673 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
674 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
675 * never happens, and the user calls this function, he'll segfault. That's
676 * what you get when you try calling a GL function that doesn't really exist.
677 *
678 * \param funcName Name of the function to create an entry-point for.
679 *
680 * \sa _glapi_add_entrypoint
Brian Paul959f8022000-03-19 01:10:11 +0000681 */
Ian Romanick1585c232005-07-28 00:29:51 +0000682
683static struct _glapi_function *
684add_function_name( const char * funcName )
Brian Paul959f8022000-03-19 01:10:11 +0000685{
Ian Romanick1585c232005-07-28 00:29:51 +0000686 struct _glapi_function * entry = NULL;
687
688 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
689 _glapi_proc entrypoint = generate_entrypoint(~0);
690 if (entrypoint != NULL) {
691 entry = & ExtEntryTable[NumExtEntryPoints];
692
693 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
694 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
695 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
696 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
697 NumExtEntryPoints++;
698 }
699 }
700
701 return entry;
702}
703
704
705/**
706 * Fill-in the dispatch stub for the named function.
707 *
708 * This function is intended to be called by a hardware driver. When called,
709 * a dispatch stub may be created created for the function. A pointer to this
710 * dispatch function will be returned by glXGetProcAddress.
711 *
712 * \param function_names Array of pointers to function names that should
713 * share a common dispatch offset.
714 * \param parameter_signature String representing the types of the parameters
715 * passed to the named function. Parameter types
716 * are converted to characters using the following
717 * rules:
718 * - 'i' for \c GLint, \c GLuint, and \c GLenum
719 * - 'p' for any pointer type
720 * - 'f' for \c GLfloat and \c GLclampf
721 * - 'd' for \c GLdouble and \c GLclampd
722 *
723 * \returns
724 * The offset in the dispatch table of the named function. A pointer to the
725 * driver's implementation of the named function should be stored at
726 * \c dispatch_table[\c offset].
727 *
728 * \sa glXGetProcAddress
729 *
730 * \warning
731 * This function can only handle up to 8 names at a time. As far as I know,
732 * the maximum number of names ever associated with an existing GL function is
733 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
734 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
735 * too painful of a limitation.
736 *
737 * \todo
738 * Determine whether or not \c parameter_signature should be allowed to be
739 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
740 * pass in an empty string.
741 *
742 * \todo
743 * Determine if code should be added to reject function names that start with
744 * 'glX'.
745 *
746 * \bug
747 * Add code to compare \c parameter_signature with the parameter signature of
748 * a static function. In order to do that, we need to find a way to \b get
749 * the parameter signature of a static function.
750 */
751
752PUBLIC int
753_glapi_add_dispatch( const char * const * function_names,
754 const char * parameter_signature )
755{
756 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
757 const char * const real_sig = (parameter_signature != NULL)
758 ? parameter_signature : "";
759 struct _glapi_function * entry[8];
760 GLboolean is_static[8];
761 unsigned i;
762 unsigned j;
763 int offset = ~0;
764 int new_offset;
765
766
767 (void) memset( is_static, 0, sizeof( is_static ) );
768 (void) memset( entry, 0, sizeof( entry ) );
769
770 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
771 /* Do some trivial validation on the name of the function.
772 */
773
Ian Romanick1585c232005-07-28 00:29:51 +0000774 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
775 return GL_FALSE;
Ian Romanick1585c232005-07-28 00:29:51 +0000776
777 /* Determine if the named function already exists. If the function does
778 * exist, it must have the same parameter signature as the function
779 * being added.
780 */
Brian Paul8ad10762002-10-11 17:41:03 +0000781
Ian Romanick1585c232005-07-28 00:29:51 +0000782 new_offset = get_static_proc_offset(function_names[i]);
783 if (new_offset >= 0) {
784 /* FIXME: Make sure the parameter signatures match! How do we get
785 * FIXME: the parameter signature for static functions?
786 */
787
788 if ( (offset != ~0) && (new_offset != offset) ) {
789 return -1;
790 }
791
792 is_static[i] = GL_TRUE;
793 offset = new_offset;
794 }
795
796
797 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
798 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
799 /* The offset may be ~0 if the function name was added by
800 * glXGetProcAddress but never filled in by the driver.
801 */
802
803 if (ExtEntryTable[j].dispatch_offset != ~0) {
804 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
805 != 0) {
806 return -1;
807 }
808
809 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
810 return -1;
811 }
812
813 offset = ExtEntryTable[j].dispatch_offset;
814 }
815
816 entry[i] = & ExtEntryTable[j];
817 break;
818 }
Brian Paul959f8022000-03-19 01:10:11 +0000819 }
820 }
821
Ian Romanick1585c232005-07-28 00:29:51 +0000822 if (offset == ~0) {
823 offset = next_dynamic_offset;
824 next_dynamic_offset++;
825 }
826
Ian Romanick1585c232005-07-28 00:29:51 +0000827 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
Ian Romanick1585c232005-07-28 00:29:51 +0000838 entry[i]->parameter_signature = str_dup(real_sig);
839 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
840 entry[i]->dispatch_offset = offset;
Brian Paul959f8022000-03-19 01:10:11 +0000841 }
Brian Paul959f8022000-03-19 01:10:11 +0000842 }
Ian Romanick1585c232005-07-28 00:29:51 +0000843
844 return offset;
Brian Paul959f8022000-03-19 01:10:11 +0000845}
846
847
Brian Paul767e15a2004-11-27 03:51:11 +0000848/**
Brian Paul959f8022000-03-19 01:10:11 +0000849 * Return offset of entrypoint for named function within dispatch table.
850 */
Adam Jackson791ce022004-12-15 23:14:29 +0000851PUBLIC GLint
Brian Paul959f8022000-03-19 01:10:11 +0000852_glapi_get_proc_offset(const char *funcName)
853{
854 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000855 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000856 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000857 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
858 return ExtEntryTable[i].dispatch_offset;
Brian Paul959f8022000-03-19 01:10:11 +0000859 }
860 }
Brian Paul959f8022000-03-19 01:10:11 +0000861 /* 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
Dave Airlief2363002006-02-10 21:46:17 +0000893#if !defined( XFree86Server ) && !defined( XGLServer )
Brian Paul959f8022000-03-19 01:10:11 +0000894 /* search static functions */
Brian Paul54f3aab2002-10-02 01:51:44 +0000895 {
Brian Paul767e15a2004-11-27 03:51:11 +0000896 const _glapi_proc func = get_static_proc_address(funcName);
Brian Paul54f3aab2002-10-02 01:51:44 +0000897 if (func)
898 return func;
899 }
Ian Romanick44b1bd72005-10-11 16:56:39 +0000900#endif /* !defined( XFree86Server ) */
Brian Paul959f8022000-03-19 01:10:11 +0000901
Ian Romanick1585c232005-07-28 00:29:51 +0000902 entry = add_function_name(funcName);
903 return (entry == NULL) ? NULL : entry->dispatch_stub;
Brian Paul54f3aab2002-10-02 01:51:44 +0000904}
Brian Paul959f8022000-03-19 01:10:11 +0000905
906
907
Brian Paul767e15a2004-11-27 03:51:11 +0000908/**
Brian Paul959f8022000-03-19 01:10:11 +0000909 * Return the name of the function at the given dispatch offset.
910 * This is only intended for debugging.
911 */
Ian Romanick967b0062005-08-10 23:54:15 +0000912const char *
Brian Paul959f8022000-03-19 01:10:11 +0000913_glapi_get_proc_name(GLuint offset)
914{
Brian Paul959f8022000-03-19 01:10:11 +0000915 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000916 const char * n;
Brian Paul54f3aab2002-10-02 01:51:44 +0000917
918 /* search built-in functions */
Ian Romanick78677992004-05-27 00:05:13 +0000919 n = get_static_proc_name(offset);
920 if ( n != NULL ) {
921 return n;
Brian Paul959f8022000-03-19 01:10:11 +0000922 }
923
924 /* search added extension functions */
925 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000926 if (ExtEntryTable[i].dispatch_offset == offset) {
927 return ExtEntryTable[i].name;
Brian Paul959f8022000-03-19 01:10:11 +0000928 }
929 }
930 return NULL;
931}
932
933
934
Brian Paul767e15a2004-11-27 03:51:11 +0000935/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000936 * Return size of dispatch table struct as number of functions (or
937 * slots).
938 */
Adam Jackson791ce022004-12-15 23:14:29 +0000939PUBLIC GLuint
Brian Paul54f3aab2002-10-02 01:51:44 +0000940_glapi_get_dispatch_table_size(void)
941{
942 return DISPATCH_TABLE_SIZE;
943}
944
945
946
Brian Paul767e15a2004-11-27 03:51:11 +0000947/**
Brian Paul959f8022000-03-19 01:10:11 +0000948 * Make sure there are no NULL pointers in the given dispatch table.
Brian Paul5104b4d2002-03-07 21:50:41 +0000949 * Intended for debugging purposes.
Brian Paul959f8022000-03-19 01:10:11 +0000950 */
Ian Romanick967b0062005-08-10 23:54:15 +0000951void
Brian Paul959f8022000-03-19 01:10:11 +0000952_glapi_check_table(const struct _glapi_table *table)
953{
Brian Paul5104b4d2002-03-07 21:50:41 +0000954#ifdef DEBUG
Brian Paul959f8022000-03-19 01:10:11 +0000955 const GLuint entries = _glapi_get_dispatch_table_size();
956 const void **tab = (const void **) table;
957 GLuint i;
958 for (i = 1; i < entries; i++) {
959 assert(tab[i]);
960 }
961
Brian Paul959f8022000-03-19 01:10:11 +0000962 /* Do some spot checks to be sure that the dispatch table
963 * slots are assigned correctly.
964 */
965 {
966 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
967 char *BeginFunc = (char*) &table->Begin;
968 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
969 assert(BeginOffset == _gloffset_Begin);
970 assert(BeginOffset == offset);
971 }
972 {
973 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
974 char *viewportFunc = (char*) &table->Viewport;
975 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
976 assert(viewportOffset == _gloffset_Viewport);
977 assert(viewportOffset == offset);
978 }
979 {
980 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
981 char *VertexPointerFunc = (char*) &table->VertexPointer;
982 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
983 assert(VertexPointerOffset == _gloffset_VertexPointer);
984 assert(VertexPointerOffset == offset);
985 }
986 {
987 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
988 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
989 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
990 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
991 assert(ResetMinMaxOffset == offset);
992 }
993 {
994 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
995 char *blendColorFunc = (char*) &table->BlendColor;
996 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
997 assert(blendColorOffset == _gloffset_BlendColor);
998 assert(blendColorOffset == offset);
999 }
1000 {
1001 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
1002 char *istextureFunc = (char*) &table->IsTextureEXT;
1003 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
1004 assert(istextureOffset == _gloffset_IsTextureEXT);
1005 assert(istextureOffset == offset);
1006 }
Brian Paula14cbff2000-10-27 18:31:21 +00001007 {
1008 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
1009 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1010 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1011 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1012 assert(secondaryColor3fOffset == offset);
Brian Paul767e15a2004-11-27 03:51:11 +00001013 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
Brian Paula14cbff2000-10-27 18:31:21 +00001014 }
Brian Paul91d6f122002-05-29 15:23:16 +00001015 {
1016 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1017 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1018 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1019 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1020 assert(pointParameterivOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001021 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
Brian Paul91d6f122002-05-29 15:23:16 +00001022 }
Brian Paul54f3aab2002-10-02 01:51:44 +00001023 {
1024 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1025 char *setFenceFunc = (char*) &table->SetFenceNV;
1026 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1027 assert(setFenceOffset == _gloffset_SetFenceNV);
1028 assert(setFenceOffset == offset);
Brian Paul54f3aab2002-10-02 01:51:44 +00001029 }
Brian Paula6c423d2004-08-25 15:59:48 +00001030#else
1031 (void) table;
Brian Paul959f8022000-03-19 01:10:11 +00001032#endif
1033}
Ian Romanick25fe93f2005-04-13 20:59:15 +00001034
1035
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +00001036#if defined(PTHREADS) || defined(GLX_USE_TLS)
Ian Romanick25fe93f2005-04-13 20:59:15 +00001037/**
1038 * Perform platform-specific GL API entry-point fixups.
1039 *
1040 *
1041 */
1042static void
1043init_glapi_relocs( void )
1044{
1045#if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1046 extern void * _x86_get_dispatch(void);
1047 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1048 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1049
1050
1051 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1052 (void) memcpy( curr_func, get_disp, 6 );
Ian Romanickd319edf2006-08-22 18:22:20 +00001053 curr_func += DISPATCH_FUNCTION_SIZE;
Ian Romanick25fe93f2005-04-13 20:59:15 +00001054 }
1055#endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1056}
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +00001057#endif