Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 847:2089244671d2
prev827:d333f4248727
next863:a5e5310061e2
author nkeynes
date Thu Sep 11 22:51:24 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Completely untested render-to-texture work in progress
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 }
    58 static float parse_fog_density( uint32_t value )
    59 {
    60     union {
    61         uint32_t i;
    62         float f;
    63     } u;
    64     u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
    65     return u.f;
    66 }
    69 struct pvr2_scene_struct pvr2_scene;
    71 static gboolean vbo_init = FALSE;
    73 #ifdef ENABLE_VERTEX_BUFFER
    74 static gboolean vbo_supported = FALSE;
    75 #endif
    77 /**
    78  * Test for VBO support, and allocate all the system memory needed for the
    79  * temporary structures. GL context must have been initialized before this
    80  * point.
    81  */
    82 void pvr2_scene_init()
    83 {
    84     if( !vbo_init ) {
    85 #ifdef ENABLE_VERTEX_BUFFER
    86         if( isGLVertexBufferSupported() ) {
    87             vbo_supported = TRUE;
    88             pvr2_scene.vbo_id = 1;
    89         }
    90 #endif
    91         pvr2_scene.vertex_array = NULL;
    92         pvr2_scene.vertex_array_size = 0;
    93         pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
    94         pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
    95         vbo_init = TRUE;
    96     }
    97 }
    99 /**
   100  * Clear the scene data structures in preparation for fresh data
   101  */
   102 void pvr2_scene_reset()
   103 {
   104     pvr2_scene.poly_count = 0;
   105     pvr2_scene.vertex_count = 0;
   106     memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
   107 }
   109 void pvr2_scene_shutdown()
   110 {
   111 #ifdef ENABLE_VERTEX_BUFFER
   112     if( vbo_supported ) {
   113         glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
   114         glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
   115         pvr2_scene.vbo_id = 0;
   116     } else {
   117 #endif
   118         g_free( pvr2_scene.vertex_array );
   119         pvr2_scene.vertex_array = NULL;
   120 #ifdef ENABLE_VERTEX_BUFFER
   121     }
   122 #endif
   124     g_free( pvr2_scene.poly_array );
   125     pvr2_scene.poly_array = NULL;
   126     g_free( pvr2_scene.buf_to_poly_map );
   127     pvr2_scene.buf_to_poly_map = NULL;
   128     vbo_init = FALSE;
   129 }
   131 void *vertex_buffer_map()
   132 {
   133     // Allow 8 vertexes for the background (4+4)
   134     uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
   135 #ifdef ENABLE_VERTEX_BUFFER
   136     if( vbo_supported ) {
   137         glGetError();
   138         glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
   139         if( size > pvr2_scene.vertex_array_size ) {
   140             glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
   141             int status = glGetError();
   142             if( status != 0 ) {
   143                 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
   144                 abort();
   145             }
   146             pvr2_scene.vertex_array_size = size;
   147         }
   148         pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
   149         assert(pvr2_scene.vertex_array != NULL );
   150     } else {
   151 #endif
   152         if( size > pvr2_scene.vertex_array_size ) {
   153             pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
   154         }
   155 #ifdef ENABLE_VERTEX_BUFFER
   156     }
   157 #endif
   158     return pvr2_scene.vertex_array;
   159 }
   161 gboolean vertex_buffer_unmap()
   162 {
   163 #ifdef ENABLE_VERTEX_BUFFER
   164     if( vbo_supported ) {
   165         pvr2_scene.vertex_array = NULL;
   166         return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
   167     } else {
   168         return TRUE;
   169     }
   170 #else
   171     return TRUE;
   172 #endif
   173 }
   175 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
   176                                                  gboolean is_modified )
   177 {
   178     int vert_mul = is_modified ? 2 : 1;
   180     if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
   181         if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
   182             pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
   183             pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
   184         }
   185         return pvr2_scene.buf_to_poly_map[poly_idx];
   186     } else {
   187         struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   188         poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
   189         poly->vertex_count = vertex_count;
   190         poly->vertex_index = -1;
   191         poly->mod_vertex_index = -1;
   192         poly->next = NULL;
   193         pvr2_scene.buf_to_poly_map[poly_idx] = poly;
   194         pvr2_scene.vertex_count += (vertex_count * vert_mul);
   195         return poly;
   196     }
   197 }
   199 /**
   200  * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
   201  * volume)
   202  * @param vert Pointer to output vertex structure
   203  * @param poly1 First word of polygon context (needed to understand vertex)
   204  * @param poly2 Second word of polygon context
   205  * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
   206  * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
   207  *        the normal vertex, half the vertex length for the modified vertex.
   208  */
   209 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
   210                                        uint32_t poly2, uint32_t *pvr2_data,
   211                                        int modify_offset )
   212 {
   213     gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
   214     union pvr2_data_type {
   215         uint32_t *ival;
   216         float *fval;
   217     } data;
   219     data.ival = pvr2_data;
   221     vert->x = *data.fval++;
   222     vert->y = *data.fval++;
   224     float z = *data.fval++;
   225     if( !isfinite(z) ) {
   226         z = 0;
   227     } else if( z != 0 ) {
   228         z = 1/z;
   229     }
   230     if( z > pvr2_scene.bounds[5] ) {
   231         pvr2_scene.bounds[5] = z;
   232     } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
   233         pvr2_scene.bounds[4] = z;
   234     }
   235     vert->z = z;
   236     data.ival += modify_offset;
   239     if( POLY1_TEXTURED(poly1) ) {
   240         if( POLY1_UV16(poly1) ) {
   241             vert->u = halftofloat( *data.ival>>16 );
   242             vert->v = halftofloat( *data.ival );
   243             data.ival++;
   244         } else {
   245             vert->u = *data.fval++;
   246             vert->v = *data.fval++;
   247         }
   248         if( POLY2_TEX_BLEND(poly2) == 1 ) {
   249             force_alpha = TRUE;
   250         }
   251     }
   252     unpack_bgra(*data.ival++, vert->rgba);
   253     if( POLY1_SPECULAR(poly1) ) {
   254         unpack_bgra(*data.ival++, vert->offset_rgba);
   255     } else {
   256         vert->offset_rgba[0] = 0.0;
   257         vert->offset_rgba[1] = 0.0;
   258         vert->offset_rgba[2] = 0.0;
   259         vert->offset_rgba[3] = 0.0;
   260     }
   262     if( force_alpha ) {
   263         vert->rgba[3] = 1.0;
   264     }
   265 }
   267 /**
   268  * Compute texture, colour, and z values for 1 or more result points by interpolating from
   269  * a set of 3 input points. The result point(s) must define their x,y.
   270  */
   271 static void scene_compute_vertexes( struct vertex_struct *result,
   272                                     int result_count,
   273                                     struct vertex_struct *input,
   274                                     gboolean is_solid_shaded )
   275 {
   276     int i,j;
   277     float sx = input[2].x - input[1].x;
   278     float sy = input[2].y - input[1].y;
   279     float tx = input[0].x - input[1].x;
   280     float ty = input[0].y - input[1].y;
   282     float detxy = ((sy) * (tx)) - ((ty) * (sx));
   283     if( detxy == 0 ) {
   284         // If the input points fall on a line, they don't define a usable
   285         // polygon - the PVR2 takes the last input point as the result in
   286         // this case.
   287         for( i=0; i<result_count; i++ ) {
   288             float x = result[i].x;
   289             float y = result[i].y;
   290             memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
   291             result[i].x = x;
   292             result[i].y = y;
   293         }
   294         return;
   295     }
   296     float sz = input[2].z - input[1].z;
   297     float tz = input[0].z - input[1].z;
   298     float su = input[2].u - input[1].u;
   299     float tu = input[0].u - input[1].u;
   300     float sv = input[2].v - input[1].v;
   301     float tv = input[0].v - input[1].v;
   303     for( i=0; i<result_count; i++ ) {
   304         float t = ((result[i].x - input[1].x) * sy -
   305                 (result[i].y - input[1].y) * sx) / detxy;
   306         float s = ((result[i].y - input[1].y) * tx -
   307                 (result[i].x - input[1].x) * ty) / detxy;
   309         float rz = input[1].z + (t*tz) + (s*sz);
   310         if( rz > pvr2_scene.bounds[5] ) {
   311             pvr2_scene.bounds[5] = rz;
   312         } else if( rz < pvr2_scene.bounds[4] ) {
   313             pvr2_scene.bounds[4] = rz;
   314         }
   315         result[i].z = rz;
   316         result[i].u = input[1].u + (t*tu) + (s*su);
   317         result[i].v = input[1].v + (t*tv) + (s*sv);
   319         if( is_solid_shaded ) {
   320             memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
   321             memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
   322         } else {
   323             float *rgba0 = input[0].rgba;
   324             float *rgba1 = input[1].rgba;
   325             float *rgba2 = input[2].rgba;
   326             float *rgba3 = result[i].rgba;
   327             for( j=0; j<8; j++ ) {
   328                 float tc = *rgba0++ - *rgba1;
   329                 float sc = *rgba2++ - *rgba1;
   330                 float rc = *rgba1++ + (t*tc) + (s*sc);
   331                 *rgba3++ = rc;
   332             }
   333         }
   334     }
   335 }
   337 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
   338 {
   339     union {
   340         uint32_t i;
   341         float f;
   342     } v;
   343     v.f = z * fog_density;
   344     if( v.f < 1.0 ) v.f = 1.0;
   345     else if( v.f > 255.9999 ) v.f = 255.9999;
   347     uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
   348     return fog_table[index][0];
   349 }
   351 /**
   352  * Compute the fog coefficients for all polygons using lookup-table fog. It's 
   353  * a little more convenient to do this as a separate pass, since we don't have
   354  * to worry about computed vertexes.
   355  */
   356 static void scene_compute_lut_fog( )
   357 {
   358     int i,j;
   360     float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
   361     float fog_table[128][2];
   363     /* Parse fog table out into floating-point format */
   364     for( i=0; i<128; i++ ) {
   365         uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
   366         fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
   367         fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
   368     }
   371     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   372         int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
   373         if( mode == PVR2_POLY_FOG_LOOKUP ) {
   374             uint32_t index = pvr2_scene.poly_array[i].vertex_index;
   375             for( j=0; j<=pvr2_scene.poly_array[i].vertex_count; j++ ) {
   376                 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 
   377                     scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
   378             }
   379         } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
   380             uint32_t index = pvr2_scene.poly_array[i].vertex_index;
   381             for( j=0; j<=pvr2_scene.poly_array[i].vertex_count; j++ ) {
   382                 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
   383                 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
   384                 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
   385                 pvr2_scene.vertex_array[index+j].rgba[3] = 
   386                     scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
   387             }
   388         }
   389     }    
   390 }
   392 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
   393                                 gboolean is_modified )
   394 {
   395     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   396     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   397     uint32_t *context = ptr;
   398     unsigned int i;
   400     if( poly->vertex_index == -1 ) {
   401         ptr += (is_modified ? 5 : 3 );
   402         poly->vertex_index = pvr2_scene.vertex_index;
   404         assert( poly != NULL );
   405         assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   406         for( i=0; i<poly->vertex_count; i++ ) {
   407             pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
   408             ptr += vertex_length;
   409         }
   410         if( is_modified ) {
   411             int mod_offset = (vertex_length - 3)>>1;
   412             assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   413             ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   414             poly->mod_vertex_index = pvr2_scene.vertex_index;
   415             for( i=0; i<poly->vertex_count; i++ ) {
   416                 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
   417                 ptr += vertex_length;
   418             }
   419         }
   420     }
   421 }
   423 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
   424                                      gboolean is_modified )
   425 {
   426     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   427     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   428     uint32_t *context = ptr;
   429     unsigned int i;
   431     if( poly->vertex_index == -1 ) {
   432         // Construct it locally and copy to the vertex buffer, as the VBO is
   433         // allowed to be horribly slow for reads (ie it could be direct-mapped
   434         // vram).
   435         struct vertex_struct quad[4];
   437         assert( poly != NULL );
   438         assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   439         ptr += (is_modified ? 5 : 3 );
   440         poly->vertex_index = pvr2_scene.vertex_index;
   441         for( i=0; i<4; i++ ) {
   442             pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
   443             ptr += vertex_length;
   444         }
   445         scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   446         // Swap last two vertexes (quad arrangement => tri strip arrangement)
   447         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   448         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   449         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   450         pvr2_scene.vertex_index += 4;
   452         if( is_modified ) {
   453             int mod_offset = (vertex_length - 3)>>1;
   454             assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   455             ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   456             poly->mod_vertex_index = pvr2_scene.vertex_index;
   457             for( i=0; i<4; i++ ) {
   458                 pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
   459                 ptr += vertex_length;
   460             }
   461             scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   462             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   463             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   464             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   465             pvr2_scene.vertex_index += 4;
   466         }
   467     }
   468 }
   470 static void scene_extract_polygons( pvraddr_t tile_entry )
   471 {
   472     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   473     do {
   474         uint32_t entry = *tile_list++;
   475         if( entry >> 28 == 0x0F ) {
   476             break;
   477         } else if( entry >> 28 == 0x0E ) {
   478             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   479         } else {
   480             pvraddr_t polyaddr = entry&0x000FFFFF;
   481             int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
   482             int vertex_length = (entry >> 21) & 0x07;
   483             int context_length = 3;
   484             if( is_modified ) {
   485                 context_length = 5;
   486                 vertex_length <<= 1 ;
   487             }
   488             vertex_length += 3;
   490             if( (entry & 0xE0000000) == 0x80000000 ) {
   491                 /* Triangle(s) */
   492                 int strip_count = ((entry >> 25) & 0x0F)+1;
   493                 int polygon_length = 3 * vertex_length + context_length;
   494                 int i;
   495                 struct polygon_struct *last_poly = NULL;
   496                 for( i=0; i<strip_count; i++ ) {
   497                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
   498                     polyaddr += polygon_length;
   499                     if( last_poly != NULL && last_poly->next == NULL ) {
   500                         last_poly->next = poly;
   501                     }
   502                     last_poly = poly;
   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                 struct polygon_struct *last_poly = NULL;
   510                 for( i=0; i<strip_count; i++ ) {
   511                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
   512                     polyaddr += polygon_length;
   513                     if( last_poly != NULL && last_poly->next == NULL ) {
   514                         last_poly->next = poly;
   515                     }
   516                     last_poly = poly;
   517                 }
   518             } else {
   519                 /* Polygon */
   520                 int i, last = -1;
   521                 for( i=5; i>=0; i-- ) {
   522                     if( entry & (0x40000000>>i) ) {
   523                         last = i;
   524                         break;
   525                     }
   526                 }
   527                 if( last != -1 ) {
   528                     scene_add_polygon( polyaddr, last+3, is_modified );
   529                 }
   530             }
   531         }
   532     } while( 1 );
   533 }
   535 static void scene_extract_vertexes( pvraddr_t tile_entry )
   536 {
   537     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   538     do {
   539         uint32_t entry = *tile_list++;
   540         if( entry >> 28 == 0x0F ) {
   541             break;
   542         } else if( entry >> 28 == 0x0E ) {
   543             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   544         } else {
   545             pvraddr_t polyaddr = entry&0x000FFFFF;
   546             int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
   547             int vertex_length = (entry >> 21) & 0x07;
   548             int context_length = 3;
   549             if( is_modified ) {
   550                 context_length = 5;
   551                 vertex_length <<=1 ;
   552             }
   553             vertex_length += 3;
   555             if( (entry & 0xE0000000) == 0x80000000 ) {
   556                 /* Triangle(s) */
   557                 int strip_count = ((entry >> 25) & 0x0F)+1;
   558                 int polygon_length = 3 * vertex_length + context_length;
   559                 int i;
   560                 for( i=0; i<strip_count; i++ ) {
   561                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   562                     polyaddr += polygon_length;
   563                 }
   564             } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   565                 /* Sprite(s) */
   566                 int strip_count = ((entry >> 25) & 0x0F)+1;
   567                 int polygon_length = 4 * vertex_length + context_length;
   568                 int i;
   569                 for( i=0; i<strip_count; i++ ) {
   570                     scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
   571                     polyaddr += polygon_length;
   572                 }
   573             } else {
   574                 /* Polygon */
   575                 int i, last = -1;
   576                 for( i=5; i>=0; i-- ) {
   577                     if( entry & (0x40000000>>i) ) {
   578                         last = i;
   579                         break;
   580                     }
   581                 }
   582                 if( last != -1 ) {
   583                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   584                 }
   585             }
   586         }
   587     } while( 1 );
   588 }
   590 static void scene_extract_background( void )
   591 {
   592     uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
   593     int vertex_length = (bgplane >> 24) & 0x07;
   594     int context_length = 3, i;
   595     int is_modified = (bgplane & 0x08000000) && pvr2_scene.full_shadow;
   597     struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   598     uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
   599     poly->context = context;
   600     poly->vertex_count = 4;
   601     poly->vertex_index = pvr2_scene.vertex_count;
   602     if( is_modified ) {
   603         context_length = 5;
   604         vertex_length <<= 1;
   605         poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
   606         pvr2_scene.vertex_count += 8;
   607     } else {
   608         poly->mod_vertex_index = -1;
   609         pvr2_scene.vertex_count += 4;
   610     }
   611     vertex_length += 3;
   612     context_length += (bgplane & 0x07) * vertex_length;
   614     poly->next = NULL;
   615     pvr2_scene.bkgnd_poly = poly;
   617     struct vertex_struct base_vertexes[3];
   618     uint32_t *ptr = context + context_length;
   619     for( i=0; i<3; i++ ) {
   620         pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[1],
   621                 ptr, 0 );
   622         ptr += vertex_length;
   623     }
   624     struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
   625     result_vertexes[0].x = result_vertexes[0].y = 0;
   626     result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   627     result_vertexes[1].y = result_vertexes[2].x = 0;
   628     result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   629     scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   631     if( is_modified ) {
   632         int mod_offset = (vertex_length - 3)>>1;
   633         ptr = context + context_length;
   634         for( i=0; i<3; i++ ) {
   635             pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[3],
   636                     ptr, mod_offset );
   637             ptr += vertex_length;
   638         }
   639         result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
   640         result_vertexes[0].x = result_vertexes[0].y = 0;
   641         result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   642         result_vertexes[1].y = result_vertexes[2].x = 0;
   643         result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   644         scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   645     }
   647 }
   650 uint32_t pvr2_scene_buffer_width()
   651 {
   652     return pvr2_scene.buffer_width;
   653 }
   655 uint32_t pvr2_scene_buffer_height()
   656 {
   657     return pvr2_scene.buffer_height;
   658 }
   660 /**
   661  * Extract the current scene into the rendering structures. We run two passes
   662  * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
   663  * second pass extracts the vertex data into the VBO/vertex array.
   664  *
   665  * Difficult to do in single pass as we don't generally know the size of a
   666  * polygon for certain until we've seen all tiles containing it. It also means we
   667  * can count the vertexes and allocate the appropriate size VBO.
   668  *
   669  * FIXME: accesses into VRAM need to be bounds-checked properly
   670  */
   671 void pvr2_scene_read( void )
   672 {
   673     pvr2_scene_init();
   674     pvr2_scene_reset();
   676     pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
   677     pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
   678     pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
   679     pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
   680     pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
   682     uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
   683     if( scaler & SCALER_HSCALE ) {
   684     	/* If the horizontal scaler is in use, we're (in principle) supposed to
   685     	 * divide everything by 2. However in the interests of display quality,
   686     	 * instead we want to render to the unscaled resolution and downsample
   687     	 * only if/when required.
   688     	 */
   689     	pvr2_scene.bounds[1] *= 2;
   690     }
   692     uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
   693     unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
   694     fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
   695     unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
   697     uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
   698     uint32_t *segment = tilebuffer;
   699     pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
   700     pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
   701     pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
   703     int max_tile_x = 0;
   704     int max_tile_y = 0;
   705     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   706     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   708     if( (obj_config & 0x00200000) == 0 ) {
   709         if( isp_config & 1 ) {
   710             pvr2_scene.sort_mode = SORT_NEVER;
   711         } else {
   712             pvr2_scene.sort_mode = SORT_ALWAYS;
   713         }
   714     } else {
   715         pvr2_scene.sort_mode = SORT_TILEFLAG;
   716     }
   718     // Pass 1: Extract polygon list
   719     uint32_t control;
   720     int i;
   721     do {
   722         control = *segment++;
   723         int tile_x = SEGMENT_X(control);
   724         int tile_y = SEGMENT_Y(control);
   725         if( tile_x > max_tile_x ) {
   726             max_tile_x = tile_x;
   727         }
   728         if( tile_y > max_tile_y ) {
   729             max_tile_y = tile_y;
   730         }
   731         for( i=0; i<5; i++ ) {
   732             if( (*segment & NO_POINTER) == 0 ) {
   733                 scene_extract_polygons( *segment );
   734             }
   735             segment++;
   736         }
   737     } while( (control & SEGMENT_END) == 0 );
   739     pvr2_scene.buffer_width = (max_tile_x+1)<<5;
   740     pvr2_scene.buffer_height = (max_tile_y+1)<<5;
   742     // Pass 2: Extract vertex data
   743     vertex_buffer_map();
   744     pvr2_scene.vertex_index = 0;
   745     segment = tilebuffer;
   746     do {
   747         control = *segment++;
   748         for( i=0; i<5; i++ ) {
   749             if( (*segment & NO_POINTER) == 0 ) {
   750                 scene_extract_vertexes( *segment );
   751             }
   752             segment++;
   753         }
   754     } while( (control & SEGMENT_END) == 0 );
   756     scene_extract_background();
   757     scene_compute_lut_fog();
   759     vertex_buffer_unmap();
   760 }
   762 /**
   763  * Dump the current scene to file in a (mostly) human readable form
   764  */
   765 void pvr2_scene_dump( FILE *f )
   766 {
   767     int i,j;
   769     fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
   770     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   771         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
   772         fprintf( f, "  %08X ", ((unsigned char *)poly->context) - video_base );
   773         switch( poly->vertex_count ) {
   774         case 3: fprintf( f, "Tri     " ); break;
   775         case 4: fprintf( f, "Quad    " ); break;
   776         default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
   777         }
   778         fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
   779         if( poly->mod_vertex_index != -1 ) {
   780             fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
   781         } else {
   782             fprintf( f, "\n" );
   783         }
   785         for( j=0; j<poly->vertex_count; j++ ) {
   786             struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
   787             fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f)  %.5f,%.5f,%.5f,%.5f  %.5f %.5f %.5f %.5f\n", v->x, v->y, v->z, v->u, v->v,
   788                      v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   789                      v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   790         }
   791         if( poly->mod_vertex_index != -1 ) {
   792             fprintf( f, "  ---\n" );
   793             for( j=0; j<poly->vertex_count; j++ ) {
   794                 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
   795                 fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f)  %.5f,%.5f,%.5f,%.5f  %.5f %.5f %.5f %.5f\n", v->x, v->y, v->z, v->u, v->v,
   796                          v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   797                          v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   798             }
   799         }
   800     }
   802 }
.