Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 669:ab344e42bca9
prev667:0e1ac8da75d9
next677:3ee62740ff8f
author nkeynes
date Mon May 12 10:00:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Cleanup most of the -Wall warnings (getting a bit sloppy...)
Convert FP code to use fixed banks rather than indirect pointer
(3-4% faster this way now)
file annotate diff log raw
nkeynes@653
     1
/**
nkeynes@653
     2
 * $Id$
nkeynes@653
     3
 *
nkeynes@653
     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@653
    26
#include "pvr2/glutil.h"
nkeynes@653
    27
#include "pvr2/scene.h"
nkeynes@653
    28
nkeynes@653
    29
nkeynes@653
    30
static inline uint32_t bgra_to_rgba(uint32_t bgra)
nkeynes@653
    31
{
nkeynes@653
    32
    return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
nkeynes@653
    33
}
nkeynes@653
    34
nkeynes@653
    35
/**
nkeynes@653
    36
 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
nkeynes@653
    37
 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
nkeynes@653
    38
 * TODO: Check the correctness of this.
nkeynes@653
    39
 */
nkeynes@653
    40
static float halftofloat( uint16_t half )
nkeynes@653
    41
{
nkeynes@653
    42
    union {
nkeynes@653
    43
        float f;
nkeynes@653
    44
        uint32_t i;
nkeynes@653
    45
    } temp;
nkeynes@653
    46
    temp.i = ((uint32_t)half)<<16;
nkeynes@653
    47
    return temp.f;
nkeynes@653
    48
}
nkeynes@653
    49
nkeynes@653
    50
nkeynes@653
    51
nkeynes@653
    52
nkeynes@653
    53
nkeynes@653
    54
struct pvr2_scene_struct pvr2_scene;
nkeynes@653
    55
nkeynes@653
    56
static gboolean vbo_init = FALSE;
nkeynes@669
    57
nkeynes@669
    58
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
    59
static gboolean vbo_supported = FALSE;
nkeynes@669
    60
#endif
nkeynes@653
    61
nkeynes@653
    62
/**
nkeynes@653
    63
 * Test for VBO support, and allocate all the system memory needed for the
nkeynes@653
    64
 * temporary structures. GL context must have been initialized before this
nkeynes@653
    65
 * point.
nkeynes@653
    66
 */
nkeynes@653
    67
void pvr2_scene_init()
nkeynes@653
    68
{
nkeynes@653
    69
    if( !vbo_init ) {
nkeynes@653
    70
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@667
    71
	if( isGLVertexBufferSupported() ) {
nkeynes@653
    72
	    vbo_supported = TRUE;
nkeynes@653
    73
	    pvr2_scene.vbo_id = 1;
nkeynes@653
    74
	}
nkeynes@653
    75
#endif
nkeynes@653
    76
	pvr2_scene.vertex_array = NULL;
nkeynes@653
    77
	pvr2_scene.vertex_array_size = 0;
nkeynes@653
    78
	pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
nkeynes@653
    79
	pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
nkeynes@653
    80
	vbo_init = TRUE;
nkeynes@653
    81
    }
nkeynes@653
    82
}
nkeynes@653
    83
nkeynes@653
    84
/**
nkeynes@653
    85
 * Clear the scene data structures in preparation for fresh data
nkeynes@653
    86
 */
nkeynes@653
    87
void pvr2_scene_reset()
nkeynes@653
    88
{
nkeynes@653
    89
    pvr2_scene.poly_count = 0;
nkeynes@653
    90
    pvr2_scene.vertex_count = 0;
nkeynes@653
    91
    memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
nkeynes@653
    92
}
nkeynes@653
    93
nkeynes@653
    94
void pvr2_scene_shutdown()
nkeynes@653
    95
{
nkeynes@653
    96
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
    97
    if( vbo_supported ) {
nkeynes@653
    98
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
nkeynes@653
    99
	glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
nkeynes@653
   100
	pvr2_scene.vbo_id = 0;
nkeynes@653
   101
    } else {
nkeynes@653
   102
#endif
nkeynes@653
   103
	g_free( pvr2_scene.vertex_array );
nkeynes@653
   104
	pvr2_scene.vertex_array = NULL;
nkeynes@653
   105
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
   106
    }
nkeynes@653
   107
#endif
nkeynes@653
   108
nkeynes@653
   109
    g_free( pvr2_scene.poly_array );
nkeynes@653
   110
    pvr2_scene.poly_array = NULL;
nkeynes@653
   111
    g_free( pvr2_scene.buf_to_poly_map );
nkeynes@653
   112
    pvr2_scene.buf_to_poly_map = NULL;
nkeynes@653
   113
    vbo_init = FALSE;
nkeynes@653
   114
}
nkeynes@653
   115
nkeynes@653
   116
void *vertex_buffer_map()
nkeynes@653
   117
{
nkeynes@653
   118
    glGetError();
nkeynes@653
   119
    uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
nkeynes@653
   120
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
   121
    if( vbo_supported ) {
nkeynes@653
   122
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
nkeynes@653
   123
	if( size > pvr2_scene.vertex_array_size ) {
nkeynes@653
   124
	    glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
nkeynes@653
   125
	    int status = glGetError();
nkeynes@653
   126
	    if( status != 0 ) {
nkeynes@653
   127
		fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
nkeynes@653
   128
		abort();
nkeynes@653
   129
	    }
nkeynes@653
   130
	    pvr2_scene.vertex_array_size = size;
nkeynes@653
   131
	}
nkeynes@653
   132
	pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
nkeynes@653
   133
	assert(pvr2_scene.vertex_array != NULL );
nkeynes@653
   134
    } else {
nkeynes@653
   135
#endif
nkeynes@653
   136
	if( size > pvr2_scene.vertex_array_size ) {
nkeynes@653
   137
	    pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
nkeynes@653
   138
	}
nkeynes@653
   139
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
   140
    }
nkeynes@653
   141
#endif
nkeynes@653
   142
    return pvr2_scene.vertex_array;
nkeynes@653
   143
}
nkeynes@653
   144
nkeynes@653
   145
gboolean vertex_buffer_unmap()
nkeynes@653
   146
{
nkeynes@653
   147
#ifdef ENABLE_VERTEX_BUFFER
nkeynes@653
   148
    if( vbo_supported ) {
nkeynes@653
   149
	pvr2_scene.vertex_array = NULL;
nkeynes@653
   150
	return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
nkeynes@653
   151
    } else {
nkeynes@653
   152
	return TRUE;
nkeynes@653
   153
    }
nkeynes@653
   154
#else
nkeynes@653
   155
    return TRUE;
nkeynes@653
   156
#endif
nkeynes@653
   157
}
nkeynes@653
   158
nkeynes@653
   159
static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
nkeynes@653
   160
							 gboolean is_modified ) 
nkeynes@653
   161
{
nkeynes@653
   162
    int vert_mul = is_modified ? 2 : 1;
nkeynes@653
   163
nkeynes@653
   164
    if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
nkeynes@653
   165
	if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
nkeynes@653
   166
	    pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
nkeynes@653
   167
	    pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
nkeynes@653
   168
	}
nkeynes@653
   169
	return pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@653
   170
    } else {
nkeynes@653
   171
	struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
nkeynes@653
   172
	poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
nkeynes@653
   173
	poly->vertex_count = vertex_count;
nkeynes@653
   174
	poly->vertex_index = -1;
nkeynes@653
   175
	poly->mod_vertex_index = -1;
nkeynes@653
   176
	poly->next = NULL;
nkeynes@653
   177
	pvr2_scene.buf_to_poly_map[poly_idx] = poly;
nkeynes@653
   178
	pvr2_scene.vertex_count += (vertex_count * vert_mul);
nkeynes@653
   179
	return poly;
nkeynes@653
   180
    }
nkeynes@653
   181
}
nkeynes@653
   182
nkeynes@653
   183
/**
nkeynes@653
   184
 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
nkeynes@653
   185
 * volume)
nkeynes@653
   186
 * @param vert Pointer to output vertex structure
nkeynes@653
   187
 * @param poly1 First word of polygon context (needed to understand vertex)
nkeynes@653
   188
 * @param poly2 Second word of polygon context
nkeynes@653
   189
 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
nkeynes@653
   190
 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
nkeynes@653
   191
 *        the normal vertex, half the vertex length for the modified vertex.
nkeynes@653
   192
 */
nkeynes@653
   193
static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1, 
nkeynes@653
   194
				       uint32_t poly2, uint32_t *pvr2_data, 
nkeynes@653
   195
				       int modify_offset )
nkeynes@653
   196
{
nkeynes@653
   197
    gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
nkeynes@653
   198
    union pvr2_data_type {
nkeynes@653
   199
	uint32_t *ival;
nkeynes@653
   200
	float *fval;
nkeynes@653
   201
    } data;
nkeynes@653
   202
nkeynes@653
   203
    data.ival = pvr2_data;
nkeynes@653
   204
    
nkeynes@653
   205
    vert->x = *data.fval++;
nkeynes@653
   206
    vert->y = *data.fval++;
nkeynes@653
   207
nkeynes@653
   208
    float z = *data.fval++;
nkeynes@653
   209
    if( !isfinite(z) ) {
nkeynes@653
   210
	z = 0;
nkeynes@653
   211
    } else if( z != 0 ) {
nkeynes@653
   212
	z = 1/z;
nkeynes@653
   213
    }
nkeynes@653
   214
    if( z > pvr2_scene.bounds[5] ) {
nkeynes@653
   215
	pvr2_scene.bounds[5] = z;
nkeynes@653
   216
    } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
nkeynes@653
   217
	pvr2_scene.bounds[4] = z;
nkeynes@653
   218
    }
nkeynes@653
   219
    vert->z = z;
nkeynes@653
   220
    data.ival += modify_offset;
nkeynes@653
   221
nkeynes@653
   222
    
nkeynes@653
   223
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@653
   224
	if( POLY1_UV16(poly1) ) {
nkeynes@653
   225
	    vert->u = halftofloat( *data.ival>>16 );
nkeynes@653
   226
	    vert->v = halftofloat( *data.ival );
nkeynes@653
   227
	    data.ival++;
nkeynes@653
   228
	} else {
nkeynes@653
   229
	    vert->u = *data.fval++;
nkeynes@653
   230
	    vert->v = *data.fval++;
nkeynes@653
   231
	}
nkeynes@653
   232
	if( POLY2_TEX_BLEND(poly2) == 1 ) {
nkeynes@653
   233
	    force_alpha = TRUE;
nkeynes@653
   234
	}
nkeynes@653
   235
    }
nkeynes@653
   236
    if( force_alpha ) {
nkeynes@653
   237
	vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
nkeynes@653
   238
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@653
   239
	    vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
nkeynes@653
   240
	} else {
nkeynes@653
   241
	    vert->offset_rgba = 0;
nkeynes@653
   242
	}
nkeynes@653
   243
    } else {
nkeynes@653
   244
	vert->rgba = bgra_to_rgba(*data.ival++);
nkeynes@653
   245
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@653
   246
	    vert->offset_rgba = bgra_to_rgba(*data.ival++);
nkeynes@653
   247
	} else {
nkeynes@653
   248
	    vert->offset_rgba = 0;
nkeynes@653
   249
	}
nkeynes@653
   250
    }
nkeynes@653
   251
}
nkeynes@653
   252
nkeynes@653
   253
/**
nkeynes@653
   254
 * Compute texture, colour, and z values for a result point by interpolating from
nkeynes@653
   255
 * a set of 3 input points. The result point must define its x,y.
nkeynes@653
   256
 */
nkeynes@653
   257
static void scene_compute_vertex( struct vertex_struct *result, 
nkeynes@653
   258
					  struct vertex_struct *input,
nkeynes@653
   259
					  gboolean is_solid_shaded )
nkeynes@653
   260
{
nkeynes@653
   261
    int i;
nkeynes@653
   262
    float sx = input[2].x - input[1].x;
nkeynes@653
   263
    float sy = input[2].y - input[1].y;
nkeynes@653
   264
    float tx = input[0].x - input[1].x;
nkeynes@653
   265
    float ty = input[0].y - input[1].y;
nkeynes@653
   266
nkeynes@653
   267
    float detxy = ((sy) * (tx)) - ((ty) * (sx));
nkeynes@653
   268
    if( detxy == 0 ) {
nkeynes@653
   269
	result->z = input[2].z;
nkeynes@653
   270
	result->u = input[2].u;
nkeynes@653
   271
	result->v = input[2].v;
nkeynes@653
   272
	result->rgba = input[2].rgba;
nkeynes@653
   273
	result->offset_rgba = input[2].offset_rgba;
nkeynes@653
   274
	return;
nkeynes@653
   275
    }
nkeynes@653
   276
    float t = ((result->x - input[1].x) * sy -
nkeynes@653
   277
	       (result->y - input[1].y) * sx) / detxy;
nkeynes@653
   278
    float s = ((result->y - input[1].y) * tx -
nkeynes@653
   279
	       (result->x - input[1].x) * ty) / detxy;
nkeynes@653
   280
nkeynes@653
   281
    float sz = input[2].z - input[1].z;
nkeynes@653
   282
    float tz = input[0].z - input[1].z;
nkeynes@653
   283
    float su = input[2].u - input[1].u;
nkeynes@653
   284
    float tu = input[0].u - input[1].u;
nkeynes@653
   285
    float sv = input[2].v - input[1].v;
nkeynes@653
   286
    float tv = input[0].v - input[1].v;
nkeynes@653
   287
nkeynes@653
   288
    float rz = input[1].z + (t*tz) + (s*sz);
nkeynes@653
   289
    if( rz > pvr2_scene.bounds[5] ) {
nkeynes@653
   290
	pvr2_scene.bounds[5] = rz;
nkeynes@653
   291
    } else if( rz < pvr2_scene.bounds[4] ) {
nkeynes@653
   292
	pvr2_scene.bounds[4] = rz; 
nkeynes@653
   293
    }
nkeynes@653
   294
    result->z = rz;
nkeynes@653
   295
    result->u = input[1].u + (t*tu) + (s*su);
nkeynes@653
   296
    result->v = input[1].v + (t*tv) + (s*sv);
nkeynes@653
   297
nkeynes@653
   298
    if( is_solid_shaded ) {
nkeynes@653
   299
	result->rgba = input[2].rgba;
nkeynes@653
   300
	result->offset_rgba = input[2].offset_rgba;
nkeynes@653
   301
    } else {
nkeynes@653
   302
	uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
nkeynes@653
   303
	uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
nkeynes@653
   304
	uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
nkeynes@653
   305
	uint8_t *rgba3 = (uint8_t *)&result->rgba;
nkeynes@653
   306
	for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
nkeynes@653
   307
	    float tc = *rgba0++ - *rgba1;
nkeynes@653
   308
	    float sc = *rgba2++ - *rgba1;
nkeynes@653
   309
	    float rc = *rgba1++ + (t*tc) + (s*sc);
nkeynes@653
   310
	    if( rc < 0 ) {
nkeynes@653
   311
		rc = 0;
nkeynes@653
   312
	    } else if( rc > 255 ) {
nkeynes@653
   313
		rc = 255;
nkeynes@653
   314
	    }
nkeynes@653
   315
	    *rgba3++ = rc;
nkeynes@653
   316
	}
nkeynes@653
   317
    }    
nkeynes@653
   318
nkeynes@653
   319
}
nkeynes@653
   320
nkeynes@653
   321
static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
nkeynes@653
   322
					gboolean is_modified )
nkeynes@653
   323
{
nkeynes@653
   324
    struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@653
   325
    uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
nkeynes@653
   326
    uint32_t *context = ptr;
nkeynes@653
   327
    unsigned int i;
nkeynes@653
   328
nkeynes@653
   329
    if( poly->vertex_index == -1 ) {
nkeynes@653
   330
	ptr += (is_modified ? 5 : 3 );
nkeynes@653
   331
	poly->vertex_index = pvr2_scene.vertex_index;
nkeynes@653
   332
	
nkeynes@653
   333
	assert( poly != NULL );
nkeynes@653
   334
	assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
nkeynes@653
   335
	for( i=0; i<poly->vertex_count; i++ ) {
nkeynes@653
   336
	    pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
nkeynes@653
   337
	    ptr += vertex_length;
nkeynes@653
   338
	}
nkeynes@653
   339
	if( is_modified ) {
nkeynes@653
   340
	    int mod_offset = (vertex_length - 3)>>1;
nkeynes@653
   341
	    assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
nkeynes@653
   342
	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
nkeynes@653
   343
	    poly->mod_vertex_index = pvr2_scene.vertex_index;
nkeynes@653
   344
	    for( i=0; i<poly->vertex_count; i++ ) {
nkeynes@653
   345
		pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
nkeynes@653
   346
		ptr += vertex_length;
nkeynes@653
   347
	    }
nkeynes@653
   348
	}
nkeynes@653
   349
    }
nkeynes@653
   350
}
nkeynes@653
   351
nkeynes@653
   352
static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length, 
nkeynes@653
   353
					     gboolean is_modified )
nkeynes@653
   354
{
nkeynes@653
   355
    struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@653
   356
    uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
nkeynes@653
   357
    uint32_t *context = ptr;
nkeynes@653
   358
    unsigned int i;
nkeynes@653
   359
nkeynes@653
   360
    if( poly->vertex_index == -1 ) {
nkeynes@653
   361
	// Construct it locally and copy to the vertex buffer, as the VBO is 
nkeynes@653
   362
	// allowed to be horribly slow for reads (ie it could be direct-mapped
nkeynes@653
   363
	// vram).
nkeynes@653
   364
	struct vertex_struct quad[4];
nkeynes@653
   365
	
nkeynes@653
   366
	assert( poly != NULL );
nkeynes@653
   367
	assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
nkeynes@653
   368
	ptr += (is_modified ? 5 : 3 );
nkeynes@653
   369
	poly->vertex_index = pvr2_scene.vertex_index;
nkeynes@653
   370
	for( i=0; i<4; i++ ) {
nkeynes@653
   371
	    pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
nkeynes@653
   372
	    ptr += vertex_length;
nkeynes@653
   373
	}
nkeynes@653
   374
	scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
nkeynes@653
   375
	// Swap last two vertexes (quad arrangement => tri strip arrangement)
nkeynes@653
   376
	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
nkeynes@653
   377
	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
nkeynes@653
   378
	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
nkeynes@653
   379
	pvr2_scene.vertex_index += 4;
nkeynes@653
   380
	
nkeynes@653
   381
	if( is_modified ) {
nkeynes@653
   382
	    int mod_offset = (vertex_length - 3)>>1;
nkeynes@653
   383
	    assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
nkeynes@653
   384
	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
nkeynes@653
   385
	    poly->mod_vertex_index = pvr2_scene.vertex_index;
nkeynes@653
   386
	    for( i=0; i<4; i++ ) {
nkeynes@653
   387
		pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
nkeynes@653
   388
		ptr += vertex_length;
nkeynes@653
   389
	    }
nkeynes@653
   390
	    scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
nkeynes@653
   391
	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
nkeynes@653
   392
	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
nkeynes@653
   393
	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
nkeynes@653
   394
	    pvr2_scene.vertex_index += 4;
nkeynes@653
   395
	}
nkeynes@653
   396
    }
nkeynes@653
   397
}
nkeynes@653
   398
nkeynes@653
   399
static void scene_extract_polygons( pvraddr_t tile_entry )
nkeynes@653
   400
{
nkeynes@653
   401
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@653
   402
    do {
nkeynes@653
   403
	uint32_t entry = *tile_list++;
nkeynes@653
   404
	if( entry >> 28 == 0x0F ) {
nkeynes@653
   405
	    break;
nkeynes@653
   406
	} else if( entry >> 28 == 0x0E ) {
nkeynes@653
   407
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@653
   408
	} else {
nkeynes@653
   409
	    pvraddr_t polyaddr = entry&0x000FFFFF;
nkeynes@653
   410
	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
nkeynes@653
   411
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@653
   412
	    int context_length = 3;
nkeynes@653
   413
	    if( is_modified ) {
nkeynes@653
   414
		context_length = 5;
nkeynes@653
   415
		vertex_length <<= 1 ;
nkeynes@653
   416
	    }
nkeynes@653
   417
	    vertex_length += 3;
nkeynes@653
   418
	    
nkeynes@653
   419
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@653
   420
		/* Triangle(s) */
nkeynes@653
   421
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@653
   422
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@653
   423
		int i;
nkeynes@653
   424
		struct polygon_struct *last_poly = NULL;
nkeynes@653
   425
		for( i=0; i<strip_count; i++ ) {
nkeynes@653
   426
		    struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
nkeynes@653
   427
		    polyaddr += polygon_length;
nkeynes@653
   428
		    if( last_poly != NULL && last_poly->next == NULL ) {
nkeynes@653
   429
			last_poly->next = poly;
nkeynes@653
   430
		    }
nkeynes@653
   431
		    last_poly = poly;
nkeynes@653
   432
		}
nkeynes@653
   433
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@653
   434
		/* Sprite(s) */
nkeynes@653
   435
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@653
   436
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@653
   437
		int i;
nkeynes@653
   438
		struct polygon_struct *last_poly = NULL;
nkeynes@653
   439
		for( i=0; i<strip_count; i++ ) {
nkeynes@653
   440
		    struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
nkeynes@653
   441
		    polyaddr += polygon_length;
nkeynes@653
   442
		    if( last_poly != NULL && last_poly->next == NULL ) {
nkeynes@653
   443
			last_poly->next = poly;
nkeynes@653
   444
		    }
nkeynes@653
   445
		    last_poly = poly;
nkeynes@653
   446
		}
nkeynes@653
   447
	    } else {
nkeynes@653
   448
		/* Polygon */
nkeynes@653
   449
		int i, last = -1;
nkeynes@653
   450
		for( i=5; i>=0; i-- ) {
nkeynes@653
   451
		    if( entry & (0x40000000>>i) ) {
nkeynes@653
   452
			last = i;
nkeynes@653
   453
			break;
nkeynes@653
   454
		    }
nkeynes@653
   455
		}
nkeynes@653
   456
		if( last != -1 ) {
nkeynes@653
   457
		    scene_add_polygon( polyaddr, last+3, is_modified );
nkeynes@653
   458
		}
nkeynes@653
   459
	    }
nkeynes@653
   460
	}
nkeynes@653
   461
    } while( 1 );
nkeynes@653
   462
}
nkeynes@653
   463
nkeynes@653
   464
static void scene_extract_vertexes( pvraddr_t tile_entry )
nkeynes@653
   465
{
nkeynes@653
   466
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@653
   467
    do {
nkeynes@653
   468
	uint32_t entry = *tile_list++;
nkeynes@653
   469
	if( entry >> 28 == 0x0F ) {
nkeynes@653
   470
	    break;
nkeynes@653
   471
	} else if( entry >> 28 == 0x0E ) {
nkeynes@653
   472
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@653
   473
	} else {
nkeynes@653
   474
	    pvraddr_t polyaddr = entry&0x000FFFFF;
nkeynes@653
   475
	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
nkeynes@653
   476
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@653
   477
	    int context_length = 3;
nkeynes@653
   478
	    if( is_modified ) {
nkeynes@653
   479
		context_length = 5;
nkeynes@653
   480
		vertex_length <<=1 ;
nkeynes@653
   481
	    }
nkeynes@653
   482
	    vertex_length += 3;
nkeynes@653
   483
	    
nkeynes@653
   484
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@653
   485
		/* Triangle(s) */
nkeynes@653
   486
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@653
   487
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@653
   488
		int i;
nkeynes@653
   489
		for( i=0; i<strip_count; i++ ) {
nkeynes@653
   490
		    scene_add_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@653
   491
		    polyaddr += polygon_length;
nkeynes@653
   492
		}
nkeynes@653
   493
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@653
   494
		/* Sprite(s) */
nkeynes@653
   495
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@653
   496
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@653
   497
		int i;
nkeynes@653
   498
		for( i=0; i<strip_count; i++ ) {
nkeynes@653
   499
		    scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@653
   500
		    polyaddr += polygon_length;
nkeynes@653
   501
		}
nkeynes@653
   502
	    } else {
nkeynes@653
   503
		/* Polygon */
nkeynes@653
   504
		int i, last = -1;
nkeynes@653
   505
		for( i=5; i>=0; i-- ) {
nkeynes@653
   506
		    if( entry & (0x40000000>>i) ) {
nkeynes@653
   507
			last = i;
nkeynes@653
   508
			break;
nkeynes@653
   509
		    }
nkeynes@653
   510
		}
nkeynes@653
   511
		if( last != -1 ) {
nkeynes@653
   512
		    scene_add_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@653
   513
		}
nkeynes@653
   514
	    }
nkeynes@653
   515
	}
nkeynes@653
   516
    } while( 1 );    
nkeynes@653
   517
}
nkeynes@653
   518
nkeynes@653
   519
uint32_t pvr2_scene_buffer_width()
nkeynes@653
   520
{
nkeynes@653
   521
    return pvr2_scene.buffer_width;
nkeynes@653
   522
}
nkeynes@653
   523
nkeynes@653
   524
uint32_t pvr2_scene_buffer_height()
nkeynes@653
   525
{
nkeynes@653
   526
    return pvr2_scene.buffer_height;
nkeynes@653
   527
}
nkeynes@653
   528
nkeynes@653
   529
/**
nkeynes@653
   530
 * Extract the current scene into the rendering structures. We run two passes
nkeynes@653
   531
 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts), 
nkeynes@653
   532
 * second pass extracts the vertex data into the VBO/vertex array.
nkeynes@653
   533
 *
nkeynes@653
   534
 * Difficult to do in single pass as we don't generally know the size of a 
nkeynes@653
   535
 * polygon for certain until we've seen all tiles containing it. It also means we
nkeynes@653
   536
 * can count the vertexes and allocate the appropriate size VBO.
nkeynes@653
   537
 *
nkeynes@653
   538
 * FIXME: accesses into VRAM need to be bounds-checked properly
nkeynes@653
   539
 */
nkeynes@653
   540
void pvr2_scene_read( void )
nkeynes@653
   541
{
nkeynes@653
   542
    pvr2_scene_init();
nkeynes@653
   543
    pvr2_scene_reset();
nkeynes@653
   544
nkeynes@653
   545
    pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
nkeynes@653
   546
    pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
nkeynes@653
   547
    pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
nkeynes@653
   548
    pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
nkeynes@653
   549
    pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
nkeynes@653
   550
nkeynes@653
   551
    uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
nkeynes@653
   552
    uint32_t *segment = tilebuffer;
nkeynes@653
   553
    pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
nkeynes@653
   554
    pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
nkeynes@653
   555
    pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
nkeynes@653
   556
   
nkeynes@653
   557
    int max_tile_x = 0;
nkeynes@653
   558
    int max_tile_y = 0;
nkeynes@653
   559
    int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
nkeynes@653
   560
    int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
nkeynes@653
   561
nkeynes@653
   562
    if( (obj_config & 0x00200000) == 0 ) {
nkeynes@653
   563
	if( isp_config & 1 ) {
nkeynes@653
   564
	    pvr2_scene.sort_mode = SORT_NEVER;
nkeynes@653
   565
	} else {
nkeynes@653
   566
	    pvr2_scene.sort_mode = SORT_ALWAYS;
nkeynes@653
   567
	}
nkeynes@653
   568
    } else {
nkeynes@653
   569
	pvr2_scene.sort_mode = SORT_TILEFLAG;
nkeynes@653
   570
    }
nkeynes@653
   571
nkeynes@653
   572
    // Pass 1: Extract polygon list 
nkeynes@653
   573
    uint32_t control;
nkeynes@653
   574
    int i;
nkeynes@653
   575
    do {
nkeynes@653
   576
	control = *segment++;
nkeynes@653
   577
	int tile_x = SEGMENT_X(control);
nkeynes@653
   578
	int tile_y = SEGMENT_Y(control);
nkeynes@653
   579
	if( tile_x > max_tile_x ) {
nkeynes@653
   580
	    max_tile_x = tile_x;
nkeynes@653
   581
	} 
nkeynes@653
   582
	if( tile_y > max_tile_y ) {
nkeynes@653
   583
	    max_tile_y = tile_y;
nkeynes@653
   584
	}
nkeynes@653
   585
	for( i=0; i<5; i++ ) {
nkeynes@653
   586
	    if( (*segment & NO_POINTER) == 0 ) {
nkeynes@653
   587
		scene_extract_polygons( *segment );
nkeynes@653
   588
	    }
nkeynes@653
   589
	    segment++;
nkeynes@653
   590
	}
nkeynes@653
   591
    } while( (control & SEGMENT_END) == 0 );
nkeynes@653
   592
nkeynes@653
   593
    pvr2_scene.buffer_width = (max_tile_x+1)<<5;
nkeynes@653
   594
    pvr2_scene.buffer_height = (max_tile_y+1)<<5;
nkeynes@653
   595
nkeynes@653
   596
    if( pvr2_scene.vertex_count > 0 ) {
nkeynes@653
   597
	// Pass 2: Extract vertex data
nkeynes@653
   598
	vertex_buffer_map();
nkeynes@653
   599
	pvr2_scene.vertex_index = 0;
nkeynes@653
   600
	segment = tilebuffer;
nkeynes@653
   601
	do {
nkeynes@653
   602
	    control = *segment++;
nkeynes@653
   603
	    for( i=0; i<5; i++ ) {
nkeynes@653
   604
		if( (*segment & NO_POINTER) == 0 ) {
nkeynes@653
   605
		    scene_extract_vertexes( *segment );
nkeynes@653
   606
		}
nkeynes@653
   607
		segment++;
nkeynes@653
   608
	    }
nkeynes@653
   609
	} while( (control & SEGMENT_END) == 0 );
nkeynes@653
   610
	vertex_buffer_unmap();
nkeynes@653
   611
    }
nkeynes@653
   612
}
nkeynes@653
   613
nkeynes@653
   614
/**
nkeynes@653
   615
 * Dump the current scene to file in a (mostly) human readable form
nkeynes@653
   616
 */
nkeynes@653
   617
void pvr2_scene_dump( FILE *f )
nkeynes@653
   618
{
nkeynes@653
   619
    int i,j;
nkeynes@653
   620
nkeynes@653
   621
    fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
nkeynes@653
   622
    for( i=0; i<pvr2_scene.poly_count; i++ ) {
nkeynes@653
   623
	struct polygon_struct *poly = &pvr2_scene.poly_array[i];
nkeynes@669
   624
	fprintf( f, "  %08X ", ((unsigned char *)poly->context) - video_base );
nkeynes@653
   625
	switch( poly->vertex_count ) {
nkeynes@653
   626
	case 3: fprintf( f, "Tri     " ); break;
nkeynes@653
   627
	case 4: fprintf( f, "Quad    " ); break;
nkeynes@653
   628
	default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
nkeynes@653
   629
	}
nkeynes@653
   630
	fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
nkeynes@653
   631
	if( poly->mod_vertex_index != -1 ) {
nkeynes@653
   632
	    fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
nkeynes@653
   633
	} else {
nkeynes@653
   634
	    fprintf( f, "\n" );
nkeynes@653
   635
	}
nkeynes@653
   636
	
nkeynes@653
   637
	for( j=0; j<poly->vertex_count; j++ ) {
nkeynes@653
   638
	    struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
nkeynes@653
   639
	    fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
nkeynes@653
   640
		     v->rgba, v->offset_rgba );
nkeynes@653
   641
	}
nkeynes@653
   642
	if( poly->mod_vertex_index != -1 ) {
nkeynes@653
   643
	    fprintf( f, "  ---\n" );
nkeynes@653
   644
	    for( j=0; j<poly->vertex_count; j++ ) {
nkeynes@653
   645
		struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
nkeynes@653
   646
		fprintf( f, "    %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
nkeynes@653
   647
			 v->rgba, v->offset_rgba );
nkeynes@653
   648
	    }
nkeynes@653
   649
	}
nkeynes@653
   650
    }
nkeynes@653
   651
nkeynes@653
   652
}
.