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