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