Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 338:8c68d9097846
prev331:a6048d3a9a79
next341:924029ff95ea
author nkeynes
date Mon Jan 29 11:24:44 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Get render size from the tile segment array
Set near clip to just 0 rather than scanning the scene
Fixup modulate RGB to force fragment alpha to 1.0
Add some debugging fprintfs
view annotate diff log raw
     1 /**
     2  * $Id: rendcore.c,v 1.15 2007-01-29 11:24:44 nkeynes Exp $
     3  *
     4  * PVR2 renderer core.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #include <sys/time.h>
    19 #include "pvr2/pvr2.h"
    20 #include "asic.h"
    22 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    23 				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    24 				      GL_ALWAYS };
    25 int pvr2_poly_srcblend[8] = { 
    26     GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    27     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    28     GL_ONE_MINUS_DST_ALPHA };
    29 int pvr2_poly_dstblend[8] = {
    30     GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    31     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    32     GL_ONE_MINUS_DST_ALPHA };
    33 int pvr2_poly_texblend[4] = {
    34     GL_REPLACE, 
    35     GL_MODULATE,  
    36     GL_DECAL, 
    37     GL_MODULATE 
    38 };
    39 int pvr2_render_colour_format[8] = {
    40     COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
    41     COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
    44 #define CULL_NONE 0
    45 #define CULL_SMALL 1
    46 #define CULL_CCW 2
    47 #define CULL_CW 3
    49 #define SEGMENT_END         0x80000000
    50 #define SEGMENT_ZCLEAR      0x40000000
    51 #define SEGMENT_SORT_TRANS  0x20000000
    52 #define SEGMENT_START       0x10000000
    53 #define SEGMENT_X(c)        (((c) >> 2) & 0x3F)
    54 #define SEGMENT_Y(c)        (((c) >> 8) & 0x3F)
    55 #define NO_POINTER          0x80000000
    57 extern char *video_base;
    59 gboolean pvr2_force_fragment_alpha = FALSE;
    60 gboolean pvr2_debug_render = FALSE;
    62 struct tile_segment {
    63     uint32_t control;
    64     pvraddr_t opaque_ptr;
    65     pvraddr_t opaquemod_ptr;
    66     pvraddr_t trans_ptr;
    67     pvraddr_t transmod_ptr;
    68     pvraddr_t punchout_ptr;
    69 };
    71 /**
    72  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    73  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    74  * TODO: Check the correctness of this.
    75  */
    76 float halftofloat( uint16_t half )
    77 {
    78     union {
    79         float f;
    80         uint32_t i;
    81     } temp;
    82     /* int e = ((half & 0x7C00) >> 10) - 15 + 127;
    84     temp.i = ((half & 0x8000) << 16) | (e << 23) |
    85     ((half & 0x03FF) << 13); */
    86     temp.i = ((uint32_t)half)<<16;
    87     return temp.f;
    88 }
    91 /**
    92  * Setup the GL context for the supplied polygon context.
    93  * @param context pointer to 3 or 5 words of polygon context
    94  * @param modified boolean flag indicating that the modified
    95  *  version should be used, rather than the normal version.
    96  */
    97 void render_set_context( uint32_t *context, int render_mode )
    98 {
    99     uint32_t poly1 = context[0], poly2, texture;
   100     if( render_mode == RENDER_FULLMOD ) {
   101 	poly2 = context[3];
   102 	texture = context[4];
   103     } else {
   104 	poly2 = context[1];
   105 	texture = context[2];
   106     }
   108     if( pvr2_debug_render ) {
   109 	fprintf( stderr, "Poly %08X %08X %08X\n", poly1, poly2, texture );
   110     }
   112     if( POLY1_DEPTH_ENABLE(poly1) ) {
   113 	glEnable( GL_DEPTH_TEST );
   114 	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   115     } else {
   116 	glDisable( GL_DEPTH_TEST );
   117     }
   119     switch( POLY1_CULL_MODE(poly1) ) {
   120     case CULL_NONE:
   121     case CULL_SMALL:
   122 	glDisable( GL_CULL_FACE );
   123 	break;
   124     case CULL_CCW:
   125 	glEnable( GL_CULL_FACE );
   126 	glFrontFace( GL_CW );
   127 	break;
   128     case CULL_CW:
   129 	glEnable( GL_CULL_FACE );
   130 	glFrontFace( GL_CCW );
   131 	break;
   132     }
   134     if( POLY1_SPECULAR(poly1) ) {
   135 	glEnable(GL_COLOR_SUM);
   136     } else {
   137 	glDisable(GL_COLOR_SUM);
   138     }
   140     pvr2_force_fragment_alpha = POLY2_ALPHA_ENABLE(poly2) ? FALSE : TRUE;
   142     if( POLY1_TEXTURED(poly1) ) {
   143 	int width = POLY2_TEX_WIDTH(poly2);
   144 	int height = POLY2_TEX_HEIGHT(poly2);
   145 	glEnable(GL_TEXTURE_2D);
   146 	texcache_get_texture( (texture&0x000FFFFF)<<3, width, height, texture );
   147 	switch( POLY2_TEX_BLEND(poly2) ) {
   148 	case 0: /* Replace */
   149 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   150 	    break;
   151 	case 2:/* Decal */
   152 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   153 	    break;
   154 	case 1: /* Modulate RGB */
   155 	    /* This is not directly supported by opengl (other than by mucking
   156 	     * with the texture format), but we get the same effect by forcing
   157 	     * the fragment alpha to 1.0 and using GL_MODULATE.
   158 	     */
   159 	    pvr2_force_fragment_alpha = TRUE;
   160 	case 3: /* Modulate RGBA */
   161 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   162 	    break;
   163 	}
   164 	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
   165 	if( POLY2_TEX_CLAMP_U(poly2) ) {
   166 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   167 	} else {
   168 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   169 	}	    
   170 	if( POLY2_TEX_CLAMP_V(poly2) ) {
   171 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   172 	} else {
   173 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   174 	}
   175     } else {
   176 	glDisable( GL_TEXTURE_2D );
   177     }
   179     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   181     int srcblend = POLY2_SRC_BLEND(poly2);
   182     int destblend = POLY2_DEST_BLEND(poly2);
   183     glBlendFunc( srcblend, destblend );
   185     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   186 	ERROR( "Accumulation buffer not supported" );
   187     }
   190 }
   192 #define FARGB_A(x) (((float)(((x)>>24)+1))/256.0)
   193 #define FARGB_R(x) (((float)((((x)>>16)&0xFF)+1))/256.0)
   194 #define FARGB_G(x) (((float)((((x)>>8)&0xFF)+1))/256.0)
   195 #define FARGB_B(x) (((float)(((x)&0xFF)+1))/256.0)
   197 void render_unpack_vertexes( struct vertex_unpacked *out, uint32_t poly1, 
   198 			     uint32_t *vertexes, int num_vertexes,
   199 			     int vertex_size, int render_mode )
   200 {
   201     int m = 0, i;
   202     if( render_mode == RENDER_FULLMOD ) {
   203 	m = (vertex_size - 3)/2;
   204     }
   206     for( i=0; i<num_vertexes; i++ ) {
   207 	float *vertexf = (float *)vertexes;
   208 	int k = m + 3;
   209 	out[i].x = vertexf[0];
   210 	out[i].y = vertexf[1];
   211 	out[i].z = vertexf[2];
   212     	if( POLY1_TEXTURED(poly1) ) {
   213 	    if( POLY1_UV16(poly1) ) {
   214 		out[i].u = halftofloat(vertexes[k]>>16);
   215 		out[i].v = halftofloat(vertexes[k]);
   216 		k++;
   217 	    } else {
   218 		out[i].u = vertexf[k];
   219 		out[i].v = vertexf[k+1];
   220 		k+=2;
   221 	    }
   222 	} else {
   223 	    out[i].u = 0;
   224 	    out[i].v = 0;
   225 	}
   226 	uint32_t argb = vertexes[k++];
   227 	out[i].rgba[0] = FARGB_R(argb);
   228 	out[i].rgba[1] = FARGB_G(argb);
   229         out[i].rgba[2] = FARGB_B(argb);
   230 	out[i].rgba[3] = FARGB_A(argb);
   231 	if( POLY1_SPECULAR(poly1) ) {
   232 	    uint32_t offset = vertexes[k++];
   233 	    out[i].offset_rgba[0] = FARGB_R(argb);
   234 	    out[i].offset_rgba[1] = FARGB_G(argb);
   235 	    out[i].offset_rgba[2] = FARGB_B(argb);
   236 	    out[i].offset_rgba[3] = FARGB_A(argb);
   237 	}
   238 	vertexes += vertex_size;
   239     }
   240 }
   242 /**
   243  * Unpack the vertexes for a quad, calculating the values for the last
   244  * vertex.
   245  * FIXME: Integrate this with rendbkg somehow
   246  */
   247 void render_unpack_quad( struct vertex_unpacked *unpacked, uint32_t poly1, 
   248 			 uint32_t *vertexes, int vertex_size,
   249 			 int render_mode )
   250 {
   251     int i;
   252     struct vertex_unpacked diff0, diff1;
   254     render_unpack_vertexes( unpacked, poly1, vertexes, 3, vertex_size, render_mode );
   256     diff0.x = unpacked[0].x - unpacked[1].x;
   257     diff0.y = unpacked[0].y - unpacked[1].y;
   258     diff1.x = unpacked[2].x - unpacked[1].x;
   259     diff1.y = unpacked[2].y - unpacked[1].y;
   261     float detxy = ((diff1.y) * (diff0.x)) - ((diff0.y) * (diff1.x));
   262     float *vertexf = (float *)(vertexes+(vertex_size*3));
   263     if( detxy == 0 ) {
   264 	memcpy( &unpacked[3], &unpacked[2], sizeof(struct vertex_unpacked) );
   265 	unpacked[3].x = vertexf[0];
   266 	unpacked[3].y = vertexf[1];
   267 	return;
   268     }	
   270     unpacked[3].x = vertexf[0];
   271     unpacked[3].y = vertexf[1];
   272     float t = ((unpacked[3].x - unpacked[1].x) * diff1.y -
   273 	       (unpacked[3].y - unpacked[1].y) * diff1.x) / detxy;
   274     float s = ((unpacked[3].y - unpacked[1].y) * diff0.x -
   275 	       (unpacked[3].x - unpacked[1].x) * diff0.y) / detxy;
   276     diff0.z = (1/unpacked[0].z) - (1/unpacked[1].z);
   277     diff1.z = (1/unpacked[2].z) - (1/unpacked[1].z);
   278     unpacked[3].z = 1/((1/unpacked[1].z) + (t*diff0.z) + (s*diff1.z));
   280     diff0.u = unpacked[0].u - unpacked[1].u;
   281     diff0.v = unpacked[0].v - unpacked[1].v;
   282     diff1.u = unpacked[2].u - unpacked[1].u;
   283     diff1.v = unpacked[2].v - unpacked[1].v;
   284     unpacked[3].u = unpacked[1].u + (t*diff0.u) + (s*diff1.u);
   285     unpacked[3].v = unpacked[1].v + (t*diff0.v) + (s*diff1.v);
   287     if( !POLY1_GOURAUD_SHADED(poly1) ) {
   288 	memcpy( unpacked[3].rgba, unpacked[2].rgba, sizeof(unpacked[2].rgba) );
   289 	memcpy( unpacked[3].offset_rgba, unpacked[2].offset_rgba, sizeof(unpacked[2].offset_rgba) );
   290     } else {
   291 	for( i=0; i<4; i++ ) {
   292 	    float d0 = unpacked[0].rgba[i] - unpacked[1].rgba[i];
   293 	    float d1 = unpacked[2].rgba[i] - unpacked[1].rgba[i];
   294 	    unpacked[3].rgba[i] = unpacked[1].rgba[i] + (t*d0) + (s*d1);
   295 	    d0 = unpacked[0].offset_rgba[i] - unpacked[1].offset_rgba[i];
   296 	    d1 = unpacked[2].offset_rgba[i] - unpacked[1].offset_rgba[i];
   297 	    unpacked[3].offset_rgba[i] = unpacked[1].offset_rgba[i] + (t*d0) + (s*d1);
   298 	}
   299     }    
   300 }
   302 void render_unpacked_vertex_array( uint32_t poly1, struct vertex_unpacked *vertexes[], 
   303 				   int num_vertexes ) {
   304     int i;
   306     glBegin( GL_TRIANGLE_STRIP );
   308     for( i=0; i<num_vertexes; i++ ) {
   309 	if( POLY1_TEXTURED(poly1) ) {
   310 	    glTexCoord2f( vertexes[i]->u, vertexes[i]->v );
   311 	}
   313 	if( pvr2_force_fragment_alpha ) {
   314 	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2], 1.0 );
   315 	} else {
   316 	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2],
   317 		       vertexes[i]->rgba[3] );
   318 	}
   319 	if( POLY1_SPECULAR(poly1) ) {
   320 	    glSecondaryColor3fEXT( vertexes[i]->offset_rgba[0],
   321 				   vertexes[i]->offset_rgba[1],
   322 				   vertexes[i]->offset_rgba[2] );
   323 	}
   324 	glVertex3f( vertexes[i]->x, vertexes[i]->y, 1/vertexes[i]->z );
   325     }
   327     glEnd();
   328 }
   330 void render_quad_vertexes( uint32_t poly1, uint32_t *vertexes, int vertex_size, int render_mode )
   331 {
   332     struct vertex_unpacked unpacked[4];
   333     struct vertex_unpacked *pt[4] = {&unpacked[0], &unpacked[1], &unpacked[3], &unpacked[2]};
   334     render_unpack_quad( unpacked, poly1, vertexes, vertex_size, render_mode );
   335     render_unpacked_vertex_array( poly1, pt, 4 );
   336 }
   338 void render_vertex_array( uint32_t poly1, uint32_t *vert_array[], int num_vertexes, int vertex_size,
   339 			  int render_mode ) 
   340 {
   341     int i, m=0;
   343     if( render_mode == RENDER_FULLMOD ) {
   344 	m = (vertex_size - 3)/2;
   345     }
   347     glBegin( GL_TRIANGLE_STRIP );
   349     for( i=0; i<num_vertexes; i++ ) {
   350 	uint32_t *vertexes = vert_array[i];
   351 	float *vertexf = (float *)vert_array[i];
   352 	uint32_t argb;
   353 	int k = m + 3;
   354 	if( POLY1_TEXTURED(poly1) ) {
   355 	    if( POLY1_UV16(poly1) ) {
   356 		glTexCoord2f( halftofloat(vertexes[k]>>16),
   357 			      halftofloat(vertexes[k]) );
   358 		k++;
   359 	    } else {
   360 		glTexCoord2f( vertexf[k], vertexf[k+1] );
   361 		k+=2;
   362 	    }
   363 	}
   365 	argb = vertexes[k++];
   366 	if( pvr2_force_fragment_alpha ) {
   367 	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
   368 			(GLubyte)argb, 0xFF );
   369 	} else {
   370 	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
   371 			(GLubyte)argb, (GLubyte)(argb >> 24) );
   372 	}
   374 	if( POLY1_SPECULAR(poly1) ) {
   375 	    uint32_t spec = vertexes[k++];
   376 	    glSecondaryColor3ubEXT( (GLubyte)(spec >> 16), (GLubyte)(spec >> 8), 
   377 				 (GLubyte)spec );
   378 	}
   379 	glVertex3f( vertexf[0], vertexf[1], 1/vertexf[2] );
   380 	vertexes += vertex_size;
   381     }
   383     glEnd();
   384 }
   386 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
   387 		      int render_mode )
   388 {
   389     uint32_t *vert_array[num_vertexes];
   390     int i;
   391     for( i=0; i<num_vertexes; i++ ) {
   392 	vert_array[i] = vertexes;
   393 	vertexes += vertex_size;
   394     }
   395     render_vertex_array( poly1, vert_array, num_vertexes, vertex_size, render_mode );
   396 }
   398 /**
   399  * Render a simple (not auto-sorted) tile
   400  */
   401 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   402     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
   403     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   404     do {
   405 	uint32_t entry = *tile_list++;
   406 	if( entry >> 28 == 0x0F ) {
   407 	    break;
   408 	} else if( entry >> 28 == 0x0E ) {
   409 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   410 	} else {
   411 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
   412 	    int is_modified = entry & 0x01000000;
   413 	    int vertex_length = (entry >> 21) & 0x07;
   414 	    int context_length = 3;
   415 	    if( is_modified && !cheap_modifier_mode ) {
   416 		context_length = 5;
   417 		vertex_length *= 2 ;
   418 	    }
   419 	    vertex_length += 3;
   421 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   422 		/* Triangle(s) */
   423 		int strip_count = ((entry >> 25) & 0x0F)+1;
   424 		int polygon_length = 3 * vertex_length + context_length;
   425 		int i;
   426 		for( i=0; i<strip_count; i++ ) {
   427 		    render_set_context( polygon, render_mode );
   428 		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
   429 				     render_mode );
   430 		    polygon += polygon_length;
   431 		}
   432 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   433 		/* Sprite(s) */
   434 		int strip_count = ((entry >> 25) & 0x0F)+1;
   435 		int polygon_length = 4 * vertex_length + context_length;
   436 		int i;
   437 		for( i=0; i<strip_count; i++ ) {
   438 		    render_set_context( polygon, render_mode );
   439 		    render_quad_vertexes( *polygon, polygon+context_length, vertex_length,
   440 					  render_mode );
   441 		    polygon += polygon_length;
   442 		}
   443 	    } else {
   444 		/* Polygon */
   445 		int i, first=-1, last = -1;
   446 		for( i=0; i<6; i++ ) {
   447 		    if( entry & (0x40000000>>i) ) {
   448 			if( first == -1 ) first = i;
   449 			last = i;
   450 		    }
   451 		}
   452 		if( first != -1 ) {
   453 		    first = 0;
   454 		    render_set_context(polygon, render_mode);
   455 		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
   456 				     (last-first+3), vertex_length, render_mode );
   457 		}
   458 	    }
   459 	}
   460     } while( 1 );
   461 }
   463 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
   464 			int clipx2, int clipy2 ) {
   466     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   467     int tile_sort;
   468     gboolean cheap_shadow;
   470     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   471     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   472     int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
   474     if( (obj_config & 0x00200000) == 0 ) {
   475 	if( isp_config & 1 ) {
   476 	    tile_sort = 0;
   477 	} else {
   478 	    tile_sort = 2;
   479 	}
   480     } else {
   481 	tile_sort = 1;
   482     }
   484     cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
   486     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   488     struct timeval tv_start, tv_end;
   489     gettimeofday(&tv_start, NULL);
   490     glEnable( GL_SCISSOR_TEST );
   491     do {
   492 	// fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
   493 	int tilex = SEGMENT_X(segment->control);
   494 	int tiley = SEGMENT_Y(segment->control);
   496 	int x1 = tilex << 5;
   497 	int y1 = tiley << 5;
   498 	if( x1 + 32 <= clipx1 ||
   499 	    y1 + 32 <= clipy1 ||
   500 	    x1 >= clipx2 ||
   501 	    y1 >= clipy2 ) {
   502 	    /* Tile completely clipped, skip */
   503 	    continue;
   504 	}
   506 	/* Set a scissor on the visible part of the tile */
   507 	int w = MIN(x1+32, clipx2) - x1;
   508 	int h = MIN(y1+32, clipy2) - y1;
   509 	x1 = MAX(x1,clipx1);
   510 	y1 = MAX(y1,clipy1);
   511 	glScissor( x1, height-y1-h, w, h );
   513 	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
   514 	    if( pvr2_debug_render ) {
   515 		fprintf( stderr, "Tile %d,%d Opaque\n", tilex, tiley );
   516 	    }
   517 	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
   518 		/* TODO */
   519 	    }
   520 	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
   521 	}
   523 	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
   524 	    if( pvr2_debug_render ) {
   525 		fprintf( stderr, "Tile %d,%d Trans\n", tilex, tiley );
   526 	    }
   527 	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
   528 		/* TODO */
   529 	    } 
   530 	    if( tile_sort == 2 || 
   531 		(tile_sort == 1 && ((segment->control & SEGMENT_SORT_TRANS)==0)) ) {
   532 		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   533 	    } else {
   534 		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   535 	    }
   536 	}
   538 	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
   539 	    if( pvr2_debug_render ) {
   540 		fprintf( stderr, "Tile %d,%d Punchout\n", tilex, tiley );
   541 	    }
   542 	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
   543 	}
   544     } while( ((segment++)->control & SEGMENT_END) == 0 );
   545     glDisable( GL_SCISSOR_TEST );
   547     gettimeofday(&tv_end, NULL);
   548     timersub(&tv_end,&tv_start, &tv_start);
   549 }
   551 static float render_find_maximum_tile_z( pvraddr_t tile_entry, float inputz )
   552 {
   553     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
   554     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   555     int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100;
   556     int i, j;
   557     float z = inputz;
   558     do {
   559 	uint32_t entry = *tile_list++;
   560 	if( entry >> 28 == 0x0F ) {
   561 	    break;
   562 	} else if( entry >> 28 == 0x0E ) {
   563 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   564 	} else {
   565 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
   566 	    int is_modified = entry & 0x01000000;
   567 	    int vertex_length = (entry >> 21) & 0x07;
   568 	    int context_length = 3;
   569 	    if( (entry & 0x01000000) && shadow_cfg ) {
   570 		context_length = 5;
   571 		vertex_length *= 2 ;
   572 	    }
   573 	    vertex_length += 3;
   574 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   575 		/* Triangle(s) */
   576 		int strip_count = ((entry >> 25) & 0x0F)+1;
   577 		float *vertexz = (float *)(polygon+context_length+2);
   578 		for( i=0; i<strip_count; i++ ) {
   579 		    for( j=0; j<3; j++ ) {
   580 			if( *vertexz > z ) {
   581 			    z = *vertexz;
   582 			}
   583 			vertexz += vertex_length;
   584 		    }
   585 		    vertexz += context_length;
   586 		}
   587 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   588 		/* Sprite(s) */
   589 		int strip_count = ((entry >> 25) & 0x0F)+1;
   590 		int polygon_length = 4 * vertex_length + context_length;
   591 		int i;
   592 		float *vertexz = (float *)(polygon+context_length+2);
   593 		for( i=0; i<strip_count; i++ ) {
   594 		    for( j=0; j<4; j++ ) {
   595 			if( *vertexz > z ) {
   596 			    z = *vertexz;
   597 			}
   598 			vertexz += vertex_length;
   599 		    }
   600 		    vertexz+=context_length;
   601 		}
   602 	    } else {
   603 		/* Polygon */
   604 		int i, first=-1, last = -1;
   605 		float *vertexz = (float *)polygon+context_length+2;
   606 		for( i=0; i<6; i++ ) {
   607 		    if( (entry & (0x40000000>>i)) && *vertexz > z ) {
   608 			z = *vertexz;
   609 		    }
   610 		    vertexz += vertex_length;
   611 		}
   612 	    }
   613 	}
   614     } while(1);
   615     return z;
   616 }
   618 /**
   619  * Scan through the scene to determine the largest z value (in order to set up
   620  * an appropriate near clip plane).
   621  */
   622 float pvr2_render_find_maximum_z( )
   623 {
   624     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   625     float maximumz = MMIO_READF( PVR2, RENDER_FARCLIP ); /* Initialize to the far clip plane */
   627     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   628     do {
   630 	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
   631 	    maximumz = render_find_maximum_tile_z(segment->opaque_ptr, maximumz);
   632 	}
   633 	if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
   634 	    maximumz = render_find_maximum_tile_z(segment->opaquemod_ptr, maximumz);
   635 	}
   636 	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
   637 	    maximumz = render_find_maximum_tile_z(segment->trans_ptr, maximumz);
   638 	}
   639 	if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
   640 	    maximumz = render_find_maximum_tile_z(segment->transmod_ptr, maximumz);
   641 	}
   642 	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
   643 	    maximumz = render_find_maximum_tile_z(segment->punchout_ptr, maximumz);
   644 	}
   646     } while( ((segment++)->control & SEGMENT_END) == 0 );
   648     return 1/maximumz;
   649 }
   651 /**
   652  * Scan the segment info to determine the width and height of the render (in 
   653  * pixels).
   654  * @param x,y output values to receive the width and height info.
   655  */
   656 void pvr2_render_getsize( int *x, int *y ) 
   657 {
   658     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   659     int maxx = 0, maxy = 0;
   661     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   662     do {
   663 	int tilex = SEGMENT_X(segment->control);
   664 	int tiley = SEGMENT_Y(segment->control);
   665 	if( tilex > maxx ) {
   666 	    maxx = tilex;
   667 	} 
   668 	if( tiley > maxy ) {
   669 	    maxy = tiley;
   670 	}
   671     } while( ((segment++)->control & SEGMENT_END) == 0 );
   673     *x = (maxx+1)<<5;
   674     *y = (maxy+1)<<5;
   675 }
.