Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 667:0e1ac8da75d9
prev653:3202ff01d48e
next669:ab344e42bca9
author nkeynes
date Sat Apr 19 02:39:37 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Centralize gl ext checks in glutil.c
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Manage the internal vertex/polygon buffers and scene data structure. 
     5  * Where possible this uses VBOs for the vertex + index data.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <assert.h>
    21 #include <string.h>
    22 #include <math.h>
    23 #include "lxdream.h"
    24 #include "display.h"
    25 #include "pvr2/pvr2.h"
    26 #include "pvr2/glutil.h"
    27 #include "pvr2/scene.h"
    30 static inline uint32_t bgra_to_rgba(uint32_t bgra)
    31 {
    32     return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
    33 }
    35 /**
    36  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    37  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    38  * TODO: Check the correctness of this.
    39  */
    40 static float halftofloat( uint16_t half )
    41 {
    42     union {
    43         float f;
    44         uint32_t i;
    45     } temp;
    46     temp.i = ((uint32_t)half)<<16;
    47     return temp.f;
    48 }
    54 struct pvr2_scene_struct pvr2_scene;
    56 static gboolean vbo_init = FALSE;
    57 static gboolean vbo_supported = FALSE;
    59 /**
    60  * Test for VBO support, and allocate all the system memory needed for the
    61  * temporary structures. GL context must have been initialized before this
    62  * point.
    63  */
    64 void pvr2_scene_init()
    65 {
    66     if( !vbo_init ) {
    67 #ifdef ENABLE_VERTEX_BUFFER
    68 	if( isGLVertexBufferSupported() ) {
    69 	    vbo_supported = TRUE;
    70 	    pvr2_scene.vbo_id = 1;
    71 	}
    72 #endif
    73 	pvr2_scene.vertex_array = NULL;
    74 	pvr2_scene.vertex_array_size = 0;
    75 	pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
    76 	pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
    77 	vbo_init = TRUE;
    78     }
    79 }
    81 /**
    82  * Clear the scene data structures in preparation for fresh data
    83  */
    84 void pvr2_scene_reset()
    85 {
    86     pvr2_scene.poly_count = 0;
    87     pvr2_scene.vertex_count = 0;
    88     memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
    89 }
    91 void pvr2_scene_shutdown()
    92 {
    93 #ifdef ENABLE_VERTEX_BUFFER
    94     if( vbo_supported ) {
    95 	glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
    96 	glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
    97 	pvr2_scene.vbo_id = 0;
    98     } else {
    99 #endif
   100 	g_free( pvr2_scene.vertex_array );
   101 	pvr2_scene.vertex_array = NULL;
   102 #ifdef ENABLE_VERTEX_BUFFER
   103     }
   104 #endif
   106     g_free( pvr2_scene.poly_array );
   107     pvr2_scene.poly_array = NULL;
   108     g_free( pvr2_scene.buf_to_poly_map );
   109     pvr2_scene.buf_to_poly_map = NULL;
   110     vbo_init = FALSE;
   111 }
   113 void *vertex_buffer_map()
   114 {
   115     glGetError();
   116     uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
   117 #ifdef ENABLE_VERTEX_BUFFER
   118     if( vbo_supported ) {
   119 	glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
   120 	if( size > pvr2_scene.vertex_array_size ) {
   121 	    glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
   122 	    int status = glGetError();
   123 	    if( status != 0 ) {
   124 		fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
   125 		abort();
   126 	    }
   127 	    pvr2_scene.vertex_array_size = size;
   128 	}
   129 	pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
   130 	assert(pvr2_scene.vertex_array != NULL );
   131     } else {
   132 #endif
   133 	if( size > pvr2_scene.vertex_array_size ) {
   134 	    pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
   135 	}
   136 #ifdef ENABLE_VERTEX_BUFFER
   137     }
   138 #endif
   139     return pvr2_scene.vertex_array;
   140 }
   142 gboolean vertex_buffer_unmap()
   143 {
   144 #ifdef ENABLE_VERTEX_BUFFER
   145     if( vbo_supported ) {
   146 	pvr2_scene.vertex_array = NULL;
   147 	return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
   148     } else {
   149 	return TRUE;
   150     }
   151 #else
   152     return TRUE;
   153 #endif
   154 }
   156 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
   157 							 gboolean is_modified ) 
   158 {
   159     int vert_mul = is_modified ? 2 : 1;
   161     if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
   162 	if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
   163 	    pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
   164 	    pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
   165 	}
   166 	return pvr2_scene.buf_to_poly_map[poly_idx];
   167     } else {
   168 	struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   169 	poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
   170 	poly->vertex_count = vertex_count;
   171 	poly->vertex_index = -1;
   172 	poly->mod_vertex_index = -1;
   173 	poly->next = NULL;
   174 	pvr2_scene.buf_to_poly_map[poly_idx] = poly;
   175 	pvr2_scene.vertex_count += (vertex_count * vert_mul);
   176 	return poly;
   177     }
   178 }
   180 /**
   181  * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
   182  * volume)
   183  * @param vert Pointer to output vertex structure
   184  * @param poly1 First word of polygon context (needed to understand vertex)
   185  * @param poly2 Second word of polygon context
   186  * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
   187  * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
   188  *        the normal vertex, half the vertex length for the modified vertex.
   189  */
   190 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1, 
   191 				       uint32_t poly2, uint32_t *pvr2_data, 
   192 				       int modify_offset )
   193 {
   194     gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
   195     union pvr2_data_type {
   196 	uint32_t *ival;
   197 	float *fval;
   198     } data;
   200     data.ival = pvr2_data;
   202     vert->x = *data.fval++;
   203     vert->y = *data.fval++;
   205     float z = *data.fval++;
   206     if( !isfinite(z) ) {
   207 	z = 0;
   208     } else if( z != 0 ) {
   209 	z = 1/z;
   210     }
   211     if( z > pvr2_scene.bounds[5] ) {
   212 	pvr2_scene.bounds[5] = z;
   213     } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
   214 	pvr2_scene.bounds[4] = z;
   215     }
   216     vert->z = z;
   217     data.ival += modify_offset;
   220     if( POLY1_TEXTURED(poly1) ) {
   221 	if( POLY1_UV16(poly1) ) {
   222 	    vert->u = halftofloat( *data.ival>>16 );
   223 	    vert->v = halftofloat( *data.ival );
   224 	    data.ival++;
   225 	} else {
   226 	    vert->u = *data.fval++;
   227 	    vert->v = *data.fval++;
   228 	}
   229 	if( POLY2_TEX_BLEND(poly2) == 1 ) {
   230 	    force_alpha = TRUE;
   231 	}
   232     }
   233     if( force_alpha ) {
   234 	vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
   235 	if( POLY1_SPECULAR(poly1) ) {
   236 	    vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
   237 	} else {
   238 	    vert->offset_rgba = 0;
   239 	}
   240     } else {
   241 	vert->rgba = bgra_to_rgba(*data.ival++);
   242 	if( POLY1_SPECULAR(poly1) ) {
   243 	    vert->offset_rgba = bgra_to_rgba(*data.ival++);
   244 	} else {
   245 	    vert->offset_rgba = 0;
   246 	}
   247     }
   248 }
   250 /**
   251  * Compute texture, colour, and z values for a result point by interpolating from
   252  * a set of 3 input points. The result point must define its x,y.
   253  */
   254 static void scene_compute_vertex( struct vertex_struct *result, 
   255 					  struct vertex_struct *input,
   256 					  gboolean is_solid_shaded )
   257 {
   258     int i;
   259     float sx = input[2].x - input[1].x;
   260     float sy = input[2].y - input[1].y;
   261     float tx = input[0].x - input[1].x;
   262     float ty = input[0].y - input[1].y;
   264     float detxy = ((sy) * (tx)) - ((ty) * (sx));
   265     if( detxy == 0 ) {
   266 	result->z = input[2].z;
   267 	result->u = input[2].u;
   268 	result->v = input[2].v;
   269 	result->rgba = input[2].rgba;
   270 	result->offset_rgba = input[2].offset_rgba;
   271 	return;
   272     }
   273     float t = ((result->x - input[1].x) * sy -
   274 	       (result->y - input[1].y) * sx) / detxy;
   275     float s = ((result->y - input[1].y) * tx -
   276 	       (result->x - input[1].x) * ty) / detxy;
   278     float sz = input[2].z - input[1].z;
   279     float tz = input[0].z - input[1].z;
   280     float su = input[2].u - input[1].u;
   281     float tu = input[0].u - input[1].u;
   282     float sv = input[2].v - input[1].v;
   283     float tv = input[0].v - input[1].v;
   285     float rz = input[1].z + (t*tz) + (s*sz);
   286     if( rz > pvr2_scene.bounds[5] ) {
   287 	pvr2_scene.bounds[5] = rz;
   288     } else if( rz < pvr2_scene.bounds[4] ) {
   289 	pvr2_scene.bounds[4] = rz; 
   290     }
   291     result->z = rz;
   292     result->u = input[1].u + (t*tu) + (s*su);
   293     result->v = input[1].v + (t*tv) + (s*sv);
   295     if( is_solid_shaded ) {
   296 	result->rgba = input[2].rgba;
   297 	result->offset_rgba = input[2].offset_rgba;
   298     } else {
   299 	uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
   300 	uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
   301 	uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
   302 	uint8_t *rgba3 = (uint8_t *)&result->rgba;
   303 	for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
   304 	    float tc = *rgba0++ - *rgba1;
   305 	    float sc = *rgba2++ - *rgba1;
   306 	    float rc = *rgba1++ + (t*tc) + (s*sc);
   307 	    if( rc < 0 ) {
   308 		rc = 0;
   309 	    } else if( rc > 255 ) {
   310 		rc = 255;
   311 	    }
   312 	    *rgba3++ = rc;
   313 	}
   314     }    
   316 }
   318 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
   319 					gboolean is_modified )
   320 {
   321     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   322     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   323     uint32_t *context = ptr;
   324     unsigned int i;
   326     if( poly->vertex_index == -1 ) {
   327 	ptr += (is_modified ? 5 : 3 );
   328 	poly->vertex_index = pvr2_scene.vertex_index;
   330 	assert( poly != NULL );
   331 	assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   332 	for( i=0; i<poly->vertex_count; i++ ) {
   333 	    pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
   334 	    ptr += vertex_length;
   335 	}
   336 	if( is_modified ) {
   337 	    int mod_offset = (vertex_length - 3)>>1;
   338 	    assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   339 	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   340 	    poly->mod_vertex_index = pvr2_scene.vertex_index;
   341 	    for( i=0; i<poly->vertex_count; i++ ) {
   342 		pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
   343 		ptr += vertex_length;
   344 	    }
   345 	}
   346     }
   347 }
   349 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length, 
   350 					     gboolean is_modified )
   351 {
   352     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   353     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   354     uint32_t *context = ptr;
   355     unsigned int i;
   357     if( poly->vertex_index == -1 ) {
   358 	// Construct it locally and copy to the vertex buffer, as the VBO is 
   359 	// allowed to be horribly slow for reads (ie it could be direct-mapped
   360 	// vram).
   361 	struct vertex_struct quad[4];
   363 	assert( poly != NULL );
   364 	assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   365 	ptr += (is_modified ? 5 : 3 );
   366 	poly->vertex_index = pvr2_scene.vertex_index;
   367 	for( i=0; i<4; i++ ) {
   368 	    pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
   369 	    ptr += vertex_length;
   370 	}
   371 	scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   372 	// Swap last two vertexes (quad arrangement => tri strip arrangement)
   373 	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   374 	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   375 	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   376 	pvr2_scene.vertex_index += 4;
   378 	if( is_modified ) {
   379 	    int mod_offset = (vertex_length - 3)>>1;
   380 	    assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   381 	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   382 	    poly->mod_vertex_index = pvr2_scene.vertex_index;
   383 	    for( i=0; i<4; i++ ) {
   384 		pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
   385 		ptr += vertex_length;
   386 	    }
   387 	    scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   388 	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   389 	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   390 	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   391 	    pvr2_scene.vertex_index += 4;
   392 	}
   393     }
   394 }
   396 static void scene_extract_polygons( pvraddr_t tile_entry )
   397 {
   398     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   399     do {
   400 	uint32_t entry = *tile_list++;
   401 	if( entry >> 28 == 0x0F ) {
   402 	    break;
   403 	} else if( entry >> 28 == 0x0E ) {
   404 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   405 	} else {
   406 	    pvraddr_t polyaddr = entry&0x000FFFFF;
   407 	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
   408 	    int vertex_length = (entry >> 21) & 0x07;
   409 	    int context_length = 3;
   410 	    if( is_modified ) {
   411 		context_length = 5;
   412 		vertex_length <<= 1 ;
   413 	    }
   414 	    vertex_length += 3;
   416 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   417 		/* Triangle(s) */
   418 		int strip_count = ((entry >> 25) & 0x0F)+1;
   419 		int polygon_length = 3 * vertex_length + context_length;
   420 		int i;
   421 		struct polygon_struct *last_poly = NULL;
   422 		for( i=0; i<strip_count; i++ ) {
   423 		    struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
   424 		    polyaddr += polygon_length;
   425 		    if( last_poly != NULL && last_poly->next == NULL ) {
   426 			last_poly->next = poly;
   427 		    }
   428 		    last_poly = poly;
   429 		}
   430 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   431 		/* Sprite(s) */
   432 		int strip_count = ((entry >> 25) & 0x0F)+1;
   433 		int polygon_length = 4 * vertex_length + context_length;
   434 		int i;
   435 		struct polygon_struct *last_poly = NULL;
   436 		for( i=0; i<strip_count; i++ ) {
   437 		    struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
   438 		    polyaddr += polygon_length;
   439 		    if( last_poly != NULL && last_poly->next == NULL ) {
   440 			last_poly->next = poly;
   441 		    }
   442 		    last_poly = poly;
   443 		}
   444 	    } else {
   445 		/* Polygon */
   446 		int i, last = -1;
   447 		for( i=5; i>=0; i-- ) {
   448 		    if( entry & (0x40000000>>i) ) {
   449 			last = i;
   450 			break;
   451 		    }
   452 		}
   453 		if( last != -1 ) {
   454 		    scene_add_polygon( polyaddr, last+3, is_modified );
   455 		}
   456 	    }
   457 	}
   458     } while( 1 );
   459 }
   461 static void scene_extract_vertexes( pvraddr_t tile_entry )
   462 {
   463     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   464     do {
   465 	uint32_t entry = *tile_list++;
   466 	if( entry >> 28 == 0x0F ) {
   467 	    break;
   468 	} else if( entry >> 28 == 0x0E ) {
   469 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   470 	} else {
   471 	    pvraddr_t polyaddr = entry&0x000FFFFF;
   472 	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
   473 	    int vertex_length = (entry >> 21) & 0x07;
   474 	    int context_length = 3;
   475 	    if( is_modified ) {
   476 		context_length = 5;
   477 		vertex_length <<=1 ;
   478 	    }
   479 	    vertex_length += 3;
   481 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   482 		/* Triangle(s) */
   483 		int strip_count = ((entry >> 25) & 0x0F)+1;
   484 		int polygon_length = 3 * vertex_length + context_length;
   485 		int i;
   486 		for( i=0; i<strip_count; i++ ) {
   487 		    scene_add_vertexes( polyaddr, vertex_length, is_modified );
   488 		    polyaddr += polygon_length;
   489 		}
   490 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   491 		/* Sprite(s) */
   492 		int strip_count = ((entry >> 25) & 0x0F)+1;
   493 		int polygon_length = 4 * vertex_length + context_length;
   494 		int i;
   495 		for( i=0; i<strip_count; i++ ) {
   496 		    scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
   497 		    polyaddr += polygon_length;
   498 		}
   499 	    } else {
   500 		/* Polygon */
   501 		int i, last = -1;
   502 		for( i=5; i>=0; i-- ) {
   503 		    if( entry & (0x40000000>>i) ) {
   504 			last = i;
   505 			break;
   506 		    }
   507 		}
   508 		if( last != -1 ) {
   509 		    scene_add_vertexes( polyaddr, vertex_length, is_modified );
   510 		}
   511 	    }
   512 	}
   513     } while( 1 );    
   514 }
   516 uint32_t pvr2_scene_buffer_width()
   517 {
   518     return pvr2_scene.buffer_width;
   519 }
   521 uint32_t pvr2_scene_buffer_height()
   522 {
   523     return pvr2_scene.buffer_height;
   524 }
   526 /**
   527  * Extract the current scene into the rendering structures. We run two passes
   528  * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts), 
   529  * second pass extracts the vertex data into the VBO/vertex array.
   530  *
   531  * Difficult to do in single pass as we don't generally know the size of a 
   532  * polygon for certain until we've seen all tiles containing it. It also means we
   533  * can count the vertexes and allocate the appropriate size VBO.
   534  *
   535  * FIXME: accesses into VRAM need to be bounds-checked properly
   536  */
   537 void pvr2_scene_read( void )
   538 {
   539     pvr2_scene_init();
   540     pvr2_scene_reset();
   542     pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
   543     pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
   544     pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
   545     pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
   546     pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
   548     uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
   549     uint32_t *segment = tilebuffer;
   550     pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
   551     pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
   552     pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
   554     int max_tile_x = 0;
   555     int max_tile_y = 0;
   556     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   557     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   559     if( (obj_config & 0x00200000) == 0 ) {
   560 	if( isp_config & 1 ) {
   561 	    pvr2_scene.sort_mode = SORT_NEVER;
   562 	} else {
   563 	    pvr2_scene.sort_mode = SORT_ALWAYS;
   564 	}
   565     } else {
   566 	pvr2_scene.sort_mode = SORT_TILEFLAG;
   567     }
   569     // Pass 1: Extract polygon list 
   570     uint32_t control;
   571     int i;
   572     do {
   573 	control = *segment++;
   574 	int tile_x = SEGMENT_X(control);
   575 	int tile_y = SEGMENT_Y(control);
   576 	if( tile_x > max_tile_x ) {
   577 	    max_tile_x = tile_x;
   578 	} 
   579 	if( tile_y > max_tile_y ) {
   580 	    max_tile_y = tile_y;
   581 	}
   582 	for( i=0; i<5; i++ ) {
   583 	    if( (*segment & NO_POINTER) == 0 ) {
   584 		scene_extract_polygons( *segment );
   585 	    }
   586 	    segment++;
   587 	}
   588     } while( (control & SEGMENT_END) == 0 );
   590     pvr2_scene.buffer_width = (max_tile_x+1)<<5;
   591     pvr2_scene.buffer_height = (max_tile_y+1)<<5;
   593     if( pvr2_scene.vertex_count > 0 ) {
   594 	// Pass 2: Extract vertex data
   595 	vertex_buffer_map();
   596 	pvr2_scene.vertex_index = 0;
   597 	segment = tilebuffer;
   598 	do {
   599 	    control = *segment++;
   600 	    for( i=0; i<5; i++ ) {
   601 		if( (*segment & NO_POINTER) == 0 ) {
   602 		    scene_extract_vertexes( *segment );
   603 		}
   604 		segment++;
   605 	    }
   606 	} while( (control & SEGMENT_END) == 0 );
   607 	vertex_buffer_unmap();
   608     }
   609 }
   611 /**
   612  * Dump the current scene to file in a (mostly) human readable form
   613  */
   614 void pvr2_scene_dump( FILE *f )
   615 {
   616     int i,j;
   618     fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
   619     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   620 	struct polygon_struct *poly = &pvr2_scene.poly_array[i];
   621 	fprintf( f, "  %08X ", ((char *)poly->context) - video_base );
   622 	switch( poly->vertex_count ) {
   623 	case 3: fprintf( f, "Tri     " ); break;
   624 	case 4: fprintf( f, "Quad    " ); break;
   625 	default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
   626 	}
   627 	fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
   628 	if( poly->mod_vertex_index != -1 ) {
   629 	    fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
   630 	} else {
   631 	    fprintf( f, "\n" );
   632 	}
   634 	for( j=0; j<poly->vertex_count; j++ ) {
   635 	    struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
   636 	    fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
   637 		     v->rgba, v->offset_rgba );
   638 	}
   639 	if( poly->mod_vertex_index != -1 ) {
   640 	    fprintf( f, "  ---\n" );
   641 	    for( j=0; j<poly->vertex_count; j++ ) {
   642 		struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
   643 		fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
   644 			 v->rgba, v->offset_rgba );
   645 	    }
   646 	}
   647     }
   649 }
.