f2b757a91b8b683b857dda478909b324544dfc46
[mesa.git] / src / mesa / math / m_debug_clip.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Gareth Hughes
26 */
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/macros.h"
31 #include "main/imports.h"
32
33 #include "m_matrix.h"
34 #include "m_xform.h"
35
36 #include "m_debug.h"
37 #include "m_debug_util.h"
38
39 #ifdef __UNIXOS2__
40 /* The linker doesn't like empty files */
41 static char dummy;
42 #endif
43
44 #ifdef DEBUG_MATH /* This code only used for debugging */
45
46 static clip_func *clip_tab[2] = {
47 _mesa_clip_tab,
48 _mesa_clip_np_tab
49 };
50 static char *cnames[2] = {
51 "_mesa_clip_tab",
52 "_mesa_clip_np_tab"
53 };
54 #ifdef RUN_DEBUG_BENCHMARK
55 static char *cstrings[2] = {
56 "clip, perspective divide",
57 "clip, no divide"
58 };
59 #endif
60
61
62 /* =============================================================
63 * Reference cliptests
64 */
65
66 static GLvector4f *ref_cliptest_points4( GLvector4f *clip_vec,
67 GLvector4f *proj_vec,
68 GLubyte clipMask[],
69 GLubyte *orMask,
70 GLubyte *andMask,
71 GLboolean viewport_z_clip )
72 {
73 const GLuint stride = clip_vec->stride;
74 const GLuint count = clip_vec->count;
75 const GLfloat *from = (GLfloat *)clip_vec->start;
76 GLuint c = 0;
77 GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
78 GLubyte tmpAndMask = *andMask;
79 GLubyte tmpOrMask = *orMask;
80 GLuint i;
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];
86 GLubyte mask = 0;
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;
94 }
95 clipMask[i] = mask;
96 if ( mask ) {
97 c++;
98 tmpAndMask &= mask;
99 tmpOrMask |= mask;
100 vProj[i][0] = 0;
101 vProj[i][1] = 0;
102 vProj[i][2] = 0;
103 vProj[i][3] = 1;
104 } else {
105 GLfloat oow = 1.0F / cw;
106 vProj[i][0] = cx * oow;
107 vProj[i][1] = cy * oow;
108 vProj[i][2] = cz * oow;
109 vProj[i][3] = oow;
110 }
111 }
112
113 *orMask = tmpOrMask;
114 *andMask = (GLubyte) (c < count ? 0 : tmpAndMask);
115
116 proj_vec->flags |= VEC_SIZE_4;
117 proj_vec->size = 4;
118 proj_vec->count = clip_vec->count;
119 return proj_vec;
120 }
121
122 /* Keep these here for now, even though we don't use them...
123 */
124 static GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec,
125 GLvector4f *proj_vec,
126 GLubyte clipMask[],
127 GLubyte *orMask,
128 GLubyte *andMask )
129 {
130 const GLuint stride = clip_vec->stride;
131 const GLuint count = clip_vec->count;
132 const GLfloat *from = (GLfloat *)clip_vec->start;
133
134 GLubyte tmpOrMask = *orMask;
135 GLubyte tmpAndMask = *andMask;
136 GLuint i;
137 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
138 const GLfloat cx = from[0], cy = from[1], cz = from[2];
139 GLubyte mask = 0;
140 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
141 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
142 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
143 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
144 if ( cz > 1.0 ) mask |= CLIP_FAR_BIT;
145 else if ( cz < -1.0 ) mask |= CLIP_NEAR_BIT;
146 clipMask[i] = mask;
147 tmpOrMask |= mask;
148 tmpAndMask &= mask;
149 }
150
151 *orMask = tmpOrMask;
152 *andMask = tmpAndMask;
153 return clip_vec;
154 }
155
156 static GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec,
157 GLvector4f *proj_vec,
158 GLubyte clipMask[],
159 GLubyte *orMask,
160 GLubyte *andMask )
161 {
162 const GLuint stride = clip_vec->stride;
163 const GLuint count = clip_vec->count;
164 const GLfloat *from = (GLfloat *)clip_vec->start;
165
166 GLubyte tmpOrMask = *orMask;
167 GLubyte tmpAndMask = *andMask;
168 GLuint i;
169 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
170 const GLfloat cx = from[0], cy = from[1];
171 GLubyte mask = 0;
172 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
173 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
174 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
175 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
176 clipMask[i] = mask;
177 tmpOrMask |= mask;
178 tmpAndMask &= mask;
179 }
180
181 *orMask = tmpOrMask;
182 *andMask = tmpAndMask;
183 return clip_vec;
184 }
185
186 static clip_func ref_cliptest[5] = {
187 0,
188 0,
189 ref_cliptest_points2,
190 ref_cliptest_points3,
191 ref_cliptest_points4
192 };
193
194
195 /* =============================================================
196 * Cliptest tests
197 */
198
199 ALIGN16(static GLfloat, s[TEST_COUNT][4]);
200 ALIGN16(static GLfloat, d[TEST_COUNT][4]);
201 ALIGN16(static GLfloat, r[TEST_COUNT][4]);
202
203
204 static int test_cliptest_function( clip_func func, int np,
205 int psize, long *cycles )
206 {
207 GLvector4f source[1], dest[1], ref[1];
208 GLubyte dm[TEST_COUNT], dco, dca;
209 GLubyte rm[TEST_COUNT], rco, rca;
210 int i, j;
211 #ifdef RUN_DEBUG_BENCHMARK
212 int cycle_i; /* the counter for the benchmarks we run */
213 #endif
214
215 (void) cycles;
216
217 if ( psize > 4 ) {
218 _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" );
219 return 0;
220 }
221
222 for ( i = 0 ; i < TEST_COUNT ; i++) {
223 ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
224 ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
225 for ( j = 0 ; j < psize ; j++ )
226 s[i][j] = rnd();
227 }
228
229 source->data = (GLfloat(*)[4])s;
230 source->start = (GLfloat *)s;
231 source->count = TEST_COUNT;
232 source->stride = sizeof(s[0]);
233 source->size = 4;
234 source->flags = 0;
235
236 dest->data = (GLfloat(*)[4])d;
237 dest->start = (GLfloat *)d;
238 dest->count = TEST_COUNT;
239 dest->stride = sizeof(float[4]);
240 dest->size = 0;
241 dest->flags = 0;
242
243 ref->data = (GLfloat(*)[4])r;
244 ref->start = (GLfloat *)r;
245 ref->count = TEST_COUNT;
246 ref->stride = sizeof(float[4]);
247 ref->size = 0;
248 ref->flags = 0;
249
250 dco = rco = 0;
251 dca = rca = CLIP_FRUSTUM_BITS;
252
253 ref_cliptest[psize]( source, ref, rm, &rco, &rca );
254
255 if ( mesa_profile ) {
256 BEGIN_RACE( *cycles );
257 func( source, dest, dm, &dco, &dca );
258 END_RACE( *cycles );
259 }
260 else {
261 func( source, dest, dm, &dco, &dca );
262 }
263
264 if ( dco != rco ) {
265 _mesa_printf( "\n-----------------------------\n" );
266 _mesa_printf( "dco = 0x%02x rco = 0x%02x\n", dco, rco );
267 return 0;
268 }
269 if ( dca != rca ) {
270 _mesa_printf( "\n-----------------------------\n" );
271 _mesa_printf( "dca = 0x%02x rca = 0x%02x\n", dca, rca );
272 return 0;
273 }
274 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
275 if ( dm[i] != rm[i] ) {
276 _mesa_printf( "\n-----------------------------\n" );
277 _mesa_printf( "(i = %i)\n", i );
278 _mesa_printf( "dm = 0x%02x rm = 0x%02x\n", dm[i], rm[i] );
279 return 0;
280 }
281 }
282
283 /* Only verify output on projected points4 case. FIXME: Do we need
284 * to test other cases?
285 */
286 if ( np || psize < 4 )
287 return 1;
288
289 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
290 for ( j = 0 ; j < 4 ; j++ ) {
291 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
292 _mesa_printf( "\n-----------------------------\n" );
293 _mesa_printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n",
294 i, j, dm[i], rm[i] );
295 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
296 d[i][0], r[i][0], r[i][0]-d[i][0],
297 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
298 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
299 d[i][1], r[i][1], r[i][1]-d[i][1],
300 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
301 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
302 d[i][2], r[i][2], r[i][2]-d[i][2],
303 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
304 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
305 d[i][3], r[i][3], r[i][3]-d[i][3],
306 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
307 return 0;
308 }
309 }
310 }
311
312 return 1;
313 }
314
315 void _math_test_all_cliptest_functions( char *description )
316 {
317 int np, psize;
318 long benchmark_tab[2][4];
319 static int first_time = 1;
320
321 if ( first_time ) {
322 first_time = 0;
323 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
324 }
325
326 #ifdef RUN_DEBUG_BENCHMARK
327 if ( mesa_profile ) {
328 if ( !counter_overhead ) {
329 INIT_COUNTER();
330 _mesa_printf( "counter overhead: %ld cycles\n\n", counter_overhead );
331 }
332 _mesa_printf( "cliptest results after hooking in %s functions:\n", description );
333 }
334 #endif
335
336 #ifdef RUN_DEBUG_BENCHMARK
337 if ( mesa_profile ) {
338 _mesa_printf( "\n\t" );
339 for ( psize = 2 ; psize <= 4 ; psize++ ) {
340 _mesa_printf( " p%d\t", psize );
341 }
342 _mesa_printf( "\n--------------------------------------------------------\n\t" );
343 }
344 #endif
345
346 for ( np = 0 ; np < 2 ; np++ ) {
347 for ( psize = 2 ; psize <= 4 ; psize++ ) {
348 clip_func func = clip_tab[np][psize];
349 long *cycles = &(benchmark_tab[np][psize-1]);
350
351 if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) {
352 char buf[100];
353 _mesa_sprintf( buf, "%s[%d] failed test (%s)",
354 cnames[np], psize, description );
355 _mesa_problem( NULL, buf );
356 }
357 #ifdef RUN_DEBUG_BENCHMARK
358 if ( mesa_profile )
359 _mesa_printf( " %li\t", benchmark_tab[np][psize-1] );
360 #endif
361 }
362 #ifdef RUN_DEBUG_BENCHMARK
363 if ( mesa_profile )
364 _mesa_printf( " | [%s]\n\t", cstrings[np] );
365 #endif
366 }
367 #ifdef RUN_DEBUG_BENCHMARK
368 if ( mesa_profile )
369 _mesa_printf( "\n" );
370 #endif
371 }
372
373
374 #endif /* DEBUG_MATH */