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