blob: 0c4c91c32c0da6c5e08d88b65c20b9597d97876d [file] [log] [blame]
Brian Paulb5fd8862001-11-18 22:48:11 +00001/* $Id: glapi.c,v 1.59 2001/11/18 22:48:11 brianp Exp $ */
Brian Paulfbd8f211999-11-11 01:22:25 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul3a71d052000-09-05 20:17:37 +00005 * Version: 3.5
Brian Paulfbd8f211999-11-11 01:22:25 +00006 *
Brian Paulab0c8862001-01-23 23:35:47 +00007 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
Brian Paulfbd8f211999-11-11 01:22:25 +00008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
Brian Paul7e1161b1999-11-25 18:17:04 +000028/*
29 * This file manages the OpenGL API dispatch layer.
30 * The dispatch table (struct _glapi_table) is basically just a list
31 * of function pointers.
32 * There are functions to set/get the current dispatch table for the
33 * current thread and to manage registration/dispatch of dynamically
34 * added extension functions.
Brian Paul9f943992000-01-28 19:03:33 +000035 *
36 * It's intended that this file and the other glapi*.[ch] files are
37 * flexible enough to be reused in several places: XFree86, DRI-
38 * based libGL.so, and perhaps the SGI SI.
39 *
40 * There are no dependencies on Mesa in this code.
Brian Paulab0c8862001-01-23 23:35:47 +000041 *
42 * Versions (API changes):
43 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
44 * 2001/01/16 - added dispatch override feature for Mesa 3.5
Brian Paul7e1161b1999-11-25 18:17:04 +000045 */
46
47
48
Brian Paul3c27be32000-02-10 21:27:48 +000049#include "glheader.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000050#include "glapi.h"
Brian Paul0c239fc1999-12-16 12:38:11 +000051#include "glapioffsets.h"
52#include "glapitable.h"
Brian Paul9f943992000-01-28 19:03:33 +000053#include "glthread.h"
Brian Paulbb72d321999-12-16 17:31:59 +000054
Brian Paul3c257e12001-03-28 17:19:58 +000055/***** BEGIN NO-OP DISPATCH *****/
56
57static GLboolean WarnFlag = GL_FALSE;
58
59void
60_glapi_noop_enable_warnings(GLboolean enable)
61{
62 WarnFlag = enable;
63}
64
65static GLboolean
66warn(void)
67{
68 if (WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
69 return GL_TRUE;
70 else
71 return GL_FALSE;
72}
73
74
75#define KEYWORD1 static
76#define KEYWORD2
77#define NAME(func) NoOp##func
78
79#define F stderr
80
81#define DISPATCH(func, args, msg) \
82 if (warn()) { \
83 fprintf(stderr, "GL User Error: calling "); \
84 fprintf msg; \
85 fprintf(stderr, " without a current context\n"); \
86 }
87
88#define RETURN_DISPATCH(func, args, msg) \
89 if (warn()) { \
90 fprintf(stderr, "GL User Error: calling "); \
91 fprintf msg; \
92 fprintf(stderr, " without a current context\n"); \
93 } \
94 return 0
95
96#define DISPATCH_TABLE_NAME __glapi_noop_table
97#define UNUSED_TABLE_NAME __usused_noop_functions
98
99#define TABLE_ENTRY(name) (void *) NoOp##name
100
101static int NoOpUnused(void)
102{
103 if (warn()) {
104 fprintf(stderr, "GL User Error: calling extension function without a current context\n");
105 }
106 return 0;
107}
108
109#include "glapitemp.h"
110
111/***** END NO-OP DISPATCH *****/
112
113
114
115/***** BEGIN THREAD-SAFE DISPATCH *****/
116/* if we support thread-safety, build a special dispatch table for use
117 * in thread-safety mode (ThreadSafe == GL_TRUE). Each entry in the
118 * dispatch table will call _glthread_GetTSD() to get the actual dispatch
119 * table bound to the current thread, then jump through that table.
120 */
121
122#if defined(THREADS)
123
124static GLboolean ThreadSafe = GL_FALSE; /* In thread-safe mode? */
125static _glthread_TSD DispatchTSD; /* Per-thread dispatch pointer */
126static _glthread_TSD RealDispatchTSD; /* only when using override */
127static _glthread_TSD ContextTSD; /* Per-thread context pointer */
128
129
130#define KEYWORD1 static
131#define KEYWORD2 GLAPIENTRY
132#define NAME(func) _ts_##func
133
134#define DISPATCH(FUNC, ARGS, MESSAGE) \
135 struct _glapi_table *dispatch; \
136 dispatch = (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); \
137 if (!dispatch) \
138 dispatch = (struct _glapi_table *) __glapi_noop_table; \
139 (dispatch->FUNC) ARGS
140
141#define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
142 struct _glapi_table *dispatch; \
143 dispatch = (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD); \
144 if (!dispatch) \
145 dispatch = (struct _glapi_table *) __glapi_noop_table; \
146 return (dispatch->FUNC) ARGS
147
148#define DISPATCH_TABLE_NAME __glapi_threadsafe_table
149#define UNUSED_TABLE_NAME __usused_threadsafe_functions
150
151#define TABLE_ENTRY(name) (void *) _ts_##name
152
153static int _ts_Unused(void)
154{
155 return 0;
156}
157
158#include "glapitemp.h"
159
160#endif
161
162/***** END THREAD-SAFE DISPATCH *****/
163
164
165
Brian Paul8ceb5c32000-02-24 22:14:04 +0000166struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table;
Brian Paulab0c8862001-01-23 23:35:47 +0000167struct _glapi_table *_glapi_RealDispatch = (struct _glapi_table *) __glapi_noop_table;
Brian Paul0f710251999-12-15 15:02:30 +0000168
Brian Paul8f91fb61999-12-17 14:51:28 +0000169/* Used when thread safety disabled */
Brian Paulf9b97d92000-01-28 20:17:42 +0000170void *_glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000171
Brian Paul0f710251999-12-15 15:02:30 +0000172
Brian Paulbb72d321999-12-16 17:31:59 +0000173static GLuint MaxDispatchOffset = sizeof(struct _glapi_table) / sizeof(void *) - 1;
Brian Paul0f710251999-12-15 15:02:30 +0000174static GLboolean GetSizeCalled = GL_FALSE;
Brian Paulfbd8f211999-11-11 01:22:25 +0000175
Brian Paulab0c8862001-01-23 23:35:47 +0000176static GLboolean DispatchOverride = GL_FALSE;
177
178
Brian Paul3c257e12001-03-28 17:19:58 +0000179/* strdup() is actually not a standard ANSI C or POSIX routine.
180 * Irix will not define it if ANSI mode is in effect.
181 */
182static char *
183str_dup(const char *str)
Randy Frankd7361e12000-03-27 21:13:58 +0000184{
Brian Paulfffb8092000-03-29 18:46:11 +0000185 char *copy;
186 copy = (char*) malloc(strlen(str) + 1);
187 if (!copy)
188 return NULL;
189 strcpy(copy, str);
190 return copy;
Randy Frankd7361e12000-03-27 21:13:58 +0000191}
192
Brian Paul7fb54ae1999-11-19 22:33:50 +0000193
Brian Paulbb72d321999-12-16 17:31:59 +0000194
195/*
196 * We should call this periodically from a function such as glXMakeCurrent
197 * in order to test if multiple threads are being used. When we detect
198 * that situation we should then call _glapi_enable_thread_safety()
199 */
200void
201_glapi_check_multithread(void)
202{
203#if defined(THREADS)
Brian Paul26e14d22000-01-05 04:36:17 +0000204 if (!ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000205 static unsigned long knownID;
206 static GLboolean firstCall = GL_TRUE;
207 if (firstCall) {
208 knownID = _glthread_GetID();
209 firstCall = GL_FALSE;
210 }
211 else if (knownID != _glthread_GetID()) {
Brian Paul26e14d22000-01-05 04:36:17 +0000212 ThreadSafe = GL_TRUE;
Brian Paulbb72d321999-12-16 17:31:59 +0000213 }
214 }
Brian Paul26e14d22000-01-05 04:36:17 +0000215 if (ThreadSafe) {
Brian Paulbb72d321999-12-16 17:31:59 +0000216 /* make sure that this thread's dispatch pointer isn't null */
217 if (!_glapi_get_dispatch()) {
218 _glapi_set_dispatch(NULL);
219 }
220 }
221#endif
222}
223
224
225
226/*
Brian Paul8f91fb61999-12-17 14:51:28 +0000227 * Set the current context pointer for this thread.
228 * The context pointer is an opaque type which should be cast to
229 * void from the real context pointer type.
230 */
231void
Brian Paulf9b97d92000-01-28 20:17:42 +0000232_glapi_set_context(void *context)
Brian Paul8f91fb61999-12-17 14:51:28 +0000233{
234#if defined(THREADS)
Brian Paul3c27be32000-02-10 21:27:48 +0000235 _glthread_SetTSD(&ContextTSD, context);
Brian Paul26e14d22000-01-05 04:36:17 +0000236 if (ThreadSafe)
Brian Paulf9b97d92000-01-28 20:17:42 +0000237 _glapi_Context = NULL;
Brian Paul8f91fb61999-12-17 14:51:28 +0000238 else
Brian Paulf9b97d92000-01-28 20:17:42 +0000239 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000240#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000241 _glapi_Context = context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000242#endif
243}
244
245
246
247/*
248 * Get the current context pointer for this thread.
249 * The context pointer is an opaque type which should be cast from
250 * void to the real context pointer type.
251 */
252void *
Brian Paulf9b97d92000-01-28 20:17:42 +0000253_glapi_get_context(void)
Brian Paul8f91fb61999-12-17 14:51:28 +0000254{
255#if defined(THREADS)
Brian Paul26e14d22000-01-05 04:36:17 +0000256 if (ThreadSafe) {
Brian Paul8f91fb61999-12-17 14:51:28 +0000257 return _glthread_GetTSD(&ContextTSD);
258 }
259 else {
Brian Paulf9b97d92000-01-28 20:17:42 +0000260 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000261 }
262#else
Brian Paulf9b97d92000-01-28 20:17:42 +0000263 return _glapi_Context;
Brian Paul8f91fb61999-12-17 14:51:28 +0000264#endif
265}
266
267
268
269/*
Brian Paul7fb54ae1999-11-19 22:33:50 +0000270 * Set the global or per-thread dispatch table pointer.
271 */
272void
273_glapi_set_dispatch(struct _glapi_table *dispatch)
274{
275 if (!dispatch) {
276 /* use the no-op functions */
Brian Paul8ceb5c32000-02-24 22:14:04 +0000277 dispatch = (struct _glapi_table *) __glapi_noop_table;
Brian Paul7fb54ae1999-11-19 22:33:50 +0000278 }
279#ifdef DEBUG
280 else {
281 _glapi_check_table(dispatch);
282 }
283#endif
284
285#if defined(THREADS)
Brian Paulab0c8862001-01-23 23:35:47 +0000286 if (DispatchOverride) {
287 _glthread_SetTSD(&RealDispatchTSD, (void *) dispatch);
Brian Paul3a71d052000-09-05 20:17:37 +0000288 if (ThreadSafe)
Brian Paul3c257e12001-03-28 17:19:58 +0000289 _glapi_RealDispatch = (struct _glapi_table*) __glapi_threadsafe_table;
Brian Paul3a71d052000-09-05 20:17:37 +0000290 else
Brian Paulab0c8862001-01-23 23:35:47 +0000291 _glapi_RealDispatch = dispatch;
Brian Paul3b18a362000-09-26 15:27:20 +0000292 }
293 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000294 /* normal operation */
Brian Paul3b18a362000-09-26 15:27:20 +0000295 _glthread_SetTSD(&DispatchTSD, (void *) dispatch);
Brian Paul3a71d052000-09-05 20:17:37 +0000296 if (ThreadSafe)
Brian Paul3c257e12001-03-28 17:19:58 +0000297 _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table;
Brian Paul3a71d052000-09-05 20:17:37 +0000298 else
299 _glapi_Dispatch = dispatch;
300 }
Brian Paul3a71d052000-09-05 20:17:37 +0000301#else /*THREADS*/
Brian Paulab0c8862001-01-23 23:35:47 +0000302 if (DispatchOverride) {
303 _glapi_RealDispatch = dispatch;
304 }
305 else {
306 _glapi_Dispatch = dispatch;
307 }
Brian Paul3a71d052000-09-05 20:17:37 +0000308#endif /*THREADS*/
Brian Paul7fb54ae1999-11-19 22:33:50 +0000309}
310
311
Brian Paulbb72d321999-12-16 17:31:59 +0000312
Brian Paul7fb54ae1999-11-19 22:33:50 +0000313/*
Brian Paulbb72d321999-12-16 17:31:59 +0000314 * Return pointer to current dispatch table for calling thread.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000315 */
316struct _glapi_table *
317_glapi_get_dispatch(void)
318{
319#if defined(THREADS)
Brian Paul26e14d22000-01-05 04:36:17 +0000320 if (ThreadSafe) {
Brian Paulab0c8862001-01-23 23:35:47 +0000321 if (DispatchOverride) {
322 return (struct _glapi_table *) _glthread_GetTSD(&RealDispatchTSD);
323 }
324 else {
325 return (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD);
326 }
Brian Paulbb72d321999-12-16 17:31:59 +0000327 }
Brian Paul590d3471999-12-17 12:20:23 +0000328 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000329 if (DispatchOverride) {
330 assert(_glapi_RealDispatch);
331 return _glapi_RealDispatch;
332 }
333 else {
334 assert(_glapi_Dispatch);
335 return _glapi_Dispatch;
336 }
Brian Paul590d3471999-12-17 12:20:23 +0000337 }
Brian Paul7fb54ae1999-11-19 22:33:50 +0000338#else
Brian Paulc2319b42000-01-17 19:28:31 +0000339 return _glapi_Dispatch;
Brian Paul7fb54ae1999-11-19 22:33:50 +0000340#endif
341}
342
343
Brian Paulab0c8862001-01-23 23:35:47 +0000344/*
345 * Notes on dispatch overrride:
346 *
347 * Dispatch override allows an external agent to hook into the GL dispatch
348 * mechanism before execution goes into the core rendering library. For
349 * example, a trace mechanism would insert itself as an overrider, print
350 * logging info for each GL function, then dispatch to the real GL function.
351 *
352 * libGLS (GL Stream library) is another agent that might use override.
353 *
354 * We don't allow more than one layer of overriding at this time.
355 * In the future we may allow nested/layered override. In that case
356 * _glapi_begin_dispatch_override() will return an override layer,
357 * _glapi_end_dispatch_override(layer) will remove an override layer
358 * and _glapi_get_override_dispatch(layer) will return the dispatch
359 * table for a given override layer. layer = 0 will be the "real"
360 * dispatch table.
361 */
362
363/*
364 * Return: dispatch override layer number.
365 */
366int
367_glapi_begin_dispatch_override(struct _glapi_table *override)
Brian Paul3a71d052000-09-05 20:17:37 +0000368{
Brian Paulab0c8862001-01-23 23:35:47 +0000369 struct _glapi_table *real = _glapi_get_dispatch();
370
371 assert(!DispatchOverride); /* can't nest at this time */
372 DispatchOverride = GL_TRUE;
373
374 _glapi_set_dispatch(real);
Brian Paul3a71d052000-09-05 20:17:37 +0000375
376#if defined(THREADS)
Brian Paulab0c8862001-01-23 23:35:47 +0000377 _glthread_SetTSD(&DispatchTSD, (void *) override);
378 if (ThreadSafe)
Brian Paul3c257e12001-03-28 17:19:58 +0000379 _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table;
Brian Paulab0c8862001-01-23 23:35:47 +0000380 else
381 _glapi_Dispatch = override;
382#else
383 _glapi_Dispatch = override;
384#endif
385 return 1;
386}
387
388
389void
390_glapi_end_dispatch_override(int layer)
391{
392 struct _glapi_table *real = _glapi_get_dispatch();
393 (void) layer;
394 DispatchOverride = GL_FALSE;
395 _glapi_set_dispatch(real);
396 /* the rest of this isn't needed, just play it safe */
397#if defined(THREADS)
398 _glthread_SetTSD(&RealDispatchTSD, NULL);
399#endif
400 _glapi_RealDispatch = NULL;
401}
402
403
404struct _glapi_table *
405_glapi_get_override_dispatch(int layer)
406{
407 if (layer == 0) {
408 return _glapi_get_dispatch();
Brian Paul3a71d052000-09-05 20:17:37 +0000409 }
410 else {
Brian Paulab0c8862001-01-23 23:35:47 +0000411 if (DispatchOverride) {
412#if defined(THREADS)
413 return (struct _glapi_table *) _glthread_GetTSD(&DispatchTSD);
Brian Paul3a71d052000-09-05 20:17:37 +0000414#else
Brian Paulab0c8862001-01-23 23:35:47 +0000415 return _glapi_Dispatch;
Brian Paul3a71d052000-09-05 20:17:37 +0000416#endif
Brian Paulab0c8862001-01-23 23:35:47 +0000417 }
418 else {
419 return NULL;
420 }
421 }
Brian Paul3a71d052000-09-05 20:17:37 +0000422}
Brian Paulab0c8862001-01-23 23:35:47 +0000423
Brian Paul3a71d052000-09-05 20:17:37 +0000424
Brian Paul91bcefa1999-11-27 21:30:40 +0000425
426/*
427 * Return size of dispatch table struct as number of functions (or
428 * slots).
429 */
430GLuint
431_glapi_get_dispatch_table_size(void)
432{
Brian Paul0f710251999-12-15 15:02:30 +0000433 /* return sizeof(struct _glapi_table) / sizeof(void *);*/
434 GetSizeCalled = GL_TRUE;
435 return MaxDispatchOffset + 1;
Brian Paul91bcefa1999-11-27 21:30:40 +0000436}
437
438
439
Brian Paul7fb54ae1999-11-19 22:33:50 +0000440/*
441 * Get API dispatcher version string.
Brian Paul7fb54ae1999-11-19 22:33:50 +0000442 */
443const char *
444_glapi_get_version(void)
445{
Brian Paulab0c8862001-01-23 23:35:47 +0000446 return "20010116"; /* YYYYMMDD */
Brian Paul7fb54ae1999-11-19 22:33:50 +0000447}
448
449
Brian Paul0c239fc1999-12-16 12:38:11 +0000450/*
451 * For each entry in static_functions[] which use this function
452 * we should implement a dispatch function in glapitemp.h and
453 * in glapinoop.c
454 */
Brian Paulb3674092000-01-08 11:01:24 +0000455static int NotImplemented(void)
Brian Paul0c239fc1999-12-16 12:38:11 +0000456{
Brian Paulb3674092000-01-08 11:01:24 +0000457 return 0;
Brian Paul0c239fc1999-12-16 12:38:11 +0000458}
459
460
Brian Paul959f8022000-03-19 01:10:11 +0000461struct name_address_offset {
462 const char *Name;
463 GLvoid *Address;
464 GLuint Offset;
465};
466
Brian Paul0c239fc1999-12-16 12:38:11 +0000467
Brian Paulb5fd8862001-11-18 22:48:11 +0000468/* The code in this file is auto-generated */
469#include "glprocs.h"
Brian Paul7fb54ae1999-11-19 22:33:50 +0000470
Brian Paul959f8022000-03-19 01:10:11 +0000471
472
473/*
474 * Return dispatch table offset of the named static (built-in) function.
475 * Return -1 if function not found.
476 */
477static GLint
478get_static_proc_offset(const char *funcName)
479{
480 GLuint i;
481 for (i = 0; static_functions[i].Name; i++) {
482 if (strcmp(static_functions[i].Name, funcName) == 0) {
Brian Paul94666c42001-09-14 02:48:53 +0000483 return static_functions[i].Offset;
Brian Paul959f8022000-03-19 01:10:11 +0000484 }
485 }
486 return -1;
487}
488
489
490/*
491 * Return dispatch function address the named static (built-in) function.
492 * Return NULL if function not found.
493 */
494static GLvoid *
495get_static_proc_address(const char *funcName)
496{
Brian Paul9c7ca852000-10-19 20:13:12 +0000497 GLint i;
498 for (i = 0; static_functions[i].Name; i++) {
499 if (strcmp(static_functions[i].Name, funcName) == 0) {
500 return static_functions[i].Address;
501 }
502 }
503 return NULL;
Brian Paul959f8022000-03-19 01:10:11 +0000504}
505
506
507
508/**********************************************************************
509 * Extension function management.
510 */
511
512
513#define MAX_EXTENSION_FUNCS 1000
514
515static struct name_address_offset ExtEntryTable[MAX_EXTENSION_FUNCS];
516static GLuint NumExtEntryPoints = 0;
517
davem694a497e62001-06-06 22:55:28 +0000518#ifdef USE_SPARC_ASM
519extern void __glapi_sparc_icache_flush(unsigned int *);
520#endif
Brian Paul959f8022000-03-19 01:10:11 +0000521
522/*
523 * Generate a dispatch function (entrypoint) which jumps through
524 * the given slot number (offset) in the current dispatch table.
525 * We need assembly language in order to accomplish this.
526 */
527static void *
528generate_entrypoint(GLuint functionOffset)
529{
530#if defined(USE_X86_ASM)
531 /*
532 * This x86 code contributed by Josh Vanderhoof.
533 *
534 * 0: a1 10 32 54 76 movl __glapi_Dispatch,%eax
535 * 00 01 02 03 04
536 * 5: 85 c0 testl %eax,%eax
537 * 05 06
538 * 7: 74 06 je f <entrypoint+0xf>
539 * 07 08
540 * 9: ff a0 10 32 54 76 jmp *0x76543210(%eax)
541 * 09 0a 0b 0c 0d 0e
542 * f: e8 fc ff ff ff call __glapi_get_dispatch
543 * 0f 10 11 12 13
544 * 14: ff a0 10 32 54 76 jmp *0x76543210(%eax)
545 * 14 15 16 17 18 19
546 */
547 static const unsigned char temp[] = {
548 0xa1, 0x00, 0x00, 0x00, 0x00,
549 0x85, 0xc0,
550 0x74, 0x06,
551 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00,
552 0xe8, 0x00, 0x00, 0x00, 0x00,
553 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00
554 };
555 unsigned char *code = malloc(sizeof(temp));
556 unsigned int next_insn;
557 if (code) {
558 memcpy(code, temp, sizeof(temp));
559
560 *(unsigned int *)(code + 0x01) = (unsigned int)&_glapi_Dispatch;
561 *(unsigned int *)(code + 0x0b) = (unsigned int)functionOffset * 4;
562 next_insn = (unsigned int)(code + 0x14);
563 *(unsigned int *)(code + 0x10) = (unsigned int)_glapi_get_dispatch - next_insn;
564 *(unsigned int *)(code + 0x16) = (unsigned int)functionOffset * 4;
565 }
566 return code;
davem69775355a2001-06-05 23:54:00 +0000567#elif defined(USE_SPARC_ASM)
568
569#ifdef __sparc_v9__
570 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 };
591#endif
592 unsigned int *code = malloc(sizeof(insn_template));
593 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
594 if (code) {
595 memcpy(code, insn_template, sizeof(insn_template));
596
597#ifdef __sparc_v9__
598 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]);
davem69775355a2001-06-05 23:54:00 +0000613#endif
614 }
615 return code;
Brian Paul959f8022000-03-19 01:10:11 +0000616#else
617 return NULL;
618#endif
619}
620
621
622
623/*
624 * Add a new extension function entrypoint.
625 * Return: GL_TRUE = success or GL_FALSE = failure
626 */
627GLboolean
628_glapi_add_entrypoint(const char *funcName, GLuint offset)
629{
Brian Paul959f8022000-03-19 01:10:11 +0000630 /* first check if the named function is already statically present */
631 {
632 GLint index = get_static_proc_offset(funcName);
633 if (index >= 0) {
Brian Paulb51b0a82001-03-07 05:06:11 +0000634 return (GLboolean) ((GLuint) index == offset); /* bad offset! */
Brian Paul959f8022000-03-19 01:10:11 +0000635 }
636 }
637
638 {
639 /* make sure this offset/name pair is legal */
640 const char *name = _glapi_get_proc_name(offset);
641 if (name && strcmp(name, funcName) != 0)
642 return GL_FALSE; /* bad name! */
643 }
644
645 {
646 /* be sure index and name match known data */
647 GLuint i;
648 for (i = 0; i < NumExtEntryPoints; i++) {
649 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
650 /* function already registered with api */
651 if (ExtEntryTable[i].Offset == offset) {
652 return GL_TRUE; /* offsets match */
653 }
654 else {
655 return GL_FALSE; /* bad offset! */
656 }
657 }
658 }
659
Brian Paul2c3a6202000-05-24 17:53:30 +0000660 /* Make sure we don't try to add a new entrypoint after someone
661 * has already called _glapi_get_dispatch_table_size()! If that's
662 * happened the caller's information would become out of date.
663 */
664 if (GetSizeCalled)
665 return GL_FALSE;
666
Brian Paul959f8022000-03-19 01:10:11 +0000667 /* make sure we have space */
668 if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS) {
669 return GL_FALSE;
670 }
671 else {
672 void *entrypoint = generate_entrypoint(offset);
673 if (!entrypoint)
674 return GL_FALSE;
675
Brian Paulfffb8092000-03-29 18:46:11 +0000676 ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
Brian Paul959f8022000-03-19 01:10:11 +0000677 ExtEntryTable[NumExtEntryPoints].Offset = offset;
678 ExtEntryTable[NumExtEntryPoints].Address = entrypoint;
679 NumExtEntryPoints++;
680
681 if (offset > MaxDispatchOffset)
682 MaxDispatchOffset = offset;
683
684 return GL_TRUE; /* success */
685 }
686 }
687
688 /* should never get here, but play it safe */
689 return GL_FALSE;
690}
691
692
693
694#if 0000 /* prototype code for dynamic extension slot allocation */
695
696static int NextFreeOffset = 409; /*XXX*/
697#define MAX_DISPATCH_TABLE_SIZE 1000
698
699/*
700 * Dynamically allocate a dispatch slot for an extension entrypoint
701 * and generate the assembly language dispatch stub.
702 * Return the dispatch offset for the function or -1 if no room or error.
703 */
704GLint
705_glapi_add_entrypoint2(const char *funcName)
706{
707 int offset;
708
709 /* first see if extension func is already known */
710 offset = _glapi_get_proc_offset(funcName);
711 if (offset >= 0)
712 return offset;
713
714 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS
715 && NextFreeOffset < MAX_DISPATCH_TABLE_SIZE) {
716 void *entryPoint;
717 offset = NextFreeOffset;
718 entryPoint = generate_entrypoint(offset);
719 if (entryPoint) {
720 NextFreeOffset++;
Brian Paulfffb8092000-03-29 18:46:11 +0000721 ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
Brian Paul959f8022000-03-19 01:10:11 +0000722 ExtEntryTable[NumExtEntryPoints].Offset = offset;
723 ExtEntryTable[NumExtEntryPoints].Address = entryPoint;
724 NumExtEntryPoints++;
725 return offset;
726 }
727 }
728 return -1;
729}
730
731#endif
732
733
734
735/*
736 * Return offset of entrypoint for named function within dispatch table.
737 */
738GLint
739_glapi_get_proc_offset(const char *funcName)
740{
741 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000742 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000743 for (i = 0; i < NumExtEntryPoints; i++) {
744 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
745 return ExtEntryTable[i].Offset;
746 }
747 }
748
749 /* search static functions */
750 return get_static_proc_offset(funcName);
751}
752
753
754
755/*
756 * Return entrypoint for named function.
757 */
758const GLvoid *
759_glapi_get_proc_address(const char *funcName)
760{
761 /* search extension functions first */
Brian Paulb51b0a82001-03-07 05:06:11 +0000762 GLuint i;
Brian Paul959f8022000-03-19 01:10:11 +0000763 for (i = 0; i < NumExtEntryPoints; i++) {
764 if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
765 return ExtEntryTable[i].Address;
766 }
767 }
768
769 /* search static functions */
770 return get_static_proc_address(funcName);
771}
772
773
774
775
776/*
777 * Return the name of the function at the given dispatch offset.
778 * This is only intended for debugging.
779 */
780const char *
781_glapi_get_proc_name(GLuint offset)
782{
783 const GLuint n = sizeof(static_functions) / sizeof(struct name_address_offset);
784 GLuint i;
785 for (i = 0; i < n; i++) {
786 if (static_functions[i].Offset == offset)
787 return static_functions[i].Name;
788 }
789
790 /* search added extension functions */
791 for (i = 0; i < NumExtEntryPoints; i++) {
792 if (ExtEntryTable[i].Offset == offset) {
793 return ExtEntryTable[i].Name;
794 }
795 }
796 return NULL;
797}
798
799
800
801/*
802 * Make sure there are no NULL pointers in the given dispatch table.
803 * Intented for debugging purposes.
804 */
805void
806_glapi_check_table(const struct _glapi_table *table)
807{
808 const GLuint entries = _glapi_get_dispatch_table_size();
809 const void **tab = (const void **) table;
810 GLuint i;
811 for (i = 1; i < entries; i++) {
812 assert(tab[i]);
813 }
814
815#ifdef DEBUG
816 /* Do some spot checks to be sure that the dispatch table
817 * slots are assigned correctly.
818 */
819 {
820 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
821 char *BeginFunc = (char*) &table->Begin;
822 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
823 assert(BeginOffset == _gloffset_Begin);
824 assert(BeginOffset == offset);
825 }
826 {
827 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
828 char *viewportFunc = (char*) &table->Viewport;
829 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
830 assert(viewportOffset == _gloffset_Viewport);
831 assert(viewportOffset == offset);
832 }
833 {
834 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
835 char *VertexPointerFunc = (char*) &table->VertexPointer;
836 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
837 assert(VertexPointerOffset == _gloffset_VertexPointer);
838 assert(VertexPointerOffset == offset);
839 }
840 {
841 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
842 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
843 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
844 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
845 assert(ResetMinMaxOffset == offset);
846 }
847 {
848 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
849 char *blendColorFunc = (char*) &table->BlendColor;
850 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
851 assert(blendColorOffset == _gloffset_BlendColor);
852 assert(blendColorOffset == offset);
853 }
854 {
855 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
856 char *istextureFunc = (char*) &table->IsTextureEXT;
857 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
858 assert(istextureOffset == _gloffset_IsTextureEXT);
859 assert(istextureOffset == offset);
860 }
Brian Paula14cbff2000-10-27 18:31:21 +0000861 {
862 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
863 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
864 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
865 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
866 assert(secondaryColor3fOffset == offset);
867 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (void *) &glSecondaryColor3fEXT);
868 }
Brian Paul959f8022000-03-19 01:10:11 +0000869#endif
870}