Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 1240:190df8a791ca
prev1159:580436b01b6c
next1257:e1314ad3e7cc
author nkeynes
date Tue Feb 28 17:25:26 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Implement display output for the GLES2 case (no fixed function
rendering)
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         if( !POLY1_GOURAUD_SHADED(context[0]) ) {
   556             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   557             memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   558         }
   560         pvr2_scene.vertex_index += 4;
   562         if( is_modified ) {
   563             assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
   564             poly->mod_vertex_index = pvr2_scene.vertex_index;
   565             if( is_modified == SHADOW_FULL ) {
   566                 int mod_offset = (vertex_length - 3)>>1;
   567                 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
   568                 for( i=0; i<4; i++ ) {
   569                     scene_decode_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
   570                     ptr += vertex_length;
   571                 }
   572                 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
   573                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
   574                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
   575                 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
   576                 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
   577                     memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   578                     memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
   579                 }
   580             } else {
   581                 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index], 
   582                         &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
   583                 pvr2_scene.vertex_index += poly->vertex_count;
   584             }
   585             pvr2_scene.vertex_index += 4;
   586         }
   587     }
   588 }
   590 static void scene_extract_polygons( pvraddr_t tile_entry )
   591 {
   592     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   593     do {
   594         uint32_t entry = *tile_list++;
   595         if( entry >> 28 == 0x0F ) {
   596             break;
   597         } else if( entry >> 28 == 0x0E ) {
   598             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   599         } else {
   600             pvraddr_t polyaddr = entry&0x000FFFFF;
   601             shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   602             int vertex_length = (entry >> 21) & 0x07;
   603             int context_length = 3;
   604             if( is_modified == SHADOW_FULL ) {
   605                 context_length = 5;
   606                 vertex_length <<= 1 ;
   607             }
   608             vertex_length += 3;
   610             if( (entry & 0xE0000000) == 0x80000000 ) {
   611                 /* Triangle(s) */
   612                 int strip_count = ((entry >> 25) & 0x0F)+1;
   613                 int polygon_length = 3 * vertex_length + context_length;
   614                 int i;
   615                 struct polygon_struct *last_poly = NULL;
   616                 for( i=0; i<strip_count; i++ ) {
   617                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
   618                     polyaddr += polygon_length;
   619                     if( last_poly != NULL && last_poly->next == NULL ) {
   620                         last_poly->next = poly;
   621                     }
   622                     last_poly = poly;
   623                 }
   624             } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   625                 /* Sprite(s) */
   626                 int strip_count = ((entry >> 25) & 0x0F)+1;
   627                 int polygon_length = 4 * vertex_length + context_length;
   628                 int i;
   629                 struct polygon_struct *last_poly = NULL;
   630                 for( i=0; i<strip_count; i++ ) {
   631                     struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
   632                     polyaddr += polygon_length;
   633                     if( last_poly != NULL && last_poly->next == NULL ) {
   634                         last_poly->next = poly;
   635                     }
   636                     last_poly = poly;
   637                 }
   638             } else {
   639                 /* Polygon */
   640                 int i, last = -1;
   641                 for( i=5; i>=0; i-- ) {
   642                     if( entry & (0x40000000>>i) ) {
   643                         last = i;
   644                         break;
   645                     }
   646                 }
   647                 if( last != -1 ) {
   648                     scene_add_polygon( polyaddr, last+3, is_modified );
   649                 }
   650             }
   651         }
   652     } while( 1 );
   653 }
   655 static void scene_extract_vertexes( pvraddr_t tile_entry )
   656 {
   657     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   658     do {
   659         uint32_t entry = *tile_list++;
   660         if( entry >> 28 == 0x0F ) {
   661             break;
   662         } else if( entry >> 28 == 0x0E ) {
   663             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   664         } else {
   665             pvraddr_t polyaddr = entry&0x000FFFFF;
   666             shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   667             int vertex_length = (entry >> 21) & 0x07;
   668             int context_length = 3;
   669             if( is_modified == SHADOW_FULL ) {
   670                 context_length = 5;
   671                 vertex_length <<=1 ;
   672             }
   673             vertex_length += 3;
   675             if( (entry & 0xE0000000) == 0x80000000 ) {
   676                 /* Triangle(s) */
   677                 int strip_count = ((entry >> 25) & 0x0F)+1;
   678                 int polygon_length = 3 * vertex_length + context_length;
   679                 int i;
   680                 for( i=0; i<strip_count; i++ ) {
   681                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   682                     polyaddr += polygon_length;
   683                 }
   684             } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   685                 /* Sprite(s) */
   686                 int strip_count = ((entry >> 25) & 0x0F)+1;
   687                 int polygon_length = 4 * vertex_length + context_length;
   688                 int i;
   689                 for( i=0; i<strip_count; i++ ) {
   690                     scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
   691                     polyaddr += polygon_length;
   692                 }
   693             } else {
   694                 /* Polygon */
   695                 int i, last = -1;
   696                 for( i=5; i>=0; i-- ) {
   697                     if( entry & (0x40000000>>i) ) {
   698                         last = i;
   699                         break;
   700                     }
   701                 }
   702                 if( last != -1 ) {
   703                     scene_add_vertexes( polyaddr, vertex_length, is_modified );
   704                 }
   705             }
   706         }
   707     } while( 1 );
   708 }
   710 static void scene_extract_background( void )
   711 {
   712     uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
   713     int vertex_length = (bgplane >> 24) & 0x07;
   714     int context_length = 3, i;
   715     shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
   717     struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
   718     uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
   719     poly->context = context;
   720     poly->vertex_count = 4;
   721     poly->vertex_index = pvr2_scene.vertex_count;
   722     if( is_modified == SHADOW_FULL ) {
   723         context_length = 5;
   724         vertex_length <<= 1;
   725     }
   726     if( is_modified != SHADOW_NONE ) {
   727         poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
   728         pvr2_scene.vertex_count += 8;
   729     } else {
   730         poly->mod_vertex_index = -1;
   731         pvr2_scene.vertex_count += 4;
   732     }
   733     vertex_length += 3;
   734     context_length += (bgplane & 0x07) * vertex_length;
   736     poly->next = NULL;
   737     poly->sub_next = NULL;
   738     pvr2_scene.bkgnd_poly = poly;
   740     struct vertex_struct base_vertexes[3];
   741     uint32_t *ptr = context + context_length;
   742     for( i=0; i<3; i++ ) {
   743         scene_decode_vertex( &base_vertexes[i], context[0], context[1], context[2],
   744                 ptr, 0 );
   745         ptr += vertex_length;
   746     }
   747     struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
   748     result_vertexes[0].x = result_vertexes[0].y = 0;
   749     result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   750     result_vertexes[1].y = result_vertexes[2].x = 0;
   751     result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   752     scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   754     if( is_modified == SHADOW_FULL ) {
   755         int mod_offset = (vertex_length - 3)>>1;
   756         ptr = context + context_length;
   757         for( i=0; i<3; i++ ) {
   758             scene_decode_vertex( &base_vertexes[i], context[0], context[3], context[4],
   759                     ptr, mod_offset );
   760             ptr += vertex_length;
   761         }
   762         result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
   763         result_vertexes[0].x = result_vertexes[0].y = 0;
   764         result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
   765         result_vertexes[1].y = result_vertexes[2].x = 0;
   766         result_vertexes[2].y = result_vertexes[3].y  = pvr2_scene.buffer_height;
   767         scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
   768     } else if( is_modified == SHADOW_CHEAP ) {
   769         scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index], 
   770                 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
   771         pvr2_scene.vertex_index += poly->vertex_count;
   772     }
   774 }
   777 uint32_t pvr2_scene_buffer_width()
   778 {
   779     return pvr2_scene.buffer_width;
   780 }
   782 uint32_t pvr2_scene_buffer_height()
   783 {
   784     return pvr2_scene.buffer_height;
   785 }
   787 /**
   788  * Extract the current scene into the rendering structures. We run two passes
   789  * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
   790  * second pass extracts the vertex data into the VBO/vertex array.
   791  *
   792  * Difficult to do in single pass as we don't generally know the size of a
   793  * polygon for certain until we've seen all tiles containing it. It also means we
   794  * can count the vertexes and allocate the appropriate size VBO.
   795  *
   796  * FIXME: accesses into VRAM need to be bounds-checked properly
   797  */
   798 void pvr2_scene_read( void )
   799 {
   800     pvr2_scene_init();
   801     pvr2_scene_reset();
   803     pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
   804     pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
   805     pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
   806     pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
   807     pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
   809     uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
   810     if( scaler & SCALER_HSCALE ) {
   811     	/* If the horizontal scaler is in use, we're (in principle) supposed to
   812     	 * divide everything by 2. However in the interests of display quality,
   813     	 * instead we want to render to the unscaled resolution and downsample
   814     	 * only if/when required.
   815     	 */
   816     	pvr2_scene.bounds[1] *= 2;
   817     }
   819     uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
   820     unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
   821     fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
   822     unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
   824     uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
   825     uint32_t *segment = tilebuffer;
   826     uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
   827     pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
   828     pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
   829     pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
   830     scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
   832     int max_tile_x = 0;
   833     int max_tile_y = 0;
   834     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   835     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   837     if( (obj_config & 0x00200000) == 0 ) {
   838         if( isp_config & 1 ) {
   839             pvr2_scene.sort_mode = SORT_NEVER;
   840         } else {
   841             pvr2_scene.sort_mode = SORT_ALWAYS;
   842         }
   843     } else {
   844         pvr2_scene.sort_mode = SORT_TILEFLAG;
   845     }
   847     // Pass 1: Extract polygon list
   848     uint32_t control;
   849     int i;
   850     do {
   851         control = *segment++;
   852         int tile_x = SEGMENT_X(control);
   853         int tile_y = SEGMENT_Y(control);
   854         if( tile_x > max_tile_x ) {
   855             max_tile_x = tile_x;
   856         }
   857         if( tile_y > max_tile_y ) {
   858             max_tile_y = tile_y;
   859         }
   860         for( i=0; i<5; i++ ) {
   861             if( (*segment & NO_POINTER) == 0 ) {
   862                 scene_extract_polygons( *segment );
   863             }
   864             segment++;
   865         }
   866     } while( (control & SEGMENT_END) == 0 );
   868     pvr2_scene.buffer_width = (max_tile_x+1)<<5;
   869     pvr2_scene.buffer_height = (max_tile_y+1)<<5;
   871     // Pass 2: Extract vertex data
   872     vertex_buffer_map();
   873     pvr2_scene.vertex_index = 0;
   874     segment = tilebuffer;
   875     do {
   876         control = *segment++;
   877         for( i=0; i<5; i++ ) {
   878             if( (*segment & NO_POINTER) == 0 ) {
   879                 scene_extract_vertexes( *segment );
   880             }
   881             segment++;
   882         }
   883     } while( (control & SEGMENT_END) == 0 );
   885     scene_extract_background();
   886     scene_compute_lut_fog();
   887     scene_backface_cull();
   889     vertex_buffer_unmap();
   890 }
   892 void pvr2_scene_finished( )
   893 {
   894     vbuf->finished(vbuf);
   895 }
   897 /**
   898  * Dump the current scene to file in a (mostly) human readable form
   899  */
   900 void pvr2_scene_print( FILE *f )
   901 {
   902     int i,j;
   904     fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
   905     for( i=0; i<pvr2_scene.poly_count; i++ ) {
   906         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
   907         fprintf( f, "  %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
   908         switch( poly->vertex_count ) {
   909         case 3: fprintf( f, "Tri     " ); break;
   910         case 4: fprintf( f, "Quad    " ); break;
   911         default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
   912         }
   913         fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
   914         if( poly->mod_vertex_index != -1 ) {
   915             fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
   916         } else {
   917             fprintf( f, "\n" );
   918         }
   920         for( j=0; j<poly->vertex_count; j++ ) {
   921             struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
   922             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,
   923                      v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   924                      v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   925         }
   926         if( poly->mod_vertex_index != -1 ) {
   927             fprintf( f, "  ---\n" );
   928             for( j=0; j<poly->vertex_count; j++ ) {
   929                 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
   930                 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,
   931                          v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
   932                          v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
   933             }
   934         }
   935     }
   937 }
   939 void pvr2_scene_dump()
   940 {
   941     pvr2_scene_print(stdout);
   942 }
.