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