[MESA]
[reactos.git] / reactos / dll / opengl / 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 {
72 const GLuint stride = clip_vec->stride;
73 const GLuint count = clip_vec->count;
74 const GLfloat *from = (GLfloat *)clip_vec->start;
75 GLuint c = 0;
76 GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
77 GLubyte tmpAndMask = *andMask;
78 GLubyte tmpOrMask = *orMask;
79 GLuint i;
80 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
81 const GLfloat cx = from[0];
82 const GLfloat cy = from[1];
83 const GLfloat cz = from[2];
84 const GLfloat cw = from[3];
85 GLubyte mask = 0;
86 if ( -cx + cw < 0 ) mask |= CLIP_RIGHT_BIT;
87 if ( cx + cw < 0 ) mask |= CLIP_LEFT_BIT;
88 if ( -cy + cw < 0 ) mask |= CLIP_TOP_BIT;
89 if ( cy + cw < 0 ) mask |= CLIP_BOTTOM_BIT;
90 if ( -cz + cw < 0 ) mask |= CLIP_FAR_BIT;
91 if ( cz + cw < 0 ) mask |= CLIP_NEAR_BIT;
92 clipMask[i] = mask;
93 if ( mask ) {
94 c++;
95 tmpAndMask &= mask;
96 tmpOrMask |= mask;
97 vProj[i][0] = 0;
98 vProj[i][1] = 0;
99 vProj[i][2] = 0;
100 vProj[i][3] = 1;
101 } else {
102 GLfloat oow = 1.0F / cw;
103 vProj[i][0] = cx * oow;
104 vProj[i][1] = cy * oow;
105 vProj[i][2] = cz * oow;
106 vProj[i][3] = oow;
107 }
108 }
109
110 *orMask = tmpOrMask;
111 *andMask = (GLubyte) (c < count ? 0 : tmpAndMask);
112
113 proj_vec->flags |= VEC_SIZE_4;
114 proj_vec->size = 4;
115 proj_vec->count = clip_vec->count;
116 return proj_vec;
117 }
118
119 /* Keep these here for now, even though we don't use them...
120 */
121 static GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec,
122 GLvector4f *proj_vec,
123 GLubyte clipMask[],
124 GLubyte *orMask,
125 GLubyte *andMask)
126 {
127 const GLuint stride = clip_vec->stride;
128 const GLuint count = clip_vec->count;
129 const GLfloat *from = (GLfloat *)clip_vec->start;
130
131 GLubyte tmpOrMask = *orMask;
132 GLubyte tmpAndMask = *andMask;
133 GLuint i;
134 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
135 const GLfloat cx = from[0], cy = from[1], cz = from[2];
136 GLubyte mask = 0;
137 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
138 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
139 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
140 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
141 if ( cz > 1.0 ) mask |= CLIP_FAR_BIT;
142 else if ( cz < -1.0 ) mask |= CLIP_NEAR_BIT;
143 clipMask[i] = mask;
144 tmpOrMask |= mask;
145 tmpAndMask &= mask;
146 }
147
148 *orMask = tmpOrMask;
149 *andMask = tmpAndMask;
150 return clip_vec;
151 }
152
153 static GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec,
154 GLvector4f *proj_vec,
155 GLubyte clipMask[],
156 GLubyte *orMask,
157 GLubyte *andMask,
158 GLboolean viewport_z_clip )
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
168 (void) viewport_z_clip;
169
170 for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) {
171 const GLfloat cx = from[0], cy = from[1];
172 GLubyte mask = 0;
173 if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT;
174 else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT;
175 if ( cy > 1.0 ) mask |= CLIP_TOP_BIT;
176 else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT;
177 clipMask[i] = mask;
178 tmpOrMask |= mask;
179 tmpAndMask &= mask;
180 }
181
182 *orMask = tmpOrMask;
183 *andMask = tmpAndMask;
184 return clip_vec;
185 }
186
187 static clip_func ref_cliptest[5] = {
188 0,
189 0,
190 ref_cliptest_points2,
191 ref_cliptest_points3,
192 ref_cliptest_points4
193 };
194
195
196 /* =============================================================
197 * Cliptest tests
198 */
199
200 ALIGN16(static GLfloat, s[TEST_COUNT][4]);
201 ALIGN16(static GLfloat, d[TEST_COUNT][4]);
202 ALIGN16(static GLfloat, r[TEST_COUNT][4]);
203
204
205 /**
206 * Check if X, Y or Z component of the coordinate is close to W, in terms
207 * of the clip test.
208 */
209 static GLboolean
210 xyz_close_to_w(const GLfloat c[4])
211 {
212 float k = 0.0001;
213 return (fabs(c[0] - c[3]) < k ||
214 fabs(c[1] - c[3]) < k ||
215 fabs(c[2] - c[3]) < k ||
216 fabs(-c[0] - c[3]) < k ||
217 fabs(-c[1] - c[3]) < k ||
218 fabs(-c[2] - c[3]) < k);
219 }
220
221
222
223 static int test_cliptest_function( clip_func func, int np,
224 int psize, long *cycles )
225 {
226 GLvector4f source[1], dest[1], ref[1];
227 GLubyte dm[TEST_COUNT], dco, dca;
228 GLubyte rm[TEST_COUNT], rco, rca;
229 int i, j;
230 #ifdef RUN_DEBUG_BENCHMARK
231 int cycle_i; /* the counter for the benchmarks we run */
232 #endif
233 GLboolean viewport_z_clip = GL_TRUE;
234
235 (void) cycles;
236
237 if ( psize > 4 ) {
238 _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" );
239 return 0;
240 }
241
242 for ( i = 0 ; i < TEST_COUNT ; i++) {
243 ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 );
244 ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 );
245 for ( j = 0 ; j < psize ; j++ )
246 s[i][j] = rnd();
247 }
248
249 source->data = (GLfloat(*)[4])s;
250 source->start = (GLfloat *)s;
251 source->count = TEST_COUNT;
252 source->stride = sizeof(s[0]);
253 source->size = 4;
254 source->flags = 0;
255
256 dest->data = (GLfloat(*)[4])d;
257 dest->start = (GLfloat *)d;
258 dest->count = TEST_COUNT;
259 dest->stride = sizeof(float[4]);
260 dest->size = 0;
261 dest->flags = 0;
262
263 ref->data = (GLfloat(*)[4])r;
264 ref->start = (GLfloat *)r;
265 ref->count = TEST_COUNT;
266 ref->stride = sizeof(float[4]);
267 ref->size = 0;
268 ref->flags = 0;
269
270 dco = rco = 0;
271 dca = rca = CLIP_FRUSTUM_BITS;
272
273 ref_cliptest[psize]( source, ref, rm, &rco, &rca, viewport_z_clip );
274
275 if ( mesa_profile ) {
276 BEGIN_RACE( *cycles );
277 func( source, dest, dm, &dco, &dca, viewport_z_clip );
278 END_RACE( *cycles );
279 }
280 else {
281 func( source, dest, dm, &dco, &dca, viewport_z_clip );
282 }
283
284 if ( dco != rco ) {
285 printf( "\n-----------------------------\n" );
286 printf( "dco = 0x%02x rco = 0x%02x\n", dco, rco );
287 return 0;
288 }
289 if ( dca != rca ) {
290 printf( "\n-----------------------------\n" );
291 printf( "dca = 0x%02x rca = 0x%02x\n", dca, rca );
292 return 0;
293 }
294 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
295 if ( dm[i] != rm[i] ) {
296 GLfloat *c = source->start;
297 STRIDE_F(c, source->stride * i);
298 if (psize == 4 && xyz_close_to_w(c)) {
299 /* The coordinate is very close to the clip plane. The clipmask
300 * may vary depending on code path, but that's OK.
301 */
302 continue;
303 }
304 printf( "\n-----------------------------\n" );
305 printf( "mask[%d] = 0x%02x ref mask[%d] = 0x%02x\n", i, dm[i], i,rm[i] );
306 printf(" coord = %f, %f, %f, %f\n",
307 c[0], c[1], c[2], c[3]);
308 return 0;
309 }
310 }
311
312 /* Only verify output on projected points4 case. FIXME: Do we need
313 * to test other cases?
314 */
315 if ( np || psize < 4 )
316 return 1;
317
318 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
319 for ( j = 0 ; j < 4 ; j++ ) {
320 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
321 printf( "\n-----------------------------\n" );
322 printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n",
323 i, j, dm[i], rm[i] );
324 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
325 d[i][0], r[i][0], r[i][0]-d[i][0],
326 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
327 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
328 d[i][1], r[i][1], r[i][1]-d[i][1],
329 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
330 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
331 d[i][2], r[i][2], r[i][2]-d[i][2],
332 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
333 printf( "%f \t %f \t [diff = %e - %i bit missed]\n",
334 d[i][3], r[i][3], r[i][3]-d[i][3],
335 MAX_PRECISION - significand_match( d[i][3], r[i][3] ) );
336 return 0;
337 }
338 }
339 }
340
341 return 1;
342 }
343
344 void _math_test_all_cliptest_functions( char *description )
345 {
346 int np, psize;
347 long benchmark_tab[2][4];
348 static int first_time = 1;
349
350 if ( first_time ) {
351 first_time = 0;
352 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
353 }
354
355 #ifdef RUN_DEBUG_BENCHMARK
356 if ( mesa_profile ) {
357 if ( !counter_overhead ) {
358 INIT_COUNTER();
359 printf( "counter overhead: %ld cycles\n\n", counter_overhead );
360 }
361 printf( "cliptest results after hooking in %s functions:\n", description );
362 }
363 #endif
364
365 #ifdef RUN_DEBUG_BENCHMARK
366 if ( mesa_profile ) {
367 printf( "\n\t" );
368 for ( psize = 2 ; psize <= 4 ; psize++ ) {
369 printf( " p%d\t", psize );
370 }
371 printf( "\n--------------------------------------------------------\n\t" );
372 }
373 #endif
374
375 for ( np = 0 ; np < 2 ; np++ ) {
376 for ( psize = 2 ; psize <= 4 ; psize++ ) {
377 clip_func func = clip_tab[np][psize];
378 long *cycles = &(benchmark_tab[np][psize-1]);
379
380 if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) {
381 char buf[100];
382 sprintf( buf, "%s[%d] failed test (%s)",
383 cnames[np], psize, description );
384 _mesa_problem( NULL, "%s", buf );
385 }
386 #ifdef RUN_DEBUG_BENCHMARK
387 if ( mesa_profile )
388 printf( " %li\t", benchmark_tab[np][psize-1] );
389 #endif
390 }
391 #ifdef RUN_DEBUG_BENCHMARK
392 if ( mesa_profile )
393 printf( " | [%s]\n\t", cstrings[np] );
394 #endif
395 }
396 #ifdef RUN_DEBUG_BENCHMARK
397 if ( mesa_profile )
398 printf( "\n" );
399 #endif
400 }
401
402
403 #endif /* DEBUG_MATH */