e5c08a641467db3e461b4f89bf778587657fa370
[mesa.git] / src / mesa / main / querymatrix.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 **************************************************************************/
7
8
9 /**
10 * Code to implement GL_OES_query_matrix. See the spec at:
11 * http://www.khronos.org/registry/gles/extensions/OES/OES_query_matrix.txt
12 */
13
14
15 #include <stdlib.h>
16 #include <math.h>
17 #include "GLES/gl.h"
18 #include "GLES/glext.h"
19
20
21 /**
22 * This is from the GL_OES_query_matrix extension specification:
23 *
24 * GLbitfield glQueryMatrixxOES( GLfixed mantissa[16],
25 * GLint exponent[16] )
26 * mantissa[16] contains the contents of the current matrix in GLfixed
27 * format. exponent[16] contains the unbiased exponents applied to the
28 * matrix components, so that the internal representation of component i
29 * is close to mantissa[i] * 2^exponent[i]. The function returns a status
30 * word which is zero if all the components are valid. If
31 * status & (1<<i) != 0, the component i is invalid (e.g., NaN, Inf).
32 * The implementations are not required to keep track of overflows. In
33 * that case, the invalid bits are never set.
34 */
35
36 #define INT_TO_FIXED(x) ((GLfixed) ((x) << 16))
37 #define FLOAT_TO_FIXED(x) ((GLfixed) ((x) * 65536.0))
38
39 #if defined(WIN32) || defined(_WIN32_WCE)
40 /* Oddly, the fpclassify() function doesn't exist in such a form
41 * on Windows. This is an implementation using slightly different
42 * lower-level Windows functions.
43 */
44 #include <float.h>
45
46 enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
47 fpclassify(double x)
48 {
49 switch(_fpclass(x)) {
50 case _FPCLASS_SNAN: /* signaling NaN */
51 case _FPCLASS_QNAN: /* quiet NaN */
52 return FP_NAN;
53 case _FPCLASS_NINF: /* negative infinity */
54 case _FPCLASS_PINF: /* positive infinity */
55 return FP_INFINITE;
56 case _FPCLASS_NN: /* negative normal */
57 case _FPCLASS_PN: /* positive normal */
58 return FP_NORMAL;
59 case _FPCLASS_ND: /* negative denormalized */
60 case _FPCLASS_PD: /* positive denormalized */
61 return FP_SUBNORMAL;
62 case _FPCLASS_NZ: /* negative zero */
63 case _FPCLASS_PZ: /* positive zero */
64 return FP_ZERO;
65 default:
66 /* Should never get here; but if we do, this will guarantee
67 * that the pattern is not treated like a number.
68 */
69 return FP_NAN;
70 }
71 }
72
73 #elif defined(__APPLE__) || defined(__CYGWIN__)
74
75 /* fpclassify is available. */
76
77 #elif !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600
78
79 enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
80 fpclassify(double x)
81 {
82 /* XXX do something better someday */
83 return FP_NORMAL;
84 }
85
86 #endif
87
88 extern GLbitfield GL_APIENTRY _es_QueryMatrixxOES(GLfixed mantissa[16], GLint exponent[16]);
89
90 /* The Mesa functions we'll need */
91 extern void GL_APIENTRY _mesa_GetIntegerv(GLenum pname, GLint *params);
92 extern void GL_APIENTRY _mesa_GetFloatv(GLenum pname, GLfloat *params);
93
94 GLbitfield GL_APIENTRY _es_QueryMatrixxOES(GLfixed mantissa[16], GLint exponent[16])
95 {
96 GLfloat matrix[16];
97 GLint tmp;
98 GLenum currentMode = GL_FALSE;
99 GLenum desiredMatrix = GL_FALSE;
100 /* The bitfield returns 1 for each component that is invalid (i.e.
101 * NaN or Inf). In case of error, everything is invalid.
102 */
103 GLbitfield rv;
104 register unsigned int i;
105 unsigned int bit;
106
107 /* This data structure defines the mapping between the current matrix
108 * mode and the desired matrix identifier.
109 */
110 static struct {
111 GLenum currentMode;
112 GLenum desiredMatrix;
113 } modes[] = {
114 {GL_MODELVIEW, GL_MODELVIEW_MATRIX},
115 {GL_PROJECTION, GL_PROJECTION_MATRIX},
116 {GL_TEXTURE, GL_TEXTURE_MATRIX},
117 #if 0
118 /* this doesn't exist in GLES */
119 {GL_COLOR, GL_COLOR_MATRIX},
120 #endif
121 };
122
123 /* Call Mesa to get the current matrix in floating-point form. First,
124 * we have to figure out what the current matrix mode is.
125 */
126 _mesa_GetIntegerv(GL_MATRIX_MODE, &tmp);
127 currentMode = (GLenum) tmp;
128
129 /* The mode is either GL_FALSE, if for some reason we failed to query
130 * the mode, or a given mode from the above table. Search for the
131 * returned mode to get the desired matrix; if we don't find it,
132 * we can return immediately, as _mesa_GetInteger() will have
133 * logged the necessary error already.
134 */
135 for (i = 0; i < sizeof(modes)/sizeof(modes[0]); i++) {
136 if (modes[i].currentMode == currentMode) {
137 desiredMatrix = modes[i].desiredMatrix;
138 break;
139 }
140 }
141 if (desiredMatrix == GL_FALSE) {
142 /* Early error means all values are invalid. */
143 return 0xffff;
144 }
145
146 /* Now pull the matrix itself. */
147 _mesa_GetFloatv(desiredMatrix, matrix);
148
149 rv = 0;
150 for (i = 0, bit = 1; i < 16; i++, bit<<=1) {
151 float normalizedFraction;
152 int exp;
153
154 switch (fpclassify(matrix[i])) {
155 /* A "subnormal" or denormalized number is too small to be
156 * represented in normal format; but despite that it's a
157 * valid floating point number. FP_ZERO and FP_NORMAL
158 * are both valid as well. We should be fine treating
159 * these three cases as legitimate floating-point numbers.
160 */
161 case FP_SUBNORMAL:
162 case FP_NORMAL:
163 case FP_ZERO:
164 normalizedFraction = (GLfloat)frexp(matrix[i], &exp);
165 mantissa[i] = FLOAT_TO_FIXED(normalizedFraction);
166 exponent[i] = (GLint) exp;
167 break;
168
169 /* If the entry is not-a-number or an infinity, then the
170 * matrix component is invalid. The invalid flag for
171 * the component is already set; might as well set the
172 * other return values to known values. We'll set
173 * distinct values so that a savvy end user could determine
174 * whether the matrix component was a NaN or an infinity,
175 * but this is more useful for debugging than anything else
176 * since the standard doesn't specify any such magic
177 * values to return.
178 */
179 case FP_NAN:
180 mantissa[i] = INT_TO_FIXED(0);
181 exponent[i] = (GLint) 0;
182 rv |= bit;
183 break;
184
185 case FP_INFINITE:
186 /* Return +/- 1 based on whether it's a positive or
187 * negative infinity.
188 */
189 if (matrix[i] > 0) {
190 mantissa[i] = INT_TO_FIXED(1);
191 }
192 else {
193 mantissa[i] = -INT_TO_FIXED(1);
194 }
195 exponent[i] = (GLint) 0;
196 rv |= bit;
197 break;
198
199 /* We should never get here; but here's a catching case
200 * in case fpclassify() is returnings something unexpected.
201 */
202 default:
203 mantissa[i] = INT_TO_FIXED(2);
204 exponent[i] = (GLint) 0;
205 rv |= bit;
206 break;
207 }
208
209 } /* for each component */
210
211 /* All done */
212 return rv;
213 }