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