Search
lxdream.org :: lxdream/src/pvr2/render.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/render.c
changeset 118:fcec54454d10
prev113:ce4eb7959d56
next128:f98ce97cacdd
author nkeynes
date Mon Mar 20 11:59:15 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Nominally implement packed-colour (w/ or w/o texture) mode
file annotate diff log raw
nkeynes@100
     1
/**
nkeynes@118
     2
 * $Id: render.c,v 1.5 2006-03-20 11:59:15 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@103
    50
#define POLY_DEPTH_WRITE(poly) (poly->poly_cfg&0x04000000)
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
extern uint32_t pvr2_frame_counter;
nkeynes@100
    59
nkeynes@100
    60
/**
nkeynes@103
    61
 * Describes a rendering buffer that's actually held in GL, for when we need
nkeynes@103
    62
 * to fetch the bits back to vram.
nkeynes@103
    63
 */
nkeynes@103
    64
typedef struct pvr2_render_buffer {
nkeynes@103
    65
    uint32_t render_addr; /* The actual address rendered to in pvr ram */
nkeynes@103
    66
    int width, height;
nkeynes@103
    67
    int colour_format;
nkeynes@103
    68
} *pvr2_render_buffer_t;
nkeynes@103
    69
nkeynes@103
    70
struct pvr2_render_buffer front_buffer;
nkeynes@103
    71
struct pvr2_render_buffer back_buffer;
nkeynes@103
    72
nkeynes@103
    73
struct tile_descriptor {
nkeynes@103
    74
    uint32_t header[6];
nkeynes@103
    75
    struct tile_pointers {
nkeynes@103
    76
	uint32_t tile_id;
nkeynes@103
    77
	uint32_t opaque_ptr;
nkeynes@103
    78
	uint32_t opaque_mod_ptr;
nkeynes@103
    79
	uint32_t trans_ptr;
nkeynes@103
    80
	uint32_t trans_mod_ptr;
nkeynes@103
    81
	uint32_t punchout_ptr;
nkeynes@103
    82
    } tile[0];
nkeynes@103
    83
};
nkeynes@103
    84
nkeynes@103
    85
/* Textured polygon */
nkeynes@103
    86
struct pvr2_poly {
nkeynes@103
    87
    uint32_t command;
nkeynes@103
    88
    uint32_t poly_cfg; /* Bitmask */
nkeynes@103
    89
    uint32_t poly_mode; /* texture/blending mask */
nkeynes@103
    90
    uint32_t texture; /* texture data */
nkeynes@103
    91
    float alpha;
nkeynes@103
    92
    float red;
nkeynes@103
    93
    float green;
nkeynes@103
    94
    float blue;
nkeynes@103
    95
};
nkeynes@103
    96
nkeynes@103
    97
struct pvr2_specular_highlight {
nkeynes@103
    98
    float base_alpha;
nkeynes@103
    99
    float base_red;
nkeynes@103
   100
    float base_green;
nkeynes@103
   101
    float base_blue;
nkeynes@103
   102
    float offset_alpha;
nkeynes@103
   103
    float offset_red;
nkeynes@103
   104
    float offset_green;
nkeynes@103
   105
    float offset_blue;
nkeynes@103
   106
};
nkeynes@103
   107
				     
nkeynes@103
   108
nkeynes@118
   109
struct pvr2_vertex_packed {
nkeynes@103
   110
    uint32_t command;
nkeynes@103
   111
    float x, y, z;
nkeynes@103
   112
    float s,t;
nkeynes@118
   113
    uint32_t colour;
nkeynes@103
   114
    float f;
nkeynes@103
   115
};
nkeynes@103
   116
nkeynes@103
   117
nkeynes@103
   118
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   119
			      gboolean backBuffer );
nkeynes@103
   120
nkeynes@108
   121
int pvr2_render_font_list = -1;
nkeynes@108
   122
nkeynes@108
   123
int glPrintf( const char *fmt, ... )
nkeynes@108
   124
{
nkeynes@108
   125
    va_list ap;     /* our argument pointer */
nkeynes@108
   126
    char buf[256];
nkeynes@108
   127
    int len;
nkeynes@108
   128
    if (fmt == NULL)    /* if there is no string to draw do nothing */
nkeynes@108
   129
        return;
nkeynes@108
   130
    va_start(ap, fmt); 
nkeynes@108
   131
    len = vsnprintf(buf, sizeof(buf), fmt, ap);
nkeynes@108
   132
    va_end(ap);
nkeynes@108
   133
nkeynes@108
   134
    if( pvr2_render_font_list == -1 ) {
nkeynes@108
   135
	pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
nkeynes@108
   136
    }
nkeynes@108
   137
nkeynes@108
   138
    glPushAttrib(GL_LIST_BIT);
nkeynes@108
   139
    glListBase(pvr2_render_font_list - 32);
nkeynes@108
   140
    glCallLists(len, GL_UNSIGNED_BYTE, buf);
nkeynes@108
   141
    glPopAttrib();
nkeynes@108
   142
nkeynes@108
   143
    return len;
nkeynes@108
   144
}
nkeynes@108
   145
nkeynes@103
   146
nkeynes@103
   147
gboolean pvr2_render_init( void )
nkeynes@103
   148
{
nkeynes@103
   149
    front_buffer.render_addr = -1;
nkeynes@103
   150
    back_buffer.render_addr = -1;
nkeynes@103
   151
}
nkeynes@103
   152
nkeynes@103
   153
/**
nkeynes@103
   154
 * Display a rendered frame if one is available.
nkeynes@103
   155
 * @param address An address in PVR ram (0500000 range).
nkeynes@103
   156
 * @return TRUE if a frame was available to be displayed, otherwise false.
nkeynes@103
   157
 */
nkeynes@103
   158
gboolean pvr2_render_display_frame( uint32_t address )
nkeynes@103
   159
{
nkeynes@103
   160
    if( front_buffer.render_addr == address ) {
nkeynes@103
   161
	/* Current front buffer is already displayed, so do nothing
nkeynes@103
   162
	 * and tell the caller that all is well.
nkeynes@103
   163
	 */
nkeynes@103
   164
	return TRUE;
nkeynes@103
   165
    }
nkeynes@103
   166
    if( back_buffer.render_addr == address ) {
nkeynes@103
   167
	/* The more useful case - back buffer is to be displayed. Swap
nkeynes@103
   168
	 * the buffers 
nkeynes@103
   169
	 */
nkeynes@103
   170
	video_driver->display_back_buffer();
nkeynes@103
   171
	front_buffer = back_buffer;
nkeynes@103
   172
	back_buffer.render_addr = -1;
nkeynes@103
   173
	return TRUE;
nkeynes@103
   174
    }
nkeynes@103
   175
    return FALSE;
nkeynes@108
   176
}	
nkeynes@103
   177
nkeynes@103
   178
/**
nkeynes@103
   179
 * Prepare the OpenGL context to receive instructions for a new frame.
nkeynes@103
   180
 */
nkeynes@103
   181
static void pvr2_render_prepare_context( sh4addr_t render_addr, 
nkeynes@103
   182
					 uint32_t width, uint32_t height,
nkeynes@103
   183
					 uint32_t colour_format, 
nkeynes@103
   184
					 gboolean texture_target )
nkeynes@103
   185
{
nkeynes@103
   186
    /* Select and initialize the render context */
nkeynes@103
   187
    video_driver->set_render_format( width, height, colour_format, texture_target );
nkeynes@103
   188
nkeynes@103
   189
    if( back_buffer.render_addr != -1 && 
nkeynes@103
   190
	back_buffer.render_addr != render_addr ) {
nkeynes@103
   191
	/* There's a current back buffer, and we're rendering somewhere else -
nkeynes@103
   192
	 * flush the back buffer back to vram and start a new back buffer
nkeynes@103
   193
	 */
nkeynes@103
   194
	pvr2_render_copy_to_sh4( &back_buffer, TRUE );
nkeynes@103
   195
    }
nkeynes@103
   196
nkeynes@103
   197
    if( front_buffer.render_addr == render_addr ) {
nkeynes@103
   198
	/* In case we've been asked to render to the current front buffer -
nkeynes@103
   199
	 * invalidate the front buffer and render to the back buffer, ensuring
nkeynes@103
   200
	 * we swap at the next frame display.
nkeynes@103
   201
	 */
nkeynes@103
   202
	front_buffer.render_addr = -1;
nkeynes@103
   203
    }
nkeynes@103
   204
    back_buffer.render_addr = render_addr;
nkeynes@103
   205
    back_buffer.width = width;
nkeynes@103
   206
    back_buffer.height = height;
nkeynes@103
   207
    back_buffer.colour_format = colour_format;
nkeynes@103
   208
nkeynes@103
   209
    /* Setup the display model */
nkeynes@103
   210
    glDrawBuffer(GL_BACK);
nkeynes@103
   211
    glShadeModel(GL_SMOOTH);
nkeynes@103
   212
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@103
   213
    glViewport( 0, 0, width, height );
nkeynes@103
   214
    glMatrixMode(GL_PROJECTION);
nkeynes@103
   215
    glLoadIdentity();
nkeynes@108
   216
    glOrtho( 0, width, height, 0, 0, -65535 );
nkeynes@103
   217
    glMatrixMode(GL_MODELVIEW);
nkeynes@103
   218
    glLoadIdentity();
nkeynes@108
   219
    glCullFace( GL_BACK );
nkeynes@103
   220
nkeynes@103
   221
    /* Clear out the buffers */
nkeynes@103
   222
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@103
   223
    glClearDepth(1.0f);
nkeynes@103
   224
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
nkeynes@103
   225
}
nkeynes@103
   226
nkeynes@118
   227
static void pvr2_dump_display_list( uint32_t * display_list, uint32_t length )
nkeynes@118
   228
{
nkeynes@118
   229
    uint32_t i;
nkeynes@118
   230
    for( i =0; i<length; i+=4 ) {
nkeynes@118
   231
	if( (i % 32) == 0 ) {
nkeynes@118
   232
	    if( i != 0 )
nkeynes@118
   233
		fprintf( stderr, "\n" );
nkeynes@118
   234
	    fprintf( stderr, "%08X:", i );
nkeynes@118
   235
	}
nkeynes@118
   236
	fprintf( stderr, " %08X", display_list[i] );
nkeynes@118
   237
    }
nkeynes@118
   238
}
nkeynes@118
   239
nkeynes@103
   240
static void pvr2_render_display_list( uint32_t *display_list, uint32_t length )
nkeynes@103
   241
{
nkeynes@103
   242
    uint32_t *cmd_ptr = display_list;
nkeynes@118
   243
    int strip_length = 0, vertex_count = 0;
nkeynes@118
   244
    int colour_type;
nkeynes@103
   245
    gboolean textured = FALSE;
nkeynes@103
   246
    struct pvr2_poly *poly;
nkeynes@103
   247
    while( cmd_ptr < display_list+length ) {
nkeynes@118
   248
	unsigned int cmd = *cmd_ptr >> 24;
nkeynes@118
   249
	switch( cmd ) {
nkeynes@103
   250
	case PVR2_CMD_POLY_OPAQUE:
nkeynes@103
   251
	    poly = (struct pvr2_poly *)cmd_ptr;
nkeynes@103
   252
	    if( poly->command & PVR2_POLY_TEXTURED ) {
nkeynes@103
   253
		uint32_t addr = PVR2_TEX_ADDR(poly->texture);
nkeynes@103
   254
		int width = POLY_TEX_WIDTH(poly);
nkeynes@103
   255
		int height = POLY_TEX_HEIGHT(poly);
nkeynes@108
   256
		glEnable( GL_TEXTURE_2D );
nkeynes@103
   257
		texcache_get_texture( addr, width, height, poly->texture );
nkeynes@103
   258
		textured = TRUE;
nkeynes@108
   259
		glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY_TEX_BLEND(poly) );
nkeynes@103
   260
	    } else {
nkeynes@103
   261
		textured = FALSE;
nkeynes@103
   262
		glDisable( GL_TEXTURE_2D );
nkeynes@103
   263
	    }
nkeynes@103
   264
	    glBlendFunc( POLY_BLEND_SRC(poly), POLY_BLEND_DEST(poly) );
nkeynes@103
   265
	    if( poly->command & PVR2_POLY_SPECULAR ) {
nkeynes@103
   266
		/* Second block expected */
nkeynes@103
   267
	    }
nkeynes@103
   268
	    if( POLY_DEPTH_WRITE(poly) ) {
nkeynes@103
   269
		glEnable( GL_DEPTH_TEST );
nkeynes@103
   270
		glDepthFunc( POLY_DEPTH_MODE(poly) );
nkeynes@103
   271
	    } else {
nkeynes@103
   272
		glDisable( GL_DEPTH_TEST );
nkeynes@103
   273
	    }
nkeynes@103
   274
nkeynes@108
   275
	    switch( (poly->poly_cfg >> 27) & 0x03 ) {
nkeynes@108
   276
	    case 0:
nkeynes@108
   277
	    case 1:
nkeynes@108
   278
		glDisable( GL_CULL_FACE );
nkeynes@108
   279
		break;
nkeynes@108
   280
	    case 2:
nkeynes@108
   281
		glEnable( GL_CULL_FACE );
nkeynes@108
   282
		glFrontFace( GL_CW );
nkeynes@108
   283
		break;
nkeynes@108
   284
	    case 3:
nkeynes@108
   285
		glEnable( GL_CULL_FACE );
nkeynes@108
   286
		glFrontFace( GL_CCW );
nkeynes@108
   287
	    }
nkeynes@118
   288
	    strip_length = POLY_STRIP_VERTEXES( poly );
nkeynes@118
   289
	    colour_type = POLY_COLOUR_TYPE( poly );
nkeynes@118
   290
	    vertex_count = 0;
nkeynes@103
   291
	    break;
nkeynes@103
   292
	case PVR2_CMD_VERTEX_LAST:
nkeynes@103
   293
	case PVR2_CMD_VERTEX:
nkeynes@118
   294
	    if( vertex_count == 0 ) {
nkeynes@118
   295
		if( strip_length == 3 )
nkeynes@118
   296
		    glBegin( GL_TRIANGLES );
nkeynes@118
   297
		else 
nkeynes@118
   298
		    glBegin( GL_TRIANGLE_STRIP );
nkeynes@103
   299
	    }
nkeynes@118
   300
	    vertex_count++;
nkeynes@118
   301
nkeynes@118
   302
	    struct pvr2_vertex_packed *vertex = (struct pvr2_vertex_packed *)cmd_ptr;
nkeynes@103
   303
	    if( textured ) {
nkeynes@103
   304
		glTexCoord2f( vertex->s, vertex->t );
nkeynes@103
   305
	    }
nkeynes@118
   306
nkeynes@118
   307
	    switch( colour_type ) {
nkeynes@118
   308
	    case POLY_COLOUR_PACKED:
nkeynes@118
   309
		glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
nkeynes@118
   310
			    vertex->colour, vertex->colour >> 24 );
nkeynes@118
   311
		break;
nkeynes@118
   312
	    }
nkeynes@103
   313
	    glVertex3f( vertex->x, vertex->y, vertex->z );
nkeynes@103
   314
	    
nkeynes@118
   315
	    if( cmd == PVR2_CMD_VERTEX_LAST ) {
nkeynes@103
   316
		glEnd();
nkeynes@118
   317
		vertex_count = 0;
nkeynes@118
   318
	    }
nkeynes@118
   319
nkeynes@118
   320
	    if( vertex_count >= strip_length ) {
nkeynes@118
   321
		ERROR( "Rendering long strip (expected end after %d)", strip_length );
nkeynes@118
   322
		pvr2_dump_display_list( display_list, length );
nkeynes@118
   323
		return;
nkeynes@108
   324
	    }
nkeynes@103
   325
	    break;
nkeynes@103
   326
	}
nkeynes@103
   327
	cmd_ptr += 8; /* Next record */
nkeynes@103
   328
    }
nkeynes@103
   329
}
nkeynes@103
   330
nkeynes@103
   331
/**
nkeynes@103
   332
 * Render a complete scene into the OpenGL back buffer.
nkeynes@103
   333
 * Note: this will probably need to be broken up eventually once timings are
nkeynes@100
   334
 * determined.
nkeynes@100
   335
 */
nkeynes@103
   336
void pvr2_render_scene( )
nkeynes@100
   337
{
nkeynes@103
   338
    struct tile_descriptor *tile_desc =
nkeynes@103
   339
	(struct tile_descriptor *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, TILEBASE ));
nkeynes@100
   340
nkeynes@103
   341
    uint32_t render_addr = MMIO_READ( PVR2, RENDADDR1 );
nkeynes@103
   342
    gboolean render_to_tex;
nkeynes@103
   343
    if( render_addr & 0x01000000 ) {
nkeynes@103
   344
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
nkeynes@103
   345
	/* Heuristic - if we're rendering to the interlaced region we're
nkeynes@103
   346
	 * probably creating a texture rather than rendering actual output.
nkeynes@103
   347
	 * We can optimise for this case a little
nkeynes@103
   348
	 */
nkeynes@103
   349
	render_to_tex = TRUE;
nkeynes@118
   350
	WARN( "Render to texture not supported properly yet" );
nkeynes@103
   351
    } else {
nkeynes@103
   352
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
nkeynes@103
   353
	render_to_tex = FALSE;
nkeynes@103
   354
    }
nkeynes@103
   355
    uint32_t render_mode = MMIO_READ( PVR2, RENDMODE );
nkeynes@103
   356
    int width = 640; /* FIXME - get this from the tile buffer */
nkeynes@103
   357
    int height = 480;
nkeynes@103
   358
    int colour_format = pvr2_render_colour_format[render_mode&0x07];
nkeynes@103
   359
    pvr2_render_prepare_context( render_addr, width, height, colour_format, 
nkeynes@103
   360
				 render_to_tex );
nkeynes@103
   361
nkeynes@103
   362
    uint32_t *display_list = 
nkeynes@103
   363
	(uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, OBJBASE ));
nkeynes@103
   364
    uint32_t display_length = *display_list++;
nkeynes@103
   365
nkeynes@103
   366
    int clip_x = MMIO_READ( PVR2, HCLIP ) & 0x03FF;
nkeynes@103
   367
    int clip_y = MMIO_READ( PVR2, VCLIP ) & 0x03FF;
nkeynes@103
   368
    int clip_width = ((MMIO_READ( PVR2, HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
nkeynes@103
   369
    int clip_height= ((MMIO_READ( PVR2, VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
nkeynes@103
   370
nkeynes@103
   371
    if( clip_x == 0 && clip_y == 0 && clip_width == width && clip_height == height ) {
nkeynes@103
   372
	glDisable( GL_SCISSOR_TEST );
nkeynes@103
   373
    } else {
nkeynes@103
   374
	glEnable( GL_SCISSOR_TEST );
nkeynes@103
   375
	glScissor( clip_x, clip_y, clip_width, clip_height );
nkeynes@103
   376
    }
nkeynes@103
   377
nkeynes@103
   378
    /* Fog setup goes here */
nkeynes@103
   379
nkeynes@103
   380
    /* Render the display list */
nkeynes@103
   381
    pvr2_render_display_list( display_list, display_length );
nkeynes@103
   382
nkeynes@103
   383
    /* Post-render cleanup and update */
nkeynes@103
   384
nkeynes@108
   385
    /* Add frame, fps, etc data */
nkeynes@108
   386
    glRasterPos2i( 4, 16 );
nkeynes@108
   387
    //    glColor3f( 0.0f, 0.0f, 1.0f );
nkeynes@108
   388
    glPrintf( "Frame %d", pvr2_frame_counter );
nkeynes@103
   389
    
nkeynes@103
   390
    /* Generate end of render event */
nkeynes@100
   391
    asic_event( EVENT_PVR_RENDER_DONE );
nkeynes@100
   392
    DEBUG( "Rendered frame %d", pvr2_frame_counter );
nkeynes@100
   393
}
nkeynes@103
   394
nkeynes@103
   395
nkeynes@103
   396
/**
nkeynes@103
   397
 * Flush the indicated render buffer back to PVR. Caller is responsible for
nkeynes@103
   398
 * tracking whether there is actually anything in the buffer.
nkeynes@103
   399
 *
nkeynes@103
   400
 * @param buffer A render buffer indicating the address to store to, and the
nkeynes@103
   401
 * format the data needs to be in.
nkeynes@103
   402
 * @param backBuffer TRUE to flush the back buffer, FALSE for 
nkeynes@103
   403
 * the front buffer.
nkeynes@103
   404
 */
nkeynes@103
   405
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   406
			      gboolean backBuffer )
nkeynes@103
   407
{
nkeynes@103
   408
    if( buffer->render_addr == -1 )
nkeynes@103
   409
	return;
nkeynes@103
   410
    GLenum type, format = GL_RGBA;
nkeynes@103
   411
    int size = buffer->width * buffer->height;
nkeynes@103
   412
nkeynes@103
   413
    switch( buffer->colour_format ) {
nkeynes@103
   414
    case COLFMT_RGB565: 
nkeynes@103
   415
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   416
	format = GL_RGB; 
nkeynes@103
   417
	size <<= 1;
nkeynes@103
   418
	break;
nkeynes@103
   419
    case COLFMT_RGB888: 
nkeynes@103
   420
	type = GL_UNSIGNED_INT; 
nkeynes@103
   421
	format = GL_RGB;
nkeynes@103
   422
	size = (size<<1)+size;
nkeynes@103
   423
	break;
nkeynes@103
   424
    case COLFMT_ARGB1555: 
nkeynes@103
   425
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   426
	size <<= 1;
nkeynes@103
   427
	break;
nkeynes@103
   428
    case COLFMT_ARGB4444: 
nkeynes@103
   429
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   430
	size <<= 1;
nkeynes@103
   431
	break;
nkeynes@103
   432
    case COLFMT_ARGB8888: 
nkeynes@103
   433
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   434
	size <<= 2;
nkeynes@103
   435
	break;
nkeynes@103
   436
    }
nkeynes@103
   437
    
nkeynes@103
   438
    if( backBuffer ) {
nkeynes@103
   439
	glFinish();
nkeynes@103
   440
	glReadBuffer( GL_BACK );
nkeynes@103
   441
    } else {
nkeynes@103
   442
	glReadBuffer( GL_FRONT );
nkeynes@103
   443
    }
nkeynes@103
   444
nkeynes@103
   445
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   446
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   447
	char target[size];
nkeynes@103
   448
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   449
	pvr2_vram64_write( buffer->render_addr, target, size );
nkeynes@103
   450
    } else {
nkeynes@103
   451
	/* Regular buffer - go direct */
nkeynes@103
   452
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   453
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   454
    }
nkeynes@103
   455
}
nkeynes@103
   456
nkeynes@103
   457
nkeynes@103
   458
/**
nkeynes@103
   459
 * Copy data from PVR ram into the GL render buffer. 
nkeynes@103
   460
 *
nkeynes@103
   461
 * @param buffer A render buffer indicating the address to read from, and the
nkeynes@103
   462
 * format the data is in.
nkeynes@103
   463
 * @param backBuffer TRUE to write the back buffer, FALSE for 
nkeynes@103
   464
 * the front buffer.
nkeynes@103
   465
 */
nkeynes@103
   466
void pvr2_render_copy_from_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   467
				gboolean backBuffer )
nkeynes@103
   468
{
nkeynes@103
   469
    if( buffer->render_addr == -1 )
nkeynes@103
   470
	return;
nkeynes@103
   471
    GLenum type, format = GL_RGBA;
nkeynes@103
   472
    int size = buffer->width * buffer->height;
nkeynes@103
   473
nkeynes@103
   474
    switch( buffer->colour_format ) {
nkeynes@103
   475
    case COLFMT_RGB565: 
nkeynes@103
   476
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   477
	format = GL_RGB; 
nkeynes@103
   478
	size <<= 1;
nkeynes@103
   479
	break;
nkeynes@103
   480
    case COLFMT_RGB888: 
nkeynes@103
   481
	type = GL_UNSIGNED_INT; 
nkeynes@103
   482
	format = GL_RGB;
nkeynes@103
   483
	size = (size<<1)+size;
nkeynes@103
   484
	break;
nkeynes@103
   485
    case COLFMT_ARGB1555: 
nkeynes@103
   486
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   487
	size <<= 1;
nkeynes@103
   488
	break;
nkeynes@103
   489
    case COLFMT_ARGB4444: 
nkeynes@103
   490
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   491
	size <<= 1;
nkeynes@103
   492
	break;
nkeynes@103
   493
    case COLFMT_ARGB8888: 
nkeynes@103
   494
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   495
	size <<= 2;
nkeynes@103
   496
	break;
nkeynes@103
   497
    }
nkeynes@103
   498
    
nkeynes@103
   499
    if( backBuffer ) {
nkeynes@103
   500
	glDrawBuffer( GL_BACK );
nkeynes@103
   501
    } else {
nkeynes@103
   502
	glDrawBuffer( GL_FRONT );
nkeynes@103
   503
    }
nkeynes@103
   504
nkeynes@103
   505
    glRasterPos2i( 0, 0 );
nkeynes@103
   506
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   507
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   508
	char target[size];
nkeynes@103
   509
	pvr2_vram64_read( target, buffer->render_addr, size );
nkeynes@103
   510
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   511
		      format, type, target );
nkeynes@103
   512
    } else {
nkeynes@103
   513
	/* Regular buffer - go direct */
nkeynes@103
   514
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   515
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   516
		      format, type, target );
nkeynes@103
   517
    }
nkeynes@103
   518
}
.