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