2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
38 #include "m_debug_util.h"
41 /* The linker doesn't like empty files */
45 #ifdef DEBUG_MATH /* This code only used for debugging */
47 static clip_func
*clip_tab
[2] = {
51 static char *cnames
[2] = {
55 #ifdef RUN_DEBUG_BENCHMARK
56 static char *cstrings
[2] = {
57 "clip, perspective divide",
63 /* =============================================================
67 static GLvector4f
*ref_cliptest_points4( GLvector4f
*clip_vec
,
72 GLboolean viewport_z_clip
)
74 const GLuint stride
= clip_vec
->stride
;
75 const GLuint count
= clip_vec
->count
;
76 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
78 GLfloat (*vProj
)[4] = (GLfloat (*)[4])proj_vec
->start
;
79 GLubyte tmpAndMask
= *andMask
;
80 GLubyte tmpOrMask
= *orMask
;
82 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
83 const GLfloat cx
= from
[0];
84 const GLfloat cy
= from
[1];
85 const GLfloat cz
= from
[2];
86 const GLfloat cw
= from
[3];
88 if ( -cx
+ cw
< 0 ) mask
|= CLIP_RIGHT_BIT
;
89 if ( cx
+ cw
< 0 ) mask
|= CLIP_LEFT_BIT
;
90 if ( -cy
+ cw
< 0 ) mask
|= CLIP_TOP_BIT
;
91 if ( cy
+ cw
< 0 ) mask
|= CLIP_BOTTOM_BIT
;
92 if (viewport_z_clip
) {
93 if ( -cz
+ cw
< 0 ) mask
|= CLIP_FAR_BIT
;
94 if ( cz
+ cw
< 0 ) mask
|= CLIP_NEAR_BIT
;
106 GLfloat oow
= 1.0F
/ cw
;
107 vProj
[i
][0] = cx
* oow
;
108 vProj
[i
][1] = cy
* oow
;
109 vProj
[i
][2] = cz
* oow
;
115 *andMask
= (GLubyte
) (c
< count
? 0 : tmpAndMask
);
117 proj_vec
->flags
|= VEC_SIZE_4
;
119 proj_vec
->count
= clip_vec
->count
;
123 /* Keep these here for now, even though we don't use them...
125 static GLvector4f
*ref_cliptest_points3( GLvector4f
*clip_vec
,
126 GLvector4f
*proj_vec
,
130 GLboolean viewport_z_clip
)
132 const GLuint stride
= clip_vec
->stride
;
133 const GLuint count
= clip_vec
->count
;
134 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
136 GLubyte tmpOrMask
= *orMask
;
137 GLubyte tmpAndMask
= *andMask
;
139 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
140 const GLfloat cx
= from
[0], cy
= from
[1], cz
= from
[2];
142 if ( cx
> 1.0 ) mask
|= CLIP_RIGHT_BIT
;
143 else if ( cx
< -1.0 ) mask
|= CLIP_LEFT_BIT
;
144 if ( cy
> 1.0 ) mask
|= CLIP_TOP_BIT
;
145 else if ( cy
< -1.0 ) mask
|= CLIP_BOTTOM_BIT
;
146 if (viewport_z_clip
) {
147 if ( cz
> 1.0 ) mask
|= CLIP_FAR_BIT
;
148 else if ( cz
< -1.0 ) mask
|= CLIP_NEAR_BIT
;
156 *andMask
= tmpAndMask
;
160 static GLvector4f
* ref_cliptest_points2( GLvector4f
*clip_vec
,
161 GLvector4f
*proj_vec
,
165 GLboolean viewport_z_clip
)
167 const GLuint stride
= clip_vec
->stride
;
168 const GLuint count
= clip_vec
->count
;
169 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
171 GLubyte tmpOrMask
= *orMask
;
172 GLubyte tmpAndMask
= *andMask
;
175 (void) viewport_z_clip
;
177 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
178 const GLfloat cx
= from
[0], cy
= from
[1];
180 if ( cx
> 1.0 ) mask
|= CLIP_RIGHT_BIT
;
181 else if ( cx
< -1.0 ) mask
|= CLIP_LEFT_BIT
;
182 if ( cy
> 1.0 ) mask
|= CLIP_TOP_BIT
;
183 else if ( cy
< -1.0 ) mask
|= CLIP_BOTTOM_BIT
;
190 *andMask
= tmpAndMask
;
194 static clip_func ref_cliptest
[5] = {
197 ref_cliptest_points2
,
198 ref_cliptest_points3
,
203 /* =============================================================
207 ALIGN16(static GLfloat
, s
[TEST_COUNT
][4]);
208 ALIGN16(static GLfloat
, d
[TEST_COUNT
][4]);
209 ALIGN16(static GLfloat
, r
[TEST_COUNT
][4]);
213 * Check if X, Y or Z component of the coordinate is close to W, in terms
217 xyz_close_to_w(const GLfloat c
[4])
220 return (fabs(c
[0] - c
[3]) < k
||
221 fabs(c
[1] - c
[3]) < k
||
222 fabs(c
[2] - c
[3]) < k
||
223 fabs(-c
[0] - c
[3]) < k
||
224 fabs(-c
[1] - c
[3]) < k
||
225 fabs(-c
[2] - c
[3]) < k
);
230 static int test_cliptest_function( clip_func func
, int np
,
231 int psize
, long *cycles
)
233 GLvector4f source
[1], dest
[1], ref
[1];
234 GLubyte dm
[TEST_COUNT
], dco
, dca
;
235 GLubyte rm
[TEST_COUNT
], rco
, rca
;
237 #ifdef RUN_DEBUG_BENCHMARK
238 int cycle_i
; /* the counter for the benchmarks we run */
240 GLboolean viewport_z_clip
= GL_TRUE
;
245 _mesa_problem( NULL
, "test_cliptest_function called with psize > 4\n" );
249 for ( i
= 0 ; i
< TEST_COUNT
; i
++) {
250 ASSIGN_4V( d
[i
], 0.0, 0.0, 0.0, 1.0 );
251 ASSIGN_4V( s
[i
], 0.0, 0.0, 0.0, 1.0 );
252 for ( j
= 0 ; j
< psize
; j
++ )
256 source
->data
= (GLfloat(*)[4])s
;
257 source
->start
= (GLfloat
*)s
;
258 source
->count
= TEST_COUNT
;
259 source
->stride
= sizeof(s
[0]);
263 dest
->data
= (GLfloat(*)[4])d
;
264 dest
->start
= (GLfloat
*)d
;
265 dest
->count
= TEST_COUNT
;
266 dest
->stride
= sizeof(float[4]);
270 ref
->data
= (GLfloat(*)[4])r
;
271 ref
->start
= (GLfloat
*)r
;
272 ref
->count
= TEST_COUNT
;
273 ref
->stride
= sizeof(float[4]);
278 dca
= rca
= CLIP_FRUSTUM_BITS
;
280 ref_cliptest
[psize
]( source
, ref
, rm
, &rco
, &rca
, viewport_z_clip
);
282 if ( mesa_profile
) {
283 BEGIN_RACE( *cycles
);
284 func( source
, dest
, dm
, &dco
, &dca
, viewport_z_clip
);
288 func( source
, dest
, dm
, &dco
, &dca
, viewport_z_clip
);
292 printf( "\n-----------------------------\n" );
293 printf( "dco = 0x%02x rco = 0x%02x\n", dco
, rco
);
297 printf( "\n-----------------------------\n" );
298 printf( "dca = 0x%02x rca = 0x%02x\n", dca
, rca
);
301 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
302 if ( dm
[i
] != rm
[i
] ) {
303 GLfloat
*c
= source
->start
;
304 STRIDE_F(c
, source
->stride
* i
);
305 if (psize
== 4 && xyz_close_to_w(c
)) {
306 /* The coordinate is very close to the clip plane. The clipmask
307 * may vary depending on code path, but that's OK.
311 printf( "\n-----------------------------\n" );
312 printf( "mask[%d] = 0x%02x ref mask[%d] = 0x%02x\n", i
, dm
[i
], i
,rm
[i
] );
313 printf(" coord = %f, %f, %f, %f\n",
314 c
[0], c
[1], c
[2], c
[3]);
319 /* Only verify output on projected points4 case. FIXME: Do we need
320 * to test other cases?
322 if ( np
|| psize
< 4 )
325 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
326 for ( j
= 0 ; j
< 4 ; j
++ ) {
327 if ( significand_match( d
[i
][j
], r
[i
][j
] ) < REQUIRED_PRECISION
) {
328 printf( "\n-----------------------------\n" );
329 printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n",
330 i
, j
, dm
[i
], rm
[i
] );
331 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
332 d
[i
][0], r
[i
][0], r
[i
][0]-d
[i
][0],
333 MAX_PRECISION
- significand_match( d
[i
][0], r
[i
][0] ) );
334 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
335 d
[i
][1], r
[i
][1], r
[i
][1]-d
[i
][1],
336 MAX_PRECISION
- significand_match( d
[i
][1], r
[i
][1] ) );
337 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
338 d
[i
][2], r
[i
][2], r
[i
][2]-d
[i
][2],
339 MAX_PRECISION
- significand_match( d
[i
][2], r
[i
][2] ) );
340 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
341 d
[i
][3], r
[i
][3], r
[i
][3]-d
[i
][3],
342 MAX_PRECISION
- significand_match( d
[i
][3], r
[i
][3] ) );
351 void _math_test_all_cliptest_functions( char *description
)
354 long benchmark_tab
[2][4];
355 static int first_time
= 1;
359 mesa_profile
= _mesa_getenv( "MESA_PROFILE" );
362 #ifdef RUN_DEBUG_BENCHMARK
363 if ( mesa_profile
) {
364 if ( !counter_overhead
) {
366 printf( "counter overhead: %ld cycles\n\n", counter_overhead
);
368 printf( "cliptest results after hooking in %s functions:\n", description
);
372 #ifdef RUN_DEBUG_BENCHMARK
373 if ( mesa_profile
) {
375 for ( psize
= 2 ; psize
<= 4 ; psize
++ ) {
376 printf( " p%d\t", psize
);
378 printf( "\n--------------------------------------------------------\n\t" );
382 for ( np
= 0 ; np
< 2 ; np
++ ) {
383 for ( psize
= 2 ; psize
<= 4 ; psize
++ ) {
384 clip_func func
= clip_tab
[np
][psize
];
385 long *cycles
= &(benchmark_tab
[np
][psize
-1]);
387 if ( test_cliptest_function( func
, np
, psize
, cycles
) == 0 ) {
389 sprintf( buf
, "%s[%d] failed test (%s)",
390 cnames
[np
], psize
, description
);
391 _mesa_problem( NULL
, "%s", buf
);
393 #ifdef RUN_DEBUG_BENCHMARK
395 printf( " %li\t", benchmark_tab
[np
][psize
-1] );
398 #ifdef RUN_DEBUG_BENCHMARK
400 printf( " | [%s]\n\t", cstrings
[np
] );
403 #ifdef RUN_DEBUG_BENCHMARK
410 #endif /* DEBUG_MATH */