1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * Updated for P6 architecture by Gareth Hughes.
27 */
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/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
ref_transform(GLvector4f * dst,const GLmatrix * mat,const GLvector4f * src)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
init_matrix(GLfloat * m)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
test_transform_function(transform_func func,int psize,int mtype,unsigned long * cycles)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 = _mesa_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 assert(0);
210 return 0;
211 }
212 }
213 }
214
215 for ( i = 0 ; i < TEST_COUNT ; i++) {
216 ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
217 ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
218 for ( j = 0 ; j < psize ; j++ )
219 s[i][j] = rnd();
220 }
221
222 source->data = (GLfloat(*)[4])s;
223 source->start = (GLfloat *)s;
224 source->count = TEST_COUNT;
225 source->stride = sizeof(s[0]);
226 source->size = 4;
227 source->flags = 0;
228
229 dest->data = (GLfloat(*)[4])d;
230 dest->start = (GLfloat *)d;
231 dest->count = TEST_COUNT;
232 dest->stride = sizeof(float[4]);
233 dest->size = 0;
234 dest->flags = 0;
235
236 ref->data = (GLfloat(*)[4])r;
237 ref->start = (GLfloat *)r;
238 ref->count = TEST_COUNT;
239 ref->stride = sizeof(float[4]);
240 ref->size = 0;
241 ref->flags = 0;
242
243 ref_transform( ref, mat, source );
244
245 if ( mesa_profile ) {
246 BEGIN_RACE( *cycles );
247 func( dest, mat->m, source );
248 END_RACE( *cycles );
249 }
250 else {
251 func( dest, mat->m, source );
252 }
253
254 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
255 for ( j = 0 ; j < 4 ; j++ ) {
256 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
257 printf("-----------------------------\n" );
258 printf("(i = %i, j = %i)\n", i, j );
259 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
260 d[i][0], r[i][0], r[i][0]-d[i][0],
261 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
262 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
263 d[i][1], r[i][1], r[i][1]-d[i][1],
264 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
265 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
266 d[i][2], r[i][2], r[i][2]-d[i][2],
267 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
268 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
269 d[i][3], r[i][3], r[i][3]-d[i][3],
270 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
271 return 0;
272 }
273 }
274 }
275
276 _mesa_align_free( mat->m );
277 return 1;
278 }
279
_math_test_all_transform_functions(char * description)280 void _math_test_all_transform_functions( char *description )
281 {
282 int psize, mtype;
283 unsigned long benchmark_tab[4][7];
284 static int first_time = 1;
285
286 if ( first_time ) {
287 first_time = 0;
288 mesa_profile = getenv( "MESA_PROFILE" );
289 }
290
291 #ifdef RUN_DEBUG_BENCHMARK
292 if ( mesa_profile ) {
293 if ( !counter_overhead ) {
294 INIT_COUNTER();
295 printf("counter overhead: %lu cycles\n\n", counter_overhead );
296 }
297 printf("transform results after hooking in %s functions:\n", description );
298 }
299 #endif
300
301 #ifdef RUN_DEBUG_BENCHMARK
302 if ( mesa_profile ) {
303 printf("\n" );
304 for ( psize = 1 ; psize <= 4 ; psize++ ) {
305 printf(" p%d\t", psize );
306 }
307 printf("\n--------------------------------------------------------\n" );
308 }
309 #endif
310
311 for ( mtype = 0 ; mtype < 7 ; mtype++ ) {
312 for ( psize = 1 ; psize <= 4 ; psize++ ) {
313 transform_func func = _mesa_transform_tab[psize][mtypes[mtype]];
314 unsigned long *cycles = &(benchmark_tab[psize-1][mtype]);
315
316 if ( test_transform_function( func, psize, mtype, cycles ) == 0 ) {
317 char buf[100];
318 sprintf(buf, "_mesa_transform_tab[0][%d][%s] failed test (%s)",
319 psize, mstrings[mtype], description );
320 _mesa_problem( NULL, "%s", buf );
321 }
322 #ifdef RUN_DEBUG_BENCHMARK
323 if ( mesa_profile )
324 printf(" %li\t", benchmark_tab[psize-1][mtype] );
325 #endif
326 }
327 #ifdef RUN_DEBUG_BENCHMARK
328 if ( mesa_profile )
329 printf(" | [%s]\n", mstrings[mtype] );
330 #endif
331 }
332 #ifdef RUN_DEBUG_BENCHMARK
333 if ( mesa_profile )
334 printf( "\n" );
335 #endif
336 }
337
338
339 #endif /* DEBUG_MATH */
340