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