Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 352:f0df7a6d4703
prev351:41b7e55ee10f
next429:e581b90c3fb3
author nkeynes
date Sun Feb 11 10:09:32 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug 27: Implement opengl framebuffer objects
Rewrite much of the final video output stage. Now uses generic "render
buffers", implemented on GL using framebuffer objects + textures.
file annotate diff log raw
nkeynes@189
     1
/**
nkeynes@352
     2
 * $Id: rendcore.c,v 1.19 2007-02-11 10:09:32 nkeynes Exp $
nkeynes@189
     3
 *
nkeynes@189
     4
 * PVR2 renderer core.
nkeynes@189
     5
 *
nkeynes@189
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@189
     7
 *
nkeynes@189
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@189
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@189
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@189
    11
 * (at your option) any later version.
nkeynes@189
    12
 *
nkeynes@189
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@189
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@189
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@189
    16
 * GNU General Public License for more details.
nkeynes@189
    17
 */
nkeynes@215
    18
#include <sys/time.h>
nkeynes@189
    19
#include "pvr2/pvr2.h"
nkeynes@189
    20
#include "asic.h"
nkeynes@189
    21
nkeynes@221
    22
int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
nkeynes@189
    23
				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
nkeynes@189
    24
				      GL_ALWAYS };
nkeynes@221
    25
int pvr2_poly_srcblend[8] = { 
nkeynes@189
    26
    GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
nkeynes@189
    27
    GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
nkeynes@189
    28
    GL_ONE_MINUS_DST_ALPHA };
nkeynes@221
    29
int pvr2_poly_dstblend[8] = {
nkeynes@189
    30
    GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
nkeynes@189
    31
    GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
nkeynes@189
    32
    GL_ONE_MINUS_DST_ALPHA };
nkeynes@221
    33
int pvr2_poly_texblend[4] = {
nkeynes@322
    34
    GL_REPLACE, 
nkeynes@322
    35
    GL_MODULATE,  
nkeynes@322
    36
    GL_DECAL, 
nkeynes@322
    37
    GL_MODULATE 
nkeynes@322
    38
};
nkeynes@221
    39
int pvr2_render_colour_format[8] = {
nkeynes@189
    40
    COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
nkeynes@189
    41
    COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
nkeynes@189
    42
nkeynes@189
    43
nkeynes@189
    44
#define CULL_NONE 0
nkeynes@189
    45
#define CULL_SMALL 1
nkeynes@189
    46
#define CULL_CCW 2
nkeynes@189
    47
#define CULL_CW 3
nkeynes@189
    48
nkeynes@189
    49
#define SEGMENT_END         0x80000000
nkeynes@298
    50
#define SEGMENT_ZCLEAR      0x40000000
nkeynes@189
    51
#define SEGMENT_SORT_TRANS  0x20000000
nkeynes@189
    52
#define SEGMENT_START       0x10000000
nkeynes@189
    53
#define SEGMENT_X(c)        (((c) >> 2) & 0x3F)
nkeynes@189
    54
#define SEGMENT_Y(c)        (((c) >> 8) & 0x3F)
nkeynes@189
    55
#define NO_POINTER          0x80000000
nkeynes@189
    56
nkeynes@189
    57
extern char *video_base;
nkeynes@189
    58
nkeynes@322
    59
gboolean pvr2_force_fragment_alpha = FALSE;
nkeynes@338
    60
gboolean pvr2_debug_render = FALSE;
nkeynes@318
    61
nkeynes@189
    62
struct tile_segment {
nkeynes@189
    63
    uint32_t control;
nkeynes@189
    64
    pvraddr_t opaque_ptr;
nkeynes@189
    65
    pvraddr_t opaquemod_ptr;
nkeynes@189
    66
    pvraddr_t trans_ptr;
nkeynes@189
    67
    pvraddr_t transmod_ptr;
nkeynes@189
    68
    pvraddr_t punchout_ptr;
nkeynes@189
    69
};
nkeynes@189
    70
nkeynes@341
    71
void render_print_tilelist( FILE *f, uint32_t tile_entry );
nkeynes@341
    72
nkeynes@189
    73
/**
nkeynes@189
    74
 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
nkeynes@189
    75
 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
nkeynes@189
    76
 * TODO: Check the correctness of this.
nkeynes@189
    77
 */
nkeynes@189
    78
float halftofloat( uint16_t half )
nkeynes@189
    79
{
nkeynes@189
    80
    union {
nkeynes@189
    81
        float f;
nkeynes@189
    82
        uint32_t i;
nkeynes@189
    83
    } temp;
nkeynes@308
    84
    /* int e = ((half & 0x7C00) >> 10) - 15 + 127;
nkeynes@189
    85
nkeynes@189
    86
    temp.i = ((half & 0x8000) << 16) | (e << 23) |
nkeynes@308
    87
    ((half & 0x03FF) << 13); */
nkeynes@308
    88
    temp.i = ((uint32_t)half)<<16;
nkeynes@189
    89
    return temp.f;
nkeynes@189
    90
}
nkeynes@189
    91
nkeynes@189
    92
nkeynes@189
    93
/**
nkeynes@189
    94
 * Setup the GL context for the supplied polygon context.
nkeynes@189
    95
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@189
    96
 * @param modified boolean flag indicating that the modified
nkeynes@189
    97
 *  version should be used, rather than the normal version.
nkeynes@189
    98
 */
nkeynes@189
    99
void render_set_context( uint32_t *context, int render_mode )
nkeynes@189
   100
{
nkeynes@189
   101
    uint32_t poly1 = context[0], poly2, texture;
nkeynes@189
   102
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@189
   103
	poly2 = context[3];
nkeynes@189
   104
	texture = context[4];
nkeynes@189
   105
    } else {
nkeynes@189
   106
	poly2 = context[1];
nkeynes@189
   107
	texture = context[2];
nkeynes@189
   108
    }
nkeynes@338
   109
    
nkeynes@189
   110
    if( POLY1_DEPTH_ENABLE(poly1) ) {
nkeynes@189
   111
	glEnable( GL_DEPTH_TEST );
nkeynes@189
   112
	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@189
   113
    } else {
nkeynes@189
   114
	glDisable( GL_DEPTH_TEST );
nkeynes@189
   115
    }
nkeynes@189
   116
    
nkeynes@189
   117
    switch( POLY1_CULL_MODE(poly1) ) {
nkeynes@189
   118
    case CULL_NONE:
nkeynes@189
   119
    case CULL_SMALL:
nkeynes@189
   120
	glDisable( GL_CULL_FACE );
nkeynes@189
   121
	break;
nkeynes@189
   122
    case CULL_CCW:
nkeynes@189
   123
	glEnable( GL_CULL_FACE );
nkeynes@189
   124
	glFrontFace( GL_CW );
nkeynes@189
   125
	break;
nkeynes@189
   126
    case CULL_CW:
nkeynes@189
   127
	glEnable( GL_CULL_FACE );
nkeynes@189
   128
	glFrontFace( GL_CCW );
nkeynes@189
   129
	break;
nkeynes@189
   130
    }
nkeynes@189
   131
nkeynes@318
   132
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@318
   133
	glEnable(GL_COLOR_SUM);
nkeynes@318
   134
    } else {
nkeynes@318
   135
	glDisable(GL_COLOR_SUM);
nkeynes@318
   136
    }
nkeynes@318
   137
nkeynes@338
   138
    pvr2_force_fragment_alpha = POLY2_ALPHA_ENABLE(poly2) ? FALSE : TRUE;
nkeynes@338
   139
nkeynes@189
   140
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@189
   141
	int width = POLY2_TEX_WIDTH(poly2);
nkeynes@189
   142
	int height = POLY2_TEX_HEIGHT(poly2);
nkeynes@189
   143
	glEnable(GL_TEXTURE_2D);
nkeynes@286
   144
	texcache_get_texture( (texture&0x000FFFFF)<<3, width, height, texture );
nkeynes@338
   145
	switch( POLY2_TEX_BLEND(poly2) ) {
nkeynes@338
   146
	case 0: /* Replace */
nkeynes@338
   147
	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@338
   148
	    break;
nkeynes@338
   149
	case 2:/* Decal */
nkeynes@338
   150
	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
nkeynes@338
   151
	    break;
nkeynes@338
   152
	case 1: /* Modulate RGB */
nkeynes@338
   153
	    /* This is not directly supported by opengl (other than by mucking
nkeynes@338
   154
	     * with the texture format), but we get the same effect by forcing
nkeynes@338
   155
	     * the fragment alpha to 1.0 and using GL_MODULATE.
nkeynes@338
   156
	     */
nkeynes@338
   157
	    pvr2_force_fragment_alpha = TRUE;
nkeynes@338
   158
	case 3: /* Modulate RGBA */
nkeynes@338
   159
	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
nkeynes@338
   160
	    break;
nkeynes@338
   161
	}
nkeynes@352
   162
nkeynes@308
   163
	if( POLY2_TEX_CLAMP_U(poly2) ) {
nkeynes@308
   164
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@308
   165
	} else {
nkeynes@308
   166
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@308
   167
	}	    
nkeynes@308
   168
	if( POLY2_TEX_CLAMP_V(poly2) ) {
nkeynes@308
   169
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@308
   170
	} else {
nkeynes@350
   171
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@308
   172
	}
nkeynes@189
   173
    } else {
nkeynes@189
   174
	glDisable( GL_TEXTURE_2D );
nkeynes@189
   175
    }
nkeynes@189
   176
nkeynes@189
   177
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@189
   178
nkeynes@221
   179
    int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@221
   180
    int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@221
   181
    glBlendFunc( srcblend, destblend );
nkeynes@318
   182
nkeynes@322
   183
    if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@322
   184
	ERROR( "Accumulation buffer not supported" );
nkeynes@322
   185
    }
nkeynes@338
   186
nkeynes@318
   187
nkeynes@189
   188
}
nkeynes@189
   189
nkeynes@319
   190
#define FARGB_A(x) (((float)(((x)>>24)+1))/256.0)
nkeynes@319
   191
#define FARGB_R(x) (((float)((((x)>>16)&0xFF)+1))/256.0)
nkeynes@319
   192
#define FARGB_G(x) (((float)((((x)>>8)&0xFF)+1))/256.0)
nkeynes@319
   193
#define FARGB_B(x) (((float)(((x)&0xFF)+1))/256.0)
nkeynes@319
   194
nkeynes@319
   195
void render_unpack_vertexes( struct vertex_unpacked *out, uint32_t poly1, 
nkeynes@319
   196
			     uint32_t *vertexes, int num_vertexes,
nkeynes@319
   197
			     int vertex_size, int render_mode )
nkeynes@319
   198
{
nkeynes@319
   199
    int m = 0, i;
nkeynes@319
   200
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@319
   201
	m = (vertex_size - 3)/2;
nkeynes@319
   202
    }
nkeynes@319
   203
nkeynes@319
   204
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   205
	float *vertexf = (float *)vertexes;
nkeynes@319
   206
	int k = m + 3;
nkeynes@319
   207
	out[i].x = vertexf[0];
nkeynes@319
   208
	out[i].y = vertexf[1];
nkeynes@319
   209
	out[i].z = vertexf[2];
nkeynes@319
   210
    	if( POLY1_TEXTURED(poly1) ) {
nkeynes@319
   211
	    if( POLY1_UV16(poly1) ) {
nkeynes@319
   212
		out[i].u = halftofloat(vertexes[k]>>16);
nkeynes@319
   213
		out[i].v = halftofloat(vertexes[k]);
nkeynes@319
   214
		k++;
nkeynes@319
   215
	    } else {
nkeynes@319
   216
		out[i].u = vertexf[k];
nkeynes@319
   217
		out[i].v = vertexf[k+1];
nkeynes@319
   218
		k+=2;
nkeynes@319
   219
	    }
nkeynes@319
   220
	} else {
nkeynes@319
   221
	    out[i].u = 0;
nkeynes@319
   222
	    out[i].v = 0;
nkeynes@319
   223
	}
nkeynes@319
   224
	uint32_t argb = vertexes[k++];
nkeynes@319
   225
	out[i].rgba[0] = FARGB_R(argb);
nkeynes@319
   226
	out[i].rgba[1] = FARGB_G(argb);
nkeynes@319
   227
        out[i].rgba[2] = FARGB_B(argb);
nkeynes@319
   228
	out[i].rgba[3] = FARGB_A(argb);
nkeynes@319
   229
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@319
   230
	    uint32_t offset = vertexes[k++];
nkeynes@319
   231
	    out[i].offset_rgba[0] = FARGB_R(argb);
nkeynes@319
   232
	    out[i].offset_rgba[1] = FARGB_G(argb);
nkeynes@319
   233
	    out[i].offset_rgba[2] = FARGB_B(argb);
nkeynes@319
   234
	    out[i].offset_rgba[3] = FARGB_A(argb);
nkeynes@319
   235
	}
nkeynes@319
   236
	vertexes += vertex_size;
nkeynes@319
   237
    }
nkeynes@319
   238
}
nkeynes@319
   239
nkeynes@319
   240
/**
nkeynes@319
   241
 * Unpack the vertexes for a quad, calculating the values for the last
nkeynes@319
   242
 * vertex.
nkeynes@319
   243
 * FIXME: Integrate this with rendbkg somehow
nkeynes@319
   244
 */
nkeynes@319
   245
void render_unpack_quad( struct vertex_unpacked *unpacked, uint32_t poly1, 
nkeynes@319
   246
			 uint32_t *vertexes, int vertex_size,
nkeynes@319
   247
			 int render_mode )
nkeynes@319
   248
{
nkeynes@319
   249
    int i;
nkeynes@319
   250
    struct vertex_unpacked diff0, diff1;
nkeynes@319
   251
nkeynes@319
   252
    render_unpack_vertexes( unpacked, poly1, vertexes, 3, vertex_size, render_mode );
nkeynes@319
   253
    
nkeynes@319
   254
    diff0.x = unpacked[0].x - unpacked[1].x;
nkeynes@319
   255
    diff0.y = unpacked[0].y - unpacked[1].y;
nkeynes@319
   256
    diff1.x = unpacked[2].x - unpacked[1].x;
nkeynes@319
   257
    diff1.y = unpacked[2].y - unpacked[1].y;
nkeynes@319
   258
nkeynes@319
   259
    float detxy = ((diff1.y) * (diff0.x)) - ((diff0.y) * (diff1.x));
nkeynes@319
   260
    float *vertexf = (float *)(vertexes+(vertex_size*3));
nkeynes@319
   261
    if( detxy == 0 ) {
nkeynes@319
   262
	memcpy( &unpacked[3], &unpacked[2], sizeof(struct vertex_unpacked) );
nkeynes@319
   263
	unpacked[3].x = vertexf[0];
nkeynes@319
   264
	unpacked[3].y = vertexf[1];
nkeynes@319
   265
	return;
nkeynes@319
   266
    }	
nkeynes@319
   267
nkeynes@319
   268
    unpacked[3].x = vertexf[0];
nkeynes@319
   269
    unpacked[3].y = vertexf[1];
nkeynes@319
   270
    float t = ((unpacked[3].x - unpacked[1].x) * diff1.y -
nkeynes@319
   271
	       (unpacked[3].y - unpacked[1].y) * diff1.x) / detxy;
nkeynes@319
   272
    float s = ((unpacked[3].y - unpacked[1].y) * diff0.x -
nkeynes@319
   273
	       (unpacked[3].x - unpacked[1].x) * diff0.y) / detxy;
nkeynes@331
   274
    diff0.z = (1/unpacked[0].z) - (1/unpacked[1].z);
nkeynes@331
   275
    diff1.z = (1/unpacked[2].z) - (1/unpacked[1].z);
nkeynes@331
   276
    unpacked[3].z = 1/((1/unpacked[1].z) + (t*diff0.z) + (s*diff1.z));
nkeynes@319
   277
nkeynes@319
   278
    diff0.u = unpacked[0].u - unpacked[1].u;
nkeynes@319
   279
    diff0.v = unpacked[0].v - unpacked[1].v;
nkeynes@319
   280
    diff1.u = unpacked[2].u - unpacked[1].u;
nkeynes@319
   281
    diff1.v = unpacked[2].v - unpacked[1].v;
nkeynes@319
   282
    unpacked[3].u = unpacked[1].u + (t*diff0.u) + (s*diff1.u);
nkeynes@319
   283
    unpacked[3].v = unpacked[1].v + (t*diff0.v) + (s*diff1.v);
nkeynes@319
   284
nkeynes@319
   285
    if( !POLY1_GOURAUD_SHADED(poly1) ) {
nkeynes@319
   286
	memcpy( unpacked[3].rgba, unpacked[2].rgba, sizeof(unpacked[2].rgba) );
nkeynes@319
   287
	memcpy( unpacked[3].offset_rgba, unpacked[2].offset_rgba, sizeof(unpacked[2].offset_rgba) );
nkeynes@319
   288
    } else {
nkeynes@319
   289
	for( i=0; i<4; i++ ) {
nkeynes@319
   290
	    float d0 = unpacked[0].rgba[i] - unpacked[1].rgba[i];
nkeynes@319
   291
	    float d1 = unpacked[2].rgba[i] - unpacked[1].rgba[i];
nkeynes@319
   292
	    unpacked[3].rgba[i] = unpacked[1].rgba[i] + (t*d0) + (s*d1);
nkeynes@319
   293
	    d0 = unpacked[0].offset_rgba[i] - unpacked[1].offset_rgba[i];
nkeynes@319
   294
	    d1 = unpacked[2].offset_rgba[i] - unpacked[1].offset_rgba[i];
nkeynes@319
   295
	    unpacked[3].offset_rgba[i] = unpacked[1].offset_rgba[i] + (t*d0) + (s*d1);
nkeynes@319
   296
	}
nkeynes@319
   297
    }    
nkeynes@319
   298
}
nkeynes@319
   299
nkeynes@319
   300
void render_unpacked_vertex_array( uint32_t poly1, struct vertex_unpacked *vertexes[], 
nkeynes@319
   301
				   int num_vertexes ) {
nkeynes@319
   302
    int i;
nkeynes@319
   303
nkeynes@319
   304
    glBegin( GL_TRIANGLE_STRIP );
nkeynes@319
   305
nkeynes@319
   306
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   307
	if( POLY1_TEXTURED(poly1) ) {
nkeynes@319
   308
	    glTexCoord2f( vertexes[i]->u, vertexes[i]->v );
nkeynes@319
   309
	}
nkeynes@319
   310
nkeynes@322
   311
	if( pvr2_force_fragment_alpha ) {
nkeynes@322
   312
	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2], 1.0 );
nkeynes@322
   313
	} else {
nkeynes@322
   314
	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2],
nkeynes@322
   315
		       vertexes[i]->rgba[3] );
nkeynes@322
   316
	}
nkeynes@319
   317
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@319
   318
	    glSecondaryColor3fEXT( vertexes[i]->offset_rgba[0],
nkeynes@319
   319
				   vertexes[i]->offset_rgba[1],
nkeynes@319
   320
				   vertexes[i]->offset_rgba[2] );
nkeynes@319
   321
	}
nkeynes@331
   322
	glVertex3f( vertexes[i]->x, vertexes[i]->y, 1/vertexes[i]->z );
nkeynes@319
   323
    }
nkeynes@319
   324
nkeynes@319
   325
    glEnd();
nkeynes@319
   326
}
nkeynes@319
   327
nkeynes@319
   328
void render_quad_vertexes( uint32_t poly1, uint32_t *vertexes, int vertex_size, int render_mode )
nkeynes@319
   329
{
nkeynes@319
   330
    struct vertex_unpacked unpacked[4];
nkeynes@319
   331
    struct vertex_unpacked *pt[4] = {&unpacked[0], &unpacked[1], &unpacked[3], &unpacked[2]};
nkeynes@319
   332
    render_unpack_quad( unpacked, poly1, vertexes, vertex_size, render_mode );
nkeynes@319
   333
    render_unpacked_vertex_array( poly1, pt, 4 );
nkeynes@319
   334
}
nkeynes@319
   335
nkeynes@319
   336
void render_vertex_array( uint32_t poly1, uint32_t *vert_array[], int num_vertexes, int vertex_size,
nkeynes@319
   337
			  int render_mode ) 
nkeynes@189
   338
{
nkeynes@189
   339
    int i, m=0;
nkeynes@189
   340
nkeynes@189
   341
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@189
   342
	m = (vertex_size - 3)/2;
nkeynes@189
   343
    }
nkeynes@189
   344
nkeynes@189
   345
    glBegin( GL_TRIANGLE_STRIP );
nkeynes@189
   346
    
nkeynes@189
   347
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   348
	uint32_t *vertexes = vert_array[i];
nkeynes@319
   349
	float *vertexf = (float *)vert_array[i];
nkeynes@189
   350
	uint32_t argb;
nkeynes@318
   351
	int k = m + 3;
nkeynes@189
   352
	if( POLY1_TEXTURED(poly1) ) {
nkeynes@189
   353
	    if( POLY1_UV16(poly1) ) {
nkeynes@318
   354
		glTexCoord2f( halftofloat(vertexes[k]>>16),
nkeynes@318
   355
			      halftofloat(vertexes[k]) );
nkeynes@318
   356
		k++;
nkeynes@189
   357
	    } else {
nkeynes@318
   358
		glTexCoord2f( vertexf[k], vertexf[k+1] );
nkeynes@318
   359
		k+=2;
nkeynes@189
   360
	    }
nkeynes@189
   361
	}
nkeynes@189
   362
nkeynes@318
   363
	argb = vertexes[k++];
nkeynes@318
   364
	if( pvr2_force_fragment_alpha ) {
nkeynes@318
   365
	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
nkeynes@318
   366
			(GLubyte)argb, 0xFF );
nkeynes@318
   367
	} else {
nkeynes@318
   368
	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
nkeynes@318
   369
			(GLubyte)argb, (GLubyte)(argb >> 24) );
nkeynes@318
   370
	}
nkeynes@318
   371
nkeynes@318
   372
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@318
   373
	    uint32_t spec = vertexes[k++];
nkeynes@318
   374
	    glSecondaryColor3ubEXT( (GLubyte)(spec >> 16), (GLubyte)(spec >> 8), 
nkeynes@318
   375
				 (GLubyte)spec );
nkeynes@318
   376
	}
nkeynes@331
   377
	glVertex3f( vertexf[0], vertexf[1], 1/vertexf[2] );
nkeynes@189
   378
	vertexes += vertex_size;
nkeynes@189
   379
    }
nkeynes@189
   380
nkeynes@189
   381
    glEnd();
nkeynes@189
   382
}
nkeynes@189
   383
nkeynes@319
   384
void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
nkeynes@319
   385
		      int render_mode )
nkeynes@319
   386
{
nkeynes@319
   387
    uint32_t *vert_array[num_vertexes];
nkeynes@319
   388
    int i;
nkeynes@319
   389
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   390
	vert_array[i] = vertexes;
nkeynes@319
   391
	vertexes += vertex_size;
nkeynes@319
   392
    }
nkeynes@319
   393
    render_vertex_array( poly1, vert_array, num_vertexes, vertex_size, render_mode );
nkeynes@319
   394
}
nkeynes@319
   395
nkeynes@189
   396
/**
nkeynes@189
   397
 * Render a simple (not auto-sorted) tile
nkeynes@189
   398
 */
nkeynes@189
   399
void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
nkeynes@215
   400
    uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
nkeynes@189
   401
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@189
   402
    do {
nkeynes@189
   403
	uint32_t entry = *tile_list++;
nkeynes@189
   404
	if( entry >> 28 == 0x0F ) {
nkeynes@189
   405
	    break;
nkeynes@189
   406
	} else if( entry >> 28 == 0x0E ) {
nkeynes@189
   407
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@189
   408
	} else {
nkeynes@215
   409
	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
nkeynes@189
   410
	    int is_modified = entry & 0x01000000;
nkeynes@189
   411
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@189
   412
	    int context_length = 3;
nkeynes@189
   413
	    if( is_modified && !cheap_modifier_mode ) {
nkeynes@189
   414
		context_length = 5;
nkeynes@189
   415
		vertex_length *= 2 ;
nkeynes@189
   416
	    }
nkeynes@189
   417
	    vertex_length += 3;
nkeynes@308
   418
	    
nkeynes@189
   419
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@189
   420
		/* Triangle(s) */
nkeynes@189
   421
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@189
   422
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@189
   423
		int i;
nkeynes@189
   424
		for( i=0; i<strip_count; i++ ) {
nkeynes@189
   425
		    render_set_context( polygon, render_mode );
nkeynes@189
   426
		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
nkeynes@189
   427
				     render_mode );
nkeynes@189
   428
		    polygon += polygon_length;
nkeynes@189
   429
		}
nkeynes@189
   430
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@189
   431
		/* Sprite(s) */
nkeynes@319
   432
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@189
   433
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@189
   434
		int i;
nkeynes@189
   435
		for( i=0; i<strip_count; i++ ) {
nkeynes@189
   436
		    render_set_context( polygon, render_mode );
nkeynes@319
   437
		    render_quad_vertexes( *polygon, polygon+context_length, vertex_length,
nkeynes@319
   438
					  render_mode );
nkeynes@189
   439
		    polygon += polygon_length;
nkeynes@189
   440
		}
nkeynes@189
   441
	    } else {
nkeynes@189
   442
		/* Polygon */
nkeynes@189
   443
		int i, first=-1, last = -1;
nkeynes@189
   444
		for( i=0; i<6; i++ ) {
nkeynes@189
   445
		    if( entry & (0x40000000>>i) ) {
nkeynes@189
   446
			if( first == -1 ) first = i;
nkeynes@189
   447
			last = i;
nkeynes@189
   448
		    }
nkeynes@189
   449
		}
nkeynes@189
   450
		if( first != -1 ) {
nkeynes@189
   451
		    first = 0;
nkeynes@189
   452
		    render_set_context(polygon, render_mode);
nkeynes@189
   453
		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
nkeynes@189
   454
				     (last-first+3), vertex_length, render_mode );
nkeynes@189
   455
		}
nkeynes@189
   456
	    }
nkeynes@189
   457
	}
nkeynes@189
   458
    } while( 1 );
nkeynes@189
   459
}
nkeynes@189
   460
nkeynes@189
   461
void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
nkeynes@189
   462
			int clipx2, int clipy2 ) {
nkeynes@189
   463
nkeynes@191
   464
    pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
nkeynes@189
   465
    int tile_sort;
nkeynes@189
   466
    gboolean cheap_shadow;
nkeynes@189
   467
nkeynes@191
   468
    int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
nkeynes@191
   469
    int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
nkeynes@191
   470
    int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
nkeynes@189
   471
nkeynes@222
   472
    if( (obj_config & 0x00200000) == 0 ) {
nkeynes@189
   473
	if( isp_config & 1 ) {
nkeynes@189
   474
	    tile_sort = 0;
nkeynes@189
   475
	} else {
nkeynes@189
   476
	    tile_sort = 2;
nkeynes@189
   477
	}
nkeynes@189
   478
    } else {
nkeynes@189
   479
	tile_sort = 1;
nkeynes@189
   480
    }
nkeynes@189
   481
nkeynes@189
   482
    cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
nkeynes@189
   483
nkeynes@189
   484
    struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
nkeynes@189
   485
nkeynes@215
   486
    struct timeval tv_start, tv_end;
nkeynes@215
   487
    gettimeofday(&tv_start, NULL);
nkeynes@189
   488
    glEnable( GL_SCISSOR_TEST );
nkeynes@268
   489
    do {
nkeynes@221
   490
	// fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
nkeynes@189
   491
	int tilex = SEGMENT_X(segment->control);
nkeynes@189
   492
	int tiley = SEGMENT_Y(segment->control);
nkeynes@189
   493
	
nkeynes@189
   494
	int x1 = tilex << 5;
nkeynes@189
   495
	int y1 = tiley << 5;
nkeynes@189
   496
	if( x1 + 32 <= clipx1 ||
nkeynes@189
   497
	    y1 + 32 <= clipy1 ||
nkeynes@189
   498
	    x1 >= clipx2 ||
nkeynes@189
   499
	    y1 >= clipy2 ) {
nkeynes@189
   500
	    /* Tile completely clipped, skip */
nkeynes@189
   501
	    continue;
nkeynes@189
   502
	}
nkeynes@215
   503
nkeynes@189
   504
	/* Set a scissor on the visible part of the tile */
nkeynes@189
   505
	int w = MIN(x1+32, clipx2) - x1;
nkeynes@189
   506
	int h = MIN(y1+32, clipy2) - y1;
nkeynes@189
   507
	x1 = MAX(x1,clipx1);
nkeynes@189
   508
	y1 = MAX(y1,clipy1);
nkeynes@189
   509
	glScissor( x1, height-y1-h, w, h );
nkeynes@189
   510
nkeynes@189
   511
	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
nkeynes@338
   512
	    if( pvr2_debug_render ) {
nkeynes@338
   513
		fprintf( stderr, "Tile %d,%d Opaque\n", tilex, tiley );
nkeynes@341
   514
		render_print_tilelist( stderr, segment->opaque_ptr );
nkeynes@338
   515
	    }
nkeynes@189
   516
	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   517
		/* TODO */
nkeynes@189
   518
	    }
nkeynes@189
   519
	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   520
	}
nkeynes@189
   521
nkeynes@189
   522
	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
nkeynes@338
   523
	    if( pvr2_debug_render ) {
nkeynes@338
   524
		fprintf( stderr, "Tile %d,%d Trans\n", tilex, tiley );
nkeynes@341
   525
		render_print_tilelist( stderr, segment->trans_ptr );
nkeynes@338
   526
	    }
nkeynes@189
   527
	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   528
		/* TODO */
nkeynes@189
   529
	    } 
nkeynes@222
   530
	    if( tile_sort == 2 || 
nkeynes@222
   531
		(tile_sort == 1 && ((segment->control & SEGMENT_SORT_TRANS)==0)) ) {
nkeynes@189
   532
		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   533
	    } else {
nkeynes@189
   534
		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   535
	    }
nkeynes@189
   536
	}
nkeynes@189
   537
nkeynes@189
   538
	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
nkeynes@338
   539
	    if( pvr2_debug_render ) {
nkeynes@338
   540
		fprintf( stderr, "Tile %d,%d Punchout\n", tilex, tiley );
nkeynes@341
   541
		render_print_tilelist( stderr, segment->punchout_ptr );
nkeynes@338
   542
	    }
nkeynes@189
   543
	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   544
	}
nkeynes@268
   545
    } while( ((segment++)->control & SEGMENT_END) == 0 );
nkeynes@189
   546
    glDisable( GL_SCISSOR_TEST );
nkeynes@215
   547
nkeynes@215
   548
    gettimeofday(&tv_end, NULL);
nkeynes@215
   549
    timersub(&tv_end,&tv_start, &tv_start);
nkeynes@189
   550
}
nkeynes@322
   551
nkeynes@322
   552
static float render_find_maximum_tile_z( pvraddr_t tile_entry, float inputz )
nkeynes@322
   553
{
nkeynes@322
   554
    uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
nkeynes@322
   555
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@322
   556
    int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100;
nkeynes@322
   557
    int i, j;
nkeynes@322
   558
    float z = inputz;
nkeynes@322
   559
    do {
nkeynes@322
   560
	uint32_t entry = *tile_list++;
nkeynes@322
   561
	if( entry >> 28 == 0x0F ) {
nkeynes@322
   562
	    break;
nkeynes@322
   563
	} else if( entry >> 28 == 0x0E ) {
nkeynes@322
   564
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@322
   565
	} else {
nkeynes@322
   566
	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
nkeynes@322
   567
	    int is_modified = entry & 0x01000000;
nkeynes@322
   568
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@322
   569
	    int context_length = 3;
nkeynes@351
   570
	    if( (entry & 0x01000000) && (shadow_cfg==0) ) {
nkeynes@322
   571
		context_length = 5;
nkeynes@322
   572
		vertex_length *= 2 ;
nkeynes@322
   573
	    }
nkeynes@322
   574
	    vertex_length += 3;
nkeynes@322
   575
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@322
   576
		/* Triangle(s) */
nkeynes@322
   577
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@322
   578
		float *vertexz = (float *)(polygon+context_length+2);
nkeynes@322
   579
		for( i=0; i<strip_count; i++ ) {
nkeynes@322
   580
		    for( j=0; j<3; j++ ) {
nkeynes@322
   581
			if( *vertexz > z ) {
nkeynes@322
   582
			    z = *vertexz;
nkeynes@322
   583
			}
nkeynes@322
   584
			vertexz += vertex_length;
nkeynes@322
   585
		    }
nkeynes@322
   586
		    vertexz += context_length;
nkeynes@322
   587
		}
nkeynes@322
   588
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@322
   589
		/* Sprite(s) */
nkeynes@322
   590
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@322
   591
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@322
   592
		int i;
nkeynes@322
   593
		float *vertexz = (float *)(polygon+context_length+2);
nkeynes@322
   594
		for( i=0; i<strip_count; i++ ) {
nkeynes@322
   595
		    for( j=0; j<4; j++ ) {
nkeynes@322
   596
			if( *vertexz > z ) {
nkeynes@322
   597
			    z = *vertexz;
nkeynes@322
   598
			}
nkeynes@322
   599
			vertexz += vertex_length;
nkeynes@322
   600
		    }
nkeynes@322
   601
		    vertexz+=context_length;
nkeynes@322
   602
		}
nkeynes@322
   603
	    } else {
nkeynes@322
   604
		/* Polygon */
nkeynes@322
   605
		int i, first=-1, last = -1;
nkeynes@322
   606
		float *vertexz = (float *)polygon+context_length+2;
nkeynes@322
   607
		for( i=0; i<6; i++ ) {
nkeynes@322
   608
		    if( (entry & (0x40000000>>i)) && *vertexz > z ) {
nkeynes@322
   609
			z = *vertexz;
nkeynes@322
   610
		    }
nkeynes@322
   611
		    vertexz += vertex_length;
nkeynes@322
   612
		}
nkeynes@322
   613
	    }
nkeynes@322
   614
	}
nkeynes@322
   615
    } while(1);
nkeynes@322
   616
    return z;
nkeynes@322
   617
}
nkeynes@322
   618
nkeynes@322
   619
/**
nkeynes@322
   620
 * Scan through the scene to determine the largest z value (in order to set up
nkeynes@322
   621
 * an appropriate near clip plane).
nkeynes@322
   622
 */
nkeynes@322
   623
float pvr2_render_find_maximum_z( )
nkeynes@322
   624
{
nkeynes@322
   625
    pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
nkeynes@322
   626
    float maximumz = MMIO_READF( PVR2, RENDER_FARCLIP ); /* Initialize to the far clip plane */
nkeynes@322
   627
nkeynes@322
   628
    struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
nkeynes@322
   629
    do {
nkeynes@322
   630
	
nkeynes@322
   631
	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   632
	    maximumz = render_find_maximum_tile_z(segment->opaque_ptr, maximumz);
nkeynes@322
   633
	}
nkeynes@322
   634
	if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   635
	    maximumz = render_find_maximum_tile_z(segment->opaquemod_ptr, maximumz);
nkeynes@322
   636
	}
nkeynes@322
   637
	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   638
	    maximumz = render_find_maximum_tile_z(segment->trans_ptr, maximumz);
nkeynes@322
   639
	}
nkeynes@322
   640
	if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   641
	    maximumz = render_find_maximum_tile_z(segment->transmod_ptr, maximumz);
nkeynes@322
   642
	}
nkeynes@322
   643
	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   644
	    maximumz = render_find_maximum_tile_z(segment->punchout_ptr, maximumz);
nkeynes@322
   645
	}
nkeynes@322
   646
nkeynes@322
   647
    } while( ((segment++)->control & SEGMENT_END) == 0 );
nkeynes@322
   648
nkeynes@331
   649
    return 1/maximumz;
nkeynes@322
   650
}
nkeynes@338
   651
nkeynes@338
   652
/**
nkeynes@338
   653
 * Scan the segment info to determine the width and height of the render (in 
nkeynes@338
   654
 * pixels).
nkeynes@338
   655
 * @param x,y output values to receive the width and height info.
nkeynes@338
   656
 */
nkeynes@338
   657
void pvr2_render_getsize( int *x, int *y ) 
nkeynes@338
   658
{
nkeynes@338
   659
    pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
nkeynes@338
   660
    int maxx = 0, maxy = 0;
nkeynes@338
   661
nkeynes@338
   662
    struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
nkeynes@338
   663
    do {
nkeynes@338
   664
	int tilex = SEGMENT_X(segment->control);
nkeynes@338
   665
	int tiley = SEGMENT_Y(segment->control);
nkeynes@338
   666
	if( tilex > maxx ) {
nkeynes@338
   667
	    maxx = tilex;
nkeynes@338
   668
	} 
nkeynes@338
   669
	if( tiley > maxy ) {
nkeynes@338
   670
	    maxy = tiley;
nkeynes@338
   671
	}
nkeynes@338
   672
    } while( ((segment++)->control & SEGMENT_END) == 0 );
nkeynes@338
   673
nkeynes@338
   674
    *x = (maxx+1)<<5;
nkeynes@338
   675
    *y = (maxy+1)<<5;
nkeynes@338
   676
}
nkeynes@341
   677
nkeynes@341
   678
void render_print_vertexes( FILE *f, uint32_t poly1, uint32_t *vert_array[], 
nkeynes@341
   679
			    int num_vertexes, int vertex_size )
nkeynes@341
   680
{
nkeynes@341
   681
    char buf[256], *p;
nkeynes@341
   682
    int i,j, k;
nkeynes@341
   683
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@341
   684
	p = buf;
nkeynes@341
   685
	float *vertf = (float *)vert_array[i];
nkeynes@341
   686
	uint32_t *verti = (uint32_t *)vert_array[i];
nkeynes@341
   687
	p += sprintf( p, "  V %9.5f,%9.5f,%9.5f  ", vertf[0], vertf[1], vertf[2] );
nkeynes@341
   688
	k = 3;
nkeynes@341
   689
	if( POLY1_TEXTURED(poly1) ) {
nkeynes@341
   690
	    if( POLY1_UV16(poly1) ) {
nkeynes@341
   691
		p += sprintf( p, "uv=%9.5f,%9.5f  ",
nkeynes@341
   692
			       halftofloat(verti[k]>>16),
nkeynes@341
   693
			       halftofloat(verti[k]) );
nkeynes@341
   694
		k++;
nkeynes@341
   695
	    } else {
nkeynes@341
   696
		p += sprintf( p, "uv=%9.5f,%9.5f  ", vertf[k], vertf[k+1] );
nkeynes@341
   697
		k+=2;
nkeynes@341
   698
	    }
nkeynes@341
   699
	}
nkeynes@341
   700
nkeynes@341
   701
	p += sprintf( p, "%08X ", verti[k++] );
nkeynes@341
   702
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@341
   703
	    p += sprintf( p, "%08X", verti[k++] );
nkeynes@341
   704
	}
nkeynes@341
   705
	p += sprintf( p, "\n" );
nkeynes@341
   706
	fprintf( f, buf );
nkeynes@341
   707
    }
nkeynes@341
   708
}
nkeynes@341
   709
nkeynes@341
   710
void render_print_polygon( FILE *f, uint32_t entry )
nkeynes@341
   711
{
nkeynes@341
   712
    uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
nkeynes@341
   713
    int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100;
nkeynes@341
   714
    int i, j;
nkeynes@341
   715
nkeynes@341
   716
    if( entry >> 28 == 0x0F ) {
nkeynes@341
   717
	fprintf( f, "EOT\n" );
nkeynes@341
   718
    } else if( entry >> 28 == 0x0E ) {
nkeynes@341
   719
	fprintf( f, "LINK %08X\n", entry &0x7FFFFF );
nkeynes@341
   720
    } else {
nkeynes@341
   721
	uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
nkeynes@341
   722
	int is_modified = entry & 0x01000000;
nkeynes@341
   723
	int vertex_length = (entry >> 21) & 0x07;
nkeynes@341
   724
	int context_length = 3;
nkeynes@351
   725
	if( (entry & 0x01000000) && (shadow_cfg==0) ) {
nkeynes@341
   726
	    context_length = 5;
nkeynes@341
   727
	    vertex_length *= 2 ;
nkeynes@341
   728
	}
nkeynes@341
   729
	vertex_length += 3;
nkeynes@341
   730
	if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@341
   731
	    /* Triangle(s) */
nkeynes@341
   732
	    int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@341
   733
	    for( i=0; i<strip_count; i++ ) {
nkeynes@341
   734
		fprintf( f, "TRI  %08X %08X %08X\n", polygon[0], polygon[1], polygon[2] ); 
nkeynes@341
   735
		uint32_t *array[3];
nkeynes@341
   736
		array[0] = polygon + context_length;
nkeynes@341
   737
		array[1] = array[0] + vertex_length;
nkeynes@341
   738
		array[2] = array[1] + vertex_length;
nkeynes@341
   739
		render_print_vertexes( f, *polygon, array, 3, vertex_length );
nkeynes@341
   740
		polygon = array[2] + vertex_length;
nkeynes@341
   741
	    }
nkeynes@341
   742
	} else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@341
   743
	    /* Sprite(s) */
nkeynes@341
   744
	    int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@341
   745
	    for( i=0; i<strip_count; i++ ) {
nkeynes@341
   746
		fprintf( f, "QUAD %08X %08X %08X\n", polygon[0], polygon[1], polygon[2] ); 
nkeynes@341
   747
		uint32_t *array[4];
nkeynes@341
   748
		array[0] = polygon + context_length;
nkeynes@341
   749
		array[1] = array[0] + vertex_length;
nkeynes@341
   750
		array[2] = array[1] + vertex_length;
nkeynes@341
   751
		array[3] = array[2] + vertex_length;
nkeynes@341
   752
		render_print_vertexes( f, *polygon, array, 4, vertex_length );
nkeynes@341
   753
		polygon = array[3] + vertex_length;
nkeynes@341
   754
	    }
nkeynes@341
   755
	} else {
nkeynes@341
   756
	    /* Polygon */
nkeynes@341
   757
	    int last = -1;
nkeynes@341
   758
	    float *vertexz = (float *)polygon+context_length+2;
nkeynes@341
   759
	    uint32_t *array[8];
nkeynes@341
   760
	    for( i=0; i<6; i++ ) {
nkeynes@341
   761
		if( entry & (0x40000000>>i) ) {
nkeynes@341
   762
		    last = i;
nkeynes@341
   763
		}
nkeynes@341
   764
	    }
nkeynes@341
   765
	    fprintf( f, "POLY %08X %08X %08X\n", polygon[0], polygon[1], polygon[2] );
nkeynes@341
   766
	    for( i=0; i<last+2; i++ ) {
nkeynes@341
   767
		array[i] = polygon + context_length + vertex_length*i;
nkeynes@341
   768
	    }
nkeynes@341
   769
	    render_print_vertexes( f, *polygon, array, last+2, vertex_length );
nkeynes@341
   770
	}
nkeynes@341
   771
    }
nkeynes@341
   772
}
nkeynes@341
   773
nkeynes@341
   774
void render_print_tilelist( FILE *f, uint32_t tile_entry )
nkeynes@341
   775
{
nkeynes@341
   776
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@341
   777
    do {
nkeynes@341
   778
	uint32_t entry = *tile_list++;
nkeynes@341
   779
	if( entry >> 28 == 0x0F ) {
nkeynes@341
   780
	    break;
nkeynes@341
   781
	} else if( entry >> 28 == 0x0E ) {
nkeynes@341
   782
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@341
   783
	} else {
nkeynes@341
   784
	    render_print_polygon(f, entry);
nkeynes@341
   785
	}
nkeynes@341
   786
    } while( 1 );
nkeynes@341
   787
}
nkeynes@341
   788
.