getstring.c revision b8e80941
1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg#include <stdbool.h>
27848b8605Smrg#include "glheader.h"
28848b8605Smrg#include "context.h"
29b8e80941Smrg#include "debug_output.h"
30848b8605Smrg#include "get.h"
31848b8605Smrg#include "enums.h"
32848b8605Smrg#include "extensions.h"
33848b8605Smrg#include "mtypes.h"
34b8e80941Smrg#include "macros.h"
35b8e80941Smrg#include "version.h"
36848b8605Smrg
37848b8605Smrg/**
38848b8605Smrg * Return the string for a glGetString(GL_SHADING_LANGUAGE_VERSION) query.
39848b8605Smrg */
40848b8605Smrgstatic const GLubyte *
41848b8605Smrgshading_language_version(struct gl_context *ctx)
42848b8605Smrg{
43848b8605Smrg   switch (ctx->API) {
44848b8605Smrg   case API_OPENGL_COMPAT:
45848b8605Smrg   case API_OPENGL_CORE:
46848b8605Smrg      switch (ctx->Const.GLSLVersion) {
47848b8605Smrg      case 120:
48848b8605Smrg         return (const GLubyte *) "1.20";
49848b8605Smrg      case 130:
50848b8605Smrg         return (const GLubyte *) "1.30";
51848b8605Smrg      case 140:
52848b8605Smrg         return (const GLubyte *) "1.40";
53848b8605Smrg      case 150:
54848b8605Smrg         return (const GLubyte *) "1.50";
55848b8605Smrg      case 330:
56848b8605Smrg         return (const GLubyte *) "3.30";
57848b8605Smrg      case 400:
58848b8605Smrg         return (const GLubyte *) "4.00";
59848b8605Smrg      case 410:
60848b8605Smrg         return (const GLubyte *) "4.10";
61848b8605Smrg      case 420:
62848b8605Smrg         return (const GLubyte *) "4.20";
63b8e80941Smrg      case 430:
64b8e80941Smrg         return (const GLubyte *) "4.30";
65b8e80941Smrg      case 440:
66b8e80941Smrg         return (const GLubyte *) "4.40";
67b8e80941Smrg      case 450:
68b8e80941Smrg         return (const GLubyte *) "4.50";
69b8e80941Smrg      case 460:
70b8e80941Smrg         return (const GLubyte *) "4.60";
71848b8605Smrg      default:
72848b8605Smrg         _mesa_problem(ctx,
73848b8605Smrg                       "Invalid GLSL version in shading_language_version()");
74848b8605Smrg         return (const GLubyte *) 0;
75848b8605Smrg      }
76848b8605Smrg      break;
77848b8605Smrg
78848b8605Smrg   case API_OPENGLES2:
79b8e80941Smrg      switch (ctx->Version) {
80b8e80941Smrg      case 20:
81b8e80941Smrg         return (const GLubyte *) "OpenGL ES GLSL ES 1.0.16";
82b8e80941Smrg      case 30:
83b8e80941Smrg         return (const GLubyte *) "OpenGL ES GLSL ES 3.00";
84b8e80941Smrg      case 31:
85b8e80941Smrg         return (const GLubyte *) "OpenGL ES GLSL ES 3.10";
86b8e80941Smrg      case 32:
87b8e80941Smrg         return (const GLubyte *) "OpenGL ES GLSL ES 3.20";
88b8e80941Smrg      default:
89b8e80941Smrg         _mesa_problem(ctx,
90b8e80941Smrg                       "Invalid OpenGL ES version in shading_language_version()");
91b8e80941Smrg         return (const GLubyte *) 0;
92b8e80941Smrg      }
93848b8605Smrg   case API_OPENGLES:
94848b8605Smrg      /* fall-through */
95848b8605Smrg
96848b8605Smrg   default:
97848b8605Smrg      _mesa_problem(ctx, "Unexpected API value in shading_language_version()");
98848b8605Smrg      return (const GLubyte *) 0;
99848b8605Smrg   }
100848b8605Smrg}
101848b8605Smrg
102848b8605Smrg
103848b8605Smrg/**
104848b8605Smrg * Query string-valued state.  The return value should _not_ be freed by
105848b8605Smrg * the caller.
106848b8605Smrg *
107848b8605Smrg * \param name  the state variable to query.
108848b8605Smrg *
109848b8605Smrg * \sa glGetString().
110848b8605Smrg *
111848b8605Smrg * Tries to get the string from dd_function_table::GetString, otherwise returns
112848b8605Smrg * the hardcoded strings.
113848b8605Smrg */
114848b8605Smrgconst GLubyte * GLAPIENTRY
115848b8605Smrg_mesa_GetString( GLenum name )
116848b8605Smrg{
117848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
118848b8605Smrg   static const char *vendor = "Brian Paul";
119848b8605Smrg   static const char *renderer = "Mesa";
120848b8605Smrg
121848b8605Smrg   if (!ctx)
122848b8605Smrg      return NULL;
123848b8605Smrg
124848b8605Smrg   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
125848b8605Smrg
126848b8605Smrg   /* this is a required driver function */
127848b8605Smrg   assert(ctx->Driver.GetString);
128848b8605Smrg   {
129848b8605Smrg      /* Give the driver the chance to handle this query */
130b8e80941Smrg      const GLubyte *str = ctx->Driver.GetString(ctx, name);
131848b8605Smrg      if (str)
132848b8605Smrg         return str;
133848b8605Smrg   }
134848b8605Smrg
135848b8605Smrg   switch (name) {
136848b8605Smrg      case GL_VENDOR:
137848b8605Smrg         return (const GLubyte *) vendor;
138848b8605Smrg      case GL_RENDERER:
139848b8605Smrg         return (const GLubyte *) renderer;
140848b8605Smrg      case GL_VERSION:
141848b8605Smrg         return (const GLubyte *) ctx->VersionString;
142848b8605Smrg      case GL_EXTENSIONS:
143848b8605Smrg         if (ctx->API == API_OPENGL_CORE) {
144848b8605Smrg            _mesa_error(ctx, GL_INVALID_ENUM, "glGetString(GL_EXTENSIONS)");
145848b8605Smrg            return (const GLubyte *) 0;
146848b8605Smrg         }
147b8e80941Smrg         if (!ctx->Extensions.String)
148b8e80941Smrg            ctx->Extensions.String = _mesa_make_extension_string(ctx);
149848b8605Smrg         return (const GLubyte *) ctx->Extensions.String;
150848b8605Smrg      case GL_SHADING_LANGUAGE_VERSION:
151848b8605Smrg         if (ctx->API == API_OPENGLES)
152848b8605Smrg            break;
153848b8605Smrg	 return shading_language_version(ctx);
154848b8605Smrg      case GL_PROGRAM_ERROR_STRING_ARB:
155848b8605Smrg         if (ctx->API == API_OPENGL_COMPAT &&
156848b8605Smrg             (ctx->Extensions.ARB_fragment_program ||
157848b8605Smrg              ctx->Extensions.ARB_vertex_program)) {
158848b8605Smrg            return (const GLubyte *) ctx->Program.ErrorString;
159848b8605Smrg         }
160848b8605Smrg         break;
161848b8605Smrg      default:
162848b8605Smrg         break;
163848b8605Smrg   }
164848b8605Smrg
165848b8605Smrg   _mesa_error( ctx, GL_INVALID_ENUM, "glGetString" );
166848b8605Smrg   return (const GLubyte *) 0;
167848b8605Smrg}
168848b8605Smrg
169848b8605Smrg
170848b8605Smrg/**
171848b8605Smrg * GL3
172848b8605Smrg */
173848b8605Smrgconst GLubyte * GLAPIENTRY
174848b8605Smrg_mesa_GetStringi(GLenum name, GLuint index)
175848b8605Smrg{
176848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
177848b8605Smrg
178848b8605Smrg   if (!ctx)
179848b8605Smrg      return NULL;
180848b8605Smrg
181848b8605Smrg   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
182848b8605Smrg
183848b8605Smrg   switch (name) {
184848b8605Smrg   case GL_EXTENSIONS:
185848b8605Smrg      if (index >= _mesa_get_extension_count(ctx)) {
186848b8605Smrg         _mesa_error(ctx, GL_INVALID_VALUE, "glGetStringi(index=%u)", index);
187848b8605Smrg         return (const GLubyte *) 0;
188848b8605Smrg      }
189848b8605Smrg      return _mesa_get_enabled_extension(ctx, index);
190b8e80941Smrg   case GL_SHADING_LANGUAGE_VERSION:
191b8e80941Smrg      {
192b8e80941Smrg         char *version;
193b8e80941Smrg         int num;
194b8e80941Smrg         if (!_mesa_is_desktop_gl(ctx) || ctx->Version < 43) {
195b8e80941Smrg            _mesa_error(ctx, GL_INVALID_ENUM,
196b8e80941Smrg                        "glGetStringi(GL_SHADING_LANGUAGE_VERSION): "
197b8e80941Smrg                        "supported only in GL4.3 and later");
198b8e80941Smrg            return (const GLubyte *) 0;
199b8e80941Smrg         }
200b8e80941Smrg         num = _mesa_get_shading_language_version(ctx, index, &version);
201b8e80941Smrg         if (index >= num) {
202b8e80941Smrg            _mesa_error(ctx, GL_INVALID_VALUE,
203b8e80941Smrg                        "glGetStringi(GL_SHADING_LANGUAGE_VERSION, index=%d)",
204b8e80941Smrg                        index);
205b8e80941Smrg            return (const GLubyte *) 0;
206b8e80941Smrg         }
207b8e80941Smrg         return (const GLubyte *) version;
208b8e80941Smrg      }
209848b8605Smrg   default:
210848b8605Smrg      _mesa_error(ctx, GL_INVALID_ENUM, "glGetStringi");
211848b8605Smrg      return (const GLubyte *) 0;
212848b8605Smrg   }
213848b8605Smrg}
214848b8605Smrg
215848b8605Smrg
216848b8605Smrg
217848b8605Smrg/**
218848b8605Smrg * Return pointer-valued state, such as a vertex array pointer.
219848b8605Smrg *
220848b8605Smrg * \param pname  names state to be queried
221848b8605Smrg * \param params  returns the pointer value
222848b8605Smrg *
223848b8605Smrg * \sa glGetPointerv().
224848b8605Smrg *
225848b8605Smrg * Tries to get the specified pointer via dd_function_table::GetPointerv,
226848b8605Smrg * otherwise gets the specified pointer from the current context.
227848b8605Smrg */
228848b8605Smrgvoid GLAPIENTRY
229848b8605Smrg_mesa_GetPointerv( GLenum pname, GLvoid **params )
230848b8605Smrg{
231848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
232848b8605Smrg   const GLuint clientUnit = ctx->Array.ActiveTexture;
233b8e80941Smrg   const char *callerstr;
234b8e80941Smrg
235b8e80941Smrg   if (_mesa_is_desktop_gl(ctx))
236b8e80941Smrg      callerstr = "glGetPointerv";
237b8e80941Smrg   else
238b8e80941Smrg      callerstr = "glGetPointervKHR";
239848b8605Smrg
240848b8605Smrg   if (!params)
241848b8605Smrg      return;
242848b8605Smrg
243848b8605Smrg   if (MESA_VERBOSE & VERBOSE_API)
244b8e80941Smrg      _mesa_debug(ctx, "%s %s\n", callerstr, _mesa_enum_to_string(pname));
245848b8605Smrg
246848b8605Smrg   switch (pname) {
247848b8605Smrg      case GL_VERTEX_ARRAY_POINTER:
248848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
249848b8605Smrg            goto invalid_pname;
250848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Ptr;
251848b8605Smrg         break;
252848b8605Smrg      case GL_NORMAL_ARRAY_POINTER:
253848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
254848b8605Smrg            goto invalid_pname;
255848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_NORMAL].Ptr;
256848b8605Smrg         break;
257848b8605Smrg      case GL_COLOR_ARRAY_POINTER:
258848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
259848b8605Smrg            goto invalid_pname;
260848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR0].Ptr;
261848b8605Smrg         break;
262848b8605Smrg      case GL_SECONDARY_COLOR_ARRAY_POINTER_EXT:
263848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
264848b8605Smrg            goto invalid_pname;
265848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR1].Ptr;
266848b8605Smrg         break;
267848b8605Smrg      case GL_FOG_COORDINATE_ARRAY_POINTER_EXT:
268848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
269848b8605Smrg            goto invalid_pname;
270848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_FOG].Ptr;
271848b8605Smrg         break;
272848b8605Smrg      case GL_INDEX_ARRAY_POINTER:
273848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
274848b8605Smrg            goto invalid_pname;
275848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Ptr;
276848b8605Smrg         break;
277848b8605Smrg      case GL_TEXTURE_COORD_ARRAY_POINTER:
278848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
279848b8605Smrg            goto invalid_pname;
280848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_TEX(clientUnit)].Ptr;
281848b8605Smrg         break;
282848b8605Smrg      case GL_EDGE_FLAG_ARRAY_POINTER:
283848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
284848b8605Smrg            goto invalid_pname;
285848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Ptr;
286848b8605Smrg         break;
287848b8605Smrg      case GL_FEEDBACK_BUFFER_POINTER:
288848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
289848b8605Smrg            goto invalid_pname;
290848b8605Smrg         *params = ctx->Feedback.Buffer;
291848b8605Smrg         break;
292848b8605Smrg      case GL_SELECTION_BUFFER_POINTER:
293848b8605Smrg         if (ctx->API != API_OPENGL_COMPAT)
294848b8605Smrg            goto invalid_pname;
295848b8605Smrg         *params = ctx->Select.Buffer;
296848b8605Smrg         break;
297848b8605Smrg      case GL_POINT_SIZE_ARRAY_POINTER_OES:
298848b8605Smrg         if (ctx->API != API_OPENGLES)
299848b8605Smrg            goto invalid_pname;
300848b8605Smrg         *params = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POINT_SIZE].Ptr;
301848b8605Smrg         break;
302848b8605Smrg      case GL_DEBUG_CALLBACK_FUNCTION_ARB:
303848b8605Smrg      case GL_DEBUG_CALLBACK_USER_PARAM_ARB:
304b8e80941Smrg         *params = _mesa_get_debug_state_ptr(ctx, pname);
305848b8605Smrg         break;
306848b8605Smrg      default:
307848b8605Smrg         goto invalid_pname;
308848b8605Smrg   }
309848b8605Smrg
310848b8605Smrg   return;
311848b8605Smrg
312848b8605Smrginvalid_pname:
313b8e80941Smrg   _mesa_error( ctx, GL_INVALID_ENUM, "%s", callerstr);
314848b8605Smrg   return;
315848b8605Smrg}
316848b8605Smrg
317848b8605Smrg
318848b8605Smrg/**
319848b8605Smrg * Returns the current GL error code, or GL_NO_ERROR.
320848b8605Smrg * \return current error code
321848b8605Smrg *
322848b8605Smrg * Returns __struct gl_contextRec::ErrorValue.
323848b8605Smrg */
324848b8605SmrgGLenum GLAPIENTRY
325848b8605Smrg_mesa_GetError( void )
326848b8605Smrg{
327848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
328848b8605Smrg   GLenum e = ctx->ErrorValue;
329848b8605Smrg   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
330848b8605Smrg
331b8e80941Smrg   /* From Issue (3) of the KHR_no_error spec:
332b8e80941Smrg    *
333b8e80941Smrg    *    "Should glGetError() always return NO_ERROR or have undefined
334b8e80941Smrg    *    results?
335848b8605Smrg    *
336b8e80941Smrg    *    RESOLVED: It should for all errors except OUT_OF_MEMORY."
337848b8605Smrg    */
338b8e80941Smrg   if (_mesa_is_no_error_enabled(ctx) && e != GL_OUT_OF_MEMORY) {
339b8e80941Smrg      e = GL_NO_ERROR;
340848b8605Smrg   }
341848b8605Smrg
342b8e80941Smrg   if (MESA_VERBOSE & VERBOSE_API)
343b8e80941Smrg      _mesa_debug(ctx, "glGetError <-- %s\n", _mesa_enum_to_string(e));
344848b8605Smrg
345b8e80941Smrg   ctx->ErrorValue = (GLenum) GL_NO_ERROR;
346b8e80941Smrg   ctx->ErrorDebugCount = 0;
347b8e80941Smrg   return e;
348848b8605Smrg}
349