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