blob: 3f2ec270fef6e9ea5f0180d6b57477da80c19a65 [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
Daniel Borca722cb892004-01-07 12:37:09 +0000102#define KEYWORD2 GLAPIENTRY
Brian Paul3c257e12001-03-28 17:19:58 +0000103#define NAME(func) NoOp##func
104
Brian Paul4e9676f2002-06-29 19:48:15 +0000105#define F NULL
Brian Paul3c257e12001-03-28 17:19:58 +0000106
Brian Paul5849e3d2004-11-05 18:32:02 +0000107#define DISPATCH(func, args, msg) \
108 if (warn()) { \
109 warning_func(NULL, "GL User Error: called without context: %s", #func); \
Brian Paul3c257e12001-03-28 17:19:58 +0000110 }
111
Brian Paul5849e3d2004-11-05 18:32:02 +0000112#define RETURN_DISPATCH(func, args, msg) \
113 if (warn()) { \
114 warning_func(NULL, "GL User Error: called without context: %s", #func); \
115 } \
Brian Paul3c257e12001-03-28 17:19:58 +0000116 return 0
117
118#define DISPATCH_TABLE_NAME __glapi_noop_table
Brian Paul767e15a2004-11-27 03:51:11 +0000119#define UNUSED_TABLE_NAME __unused_noop_functions
Brian Paul3c257e12001-03-28 17:19:58 +0000120
Brian Paul767e15a2004-11-27 03:51:11 +0000121#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
Brian Paul3c257e12001-03-28 17:19:58 +0000122
Brian Paula760ccf2004-12-03 15:24:34 +0000123static GLint NoOpUnused(void)
Brian Paul3c257e12001-03-28 17:19:58 +0000124{
125 if (warn()) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000126 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
Brian Paul3c257e12001-03-28 17:19:58 +0000127 }
128 return 0;
129}
130
131#include "glapitemp.h"
132
133/***** END NO-OP DISPATCH *****/
134
135
136
Ian Romanick967b0062005-08-10 23:54:15 +0000137/**
138 * \name Current dispatch and current context control variables
139 *
140 * Depending on whether or not multithreading is support, and the type of
141 * support available, several variables are used to store the current context
142 * pointer and the current dispatch table pointer. In the non-threaded case,
143 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
144 * purpose.
145 *
146 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
147 * \c _glapi_Context will be \c NULL if an application is detected as being
148 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
149 * and \c _glapi_Context just like the case without any threading support.
150 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
151 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
152 * static dispatch functions access these variables via \c _glapi_get_dispatch
153 * and \c _glapi_get_context.
154 *
155 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
156 * possible for the original thread to be setting it at the same instant a new
157 * thread, perhaps running on a different processor, is clearing it. Because
158 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
159 * used to determine whether or not the application is multithreaded.
160 *
161 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
162 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
163 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
164 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
165 * between TLS enabled loaders and non-TLS DRI drivers.
166 */
167/*@{*/
168#if defined(GLX_USE_TLS)
169
170PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
171 __attribute__((tls_model("initial-exec")))
172 = (struct _glapi_table *) __glapi_noop_table;
173
174PUBLIC __thread void * _glapi_tls_Context
175 __attribute__((tls_model("initial-exec")));
176
177PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
178PUBLIC const void *_glapi_Context = NULL;
179
180#else
Brian Paul3c257e12001-03-28 17:19:58 +0000181
182#if defined(THREADS)
183
Ian Romanickd14d1032004-07-02 00:01:09 +0000184static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000185_glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000186static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
Brian Paul3c257e12001-03-28 17:19:58 +0000187
Ian Romanick967b0062005-08-10 23:54:15 +0000188#endif /* defined(THREADS) */
Brian Paul3c257e12001-03-28 17:19:58 +0000189
Ian Romanick967b0062005-08-10 23:54:15 +0000190PUBLIC struct _glapi_table *_glapi_Dispatch =
191 (struct _glapi_table *) __glapi_noop_table;
Adam Jackson791ce022004-12-15 23:14:29 +0000192PUBLIC void *_glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000193
Ian Romanick25fe93f2005-04-13 20:59:15 +0000194#endif /* defined(GLX_USE_TLS) */
Ian Romanick967b0062005-08-10 23:54:15 +0000195/*@}*/
Brian Paulab0c8862001-01-23 23:35:47 +0000196
Brian Paul54f3aab2002-10-02 01:51:44 +0000197
Brian Paul767e15a2004-11-27 03:51:11 +0000198/**
199 * strdup() is actually not a standard ANSI C or POSIX routine.
Brian Paul3c257e12001-03-28 17:19:58 +0000200 * Irix will not define it if ANSI mode is in effect.
201 */
202static char *
203str_dup(const char *str)
Randy Frankd7361e12000-03-27 21:13:58 +0000204{
Brian Paulfffb8092000-03-29 18:46:11 +0000205 char *copy;
206 copy = (char*) malloc(strlen(str) + 1);
207 if (!copy)
208 return NULL;
209 strcpy(copy, str);
210 return copy;
Randy Frankd7361e12000-03-27 21:13:58 +0000211}
212
Brian Paul7fb54ae1999-11-19 22:33:50 +0000213
Brian Paulbb72d321999-12-16 17:31:59 +0000214
Brian Paul767e15a2004-11-27 03:51:11 +0000215/**
Brian Paulbb72d321999-12-16 17:31:59 +0000216 * We should call this periodically from a function such as glXMakeCurrent
Brian Paul5104b4d2002-03-07 21:50:41 +0000217 * in order to test if multiple threads are being used.
Brian Paulbb72d321999-12-16 17:31:59 +0000218 */
Ian Romanick967b0062005-08-10 23:54:15 +0000219void
Brian Paulbb72d321999-12-16 17:31:59 +0000220_glapi_check_multithread(void)
221{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000222#if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000223 if (!ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000224 static unsigned long knownID;
225 static GLboolean firstCall = GL_TRUE;
226 if (firstCall) {
227 knownID = _glthread_GetID();
228 firstCall = GL_FALSE;
229 }
230 else if (knownID != _glthread_GetID()) {
Ian Romanickd14d1032004-07-02 00:01:09 +0000231 ThreadSafe = GL_TRUE;
Brian Paulbb72d321999-12-16 17:31:59 +0000232 _glapi_set_dispatch(NULL);
233 }
234 }
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000235 else if (!_glapi_get_dispatch()) {
236 /* make sure that this thread's dispatch pointer isn't null */
237 _glapi_set_dispatch(NULL);
238 }
Brian Paulbb72d321999-12-16 17:31:59 +0000239#endif
240}
241
242
243
Brian Paul767e15a2004-11-27 03:51:11 +0000244/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000245 * Set the current context pointer for this thread.
246 * The context pointer is an opaque type which should be cast to
247 * void from the real context pointer type.
248 */
Adam Jackson791ce022004-12-15 23:14:29 +0000249PUBLIC void
Brian Paulf9b97d92000-01-28 20:17:42 +0000250_glapi_set_context(void *context)
Brian Paul8f91fb61999-12-17 14:51:28 +0000251{
Brian Paul767e15a2004-11-27 03:51:11 +0000252 (void) __unused_noop_functions; /* silence a warning */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000253#if defined(GLX_USE_TLS)
254 _glapi_tls_Context = context;
255#elif defined(THREADS)
Brian Paul3c27be32000-02-10 21:27:48 +0000256 _glthread_SetTSD(&ContextTSD, context);
Ian Romanickd14d1032004-07-02 00:01:09 +0000257 _glapi_Context = (ThreadSafe) ? NULL : context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000258#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000259 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000260#endif
261}
262
263
264
Brian Paul767e15a2004-11-27 03:51:11 +0000265/**
Brian Paul8f91fb61999-12-17 14:51:28 +0000266 * Get the current context pointer for this thread.
267 * The context pointer is an opaque type which should be cast from
268 * void to the real context pointer type.
269 */
Adam Jackson791ce022004-12-15 23:14:29 +0000270PUBLIC void *
Brian Paulf9b97d92000-01-28 20:17:42 +0000271_glapi_get_context(void)
Brian Paul8f91fb61999-12-17 14:51:28 +0000272{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000273#if defined(GLX_USE_TLS)
274 return _glapi_tls_Context;
275#elif defined(THREADS)
Ian Romanickd14d1032004-07-02 00:01:09 +0000276 if (ThreadSafe) {
Brian Paul8f91fb61999-12-17 14:51:28 +0000277 return _glthread_GetTSD(&ContextTSD);
278 }
279 else {
Brian Paulf9b97d92000-01-28 20:17:42 +0000280 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000281 }
282#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000283 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000284#endif
285}
286
287
288
Brian Paul767e15a2004-11-27 03:51:11 +0000289/**
Brian Paul7fb54ae1999-11-19 22:33:50 +0000290 * Set the global or per-thread dispatch table pointer.
Brian Paul514a15c2006-03-15 20:56:22 +0000291 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
292 * table (__glapi_noop_table).
Brian Paul7fb54ae1999-11-19 22:33:50 +0000293 */
Adam Jackson791ce022004-12-15 23:14:29 +0000294PUBLIC void
Brian Paul7fb54ae1999-11-19 22:33:50 +0000295_glapi_set_dispatch(struct _glapi_table *dispatch)
296{
Ian Romanick25fe93f2005-04-13 20:59:15 +0000297#if defined(PTHREADS) || defined(GLX_USE_TLS)
298 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000299 pthread_once( & once_control, init_glapi_relocs );
300#endif
301
Ian Romanick6c5402b2004-07-05 22:40:54 +0000302 if (!dispatch) {
303 /* use the no-op functions */
304 dispatch = (struct _glapi_table *) __glapi_noop_table;
305 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000306#ifdef DEBUG
Ian Romanick6c5402b2004-07-05 22:40:54 +0000307 else {
Brian Paul7fb54ae1999-11-19 22:33:50 +0000308 _glapi_check_table(dispatch);
309 }
310#endif
311
Ian Romanick25fe93f2005-04-13 20:59:15 +0000312#if defined(GLX_USE_TLS)
Ian Romanick967b0062005-08-10 23:54:15 +0000313 _glapi_tls_Dispatch = dispatch;
Ian Romanick25fe93f2005-04-13 20:59:15 +0000314#elif defined(THREADS)
Ian Romanick967b0062005-08-10 23:54:15 +0000315 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
316 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000317#else /*THREADS*/
Ian Romanick967b0062005-08-10 23:54:15 +0000318 _glapi_Dispatch = dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000319#endif /*THREADS*/
Brian Paul7fb54ae1999-11-19 22:33:50 +0000320}
321
322
Brian Paulbb72d321999-12-16 17:31:59 +0000323
Brian Paul767e15a2004-11-27 03:51:11 +0000324/**
Brian Paulbb72d321999-12-16 17:31:59 +0000325 * Return pointer to current dispatch table for calling thread.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000326 */
Adam Jackson791ce022004-12-15 23:14:29 +0000327PUBLIC struct _glapi_table *
Brian Paul7fb54ae1999-11-19 22:33:50 +0000328_glapi_get_dispatch(void)
329{
Ian Romanick967b0062005-08-10 23:54:15 +0000330 struct _glapi_table * api;
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 return api;
Brian Paul3a71d052000-09-05 20:17:37 +0000341}
Brian Paulab0c8862001-01-23 23:35:47 +0000342
Brian Paul3a71d052000-09-05 20:17:37 +0000343
Brian Paul514a15c2006-03-15 20:56:22 +0000344
345/***
346 *** The rest of this file is pretty much concerned with GetProcAddress
347 *** functionality.
348 ***/
349
Dave Airlief2363002006-02-10 21:46:17 +0000350#if !defined( USE_X86_ASM ) && !defined( XFree86Server ) && !defined ( XGLServer )
Ian Romanick78677992004-05-27 00:05:13 +0000351#define NEED_FUNCTION_POINTER
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000352#endif
Ian Romanick78677992004-05-27 00:05:13 +0000353
Brian Paul54f3aab2002-10-02 01:51:44 +0000354/* The code in this file is auto-generated with Python */
Brian Paulb5fd8862001-11-18 22:48:11 +0000355#include "glprocs.h"
Brian Paul7fb54ae1999-11-19 22:33:50 +0000356
Brian Paul959f8022000-03-19 01:10:11 +0000357
Brian Paul767e15a2004-11-27 03:51:11 +0000358/**
359 * Search the table of static entrypoint functions for the named function
360 * and return the corresponding glprocs_table_t entry.
361 */
Ian Romanick78677992004-05-27 00:05:13 +0000362static const glprocs_table_t *
363find_entry( const char * n )
364{
Brian Paula760ccf2004-12-03 15:24:34 +0000365 GLuint i;
Brian Paula760ccf2004-12-03 15:24:34 +0000366 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Brian Paul514a15c2006-03-15 20:56:22 +0000367 const char *testName = gl_string_table + static_functions[i].Name_offset;
368 if (strcmp(testName, n) == 0) {
369 return &static_functions[i];
Ian Romanick78677992004-05-27 00:05:13 +0000370 }
371 }
372 return NULL;
373}
374
Brian Paul767e15a2004-11-27 03:51:11 +0000375
376/**
Brian Paul959f8022000-03-19 01:10:11 +0000377 * Return dispatch table offset of the named static (built-in) function.
378 * Return -1 if function not found.
379 */
380static GLint
381get_static_proc_offset(const char *funcName)
382{
Ian Romanick78677992004-05-27 00:05:13 +0000383 const glprocs_table_t * const f = find_entry( funcName );
Brian Paul514a15c2006-03-15 20:56:22 +0000384 if (f) {
Ian Romanick78677992004-05-27 00:05:13 +0000385 return f->Offset;
Brian Paul959f8022000-03-19 01:10:11 +0000386 }
387 return -1;
388}
389
390
Brian Paul514a15c2006-03-15 20:56:22 +0000391#if !defined(XFree86Server) && !defined(XGLServer)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000392#ifdef USE_X86_ASM
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000393
Ian Romanick25fe93f2005-04-13 20:59:15 +0000394#if defined( GLX_USE_TLS )
395extern GLubyte gl_dispatch_functions_start[];
396extern GLubyte gl_dispatch_functions_end[];
397#else
398extern const GLubyte gl_dispatch_functions_start[];
399#endif
400
401# if defined(THREADS) && !defined(GLX_USE_TLS)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000402# define X86_DISPATCH_FUNCTION_SIZE 32
403# else
404# define X86_DISPATCH_FUNCTION_SIZE 16
405# endif
406
Brian Paul514a15c2006-03-15 20:56:22 +0000407#endif /* USE_X86_ASM */
408
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000409
Brian Paul767e15a2004-11-27 03:51:11 +0000410/**
Brian Paul514a15c2006-03-15 20:56:22 +0000411 * Return dispatch function address for the named static (built-in) function.
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000412 * Return NULL if function not found.
413 */
Brian Paul209bd3a2004-11-27 04:02:32 +0000414static const _glapi_proc
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000415get_static_proc_address(const char *funcName)
416{
417 const glprocs_table_t * const f = find_entry( funcName );
Brian Paul514a15c2006-03-15 20:56:22 +0000418 if (f) {
419#ifdef USE_X86_ASM
Brian Paul209bd3a2004-11-27 04:02:32 +0000420 return (_glapi_proc) (gl_dispatch_functions_start
421 + (X86_DISPATCH_FUNCTION_SIZE * f->Offset));
Brian Paul514a15c2006-03-15 20:56:22 +0000422#else
423 return f->Address;
424#endif
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000425 }
426 else {
427 return NULL;
428 }
429}
430
Brian Paul514a15c2006-03-15 20:56:22 +0000431#endif /* !defined(XFree86Server) && !defined(XGLServer) */
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000432
433
Ian Romanick78677992004-05-27 00:05:13 +0000434
Brian Paul767e15a2004-11-27 03:51:11 +0000435/**
436 * Return the name of the function at the given offset in the dispatch
437 * table. For debugging only.
438 */
Ian Romanick78677992004-05-27 00:05:13 +0000439static const char *
440get_static_proc_name( GLuint offset )
441{
Brian Paula760ccf2004-12-03 15:24:34 +0000442 GLuint i;
Brian Paula760ccf2004-12-03 15:24:34 +0000443 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
Ian Romanick78677992004-05-27 00:05:13 +0000444 if (static_functions[i].Offset == offset) {
445 return gl_string_table + static_functions[i].Name_offset;
Brian Paul9c7ca852000-10-19 20:13:12 +0000446 }
447 }
448 return NULL;
Brian Paul959f8022000-03-19 01:10:11 +0000449}
450
451
452
453/**********************************************************************
454 * Extension function management.
455 */
456
Brian Paul54f3aab2002-10-02 01:51:44 +0000457/*
458 * Number of extension functions which we can dynamically add at runtime.
459 */
460#define MAX_EXTENSION_FUNCS 300
Brian Paul959f8022000-03-19 01:10:11 +0000461
Brian Paul54f3aab2002-10-02 01:51:44 +0000462
463/*
Brian Paul767e15a2004-11-27 03:51:11 +0000464 * The dispatch table size (number of entries) is the size of the
Brian Paul54f3aab2002-10-02 01:51:44 +0000465 * _glapi_table struct plus the number of dynamic entries we can add.
466 * The extra slots can be filled in by DRI drivers that register new extension
467 * functions.
468 */
469#define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
470
Brian Paul959f8022000-03-19 01:10:11 +0000471
Ian Romanick1585c232005-07-28 00:29:51 +0000472/**
473 * Track information about a function added to the GL API.
474 */
475struct _glapi_function {
476 /**
477 * Name of the function.
478 */
479 const char * name;
480
481
482 /**
483 * Text string that describes the types of the parameters passed to the
484 * named function. Parameter types are converted to characters using the
485 * following rules:
486 * - 'i' for \c GLint, \c GLuint, and \c GLenum
487 * - 'p' for any pointer type
488 * - 'f' for \c GLfloat and \c GLclampf
489 * - 'd' for \c GLdouble and \c GLclampd
490 */
491 const char * parameter_signature;
492
493
494 /**
495 * Offset in the dispatch table where the pointer to the real function is
496 * located. If the driver has not requested that the named function be
497 * added to the dispatch table, this will have the value ~0.
498 */
499 unsigned dispatch_offset;
500
501
502 /**
503 * Pointer to the dispatch stub for the named function.
504 *
505 * \todo
506 * The semantic of this field should be changed slightly. Currently, it
507 * is always expected to be non-\c NULL. However, it would be better to
508 * only allocate the entry-point stub when the application requests the
509 * function via \c glXGetProcAddress. This would save memory for all the
510 * functions that the driver exports but that the application never wants
511 * to call.
512 */
513 _glapi_proc dispatch_stub;
Brian Paul767e15a2004-11-27 03:51:11 +0000514};
515
516
Ian Romanick1585c232005-07-28 00:29:51 +0000517static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
Brian Paul959f8022000-03-19 01:10:11 +0000518static GLuint NumExtEntryPoints = 0;
519
davem694a497e62001-06-06 22:55:28 +0000520#ifdef USE_SPARC_ASM
521extern void __glapi_sparc_icache_flush(unsigned int *);
522#endif
Brian Paul959f8022000-03-19 01:10:11 +0000523
Brian Paul767e15a2004-11-27 03:51:11 +0000524/**
Brian Paul959f8022000-03-19 01:10:11 +0000525 * Generate a dispatch function (entrypoint) which jumps through
526 * the given slot number (offset) in the current dispatch table.
527 * We need assembly language in order to accomplish this.
528 */
Brian Paul767e15a2004-11-27 03:51:11 +0000529static _glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000530generate_entrypoint(GLuint functionOffset)
531{
532#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000533 /* 32 is chosen as something of a magic offset. For x86, the dispatch
534 * at offset 32 is the first one where the offset in the
535 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
Brian Paul959f8022000-03-19 01:10:11 +0000536 */
Ian Romanick25fe93f2005-04-13 20:59:15 +0000537 const GLubyte * const template_func = gl_dispatch_functions_start
538 + (X86_DISPATCH_FUNCTION_SIZE * 32);
539 GLubyte * const code = (GLubyte *) malloc( X86_DISPATCH_FUNCTION_SIZE );
Brian Paul959f8022000-03-19 01:10:11 +0000540
Ian Romanick25fe93f2005-04-13 20:59:15 +0000541
542 if ( code != NULL ) {
543 (void) memcpy( code, template_func, X86_DISPATCH_FUNCTION_SIZE );
544 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
Brian Paul959f8022000-03-19 01:10:11 +0000545 }
Ian Romanick25fe93f2005-04-13 20:59:15 +0000546
Brian Paul767e15a2004-11-27 03:51:11 +0000547 return (_glapi_proc) code;
davem69775355a2001-06-05 23:54:00 +0000548#elif defined(USE_SPARC_ASM)
549
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000550#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000551 static const unsigned int insn_template[] = {
552 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
553 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
554 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
555 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
556 0x8528b020, /* sllx %g2, 32, %g2 */
557 0xc2584002, /* ldx [%g1 + %g2], %g1 */
558 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
559 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
560 0xc6584002, /* ldx [%g1 + %g2], %g3 */
561 0x81c0c000, /* jmpl %g3, %g0 */
562 0x01000000 /* nop */
563 };
564#else
565 static const unsigned int insn_template[] = {
566 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
567 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
568 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
569 0x81c0c000, /* jmpl %g3, %g0 */
570 0x01000000 /* nop */
571 };
Brian Paul514a15c2006-03-15 20:56:22 +0000572#endif /* __arch64__ */
Brian Pauldec2a4d2002-10-29 15:03:14 +0000573 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
davem69775355a2001-06-05 23:54:00 +0000574 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
575 if (code) {
576 memcpy(code, insn_template, sizeof(insn_template));
577
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000578#ifdef __arch64__
davem69775355a2001-06-05 23:54:00 +0000579 code[0] |= (glapi_addr >> (32 + 10));
580 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
davem694a497e62001-06-06 22:55:28 +0000581 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000582 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
583 code[3] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000584 __glapi_sparc_icache_flush(&code[2]);
davem69775355a2001-06-05 23:54:00 +0000585 code[6] |= ((functionOffset * 8) >> 10);
586 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000587 __glapi_sparc_icache_flush(&code[6]);
davem69775355a2001-06-05 23:54:00 +0000588#else
589 code[0] |= (glapi_addr >> 10);
590 code[1] |= (glapi_addr & ((1 << 10) - 1));
davem694a497e62001-06-06 22:55:28 +0000591 __glapi_sparc_icache_flush(&code[0]);
davem69775355a2001-06-05 23:54:00 +0000592 code[2] |= (functionOffset * 4);
davem694a497e62001-06-06 22:55:28 +0000593 __glapi_sparc_icache_flush(&code[2]);
Brian Paul514a15c2006-03-15 20:56:22 +0000594#endif /* __arch64__ */
davem69775355a2001-06-05 23:54:00 +0000595 }
Brian Paul767e15a2004-11-27 03:51:11 +0000596 return (_glapi_proc) code;
Brian Paul959f8022000-03-19 01:10:11 +0000597#else
Brian Paula6c423d2004-08-25 15:59:48 +0000598 (void) functionOffset;
Brian Paul959f8022000-03-19 01:10:11 +0000599 return NULL;
Brian Paul54f3aab2002-10-02 01:51:44 +0000600#endif /* USE_*_ASM */
Brian Paul959f8022000-03-19 01:10:11 +0000601}
602
603
Brian Paul767e15a2004-11-27 03:51:11 +0000604/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000605 * This function inserts a new dispatch offset into the assembly language
606 * stub that was generated with the preceeding function.
607 */
608static void
Brian Paul767e15a2004-11-27 03:51:11 +0000609fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
Brian Paul54f3aab2002-10-02 01:51:44 +0000610{
611#if defined(USE_X86_ASM)
Ian Romanick25fe93f2005-04-13 20:59:15 +0000612 GLubyte * const code = (GLubyte *) entrypoint;
Brian Paul54f3aab2002-10-02 01:51:44 +0000613
Ian Romanick25fe93f2005-04-13 20:59:15 +0000614#if X86_DISPATCH_FUNCTION_SIZE == 32
615 *((unsigned int *)(code + 11)) = 4 * offset;
616 *((unsigned int *)(code + 22)) = 4 * offset;
617#elif X86_DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
618 *((unsigned int *)(code + 8)) = 4 * offset;
619#elif X86_DISPATCH_FUNCTION_SIZE == 16
620 *((unsigned int *)(code + 7)) = 4 * offset;
621#else
622# error Invalid X86_DISPATCH_FUNCTION_SIZE!
623#endif
Brian Paul54f3aab2002-10-02 01:51:44 +0000624
625#elif defined(USE_SPARC_ASM)
626
627 /* XXX this hasn't been tested! */
628 unsigned int *code = (unsigned int *) entrypoint;
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000629#ifdef __arch64__
Brian Paul54f3aab2002-10-02 01:51:44 +0000630 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
631 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
632 code[6] |= ((offset * 8) >> 10);
633 code[7] |= ((offset * 8) & ((1 << 10) - 1));
634 __glapi_sparc_icache_flush(&code[6]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000635#else /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000636 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
Brian Paul944ea202002-10-17 16:29:17 +0000637 code[2] |= (offset * 4);
Brian Paul54f3aab2002-10-02 01:51:44 +0000638 __glapi_sparc_icache_flush(&code[2]);
Ian Romanick9f23a3a2005-07-28 00:11:10 +0000639#endif /* __arch64__ */
Brian Paul54f3aab2002-10-02 01:51:44 +0000640
Brian Paula6c423d2004-08-25 15:59:48 +0000641#else
642
643 /* an unimplemented architecture */
644 (void) entrypoint;
645 (void) offset;
646
Brian Paul54f3aab2002-10-02 01:51:44 +0000647#endif /* USE_*_ASM */
648}
649
Brian Paul959f8022000-03-19 01:10:11 +0000650
Brian Paul767e15a2004-11-27 03:51:11 +0000651/**
Ian Romanick1585c232005-07-28 00:29:51 +0000652 * Generate new entrypoint
653 *
654 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
655 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
656 * never happens, and the user calls this function, he'll segfault. That's
657 * what you get when you try calling a GL function that doesn't really exist.
658 *
659 * \param funcName Name of the function to create an entry-point for.
660 *
661 * \sa _glapi_add_entrypoint
Brian Paul959f8022000-03-19 01:10:11 +0000662 */
Ian Romanick1585c232005-07-28 00:29:51 +0000663
664static struct _glapi_function *
665add_function_name( const char * funcName )
Brian Paul959f8022000-03-19 01:10:11 +0000666{
Ian Romanick1585c232005-07-28 00:29:51 +0000667 struct _glapi_function * entry = NULL;
668
669 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
670 _glapi_proc entrypoint = generate_entrypoint(~0);
671 if (entrypoint != NULL) {
672 entry = & ExtEntryTable[NumExtEntryPoints];
673
674 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
675 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
676 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
677 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
678 NumExtEntryPoints++;
679 }
680 }
681
682 return entry;
683}
684
685
686/**
687 * Fill-in the dispatch stub for the named function.
688 *
689 * This function is intended to be called by a hardware driver. When called,
690 * a dispatch stub may be created created for the function. A pointer to this
691 * dispatch function will be returned by glXGetProcAddress.
692 *
693 * \param function_names Array of pointers to function names that should
694 * share a common dispatch offset.
695 * \param parameter_signature String representing the types of the parameters
696 * passed to the named function. Parameter types
697 * are converted to characters using the following
698 * rules:
699 * - 'i' for \c GLint, \c GLuint, and \c GLenum
700 * - 'p' for any pointer type
701 * - 'f' for \c GLfloat and \c GLclampf
702 * - 'd' for \c GLdouble and \c GLclampd
703 *
704 * \returns
705 * The offset in the dispatch table of the named function. A pointer to the
706 * driver's implementation of the named function should be stored at
707 * \c dispatch_table[\c offset].
708 *
709 * \sa glXGetProcAddress
710 *
711 * \warning
712 * This function can only handle up to 8 names at a time. As far as I know,
713 * the maximum number of names ever associated with an existing GL function is
714 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
715 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
716 * too painful of a limitation.
717 *
718 * \todo
719 * Determine whether or not \c parameter_signature should be allowed to be
720 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
721 * pass in an empty string.
722 *
723 * \todo
724 * Determine if code should be added to reject function names that start with
725 * 'glX'.
726 *
727 * \bug
728 * Add code to compare \c parameter_signature with the parameter signature of
729 * a static function. In order to do that, we need to find a way to \b get
730 * the parameter signature of a static function.
731 */
732
733PUBLIC int
734_glapi_add_dispatch( const char * const * function_names,
735 const char * parameter_signature )
736{
737 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
738 const char * const real_sig = (parameter_signature != NULL)
739 ? parameter_signature : "";
740 struct _glapi_function * entry[8];
741 GLboolean is_static[8];
742 unsigned i;
743 unsigned j;
744 int offset = ~0;
745 int new_offset;
746
747
748 (void) memset( is_static, 0, sizeof( is_static ) );
749 (void) memset( entry, 0, sizeof( entry ) );
750
751 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
752 /* Do some trivial validation on the name of the function.
753 */
754
Ian Romanick1585c232005-07-28 00:29:51 +0000755 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
756 return GL_FALSE;
Ian Romanick1585c232005-07-28 00:29:51 +0000757
758 /* Determine if the named function already exists. If the function does
759 * exist, it must have the same parameter signature as the function
760 * being added.
761 */
Brian Paul8ad10762002-10-11 17:41:03 +0000762
Ian Romanick1585c232005-07-28 00:29:51 +0000763 new_offset = get_static_proc_offset(function_names[i]);
764 if (new_offset >= 0) {
765 /* FIXME: Make sure the parameter signatures match! How do we get
766 * FIXME: the parameter signature for static functions?
767 */
768
769 if ( (offset != ~0) && (new_offset != offset) ) {
770 return -1;
771 }
772
773 is_static[i] = GL_TRUE;
774 offset = new_offset;
775 }
776
777
778 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
779 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
780 /* The offset may be ~0 if the function name was added by
781 * glXGetProcAddress but never filled in by the driver.
782 */
783
784 if (ExtEntryTable[j].dispatch_offset != ~0) {
785 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
786 != 0) {
787 return -1;
788 }
789
790 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
791 return -1;
792 }
793
794 offset = ExtEntryTable[j].dispatch_offset;
795 }
796
797 entry[i] = & ExtEntryTable[j];
798 break;
799 }
Brian Paul959f8022000-03-19 01:10:11 +0000800 }
801 }
802
Ian Romanick1585c232005-07-28 00:29:51 +0000803 if (offset == ~0) {
804 offset = next_dynamic_offset;
805 next_dynamic_offset++;
806 }
807
Ian Romanick1585c232005-07-28 00:29:51 +0000808 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
809 if (! is_static[i] ) {
810 if (entry[i] == NULL) {
811 entry[i] = add_function_name( function_names[i] );
812 if (entry[i] == NULL) {
813 /* FIXME: Possible memory leak here.
814 */
815 return -1;
816 }
817 }
818
Ian Romanick1585c232005-07-28 00:29:51 +0000819 entry[i]->parameter_signature = str_dup(real_sig);
820 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
821 entry[i]->dispatch_offset = offset;
Brian Paul959f8022000-03-19 01:10:11 +0000822 }
Brian Paul959f8022000-03-19 01:10:11 +0000823 }
Ian Romanick1585c232005-07-28 00:29:51 +0000824
825 return offset;
Brian Paul959f8022000-03-19 01:10:11 +0000826}
827
828
Brian Paul767e15a2004-11-27 03:51:11 +0000829/**
Brian Paul959f8022000-03-19 01:10:11 +0000830 * Return offset of entrypoint for named function within dispatch table.
831 */
Adam Jackson791ce022004-12-15 23:14:29 +0000832PUBLIC GLint
Brian Paul959f8022000-03-19 01:10:11 +0000833_glapi_get_proc_offset(const char *funcName)
834{
835 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000836 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000837 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000838 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
839 return ExtEntryTable[i].dispatch_offset;
Brian Paul959f8022000-03-19 01:10:11 +0000840 }
841 }
Brian Paul959f8022000-03-19 01:10:11 +0000842 /* search static functions */
843 return get_static_proc_offset(funcName);
844}
845
846
847
Brian Paul767e15a2004-11-27 03:51:11 +0000848/**
849 * Return pointer to the named function. If the function name isn't found
850 * in the name of static functions, try generating a new API entrypoint on
851 * the fly with assembly language.
Brian Paul959f8022000-03-19 01:10:11 +0000852 */
Ian Romanick967b0062005-08-10 23:54:15 +0000853_glapi_proc
Brian Paul959f8022000-03-19 01:10:11 +0000854_glapi_get_proc_address(const char *funcName)
855{
Ian Romanick1585c232005-07-28 00:29:51 +0000856 struct _glapi_function * entry;
Brian Paulb51b0a82001-03-07 05:06:11 +0000857 GLuint i;
Brian Paula6ed6f42003-08-27 14:48:16 +0000858
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000859#ifdef MANGLE
860 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
861 return NULL;
862#else
Brian Paula6ed6f42003-08-27 14:48:16 +0000863 if (funcName[0] != 'g' || funcName[1] != 'l')
864 return NULL;
Brian Paulf7b4e0d2004-04-23 20:33:07 +0000865#endif
Brian Paula6ed6f42003-08-27 14:48:16 +0000866
867 /* search extension functions first */
Brian Paul959f8022000-03-19 01:10:11 +0000868 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000869 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
870 return ExtEntryTable[i].dispatch_stub;
Brian Paul959f8022000-03-19 01:10:11 +0000871 }
872 }
873
Dave Airlief2363002006-02-10 21:46:17 +0000874#if !defined( XFree86Server ) && !defined( XGLServer )
Brian Paul959f8022000-03-19 01:10:11 +0000875 /* search static functions */
Brian Paul54f3aab2002-10-02 01:51:44 +0000876 {
Brian Paul767e15a2004-11-27 03:51:11 +0000877 const _glapi_proc func = get_static_proc_address(funcName);
Brian Paul54f3aab2002-10-02 01:51:44 +0000878 if (func)
879 return func;
880 }
Ian Romanick44b1bd72005-10-11 16:56:39 +0000881#endif /* !defined( XFree86Server ) */
Brian Paul959f8022000-03-19 01:10:11 +0000882
Ian Romanick1585c232005-07-28 00:29:51 +0000883 entry = add_function_name(funcName);
884 return (entry == NULL) ? NULL : entry->dispatch_stub;
Brian Paul54f3aab2002-10-02 01:51:44 +0000885}
Brian Paul959f8022000-03-19 01:10:11 +0000886
887
888
Brian Paul767e15a2004-11-27 03:51:11 +0000889/**
Brian Paul959f8022000-03-19 01:10:11 +0000890 * Return the name of the function at the given dispatch offset.
891 * This is only intended for debugging.
892 */
Ian Romanick967b0062005-08-10 23:54:15 +0000893const char *
Brian Paul959f8022000-03-19 01:10:11 +0000894_glapi_get_proc_name(GLuint offset)
895{
Brian Paul959f8022000-03-19 01:10:11 +0000896 GLuint i;
Ian Romanick78677992004-05-27 00:05:13 +0000897 const char * n;
Brian Paul54f3aab2002-10-02 01:51:44 +0000898
899 /* search built-in functions */
Ian Romanick78677992004-05-27 00:05:13 +0000900 n = get_static_proc_name(offset);
901 if ( n != NULL ) {
902 return n;
Brian Paul959f8022000-03-19 01:10:11 +0000903 }
904
905 /* search added extension functions */
906 for (i = 0; i < NumExtEntryPoints; i++) {
Ian Romanick1585c232005-07-28 00:29:51 +0000907 if (ExtEntryTable[i].dispatch_offset == offset) {
908 return ExtEntryTable[i].name;
Brian Paul959f8022000-03-19 01:10:11 +0000909 }
910 }
911 return NULL;
912}
913
914
915
Brian Paul767e15a2004-11-27 03:51:11 +0000916/**
Brian Paul54f3aab2002-10-02 01:51:44 +0000917 * Return size of dispatch table struct as number of functions (or
918 * slots).
919 */
Adam Jackson791ce022004-12-15 23:14:29 +0000920PUBLIC GLuint
Brian Paul54f3aab2002-10-02 01:51:44 +0000921_glapi_get_dispatch_table_size(void)
922{
923 return DISPATCH_TABLE_SIZE;
924}
925
926
927
Brian Paul767e15a2004-11-27 03:51:11 +0000928/**
Brian Paul959f8022000-03-19 01:10:11 +0000929 * Make sure there are no NULL pointers in the given dispatch table.
Brian Paul5104b4d2002-03-07 21:50:41 +0000930 * Intended for debugging purposes.
Brian Paul959f8022000-03-19 01:10:11 +0000931 */
Ian Romanick967b0062005-08-10 23:54:15 +0000932void
Brian Paul959f8022000-03-19 01:10:11 +0000933_glapi_check_table(const struct _glapi_table *table)
934{
Brian Paul5104b4d2002-03-07 21:50:41 +0000935#ifdef DEBUG
Brian Paul959f8022000-03-19 01:10:11 +0000936 const GLuint entries = _glapi_get_dispatch_table_size();
937 const void **tab = (const void **) table;
938 GLuint i;
939 for (i = 1; i < entries; i++) {
940 assert(tab[i]);
941 }
942
Brian Paul959f8022000-03-19 01:10:11 +0000943 /* Do some spot checks to be sure that the dispatch table
944 * slots are assigned correctly.
945 */
946 {
947 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
948 char *BeginFunc = (char*) &table->Begin;
949 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
950 assert(BeginOffset == _gloffset_Begin);
951 assert(BeginOffset == offset);
952 }
953 {
954 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
955 char *viewportFunc = (char*) &table->Viewport;
956 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
957 assert(viewportOffset == _gloffset_Viewport);
958 assert(viewportOffset == offset);
959 }
960 {
961 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
962 char *VertexPointerFunc = (char*) &table->VertexPointer;
963 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
964 assert(VertexPointerOffset == _gloffset_VertexPointer);
965 assert(VertexPointerOffset == offset);
966 }
967 {
968 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
969 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
970 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
971 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
972 assert(ResetMinMaxOffset == offset);
973 }
974 {
975 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
976 char *blendColorFunc = (char*) &table->BlendColor;
977 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
978 assert(blendColorOffset == _gloffset_BlendColor);
979 assert(blendColorOffset == offset);
980 }
981 {
982 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
983 char *istextureFunc = (char*) &table->IsTextureEXT;
984 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
985 assert(istextureOffset == _gloffset_IsTextureEXT);
986 assert(istextureOffset == offset);
987 }
Brian Paula14cbff2000-10-27 18:31:21 +0000988 {
989 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
990 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
991 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
992 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
993 assert(secondaryColor3fOffset == offset);
Brian Paul767e15a2004-11-27 03:51:11 +0000994 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
Brian Paula14cbff2000-10-27 18:31:21 +0000995 }
Brian Paul91d6f122002-05-29 15:23:16 +0000996 {
997 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
998 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
999 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1000 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1001 assert(pointParameterivOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001002 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
Brian Paul91d6f122002-05-29 15:23:16 +00001003 }
Brian Paul54f3aab2002-10-02 01:51:44 +00001004 {
1005 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1006 char *setFenceFunc = (char*) &table->SetFenceNV;
1007 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1008 assert(setFenceOffset == _gloffset_SetFenceNV);
1009 assert(setFenceOffset == offset);
Brian Paula760ccf2004-12-03 15:24:34 +00001010 assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc) &glSetFenceNV);
Brian Paul54f3aab2002-10-02 01:51:44 +00001011 }
Brian Paula6c423d2004-08-25 15:59:48 +00001012#else
1013 (void) table;
Brian Paul959f8022000-03-19 01:10:11 +00001014#endif
1015}
Ian Romanick25fe93f2005-04-13 20:59:15 +00001016
1017
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +00001018#if defined(PTHREADS) || defined(GLX_USE_TLS)
Ian Romanick25fe93f2005-04-13 20:59:15 +00001019/**
1020 * Perform platform-specific GL API entry-point fixups.
1021 *
1022 *
1023 */
1024static void
1025init_glapi_relocs( void )
1026{
1027#if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1028 extern void * _x86_get_dispatch(void);
1029 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1030 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1031
1032
1033 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1034 (void) memcpy( curr_func, get_disp, 6 );
1035 curr_func += X86_DISPATCH_FUNCTION_SIZE;
1036 }
1037#endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1038}
Kristian Høgsberg3a6d9682006-03-29 22:32:38 +00001039#endif