e76293c7f4eb6256be2a2fb5097ea9a9b7304b00
[mesa.git] / src / mesa / main / remap.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file remap.c
28 * Remap table management.
29 *
30 * Entries in the dispatch table are either static or dynamic. The
31 * dispatch table is shared by mesa core and glapi. When they are
32 * built separately, it is possible that a static entry in mesa core
33 * is dynamic, or assigned a different static offset, in glapi. The
34 * remap table is in charge of mapping a static entry in mesa core to
35 * a dynamic entry, or the corresponding static entry, in glapi.
36 */
37
38 #include "remap.h"
39 #include "imports.h"
40 #include "glapi/glapi.h"
41
42 #define MAX_ENTRY_POINTS 16
43
44 #define need_MESA_remap_table
45 #include "main/remap_helper.h"
46
47
48 /* this is global for quick access */
49 int driDispatchRemapTable[driDispatchRemapTable_size];
50
51
52 /**
53 * Map a function by its spec. The function will be added to glapi,
54 * and the dispatch offset will be returned.
55 *
56 * \param spec a '\0'-separated string array specifying a function.
57 * It begins with the parameter signature of the function,
58 * followed by the names of the entry points. An empty entry
59 * point name terminates the array.
60 *
61 * \return the offset of the (re-)mapped function in the dispatch
62 * table, or -1.
63 */
64 GLint
65 _mesa_map_function_spec(const char *spec)
66 {
67 const char *signature;
68 const char *names[MAX_ENTRY_POINTS + 1];
69 GLint num_names = 0;
70
71 if (!spec)
72 return -1;
73
74 signature = spec;
75 spec += strlen(spec) + 1;
76
77 /* spec is terminated by an empty string */
78 while (*spec) {
79 names[num_names] = spec;
80 num_names++;
81 if (num_names >= MAX_ENTRY_POINTS)
82 break;
83 spec += strlen(spec) + 1;
84 }
85 if (!num_names)
86 return -1;
87
88 names[num_names] = NULL;
89
90 /* add the entry points to the dispatch table */
91 return _glapi_add_dispatch(names, signature);
92 }
93
94
95 /**
96 * Initialize the remap table. This is called in one_time_init().
97 * The remap table needs to be initialized before calling the
98 * CALL/GET/SET macros defined in main/dispatch.h.
99 */
100 static void
101 _mesa_do_init_remap_table(const char *pool,
102 int size,
103 const struct gl_function_pool_remap *remap)
104 {
105 static GLboolean initialized = GL_FALSE;
106 GLint i;
107
108 if (initialized)
109 return;
110 initialized = GL_TRUE;
111
112 /* initialize the remap table */
113 for (i = 0; i < size; i++) {
114 GLint offset;
115 const char *spec;
116
117 /* sanity check */
118 assert(i == remap[i].remap_index);
119 spec = _mesa_function_pool + remap[i].pool_index;
120
121 offset = _mesa_map_function_spec(spec);
122 /* store the dispatch offset in the remap table */
123 driDispatchRemapTable[i] = offset;
124 if (offset < 0) {
125 const char *name = spec + strlen(spec) + 1;
126 _mesa_warning(NULL, "failed to remap %s", name);
127 }
128 }
129 }
130
131
132 void
133 _mesa_init_remap_table(void)
134 {
135 _mesa_do_init_remap_table(_mesa_function_pool,
136 driDispatchRemapTable_size,
137 MESA_remap_table_functions);
138 }