mesa: Restore 78-column wrapping of license text in C-style comments.
[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 * 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.
24 *
25 * Authors:
26 * Gareth Hughes
27 */
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/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_MATH /* 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 GLboolean viewport_z_clip )
73 {
74 const GLuint stride = clip_vec->stride;
75 const GLuint count = clip_vec->count;
76 const GLfloat *from = (GLfloat *)clip_vec->start;
77 GLuint c = 0;
78 GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
79 GLubyte tmpAndMask = *andMask;
80 GLubyte tmpOrMask = *orMask;
81 GLuint i;
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];
87 GLubyte mask = 0;
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;
95 }
96 clipMask[i] = mask;
97 if ( mask ) {
98 c++;
99 tmpAndMask &= mask;
100 tmpOrMask |= mask;
101 vProj[i][0] = 0;
102 vProj[i][1] = 0;
103 vProj[i][2] = 0;
104 vProj[i][3] = 1;
105 } else {
106 GLfloat oow = 1.0F / cw;
107 vProj[i][0] = cx * oow;
108 vProj[i][1] = cy * oow;
109 vProj[i][2] = cz * oow;
110 vProj[i][3] = oow;
111 }
112 }
113
114 *orMask = tmpOrMask;
115 *andMask = (GLubyte) (c < count ? 0 : tmpAndMask);
116
117 proj_vec->flags |= VEC_SIZE_4;
118 proj_vec->size = 4;
119 proj_vec->count = clip_vec->count;
120 return proj_vec;
121 }
122
123 /* Keep these here for now, even though we don't use them...
124 */
125 static GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec,
126 GLvector4f *proj_vec,
127 GLubyte clipMask[],
128 GLubyte *orMask,
129 GLubyte *andMask,
130 GLboolean viewport_z_clip )
131 {
132 const GLuint stride = clip_vec->stride;
133 const GLuint count = clip_vec->count;
134 const GLfloat *from = (GLfloat *)clip_vec->start;
135
136 GLubyte tmpOrMask = *orMask;
137 GLubyte tmpAndMask = *andMask;
138 GLuint i;
139 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
140 const GLfloat cx = from[0], cy = from[1], cz = from[2];
141 GLubyte mask = 0;
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;
149 }
150 clipMask[i] = mask;
151 tmpOrMask |= mask;
152 tmpAndMask &= mask;
153 }
154
155 *orMask = tmpOrMask;
156 *andMask = tmpAndMask;
157 return clip_vec;
158 }
159
160 static GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec,
161 GLvector4f *proj_vec,
162 GLubyte clipMask[],
163 GLubyte *orMask,
164 GLubyte *andMask,
165 GLboolean viewport_z_clip )
166 {
167 const GLuint stride = clip_vec->stride;
168 const GLuint count = clip_vec->count;
169 const GLfloat *from = (GLfloat *)clip_vec->start;
170
171 GLubyte tmpOrMask = *orMask;
172 GLubyte tmpAndMask = *andMask;
173 GLuint i;
174
175 (void) viewport_z_clip;
176
177 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
178 const GLfloat cx = from[0], cy = from[1];
179 GLubyte mask = 0;
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;
184 clipMask[i] = mask;
185 tmpOrMask |= mask;
186 tmpAndMask &= mask;
187 }
188
189 *orMask = tmpOrMask;
190 *andMask = tmpAndMask;
191 return clip_vec;
192 }
193
194 static clip_func ref_cliptest[5] = {
195 0,
196 0,
197 ref_cliptest_points2,
198 ref_cliptest_points3,
199 ref_cliptest_points4
200 };
201
202
203 /* =============================================================
204 * Cliptest tests
205 */
206
207 ALIGN16(static GLfloat, s[TEST_COUNT][4]);
208 ALIGN16(static GLfloat, d[TEST_COUNT][4]);
209 ALIGN16(static GLfloat, r[TEST_COUNT][4]);
210
211
212 /**
213 * Check if X, Y or Z component of the coordinate is close to W, in terms
214 * of the clip test.
215 */
216 static GLboolean
217 xyz_close_to_w(const GLfloat c[4])
218 {
219 float k = 0.0001;
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);
226 }
227
228
229
230 static int test_cliptest_function( clip_func func, int np,
231 int psize, long *cycles )
232 {
233 GLvector4f source[1], dest[1], ref[1];
234 GLubyte dm[TEST_COUNT], dco, dca;
235 GLubyte rm[TEST_COUNT], rco, rca;
236 int i, j;
237 #ifdef RUN_DEBUG_BENCHMARK
238 int cycle_i; /* the counter for the benchmarks we run */
239 #endif
240 GLboolean viewport_z_clip = GL_TRUE;
241
242 (void) cycles;
243
244 if ( psize > 4 ) {
245 _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" );
246 return 0;
247 }
248
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++ )
253 s[i][j] = rnd();
254 }
255
256 source->data = (GLfloat(*)[4])s;
257 source->start = (GLfloat *)s;
258 source->count = TEST_COUNT;
259 source->stride = sizeof(s[0]);
260 source->size = 4;
261 source->flags = 0;
262
263 dest->data = (GLfloat(*)[4])d;
264 dest->start = (GLfloat *)d;
265 dest->count = TEST_COUNT;
266 dest->stride = sizeof(float[4]);
267 dest->size = 0;
268 dest->flags = 0;
269
270 ref->data = (GLfloat(*)[4])r;
271 ref->start = (GLfloat *)r;
272 ref->count = TEST_COUNT;
273 ref->stride = sizeof(float[4]);
274 ref->size = 0;
275 ref->flags = 0;
276
277 dco = rco = 0;
278 dca = rca = CLIP_FRUSTUM_BITS;
279
280 ref_cliptest[psize]( source, ref, rm, &rco, &rca, viewport_z_clip );
281
282 if ( mesa_profile ) {
283 BEGIN_RACE( *cycles );
284 func( source, dest, dm, &dco, &dca, viewport_z_clip );
285 END_RACE( *cycles );
286 }
287 else {
288 func( source, dest, dm, &dco, &dca, viewport_z_clip );
289 }
290
291 if ( dco != rco ) {
292 printf( "\n-----------------------------\n" );
293 printf( "dco = 0x%02x rco = 0x%02x\n", dco, rco );
294 return 0;
295 }
296 if ( dca != rca ) {
297 printf( "\n-----------------------------\n" );
298 printf( "dca = 0x%02x rca = 0x%02x\n", dca, rca );
299 return 0;
300 }
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.
308 */
309 continue;
310 }
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]);
315 return 0;
316 }
317 }
318
319 /* Only verify output on projected points4 case. FIXME: Do we need
320 * to test other cases?
321 */
322 if ( np || psize < 4 )
323 return 1;
324
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] ) );
343 return 0;
344 }
345 }
346 }
347
348 return 1;
349 }
350
351 void _math_test_all_cliptest_functions( char *description )
352 {
353 int np, psize;
354 long benchmark_tab[2][4];
355 static int first_time = 1;
356
357 if ( first_time ) {
358 first_time = 0;
359 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
360 }
361
362 #ifdef RUN_DEBUG_BENCHMARK
363 if ( mesa_profile ) {
364 if ( !counter_overhead ) {
365 INIT_COUNTER();
366 printf( "counter overhead: %ld cycles\n\n", counter_overhead );
367 }
368 printf( "cliptest results after hooking in %s functions:\n", description );
369 }
370 #endif
371
372 #ifdef RUN_DEBUG_BENCHMARK
373 if ( mesa_profile ) {
374 printf( "\n\t" );
375 for ( psize = 2 ; psize <= 4 ; psize++ ) {
376 printf( " p%d\t", psize );
377 }
378 printf( "\n--------------------------------------------------------\n\t" );
379 }
380 #endif
381
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]);
386
387 if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) {
388 char buf[100];
389 sprintf( buf, "%s[%d] failed test (%s)",
390 cnames[np], psize, description );
391 _mesa_problem( NULL, "%s", buf );
392 }
393 #ifdef RUN_DEBUG_BENCHMARK
394 if ( mesa_profile )
395 printf( " %li\t", benchmark_tab[np][psize-1] );
396 #endif
397 }
398 #ifdef RUN_DEBUG_BENCHMARK
399 if ( mesa_profile )
400 printf( " | [%s]\n\t", cstrings[np] );
401 #endif
402 }
403 #ifdef RUN_DEBUG_BENCHMARK
404 if ( mesa_profile )
405 printf( "\n" );
406 #endif
407 }
408
409
410 #endif /* DEBUG_MATH */