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