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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/macros.h"
31 #include "main/imports.h"
37 #include "m_debug_util.h"
40 /* The linker doesn't like empty files */
44 #ifdef DEBUG_MATH /* This code only used for debugging */
46 static clip_func
*clip_tab
[2] = {
50 static char *cnames
[2] = {
54 #ifdef RUN_DEBUG_BENCHMARK
55 static char *cstrings
[2] = {
56 "clip, perspective divide",
62 /* =============================================================
66 static GLvector4f
*ref_cliptest_points4( GLvector4f
*clip_vec
,
71 GLboolean viewport_z_clip
)
73 const GLuint stride
= clip_vec
->stride
;
74 const GLuint count
= clip_vec
->count
;
75 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
77 GLfloat (*vProj
)[4] = (GLfloat (*)[4])proj_vec
->start
;
78 GLubyte tmpAndMask
= *andMask
;
79 GLubyte tmpOrMask
= *orMask
;
81 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
82 const GLfloat cx
= from
[0];
83 const GLfloat cy
= from
[1];
84 const GLfloat cz
= from
[2];
85 const GLfloat cw
= from
[3];
87 if ( -cx
+ cw
< 0 ) mask
|= CLIP_RIGHT_BIT
;
88 if ( cx
+ cw
< 0 ) mask
|= CLIP_LEFT_BIT
;
89 if ( -cy
+ cw
< 0 ) mask
|= CLIP_TOP_BIT
;
90 if ( cy
+ cw
< 0 ) mask
|= CLIP_BOTTOM_BIT
;
91 if (viewport_z_clip
) {
92 if ( -cz
+ cw
< 0 ) mask
|= CLIP_FAR_BIT
;
93 if ( cz
+ cw
< 0 ) mask
|= CLIP_NEAR_BIT
;
105 GLfloat oow
= 1.0F
/ cw
;
106 vProj
[i
][0] = cx
* oow
;
107 vProj
[i
][1] = cy
* oow
;
108 vProj
[i
][2] = cz
* oow
;
114 *andMask
= (GLubyte
) (c
< count
? 0 : tmpAndMask
);
116 proj_vec
->flags
|= VEC_SIZE_4
;
118 proj_vec
->count
= clip_vec
->count
;
122 /* Keep these here for now, even though we don't use them...
124 static GLvector4f
*ref_cliptest_points3( GLvector4f
*clip_vec
,
125 GLvector4f
*proj_vec
,
129 GLboolean viewport_z_clip
)
131 const GLuint stride
= clip_vec
->stride
;
132 const GLuint count
= clip_vec
->count
;
133 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
135 GLubyte tmpOrMask
= *orMask
;
136 GLubyte tmpAndMask
= *andMask
;
138 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
139 const GLfloat cx
= from
[0], cy
= from
[1], cz
= from
[2];
141 if ( cx
> 1.0 ) mask
|= CLIP_RIGHT_BIT
;
142 else if ( cx
< -1.0 ) mask
|= CLIP_LEFT_BIT
;
143 if ( cy
> 1.0 ) mask
|= CLIP_TOP_BIT
;
144 else if ( cy
< -1.0 ) mask
|= CLIP_BOTTOM_BIT
;
145 if (viewport_z_clip
) {
146 if ( cz
> 1.0 ) mask
|= CLIP_FAR_BIT
;
147 else if ( cz
< -1.0 ) mask
|= CLIP_NEAR_BIT
;
155 *andMask
= tmpAndMask
;
159 static GLvector4f
* ref_cliptest_points2( GLvector4f
*clip_vec
,
160 GLvector4f
*proj_vec
,
164 GLboolean viewport_z_clip
)
166 const GLuint stride
= clip_vec
->stride
;
167 const GLuint count
= clip_vec
->count
;
168 const GLfloat
*from
= (GLfloat
*)clip_vec
->start
;
170 GLubyte tmpOrMask
= *orMask
;
171 GLubyte tmpAndMask
= *andMask
;
174 (void) viewport_z_clip
;
176 for ( i
= 0 ; i
< count
; i
++, STRIDE_F(from
, stride
) ) {
177 const GLfloat cx
= from
[0], cy
= from
[1];
179 if ( cx
> 1.0 ) mask
|= CLIP_RIGHT_BIT
;
180 else if ( cx
< -1.0 ) mask
|= CLIP_LEFT_BIT
;
181 if ( cy
> 1.0 ) mask
|= CLIP_TOP_BIT
;
182 else if ( cy
< -1.0 ) mask
|= CLIP_BOTTOM_BIT
;
189 *andMask
= tmpAndMask
;
193 static clip_func ref_cliptest
[5] = {
196 ref_cliptest_points2
,
197 ref_cliptest_points3
,
202 /* =============================================================
206 ALIGN16(static GLfloat
, s
[TEST_COUNT
][4]);
207 ALIGN16(static GLfloat
, d
[TEST_COUNT
][4]);
208 ALIGN16(static GLfloat
, r
[TEST_COUNT
][4]);
211 static int test_cliptest_function( clip_func func
, int np
,
212 int psize
, long *cycles
)
214 GLvector4f source
[1], dest
[1], ref
[1];
215 GLubyte dm
[TEST_COUNT
], dco
, dca
;
216 GLubyte rm
[TEST_COUNT
], rco
, rca
;
218 #ifdef RUN_DEBUG_BENCHMARK
219 int cycle_i
; /* the counter for the benchmarks we run */
221 GLboolean viewport_z_clip
= GL_TRUE
;
226 _mesa_problem( NULL
, "test_cliptest_function called with psize > 4\n" );
230 for ( i
= 0 ; i
< TEST_COUNT
; i
++) {
231 ASSIGN_4V( d
[i
], 0.0, 0.0, 0.0, 1.0 );
232 ASSIGN_4V( s
[i
], 0.0, 0.0, 0.0, 1.0 );
233 for ( j
= 0 ; j
< psize
; j
++ )
237 source
->data
= (GLfloat(*)[4])s
;
238 source
->start
= (GLfloat
*)s
;
239 source
->count
= TEST_COUNT
;
240 source
->stride
= sizeof(s
[0]);
244 dest
->data
= (GLfloat(*)[4])d
;
245 dest
->start
= (GLfloat
*)d
;
246 dest
->count
= TEST_COUNT
;
247 dest
->stride
= sizeof(float[4]);
251 ref
->data
= (GLfloat(*)[4])r
;
252 ref
->start
= (GLfloat
*)r
;
253 ref
->count
= TEST_COUNT
;
254 ref
->stride
= sizeof(float[4]);
259 dca
= rca
= CLIP_FRUSTUM_BITS
;
261 ref_cliptest
[psize
]( source
, ref
, rm
, &rco
, &rca
, viewport_z_clip
);
263 if ( mesa_profile
) {
264 BEGIN_RACE( *cycles
);
265 func( source
, dest
, dm
, &dco
, &dca
, viewport_z_clip
);
269 func( source
, dest
, dm
, &dco
, &dca
, viewport_z_clip
);
273 _mesa_printf( "\n-----------------------------\n" );
274 _mesa_printf( "dco = 0x%02x rco = 0x%02x\n", dco
, rco
);
278 _mesa_printf( "\n-----------------------------\n" );
279 _mesa_printf( "dca = 0x%02x rca = 0x%02x\n", dca
, rca
);
282 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
283 if ( dm
[i
] != rm
[i
] ) {
284 _mesa_printf( "\n-----------------------------\n" );
285 _mesa_printf( "(i = %i)\n", i
);
286 _mesa_printf( "dm = 0x%02x rm = 0x%02x\n", dm
[i
], rm
[i
] );
291 /* Only verify output on projected points4 case. FIXME: Do we need
292 * to test other cases?
294 if ( np
|| psize
< 4 )
297 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
298 for ( j
= 0 ; j
< 4 ; j
++ ) {
299 if ( significand_match( d
[i
][j
], r
[i
][j
] ) < REQUIRED_PRECISION
) {
300 _mesa_printf( "\n-----------------------------\n" );
301 _mesa_printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n",
302 i
, j
, dm
[i
], rm
[i
] );
303 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
304 d
[i
][0], r
[i
][0], r
[i
][0]-d
[i
][0],
305 MAX_PRECISION
- significand_match( d
[i
][0], r
[i
][0] ) );
306 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
307 d
[i
][1], r
[i
][1], r
[i
][1]-d
[i
][1],
308 MAX_PRECISION
- significand_match( d
[i
][1], r
[i
][1] ) );
309 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
310 d
[i
][2], r
[i
][2], r
[i
][2]-d
[i
][2],
311 MAX_PRECISION
- significand_match( d
[i
][2], r
[i
][2] ) );
312 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
313 d
[i
][3], r
[i
][3], r
[i
][3]-d
[i
][3],
314 MAX_PRECISION
- significand_match( d
[i
][3], r
[i
][3] ) );
323 void _math_test_all_cliptest_functions( char *description
)
326 long benchmark_tab
[2][4];
327 static int first_time
= 1;
331 mesa_profile
= _mesa_getenv( "MESA_PROFILE" );
334 #ifdef RUN_DEBUG_BENCHMARK
335 if ( mesa_profile
) {
336 if ( !counter_overhead
) {
338 _mesa_printf( "counter overhead: %ld cycles\n\n", counter_overhead
);
340 _mesa_printf( "cliptest results after hooking in %s functions:\n", description
);
344 #ifdef RUN_DEBUG_BENCHMARK
345 if ( mesa_profile
) {
346 _mesa_printf( "\n\t" );
347 for ( psize
= 2 ; psize
<= 4 ; psize
++ ) {
348 _mesa_printf( " p%d\t", psize
);
350 _mesa_printf( "\n--------------------------------------------------------\n\t" );
354 for ( np
= 0 ; np
< 2 ; np
++ ) {
355 for ( psize
= 2 ; psize
<= 4 ; psize
++ ) {
356 clip_func func
= clip_tab
[np
][psize
];
357 long *cycles
= &(benchmark_tab
[np
][psize
-1]);
359 if ( test_cliptest_function( func
, np
, psize
, cycles
) == 0 ) {
361 _mesa_sprintf( buf
, "%s[%d] failed test (%s)",
362 cnames
[np
], psize
, description
);
363 _mesa_problem( NULL
, buf
);
365 #ifdef RUN_DEBUG_BENCHMARK
367 _mesa_printf( " %li\t", benchmark_tab
[np
][psize
-1] );
370 #ifdef RUN_DEBUG_BENCHMARK
372 _mesa_printf( " | [%s]\n\t", cstrings
[np
] );
375 #ifdef RUN_DEBUG_BENCHMARK
377 _mesa_printf( "\n" );
382 #endif /* DEBUG_MATH */