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