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