Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 331:a6048d3a9a79
prev322:354407942957
next338:8c68d9097846
author nkeynes
date Fri Jan 26 01:37:39 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Z should be rendered as 1/Z - fixes a host of depth issues
file annotate diff log raw
nkeynes@189
     1
/**
nkeynes@331
     2
 * $Id: rendcore.c,v 1.14 2007-01-26 01:37:39 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@318
    60
nkeynes@189
    61
struct tile_segment {
nkeynes@189
    62
    uint32_t control;
nkeynes@189
    63
    pvraddr_t opaque_ptr;
nkeynes@189
    64
    pvraddr_t opaquemod_ptr;
nkeynes@189
    65
    pvraddr_t trans_ptr;
nkeynes@189
    66
    pvraddr_t transmod_ptr;
nkeynes@189
    67
    pvraddr_t punchout_ptr;
nkeynes@189
    68
};
nkeynes@189
    69
nkeynes@189
    70
/**
nkeynes@189
    71
 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
nkeynes@189
    72
 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
nkeynes@189
    73
 * TODO: Check the correctness of this.
nkeynes@189
    74
 */
nkeynes@189
    75
float halftofloat( uint16_t half )
nkeynes@189
    76
{
nkeynes@189
    77
    union {
nkeynes@189
    78
        float f;
nkeynes@189
    79
        uint32_t i;
nkeynes@189
    80
    } temp;
nkeynes@308
    81
    /* int e = ((half & 0x7C00) >> 10) - 15 + 127;
nkeynes@189
    82
nkeynes@189
    83
    temp.i = ((half & 0x8000) << 16) | (e << 23) |
nkeynes@308
    84
    ((half & 0x03FF) << 13); */
nkeynes@308
    85
    temp.i = ((uint32_t)half)<<16;
nkeynes@189
    86
    return temp.f;
nkeynes@189
    87
}
nkeynes@189
    88
nkeynes@189
    89
nkeynes@189
    90
/**
nkeynes@189
    91
 * Setup the GL context for the supplied polygon context.
nkeynes@189
    92
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@189
    93
 * @param modified boolean flag indicating that the modified
nkeynes@189
    94
 *  version should be used, rather than the normal version.
nkeynes@189
    95
 */
nkeynes@189
    96
void render_set_context( uint32_t *context, int render_mode )
nkeynes@189
    97
{
nkeynes@189
    98
    uint32_t poly1 = context[0], poly2, texture;
nkeynes@189
    99
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@189
   100
	poly2 = context[3];
nkeynes@189
   101
	texture = context[4];
nkeynes@189
   102
    } else {
nkeynes@189
   103
	poly2 = context[1];
nkeynes@189
   104
	texture = context[2];
nkeynes@189
   105
    }
nkeynes@189
   106
nkeynes@189
   107
    if( POLY1_DEPTH_ENABLE(poly1) ) {
nkeynes@189
   108
	glEnable( GL_DEPTH_TEST );
nkeynes@189
   109
	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@189
   110
    } else {
nkeynes@189
   111
	glDisable( GL_DEPTH_TEST );
nkeynes@189
   112
    }
nkeynes@189
   113
    
nkeynes@189
   114
    switch( POLY1_CULL_MODE(poly1) ) {
nkeynes@189
   115
    case CULL_NONE:
nkeynes@189
   116
    case CULL_SMALL:
nkeynes@189
   117
	glDisable( GL_CULL_FACE );
nkeynes@189
   118
	break;
nkeynes@189
   119
    case CULL_CCW:
nkeynes@189
   120
	glEnable( GL_CULL_FACE );
nkeynes@189
   121
	glFrontFace( GL_CW );
nkeynes@189
   122
	break;
nkeynes@189
   123
    case CULL_CW:
nkeynes@189
   124
	glEnable( GL_CULL_FACE );
nkeynes@189
   125
	glFrontFace( GL_CCW );
nkeynes@189
   126
	break;
nkeynes@189
   127
    }
nkeynes@189
   128
nkeynes@318
   129
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@318
   130
	glEnable(GL_COLOR_SUM);
nkeynes@318
   131
    } else {
nkeynes@318
   132
	glDisable(GL_COLOR_SUM);
nkeynes@318
   133
    }
nkeynes@318
   134
nkeynes@189
   135
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@189
   136
	int width = POLY2_TEX_WIDTH(poly2);
nkeynes@189
   137
	int height = POLY2_TEX_HEIGHT(poly2);
nkeynes@189
   138
	glEnable(GL_TEXTURE_2D);
nkeynes@286
   139
	texcache_get_texture( (texture&0x000FFFFF)<<3, width, height, texture );
nkeynes@189
   140
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
nkeynes@308
   141
	if( POLY2_TEX_CLAMP_U(poly2) ) {
nkeynes@308
   142
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@308
   143
	} else {
nkeynes@308
   144
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@308
   145
	}	    
nkeynes@308
   146
	if( POLY2_TEX_CLAMP_V(poly2) ) {
nkeynes@308
   147
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@308
   148
	} else {
nkeynes@308
   149
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@308
   150
	}
nkeynes@189
   151
    } else {
nkeynes@189
   152
	glDisable( GL_TEXTURE_2D );
nkeynes@189
   153
    }
nkeynes@189
   154
nkeynes@189
   155
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@189
   156
nkeynes@221
   157
    int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@221
   158
    int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@221
   159
    glBlendFunc( srcblend, destblend );
nkeynes@318
   160
nkeynes@322
   161
    if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@322
   162
	ERROR( "Accumulation buffer not supported" );
nkeynes@322
   163
    }
nkeynes@322
   164
	    
nkeynes@318
   165
    pvr2_force_fragment_alpha = POLY2_ALPHA_ENABLE(poly2) ? FALSE : TRUE;
nkeynes@318
   166
nkeynes@189
   167
}
nkeynes@189
   168
nkeynes@319
   169
#define FARGB_A(x) (((float)(((x)>>24)+1))/256.0)
nkeynes@319
   170
#define FARGB_R(x) (((float)((((x)>>16)&0xFF)+1))/256.0)
nkeynes@319
   171
#define FARGB_G(x) (((float)((((x)>>8)&0xFF)+1))/256.0)
nkeynes@319
   172
#define FARGB_B(x) (((float)(((x)&0xFF)+1))/256.0)
nkeynes@319
   173
nkeynes@319
   174
void render_unpack_vertexes( struct vertex_unpacked *out, uint32_t poly1, 
nkeynes@319
   175
			     uint32_t *vertexes, int num_vertexes,
nkeynes@319
   176
			     int vertex_size, int render_mode )
nkeynes@319
   177
{
nkeynes@319
   178
    int m = 0, i;
nkeynes@319
   179
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@319
   180
	m = (vertex_size - 3)/2;
nkeynes@319
   181
    }
nkeynes@319
   182
nkeynes@319
   183
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   184
	float *vertexf = (float *)vertexes;
nkeynes@319
   185
	int k = m + 3;
nkeynes@319
   186
	out[i].x = vertexf[0];
nkeynes@319
   187
	out[i].y = vertexf[1];
nkeynes@319
   188
	out[i].z = vertexf[2];
nkeynes@319
   189
    	if( POLY1_TEXTURED(poly1) ) {
nkeynes@319
   190
	    if( POLY1_UV16(poly1) ) {
nkeynes@319
   191
		out[i].u = halftofloat(vertexes[k]>>16);
nkeynes@319
   192
		out[i].v = halftofloat(vertexes[k]);
nkeynes@319
   193
		k++;
nkeynes@319
   194
	    } else {
nkeynes@319
   195
		out[i].u = vertexf[k];
nkeynes@319
   196
		out[i].v = vertexf[k+1];
nkeynes@319
   197
		k+=2;
nkeynes@319
   198
	    }
nkeynes@319
   199
	} else {
nkeynes@319
   200
	    out[i].u = 0;
nkeynes@319
   201
	    out[i].v = 0;
nkeynes@319
   202
	}
nkeynes@319
   203
	uint32_t argb = vertexes[k++];
nkeynes@319
   204
	out[i].rgba[0] = FARGB_R(argb);
nkeynes@319
   205
	out[i].rgba[1] = FARGB_G(argb);
nkeynes@319
   206
        out[i].rgba[2] = FARGB_B(argb);
nkeynes@319
   207
	out[i].rgba[3] = FARGB_A(argb);
nkeynes@319
   208
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@319
   209
	    uint32_t offset = vertexes[k++];
nkeynes@319
   210
	    out[i].offset_rgba[0] = FARGB_R(argb);
nkeynes@319
   211
	    out[i].offset_rgba[1] = FARGB_G(argb);
nkeynes@319
   212
	    out[i].offset_rgba[2] = FARGB_B(argb);
nkeynes@319
   213
	    out[i].offset_rgba[3] = FARGB_A(argb);
nkeynes@319
   214
	}
nkeynes@319
   215
	vertexes += vertex_size;
nkeynes@319
   216
    }
nkeynes@319
   217
}
nkeynes@319
   218
nkeynes@319
   219
/**
nkeynes@319
   220
 * Unpack the vertexes for a quad, calculating the values for the last
nkeynes@319
   221
 * vertex.
nkeynes@319
   222
 * FIXME: Integrate this with rendbkg somehow
nkeynes@319
   223
 */
nkeynes@319
   224
void render_unpack_quad( struct vertex_unpacked *unpacked, uint32_t poly1, 
nkeynes@319
   225
			 uint32_t *vertexes, int vertex_size,
nkeynes@319
   226
			 int render_mode )
nkeynes@319
   227
{
nkeynes@319
   228
    int i;
nkeynes@319
   229
    struct vertex_unpacked diff0, diff1;
nkeynes@319
   230
nkeynes@319
   231
    render_unpack_vertexes( unpacked, poly1, vertexes, 3, vertex_size, render_mode );
nkeynes@319
   232
    
nkeynes@319
   233
    diff0.x = unpacked[0].x - unpacked[1].x;
nkeynes@319
   234
    diff0.y = unpacked[0].y - unpacked[1].y;
nkeynes@319
   235
    diff1.x = unpacked[2].x - unpacked[1].x;
nkeynes@319
   236
    diff1.y = unpacked[2].y - unpacked[1].y;
nkeynes@319
   237
nkeynes@319
   238
    float detxy = ((diff1.y) * (diff0.x)) - ((diff0.y) * (diff1.x));
nkeynes@319
   239
    float *vertexf = (float *)(vertexes+(vertex_size*3));
nkeynes@319
   240
    if( detxy == 0 ) {
nkeynes@319
   241
	memcpy( &unpacked[3], &unpacked[2], sizeof(struct vertex_unpacked) );
nkeynes@319
   242
	unpacked[3].x = vertexf[0];
nkeynes@319
   243
	unpacked[3].y = vertexf[1];
nkeynes@319
   244
	return;
nkeynes@319
   245
    }	
nkeynes@319
   246
nkeynes@319
   247
    unpacked[3].x = vertexf[0];
nkeynes@319
   248
    unpacked[3].y = vertexf[1];
nkeynes@319
   249
    float t = ((unpacked[3].x - unpacked[1].x) * diff1.y -
nkeynes@319
   250
	       (unpacked[3].y - unpacked[1].y) * diff1.x) / detxy;
nkeynes@319
   251
    float s = ((unpacked[3].y - unpacked[1].y) * diff0.x -
nkeynes@319
   252
	       (unpacked[3].x - unpacked[1].x) * diff0.y) / detxy;
nkeynes@331
   253
    diff0.z = (1/unpacked[0].z) - (1/unpacked[1].z);
nkeynes@331
   254
    diff1.z = (1/unpacked[2].z) - (1/unpacked[1].z);
nkeynes@331
   255
    unpacked[3].z = 1/((1/unpacked[1].z) + (t*diff0.z) + (s*diff1.z));
nkeynes@319
   256
nkeynes@319
   257
    diff0.u = unpacked[0].u - unpacked[1].u;
nkeynes@319
   258
    diff0.v = unpacked[0].v - unpacked[1].v;
nkeynes@319
   259
    diff1.u = unpacked[2].u - unpacked[1].u;
nkeynes@319
   260
    diff1.v = unpacked[2].v - unpacked[1].v;
nkeynes@319
   261
    unpacked[3].u = unpacked[1].u + (t*diff0.u) + (s*diff1.u);
nkeynes@319
   262
    unpacked[3].v = unpacked[1].v + (t*diff0.v) + (s*diff1.v);
nkeynes@319
   263
nkeynes@319
   264
    if( !POLY1_GOURAUD_SHADED(poly1) ) {
nkeynes@319
   265
	memcpy( unpacked[3].rgba, unpacked[2].rgba, sizeof(unpacked[2].rgba) );
nkeynes@319
   266
	memcpy( unpacked[3].offset_rgba, unpacked[2].offset_rgba, sizeof(unpacked[2].offset_rgba) );
nkeynes@319
   267
    } else {
nkeynes@319
   268
	for( i=0; i<4; i++ ) {
nkeynes@319
   269
	    float d0 = unpacked[0].rgba[i] - unpacked[1].rgba[i];
nkeynes@319
   270
	    float d1 = unpacked[2].rgba[i] - unpacked[1].rgba[i];
nkeynes@319
   271
	    unpacked[3].rgba[i] = unpacked[1].rgba[i] + (t*d0) + (s*d1);
nkeynes@319
   272
	    d0 = unpacked[0].offset_rgba[i] - unpacked[1].offset_rgba[i];
nkeynes@319
   273
	    d1 = unpacked[2].offset_rgba[i] - unpacked[1].offset_rgba[i];
nkeynes@319
   274
	    unpacked[3].offset_rgba[i] = unpacked[1].offset_rgba[i] + (t*d0) + (s*d1);
nkeynes@319
   275
	}
nkeynes@319
   276
    }    
nkeynes@319
   277
}
nkeynes@319
   278
nkeynes@319
   279
void render_unpacked_vertex_array( uint32_t poly1, struct vertex_unpacked *vertexes[], 
nkeynes@319
   280
				   int num_vertexes ) {
nkeynes@319
   281
    int i;
nkeynes@319
   282
nkeynes@319
   283
    glBegin( GL_TRIANGLE_STRIP );
nkeynes@319
   284
nkeynes@319
   285
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   286
	if( POLY1_TEXTURED(poly1) ) {
nkeynes@319
   287
	    glTexCoord2f( vertexes[i]->u, vertexes[i]->v );
nkeynes@319
   288
	}
nkeynes@319
   289
nkeynes@322
   290
	if( pvr2_force_fragment_alpha ) {
nkeynes@322
   291
	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2], 1.0 );
nkeynes@322
   292
	} else {
nkeynes@322
   293
	    glColor4f( vertexes[i]->rgba[0], vertexes[i]->rgba[1], vertexes[i]->rgba[2],
nkeynes@322
   294
		       vertexes[i]->rgba[3] );
nkeynes@322
   295
	}
nkeynes@319
   296
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@319
   297
	    glSecondaryColor3fEXT( vertexes[i]->offset_rgba[0],
nkeynes@319
   298
				   vertexes[i]->offset_rgba[1],
nkeynes@319
   299
				   vertexes[i]->offset_rgba[2] );
nkeynes@319
   300
	}
nkeynes@331
   301
	glVertex3f( vertexes[i]->x, vertexes[i]->y, 1/vertexes[i]->z );
nkeynes@319
   302
    }
nkeynes@319
   303
nkeynes@319
   304
    glEnd();
nkeynes@319
   305
}
nkeynes@319
   306
nkeynes@319
   307
void render_quad_vertexes( uint32_t poly1, uint32_t *vertexes, int vertex_size, int render_mode )
nkeynes@319
   308
{
nkeynes@319
   309
    struct vertex_unpacked unpacked[4];
nkeynes@319
   310
    struct vertex_unpacked *pt[4] = {&unpacked[0], &unpacked[1], &unpacked[3], &unpacked[2]};
nkeynes@319
   311
    render_unpack_quad( unpacked, poly1, vertexes, vertex_size, render_mode );
nkeynes@319
   312
    render_unpacked_vertex_array( poly1, pt, 4 );
nkeynes@319
   313
}
nkeynes@319
   314
nkeynes@319
   315
void render_vertex_array( uint32_t poly1, uint32_t *vert_array[], int num_vertexes, int vertex_size,
nkeynes@319
   316
			  int render_mode ) 
nkeynes@189
   317
{
nkeynes@189
   318
    int i, m=0;
nkeynes@189
   319
nkeynes@189
   320
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@189
   321
	m = (vertex_size - 3)/2;
nkeynes@189
   322
    }
nkeynes@189
   323
nkeynes@189
   324
    glBegin( GL_TRIANGLE_STRIP );
nkeynes@189
   325
    
nkeynes@189
   326
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   327
	uint32_t *vertexes = vert_array[i];
nkeynes@319
   328
	float *vertexf = (float *)vert_array[i];
nkeynes@189
   329
	uint32_t argb;
nkeynes@318
   330
	int k = m + 3;
nkeynes@189
   331
	if( POLY1_TEXTURED(poly1) ) {
nkeynes@189
   332
	    if( POLY1_UV16(poly1) ) {
nkeynes@318
   333
		glTexCoord2f( halftofloat(vertexes[k]>>16),
nkeynes@318
   334
			      halftofloat(vertexes[k]) );
nkeynes@318
   335
		k++;
nkeynes@189
   336
	    } else {
nkeynes@318
   337
		glTexCoord2f( vertexf[k], vertexf[k+1] );
nkeynes@318
   338
		k+=2;
nkeynes@189
   339
	    }
nkeynes@189
   340
	}
nkeynes@189
   341
nkeynes@318
   342
	argb = vertexes[k++];
nkeynes@318
   343
	if( pvr2_force_fragment_alpha ) {
nkeynes@318
   344
	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
nkeynes@318
   345
			(GLubyte)argb, 0xFF );
nkeynes@318
   346
	} else {
nkeynes@318
   347
	    glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
nkeynes@318
   348
			(GLubyte)argb, (GLubyte)(argb >> 24) );
nkeynes@318
   349
	}
nkeynes@318
   350
nkeynes@318
   351
	if( POLY1_SPECULAR(poly1) ) {
nkeynes@318
   352
	    uint32_t spec = vertexes[k++];
nkeynes@318
   353
	    glSecondaryColor3ubEXT( (GLubyte)(spec >> 16), (GLubyte)(spec >> 8), 
nkeynes@318
   354
				 (GLubyte)spec );
nkeynes@318
   355
	}
nkeynes@331
   356
	glVertex3f( vertexf[0], vertexf[1], 1/vertexf[2] );
nkeynes@189
   357
	vertexes += vertex_size;
nkeynes@189
   358
    }
nkeynes@189
   359
nkeynes@189
   360
    glEnd();
nkeynes@189
   361
}
nkeynes@189
   362
nkeynes@319
   363
void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
nkeynes@319
   364
		      int render_mode )
nkeynes@319
   365
{
nkeynes@319
   366
    uint32_t *vert_array[num_vertexes];
nkeynes@319
   367
    int i;
nkeynes@319
   368
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@319
   369
	vert_array[i] = vertexes;
nkeynes@319
   370
	vertexes += vertex_size;
nkeynes@319
   371
    }
nkeynes@319
   372
    render_vertex_array( poly1, vert_array, num_vertexes, vertex_size, render_mode );
nkeynes@319
   373
}
nkeynes@319
   374
nkeynes@189
   375
/**
nkeynes@189
   376
 * Render a simple (not auto-sorted) tile
nkeynes@189
   377
 */
nkeynes@189
   378
void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
nkeynes@215
   379
    uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
nkeynes@189
   380
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@189
   381
    do {
nkeynes@189
   382
	uint32_t entry = *tile_list++;
nkeynes@189
   383
	if( entry >> 28 == 0x0F ) {
nkeynes@189
   384
	    break;
nkeynes@189
   385
	} else if( entry >> 28 == 0x0E ) {
nkeynes@189
   386
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@189
   387
	} else {
nkeynes@215
   388
	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
nkeynes@189
   389
	    int is_modified = entry & 0x01000000;
nkeynes@189
   390
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@189
   391
	    int context_length = 3;
nkeynes@189
   392
	    if( is_modified && !cheap_modifier_mode ) {
nkeynes@189
   393
		context_length = 5;
nkeynes@189
   394
		vertex_length *= 2 ;
nkeynes@189
   395
	    }
nkeynes@189
   396
	    vertex_length += 3;
nkeynes@308
   397
	    
nkeynes@189
   398
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@189
   399
		/* Triangle(s) */
nkeynes@189
   400
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@189
   401
		int polygon_length = 3 * vertex_length + context_length;
nkeynes@189
   402
		int i;
nkeynes@189
   403
		for( i=0; i<strip_count; i++ ) {
nkeynes@189
   404
		    render_set_context( polygon, render_mode );
nkeynes@189
   405
		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
nkeynes@189
   406
				     render_mode );
nkeynes@189
   407
		    polygon += polygon_length;
nkeynes@189
   408
		}
nkeynes@189
   409
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@189
   410
		/* Sprite(s) */
nkeynes@319
   411
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@189
   412
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@189
   413
		int i;
nkeynes@189
   414
		for( i=0; i<strip_count; i++ ) {
nkeynes@189
   415
		    render_set_context( polygon, render_mode );
nkeynes@319
   416
		    render_quad_vertexes( *polygon, polygon+context_length, vertex_length,
nkeynes@319
   417
					  render_mode );
nkeynes@189
   418
		    polygon += polygon_length;
nkeynes@189
   419
		}
nkeynes@189
   420
	    } else {
nkeynes@189
   421
		/* Polygon */
nkeynes@189
   422
		int i, first=-1, last = -1;
nkeynes@189
   423
		for( i=0; i<6; i++ ) {
nkeynes@189
   424
		    if( entry & (0x40000000>>i) ) {
nkeynes@189
   425
			if( first == -1 ) first = i;
nkeynes@189
   426
			last = i;
nkeynes@189
   427
		    }
nkeynes@189
   428
		}
nkeynes@189
   429
		if( first != -1 ) {
nkeynes@189
   430
		    first = 0;
nkeynes@189
   431
		    render_set_context(polygon, render_mode);
nkeynes@189
   432
		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
nkeynes@189
   433
				     (last-first+3), vertex_length, render_mode );
nkeynes@189
   434
		}
nkeynes@189
   435
	    }
nkeynes@189
   436
	}
nkeynes@189
   437
    } while( 1 );
nkeynes@189
   438
}
nkeynes@189
   439
nkeynes@189
   440
void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
nkeynes@189
   441
			int clipx2, int clipy2 ) {
nkeynes@189
   442
nkeynes@191
   443
    pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
nkeynes@189
   444
    int tile_sort;
nkeynes@189
   445
    gboolean cheap_shadow;
nkeynes@189
   446
nkeynes@191
   447
    int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
nkeynes@191
   448
    int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
nkeynes@191
   449
    int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
nkeynes@189
   450
nkeynes@222
   451
    if( (obj_config & 0x00200000) == 0 ) {
nkeynes@189
   452
	if( isp_config & 1 ) {
nkeynes@189
   453
	    tile_sort = 0;
nkeynes@189
   454
	} else {
nkeynes@189
   455
	    tile_sort = 2;
nkeynes@189
   456
	}
nkeynes@189
   457
    } else {
nkeynes@189
   458
	tile_sort = 1;
nkeynes@189
   459
    }
nkeynes@189
   460
nkeynes@189
   461
    cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
nkeynes@189
   462
nkeynes@189
   463
    struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
nkeynes@189
   464
nkeynes@215
   465
    struct timeval tv_start, tv_end;
nkeynes@215
   466
    gettimeofday(&tv_start, NULL);
nkeynes@189
   467
    glEnable( GL_SCISSOR_TEST );
nkeynes@268
   468
    do {
nkeynes@221
   469
	// fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
nkeynes@189
   470
	int tilex = SEGMENT_X(segment->control);
nkeynes@189
   471
	int tiley = SEGMENT_Y(segment->control);
nkeynes@189
   472
	
nkeynes@189
   473
	int x1 = tilex << 5;
nkeynes@189
   474
	int y1 = tiley << 5;
nkeynes@189
   475
	if( x1 + 32 <= clipx1 ||
nkeynes@189
   476
	    y1 + 32 <= clipy1 ||
nkeynes@189
   477
	    x1 >= clipx2 ||
nkeynes@189
   478
	    y1 >= clipy2 ) {
nkeynes@189
   479
	    /* Tile completely clipped, skip */
nkeynes@189
   480
	    continue;
nkeynes@189
   481
	}
nkeynes@215
   482
nkeynes@189
   483
	/* Set a scissor on the visible part of the tile */
nkeynes@189
   484
	int w = MIN(x1+32, clipx2) - x1;
nkeynes@189
   485
	int h = MIN(y1+32, clipy2) - y1;
nkeynes@189
   486
	x1 = MAX(x1,clipx1);
nkeynes@189
   487
	y1 = MAX(y1,clipy1);
nkeynes@189
   488
	glScissor( x1, height-y1-h, w, h );
nkeynes@189
   489
nkeynes@189
   490
	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   491
	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   492
		/* TODO */
nkeynes@189
   493
	    }
nkeynes@189
   494
	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   495
	}
nkeynes@189
   496
nkeynes@189
   497
	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   498
	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   499
		/* TODO */
nkeynes@189
   500
	    } 
nkeynes@222
   501
	    if( tile_sort == 2 || 
nkeynes@222
   502
		(tile_sort == 1 && ((segment->control & SEGMENT_SORT_TRANS)==0)) ) {
nkeynes@189
   503
		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   504
	    } else {
nkeynes@189
   505
		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   506
	    }
nkeynes@189
   507
	}
nkeynes@189
   508
nkeynes@189
   509
	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
nkeynes@189
   510
	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
nkeynes@189
   511
	}
nkeynes@268
   512
    } while( ((segment++)->control & SEGMENT_END) == 0 );
nkeynes@189
   513
    glDisable( GL_SCISSOR_TEST );
nkeynes@215
   514
nkeynes@215
   515
    gettimeofday(&tv_end, NULL);
nkeynes@215
   516
    timersub(&tv_end,&tv_start, &tv_start);
nkeynes@189
   517
}
nkeynes@322
   518
nkeynes@322
   519
static float render_find_maximum_tile_z( pvraddr_t tile_entry, float inputz )
nkeynes@322
   520
{
nkeynes@322
   521
    uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
nkeynes@322
   522
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@322
   523
    int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100;
nkeynes@322
   524
    int i, j;
nkeynes@322
   525
    float z = inputz;
nkeynes@322
   526
    do {
nkeynes@322
   527
	uint32_t entry = *tile_list++;
nkeynes@322
   528
	if( entry >> 28 == 0x0F ) {
nkeynes@322
   529
	    break;
nkeynes@322
   530
	} else if( entry >> 28 == 0x0E ) {
nkeynes@322
   531
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@322
   532
	} else {
nkeynes@322
   533
	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
nkeynes@322
   534
	    int is_modified = entry & 0x01000000;
nkeynes@322
   535
	    int vertex_length = (entry >> 21) & 0x07;
nkeynes@322
   536
	    int context_length = 3;
nkeynes@322
   537
	    if( (entry & 0x01000000) && shadow_cfg ) {
nkeynes@322
   538
		context_length = 5;
nkeynes@322
   539
		vertex_length *= 2 ;
nkeynes@322
   540
	    }
nkeynes@322
   541
	    vertex_length += 3;
nkeynes@322
   542
	    if( (entry & 0xE0000000) == 0x80000000 ) {
nkeynes@322
   543
		/* Triangle(s) */
nkeynes@322
   544
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@322
   545
		float *vertexz = (float *)(polygon+context_length+2);
nkeynes@322
   546
		for( i=0; i<strip_count; i++ ) {
nkeynes@322
   547
		    for( j=0; j<3; j++ ) {
nkeynes@322
   548
			if( *vertexz > z ) {
nkeynes@322
   549
			    z = *vertexz;
nkeynes@322
   550
			}
nkeynes@322
   551
			vertexz += vertex_length;
nkeynes@322
   552
		    }
nkeynes@322
   553
		    vertexz += context_length;
nkeynes@322
   554
		}
nkeynes@322
   555
	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
nkeynes@322
   556
		/* Sprite(s) */
nkeynes@322
   557
		int strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@322
   558
		int polygon_length = 4 * vertex_length + context_length;
nkeynes@322
   559
		int i;
nkeynes@322
   560
		float *vertexz = (float *)(polygon+context_length+2);
nkeynes@322
   561
		for( i=0; i<strip_count; i++ ) {
nkeynes@322
   562
		    for( j=0; j<4; j++ ) {
nkeynes@322
   563
			if( *vertexz > z ) {
nkeynes@322
   564
			    z = *vertexz;
nkeynes@322
   565
			}
nkeynes@322
   566
			vertexz += vertex_length;
nkeynes@322
   567
		    }
nkeynes@322
   568
		    vertexz+=context_length;
nkeynes@322
   569
		}
nkeynes@322
   570
	    } else {
nkeynes@322
   571
		/* Polygon */
nkeynes@322
   572
		int i, first=-1, last = -1;
nkeynes@322
   573
		float *vertexz = (float *)polygon+context_length+2;
nkeynes@322
   574
		for( i=0; i<6; i++ ) {
nkeynes@322
   575
		    if( (entry & (0x40000000>>i)) && *vertexz > z ) {
nkeynes@322
   576
			z = *vertexz;
nkeynes@322
   577
		    }
nkeynes@322
   578
		    vertexz += vertex_length;
nkeynes@322
   579
		}
nkeynes@322
   580
	    }
nkeynes@322
   581
	}
nkeynes@322
   582
    } while(1);
nkeynes@322
   583
    return z;
nkeynes@322
   584
}
nkeynes@322
   585
nkeynes@322
   586
/**
nkeynes@322
   587
 * Scan through the scene to determine the largest z value (in order to set up
nkeynes@322
   588
 * an appropriate near clip plane).
nkeynes@322
   589
 */
nkeynes@322
   590
float pvr2_render_find_maximum_z( )
nkeynes@322
   591
{
nkeynes@322
   592
    pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
nkeynes@322
   593
    float maximumz = MMIO_READF( PVR2, RENDER_FARCLIP ); /* Initialize to the far clip plane */
nkeynes@322
   594
nkeynes@322
   595
    struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
nkeynes@322
   596
    do {
nkeynes@322
   597
	
nkeynes@322
   598
	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   599
	    maximumz = render_find_maximum_tile_z(segment->opaque_ptr, maximumz);
nkeynes@322
   600
	}
nkeynes@322
   601
	if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   602
	    maximumz = render_find_maximum_tile_z(segment->opaquemod_ptr, maximumz);
nkeynes@322
   603
	}
nkeynes@322
   604
	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   605
	    maximumz = render_find_maximum_tile_z(segment->trans_ptr, maximumz);
nkeynes@322
   606
	}
nkeynes@322
   607
	if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   608
	    maximumz = render_find_maximum_tile_z(segment->transmod_ptr, maximumz);
nkeynes@322
   609
	}
nkeynes@322
   610
	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
nkeynes@322
   611
	    maximumz = render_find_maximum_tile_z(segment->punchout_ptr, maximumz);
nkeynes@322
   612
	}
nkeynes@322
   613
nkeynes@322
   614
    } while( ((segment++)->control & SEGMENT_END) == 0 );
nkeynes@322
   615
nkeynes@331
   616
    return 1/maximumz;
nkeynes@322
   617
}
.