Move the transform and lighting code to two new directories
[mesa.git] / src / mesa / math / m_debug_xform.c
1 /* $Id: m_debug_xform.c,v 1.1 2000/11/16 21:05:41 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /*
28 * Updated for P6 architecture by Gareth Hughes.
29 */
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "mem.h"
34
35 #include "m_debug_xform.h"
36 #include "m_matrix.h"
37 #include "m_xform.h"
38
39
40 #ifdef DEBUG /* This code only used for debugging */
41
42
43 /* Comment this out to deactivate the cycle counter.
44 * NOTE: it works only on CPUs which know the 'rdtsc' command (586 or higher)
45 * (hope, you don't try to debug Mesa on a 386 ;)
46 */
47 #if defined(__GNUC__) && defined(__i386__) && defined(USE_X86_ASM)
48 #define RUN_XFORM_BENCHMARK
49 #endif
50
51 #define TEST_COUNT 128 /* size of the tested vector array */
52
53 #define REQUIRED_PRECISION 10 /* allow 4 bits to miss */
54 #define MAX_PRECISION 24 /* max. precision possible */
55
56
57 #ifdef RUN_XFORM_BENCHMARK
58 /* Overhead of profiling counter in cycles. Automatically adjusted to
59 * your machine at run time - counter initialization should give very
60 * consistent results.
61 */
62 static int need_counter = 1;
63 static long counter_overhead = 0;
64
65 /* Modify the the number of tests if you like.
66 * We take the minimum of all results, because every error should be
67 * positive (time used by other processes, task switches etc).
68 * It is assumed that all calculations are done in the cache.
69 */
70
71 #if 1 /* PPro, PII, PIII version */
72
73 /* Profiling on the P6 architecture requires a little more work, due to
74 * the internal out-of-order execution. We must perform a serializing
75 * 'cpuid' instruction before and after the 'rdtsc' instructions to make
76 * sure no other uops are executed when we sample the timestamp counter.
77 */
78 #define INIT_COUNTER() \
79 do { \
80 int cycle_i; \
81 counter_overhead = LONG_MAX; \
82 for ( cycle_i = 0 ; cycle_i < 4 ; cycle_i++ ) { \
83 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
84 __asm__ ( "push %%ebx \n" \
85 "xor %%eax, %%eax \n" \
86 "cpuid \n" \
87 "rdtsc \n" \
88 "mov %%eax, %0 \n" \
89 "xor %%eax, %%eax \n" \
90 "cpuid \n" \
91 "pop %%ebx \n" \
92 "push %%ebx \n" \
93 "xor %%eax, %%eax \n" \
94 "cpuid \n" \
95 "rdtsc \n" \
96 "mov %%eax, %1 \n" \
97 "xor %%eax, %%eax \n" \
98 "cpuid \n" \
99 "pop %%ebx \n" \
100 : "=m" (cycle_tmp1), "=m" (cycle_tmp2) \
101 : : "eax", "ecx", "edx" ); \
102 if ( counter_overhead > (cycle_tmp2 - cycle_tmp1) ) { \
103 counter_overhead = cycle_tmp2 - cycle_tmp1; \
104 } \
105 } \
106 } while (0)
107
108 #define BEGIN_RACE(x) \
109 x = LONG_MAX; \
110 for ( cycle_i = 0 ; cycle_i < 10 ; cycle_i++ ) { \
111 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
112 __asm__ ( "push %%ebx \n" \
113 "xor %%eax, %%eax \n" \
114 "cpuid \n" \
115 "rdtsc \n" \
116 "mov %%eax, %0 \n" \
117 "xor %%eax, %%eax \n" \
118 "cpuid \n" \
119 "pop %%ebx \n" \
120 : "=m" (cycle_tmp1) \
121 : : "eax", "ecx", "edx" );
122
123 #define END_RACE(x) \
124 __asm__ ( "push %%ebx \n" \
125 "xor %%eax, %%eax \n" \
126 "cpuid \n" \
127 "rdtsc \n" \
128 "mov %%eax, %0 \n" \
129 "xor %%eax, %%eax \n" \
130 "cpuid \n" \
131 "pop %%ebx \n" \
132 : "=m" (cycle_tmp2) \
133 : : "eax", "ecx", "edx" ); \
134 if ( x > (cycle_tmp2 - cycle_tmp1) ) { \
135 x = cycle_tmp2 - cycle_tmp1; \
136 } \
137 } \
138 x -= counter_overhead;
139
140 #else /* PPlain, PMMX version */
141
142 /* To ensure accurate results, we stall the pipelines with the
143 * non-pairable 'cdq' instruction. This ensures all the code being
144 * profiled is complete when the 'rdtsc' instruction executes.
145 */
146 #define INIT_COUNTER(x) \
147 do { \
148 int cycle_i; \
149 x = LONG_MAX; \
150 for ( cycle_i = 0 ; cycle_i < 32 ; cycle_i++ ) { \
151 long cycle_tmp1, cycle_tmp2, dummy; \
152 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
153 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
154 __asm__ ( "cdq" ); \
155 __asm__ ( "cdq" ); \
156 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) ); \
157 __asm__ ( "cdq" ); \
158 __asm__ ( "cdq" ); \
159 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
160 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
161 x = cycle_tmp2 - cycle_tmp1; \
162 } \
163 } while (0)
164
165 #define BEGIN_RACE(x) \
166 x = LONG_MAX; \
167 for ( cycle_i = 0 ; cycle_i < 16 ; cycle_i++ ) { \
168 long cycle_tmp1, cycle_tmp2, dummy; \
169 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
170 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
171 __asm__ ( "cdq" ); \
172 __asm__ ( "cdq" ); \
173 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) );
174
175
176 #define END_RACE(x) \
177 __asm__ ( "cdq" ); \
178 __asm__ ( "cdq" ); \
179 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
180 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
181 x = cycle_tmp2 - cycle_tmp1; \
182 } \
183 x -= counter_overhead;
184
185 #endif
186
187 #else
188
189 #define BEGIN_RACE(x)
190 #define END_RACE(x)
191
192 #endif
193
194
195 static char *mesa_profile = NULL;
196
197
198 enum { NIL=0, ONE=1, NEG=-1, VAR=2 };
199
200 static int m_general[16] = {
201 VAR, VAR, VAR, VAR,
202 VAR, VAR, VAR, VAR,
203 VAR, VAR, VAR, VAR,
204 VAR, VAR, VAR, VAR
205 };
206 static int m_identity[16] = {
207 ONE, NIL, NIL, NIL,
208 NIL, ONE, NIL, NIL,
209 NIL, NIL, ONE, NIL,
210 NIL, NIL, NIL, ONE
211 };
212 static int m_2d[16] = {
213 VAR, VAR, NIL, VAR,
214 VAR, VAR, NIL, VAR,
215 NIL, NIL, ONE, NIL,
216 NIL, NIL, NIL, ONE
217 };
218 static int m_2d_no_rot[16] = {
219 VAR, NIL, NIL, VAR,
220 NIL, VAR, NIL, VAR,
221 NIL, NIL, ONE, NIL,
222 NIL, NIL, NIL, ONE
223 };
224 static int m_3d[16] = {
225 VAR, VAR, VAR, VAR,
226 VAR, VAR, VAR, VAR,
227 VAR, VAR, VAR, VAR,
228 NIL, NIL, NIL, ONE
229 };
230 static int m_3d_no_rot[16] = {
231 VAR, NIL, NIL, VAR,
232 NIL, VAR, NIL, VAR,
233 NIL, NIL, VAR, VAR,
234 NIL, NIL, NIL, ONE
235 };
236 static int m_perspective[16] = {
237 VAR, NIL, VAR, NIL,
238 NIL, VAR, VAR, NIL,
239 NIL, NIL, VAR, VAR,
240 NIL, NIL, NEG, NIL
241 };
242 static int *templates[7] = {
243 m_general,
244 m_identity,
245 m_3d_no_rot,
246 m_perspective,
247 m_2d,
248 m_2d_no_rot,
249 m_3d
250 };
251 static int mtypes[7] = {
252 MATRIX_GENERAL,
253 MATRIX_IDENTITY,
254 MATRIX_3D_NO_ROT,
255 MATRIX_PERSPECTIVE,
256 MATRIX_2D,
257 MATRIX_2D_NO_ROT,
258 MATRIX_3D
259 };
260 static char *mstrings[7] = {
261 "MATRIX_GENERAL",
262 "MATRIX_IDENTITY",
263 "MATRIX_3D_NO_ROT",
264 "MATRIX_PERSPECTIVE",
265 "MATRIX_2D",
266 "MATRIX_2D_NO_ROT",
267 "MATRIX_3D"
268 };
269
270
271
272 static int m_norm_identity[16] = {
273 ONE, NIL, NIL, NIL,
274 NIL, ONE, NIL, NIL,
275 NIL, NIL, ONE, NIL,
276 NIL, NIL, NIL, NIL
277 };
278 static int m_norm_general[16] = {
279 VAR, VAR, VAR, NIL,
280 VAR, VAR, VAR, NIL,
281 VAR, VAR, VAR, NIL,
282 NIL, NIL, NIL, NIL
283 };
284 static int m_norm_no_rot[16] = {
285 VAR, NIL, NIL, NIL,
286 NIL, VAR, NIL, NIL,
287 NIL, NIL, VAR, NIL,
288 NIL, NIL, NIL, NIL
289 };
290 static int *norm_templates[8] = {
291 m_norm_no_rot,
292 m_norm_no_rot,
293 m_norm_no_rot,
294 m_norm_general,
295 m_norm_general,
296 m_norm_general,
297 m_norm_identity,
298 m_norm_identity
299 };
300 static int norm_types[8] = {
301 NORM_TRANSFORM_NO_ROT,
302 NORM_TRANSFORM_NO_ROT | NORM_RESCALE,
303 NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE,
304 NORM_TRANSFORM,
305 NORM_TRANSFORM | NORM_RESCALE,
306 NORM_TRANSFORM | NORM_NORMALIZE,
307 NORM_RESCALE,
308 NORM_NORMALIZE
309 };
310 static int norm_scale_types[8] = { /* rescale factor */
311 NIL, /* NIL disables rescaling */
312 VAR,
313 NIL,
314 NIL,
315 VAR,
316 NIL,
317 VAR,
318 NIL
319 };
320 static int norm_normalize_types[8] = { /* normalizing ?? (no = 0) */
321 0,
322 0,
323 1,
324 0,
325 0,
326 1,
327 0,
328 1
329 };
330 static char *norm_strings[8] = {
331 "NORM_TRANSFORM_NO_ROT",
332 "NORM_TRANSFORM_NO_ROT | NORM_RESCALE",
333 "NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE",
334 "NORM_TRANSFORM",
335 "NORM_TRANSFORM | NORM_RESCALE",
336 "NORM_TRANSFORM | NORM_NORMALIZE",
337 "NORM_RESCALE",
338 "NORM_NORMALIZE"
339 };
340
341
342
343 /* ================================================================
344 * Helper functions
345 */
346
347 static GLfloat rnd( void )
348 {
349 GLfloat f = (GLfloat)rand() / (GLfloat)RAND_MAX;
350 GLfloat gran = (GLfloat)(1 << 13);
351
352 f = (GLfloat)(GLint)(f * gran) / gran;
353
354 return f * 2.0 - 1.0;
355 }
356
357 static int significand_match( GLfloat a, GLfloat b )
358 {
359 GLfloat d = a - b;
360 int a_ex, b_ex, d_ex;
361
362 if ( d == 0.0F ) {
363 return MAX_PRECISION; /* Exact match */
364 }
365
366 if ( a == 0.0F || b == 0.0F ) {
367 /* It would probably be better to check if the
368 * non-zero number is denormalized and return
369 * the index of the highest set bit here.
370 */
371 return 0;
372 }
373
374 frexp( a, &a_ex );
375 frexp( b, &b_ex );
376 frexp( d, &d_ex );
377
378 if ( a_ex < b_ex )
379 return a_ex - d_ex;
380 else
381 return b_ex - d_ex;
382 }
383
384
385
386 /* ================================================================
387 * Reference transformations
388 */
389
390 static void ref_transform( GLvector4f *dst,
391 const GLmatrix *mat,
392 const GLvector4f *src,
393 const GLubyte *clipmask,
394 const GLubyte flag )
395 {
396 int i;
397 GLfloat *s = (GLfloat *)src->start;
398 GLfloat (*d)[4] = (GLfloat (*)[4])dst->start;
399 const GLfloat *m = mat->m;
400
401 (void) clipmask;
402 (void) flag;
403
404 for ( i = 0 ; i < src->count ; i++ ) {
405 GLfloat x = s[0], y = s[1], z = s[2], w = s[3];
406 d[i][0] = m[0]*x + m[4]*y + m[ 8]*z + m[12]*w;
407 d[i][1] = m[1]*x + m[5]*y + m[ 9]*z + m[13]*w;
408 d[i][2] = m[2]*x + m[6]*y + m[10]*z + m[14]*w;
409 d[i][3] = m[3]*x + m[7]*y + m[11]*z + m[15]*w;
410 s = (GLfloat *)((char *)s + src->stride);
411 }
412 }
413
414 static void ref_norm_transform_rescale( const GLmatrix *mat,
415 GLfloat scale,
416 const GLvector3f *in,
417 const GLfloat *lengths,
418 const GLubyte mask[],
419 GLvector3f *dest )
420 {
421 int i;
422 const GLfloat *s = in->start;
423 const GLfloat *m = mat->inv;
424 GLfloat (*out)[3] = (GLfloat (*)[3])dest->start;
425
426 (void) mask;
427 (void) lengths;
428
429 for ( i = 0 ; i < in->count ; i++ ) {
430 GLfloat x = s[0], y = s[1], z = s[2] ;
431 GLfloat tx = m[0]*x + m[1]*y + m[ 2]*z ;
432 GLfloat ty = m[4]*x + m[5]*y + m[ 6]*z ;
433 GLfloat tz = m[8]*x + m[9]*y + m[10]*z ;
434
435 out[i][0] = tx * scale;
436 out[i][1] = ty * scale;
437 out[i][2] = tz * scale;
438
439 s = (GLfloat *)((char *)s + in->stride);
440 }
441 }
442
443 static void ref_norm_transform_normalize( const GLmatrix *mat,
444 GLfloat scale,
445 const GLvector3f *in,
446 const GLfloat *lengths,
447 const GLubyte mask[],
448 GLvector3f *dest )
449 {
450 int i;
451 const GLfloat *s = in->start;
452 const GLfloat *m = mat->inv;
453 GLfloat (*out)[3] = (GLfloat (*)[3])dest->start;
454
455 (void) mask;
456
457 for ( i = 0 ; i < in->count ; i++ ) {
458 GLfloat x = s[0], y = s[1], z = s[2] ;
459 GLfloat tx = m[0]*x + m[1]*y + m[ 2]*z ;
460 GLfloat ty = m[4]*x + m[5]*y + m[ 6]*z ;
461 GLfloat tz = m[8]*x + m[9]*y + m[10]*z ;
462
463 if ( !lengths ) {
464 GLfloat len = tx*tx + ty*ty + tz*tz;
465 if ( len > 1e-20 ) {
466 /* Hmmm, don't know how we could test the precalculated
467 * length case...
468 */
469 scale = 1.0 / sqrt( len );
470 out[i][0] = tx * scale;
471 out[i][1] = ty * scale;
472 out[i][2] = tz * scale;
473 } else {
474 out[i][0] = out[i][1] = out[i][2] = 0;
475 }
476 } else {
477 scale = lengths[i];;
478 out[i][0] = tx * scale;
479 out[i][1] = ty * scale;
480 out[i][2] = tz * scale;
481 }
482
483 s = (GLfloat *)((char *)s + in->stride);
484 }
485 }
486
487
488
489 /* ================================================================
490 * Vertex transformation tests
491 */
492
493 /* Ensure our arrays are correctly aligned.
494 */
495 #if defined(__GNUC__)
496 #define ALIGN16(x) x __attribute__ ((aligned (16)))
497 #else
498 #define ALIGN16(x) x
499 #endif
500 static GLfloat ALIGN16(s[TEST_COUNT][5]);
501 static GLfloat ALIGN16(d[TEST_COUNT][4]);
502 static GLfloat ALIGN16(r[TEST_COUNT][4]);
503
504 static int test_transform_function( transform_func func, int psize, int mtype,
505 int masked, long *cycles )
506 {
507 GLvector4f source[1], dest[1], ref[1];
508 GLmatrix mat[1];
509 GLfloat *m;
510 GLubyte mask[TEST_COUNT];
511 int i, j;
512 #ifdef RUN_XFORM_BENCHMARK
513 int cycle_i; /* the counter for the benchmarks we run */
514 #endif
515
516 (void) cycles;
517
518 if ( psize > 4 ) {
519 gl_problem( NULL, "test_transform_function called with psize > 4\n" );
520 return 0;
521 }
522
523 mat->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
524 mat->type = mtypes[mtype];
525
526 m = mat->m;
527
528 m[0] = 63.0; m[4] = 43.0; m[ 8] = 29.0; m[12] = 43.0;
529 m[1] = 55.0; m[5] = 17.0; m[ 9] = 31.0; m[13] = 7.0;
530 m[2] = 44.0; m[6] = 9.0; m[10] = 7.0; m[14] = 3.0;
531 m[3] = 11.0; m[7] = 23.0; m[11] = 91.0; m[15] = 9.0;
532
533 for ( i = 0 ; i < 4 ; i++ ) {
534 for ( j = 0 ; j < 4 ; j++ ) {
535 switch ( templates[mtype][i * 4 + j] ) {
536 case NIL:
537 m[j * 4 + i] = 0.0;
538 break;
539 case ONE:
540 m[j * 4 + i] = 1.0;
541 break;
542 case NEG:
543 m[j * 4 + i] = -1.0;
544 break;
545 case VAR:
546 break;
547 default:
548 abort();
549 }
550 }
551 }
552
553 for ( i = 0 ; i < TEST_COUNT ; i++) {
554 mask[i] = i % 2; /* mask every 2nd element */
555 d[i][0] = s[i][0] = 0.0;
556 d[i][1] = s[i][1] = 0.0;
557 d[i][2] = s[i][2] = 0.0;
558 d[i][3] = s[i][3] = 1.0;
559 for ( j = 0 ; j < psize ; j++ )
560 s[i][j] = rnd();
561 }
562
563 source->data = (GLfloat(*)[4])s;
564 source->start = (GLfloat *)s;
565 source->count = TEST_COUNT;
566 source->stride = sizeof(s[0]);
567 source->size = 4;
568 source->flags = 0;
569
570 dest->data = (GLfloat(*)[4])d;
571 dest->start = (GLfloat *)d;
572 dest->count = TEST_COUNT;
573 dest->stride = sizeof(float[4]);
574 dest->size = 0;
575 dest->flags = 0;
576
577 ref->data = (GLfloat(*)[4])r;
578 ref->start = (GLfloat *)r;
579 ref->count = TEST_COUNT;
580 ref->stride = sizeof(float[4]);
581 ref->size = 0;
582 ref->flags = 0;
583
584 ref_transform( ref, mat, source, NULL, 0 );
585
586 if ( mesa_profile ) {
587 if ( masked ) {
588 BEGIN_RACE( *cycles );
589 func( dest, mat->m, source, mask, 1 );
590 END_RACE( *cycles );
591 } else {
592 BEGIN_RACE( *cycles );
593 func( dest, mat->m, source, NULL, 0 );
594 END_RACE( *cycles );
595 }
596 }
597 else {
598 if ( masked ) {
599 func( dest, mat->m, source, mask, 1 );
600 } else {
601 func( dest, mat->m, source, NULL, 0 );
602 }
603 }
604
605 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
606 if ( masked && (mask[i] & 1) )
607 continue;
608
609 for ( j = 0 ; j < 4 ; j++ ) {
610 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
611 printf( "-----------------------------\n" );
612 printf( "(i = %i, j = %i)\n", i, j );
613 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
614 d[i][0], r[i][0], r[i][0]-d[i][0],
615 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
616 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
617 d[i][1], r[i][1], r[i][1]-d[i][1],
618 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
619 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
620 d[i][2], r[i][2], r[i][2]-d[i][2],
621 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
622 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
623 d[i][3], r[i][3], r[i][3]-d[i][3],
624 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
625 return 0;
626 }
627 }
628 }
629
630 ALIGN_FREE( mat->m );
631 return 1;
632 }
633
634 void gl_test_all_transform_functions( char *description )
635 {
636 int masked, psize, mtype;
637 long benchmark_tab[2][4][7];
638 static int first_time = 1;
639
640 if ( first_time ) {
641 first_time = 0;
642 mesa_profile = getenv( "MESA_PROFILE" );
643 }
644
645 #ifdef RUN_XFORM_BENCHMARK
646 if ( mesa_profile ) {
647 if ( need_counter ) {
648 need_counter = 0;
649 INIT_COUNTER();
650 printf( "counter overhead: %ld cycles\n\n", counter_overhead );
651 }
652 printf( "transform results after hooking in %s functions:\n", description );
653 }
654 #endif
655
656 for ( masked = 0 ; masked <= 1 ; masked++ ) {
657 int cma = masked ? 1 : 0;
658 char *cmastring = masked ? "CULL_MASK_ACTIVE" : "0";
659
660 #ifdef RUN_XFORM_BENCHMARK
661 if ( mesa_profile ) {
662 printf( "\n culling: %s \n", masked ? "CULL_MASK_ACTIVE" : "0" );
663 for ( psize = 1 ; psize <= 4 ; psize++ ) {
664 printf( " p%d\t", psize );
665 }
666 printf( "\n--------------------------------------------------------\n" );
667 }
668 #endif
669
670 for ( mtype = 0 ; mtype < 7 ; mtype++ ) {
671 for ( psize = 1 ; psize <= 4 ; psize++ ) {
672 transform_func func = gl_transform_tab[cma][psize][mtypes[mtype]];
673 long *cycles = &(benchmark_tab[cma][psize-1][mtype]);
674
675 if ( test_transform_function( func, psize, mtype,
676 masked, cycles ) == 0 ) {
677 char buf[100];
678 sprintf( buf, "gl_transform_tab[%s][%d][%s] failed test (%s)",
679 cmastring, psize, mstrings[mtype], description );
680 gl_problem( NULL, buf );
681 }
682 #ifdef RUN_XFORM_BENCHMARK
683 if ( mesa_profile )
684 printf( " %li\t", benchmark_tab[cma][psize-1][mtype] );
685 #endif
686 }
687 #ifdef RUN_XFORM_BENCHMARK
688 if ( mesa_profile )
689 printf( " | [%s]\n", mstrings[mtype] );
690 #endif
691 }
692 #ifdef RUN_XFORM_BENCHMARK
693 if ( mesa_profile )
694 printf( "\n" );
695 #endif
696 }
697 }
698
699
700
701 /* ================================================================
702 * Normal transformation tests
703 */
704
705 static int test_norm_function( normal_func func, int mtype,
706 int masked, long *cycles )
707 {
708 GLvector3f source[1], dest[1], dest2[1], ref[1], ref2[1];
709 GLmatrix mat[1];
710 GLfloat s[TEST_COUNT][5], d[TEST_COUNT][3], r[TEST_COUNT][3];
711 GLfloat d2[TEST_COUNT][3], r2[TEST_COUNT][3], length[TEST_COUNT];
712 GLfloat scale;
713 GLfloat *m;
714 GLubyte mask[TEST_COUNT];
715 int i, j;
716 #ifdef RUN_XFORM_BENCHMARK
717 int cycle_i; /* the counter for the benchmarks we run */
718 #endif
719
720 (void) cycles;
721
722 mat->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
723 mat->inv = m = mat->m;
724
725 m[0] = 63.0; m[4] = 43.0; m[ 8] = 29.0; m[12] = 43.0;
726 m[1] = 55.0; m[5] = 17.0; m[ 9] = 31.0; m[13] = 7.0;
727 m[2] = 44.0; m[6] = 9.0; m[10] = 7.0; m[14] = 3.0;
728 m[3] = 11.0; m[7] = 23.0; m[11] = 91.0; m[15] = 9.0;
729
730 scale = 1.0F + rnd () * norm_scale_types[mtype];
731
732 for ( i = 0 ; i < 4 ; i++ ) {
733 for ( j = 0 ; j < 4 ; j++ ) {
734 switch ( norm_templates[mtype][i * 4 + j] ) {
735 case NIL:
736 m[j * 4 + i] = 0.0;
737 break;
738 case ONE:
739 m[j * 4 + i] = 1.0;
740 break;
741 case NEG:
742 m[j * 4 + i] = -1.0;
743 break;
744 case VAR:
745 break;
746 default:
747 abort();
748 }
749 }
750 }
751
752 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
753 mask[i] = i % 2; /* mask every 2nd element */
754 d[i][0] = s[i][0] = d2[i][0] = 0.0;
755 d[i][1] = s[i][1] = d2[i][1] = 0.0;
756 d[i][2] = s[i][2] = d2[i][2] = 0.0;
757 for ( j = 0 ; j < 3 ; j++ )
758 s[i][j] = rnd();
759 length[i] = 1 / sqrt( s[i][0]*s[i][0] +
760 s[i][1]*s[i][1] +
761 s[i][2]*s[i][2] );
762 }
763
764 source->data = (GLfloat(*)[3])s;
765 source->start = (GLfloat *)s;
766 source->count = TEST_COUNT;
767 source->stride = sizeof(s[0]);
768 source->flags = 0;
769
770 dest->data = (GLfloat(*)[3])d;
771 dest->start = (GLfloat *)d;
772 dest->count = TEST_COUNT;
773 dest->stride = sizeof(float[3]);
774 dest->flags = 0;
775
776 dest2->data = (GLfloat(*)[3])d2;
777 dest2->start = (GLfloat *)d2;
778 dest2->count = TEST_COUNT;
779 dest2->stride = sizeof(float[3]);
780 dest2->flags = 0;
781
782 ref->data = (GLfloat(*)[3])r;
783 ref->start = (GLfloat *)r;
784 ref->count = TEST_COUNT;
785 ref->stride = sizeof(float[3]);
786 ref->flags = 0;
787
788 ref2->data = (GLfloat(*)[3])r2;
789 ref2->start = (GLfloat *)r2;
790 ref2->count = TEST_COUNT;
791 ref2->stride = sizeof(float[3]);
792 ref2->flags = 0;
793
794 if ( norm_normalize_types[mtype] == 0 ) {
795 ref_norm_transform_rescale( mat, scale, source, NULL, NULL, ref );
796 } else {
797 ref_norm_transform_normalize( mat, scale, source, NULL, NULL, ref );
798 ref_norm_transform_normalize( mat, scale, source, length, NULL, ref2 );
799 }
800
801 if ( mesa_profile ) {
802 if ( masked ) {
803 BEGIN_RACE( *cycles );
804 func( mat, scale, source, NULL, mask, dest );
805 END_RACE( *cycles );
806 func( mat, scale, source, length, mask, dest2 );
807 } else {
808 BEGIN_RACE( *cycles );
809 func( mat, scale, source, NULL, NULL, dest );
810 END_RACE( *cycles );
811 func( mat, scale, source, length, NULL, dest2 );
812 }
813 } else {
814 if ( masked ) {
815 func( mat, scale, source, NULL, mask, dest );
816 func( mat, scale, source, length, mask, dest2 );
817 } else {
818 func( mat, scale, source, NULL, NULL, dest );
819 func( mat, scale, source, length, NULL, dest2 );
820 }
821 }
822
823 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
824 if ( masked && !(mask[i] & 1) )
825 continue;
826
827 for ( j = 0 ; j < 3 ; j++ ) {
828 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
829 printf( "-----------------------------\n" );
830 printf( "(i = %i, j = %i)\n", i, j );
831 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
832 d[i][0], r[i][0], r[i][0]/d[i][0],
833 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
834 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
835 d[i][1], r[i][1], r[i][1]/d[i][1],
836 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
837 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
838 d[i][2], r[i][2], r[i][2]/d[i][2],
839 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
840 return 0;
841 }
842
843 if ( norm_normalize_types[mtype] != 0 ) {
844 if ( significand_match( d2[i][j], r2[i][j] ) < REQUIRED_PRECISION ) {
845 printf( "------------------- precalculated length case ------\n" );
846 printf( "(i = %i, j = %i)\n", i, j );
847 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
848 d2[i][0], r2[i][0], r2[i][0]/d2[i][0],
849 MAX_PRECISION - significand_match( d2[i][0], r2[i][0] ) );
850 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
851 d2[i][1], r2[i][1], r2[i][1]/d2[i][1],
852 MAX_PRECISION - significand_match( d2[i][1], r2[i][1] ) );
853 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
854 d2[i][2], r2[i][2], r2[i][2]/d2[i][2],
855 MAX_PRECISION - significand_match( d2[i][2], r2[i][2] ) );
856 return 0;
857 }
858 }
859 }
860 }
861
862 ALIGN_FREE( mat->m );
863 return 1;
864 }
865
866 void gl_test_all_normal_transform_functions( char *description )
867 {
868 int masked;
869 int mtype;
870 long benchmark_tab[0xf][0x4];
871 static int first_time = 1;
872
873 if ( first_time ) {
874 first_time = 0;
875 mesa_profile = getenv( "MESA_PROFILE" );
876 }
877
878 #ifdef RUN_XFORM_BENCHMARK
879 if ( mesa_profile ) {
880 if ( need_counter ) {
881 need_counter = 0;
882 INIT_COUNTER();
883 printf( "counter overhead: %ld cycles\n\n", counter_overhead );
884 }
885 printf( "normal transform results after hooking in %s functions:\n",
886 description );
887 }
888 #endif
889
890 for ( masked = 0 ; masked <= 1 ; masked++ ) {
891 int cma = masked ? 1 : 0;
892 char *cmastring = masked ? "CULL_MASK_ACTIVE" : "0";
893
894 #ifdef RUN_XFORM_BENCHMARK
895 if ( mesa_profile ) {
896 printf( "\n culling: %s \n", masked ? "CULL_MASK_ACTIVE" : "0" );
897 printf( "\n-------------------------------------------------------\n" );
898 }
899 #endif
900
901 for ( mtype = 0 ; mtype < 8 ; mtype++ ) {
902 normal_func func = gl_normal_tab[norm_types[mtype]][cma];
903 long *cycles = &(benchmark_tab[mtype][cma]);
904
905 if ( test_norm_function( func, mtype, masked, cycles ) == 0 ) {
906 char buf[100];
907 sprintf( buf, "gl_normal_tab[%s][%s] failed test (%s)",
908 cmastring, norm_strings[mtype], description );
909 gl_problem( NULL, buf );
910 }
911
912 #ifdef RUN_XFORM_BENCHMARK
913 if ( mesa_profile ) {
914 printf( " %li\t", benchmark_tab[mtype][cma] );
915 printf( " | [%s]\n", norm_strings[mtype] );
916 }
917 }
918 if ( mesa_profile )
919 printf( "\n" );
920 #else
921 }
922 #endif
923 }
924 #ifdef RUN_XFORM_BENCHMARK
925 if ( mesa_profile )
926 fflush( stdout );
927 #endif
928 }
929
930 #endif /* DEBUG */