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