1848b8605Smrg/* 2848b8605Smrg * Mesa 3-D graphics library 3848b8605Smrg * 4848b8605Smrg * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 5848b8605Smrg * 6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7848b8605Smrg * copy of this software and associated documentation files (the "Software"), 8848b8605Smrg * to deal in the Software without restriction, including without limitation 9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the 11848b8605Smrg * Software is furnished to do so, subject to the following conditions: 12848b8605Smrg * 13848b8605Smrg * The above copyright notice and this permission notice shall be included 14848b8605Smrg * in all copies or substantial portions of the Software. 15848b8605Smrg * 16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE. 23848b8605Smrg * 24848b8605Smrg * Authors: 25848b8605Smrg * Gareth Hughes 26848b8605Smrg */ 27848b8605Smrg 28848b8605Smrg#include "main/glheader.h" 29848b8605Smrg#include "main/context.h" 30848b8605Smrg#include "main/macros.h" 31848b8605Smrg#include "main/imports.h" 32848b8605Smrg 33848b8605Smrg#include "m_matrix.h" 34848b8605Smrg#include "m_xform.h" 35848b8605Smrg 36848b8605Smrg#include "m_debug.h" 37848b8605Smrg#include "m_debug_util.h" 38848b8605Smrg 39848b8605Smrg#ifdef __UNIXOS2__ 40848b8605Smrg/* The linker doesn't like empty files */ 41848b8605Smrgstatic char dummy; 42848b8605Smrg#endif 43848b8605Smrg 44848b8605Smrg#ifdef DEBUG_MATH /* This code only used for debugging */ 45848b8605Smrg 46848b8605Smrgstatic clip_func *clip_tab[2] = { 47848b8605Smrg _mesa_clip_tab, 48848b8605Smrg _mesa_clip_np_tab 49848b8605Smrg}; 50848b8605Smrgstatic char *cnames[2] = { 51848b8605Smrg "_mesa_clip_tab", 52848b8605Smrg "_mesa_clip_np_tab" 53848b8605Smrg}; 54848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 55848b8605Smrgstatic char *cstrings[2] = { 56848b8605Smrg "clip, perspective divide", 57848b8605Smrg "clip, no divide" 58848b8605Smrg}; 59848b8605Smrg#endif 60848b8605Smrg 61848b8605Smrg 62848b8605Smrg/* ============================================================= 63848b8605Smrg * Reference cliptests 64848b8605Smrg */ 65848b8605Smrg 66848b8605Smrgstatic GLvector4f *ref_cliptest_points4( GLvector4f *clip_vec, 67848b8605Smrg GLvector4f *proj_vec, 68848b8605Smrg GLubyte clipMask[], 69848b8605Smrg GLubyte *orMask, 70848b8605Smrg GLubyte *andMask, 71848b8605Smrg GLboolean viewport_z_clip ) 72848b8605Smrg{ 73848b8605Smrg const GLuint stride = clip_vec->stride; 74848b8605Smrg const GLuint count = clip_vec->count; 75848b8605Smrg const GLfloat *from = (GLfloat *)clip_vec->start; 76848b8605Smrg GLuint c = 0; 77848b8605Smrg GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start; 78848b8605Smrg GLubyte tmpAndMask = *andMask; 79848b8605Smrg GLubyte tmpOrMask = *orMask; 80848b8605Smrg GLuint i; 81848b8605Smrg for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) { 82848b8605Smrg const GLfloat cx = from[0]; 83848b8605Smrg const GLfloat cy = from[1]; 84848b8605Smrg const GLfloat cz = from[2]; 85848b8605Smrg const GLfloat cw = from[3]; 86848b8605Smrg GLubyte mask = 0; 87848b8605Smrg if ( -cx + cw < 0 ) mask |= CLIP_RIGHT_BIT; 88848b8605Smrg if ( cx + cw < 0 ) mask |= CLIP_LEFT_BIT; 89848b8605Smrg if ( -cy + cw < 0 ) mask |= CLIP_TOP_BIT; 90848b8605Smrg if ( cy + cw < 0 ) mask |= CLIP_BOTTOM_BIT; 91848b8605Smrg if (viewport_z_clip) { 92848b8605Smrg if ( -cz + cw < 0 ) mask |= CLIP_FAR_BIT; 93848b8605Smrg if ( cz + cw < 0 ) mask |= CLIP_NEAR_BIT; 94848b8605Smrg } 95848b8605Smrg clipMask[i] = mask; 96848b8605Smrg if ( mask ) { 97848b8605Smrg c++; 98848b8605Smrg tmpAndMask &= mask; 99848b8605Smrg tmpOrMask |= mask; 100848b8605Smrg vProj[i][0] = 0; 101848b8605Smrg vProj[i][1] = 0; 102848b8605Smrg vProj[i][2] = 0; 103848b8605Smrg vProj[i][3] = 1; 104848b8605Smrg } else { 105848b8605Smrg GLfloat oow = 1.0F / cw; 106848b8605Smrg vProj[i][0] = cx * oow; 107848b8605Smrg vProj[i][1] = cy * oow; 108848b8605Smrg vProj[i][2] = cz * oow; 109848b8605Smrg vProj[i][3] = oow; 110848b8605Smrg } 111848b8605Smrg } 112848b8605Smrg 113848b8605Smrg *orMask = tmpOrMask; 114848b8605Smrg *andMask = (GLubyte) (c < count ? 0 : tmpAndMask); 115848b8605Smrg 116848b8605Smrg proj_vec->flags |= VEC_SIZE_4; 117848b8605Smrg proj_vec->size = 4; 118848b8605Smrg proj_vec->count = clip_vec->count; 119848b8605Smrg return proj_vec; 120848b8605Smrg} 121848b8605Smrg 122848b8605Smrg/* Keep these here for now, even though we don't use them... 123848b8605Smrg */ 124848b8605Smrgstatic GLvector4f *ref_cliptest_points3( GLvector4f *clip_vec, 125848b8605Smrg GLvector4f *proj_vec, 126848b8605Smrg GLubyte clipMask[], 127848b8605Smrg GLubyte *orMask, 128848b8605Smrg GLubyte *andMask, 129848b8605Smrg GLboolean viewport_z_clip ) 130848b8605Smrg{ 131848b8605Smrg const GLuint stride = clip_vec->stride; 132848b8605Smrg const GLuint count = clip_vec->count; 133848b8605Smrg const GLfloat *from = (GLfloat *)clip_vec->start; 134848b8605Smrg 135848b8605Smrg GLubyte tmpOrMask = *orMask; 136848b8605Smrg GLubyte tmpAndMask = *andMask; 137848b8605Smrg GLuint i; 138848b8605Smrg for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) { 139848b8605Smrg const GLfloat cx = from[0], cy = from[1], cz = from[2]; 140848b8605Smrg GLubyte mask = 0; 141848b8605Smrg if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT; 142848b8605Smrg else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT; 143848b8605Smrg if ( cy > 1.0 ) mask |= CLIP_TOP_BIT; 144848b8605Smrg else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT; 145848b8605Smrg if (viewport_z_clip) { 146848b8605Smrg if ( cz > 1.0 ) mask |= CLIP_FAR_BIT; 147848b8605Smrg else if ( cz < -1.0 ) mask |= CLIP_NEAR_BIT; 148848b8605Smrg } 149848b8605Smrg clipMask[i] = mask; 150848b8605Smrg tmpOrMask |= mask; 151848b8605Smrg tmpAndMask &= mask; 152848b8605Smrg } 153848b8605Smrg 154848b8605Smrg *orMask = tmpOrMask; 155848b8605Smrg *andMask = tmpAndMask; 156848b8605Smrg return clip_vec; 157848b8605Smrg} 158848b8605Smrg 159848b8605Smrgstatic GLvector4f * ref_cliptest_points2( GLvector4f *clip_vec, 160848b8605Smrg GLvector4f *proj_vec, 161848b8605Smrg GLubyte clipMask[], 162848b8605Smrg GLubyte *orMask, 163848b8605Smrg GLubyte *andMask, 164848b8605Smrg GLboolean viewport_z_clip ) 165848b8605Smrg{ 166848b8605Smrg const GLuint stride = clip_vec->stride; 167848b8605Smrg const GLuint count = clip_vec->count; 168848b8605Smrg const GLfloat *from = (GLfloat *)clip_vec->start; 169848b8605Smrg 170848b8605Smrg GLubyte tmpOrMask = *orMask; 171848b8605Smrg GLubyte tmpAndMask = *andMask; 172848b8605Smrg GLuint i; 173848b8605Smrg 174848b8605Smrg (void) viewport_z_clip; 175848b8605Smrg 176848b8605Smrg for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) { 177848b8605Smrg const GLfloat cx = from[0], cy = from[1]; 178848b8605Smrg GLubyte mask = 0; 179848b8605Smrg if ( cx > 1.0 ) mask |= CLIP_RIGHT_BIT; 180848b8605Smrg else if ( cx < -1.0 ) mask |= CLIP_LEFT_BIT; 181848b8605Smrg if ( cy > 1.0 ) mask |= CLIP_TOP_BIT; 182848b8605Smrg else if ( cy < -1.0 ) mask |= CLIP_BOTTOM_BIT; 183848b8605Smrg clipMask[i] = mask; 184848b8605Smrg tmpOrMask |= mask; 185848b8605Smrg tmpAndMask &= mask; 186848b8605Smrg } 187848b8605Smrg 188848b8605Smrg *orMask = tmpOrMask; 189848b8605Smrg *andMask = tmpAndMask; 190848b8605Smrg return clip_vec; 191848b8605Smrg} 192848b8605Smrg 193848b8605Smrgstatic clip_func ref_cliptest[5] = { 194848b8605Smrg 0, 195848b8605Smrg 0, 196848b8605Smrg ref_cliptest_points2, 197848b8605Smrg ref_cliptest_points3, 198848b8605Smrg ref_cliptest_points4 199848b8605Smrg}; 200848b8605Smrg 201848b8605Smrg 202848b8605Smrg/* ============================================================= 203848b8605Smrg * Cliptest tests 204848b8605Smrg */ 205848b8605Smrg 206848b8605SmrgALIGN16(static GLfloat, s[TEST_COUNT][4]); 207848b8605SmrgALIGN16(static GLfloat, d[TEST_COUNT][4]); 208848b8605SmrgALIGN16(static GLfloat, r[TEST_COUNT][4]); 209848b8605Smrg 210848b8605Smrg 211848b8605Smrg/** 212848b8605Smrg * Check if X, Y or Z component of the coordinate is close to W, in terms 213848b8605Smrg * of the clip test. 214848b8605Smrg */ 215848b8605Smrgstatic GLboolean 216848b8605Smrgxyz_close_to_w(const GLfloat c[4]) 217848b8605Smrg{ 218848b8605Smrg float k = 0.0001; 219848b8605Smrg return (fabs(c[0] - c[3]) < k || 220848b8605Smrg fabs(c[1] - c[3]) < k || 221848b8605Smrg fabs(c[2] - c[3]) < k || 222848b8605Smrg fabs(-c[0] - c[3]) < k || 223848b8605Smrg fabs(-c[1] - c[3]) < k || 224848b8605Smrg fabs(-c[2] - c[3]) < k); 225848b8605Smrg} 226848b8605Smrg 227848b8605Smrg 228848b8605Smrg 229848b8605Smrgstatic int test_cliptest_function( clip_func func, int np, 230848b8605Smrg int psize, long *cycles ) 231848b8605Smrg{ 232848b8605Smrg GLvector4f source[1], dest[1], ref[1]; 233848b8605Smrg GLubyte dm[TEST_COUNT], dco, dca; 234848b8605Smrg GLubyte rm[TEST_COUNT], rco, rca; 235848b8605Smrg int i, j; 236848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 237848b8605Smrg int cycle_i; /* the counter for the benchmarks we run */ 238848b8605Smrg#endif 239848b8605Smrg GLboolean viewport_z_clip = GL_TRUE; 240848b8605Smrg 241848b8605Smrg (void) cycles; 242848b8605Smrg 243848b8605Smrg if ( psize > 4 ) { 244848b8605Smrg _mesa_problem( NULL, "test_cliptest_function called with psize > 4\n" ); 245848b8605Smrg return 0; 246848b8605Smrg } 247848b8605Smrg 248848b8605Smrg for ( i = 0 ; i < TEST_COUNT ; i++) { 249848b8605Smrg ASSIGN_4V( d[i], 0.0, 0.0, 0.0, 1.0 ); 250848b8605Smrg ASSIGN_4V( s[i], 0.0, 0.0, 0.0, 1.0 ); 251848b8605Smrg for ( j = 0 ; j < psize ; j++ ) 252848b8605Smrg s[i][j] = rnd(); 253848b8605Smrg } 254848b8605Smrg 255848b8605Smrg source->data = (GLfloat(*)[4])s; 256848b8605Smrg source->start = (GLfloat *)s; 257848b8605Smrg source->count = TEST_COUNT; 258848b8605Smrg source->stride = sizeof(s[0]); 259848b8605Smrg source->size = 4; 260848b8605Smrg source->flags = 0; 261848b8605Smrg 262848b8605Smrg dest->data = (GLfloat(*)[4])d; 263848b8605Smrg dest->start = (GLfloat *)d; 264848b8605Smrg dest->count = TEST_COUNT; 265848b8605Smrg dest->stride = sizeof(float[4]); 266848b8605Smrg dest->size = 0; 267848b8605Smrg dest->flags = 0; 268848b8605Smrg 269848b8605Smrg ref->data = (GLfloat(*)[4])r; 270848b8605Smrg ref->start = (GLfloat *)r; 271848b8605Smrg ref->count = TEST_COUNT; 272848b8605Smrg ref->stride = sizeof(float[4]); 273848b8605Smrg ref->size = 0; 274848b8605Smrg ref->flags = 0; 275848b8605Smrg 276848b8605Smrg dco = rco = 0; 277848b8605Smrg dca = rca = CLIP_FRUSTUM_BITS; 278848b8605Smrg 279848b8605Smrg ref_cliptest[psize]( source, ref, rm, &rco, &rca, viewport_z_clip ); 280848b8605Smrg 281848b8605Smrg if ( mesa_profile ) { 282848b8605Smrg BEGIN_RACE( *cycles ); 283848b8605Smrg func( source, dest, dm, &dco, &dca, viewport_z_clip ); 284848b8605Smrg END_RACE( *cycles ); 285848b8605Smrg } 286848b8605Smrg else { 287848b8605Smrg func( source, dest, dm, &dco, &dca, viewport_z_clip ); 288848b8605Smrg } 289848b8605Smrg 290848b8605Smrg if ( dco != rco ) { 291848b8605Smrg printf( "\n-----------------------------\n" ); 292848b8605Smrg printf( "dco = 0x%02x rco = 0x%02x\n", dco, rco ); 293848b8605Smrg return 0; 294848b8605Smrg } 295848b8605Smrg if ( dca != rca ) { 296848b8605Smrg printf( "\n-----------------------------\n" ); 297848b8605Smrg printf( "dca = 0x%02x rca = 0x%02x\n", dca, rca ); 298848b8605Smrg return 0; 299848b8605Smrg } 300848b8605Smrg for ( i = 0 ; i < TEST_COUNT ; i++ ) { 301848b8605Smrg if ( dm[i] != rm[i] ) { 302848b8605Smrg GLfloat *c = source->start; 303848b8605Smrg STRIDE_F(c, source->stride * i); 304848b8605Smrg if (psize == 4 && xyz_close_to_w(c)) { 305848b8605Smrg /* The coordinate is very close to the clip plane. The clipmask 306848b8605Smrg * may vary depending on code path, but that's OK. 307848b8605Smrg */ 308848b8605Smrg continue; 309848b8605Smrg } 310848b8605Smrg printf( "\n-----------------------------\n" ); 311848b8605Smrg printf( "mask[%d] = 0x%02x ref mask[%d] = 0x%02x\n", i, dm[i], i,rm[i] ); 312848b8605Smrg printf(" coord = %f, %f, %f, %f\n", 313848b8605Smrg c[0], c[1], c[2], c[3]); 314848b8605Smrg return 0; 315848b8605Smrg } 316848b8605Smrg } 317848b8605Smrg 318848b8605Smrg /* Only verify output on projected points4 case. FIXME: Do we need 319848b8605Smrg * to test other cases? 320848b8605Smrg */ 321848b8605Smrg if ( np || psize < 4 ) 322848b8605Smrg return 1; 323848b8605Smrg 324848b8605Smrg for ( i = 0 ; i < TEST_COUNT ; i++ ) { 325848b8605Smrg for ( j = 0 ; j < 4 ; j++ ) { 326848b8605Smrg if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) { 327848b8605Smrg printf( "\n-----------------------------\n" ); 328848b8605Smrg printf( "(i = %i, j = %i) dm = 0x%02x rm = 0x%02x\n", 329848b8605Smrg i, j, dm[i], rm[i] ); 330848b8605Smrg printf( "%f \t %f \t [diff = %e - %i bit missed]\n", 331848b8605Smrg d[i][0], r[i][0], r[i][0]-d[i][0], 332848b8605Smrg MAX_PRECISION - significand_match( d[i][0], r[i][0] ) ); 333848b8605Smrg printf( "%f \t %f \t [diff = %e - %i bit missed]\n", 334848b8605Smrg d[i][1], r[i][1], r[i][1]-d[i][1], 335848b8605Smrg MAX_PRECISION - significand_match( d[i][1], r[i][1] ) ); 336848b8605Smrg printf( "%f \t %f \t [diff = %e - %i bit missed]\n", 337848b8605Smrg d[i][2], r[i][2], r[i][2]-d[i][2], 338848b8605Smrg MAX_PRECISION - significand_match( d[i][2], r[i][2] ) ); 339848b8605Smrg printf( "%f \t %f \t [diff = %e - %i bit missed]\n", 340848b8605Smrg d[i][3], r[i][3], r[i][3]-d[i][3], 341848b8605Smrg MAX_PRECISION - significand_match( d[i][3], r[i][3] ) ); 342848b8605Smrg return 0; 343848b8605Smrg } 344848b8605Smrg } 345848b8605Smrg } 346848b8605Smrg 347848b8605Smrg return 1; 348848b8605Smrg} 349848b8605Smrg 350848b8605Smrgvoid _math_test_all_cliptest_functions( char *description ) 351848b8605Smrg{ 352848b8605Smrg int np, psize; 353848b8605Smrg long benchmark_tab[2][4]; 354848b8605Smrg static int first_time = 1; 355848b8605Smrg 356848b8605Smrg if ( first_time ) { 357848b8605Smrg first_time = 0; 358b8e80941Smrg mesa_profile = getenv( "MESA_PROFILE" ); 359848b8605Smrg } 360848b8605Smrg 361848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 362848b8605Smrg if ( mesa_profile ) { 363848b8605Smrg if ( !counter_overhead ) { 364848b8605Smrg INIT_COUNTER(); 365848b8605Smrg printf( "counter overhead: %ld cycles\n\n", counter_overhead ); 366848b8605Smrg } 367848b8605Smrg printf( "cliptest results after hooking in %s functions:\n", description ); 368848b8605Smrg } 369848b8605Smrg#endif 370848b8605Smrg 371848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 372848b8605Smrg if ( mesa_profile ) { 373848b8605Smrg printf( "\n\t" ); 374848b8605Smrg for ( psize = 2 ; psize <= 4 ; psize++ ) { 375848b8605Smrg printf( " p%d\t", psize ); 376848b8605Smrg } 377848b8605Smrg printf( "\n--------------------------------------------------------\n\t" ); 378848b8605Smrg } 379848b8605Smrg#endif 380848b8605Smrg 381848b8605Smrg for ( np = 0 ; np < 2 ; np++ ) { 382848b8605Smrg for ( psize = 2 ; psize <= 4 ; psize++ ) { 383848b8605Smrg clip_func func = clip_tab[np][psize]; 384848b8605Smrg long *cycles = &(benchmark_tab[np][psize-1]); 385848b8605Smrg 386848b8605Smrg if ( test_cliptest_function( func, np, psize, cycles ) == 0 ) { 387848b8605Smrg char buf[100]; 388848b8605Smrg sprintf( buf, "%s[%d] failed test (%s)", 389848b8605Smrg cnames[np], psize, description ); 390848b8605Smrg _mesa_problem( NULL, "%s", buf ); 391848b8605Smrg } 392848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 393848b8605Smrg if ( mesa_profile ) 394848b8605Smrg printf( " %li\t", benchmark_tab[np][psize-1] ); 395848b8605Smrg#endif 396848b8605Smrg } 397848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 398848b8605Smrg if ( mesa_profile ) 399848b8605Smrg printf( " | [%s]\n\t", cstrings[np] ); 400848b8605Smrg#endif 401848b8605Smrg } 402848b8605Smrg#ifdef RUN_DEBUG_BENCHMARK 403848b8605Smrg if ( mesa_profile ) 404848b8605Smrg printf( "\n" ); 405848b8605Smrg#endif 406848b8605Smrg} 407848b8605Smrg 408848b8605Smrg 409848b8605Smrg#endif /* DEBUG_MATH */ 410