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