Search
lxdream.org :: lxdream/src/pvr2/render.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/render.c
changeset 144:7f0714e89aaa
prev132:48a8cc541742
next161:408b9210395f
author nkeynes
date Mon May 15 08:28:52 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Rename video_driver to display_driver
Add input source to display
Implement configuration file support
Hook controllers up to configuration
file annotate diff log raw
nkeynes@100
     1
/**
nkeynes@144
     2
 * $Id: render.c,v 1.8 2006-05-15 08:28:52 nkeynes Exp $
nkeynes@100
     3
 *
nkeynes@100
     4
 * PVR2 Renderer support. This is where the real work happens.
nkeynes@100
     5
 *
nkeynes@100
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@100
     7
 *
nkeynes@100
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@100
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@100
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@100
    11
 * (at your option) any later version.
nkeynes@100
    12
 *
nkeynes@100
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@100
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@100
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@100
    16
 * GNU General Public License for more details.
nkeynes@100
    17
 */
nkeynes@100
    18
nkeynes@100
    19
#include "pvr2/pvr2.h"
nkeynes@100
    20
#include "asic.h"
nkeynes@103
    21
nkeynes@103
    22
nkeynes@118
    23
#define POLY_COLOUR_PACKED 0x00000000
nkeynes@118
    24
#define POLY_COLOUR_FLOAT 0x00000010
nkeynes@118
    25
#define POLY_COLOUR_INTENSITY 0x00000020
nkeynes@118
    26
#define POLY_COLOUR_INTENSITY_PREV 0x00000030
nkeynes@103
    27
nkeynes@103
    28
static int pvr2_poly_vertexes[4] = { 3, 4, 6, 8 };
nkeynes@103
    29
static int pvr2_poly_type[4] = { GL_TRIANGLES, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLE_STRIP };
nkeynes@103
    30
static int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
nkeynes@103
    31
				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
nkeynes@103
    32
				      GL_ALWAYS };
nkeynes@103
    33
static int pvr2_poly_srcblend[8] = { 
nkeynes@103
    34
    GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
nkeynes@103
    35
    GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
nkeynes@103
    36
    GL_ONE_MINUS_DST_ALPHA };
nkeynes@103
    37
static int pvr2_poly_dstblend[8] = {
nkeynes@103
    38
    GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
nkeynes@103
    39
    GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
nkeynes@103
    40
    GL_ONE_MINUS_DST_ALPHA };
nkeynes@108
    41
static int pvr2_poly_texblend[4] = {
nkeynes@108
    42
    GL_REPLACE, GL_BLEND, GL_DECAL, GL_MODULATE };
nkeynes@103
    43
static int pvr2_render_colour_format[8] = {
nkeynes@103
    44
    COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
nkeynes@103
    45
    COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
nkeynes@103
    46
nkeynes@103
    47
#define POLY_STRIP_TYPE(poly) ( pvr2_poly_type[((poly->command)>>18)&0x03] )
nkeynes@103
    48
#define POLY_STRIP_VERTEXES(poly) ( pvr2_poly_vertexes[((poly->command)>>18)&0x03] )
nkeynes@103
    49
#define POLY_DEPTH_MODE(poly) ( pvr2_poly_depthmode[poly->poly_cfg>>29] )
nkeynes@128
    50
#define POLY_DEPTH_WRITE(poly) ((poly->poly_cfg&0x04000000) == 0 )
nkeynes@103
    51
#define POLY_TEX_WIDTH(poly) ( 1<< (((poly->poly_mode >> 3) & 0x07 ) + 3) )
nkeynes@103
    52
#define POLY_TEX_HEIGHT(poly) ( 1<< (((poly->poly_mode) & 0x07 ) + 3) )
nkeynes@103
    53
#define POLY_BLEND_SRC(poly) ( pvr2_poly_srcblend[(poly->poly_mode) >> 29] )
nkeynes@103
    54
#define POLY_BLEND_DEST(poly) ( pvr2_poly_dstblend[((poly->poly_mode)>>26)&0x07] )
nkeynes@108
    55
#define POLY_TEX_BLEND(poly) ( pvr2_poly_texblend[((poly->poly_mode) >> 6)&0x03] )
nkeynes@118
    56
#define POLY_COLOUR_TYPE(poly) ( poly->command & 0x00000030 )
nkeynes@100
    57
nkeynes@100
    58
/**
nkeynes@103
    59
 * Describes a rendering buffer that's actually held in GL, for when we need
nkeynes@103
    60
 * to fetch the bits back to vram.
nkeynes@103
    61
 */
nkeynes@103
    62
typedef struct pvr2_render_buffer {
nkeynes@103
    63
    uint32_t render_addr; /* The actual address rendered to in pvr ram */
nkeynes@103
    64
    int width, height;
nkeynes@103
    65
    int colour_format;
nkeynes@103
    66
} *pvr2_render_buffer_t;
nkeynes@103
    67
nkeynes@103
    68
struct pvr2_render_buffer front_buffer;
nkeynes@103
    69
struct pvr2_render_buffer back_buffer;
nkeynes@103
    70
nkeynes@103
    71
struct tile_descriptor {
nkeynes@103
    72
    uint32_t header[6];
nkeynes@103
    73
    struct tile_pointers {
nkeynes@103
    74
	uint32_t tile_id;
nkeynes@103
    75
	uint32_t opaque_ptr;
nkeynes@103
    76
	uint32_t opaque_mod_ptr;
nkeynes@103
    77
	uint32_t trans_ptr;
nkeynes@103
    78
	uint32_t trans_mod_ptr;
nkeynes@103
    79
	uint32_t punchout_ptr;
nkeynes@103
    80
    } tile[0];
nkeynes@103
    81
};
nkeynes@103
    82
nkeynes@103
    83
/* Textured polygon */
nkeynes@103
    84
struct pvr2_poly {
nkeynes@103
    85
    uint32_t command;
nkeynes@103
    86
    uint32_t poly_cfg; /* Bitmask */
nkeynes@103
    87
    uint32_t poly_mode; /* texture/blending mask */
nkeynes@103
    88
    uint32_t texture; /* texture data */
nkeynes@103
    89
    float alpha;
nkeynes@103
    90
    float red;
nkeynes@103
    91
    float green;
nkeynes@103
    92
    float blue;
nkeynes@103
    93
};
nkeynes@103
    94
nkeynes@103
    95
struct pvr2_specular_highlight {
nkeynes@103
    96
    float base_alpha;
nkeynes@103
    97
    float base_red;
nkeynes@103
    98
    float base_green;
nkeynes@103
    99
    float base_blue;
nkeynes@103
   100
    float offset_alpha;
nkeynes@103
   101
    float offset_red;
nkeynes@103
   102
    float offset_green;
nkeynes@103
   103
    float offset_blue;
nkeynes@103
   104
};
nkeynes@103
   105
				     
nkeynes@103
   106
nkeynes@118
   107
struct pvr2_vertex_packed {
nkeynes@103
   108
    uint32_t command;
nkeynes@103
   109
    float x, y, z;
nkeynes@103
   110
    float s,t;
nkeynes@118
   111
    uint32_t colour;
nkeynes@103
   112
    float f;
nkeynes@103
   113
};
nkeynes@103
   114
nkeynes@128
   115
struct pvr2_vertex_float {
nkeynes@128
   116
    uint32_t command;
nkeynes@128
   117
    float x,y,z;
nkeynes@128
   118
    float a, r, g, b;
nkeynes@128
   119
};
nkeynes@128
   120
nkeynes@128
   121
union pvr2_vertex {
nkeynes@128
   122
    struct pvr2_vertex_packed pack;
nkeynes@128
   123
    struct pvr2_vertex_float flt;
nkeynes@128
   124
};
nkeynes@128
   125
nkeynes@128
   126
typedef struct pvr2_bgplane_packed {
nkeynes@128
   127
        uint32_t        poly_cfg, poly_mode;
nkeynes@128
   128
        uint32_t        texture_mode;
nkeynes@128
   129
        float           x1, y1, z1;
nkeynes@128
   130
        uint32_t          colour1;
nkeynes@128
   131
        float           x2, y2, z2;
nkeynes@128
   132
        uint32_t          colour2;
nkeynes@128
   133
        float           x3, y3, z3;
nkeynes@128
   134
        uint32_t          colour3;
nkeynes@128
   135
} *pvr2_bgplane_packed_t;
nkeynes@128
   136
nkeynes@128
   137
nkeynes@103
   138
nkeynes@103
   139
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   140
			      gboolean backBuffer );
nkeynes@103
   141
nkeynes@108
   142
int pvr2_render_font_list = -1;
nkeynes@132
   143
int pvr2_render_trace = 0;
nkeynes@108
   144
nkeynes@132
   145
int glPrintf( int x, int y, const char *fmt, ... )
nkeynes@108
   146
{
nkeynes@108
   147
    va_list ap;     /* our argument pointer */
nkeynes@108
   148
    char buf[256];
nkeynes@108
   149
    int len;
nkeynes@108
   150
    if (fmt == NULL)    /* if there is no string to draw do nothing */
nkeynes@108
   151
        return;
nkeynes@108
   152
    va_start(ap, fmt); 
nkeynes@108
   153
    len = vsnprintf(buf, sizeof(buf), fmt, ap);
nkeynes@108
   154
    va_end(ap);
nkeynes@108
   155
nkeynes@108
   156
nkeynes@108
   157
    glPushAttrib(GL_LIST_BIT);
nkeynes@132
   158
    glDisable( GL_DEPTH_TEST );
nkeynes@132
   159
    glDisable( GL_BLEND );
nkeynes@132
   160
    glDisable( GL_TEXTURE_2D );
nkeynes@132
   161
    glDisable( GL_ALPHA_TEST );
nkeynes@108
   162
    glListBase(pvr2_render_font_list - 32);
nkeynes@132
   163
    glColor3f( 1.0, 1.0, 1.0 );
nkeynes@132
   164
    glRasterPos2i( x, y );
nkeynes@108
   165
    glCallLists(len, GL_UNSIGNED_BYTE, buf);
nkeynes@108
   166
    glPopAttrib();
nkeynes@108
   167
nkeynes@108
   168
    return len;
nkeynes@108
   169
}
nkeynes@108
   170
nkeynes@103
   171
nkeynes@103
   172
gboolean pvr2_render_init( void )
nkeynes@103
   173
{
nkeynes@103
   174
    front_buffer.render_addr = -1;
nkeynes@103
   175
    back_buffer.render_addr = -1;
nkeynes@103
   176
}
nkeynes@103
   177
nkeynes@103
   178
/**
nkeynes@103
   179
 * Display a rendered frame if one is available.
nkeynes@103
   180
 * @param address An address in PVR ram (0500000 range).
nkeynes@103
   181
 * @return TRUE if a frame was available to be displayed, otherwise false.
nkeynes@103
   182
 */
nkeynes@103
   183
gboolean pvr2_render_display_frame( uint32_t address )
nkeynes@103
   184
{
nkeynes@103
   185
    if( front_buffer.render_addr == address ) {
nkeynes@103
   186
	/* Current front buffer is already displayed, so do nothing
nkeynes@103
   187
	 * and tell the caller that all is well.
nkeynes@103
   188
	 */
nkeynes@103
   189
	return TRUE;
nkeynes@103
   190
    }
nkeynes@103
   191
    if( back_buffer.render_addr == address ) {
nkeynes@103
   192
	/* The more useful case - back buffer is to be displayed. Swap
nkeynes@103
   193
	 * the buffers 
nkeynes@103
   194
	 */
nkeynes@144
   195
	display_driver->display_back_buffer();
nkeynes@103
   196
	front_buffer = back_buffer;
nkeynes@103
   197
	back_buffer.render_addr = -1;
nkeynes@103
   198
	return TRUE;
nkeynes@103
   199
    }
nkeynes@103
   200
    return FALSE;
nkeynes@108
   201
}	
nkeynes@103
   202
nkeynes@103
   203
/**
nkeynes@103
   204
 * Prepare the OpenGL context to receive instructions for a new frame.
nkeynes@103
   205
 */
nkeynes@103
   206
static void pvr2_render_prepare_context( sh4addr_t render_addr, 
nkeynes@103
   207
					 uint32_t width, uint32_t height,
nkeynes@103
   208
					 uint32_t colour_format, 
nkeynes@128
   209
					 float bgplanez,
nkeynes@103
   210
					 gboolean texture_target )
nkeynes@103
   211
{
nkeynes@103
   212
    /* Select and initialize the render context */
nkeynes@144
   213
    display_driver->set_render_format( width, height, colour_format, texture_target );
nkeynes@103
   214
nkeynes@132
   215
    if( pvr2_render_font_list == -1 ) {
nkeynes@132
   216
	pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
nkeynes@132
   217
    }
nkeynes@132
   218
nkeynes@103
   219
    if( back_buffer.render_addr != -1 && 
nkeynes@103
   220
	back_buffer.render_addr != render_addr ) {
nkeynes@103
   221
	/* There's a current back buffer, and we're rendering somewhere else -
nkeynes@103
   222
	 * flush the back buffer back to vram and start a new back buffer
nkeynes@103
   223
	 */
nkeynes@103
   224
	pvr2_render_copy_to_sh4( &back_buffer, TRUE );
nkeynes@103
   225
    }
nkeynes@103
   226
nkeynes@103
   227
    if( front_buffer.render_addr == render_addr ) {
nkeynes@103
   228
	/* In case we've been asked to render to the current front buffer -
nkeynes@103
   229
	 * invalidate the front buffer and render to the back buffer, ensuring
nkeynes@103
   230
	 * we swap at the next frame display.
nkeynes@103
   231
	 */
nkeynes@103
   232
	front_buffer.render_addr = -1;
nkeynes@103
   233
    }
nkeynes@103
   234
    back_buffer.render_addr = render_addr;
nkeynes@103
   235
    back_buffer.width = width;
nkeynes@103
   236
    back_buffer.height = height;
nkeynes@103
   237
    back_buffer.colour_format = colour_format;
nkeynes@103
   238
nkeynes@103
   239
    /* Setup the display model */
nkeynes@103
   240
    glDrawBuffer(GL_BACK);
nkeynes@103
   241
    glShadeModel(GL_SMOOTH);
nkeynes@103
   242
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@103
   243
    glViewport( 0, 0, width, height );
nkeynes@103
   244
    glMatrixMode(GL_PROJECTION);
nkeynes@103
   245
    glLoadIdentity();
nkeynes@128
   246
    glOrtho( 0, width, height, 0, bgplanez, -1 );
nkeynes@103
   247
    glMatrixMode(GL_MODELVIEW);
nkeynes@103
   248
    glLoadIdentity();
nkeynes@108
   249
    glCullFace( GL_BACK );
nkeynes@103
   250
nkeynes@103
   251
    /* Clear out the buffers */
nkeynes@103
   252
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@128
   253
    glClearDepth(bgplanez);
nkeynes@103
   254
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
nkeynes@103
   255
}
nkeynes@103
   256
nkeynes@118
   257
static void pvr2_dump_display_list( uint32_t * display_list, uint32_t length )
nkeynes@118
   258
{
nkeynes@118
   259
    uint32_t i;
nkeynes@128
   260
    gboolean vertex = FALSE;
nkeynes@128
   261
    for( i =0; i<length>>2; i++ ) {
nkeynes@128
   262
	if( (i % 8) == 0 ) {
nkeynes@118
   263
	    if( i != 0 )
nkeynes@118
   264
		fprintf( stderr, "\n" );
nkeynes@132
   265
	    fprintf( stderr, "%08X:", i*4 );
nkeynes@128
   266
	    if( display_list[i] == 0xE0000000 ||
nkeynes@128
   267
		display_list[i] == 0xF0000000 ) 
nkeynes@128
   268
		vertex = TRUE;
nkeynes@128
   269
	    else vertex = FALSE;
nkeynes@118
   270
	}
nkeynes@128
   271
	if( vertex && (i%8) > 0 && (i%8) < 4 )
nkeynes@128
   272
	    fprintf( stderr, " %f", ((float *)display_list)[i] );
nkeynes@128
   273
	else
nkeynes@128
   274
	    fprintf( stderr, " %08X", display_list[i] );
nkeynes@118
   275
    }
nkeynes@128
   276
    fprintf( stderr, "\n" );
nkeynes@118
   277
}
nkeynes@118
   278
nkeynes@103
   279
static void pvr2_render_display_list( uint32_t *display_list, uint32_t length )
nkeynes@103
   280
{
nkeynes@103
   281
    uint32_t *cmd_ptr = display_list;
nkeynes@118
   282
    int strip_length = 0, vertex_count = 0;
nkeynes@118
   283
    int colour_type;
nkeynes@103
   284
    gboolean textured = FALSE;
nkeynes@132
   285
    gboolean shaded = FALSE;
nkeynes@103
   286
    struct pvr2_poly *poly;
nkeynes@132
   287
    if( pvr2_render_trace ) {
nkeynes@132
   288
	fprintf( stderr, "-------- %d\n", pvr2_get_frame_count() );
nkeynes@132
   289
	pvr2_dump_display_list( display_list, length );
nkeynes@132
   290
    }
nkeynes@132
   291
    while( cmd_ptr < display_list+(length>>2) ) {
nkeynes@118
   292
	unsigned int cmd = *cmd_ptr >> 24;
nkeynes@118
   293
	switch( cmd ) {
nkeynes@103
   294
	case PVR2_CMD_POLY_OPAQUE:
nkeynes@128
   295
	case PVR2_CMD_POLY_TRANS:
nkeynes@132
   296
	case PVR2_CMD_POLY_PUNCHOUT:
nkeynes@103
   297
	    poly = (struct pvr2_poly *)cmd_ptr;
nkeynes@103
   298
	    if( poly->command & PVR2_POLY_TEXTURED ) {
nkeynes@103
   299
		uint32_t addr = PVR2_TEX_ADDR(poly->texture);
nkeynes@103
   300
		int width = POLY_TEX_WIDTH(poly);
nkeynes@103
   301
		int height = POLY_TEX_HEIGHT(poly);
nkeynes@108
   302
		glEnable( GL_TEXTURE_2D );
nkeynes@103
   303
		texcache_get_texture( addr, width, height, poly->texture );
nkeynes@103
   304
		textured = TRUE;
nkeynes@108
   305
		glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY_TEX_BLEND(poly) );
nkeynes@103
   306
	    } else {
nkeynes@103
   307
		textured = FALSE;
nkeynes@103
   308
		glDisable( GL_TEXTURE_2D );
nkeynes@103
   309
	    }
nkeynes@103
   310
	    glBlendFunc( POLY_BLEND_SRC(poly), POLY_BLEND_DEST(poly) );
nkeynes@103
   311
	    if( poly->command & PVR2_POLY_SPECULAR ) {
nkeynes@103
   312
		/* Second block expected */
nkeynes@103
   313
	    }
nkeynes@103
   314
	    if( POLY_DEPTH_WRITE(poly) ) {
nkeynes@103
   315
		glEnable( GL_DEPTH_TEST );
nkeynes@103
   316
		glDepthFunc( POLY_DEPTH_MODE(poly) );
nkeynes@103
   317
	    } else {
nkeynes@103
   318
		glDisable( GL_DEPTH_TEST );
nkeynes@103
   319
	    }
nkeynes@103
   320
nkeynes@108
   321
	    switch( (poly->poly_cfg >> 27) & 0x03 ) {
nkeynes@108
   322
	    case 0:
nkeynes@108
   323
	    case 1:
nkeynes@108
   324
		glDisable( GL_CULL_FACE );
nkeynes@108
   325
		break;
nkeynes@108
   326
	    case 2:
nkeynes@108
   327
		glEnable( GL_CULL_FACE );
nkeynes@108
   328
		glFrontFace( GL_CW );
nkeynes@108
   329
		break;
nkeynes@108
   330
	    case 3:
nkeynes@108
   331
		glEnable( GL_CULL_FACE );
nkeynes@108
   332
		glFrontFace( GL_CCW );
nkeynes@108
   333
	    }
nkeynes@118
   334
	    strip_length = POLY_STRIP_VERTEXES( poly );
nkeynes@118
   335
	    colour_type = POLY_COLOUR_TYPE( poly );
nkeynes@118
   336
	    vertex_count = 0;
nkeynes@132
   337
	    if( poly->command & PVR2_POLY_SHADED ) {
nkeynes@132
   338
		shaded = TRUE;
nkeynes@132
   339
	    } else {
nkeynes@132
   340
		shaded = FALSE;
nkeynes@132
   341
	    }
nkeynes@132
   342
	    if( poly->poly_mode & PVR2_POLY_MODE_TEXALPHA ) {
nkeynes@132
   343
		glDisable( GL_BLEND );
nkeynes@132
   344
	    } else {
nkeynes@132
   345
		glEnable( GL_BLEND );
nkeynes@132
   346
	    }
nkeynes@132
   347
nkeynes@132
   348
	    break;
nkeynes@132
   349
	case PVR2_CMD_MOD_OPAQUE:
nkeynes@132
   350
	case PVR2_CMD_MOD_TRANS:
nkeynes@132
   351
	    /* TODO */
nkeynes@132
   352
	    break;
nkeynes@132
   353
	case PVR2_CMD_END_OF_LIST:
nkeynes@103
   354
	    break;
nkeynes@103
   355
	case PVR2_CMD_VERTEX_LAST:
nkeynes@103
   356
	case PVR2_CMD_VERTEX:
nkeynes@118
   357
	    if( vertex_count == 0 ) {
nkeynes@132
   358
		glBegin( GL_TRIANGLE_STRIP );
nkeynes@103
   359
	    }
nkeynes@118
   360
	    vertex_count++;
nkeynes@118
   361
nkeynes@118
   362
	    struct pvr2_vertex_packed *vertex = (struct pvr2_vertex_packed *)cmd_ptr;
nkeynes@103
   363
	    if( textured ) {
nkeynes@103
   364
		glTexCoord2f( vertex->s, vertex->t );
nkeynes@128
   365
nkeynes@132
   366
		if( shaded || vertex_count == 1) {
nkeynes@132
   367
		    switch( colour_type ) {
nkeynes@132
   368
		    case POLY_COLOUR_PACKED:
nkeynes@132
   369
			glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
nkeynes@132
   370
				    vertex->colour, vertex->colour >> 24 );
nkeynes@132
   371
			break;
nkeynes@132
   372
		    }
nkeynes@128
   373
		}
nkeynes@128
   374
	    } else {
nkeynes@132
   375
		if( shaded || vertex_count == 1 ) {
nkeynes@132
   376
		    switch( colour_type ) {
nkeynes@132
   377
		    case POLY_COLOUR_PACKED:
nkeynes@132
   378
			glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
nkeynes@132
   379
				    vertex->colour, vertex->colour >> 24 );
nkeynes@132
   380
			break;
nkeynes@132
   381
		    case POLY_COLOUR_FLOAT: 
nkeynes@132
   382
			{
nkeynes@132
   383
			    struct pvr2_vertex_float *v = (struct pvr2_vertex_float *)cmd_ptr;
nkeynes@132
   384
			    glColor4f( v->r, v->g, v->b, v->a );
nkeynes@132
   385
			}
nkeynes@132
   386
			break;
nkeynes@128
   387
		    }
nkeynes@128
   388
		}
nkeynes@103
   389
	    }
nkeynes@118
   390
nkeynes@103
   391
	    glVertex3f( vertex->x, vertex->y, vertex->z );
nkeynes@103
   392
	    
nkeynes@118
   393
	    if( cmd == PVR2_CMD_VERTEX_LAST ) {
nkeynes@103
   394
		glEnd();
nkeynes@118
   395
		vertex_count = 0;
nkeynes@118
   396
	    }
nkeynes@103
   397
	    break;
nkeynes@132
   398
	default:
nkeynes@132
   399
	    ERROR( "Unhandled command %08X in display list", *cmd_ptr );
nkeynes@132
   400
	    pvr2_dump_display_list( display_list, length );
nkeynes@132
   401
	    return;
nkeynes@103
   402
	}
nkeynes@103
   403
	cmd_ptr += 8; /* Next record */
nkeynes@103
   404
    }
nkeynes@103
   405
}
nkeynes@103
   406
nkeynes@128
   407
#define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
nkeynes@128
   408
#define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
nkeynes@128
   409
nkeynes@128
   410
/**
nkeynes@128
   411
 * Render the background plane as best we can. Unfortunately information
nkeynes@128
   412
 * is a little scant, to say the least.
nkeynes@128
   413
 */
nkeynes@128
   414
void pvr2_render_draw_backplane( uint32_t mode, uint32_t *poly )
nkeynes@128
   415
{
nkeynes@128
   416
    if( (mode >> 24) == 0x01 ) {
nkeynes@128
   417
	/* Packed colour. I think */
nkeynes@128
   418
	pvr2_bgplane_packed_t bg = (pvr2_bgplane_packed_t)poly;
nkeynes@128
   419
	if( bg->colour1 != bg->colour2 || bg->colour2 != bg->colour3 ) {
nkeynes@128
   420
	    WARN( "Multiple background colours specified. Confused" );
nkeynes@128
   421
	}
nkeynes@128
   422
	float x1 = MIN3( bg->x1, bg->x2, bg->x3 );
nkeynes@128
   423
	float y1 = MIN3( bg->y1, bg->y2, bg->y3 );
nkeynes@128
   424
	float x2 = MAX3( bg->x1, bg->x2, bg->x3 );
nkeynes@128
   425
	float y2 = MAX3( bg->y1, bg->y2, bg->y3 );
nkeynes@128
   426
	float z = MIN3( bg->z1, bg->z2, bg->z3 );
nkeynes@128
   427
	glDisable( GL_TEXTURE_2D );
nkeynes@128
   428
	glDisable( GL_DEPTH_TEST );
nkeynes@128
   429
	glColor3ub( (uint8_t)(bg->colour1 >> 16), (uint8_t)(bg->colour1 >> 8), 
nkeynes@128
   430
		    (uint8_t)bg->colour1 );
nkeynes@128
   431
	glBegin( GL_QUADS );
nkeynes@128
   432
	glVertex3f( x1, y1, z );
nkeynes@128
   433
	glVertex3f( x2, y1, z );
nkeynes@128
   434
	glVertex3f( x2, y2, z );
nkeynes@128
   435
	glVertex3f( x1, y2, z );
nkeynes@128
   436
	glEnd();
nkeynes@128
   437
    } else {
nkeynes@128
   438
	WARN( "Unknown bgplane mode: %08X", mode );
nkeynes@128
   439
	fwrite_dump( poly, 48, stderr );
nkeynes@128
   440
    }
nkeynes@128
   441
}
nkeynes@128
   442
nkeynes@103
   443
/**
nkeynes@103
   444
 * Render a complete scene into the OpenGL back buffer.
nkeynes@103
   445
 * Note: this will probably need to be broken up eventually once timings are
nkeynes@100
   446
 * determined.
nkeynes@100
   447
 */
nkeynes@103
   448
void pvr2_render_scene( )
nkeynes@100
   449
{
nkeynes@103
   450
    struct tile_descriptor *tile_desc =
nkeynes@103
   451
	(struct tile_descriptor *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, TILEBASE ));
nkeynes@100
   452
nkeynes@103
   453
    uint32_t render_addr = MMIO_READ( PVR2, RENDADDR1 );
nkeynes@103
   454
    gboolean render_to_tex;
nkeynes@103
   455
    if( render_addr & 0x01000000 ) {
nkeynes@103
   456
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
nkeynes@103
   457
	/* Heuristic - if we're rendering to the interlaced region we're
nkeynes@103
   458
	 * probably creating a texture rather than rendering actual output.
nkeynes@103
   459
	 * We can optimise for this case a little
nkeynes@103
   460
	 */
nkeynes@103
   461
	render_to_tex = TRUE;
nkeynes@118
   462
	WARN( "Render to texture not supported properly yet" );
nkeynes@103
   463
    } else {
nkeynes@103
   464
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
nkeynes@103
   465
	render_to_tex = FALSE;
nkeynes@103
   466
    }
nkeynes@128
   467
    
nkeynes@128
   468
    float bgplanez = MMIO_READF( PVR2, BGPLANEZ );
nkeynes@103
   469
    uint32_t render_mode = MMIO_READ( PVR2, RENDMODE );
nkeynes@103
   470
    int width = 640; /* FIXME - get this from the tile buffer */
nkeynes@103
   471
    int height = 480;
nkeynes@103
   472
    int colour_format = pvr2_render_colour_format[render_mode&0x07];
nkeynes@103
   473
    pvr2_render_prepare_context( render_addr, width, height, colour_format, 
nkeynes@128
   474
				 bgplanez, render_to_tex );
nkeynes@103
   475
nkeynes@103
   476
    uint32_t *display_list = 
nkeynes@103
   477
	(uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, OBJBASE ));
nkeynes@128
   478
nkeynes@103
   479
    uint32_t display_length = *display_list++;
nkeynes@103
   480
nkeynes@103
   481
    int clip_x = MMIO_READ( PVR2, HCLIP ) & 0x03FF;
nkeynes@103
   482
    int clip_y = MMIO_READ( PVR2, VCLIP ) & 0x03FF;
nkeynes@103
   483
    int clip_width = ((MMIO_READ( PVR2, HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
nkeynes@103
   484
    int clip_height= ((MMIO_READ( PVR2, VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
nkeynes@103
   485
nkeynes@103
   486
    if( clip_x == 0 && clip_y == 0 && clip_width == width && clip_height == height ) {
nkeynes@103
   487
	glDisable( GL_SCISSOR_TEST );
nkeynes@103
   488
    } else {
nkeynes@103
   489
	glEnable( GL_SCISSOR_TEST );
nkeynes@103
   490
	glScissor( clip_x, clip_y, clip_width, clip_height );
nkeynes@103
   491
    }
nkeynes@103
   492
nkeynes@103
   493
    /* Fog setup goes here */
nkeynes@103
   494
nkeynes@128
   495
    /* Render the background plane */
nkeynes@128
   496
    uint32_t bgplane_mode = MMIO_READ(PVR2, BGPLANE);
nkeynes@128
   497
    uint32_t *bgplane = display_list + (((bgplane_mode & 0x00FFFFFF)) >> 3) - 1;
nkeynes@128
   498
    pvr2_render_draw_backplane( bgplane_mode, bgplane );
nkeynes@128
   499
nkeynes@103
   500
    /* Render the display list */
nkeynes@103
   501
    pvr2_render_display_list( display_list, display_length );
nkeynes@103
   502
nkeynes@103
   503
    /* Post-render cleanup and update */
nkeynes@103
   504
nkeynes@108
   505
    /* Add frame, fps, etc data */
nkeynes@132
   506
    glPrintf( 4, 16, "Frame %d", pvr2_get_frame_count() );
nkeynes@103
   507
    
nkeynes@103
   508
    /* Generate end of render event */
nkeynes@100
   509
    asic_event( EVENT_PVR_RENDER_DONE );
nkeynes@132
   510
    DEBUG( "Rendered frame %d", pvr2_get_frame_count() );
nkeynes@100
   511
}
nkeynes@103
   512
nkeynes@103
   513
nkeynes@103
   514
/**
nkeynes@103
   515
 * Flush the indicated render buffer back to PVR. Caller is responsible for
nkeynes@103
   516
 * tracking whether there is actually anything in the buffer.
nkeynes@103
   517
 *
nkeynes@103
   518
 * @param buffer A render buffer indicating the address to store to, and the
nkeynes@103
   519
 * format the data needs to be in.
nkeynes@103
   520
 * @param backBuffer TRUE to flush the back buffer, FALSE for 
nkeynes@103
   521
 * the front buffer.
nkeynes@103
   522
 */
nkeynes@103
   523
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   524
			      gboolean backBuffer )
nkeynes@103
   525
{
nkeynes@103
   526
    if( buffer->render_addr == -1 )
nkeynes@103
   527
	return;
nkeynes@103
   528
    GLenum type, format = GL_RGBA;
nkeynes@103
   529
    int size = buffer->width * buffer->height;
nkeynes@103
   530
nkeynes@103
   531
    switch( buffer->colour_format ) {
nkeynes@103
   532
    case COLFMT_RGB565: 
nkeynes@103
   533
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   534
	format = GL_RGB; 
nkeynes@103
   535
	size <<= 1;
nkeynes@103
   536
	break;
nkeynes@103
   537
    case COLFMT_RGB888: 
nkeynes@103
   538
	type = GL_UNSIGNED_INT; 
nkeynes@103
   539
	format = GL_RGB;
nkeynes@103
   540
	size = (size<<1)+size;
nkeynes@103
   541
	break;
nkeynes@103
   542
    case COLFMT_ARGB1555: 
nkeynes@103
   543
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   544
	size <<= 1;
nkeynes@103
   545
	break;
nkeynes@103
   546
    case COLFMT_ARGB4444: 
nkeynes@103
   547
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   548
	size <<= 1;
nkeynes@103
   549
	break;
nkeynes@103
   550
    case COLFMT_ARGB8888: 
nkeynes@103
   551
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   552
	size <<= 2;
nkeynes@103
   553
	break;
nkeynes@103
   554
    }
nkeynes@103
   555
    
nkeynes@103
   556
    if( backBuffer ) {
nkeynes@103
   557
	glFinish();
nkeynes@103
   558
	glReadBuffer( GL_BACK );
nkeynes@103
   559
    } else {
nkeynes@103
   560
	glReadBuffer( GL_FRONT );
nkeynes@103
   561
    }
nkeynes@103
   562
nkeynes@103
   563
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   564
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   565
	char target[size];
nkeynes@103
   566
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   567
	pvr2_vram64_write( buffer->render_addr, target, size );
nkeynes@103
   568
    } else {
nkeynes@103
   569
	/* Regular buffer - go direct */
nkeynes@103
   570
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   571
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   572
    }
nkeynes@103
   573
}
nkeynes@103
   574
nkeynes@103
   575
nkeynes@103
   576
/**
nkeynes@103
   577
 * Copy data from PVR ram into the GL render buffer. 
nkeynes@103
   578
 *
nkeynes@103
   579
 * @param buffer A render buffer indicating the address to read from, and the
nkeynes@103
   580
 * format the data is in.
nkeynes@103
   581
 * @param backBuffer TRUE to write the back buffer, FALSE for 
nkeynes@103
   582
 * the front buffer.
nkeynes@103
   583
 */
nkeynes@103
   584
void pvr2_render_copy_from_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   585
				gboolean backBuffer )
nkeynes@103
   586
{
nkeynes@103
   587
    if( buffer->render_addr == -1 )
nkeynes@103
   588
	return;
nkeynes@103
   589
    GLenum type, format = GL_RGBA;
nkeynes@103
   590
    int size = buffer->width * buffer->height;
nkeynes@103
   591
nkeynes@103
   592
    switch( buffer->colour_format ) {
nkeynes@103
   593
    case COLFMT_RGB565: 
nkeynes@103
   594
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   595
	format = GL_RGB; 
nkeynes@103
   596
	size <<= 1;
nkeynes@103
   597
	break;
nkeynes@103
   598
    case COLFMT_RGB888: 
nkeynes@103
   599
	type = GL_UNSIGNED_INT; 
nkeynes@103
   600
	format = GL_RGB;
nkeynes@103
   601
	size = (size<<1)+size;
nkeynes@103
   602
	break;
nkeynes@103
   603
    case COLFMT_ARGB1555: 
nkeynes@103
   604
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   605
	size <<= 1;
nkeynes@103
   606
	break;
nkeynes@103
   607
    case COLFMT_ARGB4444: 
nkeynes@103
   608
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   609
	size <<= 1;
nkeynes@103
   610
	break;
nkeynes@103
   611
    case COLFMT_ARGB8888: 
nkeynes@103
   612
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   613
	size <<= 2;
nkeynes@103
   614
	break;
nkeynes@103
   615
    }
nkeynes@103
   616
    
nkeynes@103
   617
    if( backBuffer ) {
nkeynes@103
   618
	glDrawBuffer( GL_BACK );
nkeynes@103
   619
    } else {
nkeynes@103
   620
	glDrawBuffer( GL_FRONT );
nkeynes@103
   621
    }
nkeynes@103
   622
nkeynes@103
   623
    glRasterPos2i( 0, 0 );
nkeynes@103
   624
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   625
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   626
	char target[size];
nkeynes@103
   627
	pvr2_vram64_read( target, buffer->render_addr, size );
nkeynes@103
   628
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   629
		      format, type, target );
nkeynes@103
   630
    } else {
nkeynes@103
   631
	/* Regular buffer - go direct */
nkeynes@103
   632
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   633
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   634
		      format, type, target );
nkeynes@103
   635
    }
nkeynes@103
   636
}
.