i965: Fix readpixels from ReadBuffer != DrawBuffer.
[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
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
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
41 #include "main/dispatch.h"
42
43
44 #if FEATURE_remap_table
45
46
47 #define need_MESA_remap_table
48 #include "main/remap_helper.h"
49
50 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
51 #define MAX_ENTRY_POINTS 16
52
53
54 /* this is global for quick access */
55 int driDispatchRemapTable[driDispatchRemapTable_size];
56
57
58 /**
59 * Return the spec string associated with the given function index.
60 * The index is available from including remap_helper.h.
61 *
62 * \param func_index an opaque function index.
63 *
64 * \return the spec string associated with the function index, or NULL.
65 */
66 const char *
67 _mesa_get_function_spec(GLint func_index)
68 {
69 if (func_index < ARRAY_SIZE(_mesa_function_pool))
70 return _mesa_function_pool + func_index;
71 else
72 return NULL;
73 }
74
75
76 /**
77 * Map a function by its spec. The function will be added to glapi,
78 * and the dispatch offset will be returned.
79 *
80 * \param spec a '\0'-separated string array specifying a function.
81 * It begins with the parameter signature of the function,
82 * followed by the names of the entry points. An empty entry
83 * point name terminates the array.
84 *
85 * \return the offset of the (re-)mapped function in the dispatch
86 * table, or -1.
87 */
88 GLint
89 _mesa_map_function_spec(const char *spec)
90 {
91 const char *signature;
92 const char *names[MAX_ENTRY_POINTS + 1];
93 GLint num_names = 0;
94
95 if (!spec)
96 return -1;
97
98 signature = spec;
99 spec += strlen(spec) + 1;
100
101 /* spec is terminated by an empty string */
102 while (*spec) {
103 names[num_names] = spec;
104 num_names++;
105 if (num_names >= MAX_ENTRY_POINTS)
106 break;
107 spec += strlen(spec) + 1;
108 }
109 if (!num_names)
110 return -1;
111
112 names[num_names] = NULL;
113
114 /* add the entry points to the dispatch table */
115 return _glapi_add_dispatch(names, signature);
116 }
117
118
119 /**
120 * Map an array of functions. This is a convenient function for
121 * use with arrays available from including remap_helper.h.
122 *
123 * Note that the dispatch offsets of the functions are not returned.
124 * If they are needed, _mesa_map_function_spec() should be used.
125 *
126 * \param func_array an array of function remaps.
127 */
128 void
129 _mesa_map_function_array(const struct gl_function_remap *func_array)
130 {
131 GLint i;
132
133 if (!func_array)
134 return;
135
136 for (i = 0; func_array[i].func_index != -1; i++) {
137 const char *spec;
138 GLint offset;
139
140 spec = _mesa_get_function_spec(func_array[i].func_index);
141 if (!spec) {
142 _mesa_problem(NULL, "invalid function index %d",
143 func_array[i].func_index);
144 continue;
145 }
146
147 offset = _mesa_map_function_spec(spec);
148 /* error checks */
149 if (offset < 0) {
150 const char *name = spec + strlen(spec) + 1;
151 _mesa_warning(NULL, "failed to remap %s", name);
152 }
153 else if (func_array[i].dispatch_offset >= 0 &&
154 offset != func_array[i].dispatch_offset) {
155 const char *name = spec + strlen(spec) + 1;
156 _mesa_problem(NULL, "%s should be mapped to %d, not %d",
157 name, func_array[i].dispatch_offset, offset);
158 }
159 }
160 }
161
162
163 /**
164 * Map the functions which are already static.
165 *
166 * When a extension function are incorporated into the ABI, the
167 * extension suffix is usually stripped. Mapping such functions
168 * makes sure the alternative names are available.
169 *
170 * Note that functions mapped by _mesa_init_remap_table() are
171 * excluded.
172 */
173 void
174 _mesa_map_static_functions(void)
175 {
176 /* Remap static functions which have alternative names and are in the ABI.
177 * This is to be on the safe side. glapi should have defined those names.
178 */
179 _mesa_map_function_array(MESA_alt_functions);
180 }
181
182
183 /**
184 * Initialize the remap table. This is called in one_time_init().
185 * The remap table needs to be initialized before calling the
186 * CALL/GET/SET macros defined in main/dispatch.h.
187 */
188 void
189 _mesa_init_remap_table(void)
190 {
191 static GLboolean initialized = GL_FALSE;
192 GLint i;
193
194 if (initialized)
195 return;
196 initialized = GL_TRUE;
197
198 /* initialize the remap table */
199 for (i = 0; i < ARRAY_SIZE(driDispatchRemapTable); i++) {
200 GLint offset;
201 const char *spec;
202
203 /* sanity check */
204 ASSERT(i == MESA_remap_table_functions[i].remap_index);
205 spec = _mesa_function_pool + MESA_remap_table_functions[i].pool_index;
206
207 offset = _mesa_map_function_spec(spec);
208 /* store the dispatch offset in the remap table */
209 driDispatchRemapTable[i] = offset;
210 if (offset < 0)
211 _mesa_warning(NULL, "failed to remap index %d", i);
212 }
213 }
214
215
216 #endif /* FEATURE_remap_table */