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