aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/math/m_debug_clip.c
blob: c74ca5f7e451de22cbfe89bc0287f0ed5c0d98e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

/*
 * Mesa 3-D graphics library
 * Version:  5.0
 *
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Gareth Hughes
 */

#include "glheader.h"
#include "context.h"
#include "macros.h"
#include "imports.h"

#include "m_matrix.h"
#include "m_xform.h"

#include "m_debug.h"
#include "m_debug_util.h"

#ifdef __UNIXOS2__
/* The linker doesn't like empty files */
static char dummy;
#endif

#ifdef DEBUG  /* This code only used for debugging */

static clip_func *clip_tab[2] = {
   _mesa_clip_tab,
   _mesa_clip_np_tab
};
static char *cnames[2] = {
   "_mesa_clip_tab",
   "_mesa_clip_np_tab"
};
#ifdef RUN_DEBUG_BENCHMARK
static char *cstrings[2] = {
   "clip, perspective divide",
   "clip, no divide"
};
#endif


/* =============================================================
 * Reference cliptests
 */

static GLvector4f *ref_cliptest_points4( GLvector4f *clip_vec,
					 GLvector4f *proj_vec,
					 GLubyte clipMask[],
					 GLubyte *orMask,
					 GLubyte *andMask )
{
   const GLuint stride = clip_vec->stride;
   const GLuint count = clip_vec->count;
   const GLfloat *from = (GLfloat *)clip_vec->start;
   GLuint c = 0;
   GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
   GLubyte tmpAndMask = *andMask;
   GLubyte tmpOrMask = *orMask;
   GLuint i;
   for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
      const GLfloat cx = from[0];
      const GLfloat cy = from[1];
      const GLfloat cz = from[2];
      const GLfloat cw = from[3];
      GLubyte mask = 0;
      if ( -cx + cw < 0 ) mask |= CLIP_RIGHT_BIT;
      if (  cx + cw < 0 ) mask |= CLIP_LEFT_BIT;
      if ( -cy + cw < 0 ) mask |= CLIP_TOP_BIT;
      if (  cy + cw < 0 ) mask |= CLIP_BOTTOM_BIT;
      if ( -cz + cw < 0 ) mask |= CLIP_FAR_BIT;
      if (  cz + cw < 0 ) mask |= CLIP_NEAR_BIT;
      clipMask[i] = mask;
      if ( mask ) {
	 c++;
	 tmpAndMask &= mask;
	 tmpOrMask |= mask;
	 vProj[i][0] = 0;
	 vProj[i][1] = 0;
	 vProj[i][2] = 0;
	 vProj[i][3] = 1;
      } else {
	 GLfloat oow = 1.0F / cw;
	 vProj[i][0] = cx * oow;
	 vProj[i][1] = cy * oow;
	 vProj[i][2] = cz * oow;
	 vProj[i][3] = oow;
      }
   }

   *orMask = tmpOrMask;
   *andMask = (GLubyte) (c < count ? 0 : tmpAndMask);

   proj_vec->flags |= VEC_SIZE_4;
   proj_vec->size = 4;
   proj_vec->count = clip_vec->count;
   return proj_vec;
}

/* Keep these here for now, even though we don't use them...
 */
static GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec,
					 GLvector4f *proj_vec,
					 GLubyte clipMask[],
					 GLubyte *orMask,
					 GLubyte *andMask )
{
   const GLuint stride = clip_vec->stride;
   const GLuint count = clip_vec->count;
   const GLfloat *from = (GLfloat *)clip_vec->start;

   GLubyte tmpOrMask = *orMask;
   GLubyte tmpAndMask = *andMask;
   GLuint i;
   for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
      const GLfloat cx = from[0], cy = from[1], cz = from[2];
      GLubyte mask = 0;
      if ( cx >  1.0 )		mask |= CLIP_RIGHT_BIT;
      else if ( cx < -1.0 )	mask |= CLIP_LEFT_BIT;
      if ( cy >  1.0 )		mask |= CLIP_TOP_BIT;
      else if ( cy < -1.0 )	mask |= CLIP_BOTTOM_BIT;
      if ( cz >  1.0 )		mask |= CLIP_FAR_BIT;
      else if ( cz < -1.0 )	mask |= CLIP_NEAR_BIT;
      clipMask[i] = mask;
      tmpOrMask |= mask;
      tmpAndMask &= mask;
   }

   *orMask = tmpOrMask;
   *andMask = tmpAndMask;
   return clip_vec;
}

static GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec,
					  GLvector4f *proj_vec,
					  GLubyte clipMask[],
					  GLubyte *orMask,
					  GLubyte *andMask )
{
   const GLuint stride = clip_vec->stride;
   const GLuint count = clip_vec->count;
   const GLfloat *from = (GLfloat *)clip_vec->start;

   GLubyte tmpOrMask = *orMask;
   GLubyte tmpAndMask = *andMask;
   GLuint i;
   for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
      const GLfloat cx = from[0], cy = from[1];
      GLubyte mask = 0;
      if ( cx >  1.0 )		mask |= CLIP_RIGHT_BIT;
      else if ( cx < -1.0 )	mask |= CLIP_LEFT_BIT;
      if ( cy >  1.0 )		mask |= CLIP_TOP_BIT;
      else if ( cy < -1.0 )	mask |= CLIP_BOTTOM_BIT;
      clipMask[i] = mask;
      tmpOrMask |= mask;
      tmpAndMask &= mask;
   }

   *orMask = tmpOrMask;
   *andMask = tmpAndMask;
   return clip_vec;
}

static clip_func ref_cliptest[5] = {
   0,
   0,
   ref_cliptest_points2,
   ref_cliptest_points3,
   ref_cliptest_points4
};


/* =============================================================
 * Cliptest tests
 */

static GLfloat s[TEST_COUNT][4] ALIGN16;
static GLfloat d[TEST_COUNT][4] ALIGN16;
static GLfloat r[TEST_COUNT][4] ALIGN16;

static int test_cliptest_function( clip_func func, int np,
				   int psize, long *cycles )
{
   GLvector4f source[1], dest[1], ref[1];
   GLubyte dm[TEST_COUNT], dco, dca;
   GLubyte rm[TEST_COUNT], rco, rca;
   int i, j;
#ifdef  RUN_DEBUG_BENCHMARK
   int cycle_i;                /* the counter for the benchmarks we run */
#endif

   (void) cycles;

   if ( psize > 4 ) {
      _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" );
      return 0;
   }

   for ( i = 0 ; i < TEST_COUNT ; i++) {
      ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
      ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
      for ( j = 0 ; j < psize ; j++ )
         s[i][j] = rnd();
   }

   source->data = (GLfloat(*)[4])s;
   source->start = (GLfloat *)s;
   source->count = TEST_COUNT;
   source->stride = sizeof(s[0]);
   source->size = 4;
   source->flags = 0;

   dest->data = (GLfloat(*)[4])d;
   dest->start = (GLfloat *)d;
   dest->count = TEST_COUNT;
   dest->stride = sizeof(float[4]);
   dest->size = 0;
   dest->flags = 0;

   ref->data = (GLfloat(*)[4])r;
   ref->start = (GLfloat *)r;
   ref->count = TEST_COUNT;
   ref->stride = sizeof(float[4]);
   ref->size = 0;
   ref->flags = 0;

   dco = rco = 0;
   dca = rca = CLIP_ALL_BITS;

   ref_cliptest[psize]( source, ref, rm, &rco, &rca );

   if ( mesa_profile ) {
      BEGIN_RACE( *cycles );
      func( source, dest, dm, &dco, &dca );
      END_RACE( *cycles );
   }
   else {
      func( source, dest, dm, &dco, &dca );
   }

   if ( dco != rco ) {
      _mesa_printf( "\n-----------------------------\n" );
      _mesa_printf( "dco = 0x%02x   rco = 0x%02x\n", dco, rco );
      return 0;
   }
   if ( dca != rca ) {
      _mesa_printf( "\n-----------------------------\n" );
      _mesa_printf( "dca = 0x%02x   rca = 0x%02x\n", dca, rca );
      return 0;
   }
   for ( i = 0 ; i < TEST_COUNT ; i++ ) {
      if ( dm[i] != rm[i] ) {
	 _mesa_printf( "\n-----------------------------\n" );
	 _mesa_printf( "(i = %i)\n", i );
	 _mesa_printf( "dm = 0x%02x   rm = 0x%02x\n", dm[i], rm[i] );
	 return 0;
      }
   }

   /* Only verify output on projected points4 case.  FIXME: Do we need
    * to test other cases?
    */
   if ( np || psize < 4 )
      return 1;

   for ( i = 0 ; i < TEST_COUNT ; i++ ) {
      for ( j = 0 ; j < 4 ; j++ ) {
         if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
            _mesa_printf( "\n-----------------------------\n" );
            _mesa_printf( "(i = %i, j = %i)  dm = 0x%02x   rm = 0x%02x\n",
		    i, j, dm[i], rm[i] );
            _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
		    d[i][0], r[i][0], r[i][0]-d[i][0],
		    MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
            _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
		    d[i][1], r[i][1], r[i][1]-d[i][1],
		    MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
            _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
		    d[i][2], r[i][2], r[i][2]-d[i][2],
		    MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
            _mesa_printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
		    d[i][3], r[i][3], r[i][3]-d[i][3],
		    MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
            return 0;
         }
      }
   }

   return 1;
}

void _math_test_all_cliptest_functions( char *description )
{
   int np, psize;
   long benchmark_tab[2][4];
   static int first_time = 1;

   if ( first_time ) {
      first_time = 0;
      mesa_profile = _mesa_getenv( "MESA_PROFILE" );
   }

#ifdef RUN_DEBUG_BENCHMARK
   if ( mesa_profile ) {
      if ( !counter_overhead ) {
	 INIT_COUNTER();
	 _mesa_printf( "counter overhead: %ld cycles\n\n", counter_overhead );
      }
      _mesa_printf( "cliptest results after hooking in %s functions:\n", description );
   }
#endif

#ifdef RUN_DEBUG_BENCHMARK
   if ( mesa_profile ) {
      _mesa_printf( "\n\t" );
      for ( psize = 2 ; psize <= 4 ; psize++ ) {
	 _mesa_printf( " p%d\t", psize );
      }
      _mesa_printf( "\n--------------------------------------------------------\n\t" );
   }
#endif

   for ( np = 0 ; np < 2 ; np++ ) {
      for ( psize = 2 ; psize <= 4 ; psize++ ) {
	 clip_func func = clip_tab[np][psize];
	 long *cycles = &(benchmark_tab[np][psize-1]);

	 if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) {
	    char buf[100];
	    _mesa_sprintf( buf, "%s[%d] failed test (%s)",
		     cnames[np], psize, description );
	    _mesa_problem( NULL, buf );
	 }
#ifdef RUN_DEBUG_BENCHMARK
	 if ( mesa_profile )
	    _mesa_printf( " %li\t", benchmark_tab[np][psize-1] );
#endif
      }
#ifdef RUN_DEBUG_BENCHMARK
      if ( mesa_profile )
	 _mesa_printf( " | [%s]\n\t", cstrings[np] );
#endif
   }
#ifdef RUN_DEBUG_BENCHMARK
   if ( mesa_profile )
      _mesa_printf( "\n" );
#endif
}


#endif /* DEBUG */