1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 **************************************************************************/
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
18 #include "querymatrix.h"
23 * This is from the GL_OES_query_matrix extension specification:
25 * GLbitfield glQueryMatrixxOES( GLfixed mantissa[16],
26 * GLint exponent[16] )
27 * mantissa[16] contains the contents of the current matrix in GLfixed
28 * format. exponent[16] contains the unbiased exponents applied to the
29 * matrix components, so that the internal representation of component i
30 * is close to mantissa[i] * 2^exponent[i]. The function returns a status
31 * word which is zero if all the components are valid. If
32 * status & (1<<i) != 0, the component i is invalid (e.g., NaN, Inf).
33 * The implementations are not required to keep track of overflows. In
34 * that case, the invalid bits are never set.
37 #define INT_TO_FIXED(x) ((GLfixed) ((x) << 16))
38 #define FLOAT_TO_FIXED(x) ((GLfixed) ((x) * 65536.0))
42 _mesa_QueryMatrixxOES(GLfixed mantissa
[16], GLint exponent
[16])
46 GLenum currentMode
= GL_FALSE
;
47 GLenum desiredMatrix
= GL_FALSE
;
48 /* The bitfield returns 1 for each component that is invalid (i.e.
49 * NaN or Inf). In case of error, everything is invalid.
54 /* This data structure defines the mapping between the current matrix
55 * mode and the desired matrix identifier.
61 {GL_MODELVIEW
, GL_MODELVIEW_MATRIX
},
62 {GL_PROJECTION
, GL_PROJECTION_MATRIX
},
63 {GL_TEXTURE
, GL_TEXTURE_MATRIX
},
66 /* Call Mesa to get the current matrix in floating-point form. First,
67 * we have to figure out what the current matrix mode is.
69 _mesa_GetIntegerv(GL_MATRIX_MODE
, &tmp
);
70 currentMode
= (GLenum
) tmp
;
72 /* The mode is either GL_FALSE, if for some reason we failed to query
73 * the mode, or a given mode from the above table. Search for the
74 * returned mode to get the desired matrix; if we don't find it,
75 * we can return immediately, as _mesa_GetInteger() will have
76 * logged the necessary error already.
78 for (i
= 0; i
< ARRAY_SIZE(modes
); i
++) {
79 if (modes
[i
].currentMode
== currentMode
) {
80 desiredMatrix
= modes
[i
].desiredMatrix
;
84 if (desiredMatrix
== GL_FALSE
) {
85 /* Early error means all values are invalid. */
89 /* Now pull the matrix itself. */
90 _mesa_GetFloatv(desiredMatrix
, matrix
);
93 for (i
= 0, bit
= 1; i
< 16; i
++, bit
<<=1) {
94 float normalizedFraction
;
97 switch (fpclassify(matrix
[i
])) {
101 /* A "subnormal" or denormalized number is too small to be
102 * represented in normal format; but despite that it's a
103 * valid floating point number. FP_ZERO and FP_NORMAL
104 * are both valid as well. We should be fine treating
105 * these three cases as legitimate floating-point numbers.
107 normalizedFraction
= (GLfloat
)frexp(matrix
[i
], &exp
);
108 mantissa
[i
] = FLOAT_TO_FIXED(normalizedFraction
);
109 exponent
[i
] = (GLint
) exp
;
113 /* If the entry is not-a-number or an infinity, then the
114 * matrix component is invalid. The invalid flag for
115 * the component is already set; might as well set the
116 * other return values to known values. We'll set
117 * distinct values so that a savvy end user could determine
118 * whether the matrix component was a NaN or an infinity,
119 * but this is more useful for debugging than anything else
120 * since the standard doesn't specify any such magic
123 mantissa
[i
] = INT_TO_FIXED(0);
124 exponent
[i
] = (GLint
) 0;
129 /* Return +/- 1 based on whether it's a positive or
133 mantissa
[i
] = INT_TO_FIXED(1);
136 mantissa
[i
] = -INT_TO_FIXED(1);
138 exponent
[i
] = (GLint
) 0;
143 /* We should never get here; but here's a catching case
144 * in case fpclassify() is returnings something unexpected.
146 mantissa
[i
] = INT_TO_FIXED(2);
147 exponent
[i
] = (GLint
) 0;
152 } /* for each component */