Search
lxdream.org :: lxdream/src/pvr2/scene.c :: diff
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 (13 years ago)
permissions -rw-r--r--
last change Merge lxdream-render branch (643:670) to trunk
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/pvr2/scene.c Fri Mar 28 12:32:25 2008 +0000
1.3 @@ -0,0 +1,651 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Manage the internal vertex/polygon buffers and scene data structure.
1.8 + * Where possible this uses VBOs for the vertex + index data.
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +
1.23 +#include <assert.h>
1.24 +#include <string.h>
1.25 +#include <math.h>
1.26 +#include "lxdream.h"
1.27 +#include "display.h"
1.28 +#include "pvr2/pvr2.h"
1.29 +#include "pvr2/glutil.h"
1.30 +#include "pvr2/scene.h"
1.31 +
1.32 +#define VBO_EXT_STRING "GL_ARB_vertex_buffer_object"
1.33 +#define PBO_EXT_STRING "GL_ARB_pixel_buffer_object"
1.34 +
1.35 +static inline uint32_t bgra_to_rgba(uint32_t bgra)
1.36 +{
1.37 + return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
1.38 +}
1.39 +
1.40 +/**
1.41 + * Convert a half-float (16-bit) FP number to a regular 32-bit float.
1.42 + * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
1.43 + * TODO: Check the correctness of this.
1.44 + */
1.45 +static float halftofloat( uint16_t half )
1.46 +{
1.47 + union {
1.48 + float f;
1.49 + uint32_t i;
1.50 + } temp;
1.51 + temp.i = ((uint32_t)half)<<16;
1.52 + return temp.f;
1.53 +}
1.54 +
1.55 +
1.56 +
1.57 +
1.58 +
1.59 +struct pvr2_scene_struct pvr2_scene;
1.60 +
1.61 +static gboolean vbo_init = FALSE;
1.62 +static gboolean vbo_supported = FALSE;
1.63 +
1.64 +/**
1.65 + * Test for VBO support, and allocate all the system memory needed for the
1.66 + * temporary structures. GL context must have been initialized before this
1.67 + * point.
1.68 + */
1.69 +void pvr2_scene_init()
1.70 +{
1.71 + if( !vbo_init ) {
1.72 +#ifdef ENABLE_VERTEX_BUFFER
1.73 + if( isGLExtensionSupported(VBO_EXT_STRING) ) {
1.74 + vbo_supported = TRUE;
1.75 + pvr2_scene.vbo_id = 1;
1.76 + }
1.77 +#endif
1.78 + pvr2_scene.vertex_array = NULL;
1.79 + pvr2_scene.vertex_array_size = 0;
1.80 + pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
1.81 + pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
1.82 + vbo_init = TRUE;
1.83 + }
1.84 +}
1.85 +
1.86 +/**
1.87 + * Clear the scene data structures in preparation for fresh data
1.88 + */
1.89 +void pvr2_scene_reset()
1.90 +{
1.91 + pvr2_scene.poly_count = 0;
1.92 + pvr2_scene.vertex_count = 0;
1.93 + memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
1.94 +}
1.95 +
1.96 +void pvr2_scene_shutdown()
1.97 +{
1.98 +#ifdef ENABLE_VERTEX_BUFFER
1.99 + if( vbo_supported ) {
1.100 + glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1.101 + glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
1.102 + pvr2_scene.vbo_id = 0;
1.103 + } else {
1.104 +#endif
1.105 + g_free( pvr2_scene.vertex_array );
1.106 + pvr2_scene.vertex_array = NULL;
1.107 +#ifdef ENABLE_VERTEX_BUFFER
1.108 + }
1.109 +#endif
1.110 +
1.111 + g_free( pvr2_scene.poly_array );
1.112 + pvr2_scene.poly_array = NULL;
1.113 + g_free( pvr2_scene.buf_to_poly_map );
1.114 + pvr2_scene.buf_to_poly_map = NULL;
1.115 + vbo_init = FALSE;
1.116 +}
1.117 +
1.118 +void *vertex_buffer_map()
1.119 +{
1.120 + glGetError();
1.121 + uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
1.122 +#ifdef ENABLE_VERTEX_BUFFER
1.123 + if( vbo_supported ) {
1.124 + glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
1.125 + if( size > pvr2_scene.vertex_array_size ) {
1.126 + glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
1.127 + int status = glGetError();
1.128 + if( status != 0 ) {
1.129 + fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
1.130 + abort();
1.131 + }
1.132 + pvr2_scene.vertex_array_size = size;
1.133 + }
1.134 + pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
1.135 + assert(pvr2_scene.vertex_array != NULL );
1.136 + } else {
1.137 +#endif
1.138 + if( size > pvr2_scene.vertex_array_size ) {
1.139 + pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
1.140 + }
1.141 +#ifdef ENABLE_VERTEX_BUFFER
1.142 + }
1.143 +#endif
1.144 + return pvr2_scene.vertex_array;
1.145 +}
1.146 +
1.147 +gboolean vertex_buffer_unmap()
1.148 +{
1.149 +#ifdef ENABLE_VERTEX_BUFFER
1.150 + if( vbo_supported ) {
1.151 + pvr2_scene.vertex_array = NULL;
1.152 + return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
1.153 + } else {
1.154 + return TRUE;
1.155 + }
1.156 +#else
1.157 + return TRUE;
1.158 +#endif
1.159 +}
1.160 +
1.161 +static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
1.162 + gboolean is_modified )
1.163 +{
1.164 + int vert_mul = is_modified ? 2 : 1;
1.165 +
1.166 + if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
1.167 + if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
1.168 + pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
1.169 + pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
1.170 + }
1.171 + return pvr2_scene.buf_to_poly_map[poly_idx];
1.172 + } else {
1.173 + struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
1.174 + poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
1.175 + poly->vertex_count = vertex_count;
1.176 + poly->vertex_index = -1;
1.177 + poly->mod_vertex_index = -1;
1.178 + poly->next = NULL;
1.179 + pvr2_scene.buf_to_poly_map[poly_idx] = poly;
1.180 + pvr2_scene.vertex_count += (vertex_count * vert_mul);
1.181 + return poly;
1.182 + }
1.183 +}
1.184 +
1.185 +/**
1.186 + * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
1.187 + * volume)
1.188 + * @param vert Pointer to output vertex structure
1.189 + * @param poly1 First word of polygon context (needed to understand vertex)
1.190 + * @param poly2 Second word of polygon context
1.191 + * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
1.192 + * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
1.193 + * the normal vertex, half the vertex length for the modified vertex.
1.194 + */
1.195 +static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
1.196 + uint32_t poly2, uint32_t *pvr2_data,
1.197 + int modify_offset )
1.198 +{
1.199 + gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
1.200 + union pvr2_data_type {
1.201 + uint32_t *ival;
1.202 + float *fval;
1.203 + } data;
1.204 +
1.205 + data.ival = pvr2_data;
1.206 +
1.207 + vert->x = *data.fval++;
1.208 + vert->y = *data.fval++;
1.209 +
1.210 + float z = *data.fval++;
1.211 + if( !isfinite(z) ) {
1.212 + z = 0;
1.213 + } else if( z != 0 ) {
1.214 + z = 1/z;
1.215 + }
1.216 + if( z > pvr2_scene.bounds[5] ) {
1.217 + pvr2_scene.bounds[5] = z;
1.218 + } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
1.219 + pvr2_scene.bounds[4] = z;
1.220 + }
1.221 + vert->z = z;
1.222 + data.ival += modify_offset;
1.223 +
1.224 +
1.225 + if( POLY1_TEXTURED(poly1) ) {
1.226 + if( POLY1_UV16(poly1) ) {
1.227 + vert->u = halftofloat( *data.ival>>16 );
1.228 + vert->v = halftofloat( *data.ival );
1.229 + data.ival++;
1.230 + } else {
1.231 + vert->u = *data.fval++;
1.232 + vert->v = *data.fval++;
1.233 + }
1.234 + if( POLY2_TEX_BLEND(poly2) == 1 ) {
1.235 + force_alpha = TRUE;
1.236 + }
1.237 + }
1.238 + if( force_alpha ) {
1.239 + vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
1.240 + if( POLY1_SPECULAR(poly1) ) {
1.241 + vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
1.242 + } else {
1.243 + vert->offset_rgba = 0;
1.244 + }
1.245 + } else {
1.246 + vert->rgba = bgra_to_rgba(*data.ival++);
1.247 + if( POLY1_SPECULAR(poly1) ) {
1.248 + vert->offset_rgba = bgra_to_rgba(*data.ival++);
1.249 + } else {
1.250 + vert->offset_rgba = 0;
1.251 + }
1.252 + }
1.253 +}
1.254 +
1.255 +/**
1.256 + * Compute texture, colour, and z values for a result point by interpolating from
1.257 + * a set of 3 input points. The result point must define its x,y.
1.258 + */
1.259 +static void scene_compute_vertex( struct vertex_struct *result,
1.260 + struct vertex_struct *input,
1.261 + gboolean is_solid_shaded )
1.262 +{
1.263 + int i;
1.264 + float sx = input[2].x - input[1].x;
1.265 + float sy = input[2].y - input[1].y;
1.266 + float tx = input[0].x - input[1].x;
1.267 + float ty = input[0].y - input[1].y;
1.268 +
1.269 + float detxy = ((sy) * (tx)) - ((ty) * (sx));
1.270 + if( detxy == 0 ) {
1.271 + result->z = input[2].z;
1.272 + result->u = input[2].u;
1.273 + result->v = input[2].v;
1.274 + result->rgba = input[2].rgba;
1.275 + result->offset_rgba = input[2].offset_rgba;
1.276 + return;
1.277 + }
1.278 + float t = ((result->x - input[1].x) * sy -
1.279 + (result->y - input[1].y) * sx) / detxy;
1.280 + float s = ((result->y - input[1].y) * tx -
1.281 + (result->x - input[1].x) * ty) / detxy;
1.282 +
1.283 + float sz = input[2].z - input[1].z;
1.284 + float tz = input[0].z - input[1].z;
1.285 + float su = input[2].u - input[1].u;
1.286 + float tu = input[0].u - input[1].u;
1.287 + float sv = input[2].v - input[1].v;
1.288 + float tv = input[0].v - input[1].v;
1.289 +
1.290 + float rz = input[1].z + (t*tz) + (s*sz);
1.291 + if( rz > pvr2_scene.bounds[5] ) {
1.292 + pvr2_scene.bounds[5] = rz;
1.293 + } else if( rz < pvr2_scene.bounds[4] ) {
1.294 + pvr2_scene.bounds[4] = rz;
1.295 + }
1.296 + result->z = rz;
1.297 + result->u = input[1].u + (t*tu) + (s*su);
1.298 + result->v = input[1].v + (t*tv) + (s*sv);
1.299 +
1.300 + if( is_solid_shaded ) {
1.301 + result->rgba = input[2].rgba;
1.302 + result->offset_rgba = input[2].offset_rgba;
1.303 + } else {
1.304 + uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
1.305 + uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
1.306 + uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
1.307 + uint8_t *rgba3 = (uint8_t *)&result->rgba;
1.308 + for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
1.309 + float tc = *rgba0++ - *rgba1;
1.310 + float sc = *rgba2++ - *rgba1;
1.311 + float rc = *rgba1++ + (t*tc) + (s*sc);
1.312 + if( rc < 0 ) {
1.313 + rc = 0;
1.314 + } else if( rc > 255 ) {
1.315 + rc = 255;
1.316 + }
1.317 + *rgba3++ = rc;
1.318 + }
1.319 + }
1.320 +
1.321 +}
1.322 +
1.323 +static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
1.324 + gboolean is_modified )
1.325 +{
1.326 + struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
1.327 + uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
1.328 + uint32_t *context = ptr;
1.329 + unsigned int i;
1.330 +
1.331 + if( poly->vertex_index == -1 ) {
1.332 + ptr += (is_modified ? 5 : 3 );
1.333 + poly->vertex_index = pvr2_scene.vertex_index;
1.334 +
1.335 + assert( poly != NULL );
1.336 + assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
1.337 + for( i=0; i<poly->vertex_count; i++ ) {
1.338 + pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
1.339 + ptr += vertex_length;
1.340 + }
1.341 + if( is_modified ) {
1.342 + int mod_offset = (vertex_length - 3)>>1;
1.343 + assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
1.344 + ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
1.345 + poly->mod_vertex_index = pvr2_scene.vertex_index;
1.346 + for( i=0; i<poly->vertex_count; i++ ) {
1.347 + pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
1.348 + ptr += vertex_length;
1.349 + }
1.350 + }
1.351 + }
1.352 +}
1.353 +
1.354 +static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
1.355 + gboolean is_modified )
1.356 +{
1.357 + struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
1.358 + uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
1.359 + uint32_t *context = ptr;
1.360 + unsigned int i;
1.361 +
1.362 + if( poly->vertex_index == -1 ) {
1.363 + // Construct it locally and copy to the vertex buffer, as the VBO is
1.364 + // allowed to be horribly slow for reads (ie it could be direct-mapped
1.365 + // vram).
1.366 + struct vertex_struct quad[4];
1.367 +
1.368 + assert( poly != NULL );
1.369 + assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
1.370 + ptr += (is_modified ? 5 : 3 );
1.371 + poly->vertex_index = pvr2_scene.vertex_index;
1.372 + for( i=0; i<4; i++ ) {
1.373 + pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
1.374 + ptr += vertex_length;
1.375 + }
1.376 + scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
1.377 + // Swap last two vertexes (quad arrangement => tri strip arrangement)
1.378 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
1.379 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
1.380 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
1.381 + pvr2_scene.vertex_index += 4;
1.382 +
1.383 + if( is_modified ) {
1.384 + int mod_offset = (vertex_length - 3)>>1;
1.385 + assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
1.386 + ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
1.387 + poly->mod_vertex_index = pvr2_scene.vertex_index;
1.388 + for( i=0; i<4; i++ ) {
1.389 + pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
1.390 + ptr += vertex_length;
1.391 + }
1.392 + scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
1.393 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
1.394 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
1.395 + memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
1.396 + pvr2_scene.vertex_index += 4;
1.397 + }
1.398 + }
1.399 +}
1.400 +
1.401 +static void scene_extract_polygons( pvraddr_t tile_entry )
1.402 +{
1.403 + uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
1.404 + do {
1.405 + uint32_t entry = *tile_list++;
1.406 + if( entry >> 28 == 0x0F ) {
1.407 + break;
1.408 + } else if( entry >> 28 == 0x0E ) {
1.409 + tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
1.410 + } else {
1.411 + pvraddr_t polyaddr = entry&0x000FFFFF;
1.412 + int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
1.413 + int vertex_length = (entry >> 21) & 0x07;
1.414 + int context_length = 3;
1.415 + if( is_modified ) {
1.416 + context_length = 5;
1.417 + vertex_length <<= 1 ;
1.418 + }
1.419 + vertex_length += 3;
1.420 +
1.421 + if( (entry & 0xE0000000) == 0x80000000 ) {
1.422 + /* Triangle(s) */
1.423 + int strip_count = ((entry >> 25) & 0x0F)+1;
1.424 + int polygon_length = 3 * vertex_length + context_length;
1.425 + int i;
1.426 + struct polygon_struct *last_poly = NULL;
1.427 + for( i=0; i<strip_count; i++ ) {
1.428 + struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
1.429 + polyaddr += polygon_length;
1.430 + if( last_poly != NULL && last_poly->next == NULL ) {
1.431 + last_poly->next = poly;
1.432 + }
1.433 + last_poly = poly;
1.434 + }
1.435 + } else if( (entry & 0xE0000000) == 0xA0000000 ) {
1.436 + /* Sprite(s) */
1.437 + int strip_count = ((entry >> 25) & 0x0F)+1;
1.438 + int polygon_length = 4 * vertex_length + context_length;
1.439 + int i;
1.440 + struct polygon_struct *last_poly = NULL;
1.441 + for( i=0; i<strip_count; i++ ) {
1.442 + struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
1.443 + polyaddr += polygon_length;
1.444 + if( last_poly != NULL && last_poly->next == NULL ) {
1.445 + last_poly->next = poly;
1.446 + }
1.447 + last_poly = poly;
1.448 + }
1.449 + } else {
1.450 + /* Polygon */
1.451 + int i, last = -1;
1.452 + for( i=5; i>=0; i-- ) {
1.453 + if( entry & (0x40000000>>i) ) {
1.454 + last = i;
1.455 + break;
1.456 + }
1.457 + }
1.458 + if( last != -1 ) {
1.459 + scene_add_polygon( polyaddr, last+3, is_modified );
1.460 + }
1.461 + }
1.462 + }
1.463 + } while( 1 );
1.464 +}
1.465 +
1.466 +static void scene_extract_vertexes( pvraddr_t tile_entry )
1.467 +{
1.468 + uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
1.469 + do {
1.470 + uint32_t entry = *tile_list++;
1.471 + if( entry >> 28 == 0x0F ) {
1.472 + break;
1.473 + } else if( entry >> 28 == 0x0E ) {
1.474 + tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
1.475 + } else {
1.476 + pvraddr_t polyaddr = entry&0x000FFFFF;
1.477 + int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
1.478 + int vertex_length = (entry >> 21) & 0x07;
1.479 + int context_length = 3;
1.480 + if( is_modified ) {
1.481 + context_length = 5;
1.482 + vertex_length <<=1 ;
1.483 + }
1.484 + vertex_length += 3;
1.485 +
1.486 + if( (entry & 0xE0000000) == 0x80000000 ) {
1.487 + /* Triangle(s) */
1.488 + int strip_count = ((entry >> 25) & 0x0F)+1;
1.489 + int polygon_length = 3 * vertex_length + context_length;
1.490 + int i;
1.491 + for( i=0; i<strip_count; i++ ) {
1.492 + scene_add_vertexes( polyaddr, vertex_length, is_modified );
1.493 + polyaddr += polygon_length;
1.494 + }
1.495 + } else if( (entry & 0xE0000000) == 0xA0000000 ) {
1.496 + /* Sprite(s) */
1.497 + int strip_count = ((entry >> 25) & 0x0F)+1;
1.498 + int polygon_length = 4 * vertex_length + context_length;
1.499 + int i;
1.500 + for( i=0; i<strip_count; i++ ) {
1.501 + scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
1.502 + polyaddr += polygon_length;
1.503 + }
1.504 + } else {
1.505 + /* Polygon */
1.506 + int i, last = -1;
1.507 + for( i=5; i>=0; i-- ) {
1.508 + if( entry & (0x40000000>>i) ) {
1.509 + last = i;
1.510 + break;
1.511 + }
1.512 + }
1.513 + if( last != -1 ) {
1.514 + scene_add_vertexes( polyaddr, vertex_length, is_modified );
1.515 + }
1.516 + }
1.517 + }
1.518 + } while( 1 );
1.519 +}
1.520 +
1.521 +uint32_t pvr2_scene_buffer_width()
1.522 +{
1.523 + return pvr2_scene.buffer_width;
1.524 +}
1.525 +
1.526 +uint32_t pvr2_scene_buffer_height()
1.527 +{
1.528 + return pvr2_scene.buffer_height;
1.529 +}
1.530 +
1.531 +/**
1.532 + * Extract the current scene into the rendering structures. We run two passes
1.533 + * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
1.534 + * second pass extracts the vertex data into the VBO/vertex array.
1.535 + *
1.536 + * Difficult to do in single pass as we don't generally know the size of a
1.537 + * polygon for certain until we've seen all tiles containing it. It also means we
1.538 + * can count the vertexes and allocate the appropriate size VBO.
1.539 + *
1.540 + * FIXME: accesses into VRAM need to be bounds-checked properly
1.541 + */
1.542 +void pvr2_scene_read( void )
1.543 +{
1.544 + pvr2_scene_init();
1.545 + pvr2_scene_reset();
1.546 +
1.547 + pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
1.548 + pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
1.549 + pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
1.550 + pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
1.551 + pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
1.552 +
1.553 + uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
1.554 + uint32_t *segment = tilebuffer;
1.555 + pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
1.556 + pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
1.557 + pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
1.558 +
1.559 + int max_tile_x = 0;
1.560 + int max_tile_y = 0;
1.561 + int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
1.562 + int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
1.563 +
1.564 + if( (obj_config & 0x00200000) == 0 ) {
1.565 + if( isp_config & 1 ) {
1.566 + pvr2_scene.sort_mode = SORT_NEVER;
1.567 + } else {
1.568 + pvr2_scene.sort_mode = SORT_ALWAYS;
1.569 + }
1.570 + } else {
1.571 + pvr2_scene.sort_mode = SORT_TILEFLAG;
1.572 + }
1.573 +
1.574 + // Pass 1: Extract polygon list
1.575 + uint32_t control;
1.576 + int i;
1.577 + do {
1.578 + control = *segment++;
1.579 + int tile_x = SEGMENT_X(control);
1.580 + int tile_y = SEGMENT_Y(control);
1.581 + if( tile_x > max_tile_x ) {
1.582 + max_tile_x = tile_x;
1.583 + }
1.584 + if( tile_y > max_tile_y ) {
1.585 + max_tile_y = tile_y;
1.586 + }
1.587 + for( i=0; i<5; i++ ) {
1.588 + if( (*segment & NO_POINTER) == 0 ) {
1.589 + scene_extract_polygons( *segment );
1.590 + }
1.591 + segment++;
1.592 + }
1.593 + } while( (control & SEGMENT_END) == 0 );
1.594 +
1.595 + pvr2_scene.buffer_width = (max_tile_x+1)<<5;
1.596 + pvr2_scene.buffer_height = (max_tile_y+1)<<5;
1.597 +
1.598 + if( pvr2_scene.vertex_count > 0 ) {
1.599 + // Pass 2: Extract vertex data
1.600 + vertex_buffer_map();
1.601 + pvr2_scene.vertex_index = 0;
1.602 + segment = tilebuffer;
1.603 + do {
1.604 + control = *segment++;
1.605 + for( i=0; i<5; i++ ) {
1.606 + if( (*segment & NO_POINTER) == 0 ) {
1.607 + scene_extract_vertexes( *segment );
1.608 + }
1.609 + segment++;
1.610 + }
1.611 + } while( (control & SEGMENT_END) == 0 );
1.612 + vertex_buffer_unmap();
1.613 + }
1.614 +}
1.615 +
1.616 +/**
1.617 + * Dump the current scene to file in a (mostly) human readable form
1.618 + */
1.619 +void pvr2_scene_dump( FILE *f )
1.620 +{
1.621 + int i,j;
1.622 +
1.623 + fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
1.624 + for( i=0; i<pvr2_scene.poly_count; i++ ) {
1.625 + struct polygon_struct *poly = &pvr2_scene.poly_array[i];
1.626 + fprintf( f, " %08X ", ((char *)poly->context) - video_base );
1.627 + switch( poly->vertex_count ) {
1.628 + case 3: fprintf( f, "Tri " ); break;
1.629 + case 4: fprintf( f, "Quad " ); break;
1.630 + default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
1.631 + }
1.632 + fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
1.633 + if( poly->mod_vertex_index != -1 ) {
1.634 + fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
1.635 + } else {
1.636 + fprintf( f, "\n" );
1.637 + }
1.638 +
1.639 + for( j=0; j<poly->vertex_count; j++ ) {
1.640 + struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
1.641 + fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
1.642 + v->rgba, v->offset_rgba );
1.643 + }
1.644 + if( poly->mod_vertex_index != -1 ) {
1.645 + fprintf( f, " ---\n" );
1.646 + for( j=0; j<poly->vertex_count; j++ ) {
1.647 + struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
1.648 + fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
1.649 + v->rgba, v->offset_rgba );
1.650 + }
1.651 + }
1.652 + }
1.653 +
1.654 +}
.