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