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