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