MATH_DEBUG changes from bug #4468.
[mesa.git] / src / mesa / math / m_debug_xform.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * Updated for P6 architecture by Gareth Hughes.
27 */
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "imports.h"
33
34 #include "m_matrix.h"
35 #include "m_xform.h"
36
37 #include "m_debug.h"
38 #include "m_debug_util.h"
39
40 #ifdef __UNIXOS2__
41 /* The linker doesn't like empty files */
42 static char dummy;
43 #endif
44
45 #ifdef DEBUG_MATH /* This code only used for debugging */
46
47
48 /* Overhead of profiling counter in cycles. Automatically adjusted to
49 * your machine at run time - counter initialization should give very
50 * consistent results.
51 */
52 long counter_overhead = 0;
53
54 /* This is the value of the environment variable MESA_PROFILE, and is
55 * used to determine if we should benchmark the functions as well as
56 * verify their correctness.
57 */
58 char *mesa_profile = NULL;
59
60
61 static int m_general[16] = {
62 VAR, VAR, VAR, VAR,
63 VAR, VAR, VAR, VAR,
64 VAR, VAR, VAR, VAR,
65 VAR, VAR, VAR, VAR
66 };
67 static int m_identity[16] = {
68 ONE, NIL, NIL, NIL,
69 NIL, ONE, NIL, NIL,
70 NIL, NIL, ONE, NIL,
71 NIL, NIL, NIL, ONE
72 };
73 static int m_2d[16] = {
74 VAR, VAR, NIL, VAR,
75 VAR, VAR, NIL, VAR,
76 NIL, NIL, ONE, NIL,
77 NIL, NIL, NIL, ONE
78 };
79 static int m_2d_no_rot[16] = {
80 VAR, NIL, NIL, VAR,
81 NIL, VAR, NIL, VAR,
82 NIL, NIL, ONE, NIL,
83 NIL, NIL, NIL, ONE
84 };
85 static int m_3d[16] = {
86 VAR, VAR, VAR, VAR,
87 VAR, VAR, VAR, VAR,
88 VAR, VAR, VAR, VAR,
89 NIL, NIL, NIL, ONE
90 };
91 static int m_3d_no_rot[16] = {
92 VAR, NIL, NIL, VAR,
93 NIL, VAR, NIL, VAR,
94 NIL, NIL, VAR, VAR,
95 NIL, NIL, NIL, ONE
96 };
97 static int m_perspective[16] = {
98 VAR, NIL, VAR, NIL,
99 NIL, VAR, VAR, NIL,
100 NIL, NIL, VAR, VAR,
101 NIL, NIL, NEG, NIL
102 };
103 static int *templates[7] = {
104 m_general,
105 m_identity,
106 m_3d_no_rot,
107 m_perspective,
108 m_2d,
109 m_2d_no_rot,
110 m_3d
111 };
112 static enum GLmatrixtype mtypes[7] = {
113 MATRIX_GENERAL,
114 MATRIX_IDENTITY,
115 MATRIX_3D_NO_ROT,
116 MATRIX_PERSPECTIVE,
117 MATRIX_2D,
118 MATRIX_2D_NO_ROT,
119 MATRIX_3D
120 };
121 static char *mstrings[7] = {
122 "MATRIX_GENERAL",
123 "MATRIX_IDENTITY",
124 "MATRIX_3D_NO_ROT",
125 "MATRIX_PERSPECTIVE",
126 "MATRIX_2D",
127 "MATRIX_2D_NO_ROT",
128 "MATRIX_3D"
129 };
130
131
132 /* =============================================================
133 * Reference transformations
134 */
135
136 static void ref_transform( GLvector4f *dst,
137 const GLmatrix *mat,
138 const GLvector4f *src )
139 {
140 GLuint i;
141 GLfloat *s = (GLfloat *)src->start;
142 GLfloat (*d)[4] = (GLfloat (*)[4])dst->start;
143 const GLfloat *m = mat->m;
144
145 for ( i = 0 ; i < src->count ; i++ ) {
146 TRANSFORM_POINT( d[i], m, s );
147 s = (GLfloat *)((char *)s + src->stride);
148 }
149 }
150
151
152 /* =============================================================
153 * Vertex transformation tests
154 */
155
156 static void init_matrix( GLfloat *m )
157 {
158 m[0] = 63.0; m[4] = 43.0; m[ 8] = 29.0; m[12] = 43.0;
159 m[1] = 55.0; m[5] = 17.0; m[ 9] = 31.0; m[13] = 7.0;
160 m[2] = 44.0; m[6] = 9.0; m[10] = 7.0; m[14] = 3.0;
161 m[3] = 11.0; m[7] = 23.0; m[11] = 91.0; m[15] = 9.0;
162 }
163
164 ALIGN16(static GLfloat, s[TEST_COUNT][4]);
165 ALIGN16(static GLfloat, d[TEST_COUNT][4]);
166 ALIGN16(static GLfloat, r[TEST_COUNT][4]);
167
168 static int test_transform_function( transform_func func, int psize,
169 int mtype, unsigned long *cycles )
170 {
171 GLvector4f source[1], dest[1], ref[1];
172 GLmatrix mat[1];
173 GLfloat *m;
174 int i, j;
175 #ifdef RUN_DEBUG_BENCHMARK
176 int cycle_i; /* the counter for the benchmarks we run */
177 #endif
178
179 (void) cycles;
180
181 if ( psize > 4 ) {
182 _mesa_problem( NULL, "test_transform_function called with psize > 4\n" );
183 return 0;
184 }
185
186 mat->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
187 mat->type = mtypes[mtype];
188
189 m = mat->m;
190 ASSERT( ((long)m & 15) == 0 );
191
192 init_matrix( m );
193
194 for ( i = 0 ; i < 4 ; i++ ) {
195 for ( j = 0 ; j < 4 ; j++ ) {
196 switch ( templates[mtype][i * 4 + j] ) {
197 case NIL:
198 m[j * 4 + i] = 0.0;
199 break;
200 case ONE:
201 m[j * 4 + i] = 1.0;
202 break;
203 case NEG:
204 m[j * 4 + i] = -1.0;
205 break;
206 case VAR:
207 break;
208 default:
209 abort();
210 }
211 }
212 }
213
214 for ( i = 0 ; i < TEST_COUNT ; i++) {
215 ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
216 ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
217 for ( j = 0 ; j < psize ; j++ )
218 s[i][j] = rnd();
219 }
220
221 source->data = (GLfloat(*)[4])s;
222 source->start = (GLfloat *)s;
223 source->count = TEST_COUNT;
224 source->stride = sizeof(s[0]);
225 source->size = 4;
226 source->flags = 0;
227
228 dest->data = (GLfloat(*)[4])d;
229 dest->start = (GLfloat *)d;
230 dest->count = TEST_COUNT;
231 dest->stride = sizeof(float[4]);
232 dest->size = 0;
233 dest->flags = 0;
234
235 ref->data = (GLfloat(*)[4])r;
236 ref->start = (GLfloat *)r;
237 ref->count = TEST_COUNT;
238 ref->stride = sizeof(float[4]);
239 ref->size = 0;
240 ref->flags = 0;
241
242 ref_transform( ref, mat, source );
243
244 if ( mesa_profile ) {
245 BEGIN_RACE( *cycles );
246 func( dest, mat->m, source );
247 END_RACE( *cycles );
248 }
249 else {
250 func( dest, mat->m, source );
251 }
252
253 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
254 for ( j = 0 ; j < 4 ; j++ ) {
255 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
256 _mesa_printf("-----------------------------\n" );
257 _mesa_printf("(i = %i, j = %i)\n", i, j );
258 _mesa_printf("%f \t %f \t [diff = %e - %i bit missed]\n",
259 d[i][0], r[i][0], r[i][0]-d[i][0],
260 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
261 _mesa_printf("%f \t %f \t [diff = %e - %i bit missed]\n",
262 d[i][1], r[i][1], r[i][1]-d[i][1],
263 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
264 _mesa_printf("%f \t %f \t [diff = %e - %i bit missed]\n",
265 d[i][2], r[i][2], r[i][2]-d[i][2],
266 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
267 _mesa_printf("%f \t %f \t [diff = %e - %i bit missed]\n",
268 d[i][3], r[i][3], r[i][3]-d[i][3],
269 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
270 return 0;
271 }
272 }
273 }
274
275 ALIGN_FREE( mat->m );
276 return 1;
277 }
278
279 void _math_test_all_transform_functions( char *description )
280 {
281 int psize, mtype;
282 unsigned long benchmark_tab[4][7];
283 static int first_time = 1;
284
285 if ( first_time ) {
286 first_time = 0;
287 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
288 }
289
290 #ifdef RUN_DEBUG_BENCHMARK
291 if ( mesa_profile ) {
292 if ( !counter_overhead ) {
293 INIT_COUNTER();
294 _mesa_printf("counter overhead: %lu cycles\n\n", counter_overhead );
295 }
296 _mesa_printf("transform results after hooking in %s functions:\n", description );
297 }
298 #endif
299
300 #ifdef RUN_DEBUG_BENCHMARK
301 if ( mesa_profile ) {
302 _mesa_printf("\n" );
303 for ( psize = 1 ; psize <= 4 ; psize++ ) {
304 _mesa_printf(" p%d\t", psize );
305 }
306 _mesa_printf("\n--------------------------------------------------------\n" );
307 }
308 #endif
309
310 for ( mtype = 0 ; mtype < 7 ; mtype++ ) {
311 for ( psize = 1 ; psize <= 4 ; psize++ ) {
312 transform_func func = _mesa_transform_tab[psize][mtypes[mtype]];
313 unsigned long *cycles = &(benchmark_tab[psize-1][mtype]);
314
315 if ( test_transform_function( func, psize, mtype, cycles ) == 0 ) {
316 char buf[100];
317 _mesa_sprintf(buf, "_mesa_transform_tab[0][%d][%s] failed test (%s)",
318 psize, mstrings[mtype], description );
319 _mesa_problem( NULL, buf );
320 }
321 #ifdef RUN_DEBUG_BENCHMARK
322 if ( mesa_profile )
323 _mesa_printf(" %li\t", benchmark_tab[psize-1][mtype] );
324 #endif
325 }
326 #ifdef RUN_DEBUG_BENCHMARK
327 if ( mesa_profile )
328 _mesa_printf(" | [%s]\n", mstrings[mtype] );
329 #endif
330 }
331 #ifdef RUN_DEBUG_BENCHMARK
332 if ( mesa_profile )
333 _mesa_printf( "\n" );
334 #endif
335 }
336
337
338 #endif /* DEBUG_MATH */