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