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