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