Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 1298:d0eb2307b847
prev1257:e1314ad3e7cc
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Manage the internal vertex/polygon buffers and scene data structure.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <assert.h>
    20 #include <string.h>
    21 #include <math.h>
    22 #include "lxdream.h"
    23 #include "display.h"
    24 #include "pvr2/pvr2.h"
    25 #include "pvr2/pvr2mmio.h"
    26 #include "pvr2/glutil.h"
    27 #include "pvr2/scene.h"
    29 #define U8TOFLOAT(n)  (((float)((n)+1))/256.0)
    30 #define POLY_IDX(addr) ( ((uint32_t *)addr) - ((uint32_t *)pvr2_scene.pvr2_pbuf))
    32 static void unpack_bgra(uint32_t bgra, float *rgba)
    33 {
    34     rgba[0] = ((float)(((bgra&0x00FF0000)>>16) + 1)) / 256.0;
    35     rgba[1] = ((float)(((bgra&0x0000FF00)>>8) + 1)) / 256.0;
    36     rgba[2] = ((float)((bgra&0x000000FF) + 1)) / 256.0;
    37     rgba[3] = ((float)(((bgra&0xFF000000)>>24) + 1)) / 256.0;
    38 }
    40 /**
    41  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    42  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    43  * TODO: Check the correctness of this.
    44  */
    45 static float halftofloat( uint16_t half )
    46 {
    47     union {
    48         float f;
    49         uint32_t i;
    50     } temp;
    51     temp.i = ((uint32_t)half)<<16;
    52     return temp.f;
    53 }
    55 static float parse_fog_density( uint32_t value )
    56 {
    57     union {
    58         uint32_t i;
    59         float f;
    60     } u;
    61     u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
    62     return u.f;
    63 }
    65 struct pvr2_scene_struct pvr2_scene;
    66 static float scene_shadow_intensity = 0.0;
    67 static vertex_buffer_t vbuf = NULL;
    69 static void vertex_buffer_map()
    70 {
    71     // Allow 8 vertexes for the background (4+4)
    72     uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
    73     pvr2_scene.vertex_array = vbuf->map(vbuf, size);
    74 }
    76 static void vertex_buffer_unmap()
    77 {
    78     pvr2_scene.vertex_array = vbuf->unmap(vbuf);
    79 }
    81 /**
    82  * Allocate vertex buffer + temporary structures. GL context must have been initialized before this
    83  * point.
    84  */
    85 void pvr2_scene_init()
    86 {
    87     if( vbuf == NULL ) {
    88         vbuf = display_driver->create_vertex_buffer();
    89         pvr2_scene.vertex_array = NULL;
    90         pvr2_scene.vertex_array_size = 0;
    91         pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
    92         pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
    93     }
    94 }
    96 /**
    97  * Clear the scene data structures in preparation for fresh data
    98  */
    99 void pvr2_scene_reset()
   100 {
   101     /* Faster to just clear the active entries */
   102     for( int i=0; i<pvr2_scene.poly_count; i++ ) {
   103         pvr2_scene.buf_to_poly_map[POLY_IDX(pvr2_scene.poly_array[i].context)] = 0;
   104     }
   105     pvr2_scene.poly_count = 0;
   106     pvr2_scene.vertex_count = 0;
   107  }
   109 void pvr2_scene_shutdown()
   110 {
   111     vbuf->destroy(vbuf);
   112     vbuf = NULL;
   113     g_free( pvr2_scene.poly_array );
   114     pvr2_scene.poly_array = NULL;
   115     g_free( pvr2_scene.buf_to_poly_map );
   116     pvr2_scene.buf_to_poly_map = NULL;
   117 }
   119 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
   120                                                  shadow_mode_t is_modified )
   121 {
   122     int vert_mul = is_modified != SHADOW_NONE ? 2 : 1;
   124     if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
   125         if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
   126             pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
   127             pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
   128         }
   129         return pvr2_scene.buf_to_poly_map[poly_idx];
   130     } else {
   131         struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   132         poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
   133         poly->vertex_count = vertex_count;
   134         poly->vertex_index = -1;
   135         poly->mod_vertex_index = -1;
   136         poly->next = NULL;
   137         poly->sub_next = NULL;
   138         pvr2_scene.buf_to_poly_map[poly_idx] = poly;
   139         pvr2_scene.vertex_count += (vertex_count * vert_mul);
   140         return poly;
   141     }
   142 }
   144 /**
   145  * Given a starting polygon, break it at the specified triangle so that the
   146  * preceding triangles are retained, and the remainder are contained in a
   147  * new sub-polygon. Does not preserve winding.
   148  */
   149 static struct polygon_struct *scene_split_subpolygon( struct polygon_struct *parent, int split_offset )
   150 {
   151     assert( split_offset > 0 && split_offset < (parent->vertex_count-2) );
   152     assert( pvr2_scene.poly_count < MAX_POLYGONS );
   153     struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   154     poly->vertex_count = parent->vertex_count - split_offset;
   155     poly->vertex_index = parent->vertex_index + split_offset;
   156     if( parent->mod_vertex_index == -1 ) {
   157         poly->mod_vertex_index = -1;
   158     } else {
   159         poly->mod_vertex_index = parent->mod_vertex_index + split_offset;
   160     }
   161     poly->context = parent->context;
   162     poly->next = NULL;
   163     poly->sub_next = parent->sub_next;
   165     parent->sub_next = poly;
   166     parent->vertex_count = split_offset + 2;
   168     return poly;
   169 }
   171 static float scene_get_palette_offset( uint32_t tex )
   172 {
   173     uint32_t fmt = (tex & PVR2_TEX_FORMAT_MASK);
   174     if( fmt == PVR2_TEX_FORMAT_IDX4 ) {
   175         return ((float)((tex & 0x07E00000) >> 17))/1024.0 + 0.0002;
   176     } else if( fmt == PVR2_TEX_FORMAT_IDX8 ) {
   177         return ((float)((tex & 0x06000000) >> 17))/1024.0 + 0.0002;
   178     } else {
   179         return -1.0;
   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 scene_decode_vertex( struct vertex_struct *vert, uint32_t poly1,
   194                                        uint32_t poly2, uint32_t tex, 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         }
   233         switch( POLY2_TEX_BLEND(poly2) ) {
   234         case 0:/* Convert replace => modulate by setting colour values to 1.0 */
   235             vert->rgba[0] = vert->rgba[1] = vert->rgba[2] = vert->rgba[3] = 1.0;
   236             vert->tex_mode = 0.0;
   237             data.ival++; /* Skip the colour word */
   238             break;
   239         case 2: /* Decal */
   240             vert->tex_mode = 1.0;
   241             unpack_bgra(*data.ival++, vert->rgba);
   242             break;
   243         case 1:
   244             force_alpha = TRUE;
   245             /* fall-through */
   246         default:
   247             vert->tex_mode = 0.0;
   248             unpack_bgra(*data.ival++, vert->rgba);
   249             break;
   250         }
   251         vert->r = scene_get_palette_offset(tex);
   252     } else {
   253         vert->tex_mode = 2.0;
   254         vert->r = -1.0;
   255         unpack_bgra(*data.ival++, vert->rgba);
   256     }
   258     if( POLY1_SPECULAR(poly1) ) {
   259         unpack_bgra(*data.ival++, vert->offset_rgba);
   260     } else {
   261         vert->offset_rgba[0] = 0.0;
   262         vert->offset_rgba[1] = 0.0;
   263         vert->offset_rgba[2] = 0.0;
   264         vert->offset_rgba[3] = 0.0;
   265     }
   267     if( force_alpha ) {
   268         vert->rgba[3] = 1.0;
   269     }
   270 }
   272 /**
   273  * Compute texture, colour, and z values for 1 or more result points by interpolating from
   274  * a set of 3 input points. The result point(s) must define their x,y.
   275  */
   276 static void scene_compute_vertexes( struct vertex_struct *result,
   277                                     int result_count,
   278                                     struct vertex_struct *input,
   279                                     gboolean is_solid_shaded )
   280 {
   281     int i,j;
   282     float sx = input[2].x - input[1].x;
   283     float sy = input[2].y - input[1].y;
   284     float tx = input[0].x - input[1].x;
   285     float ty = input[0].y - input[1].y;
   287     float detxy = ((sy) * (tx)) - ((ty) * (sx));
   288     if( detxy == 0 ) {
   289         // If the input points fall on a line, they don't define a usable
   290         // polygon - the PVR2 takes the last input point as the result in
   291         // this case.
   292         for( i=0; i<result_count; i++ ) {
   293             float x = result[i].x;
   294             float y = result[i].y;
   295             memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
   296             result[i].x = x;
   297             result[i].y = y;
   298         }
   299         return;
   300     }
   301     float sz = input[2].z - input[1].z;
   302     float tz = input[0].z - input[1].z;
   303     float su = input[2].u - input[1].u;
   304     float tu = input[0].u - input[1].u;
   305     float sv = input[2].v - input[1].v;
   306     float tv = input[0].v - input[1].v;
   308     for( i=0; i<result_count; i++ ) {
   309         float t = ((result[i].x - input[1].x) * sy -
   310                 (result[i].y - input[1].y) * sx) / detxy;
   311         float s = ((result[i].y - input[1].y) * tx -
   312                 (result[i].x - input[1].x) * ty) / detxy;
   314         float rz = input[1].z + (t*tz) + (s*sz);
   315         if( rz > pvr2_scene.bounds[5] ) {
   316             pvr2_scene.bounds[5] = rz;
   317         } else if( rz < pvr2_scene.bounds[4] ) {
   318             pvr2_scene.bounds[4] = rz;
   319         }
   320         result[i].z = rz;
   321         result[i].u = input[1].u + (t*tu) + (s*su);
   322         result[i].v = input[1].v + (t*tv) + (s*sv);
   323         result[i].r = input[1].r; /* Last two components are flat */
   324         result[i].tex_mode = input[1].tex_mode;
   326         if( is_solid_shaded ) {
   327             memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
   328             memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
   329         } else {
   330             float *rgba0 = input[0].rgba;
   331             float *rgba1 = input[1].rgba;
   332             float *rgba2 = input[2].rgba;
   333             float *rgba3 = result[i].rgba;
   334             for( j=0; j<8; j++ ) {
   335                 float tc = *rgba0++ - *rgba1;
   336                 float sc = *rgba2++ - *rgba1;
   337                 float rc = *rgba1++ + (t*tc) + (s*sc);
   338                 *rgba3++ = rc;
   339             }
   340         }
   341     }
   342 }
   344 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
   345 {
   346     union {
   347         uint32_t i;
   348         float f;
   349     } v;
   350     v.f = z * fog_density;
   351     if( v.f < 1.0 ) v.f = 1.0;
   352     else if( v.f > 255.9999 ) v.f = 255.9999;
   354     uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
   355     return fog_table[index][0];
   356 }
   358 /**
   359  * Compute the fog coefficients for all polygons using lookup-table fog. It's 
   360  * a little more convenient to do this as a separate pass, since we don't have
   361  * to worry about computed vertexes.
   362  */
   363 static void scene_compute_lut_fog( )
   364 {
   365     int i,j;
   367     float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
   368     float fog_table[128][2];
   370     /* Parse fog table out into floating-point format */
   371     for( i=0; i<128; i++ ) {
   372         uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
   373         fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
   374         fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
   375     }
   378     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   379         int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
   380         uint32_t index = pvr2_scene.poly_array[i].vertex_index;
   381         if( mode == PVR2_POLY_FOG_LOOKUP ) {
   382             for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
   383                 float fog = scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
   384                 if( display_driver->capabilities.has_sl )
   385                     pvr2_scene.vertex_array[index+j].offset_rgba[3] = -fog;
   386                 else
   387                     pvr2_scene.vertex_array[index+j].offset_rgba[3] = fog;
   388             }
   389         } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
   390             for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
   391                 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
   392                 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
   393                 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
   394                 pvr2_scene.vertex_array[index+j].rgba[3] = 
   395                     scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
   396                 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
   397             }
   398         } else if( mode == PVR2_POLY_FOG_DISABLED ) {
   399             for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
   400                 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
   401             }
   402         }
   403     }    
   404 }
   406 /**
   407  * Manually cull back-facing polygons where we can - this actually saves
   408  * us a lot of time vs passing everything to GL to do it.
   409  */
   410 static void scene_backface_cull()
   411 {
   412     unsigned poly_idx;
   413     unsigned poly_count = pvr2_scene.poly_count; /* Note: we don't want to process any sub-polygons created here */
   414     for( poly_idx = 0; poly_idx<poly_count; poly_idx++ ) {
   415         uint32_t poly1 = pvr2_scene.poly_array[poly_idx].context[0];
   416         if( POLY1_CULL_ENABLE(poly1) ) {
   417             struct polygon_struct *poly = &pvr2_scene.poly_array[poly_idx];
   418             unsigned vert_idx = poly->vertex_index;
   419             unsigned tri_count = poly->vertex_count-2;
   420             struct vertex_struct *vert = &pvr2_scene.vertex_array[vert_idx];
   421             unsigned i;
   422             gboolean ccw = (POLY1_CULL_MODE(poly1) == CULL_CCW);
   423             int first_visible = -1, last_visible = -1;
   424             for( i=0; i<tri_count; i++ ) {
   425                 float ux = vert[i+1].x - vert[i].x;
   426                 float uy = vert[i+1].y - vert[i].y;
   427                 float vx = vert[i+2].x - vert[i].x;
   428                 float vy = vert[i+2].y - vert[i].y;
   429                 float nz = (ux*vy) - (uy*vx);
   430                 if( ccw ? nz > 0 : nz < 0 ) {
   431                     /* Surface is visible */
   432                     if( first_visible == -1 ) {
   433                         first_visible = i;
   434                         /* Elide the initial hidden triangles (note we don't
   435                          * need to care about winding anymore here) */
   436                         poly->vertex_index += i;
   437                         poly->vertex_count -= i;
   438                         if( poly->mod_vertex_index != -1 )
   439                             poly->mod_vertex_index += i;
   440                     } else if( last_visible != i-1 ) {
   441                         /* And... here we have to split the polygon. Allocate a new
   442                          * sub-polygon to hold the vertex references */
   443                         struct polygon_struct *sub = scene_split_subpolygon(poly, (i-first_visible));
   444                         poly->vertex_count -= (i-first_visible-1) - last_visible;
   445                         first_visible = i;
   446                         poly = sub;
   447                     }
   448                     last_visible = i;
   449                 } /* Else culled */
   450                 /* Invert ccw flag for triangle strip processing */
   451                 ccw = !ccw;
   452             }
   453             if( last_visible == -1 ) {
   454                 /* No visible surfaces, so we can mark the whole polygon as being vertex-less */
   455                 poly->vertex_count = 0;
   456             } else if( last_visible != tri_count-1 ) {
   457                 /* Remove final hidden tris */
   458                 poly->vertex_count -= (tri_count - 1 - last_visible);
   459             }
   460         }
   461     }
   462 }
   464 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
   465 {
   466     unsigned int i;
   468     for( i=0; i<count; i++ ) {
   469         dest->x = src->x;
   470         dest->y = src->y;
   471         dest->z = src->z;
   472         dest->u = src->u;
   473         dest->v = src->v;
   474         dest->r = src->r;
   475         dest->tex_mode = src->tex_mode;
   476         dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
   477         dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
   478         dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
   479         dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
   480         dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
   481         dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
   482         dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
   483         dest->offset_rgba[3] = src->offset_rgba[3];
   484         dest++;
   485         src++;
   486     }
   487 }
   489 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
   490                                 shadow_mode_t is_modified )
   491 {
   492     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   493     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   494     uint32_t *context = ptr;
   495     unsigned int i;
   497     if( poly->vertex_index == -1 ) {
   498         ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
   499         poly->vertex_index = pvr2_scene.vertex_index;
   501         assert( poly != NULL );
   502         assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   503         for( i=0; i<poly->vertex_count; i++ ) {
   504             scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], context[2], ptr, 0 );
   505             ptr += vertex_length;
   506         }
   507         if( is_modified ) {
   508             assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   509             poly->mod_vertex_index = pvr2_scene.vertex_index;
   510             if( is_modified == SHADOW_FULL ) {
   511                 int mod_offset = (vertex_length - 3)>>1;
   512                 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   513                 for( i=0; i<poly->vertex_count; i++ ) {
   514                     scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], context[4], ptr, mod_offset );
   515                     ptr += vertex_length;
   516                 }
   517             } else {
   518                 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index], 
   519                         &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
   520                 pvr2_scene.vertex_index += poly->vertex_count;
   521             }
   522         }
   523     }
   524 }
   526 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
   527                                      shadow_mode_t is_modified )
   528 {
   529     struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
   530     uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
   531     uint32_t *context = ptr;
   532     unsigned int i;
   534     if( poly->vertex_index == -1 ) {
   535         // Construct it locally and copy to the vertex buffer, as the VBO is
   536         // allowed to be horribly slow for reads (ie it could be direct-mapped
   537         // vram).
   538         struct vertex_struct quad[4];
   540         assert( poly != NULL );
   541         assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   542         ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
   543         poly->vertex_index = pvr2_scene.vertex_index;
   544         for( i=0; i<4; i++ ) {
   545             scene_decode_vertex( &quad[i], context[0], context[1], context[2], ptr, 0 );
   546             ptr += vertex_length;
   547         }
   548         scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   549         // Swap last two vertexes (quad arrangement => tri strip arrangement)
   550         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   551         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   552         memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   553         if( !POLY1_GOURAUD_SHADED(context[0]) ) {
   554             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   555             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   556         }
   558         pvr2_scene.vertex_index += 4;
   560         if( is_modified ) {
   561             assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   562             poly->mod_vertex_index = pvr2_scene.vertex_index;
   563             if( is_modified == SHADOW_FULL ) {
   564                 int mod_offset = (vertex_length - 3)>>1;
   565                 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   566                 for( i=0; i<4; i++ ) {
   567                     scene_decode_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
   568                     ptr += vertex_length;
   569                 }
   570                 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   571                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   572                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   573                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   574                 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
   575                     memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   576                     memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   577                 }
   578             } else {
   579                 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index], 
   580                         &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
   581                 pvr2_scene.vertex_index += poly->vertex_count;
   582             }
   583             pvr2_scene.vertex_index += 4;
   584         }
   585     }
   586 }
   588 static void scene_extract_polygons( pvraddr_t tile_entry )
   589 {
   590     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   591     do {
   592         uint32_t entry = *tile_list++;
   593         if( entry >> 28 == 0x0F ) {
   594             break;
   595         } else if( entry >> 28 == 0x0E ) {
   596             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   597         } else {
   598             pvraddr_t polyaddr = entry&0x000FFFFF;
   599             shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   600             int vertex_length = (entry >> 21) & 0x07;
   601             int context_length = 3;
   602             if( is_modified == SHADOW_FULL ) {
   603                 context_length = 5;
   604                 vertex_length <<= 1 ;
   605             }
   606             vertex_length += 3;
   608             if( (entry & 0xE0000000) == 0x80000000 ) {
   609                 /* Triangle(s) */
   610                 int strip_count = ((entry >> 25) & 0x0F)+1;
   611                 int polygon_length = 3 * vertex_length + context_length;
   612                 int i;
   613                 struct polygon_struct *last_poly = NULL;
   614                 for( i=0; i<strip_count; i++ ) {
   615                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
   616                     polyaddr += polygon_length;
   617                     if( last_poly != NULL && last_poly->next == NULL ) {
   618                         last_poly->next = poly;
   619                     }
   620                     last_poly = poly;
   621                 }
   622             } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   623                 /* Sprite(s) */
   624                 int strip_count = ((entry >> 25) & 0x0F)+1;
   625                 int polygon_length = 4 * vertex_length + context_length;
   626                 int i;
   627                 struct polygon_struct *last_poly = NULL;
   628                 for( i=0; i<strip_count; i++ ) {
   629                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
   630                     polyaddr += polygon_length;
   631                     if( last_poly != NULL && last_poly->next == NULL ) {
   632                         last_poly->next = poly;
   633                     }
   634                     last_poly = poly;
   635                 }
   636             } else {
   637                 /* Polygon */
   638                 int i, last = -1;
   639                 for( i=5; i>=0; i-- ) {
   640                     if( entry & (0x40000000>>i) ) {
   641                         last = i;
   642                         break;
   643                     }
   644                 }
   645                 if( last != -1 ) {
   646                     scene_add_polygon( polyaddr, last+3, is_modified );
   647                 }
   648             }
   649         }
   650     } while( 1 );
   651 }
   653 static void scene_extract_vertexes( pvraddr_t tile_entry )
   654 {
   655     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   656     do {
   657         uint32_t entry = *tile_list++;
   658         if( entry >> 28 == 0x0F ) {
   659             break;
   660         } else if( entry >> 28 == 0x0E ) {
   661             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   662         } else {
   663             pvraddr_t polyaddr = entry&0x000FFFFF;
   664             shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   665             int vertex_length = (entry >> 21) & 0x07;
   666             int context_length = 3;
   667             if( is_modified == SHADOW_FULL ) {
   668                 context_length = 5;
   669                 vertex_length <<=1 ;
   670             }
   671             vertex_length += 3;
   673             if( (entry & 0xE0000000) == 0x80000000 ) {
   674                 /* Triangle(s) */
   675                 int strip_count = ((entry >> 25) & 0x0F)+1;
   676                 int polygon_length = 3 * vertex_length + context_length;
   677                 int i;
   678                 for( i=0; i<strip_count; i++ ) {
   679                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   680                     polyaddr += polygon_length;
   681                 }
   682             } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   683                 /* Sprite(s) */
   684                 int strip_count = ((entry >> 25) & 0x0F)+1;
   685                 int polygon_length = 4 * vertex_length + context_length;
   686                 int i;
   687                 for( i=0; i<strip_count; i++ ) {
   688                     scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
   689                     polyaddr += polygon_length;
   690                 }
   691             } else {
   692                 /* Polygon */
   693                 int i, last = -1;
   694                 for( i=5; i>=0; i-- ) {
   695                     if( entry & (0x40000000>>i) ) {
   696                         last = i;
   697                         break;
   698                     }
   699                 }
   700                 if( last != -1 ) {
   701                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   702                 }
   703             }
   704         }
   705     } while( 1 );
   706 }
   708 static void scene_extract_background( void )
   709 {
   710     uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
   711     int vertex_length = (bgplane >> 24) & 0x07;
   712     int context_length = 3, i;
   713     shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   715     struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   716     uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
   717     poly->context = context;
   718     poly->vertex_count = 4;
   719     poly->vertex_index = pvr2_scene.vertex_count;
   720     if( is_modified == SHADOW_FULL ) {
   721         context_length = 5;
   722         vertex_length <<= 1;
   723     }
   724     if( is_modified != SHADOW_NONE ) {
   725         poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
   726         pvr2_scene.vertex_count += 8;
   727     } else {
   728         poly->mod_vertex_index = -1;
   729         pvr2_scene.vertex_count += 4;
   730     }
   731     vertex_length += 3;
   732     context_length += (bgplane & 0x07) * vertex_length;
   734     poly->next = NULL;
   735     poly->sub_next = NULL;
   736     pvr2_scene.bkgnd_poly = poly;
   738     struct vertex_struct base_vertexes[3];
   739     uint32_t *ptr = context + context_length;
   740     for( i=0; i<3; i++ ) {
   741         scene_decode_vertex( &base_vertexes[i], context[0], context[1], context[2],
   742                 ptr, 0 );
   743         ptr += vertex_length;
   744     }
   745     struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
   746     result_vertexes[0].x = result_vertexes[0].y = 0;
   747     result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   748     result_vertexes[1].y = result_vertexes[2].x = 0;
   749     result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   750     scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   752     if( is_modified == SHADOW_FULL ) {
   753         int mod_offset = (vertex_length - 3)>>1;
   754         ptr = context + context_length;
   755         for( i=0; i<3; i++ ) {
   756             scene_decode_vertex( &base_vertexes[i], context[0], context[3], context[4],
   757                     ptr, mod_offset );
   758             ptr += vertex_length;
   759         }
   760         result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
   761         result_vertexes[0].x = result_vertexes[0].y = 0;
   762         result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   763         result_vertexes[1].y = result_vertexes[2].x = 0;
   764         result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   765         scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   766     } else if( is_modified == SHADOW_CHEAP ) {
   767         scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index], 
   768                 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
   769         pvr2_scene.vertex_index += poly->vertex_count;
   770     }
   772 }
   775 uint32_t pvr2_scene_buffer_width()
   776 {
   777     return pvr2_scene.buffer_width;
   778 }
   780 uint32_t pvr2_scene_buffer_height()
   781 {
   782     return pvr2_scene.buffer_height;
   783 }
   785 /**
   786  * Extract the current scene into the rendering structures. We run two passes
   787  * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
   788  * second pass extracts the vertex data into the VBO/vertex array.
   789  *
   790  * Difficult to do in single pass as we don't generally know the size of a
   791  * polygon for certain until we've seen all tiles containing it. It also means we
   792  * can count the vertexes and allocate the appropriate size VBO.
   793  *
   794  * FIXME: accesses into VRAM need to be bounds-checked properly
   795  */
   796 void pvr2_scene_read( void )
   797 {
   798     pvr2_scene_init();
   799     pvr2_scene_reset();
   801     pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
   802     pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
   803     pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
   804     pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
   805     pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
   807     uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
   808     if( scaler & SCALER_HSCALE ) {
   809     	/* If the horizontal scaler is in use, we're (in principle) supposed to
   810     	 * divide everything by 2. However in the interests of display quality,
   811     	 * instead we want to render to the unscaled resolution and downsample
   812     	 * only if/when required.
   813     	 */
   814     	pvr2_scene.bounds[1] *= 2;
   815     }
   817     uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
   818     unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
   819     fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
   820     unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
   822     uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
   823     uint32_t *segment = tilebuffer;
   824     uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
   825     pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
   826     pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
   827     pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
   828     scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
   830     int max_tile_x = 0;
   831     int max_tile_y = 0;
   832     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   833     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   835     if( (obj_config & 0x00200000) == 0 ) {
   836         if( isp_config & 1 ) {
   837             pvr2_scene.sort_mode = SORT_NEVER;
   838         } else {
   839             pvr2_scene.sort_mode = SORT_ALWAYS;
   840         }
   841     } else {
   842         pvr2_scene.sort_mode = SORT_TILEFLAG;
   843     }
   845     // Pass 1: Extract polygon list
   846     uint32_t control;
   847     int i;
   848     do {
   849         control = *segment++;
   850         int tile_x = SEGMENT_X(control);
   851         int tile_y = SEGMENT_Y(control);
   852         if( tile_x > max_tile_x ) {
   853             max_tile_x = tile_x;
   854         }
   855         if( tile_y > max_tile_y ) {
   856             max_tile_y = tile_y;
   857         }
   858         for( i=0; i<5; i++ ) {
   859             if( (*segment & NO_POINTER) == 0 ) {
   860                 scene_extract_polygons( *segment );
   861             }
   862             segment++;
   863         }
   864     } while( (control & SEGMENT_END) == 0 );
   866     pvr2_scene.buffer_width = (max_tile_x+1)<<5;
   867     pvr2_scene.buffer_height = (max_tile_y+1)<<5;
   869     // Pass 2: Extract vertex data
   870     vertex_buffer_map();
   871     pvr2_scene.vertex_index = 0;
   872     segment = tilebuffer;
   873     do {
   874         control = *segment++;
   875         for( i=0; i<5; i++ ) {
   876             if( (*segment & NO_POINTER) == 0 ) {
   877                 scene_extract_vertexes( *segment );
   878             }
   879             segment++;
   880         }
   881     } while( (control & SEGMENT_END) == 0 );
   883     scene_extract_background();
   884     scene_compute_lut_fog();
   885     scene_backface_cull();
   887     vertex_buffer_unmap();
   888 }
   890 void pvr2_scene_finished( )
   891 {
   892     vbuf->finished(vbuf);
   893 }
   895 /**
   896  * Dump the current scene to file in a (mostly) human readable form
   897  */
   898 void pvr2_scene_print( FILE *f )
   899 {
   900     int i,j;
   902     fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
   903     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   904         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
   905         fprintf( f, "  %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
   906         switch( poly->vertex_count ) {
   907         case 3: fprintf( f, "Tri     " ); break;
   908         case 4: fprintf( f, "Quad    " ); break;
   909         default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
   910         }
   911         fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
   912         if( poly->mod_vertex_index != -1 ) {
   913             fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
   914         } else {
   915             fprintf( f, "\n" );
   916         }
   918         for( j=0; j<poly->vertex_count; j++ ) {
   919             struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
   920             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,
   921                      v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   922                      v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   923         }
   924         if( poly->mod_vertex_index != -1 ) {
   925             fprintf( f, "  ---\n" );
   926             for( j=0; j<poly->vertex_count; j++ ) {
   927                 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
   928                 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,
   929                          v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   930                          v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   931             }
   932         }
   933     }
   935 }
   937 void pvr2_scene_dump()
   938 {
   939     pvr2_scene_print(stdout);
   940 }
.