bring over build fixes from stable branch
[mesa.git] / src / mesa / math / m_debug_clip.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.0
5 *
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Gareth Hughes
27 */
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "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 /* This code only used for debugging */
46
47 static clip_func *clip_tab[2] = {
48 _mesa_clip_tab,
49 _mesa_clip_np_tab
50 };
51 static char *cnames[2] = {
52 "_mesa_clip_tab",
53 "_mesa_clip_np_tab"
54 };
55 #ifdef RUN_DEBUG_BENCHMARK
56 static char *cstrings[2] = {
57 "clip, perspective divide",
58 "clip, no divide"
59 };
60 #endif
61
62
63 /* =============================================================
64 * Reference cliptests
65 */
66
67 static GLvector4f *ref_cliptest_points4( GLvector4f *clip_vec,
68 GLvector4f *proj_vec,
69 GLubyte clipMask[],
70 GLubyte *orMask,
71 GLubyte *andMask )
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 ( -cz + cw < 0 ) mask |= CLIP_FAR_BIT;
92 if ( cz + cw < 0 ) mask |= CLIP_NEAR_BIT;
93 clipMask[i] = mask;
94 if ( mask ) {
95 c++;
96 tmpAndMask &= mask;
97 tmpOrMask |= mask;
98 vProj[i][0] = 0;
99 vProj[i][1] = 0;
100 vProj[i][2] = 0;
101 vProj[i][3] = 1;
102 } else {
103 GLfloat oow = 1.0F / cw;
104 vProj[i][0] = cx * oow;
105 vProj[i][1] = cy * oow;
106 vProj[i][2] = cz * oow;
107 vProj[i][3] = oow;
108 }
109 }
110
111 *orMask = tmpOrMask;
112 *andMask = (GLubyte) (c < count ? 0 : tmpAndMask);
113
114 proj_vec->flags |= VEC_SIZE_4;
115 proj_vec->size = 4;
116 proj_vec->count = clip_vec->count;
117 return proj_vec;
118 }
119
120 /* Keep these here for now, even though we don't use them...
121 */
122 static GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec,
123 GLvector4f *proj_vec,
124 GLubyte clipMask[],
125 GLubyte *orMask,
126 GLubyte *andMask )
127 {
128 const GLuint stride = clip_vec->stride;
129 const GLuint count = clip_vec->count;
130 const GLfloat *from = (GLfloat *)clip_vec->start;
131
132 GLubyte tmpOrMask = *orMask;
133 GLubyte tmpAndMask = *andMask;
134 GLuint i;
135 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
136 const GLfloat cx = from[0], cy = from[1], cz = from[2];
137 GLubyte mask = 0;
138 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
139 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
140 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
141 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
142 if ( cz > 1.0 ) mask |= CLIP_FAR_BIT;
143 else if ( cz < -1.0 ) mask |= CLIP_NEAR_BIT;
144 clipMask[i] = mask;
145 tmpOrMask |= mask;
146 tmpAndMask &= mask;
147 }
148
149 *orMask = tmpOrMask;
150 *andMask = tmpAndMask;
151 return clip_vec;
152 }
153
154 static GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec,
155 GLvector4f *proj_vec,
156 GLubyte clipMask[],
157 GLubyte *orMask,
158 GLubyte *andMask )
159 {
160 const GLuint stride = clip_vec->stride;
161 const GLuint count = clip_vec->count;
162 const GLfloat *from = (GLfloat *)clip_vec->start;
163
164 GLubyte tmpOrMask = *orMask;
165 GLubyte tmpAndMask = *andMask;
166 GLuint i;
167 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
168 const GLfloat cx = from[0], cy = from[1];
169 GLubyte mask = 0;
170 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
171 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
172 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
173 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
174 clipMask[i] = mask;
175 tmpOrMask |= mask;
176 tmpAndMask &= mask;
177 }
178
179 *orMask = tmpOrMask;
180 *andMask = tmpAndMask;
181 return clip_vec;
182 }
183
184 static clip_func ref_cliptest[5] = {
185 0,
186 0,
187 ref_cliptest_points2,
188 ref_cliptest_points3,
189 ref_cliptest_points4
190 };
191
192
193 /* =============================================================
194 * Cliptest tests
195 */
196
197 static GLfloat s[TEST_COUNT][4] ALIGN16;
198 static GLfloat d[TEST_COUNT][4] ALIGN16;
199 static GLfloat r[TEST_COUNT][4] ALIGN16;
200
201 static int test_cliptest_function( clip_func func, int np,
202 int psize, long *cycles )
203 {
204 GLvector4f source[1], dest[1], ref[1];
205 GLubyte dm[TEST_COUNT], dco, dca;
206 GLubyte rm[TEST_COUNT], rco, rca;
207 int i, j;
208 #ifdef RUN_DEBUG_BENCHMARK
209 int cycle_i; /* the counter for the benchmarks we run */
210 #endif
211
212 (void) cycles;
213
214 if ( psize > 4 ) {
215 _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" );
216 return 0;
217 }
218
219 for ( i = 0 ; i < TEST_COUNT ; i++) {
220 ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
221 ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
222 for ( j = 0 ; j < psize ; j++ )
223 s[i][j] = rnd();
224 }
225
226 source->data = (GLfloat(*)[4])s;
227 source->start = (GLfloat *)s;
228 source->count = TEST_COUNT;
229 source->stride = sizeof(s[0]);
230 source->size = 4;
231 source->flags = 0;
232
233 dest->data = (GLfloat(*)[4])d;
234 dest->start = (GLfloat *)d;
235 dest->count = TEST_COUNT;
236 dest->stride = sizeof(float[4]);
237 dest->size = 0;
238 dest->flags = 0;
239
240 ref->data = (GLfloat(*)[4])r;
241 ref->start = (GLfloat *)r;
242 ref->count = TEST_COUNT;
243 ref->stride = sizeof(float[4]);
244 ref->size = 0;
245 ref->flags = 0;
246
247 dco = rco = 0;
248 dca = rca = CLIP_ALL_BITS;
249
250 ref_cliptest[psize]( source, ref, rm, &rco, &rca );
251
252 if ( mesa_profile ) {
253 BEGIN_RACE( *cycles );
254 func( source, dest, dm, &dco, &dca );
255 END_RACE( *cycles );
256 }
257 else {
258 func( source, dest, dm, &dco, &dca );
259 }
260
261 if ( dco != rco ) {
262 _mesa_printf( "\n-----------------------------\n" );
263 _mesa_printf( "dco = 0x%02x rco = 0x%02x\n", dco, rco );
264 return 0;
265 }
266 if ( dca != rca ) {
267 _mesa_printf( "\n-----------------------------\n" );
268 _mesa_printf( "dca = 0x%02x rca = 0x%02x\n", dca, rca );
269 return 0;
270 }
271 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
272 if ( dm[i] != rm[i] ) {
273 _mesa_printf( "\n-----------------------------\n" );
274 _mesa_printf( "(i = %i)\n", i );
275 _mesa_printf( "dm = 0x%02x rm = 0x%02x\n", dm[i], rm[i] );
276 return 0;
277 }
278 }
279
280 /* Only verify output on projected points4 case. FIXME: Do we need
281 * to test other cases?
282 */
283 if ( np || psize < 4 )
284 return 1;
285
286 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
287 for ( j = 0 ; j < 4 ; j++ ) {
288 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
289 _mesa_printf( "\n-----------------------------\n" );
290 _mesa_printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n",
291 i, j, dm[i], rm[i] );
292 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
293 d[i][0], r[i][0], r[i][0]-d[i][0],
294 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
295 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
296 d[i][1], r[i][1], r[i][1]-d[i][1],
297 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
298 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
299 d[i][2], r[i][2], r[i][2]-d[i][2],
300 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
301 _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
302 d[i][3], r[i][3], r[i][3]-d[i][3],
303 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
304 return 0;
305 }
306 }
307 }
308
309 return 1;
310 }
311
312 void _math_test_all_cliptest_functions( char *description )
313 {
314 int np, psize;
315 long benchmark_tab[2][4];
316 static int first_time = 1;
317
318 if ( first_time ) {
319 first_time = 0;
320 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
321 }
322
323 #ifdef RUN_DEBUG_BENCHMARK
324 if ( mesa_profile ) {
325 if ( !counter_overhead ) {
326 INIT_COUNTER();
327 _mesa_printf( "counter overhead: %ld cycles\n\n", counter_overhead );
328 }
329 _mesa_printf( "cliptest results after hooking in %s functions:\n", description );
330 }
331 #endif
332
333 #ifdef RUN_DEBUG_BENCHMARK
334 if ( mesa_profile ) {
335 _mesa_printf( "\n\t" );
336 for ( psize = 2 ; psize <= 4 ; psize++ ) {
337 _mesa_printf( " p%d\t", psize );
338 }
339 _mesa_printf( "\n--------------------------------------------------------\n\t" );
340 }
341 #endif
342
343 for ( np = 0 ; np < 2 ; np++ ) {
344 for ( psize = 2 ; psize <= 4 ; psize++ ) {
345 clip_func func = clip_tab[np][psize];
346 long *cycles = &(benchmark_tab[np][psize-1]);
347
348 if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) {
349 char buf[100];
350 _mesa_sprintf( buf, "%s[%d] failed test (%s)",
351 cnames[np], psize, description );
352 _mesa_problem( NULL, buf );
353 }
354 #ifdef RUN_DEBUG_BENCHMARK
355 if ( mesa_profile )
356 _mesa_printf( " %li\t", benchmark_tab[np][psize-1] );
357 #endif
358 }
359 #ifdef RUN_DEBUG_BENCHMARK
360 if ( mesa_profile )
361 _mesa_printf( " | [%s]\n\t", cstrings[np] );
362 #endif
363 }
364 #ifdef RUN_DEBUG_BENCHMARK
365 if ( mesa_profile )
366 _mesa_printf( "\n" );
367 #endif
368 }
369
370
371 #endif /* DEBUG */