Search
lxdream.org :: lxdream/src/pvr2/scene.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/scene.c
changeset 635:76c63aac3590
next636:2ccf94f966fc
author nkeynes
date Thu Feb 14 13:54:11 2008 +0000 (13 years ago)
branchlxdream-render
permissions -rw-r--r--
last change Commit render work in progress. Main changes:
* Preliminary OSMesa support
* Move the generic gl code out to pvr2/
* Implement scene data structure + reader
* Remove the 1/z adjustments
file annotate diff log raw
nkeynes@635
     1
/**
nkeynes@635
     2
 * $Id: gl_common.c 602 2008-01-15 20:50:23Z nkeynes $
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@635
    22
#include "lxdream.h"
nkeynes@635
    23
#include "display.h"
nkeynes@635
    24
#include "pvr2/pvr2.h"
nkeynes@635
    25
#include "pvr2/glutil.h"
nkeynes@635
    26
#include "pvr2/scene.h"
nkeynes@635
    27
nkeynes@635
    28
#define VBO_EXT_STRING "GL_ARB_vertex_buffer_object"
nkeynes@635
    29
#define PBO_EXT_STRING "GL_ARB_pixel_buffer_object"
nkeynes@635
    30
nkeynes@635
    31
struct pvr2_scene_struct pvr2_scene;
nkeynes@635
    32
nkeynes@635
    33
static gboolean vbo_init = FALSE;
nkeynes@635
    34
static gboolean vbo_supported = FALSE;
nkeynes@635
    35
nkeynes@635
    36
/**
nkeynes@635
    37
 * Test for VBO support, and allocate all the system memory needed for the
nkeynes@635
    38
 * temporary structures. GL context must have been initialized before this
nkeynes@635
    39
 * point.
nkeynes@635
    40
 */
nkeynes@635
    41
void pvr2_scene_init()
nkeynes@635
    42
{
nkeynes@635
    43
    if( !vbo_init ) {
nkeynes@635
    44
	if( isGLExtensionSupported(VBO_EXT_STRING) ) {
nkeynes@635
    45
	    vbo_supported = TRUE;
nkeynes@635
    46
	    pvr2_scene.vbo_id = 1;
nkeynes@635
    47
	}
nkeynes@635
    48
	pvr2_scene.vertex_array = NULL;
nkeynes@635
    49
	pvr2_scene.vertex_array_size = 0;
nkeynes@635
    50
	pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
nkeynes@635
    51
	pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
nkeynes@635
    52
	vbo_init = TRUE;
nkeynes@635
    53
    }
nkeynes@635
    54
}
nkeynes@635
    55
nkeynes@635
    56
void pvr2_scene_shutdown()
nkeynes@635
    57
{
nkeynes@635
    58
    if( vbo_supported ) {
nkeynes@635
    59
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
nkeynes@635
    60
	glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
nkeynes@635
    61
	pvr2_scene.vbo_id = 0;
nkeynes@635
    62
    } else {
nkeynes@635
    63
	g_free( pvr2_scene.vertex_array );
nkeynes@635
    64
	pvr2_scene.vertex_array = NULL;
nkeynes@635
    65
    }
nkeynes@635
    66
    g_free( pvr2_scene.poly_array );
nkeynes@635
    67
    g_free( pvr2_scene.buf_to_poly_map );
nkeynes@635
    68
    vbo_init = FALSE;
nkeynes@635
    69
}
nkeynes@635
    70
nkeynes@635
    71
void *vertex_buffer_map()
nkeynes@635
    72
{
nkeynes@635
    73
    uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
nkeynes@635
    74
    if( vbo_supported ) {
nkeynes@635
    75
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
nkeynes@635
    76
	
nkeynes@635
    77
	if( size > pvr2_scene.vertex_array_size ) {
nkeynes@635
    78
	    glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
nkeynes@635
    79
	    assert( glGetError() == 0 );
nkeynes@635
    80
	}
nkeynes@635
    81
	pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
nkeynes@635
    82
	assert(pvr2_scene.vertex_array != NULL );
nkeynes@635
    83
    } else {
nkeynes@635
    84
	if( size > pvr2_scene.vertex_array_size ) {
nkeynes@635
    85
	    pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
nkeynes@635
    86
	}
nkeynes@635
    87
    }
nkeynes@635
    88
    return pvr2_scene.vertex_array;
nkeynes@635
    89
}
nkeynes@635
    90
nkeynes@635
    91
gboolean vertex_buffer_unmap()
nkeynes@635
    92
{
nkeynes@635
    93
    if( vbo_supported ) {
nkeynes@635
    94
	pvr2_scene.vertex_array = NULL;
nkeynes@635
    95
	return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
nkeynes@635
    96
    } else {
nkeynes@635
    97
	return TRUE;
nkeynes@635
    98
    }
nkeynes@635
    99
}
nkeynes@635
   100
nkeynes@635
   101
static struct polygon_struct *vertex_buffer_add_polygon( pvraddr_t poly_idx, int vertex_count,
nkeynes@635
   102
							 gboolean is_modified ) 
nkeynes@635
   103
{
nkeynes@635
   104
    int vert_mul = is_modified ? 2 : 1;
nkeynes@635
   105
nkeynes@635
   106
    if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
nkeynes@635
   107
	if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
nkeynes@635
   108
	    pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
nkeynes@635
   109
	    pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
nkeynes@635
   110
	}
nkeynes@635
   111
	return pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@635
   112
    } else {
nkeynes@635
   113
	struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
nkeynes@635
   114
	poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
nkeynes@635
   115
	poly->vertex_count = vertex_count;
nkeynes@635
   116
	poly->vertex_index = -1;
nkeynes@635
   117
	pvr2_scene.buf_to_poly_map[poly_idx] = poly;
nkeynes@635
   118
	pvr2_scene.vertex_count += (vertex_count * vert_mul);
nkeynes@635
   119
	return poly;
nkeynes@635
   120
    }
nkeynes@635
   121
}
nkeynes@635
   122
nkeynes@635
   123
/**
nkeynes@635
   124
 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
nkeynes@635
   125
 * volume)
nkeynes@635
   126
 * @param vert Pointer to output vertex structure
nkeynes@635
   127
 * @param poly1 First word of polygon context (needed to understand vertex)
nkeynes@635
   128
 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
nkeynes@635
   129
 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
nkeynes@635
   130
 *        the normal vertex, half the vertex length for the modified vertex.
nkeynes@635
   131
 */
nkeynes@635
   132
static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1, 
nkeynes@635
   133
				       uint32_t *pvr2_data, int modify_offset )
nkeynes@635
   134
{
nkeynes@635
   135
    union pvr2_data_type {
nkeynes@635
   136
	uint32_t *ival;
nkeynes@635
   137
	float *fval;
nkeynes@635
   138
    } data;
nkeynes@635
   139
nkeynes@635
   140
    data.ival = pvr2_data;
nkeynes@635
   141
    
nkeynes@635
   142
    vert->x = *data.fval++;
nkeynes@635
   143
    vert->y = *data.fval++;
nkeynes@635
   144
nkeynes@635
   145
    float z = *data.fval++;
nkeynes@635
   146
    if( z > pvr2_scene.bounds[5] ) {
nkeynes@635
   147
	pvr2_scene.bounds[5] = z;
nkeynes@635
   148
    } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
nkeynes@635
   149
	pvr2_scene.bounds[4] = z;
nkeynes@635
   150
    }
nkeynes@635
   151
    vert->z = z;
nkeynes@635
   152
    data.ival += modify_offset;
nkeynes@635
   153
nkeynes@635
   154
    
nkeynes@635
   155
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@635
   156
	if( POLY1_UV16(poly1) ) {
nkeynes@635
   157
	    vert->u = halftofloat( *data.ival>>16 );
nkeynes@635
   158
	    vert->v = halftofloat( *data.ival );
nkeynes@635
   159
	    data.ival++;
nkeynes@635
   160
	} else {
nkeynes@635
   161
	    vert->u = *data.fval++;
nkeynes@635
   162
	    vert->v = *data.fval++;
nkeynes@635
   163
	}
nkeynes@635
   164
    }
nkeynes@635
   165
    vert->rgba = *data.ival++;
nkeynes@635
   166
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@635
   167
	vert->offset_rgba = *data.ival++;
nkeynes@635
   168
    }
nkeynes@635
   169
}
nkeynes@635
   170
nkeynes@635
   171
/**
nkeynes@635
   172
 * Compute texture, colour, and z values for a result point by interpolating from
nkeynes@635
   173
 * a set of 3 input points. The result point must define its x,y.
nkeynes@635
   174
 */
nkeynes@635
   175
static void vertex_buffer_compute_vertex( struct vertex_struct *result, 
nkeynes@635
   176
					  struct vertex_struct *input,
nkeynes@635
   177
					  gboolean is_solid_shaded )
nkeynes@635
   178
{
nkeynes@635
   179
    int i;
nkeynes@635
   180
    float sx = input[2].x - input[1].x;
nkeynes@635
   181
    float sy = input[2].y - input[1].y;
nkeynes@635
   182
    float tx = input[0].x - input[1].x;
nkeynes@635
   183
    float ty = input[0].y - input[1].y;
nkeynes@635
   184
nkeynes@635
   185
    float detxy = ((sy) * (tx)) - ((ty) * (sx));
nkeynes@635
   186
    if( detxy == 0 ) {
nkeynes@635
   187
	result->z = input[2].z;
nkeynes@635
   188
	result->u = input[2].u;
nkeynes@635
   189
	result->v = input[2].v;
nkeynes@635
   190
	result->rgba = input[2].rgba;
nkeynes@635
   191
	result->offset_rgba = input[2].offset_rgba;
nkeynes@635
   192
	return;
nkeynes@635
   193
    }
nkeynes@635
   194
    float t = ((result->x - input[1].x) * sy -
nkeynes@635
   195
	       (result->y - input[1].y) * sx) / detxy;
nkeynes@635
   196
    float s = ((result->y - input[1].y) * tx -
nkeynes@635
   197
	       (result->x - input[1].x) * ty) / detxy;
nkeynes@635
   198
nkeynes@635
   199
    float sz = input[2].z - input[1].z;
nkeynes@635
   200
    float tz = input[0].z - input[1].z;
nkeynes@635
   201
    float su = input[2].u - input[1].u;
nkeynes@635
   202
    float tu = input[0].u - input[1].u;
nkeynes@635
   203
    float sv = input[2].v - input[1].v;
nkeynes@635
   204
    float tv = input[0].v - input[1].v;
nkeynes@635
   205
nkeynes@635
   206
    float rz = input[1].z + (t*tz) + (s*sz);
nkeynes@635
   207
    if( rz > pvr2_scene.bounds[5] ) {
nkeynes@635
   208
	pvr2_scene.bounds[5] = rz;
nkeynes@635
   209
    } else if( rz < pvr2_scene.bounds[4] ) {
nkeynes@635
   210
	pvr2_scene.bounds[4] = rz; 
nkeynes@635
   211
    }
nkeynes@635
   212
    result->z = rz;
nkeynes@635
   213
    result->u = input[1].u + (t*tu) + (s*su);
nkeynes@635
   214
    result->v = input[1].v + (t*tv) + (s*sv);
nkeynes@635
   215
nkeynes@635
   216
    if( is_solid_shaded ) {
nkeynes@635
   217
	result->rgba = input[2].rgba;
nkeynes@635
   218
	result->offset_rgba = input[2].offset_rgba;
nkeynes@635
   219
    } else {
nkeynes@635
   220
	uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
nkeynes@635
   221
	uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
nkeynes@635
   222
	uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
nkeynes@635
   223
	uint8_t *rgba3 = (uint8_t *)&result->rgba;
nkeynes@635
   224
	for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
nkeynes@635
   225
	    float tc = *rgba0++ - *rgba1;
nkeynes@635
   226
	    float sc = *rgba2++ - *rgba1;
nkeynes@635
   227
	    float rc = *rgba1++ + (t*tc) + (s*sc);
nkeynes@635
   228
	    if( rc < 0 ) {
nkeynes@635
   229
		rc = 0;
nkeynes@635
   230
	    } else if( rc > 255 ) {
nkeynes@635
   231
		rc = 255;
nkeynes@635
   232
	    }
nkeynes@635
   233
	    *rgba3++ = rc;
nkeynes@635
   234
	}
nkeynes@635
   235
    }    
nkeynes@635
   236
nkeynes@635
   237
}
nkeynes@635
   238
nkeynes@635
   239
static void vertex_buffer_add_vertexes( pvraddr_t poly_idx, int vertex_length,
nkeynes@635
   240
					gboolean is_modified )
nkeynes@635
   241
{
nkeynes@635
   242
    struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@635
   243
    uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
nkeynes@635
   244
    uint32_t *context = ptr;
nkeynes@635
   245
    unsigned int i;
nkeynes@635
   246
nkeynes@635
   247
    assert( poly != NULL );
nkeynes@635
   248
    if( poly->vertex_index == -1 ) {
nkeynes@635
   249
	ptr += (is_modified ? 5 : 3 );
nkeynes@635
   250
	poly->vertex_index = pvr2_scene.vertex_index;
nkeynes@635
   251
	assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
nkeynes@635
   252
	for( i=0; i<poly->vertex_count; i++ ) {
nkeynes@635
   253
	    pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], ptr, 0 );
nkeynes@635
   254
	    ptr += vertex_length;
nkeynes@635
   255
	}
nkeynes@635
   256
	if( is_modified ) {
nkeynes@635
   257
	    int mod_offset = (vertex_length - 3)>>1;
nkeynes@635
   258
	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
nkeynes@635
   259
	    poly->mod_vertex_index = pvr2_scene.vertex_index;
nkeynes@635
   260
	    for( i=0; i<poly->vertex_count; i++ ) {
nkeynes@635
   261
		pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], ptr, mod_offset );
nkeynes@635
   262
		ptr += vertex_length;
nkeynes@635
   263
	    }
nkeynes@635
   264
	}
nkeynes@635
   265
    }
nkeynes@635
   266
}
nkeynes@635
   267
nkeynes@635
   268
static void vertex_buffer_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length, 
nkeynes@635
   269
					     gboolean is_modified )
nkeynes@635
   270
{
nkeynes@635
   271
    struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
nkeynes@635
   272
    uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
nkeynes@635
   273
    uint32_t *context = ptr;
nkeynes@635
   274
    unsigned int i;
nkeynes@635
   275
nkeynes@635
   276
    if( poly->vertex_index == -1 ) {
nkeynes@635
   277
	// Construct it locally and copy to the vertex buffer, as the VBO is 
nkeynes@635
   278
	// allowed to be horribly slow for reads (ie it could be direct-mapped
nkeynes@635
   279
	// vram).
nkeynes@635
   280
	struct vertex_struct quad[4];
nkeynes@635
   281
	
nkeynes@635
   282
	assert( poly != NULL );
nkeynes@635
   283
	ptr += (is_modified ? 5 : 3 );
nkeynes@635
   284
	poly->vertex_index = pvr2_scene.vertex_index;
nkeynes@635
   285
	for( i=0; i<4; i++ ) {
nkeynes@635
   286
	    pvr2_decode_render_vertex( &quad[i], context[0], ptr, 0 );
nkeynes@635
   287
	    ptr += vertex_length;
nkeynes@635
   288
	}
nkeynes@635
   289
	vertex_buffer_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
nkeynes@635
   290
	memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(quad) );
nkeynes@635
   291
	pvr2_scene.vertex_index += 4;
nkeynes@635
   292
	
nkeynes@635
   293
	if( is_modified ) {
nkeynes@635
   294
	    int mod_offset = (vertex_length - 3)>>1;
nkeynes@635
   295
	    ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
nkeynes@635
   296
	    poly->mod_vertex_index = pvr2_scene.vertex_index;
nkeynes@635
   297
	    for( i=0; i<4; i++ ) {
nkeynes@635
   298
		pvr2_decode_render_vertex( &quad[4], context[0], ptr, mod_offset );
nkeynes@635
   299
		ptr += vertex_length;
nkeynes@635
   300
	    }
nkeynes@635
   301
	    vertex_buffer_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
nkeynes@635
   302
	    memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(quad) );
nkeynes@635
   303
	    pvr2_scene.vertex_index += 4;
nkeynes@635
   304
	}
nkeynes@635
   305
    }
nkeynes@635
   306
}
nkeynes@635
   307
nkeynes@635
   308
static void vertex_buffer_extract_polygons( pvraddr_t tile_entry )
nkeynes@635
   309
{
nkeynes@635
   310
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@635
   311
    do {
nkeynes@635
   312
	uint32_t entry = *tile_list++;
nkeynes@635
   313
	if( entry >> 28 == 0x0F ) {
nkeynes@635
   314
	    break;
nkeynes@635
   315
	} else if( entry >> 28 == 0x0E ) {
nkeynes@635
   316
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@635
   317
	} else {
nkeynes@635
   318
	    pvraddr_t polyaddr = entry&0x000FFFFF;
nkeynes@635
   319
	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
nkeynes@635
   320
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@635
   321
	    int context_length = 3;
nkeynes@635
   322
	    if( is_modified ) {
nkeynes@635
   323
		context_length = 5;
nkeynes@635
   324
		vertex_length <<= 1 ;
nkeynes@635
   325
	    }
nkeynes@635
   326
	    vertex_length += 3;
nkeynes@635
   327
	    
nkeynes@635
   328
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@635
   329
		/* Triangle(s) */
nkeynes@635
   330
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@635
   331
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@635
   332
		int i;
nkeynes@635
   333
		struct polygon_struct *last_poly = NULL;
nkeynes@635
   334
		for( i=0; i<strip_count; i++ ) {
nkeynes@635
   335
		    struct polygon_struct *poly = vertex_buffer_add_polygon( polyaddr, 3, is_modified );
nkeynes@635
   336
		    polyaddr += polygon_length;
nkeynes@635
   337
		    if( last_poly != NULL && last_poly->next == NULL ) {
nkeynes@635
   338
			last_poly->next = poly;
nkeynes@635
   339
		    }
nkeynes@635
   340
		    last_poly = poly;
nkeynes@635
   341
		}
nkeynes@635
   342
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@635
   343
		/* Sprite(s) */
nkeynes@635
   344
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@635
   345
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@635
   346
		int i;
nkeynes@635
   347
		struct polygon_struct *last_poly = NULL;
nkeynes@635
   348
		for( i=0; i<strip_count; i++ ) {
nkeynes@635
   349
		    struct polygon_struct *poly = vertex_buffer_add_polygon( polyaddr, 4, is_modified );
nkeynes@635
   350
		    polyaddr += polygon_length;
nkeynes@635
   351
		    if( last_poly != NULL && last_poly->next == NULL ) {
nkeynes@635
   352
			last_poly->next = poly;
nkeynes@635
   353
		    }
nkeynes@635
   354
		    last_poly = poly;
nkeynes@635
   355
		}
nkeynes@635
   356
	    } else {
nkeynes@635
   357
		/* Polygon */
nkeynes@635
   358
		int i, last = -1;
nkeynes@635
   359
		for( i=5; i>=0; i-- ) {
nkeynes@635
   360
		    if( entry & (0x40000000>>i) ) {
nkeynes@635
   361
			last = i;
nkeynes@635
   362
			break;
nkeynes@635
   363
		    }
nkeynes@635
   364
		}
nkeynes@635
   365
		if( last != -1 ) {
nkeynes@635
   366
		    vertex_buffer_add_polygon( polyaddr, last+3, is_modified );
nkeynes@635
   367
		}
nkeynes@635
   368
	    }
nkeynes@635
   369
	}
nkeynes@635
   370
    } while( 1 );
nkeynes@635
   371
}
nkeynes@635
   372
nkeynes@635
   373
static void vertex_buffer_extract_vertexes( pvraddr_t tile_entry )
nkeynes@635
   374
{
nkeynes@635
   375
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@635
   376
    do {
nkeynes@635
   377
	uint32_t entry = *tile_list++;
nkeynes@635
   378
	if( entry >> 28 == 0x0F ) {
nkeynes@635
   379
	    break;
nkeynes@635
   380
	} else if( entry >> 28 == 0x0E ) {
nkeynes@635
   381
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@635
   382
	} else {
nkeynes@635
   383
	    pvraddr_t polyaddr = entry&0x000FFFFF;
nkeynes@635
   384
	    int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
nkeynes@635
   385
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@635
   386
	    int context_length = 3;
nkeynes@635
   387
	    if( is_modified ) {
nkeynes@635
   388
		context_length = 5;
nkeynes@635
   389
		vertex_length <<=1 ;
nkeynes@635
   390
	    }
nkeynes@635
   391
	    vertex_length += 3;
nkeynes@635
   392
	    
nkeynes@635
   393
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@635
   394
		/* Triangle(s) */
nkeynes@635
   395
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@635
   396
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@635
   397
		int i;
nkeynes@635
   398
		for( i=0; i<strip_count; i++ ) {
nkeynes@635
   399
		    vertex_buffer_add_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@635
   400
		    polyaddr += polygon_length;
nkeynes@635
   401
		}
nkeynes@635
   402
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@635
   403
		/* Sprite(s) */
nkeynes@635
   404
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@635
   405
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@635
   406
		int i;
nkeynes@635
   407
		for( i=0; i<strip_count; i++ ) {
nkeynes@635
   408
		    vertex_buffer_add_quad_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@635
   409
		    polyaddr += polygon_length;
nkeynes@635
   410
		}
nkeynes@635
   411
	    } else {
nkeynes@635
   412
		/* Polygon */
nkeynes@635
   413
		int i, last = -1;
nkeynes@635
   414
		for( i=5; i>=0; i-- ) {
nkeynes@635
   415
		    if( entry & (0x40000000>>i) ) {
nkeynes@635
   416
			last = i;
nkeynes@635
   417
			break;
nkeynes@635
   418
		    }
nkeynes@635
   419
		}
nkeynes@635
   420
		if( last != -1 ) {
nkeynes@635
   421
		    vertex_buffer_add_vertexes( polyaddr, vertex_length, is_modified );
nkeynes@635
   422
		}
nkeynes@635
   423
	    }
nkeynes@635
   424
	}
nkeynes@635
   425
    } while( 1 );    
nkeynes@635
   426
}
nkeynes@635
   427
nkeynes@635
   428
/**
nkeynes@635
   429
 * Extract the current scene into the rendering structures. We run two passes
nkeynes@635
   430
 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts), 
nkeynes@635
   431
 * second pass extracts the vertex data into the VBO/vertex array.
nkeynes@635
   432
 *
nkeynes@635
   433
 * Difficult to do in single pass as we don't generally know the size of a 
nkeynes@635
   434
 * polygon for certain until we've seen all tiles containing it. It also means we
nkeynes@635
   435
 * can count the vertexes and allocate the appropriate size VBO.
nkeynes@635
   436
 *
nkeynes@635
   437
 * FIXME: accesses into VRAM need to be bounds-checked properly
nkeynes@635
   438
 */
nkeynes@635
   439
void pvr2_scene_read( void )
nkeynes@635
   440
{
nkeynes@635
   441
    pvr2_scene_init();
nkeynes@635
   442
nkeynes@635
   443
    pvr2_scene.poly_count = 0;
nkeynes@635
   444
    pvr2_scene.vertex_count = 0;
nkeynes@635
   445
    pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
nkeynes@635
   446
    pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
nkeynes@635
   447
    pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
nkeynes@635
   448
    pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
nkeynes@635
   449
    pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
nkeynes@635
   450
nkeynes@635
   451
    uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
nkeynes@635
   452
    uint32_t *segment = tilebuffer;
nkeynes@635
   453
    pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
nkeynes@635
   454
    pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
nkeynes@635
   455
    pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
nkeynes@635
   456
   
nkeynes@635
   457
    int max_tile_x = 0;
nkeynes@635
   458
    int max_tile_y = 0;
nkeynes@635
   459
    int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
nkeynes@635
   460
    int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
nkeynes@635
   461
nkeynes@635
   462
    if( (obj_config & 0x00200000) == 0 ) {
nkeynes@635
   463
	if( isp_config & 1 ) {
nkeynes@635
   464
	    pvr2_scene.sort_mode = SORT_NEVER;
nkeynes@635
   465
	} else {
nkeynes@635
   466
	    pvr2_scene.sort_mode = SORT_ALWAYS;
nkeynes@635
   467
	}
nkeynes@635
   468
    } else {
nkeynes@635
   469
	pvr2_scene.sort_mode = SORT_BYFLAG;
nkeynes@635
   470
    }
nkeynes@635
   471
nkeynes@635
   472
    // Pass 1: Extract polygon list 
nkeynes@635
   473
    uint32_t control;
nkeynes@635
   474
    int i;
nkeynes@635
   475
    do {
nkeynes@635
   476
	control = *segment++;
nkeynes@635
   477
	int tile_x = SEGMENT_X(control);
nkeynes@635
   478
	int tile_y = SEGMENT_Y(control);
nkeynes@635
   479
	if( tile_x > max_tile_x ) {
nkeynes@635
   480
	    max_tile_x = tile_x;
nkeynes@635
   481
	} 
nkeynes@635
   482
	if( tile_y > max_tile_y ) {
nkeynes@635
   483
	    max_tile_y = tile_y;
nkeynes@635
   484
	}
nkeynes@635
   485
	for( i=0; i<5; i++ ) {
nkeynes@635
   486
	    if( (*segment & NO_POINTER) == 0 ) {
nkeynes@635
   487
		vertex_buffer_extract_polygons( *segment );
nkeynes@635
   488
	    }
nkeynes@635
   489
	    segment++;
nkeynes@635
   490
	}
nkeynes@635
   491
    } while( (control & SEGMENT_END) == 0 );
nkeynes@635
   492
nkeynes@635
   493
    pvr2_scene.buffer_width = (max_tile_x+1)<<5;
nkeynes@635
   494
    pvr2_scene.buffer_height = (max_tile_y+1)<<5;
nkeynes@635
   495
nkeynes@635
   496
    if( pvr2_scene.vertex_count > 0 ) {
nkeynes@635
   497
	// Pass 2: Extract vertex data
nkeynes@635
   498
	vertex_buffer_map();
nkeynes@635
   499
	pvr2_scene.vertex_index = 0;
nkeynes@635
   500
	segment = tilebuffer;
nkeynes@635
   501
	do {
nkeynes@635
   502
	    control = *segment++;
nkeynes@635
   503
	    for( i=0; i<5; i++ ) {
nkeynes@635
   504
		if( (*segment & NO_POINTER) == 0 ) {
nkeynes@635
   505
		    vertex_buffer_extract_vertexes( *segment );
nkeynes@635
   506
		}
nkeynes@635
   507
		segment++;
nkeynes@635
   508
	    }
nkeynes@635
   509
	} while( (control & SEGMENT_END) == 0 );
nkeynes@635
   510
	
nkeynes@635
   511
	vertex_buffer_unmap();
nkeynes@635
   512
    }
nkeynes@635
   513
}
nkeynes@635
   514
nkeynes@635
   515
/**
nkeynes@635
   516
 * Render the data in the scene structure. The GL target should already be setup.
nkeynes@635
   517
 * Note: thar be GL code here.
nkeynes@635
   518
 */
nkeynes@635
   519
void vertex_buffer_render()
nkeynes@635
   520
{
nkeynes@635
   521
    /* Scene setup */
nkeynes@635
   522
nkeynes@635
   523
    glEnable( GL_SCISSOR_TEST );
nkeynes@635
   524
nkeynes@635
   525
    /* Scene render */
nkeynes@635
   526
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@635
   527
    do {
nkeynes@635
   528
	
nkeynes@635
   529
nkeynes@635
   530
    } while( !IS_LAST_SEGMENT(segment) );
nkeynes@635
   531
    glDisable( GL_SCISSOR_TEST );
nkeynes@635
   532
}
.