mesa: call _mesa_make_extension_string only as needed
[mesa.git] / src / mesa / main / getstring.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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
17 * OR 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
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdbool.h>
27 #include "glheader.h"
28 #include "context.h"
29 #include "debug_output.h"
30 #include "get.h"
31 #include "enums.h"
32 #include "extensions.h"
33 #include "mtypes.h"
34 #include "macros.h"
35
36 /**
37 * Return the string for a glGetString(GL_SHADING_LANGUAGE_VERSION) query.
38 */
39 static const GLubyte *
40 shading_language_version(struct gl_context *ctx)
41 {
42 switch (ctx->API) {
43 case API_OPENGL_COMPAT:
44 case API_OPENGL_CORE:
45 switch (ctx->Const.GLSLVersion) {
46 case 120:
47 return (const GLubyte *) "1.20";
48 case 130:
49 return (const GLubyte *) "1.30";
50 case 140:
51 return (const GLubyte *) "1.40";
52 case 150:
53 return (const GLubyte *) "1.50";
54 case 330:
55 return (const GLubyte *) "3.30";
56 case 400:
57 return (const GLubyte *) "4.00";
58 case 410:
59 return (const GLubyte *) "4.10";
60 case 420:
61 return (const GLubyte *) "4.20";
62 case 430:
63 return (const GLubyte *) "4.30";
64 case 440:
65 return (const GLubyte *) "4.40";
66 case 450:
67 return (const GLubyte *) "4.50";
68 case 460:
69 return (const GLubyte *) "4.60";
70 default:
71 _mesa_problem(ctx,
72 "Invalid GLSL version in shading_language_version()");
73 return (const GLubyte *) 0;
74 }
75 break;
76
77 case API_OPENGLES2:
78 switch (ctx->Version) {
79 case 20:
80 return (const GLubyte *) "OpenGL ES GLSL ES 1.0.16";
81 case 30:
82 return (const GLubyte *) "OpenGL ES GLSL ES 3.00";
83 case 31:
84 return (const GLubyte *) "OpenGL ES GLSL ES 3.10";
85 case 32:
86 return (const GLubyte *) "OpenGL ES GLSL ES 3.20";
87 default:
88 _mesa_problem(ctx,
89 "Invalid OpenGL ES version in shading_language_version()");
90 return (const GLubyte *) 0;
91 }
92 case API_OPENGLES:
93 /* fall-through */
94
95 default:
96 _mesa_problem(ctx, "Unexpected API value in shading_language_version()");
97 return (const GLubyte *) 0;
98 }
99 }
100
101
102 /**
103 * Query string-valued state. The return value should _not_ be freed by
104 * the caller.
105 *
106 * \param name the state variable to query.
107 *
108 * \sa glGetString().
109 *
110 * Tries to get the string from dd_function_table::GetString, otherwise returns
111 * the hardcoded strings.
112 */
113 const GLubyte * GLAPIENTRY
114 _mesa_GetString( GLenum name )
115 {
116 GET_CURRENT_CONTEXT(ctx);
117 static const char *vendor = "Brian Paul";
118 static const char *renderer = "Mesa";
119
120 if (!ctx)
121 return NULL;
122
123 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
124
125 /* this is a required driver function */
126 assert(ctx->Driver.GetString);
127 {
128 /* Give the driver the chance to handle this query */
129 const GLubyte *str = ctx->Driver.GetString(ctx, name);
130 if (str)
131 return str;
132 }
133
134 switch (name) {
135 case GL_VENDOR:
136 return (const GLubyte *) vendor;
137 case GL_RENDERER:
138 return (const GLubyte *) renderer;
139 case GL_VERSION:
140 return (const GLubyte *) ctx->VersionString;
141 case GL_EXTENSIONS:
142 if (ctx->API == API_OPENGL_CORE) {
143 _mesa_error(ctx, GL_INVALID_ENUM, "glGetString(GL_EXTENSIONS)");
144 return (const GLubyte *) 0;
145 }
146 if (!ctx->Extensions.String)
147 ctx->Extensions.String = _mesa_make_extension_string(ctx);
148 return (const GLubyte *) ctx->Extensions.String;
149 case GL_SHADING_LANGUAGE_VERSION:
150 if (ctx->API == API_OPENGLES)
151 break;
152 return shading_language_version(ctx);
153 case GL_PROGRAM_ERROR_STRING_ARB:
154 if (ctx->API == API_OPENGL_COMPAT &&
155 (ctx->Extensions.ARB_fragment_program ||
156 ctx->Extensions.ARB_vertex_program)) {
157 return (const GLubyte *) ctx->Program.ErrorString;
158 }
159 break;
160 default:
161 break;
162 }
163
164 _mesa_error( ctx, GL_INVALID_ENUM, "glGetString" );
165 return (const GLubyte *) 0;
166 }
167
168
169 /**
170 * GL3
171 */
172 const GLubyte * GLAPIENTRY
173 _mesa_GetStringi(GLenum name, GLuint index)
174 {
175 GET_CURRENT_CONTEXT(ctx);
176
177 if (!ctx)
178 return NULL;
179
180 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
181
182 switch (name) {
183 case GL_EXTENSIONS:
184 if (index >= _mesa_get_extension_count(ctx)) {
185 _mesa_error(ctx, GL_INVALID_VALUE, "glGetStringi(index=%u)", index);
186 return (const GLubyte *) 0;
187 }
188 return _mesa_get_enabled_extension(ctx, index);
189 default:
190 _mesa_error(ctx, GL_INVALID_ENUM, "glGetStringi");
191 return (const GLubyte *) 0;
192 }
193 }
194
195
196
197 /**
198 * Return pointer-valued state, such as a vertex array pointer.
199 *
200 * \param pname names state to be queried
201 * \param params returns the pointer value
202 *
203 * \sa glGetPointerv().
204 *
205 * Tries to get the specified pointer via dd_function_table::GetPointerv,
206 * otherwise gets the specified pointer from the current context.
207 */
208 void GLAPIENTRY
209 _mesa_GetPointerv( GLenum pname, GLvoid **params )
210 {
211 GET_CURRENT_CONTEXT(ctx);
212 const GLuint clientUnit = ctx->Array.ActiveTexture;
213 const char *callerstr;
214
215 if (_mesa_is_desktop_gl(ctx))
216 callerstr = "glGetPointerv";
217 else
218 callerstr = "glGetPointervKHR";
219
220 if (!params)
221 return;
222
223 if (MESA_VERBOSE & VERBOSE_API)
224 _mesa_debug(ctx, "%s %s\n", callerstr, _mesa_enum_to_string(pname));
225
226 switch (pname) {
227 case GL_VERTEX_ARRAY_POINTER:
228 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
229 goto invalid_pname;
230 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Ptr;
231 break;
232 case GL_NORMAL_ARRAY_POINTER:
233 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
234 goto invalid_pname;
235 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_NORMAL].Ptr;
236 break;
237 case GL_COLOR_ARRAY_POINTER:
238 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
239 goto invalid_pname;
240 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR0].Ptr;
241 break;
242 case GL_SECONDARY_COLOR_ARRAY_POINTER_EXT:
243 if (ctx->API != API_OPENGL_COMPAT)
244 goto invalid_pname;
245 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR1].Ptr;
246 break;
247 case GL_FOG_COORDINATE_ARRAY_POINTER_EXT:
248 if (ctx->API != API_OPENGL_COMPAT)
249 goto invalid_pname;
250 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_FOG].Ptr;
251 break;
252 case GL_INDEX_ARRAY_POINTER:
253 if (ctx->API != API_OPENGL_COMPAT)
254 goto invalid_pname;
255 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Ptr;
256 break;
257 case GL_TEXTURE_COORD_ARRAY_POINTER:
258 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
259 goto invalid_pname;
260 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_TEX(clientUnit)].Ptr;
261 break;
262 case GL_EDGE_FLAG_ARRAY_POINTER:
263 if (ctx->API != API_OPENGL_COMPAT)
264 goto invalid_pname;
265 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Ptr;
266 break;
267 case GL_FEEDBACK_BUFFER_POINTER:
268 if (ctx->API != API_OPENGL_COMPAT)
269 goto invalid_pname;
270 *params = ctx->Feedback.Buffer;
271 break;
272 case GL_SELECTION_BUFFER_POINTER:
273 if (ctx->API != API_OPENGL_COMPAT)
274 goto invalid_pname;
275 *params = ctx->Select.Buffer;
276 break;
277 case GL_POINT_SIZE_ARRAY_POINTER_OES:
278 if (ctx->API != API_OPENGLES)
279 goto invalid_pname;
280 *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POINT_SIZE].Ptr;
281 break;
282 case GL_DEBUG_CALLBACK_FUNCTION_ARB:
283 case GL_DEBUG_CALLBACK_USER_PARAM_ARB:
284 *params = _mesa_get_debug_state_ptr(ctx, pname);
285 break;
286 default:
287 goto invalid_pname;
288 }
289
290 return;
291
292 invalid_pname:
293 _mesa_error( ctx, GL_INVALID_ENUM, "%s", callerstr);
294 return;
295 }
296
297
298 /**
299 * Returns the current GL error code, or GL_NO_ERROR.
300 * \return current error code
301 *
302 * Returns __struct gl_contextRec::ErrorValue.
303 */
304 GLenum GLAPIENTRY
305 _mesa_GetError( void )
306 {
307 GET_CURRENT_CONTEXT(ctx);
308 GLenum e = ctx->ErrorValue;
309 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
310
311 /* From Issue (3) of the KHR_no_error spec:
312 *
313 * "Should glGetError() always return NO_ERROR or have undefined
314 * results?
315 *
316 * RESOLVED: It should for all errors except OUT_OF_MEMORY."
317 */
318 if (_mesa_is_no_error_enabled(ctx) && e != GL_OUT_OF_MEMORY) {
319 e = GL_NO_ERROR;
320 }
321
322 if (MESA_VERBOSE & VERBOSE_API)
323 _mesa_debug(ctx, "glGetError <-- %s\n", _mesa_enum_to_string(e));
324
325 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
326 ctx->ErrorDebugCount = 0;
327 return e;
328 }