st/mesa: rename var and update st_choose_format() comments
[mesa.git] / src / mesa / main / remap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
4 *
5 * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
6 *
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 OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file remap.c
29 * Remap table management.
30 *
31 * Entries in the dispatch table are either static or dynamic. The
32 * dispatch table is shared by mesa core and glapi. When they are
33 * built separately, it is possible that a static entry in mesa core
34 * is dynamic, or assigned a different static offset, in glapi. The
35 * remap table is in charge of mapping a static entry in mesa core to
36 * a dynamic entry, or the corresponding static entry, in glapi.
37 */
38
39 #include "remap.h"
40 #include "imports.h"
41
42 #include "main/dispatch.h"
43
44
45 #if FEATURE_remap_table
46
47
48 #define need_MESA_remap_table
49 #include "main/remap_helper.h"
50
51 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
52 #define MAX_ENTRY_POINTS 16
53
54
55 /* this is global for quick access */
56 int driDispatchRemapTable[driDispatchRemapTable_size];
57
58
59 /**
60 * Return the spec string associated with the given function index.
61 * The index is available from including remap_helper.h.
62 *
63 * \param func_index an opaque function index.
64 *
65 * \return the spec string associated with the function index, or NULL.
66 */
67 const char *
68 _mesa_get_function_spec(GLint func_index)
69 {
70 if (func_index < ARRAY_SIZE(_mesa_function_pool))
71 return _mesa_function_pool + func_index;
72 else
73 return NULL;
74 }
75
76
77 /**
78 * Map a function by its spec. The function will be added to glapi,
79 * and the dispatch offset will be returned.
80 *
81 * \param spec a '\0'-separated string array specifying a function.
82 * It begins with the parameter signature of the function,
83 * followed by the names of the entry points. An empty entry
84 * point name terminates the array.
85 *
86 * \return the offset of the (re-)mapped function in the dispatch
87 * table, or -1.
88 */
89 GLint
90 _mesa_map_function_spec(const char *spec)
91 {
92 const char *signature;
93 const char *names[MAX_ENTRY_POINTS + 1];
94 GLint num_names = 0;
95
96 if (!spec)
97 return -1;
98
99 signature = spec;
100 spec += strlen(spec) + 1;
101
102 /* spec is terminated by an empty string */
103 while (*spec) {
104 names[num_names] = spec;
105 num_names++;
106 if (num_names >= MAX_ENTRY_POINTS)
107 break;
108 spec += strlen(spec) + 1;
109 }
110 if (!num_names)
111 return -1;
112
113 names[num_names] = NULL;
114
115 /* add the entry points to the dispatch table */
116 return _glapi_add_dispatch(names, signature);
117 }
118
119
120 /**
121 * Map an array of functions. This is a convenient function for
122 * use with arrays available from including remap_helper.h.
123 *
124 * Note that the dispatch offsets of the functions are not returned.
125 * If they are needed, _mesa_map_function_spec() should be used.
126 *
127 * \param func_array an array of function remaps.
128 */
129 void
130 _mesa_map_function_array(const struct gl_function_remap *func_array)
131 {
132 GLint i;
133
134 if (!func_array)
135 return;
136
137 for (i = 0; func_array[i].func_index != -1; i++) {
138 const char *spec;
139 GLint offset;
140
141 spec = _mesa_get_function_spec(func_array[i].func_index);
142 if (!spec) {
143 _mesa_problem(NULL, "invalid function index %d",
144 func_array[i].func_index);
145 continue;
146 }
147
148 offset = _mesa_map_function_spec(spec);
149 /* error checks */
150 if (offset < 0) {
151 const char *name = spec + strlen(spec) + 1;
152 _mesa_warning(NULL, "failed to remap %s", name);
153 }
154 else if (func_array[i].dispatch_offset >= 0 &&
155 offset != func_array[i].dispatch_offset) {
156 const char *name = spec + strlen(spec) + 1;
157 _mesa_problem(NULL, "%s should be mapped to %d, not %d",
158 name, func_array[i].dispatch_offset, offset);
159 }
160 }
161 }
162
163
164 /**
165 * Map the functions which are already static.
166 *
167 * When a extension function are incorporated into the ABI, the
168 * extension suffix is usually stripped. Mapping such functions
169 * makes sure the alternative names are available.
170 *
171 * Note that functions mapped by _mesa_init_remap_table() are
172 * excluded.
173 */
174 void
175 _mesa_map_static_functions(void)
176 {
177 /* Remap static functions which have alternative names and are in the ABI.
178 * This is to be on the safe side. glapi should have defined those names.
179 */
180 _mesa_map_function_array(MESA_alt_functions);
181 }
182
183
184 /**
185 * Initialize the remap table. This is called in one_time_init().
186 * The remap table needs to be initialized before calling the
187 * CALL/GET/SET macros defined in main/dispatch.h.
188 */
189 void
190 _mesa_init_remap_table(void)
191 {
192 static GLboolean initialized = GL_FALSE;
193 GLint i;
194
195 if (initialized)
196 return;
197 initialized = GL_TRUE;
198
199 /* initialize the remap table */
200 for (i = 0; i < ARRAY_SIZE(driDispatchRemapTable); i++) {
201 GLint offset;
202 const char *spec;
203
204 /* sanity check */
205 ASSERT(i == MESA_remap_table_functions[i].remap_index);
206 spec = _mesa_function_pool + MESA_remap_table_functions[i].pool_index;
207
208 offset = _mesa_map_function_spec(spec);
209 /* store the dispatch offset in the remap table */
210 driDispatchRemapTable[i] = offset;
211 if (offset < 0)
212 _mesa_warning(NULL, "failed to remap index %d", i);
213 }
214 }
215
216
217 #endif /* FEATURE_remap_table */