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