Search
lxdream.org :: lxdream/src/pvr2/render.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/render.c
changeset 128:f98ce97cacdd
prev118:fcec54454d10
next132:48a8cc541742
author nkeynes
date Thu Mar 23 13:19:55 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Add initial backplane support
Turn GL_BLEND on for translucent polys
file annotate diff log raw
nkeynes@100
     1
/**
nkeynes@128
     2
 * $Id: render.c,v 1.6 2006-03-23 13:19:55 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
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@128
   117
struct pvr2_vertex_float {
nkeynes@128
   118
    uint32_t command;
nkeynes@128
   119
    float x,y,z;
nkeynes@128
   120
    float a, r, g, b;
nkeynes@128
   121
};
nkeynes@128
   122
nkeynes@128
   123
union pvr2_vertex {
nkeynes@128
   124
    struct pvr2_vertex_packed pack;
nkeynes@128
   125
    struct pvr2_vertex_float flt;
nkeynes@128
   126
};
nkeynes@128
   127
nkeynes@128
   128
typedef struct pvr2_bgplane_packed {
nkeynes@128
   129
        uint32_t        poly_cfg, poly_mode;
nkeynes@128
   130
        uint32_t        texture_mode;
nkeynes@128
   131
        float           x1, y1, z1;
nkeynes@128
   132
        uint32_t          colour1;
nkeynes@128
   133
        float           x2, y2, z2;
nkeynes@128
   134
        uint32_t          colour2;
nkeynes@128
   135
        float           x3, y3, z3;
nkeynes@128
   136
        uint32_t          colour3;
nkeynes@128
   137
} *pvr2_bgplane_packed_t;
nkeynes@128
   138
nkeynes@128
   139
nkeynes@103
   140
nkeynes@103
   141
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   142
			      gboolean backBuffer );
nkeynes@103
   143
nkeynes@108
   144
int pvr2_render_font_list = -1;
nkeynes@108
   145
nkeynes@108
   146
int glPrintf( const char *fmt, ... )
nkeynes@108
   147
{
nkeynes@108
   148
    va_list ap;     /* our argument pointer */
nkeynes@108
   149
    char buf[256];
nkeynes@108
   150
    int len;
nkeynes@108
   151
    if (fmt == NULL)    /* if there is no string to draw do nothing */
nkeynes@108
   152
        return;
nkeynes@108
   153
    va_start(ap, fmt); 
nkeynes@108
   154
    len = vsnprintf(buf, sizeof(buf), fmt, ap);
nkeynes@108
   155
    va_end(ap);
nkeynes@108
   156
nkeynes@108
   157
    if( pvr2_render_font_list == -1 ) {
nkeynes@128
   158
	glColor3f( 1.0, 1.0, 1.0 );
nkeynes@108
   159
	pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
nkeynes@108
   160
    }
nkeynes@108
   161
nkeynes@108
   162
    glPushAttrib(GL_LIST_BIT);
nkeynes@108
   163
    glListBase(pvr2_render_font_list - 32);
nkeynes@108
   164
    glCallLists(len, GL_UNSIGNED_BYTE, buf);
nkeynes@108
   165
    glPopAttrib();
nkeynes@108
   166
nkeynes@108
   167
    return len;
nkeynes@108
   168
}
nkeynes@108
   169
nkeynes@103
   170
nkeynes@103
   171
gboolean pvr2_render_init( void )
nkeynes@103
   172
{
nkeynes@103
   173
    front_buffer.render_addr = -1;
nkeynes@103
   174
    back_buffer.render_addr = -1;
nkeynes@103
   175
}
nkeynes@103
   176
nkeynes@103
   177
/**
nkeynes@103
   178
 * Display a rendered frame if one is available.
nkeynes@103
   179
 * @param address An address in PVR ram (0500000 range).
nkeynes@103
   180
 * @return TRUE if a frame was available to be displayed, otherwise false.
nkeynes@103
   181
 */
nkeynes@103
   182
gboolean pvr2_render_display_frame( uint32_t address )
nkeynes@103
   183
{
nkeynes@103
   184
    if( front_buffer.render_addr == address ) {
nkeynes@103
   185
	/* Current front buffer is already displayed, so do nothing
nkeynes@103
   186
	 * and tell the caller that all is well.
nkeynes@103
   187
	 */
nkeynes@103
   188
	return TRUE;
nkeynes@103
   189
    }
nkeynes@103
   190
    if( back_buffer.render_addr == address ) {
nkeynes@103
   191
	/* The more useful case - back buffer is to be displayed. Swap
nkeynes@103
   192
	 * the buffers 
nkeynes@103
   193
	 */
nkeynes@103
   194
	video_driver->display_back_buffer();
nkeynes@103
   195
	front_buffer = back_buffer;
nkeynes@103
   196
	back_buffer.render_addr = -1;
nkeynes@103
   197
	return TRUE;
nkeynes@103
   198
    }
nkeynes@103
   199
    return FALSE;
nkeynes@108
   200
}	
nkeynes@103
   201
nkeynes@103
   202
/**
nkeynes@103
   203
 * Prepare the OpenGL context to receive instructions for a new frame.
nkeynes@103
   204
 */
nkeynes@103
   205
static void pvr2_render_prepare_context( sh4addr_t render_addr, 
nkeynes@103
   206
					 uint32_t width, uint32_t height,
nkeynes@103
   207
					 uint32_t colour_format, 
nkeynes@128
   208
					 float bgplanez,
nkeynes@103
   209
					 gboolean texture_target )
nkeynes@103
   210
{
nkeynes@103
   211
    /* Select and initialize the render context */
nkeynes@103
   212
    video_driver->set_render_format( width, height, colour_format, texture_target );
nkeynes@103
   213
nkeynes@103
   214
    if( back_buffer.render_addr != -1 && 
nkeynes@103
   215
	back_buffer.render_addr != render_addr ) {
nkeynes@103
   216
	/* There's a current back buffer, and we're rendering somewhere else -
nkeynes@103
   217
	 * flush the back buffer back to vram and start a new back buffer
nkeynes@103
   218
	 */
nkeynes@103
   219
	pvr2_render_copy_to_sh4( &back_buffer, TRUE );
nkeynes@103
   220
    }
nkeynes@103
   221
nkeynes@103
   222
    if( front_buffer.render_addr == render_addr ) {
nkeynes@103
   223
	/* In case we've been asked to render to the current front buffer -
nkeynes@103
   224
	 * invalidate the front buffer and render to the back buffer, ensuring
nkeynes@103
   225
	 * we swap at the next frame display.
nkeynes@103
   226
	 */
nkeynes@103
   227
	front_buffer.render_addr = -1;
nkeynes@103
   228
    }
nkeynes@103
   229
    back_buffer.render_addr = render_addr;
nkeynes@103
   230
    back_buffer.width = width;
nkeynes@103
   231
    back_buffer.height = height;
nkeynes@103
   232
    back_buffer.colour_format = colour_format;
nkeynes@103
   233
nkeynes@103
   234
    /* Setup the display model */
nkeynes@103
   235
    glDrawBuffer(GL_BACK);
nkeynes@103
   236
    glShadeModel(GL_SMOOTH);
nkeynes@103
   237
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@103
   238
    glViewport( 0, 0, width, height );
nkeynes@103
   239
    glMatrixMode(GL_PROJECTION);
nkeynes@103
   240
    glLoadIdentity();
nkeynes@128
   241
    glOrtho( 0, width, height, 0, bgplanez, -1 );
nkeynes@103
   242
    glMatrixMode(GL_MODELVIEW);
nkeynes@103
   243
    glLoadIdentity();
nkeynes@108
   244
    glCullFace( GL_BACK );
nkeynes@103
   245
nkeynes@103
   246
    /* Clear out the buffers */
nkeynes@103
   247
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@128
   248
    glClearDepth(bgplanez);
nkeynes@103
   249
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
nkeynes@103
   250
}
nkeynes@103
   251
nkeynes@118
   252
static void pvr2_dump_display_list( uint32_t * display_list, uint32_t length )
nkeynes@118
   253
{
nkeynes@118
   254
    uint32_t i;
nkeynes@128
   255
    gboolean vertex = FALSE;
nkeynes@128
   256
    for( i =0; i<length>>2; i++ ) {
nkeynes@128
   257
	if( (i % 8) == 0 ) {
nkeynes@118
   258
	    if( i != 0 )
nkeynes@118
   259
		fprintf( stderr, "\n" );
nkeynes@128
   260
	    fprintf( stderr, "%08X:", i*32 );
nkeynes@128
   261
	    if( display_list[i] == 0xE0000000 ||
nkeynes@128
   262
		display_list[i] == 0xF0000000 ) 
nkeynes@128
   263
		vertex = TRUE;
nkeynes@128
   264
	    else vertex = FALSE;
nkeynes@118
   265
	}
nkeynes@128
   266
	if( vertex && (i%8) > 0 && (i%8) < 4 )
nkeynes@128
   267
	    fprintf( stderr, " %f", ((float *)display_list)[i] );
nkeynes@128
   268
	else
nkeynes@128
   269
	    fprintf( stderr, " %08X", display_list[i] );
nkeynes@118
   270
    }
nkeynes@128
   271
    fprintf( stderr, "\n" );
nkeynes@118
   272
}
nkeynes@118
   273
nkeynes@103
   274
static void pvr2_render_display_list( uint32_t *display_list, uint32_t length )
nkeynes@103
   275
{
nkeynes@103
   276
    uint32_t *cmd_ptr = display_list;
nkeynes@118
   277
    int strip_length = 0, vertex_count = 0;
nkeynes@118
   278
    int colour_type;
nkeynes@103
   279
    gboolean textured = FALSE;
nkeynes@103
   280
    struct pvr2_poly *poly;
nkeynes@128
   281
    fprintf( stderr, "-------- %d\n", pvr2_frame_counter );
nkeynes@128
   282
    pvr2_dump_display_list( display_list, length );
nkeynes@103
   283
    while( cmd_ptr < display_list+length ) {
nkeynes@118
   284
	unsigned int cmd = *cmd_ptr >> 24;
nkeynes@118
   285
	switch( cmd ) {
nkeynes@103
   286
	case PVR2_CMD_POLY_OPAQUE:
nkeynes@128
   287
	case PVR2_CMD_POLY_TRANS:
nkeynes@128
   288
	    if( cmd == PVR2_CMD_POLY_TRANS ) {
nkeynes@128
   289
		glEnable( GL_BLEND );
nkeynes@128
   290
	    } else {
nkeynes@128
   291
		glDisable( GL_BLEND );
nkeynes@128
   292
	    }
nkeynes@128
   293
nkeynes@103
   294
	    poly = (struct pvr2_poly *)cmd_ptr;
nkeynes@103
   295
	    if( poly->command & PVR2_POLY_TEXTURED ) {
nkeynes@103
   296
		uint32_t addr = PVR2_TEX_ADDR(poly->texture);
nkeynes@103
   297
		int width = POLY_TEX_WIDTH(poly);
nkeynes@103
   298
		int height = POLY_TEX_HEIGHT(poly);
nkeynes@108
   299
		glEnable( GL_TEXTURE_2D );
nkeynes@103
   300
		texcache_get_texture( addr, width, height, poly->texture );
nkeynes@103
   301
		textured = TRUE;
nkeynes@108
   302
		glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY_TEX_BLEND(poly) );
nkeynes@103
   303
	    } else {
nkeynes@103
   304
		textured = FALSE;
nkeynes@103
   305
		glDisable( GL_TEXTURE_2D );
nkeynes@103
   306
	    }
nkeynes@103
   307
	    glBlendFunc( POLY_BLEND_SRC(poly), POLY_BLEND_DEST(poly) );
nkeynes@103
   308
	    if( poly->command & PVR2_POLY_SPECULAR ) {
nkeynes@103
   309
		/* Second block expected */
nkeynes@103
   310
	    }
nkeynes@103
   311
	    if( POLY_DEPTH_WRITE(poly) ) {
nkeynes@103
   312
		glEnable( GL_DEPTH_TEST );
nkeynes@103
   313
		glDepthFunc( POLY_DEPTH_MODE(poly) );
nkeynes@103
   314
	    } else {
nkeynes@103
   315
		glDisable( GL_DEPTH_TEST );
nkeynes@103
   316
	    }
nkeynes@103
   317
nkeynes@108
   318
	    switch( (poly->poly_cfg >> 27) & 0x03 ) {
nkeynes@108
   319
	    case 0:
nkeynes@108
   320
	    case 1:
nkeynes@108
   321
		glDisable( GL_CULL_FACE );
nkeynes@108
   322
		break;
nkeynes@108
   323
	    case 2:
nkeynes@108
   324
		glEnable( GL_CULL_FACE );
nkeynes@108
   325
		glFrontFace( GL_CW );
nkeynes@108
   326
		break;
nkeynes@108
   327
	    case 3:
nkeynes@108
   328
		glEnable( GL_CULL_FACE );
nkeynes@108
   329
		glFrontFace( GL_CCW );
nkeynes@108
   330
	    }
nkeynes@118
   331
	    strip_length = POLY_STRIP_VERTEXES( poly );
nkeynes@118
   332
	    colour_type = POLY_COLOUR_TYPE( poly );
nkeynes@118
   333
	    vertex_count = 0;
nkeynes@103
   334
	    break;
nkeynes@103
   335
	case PVR2_CMD_VERTEX_LAST:
nkeynes@103
   336
	case PVR2_CMD_VERTEX:
nkeynes@118
   337
	    if( vertex_count == 0 ) {
nkeynes@118
   338
		if( strip_length == 3 )
nkeynes@118
   339
		    glBegin( GL_TRIANGLES );
nkeynes@118
   340
		else 
nkeynes@118
   341
		    glBegin( GL_TRIANGLE_STRIP );
nkeynes@103
   342
	    }
nkeynes@118
   343
	    vertex_count++;
nkeynes@118
   344
nkeynes@118
   345
	    struct pvr2_vertex_packed *vertex = (struct pvr2_vertex_packed *)cmd_ptr;
nkeynes@103
   346
	    if( textured ) {
nkeynes@103
   347
		glTexCoord2f( vertex->s, vertex->t );
nkeynes@128
   348
nkeynes@128
   349
		switch( colour_type ) {
nkeynes@128
   350
		case POLY_COLOUR_PACKED:
nkeynes@128
   351
		    glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
nkeynes@128
   352
				vertex->colour, vertex->colour >> 24 );
nkeynes@128
   353
		    break;
nkeynes@128
   354
		}
nkeynes@128
   355
	    } else {
nkeynes@128
   356
 		switch( colour_type ) {
nkeynes@128
   357
		case POLY_COLOUR_PACKED:
nkeynes@128
   358
		    glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
nkeynes@128
   359
				vertex->colour, vertex->colour >> 24 );
nkeynes@128
   360
		    break;
nkeynes@128
   361
		case POLY_COLOUR_FLOAT: 
nkeynes@128
   362
		    {
nkeynes@128
   363
			struct pvr2_vertex_float *v = (struct pvr2_vertex_float *)cmd_ptr;
nkeynes@128
   364
			glColor4f( v->r, v->g, v->b, v->a );
nkeynes@128
   365
		    }
nkeynes@128
   366
		    break;
nkeynes@128
   367
		}
nkeynes@103
   368
	    }
nkeynes@118
   369
nkeynes@103
   370
	    glVertex3f( vertex->x, vertex->y, vertex->z );
nkeynes@103
   371
	    
nkeynes@118
   372
	    if( cmd == PVR2_CMD_VERTEX_LAST ) {
nkeynes@103
   373
		glEnd();
nkeynes@118
   374
		vertex_count = 0;
nkeynes@118
   375
	    }
nkeynes@118
   376
nkeynes@118
   377
	    if( vertex_count >= strip_length ) {
nkeynes@118
   378
		ERROR( "Rendering long strip (expected end after %d)", strip_length );
nkeynes@118
   379
		pvr2_dump_display_list( display_list, length );
nkeynes@118
   380
		return;
nkeynes@108
   381
	    }
nkeynes@103
   382
	    break;
nkeynes@103
   383
	}
nkeynes@103
   384
	cmd_ptr += 8; /* Next record */
nkeynes@103
   385
    }
nkeynes@103
   386
}
nkeynes@103
   387
nkeynes@128
   388
#define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
nkeynes@128
   389
#define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
nkeynes@128
   390
nkeynes@128
   391
/**
nkeynes@128
   392
 * Render the background plane as best we can. Unfortunately information
nkeynes@128
   393
 * is a little scant, to say the least.
nkeynes@128
   394
 */
nkeynes@128
   395
void pvr2_render_draw_backplane( uint32_t mode, uint32_t *poly )
nkeynes@128
   396
{
nkeynes@128
   397
    if( (mode >> 24) == 0x01 ) {
nkeynes@128
   398
	/* Packed colour. I think */
nkeynes@128
   399
	pvr2_bgplane_packed_t bg = (pvr2_bgplane_packed_t)poly;
nkeynes@128
   400
	if( bg->colour1 != bg->colour2 || bg->colour2 != bg->colour3 ) {
nkeynes@128
   401
	    WARN( "Multiple background colours specified. Confused" );
nkeynes@128
   402
	}
nkeynes@128
   403
	float x1 = MIN3( bg->x1, bg->x2, bg->x3 );
nkeynes@128
   404
	float y1 = MIN3( bg->y1, bg->y2, bg->y3 );
nkeynes@128
   405
	float x2 = MAX3( bg->x1, bg->x2, bg->x3 );
nkeynes@128
   406
	float y2 = MAX3( bg->y1, bg->y2, bg->y3 );
nkeynes@128
   407
	float z = MIN3( bg->z1, bg->z2, bg->z3 );
nkeynes@128
   408
	glDisable( GL_TEXTURE_2D );
nkeynes@128
   409
	glDisable( GL_DEPTH_TEST );
nkeynes@128
   410
	glColor3ub( (uint8_t)(bg->colour1 >> 16), (uint8_t)(bg->colour1 >> 8), 
nkeynes@128
   411
		    (uint8_t)bg->colour1 );
nkeynes@128
   412
	glBegin( GL_QUADS );
nkeynes@128
   413
	glVertex3f( x1, y1, z );
nkeynes@128
   414
	glVertex3f( x2, y1, z );
nkeynes@128
   415
	glVertex3f( x2, y2, z );
nkeynes@128
   416
	glVertex3f( x1, y2, z );
nkeynes@128
   417
	glEnd();
nkeynes@128
   418
    } else {
nkeynes@128
   419
	WARN( "Unknown bgplane mode: %08X", mode );
nkeynes@128
   420
	fwrite_dump( poly, 48, stderr );
nkeynes@128
   421
    }
nkeynes@128
   422
}
nkeynes@128
   423
nkeynes@103
   424
/**
nkeynes@103
   425
 * Render a complete scene into the OpenGL back buffer.
nkeynes@103
   426
 * Note: this will probably need to be broken up eventually once timings are
nkeynes@100
   427
 * determined.
nkeynes@100
   428
 */
nkeynes@103
   429
void pvr2_render_scene( )
nkeynes@100
   430
{
nkeynes@103
   431
    struct tile_descriptor *tile_desc =
nkeynes@103
   432
	(struct tile_descriptor *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, TILEBASE ));
nkeynes@100
   433
nkeynes@103
   434
    uint32_t render_addr = MMIO_READ( PVR2, RENDADDR1 );
nkeynes@103
   435
    gboolean render_to_tex;
nkeynes@103
   436
    if( render_addr & 0x01000000 ) {
nkeynes@103
   437
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
nkeynes@103
   438
	/* Heuristic - if we're rendering to the interlaced region we're
nkeynes@103
   439
	 * probably creating a texture rather than rendering actual output.
nkeynes@103
   440
	 * We can optimise for this case a little
nkeynes@103
   441
	 */
nkeynes@103
   442
	render_to_tex = TRUE;
nkeynes@118
   443
	WARN( "Render to texture not supported properly yet" );
nkeynes@103
   444
    } else {
nkeynes@103
   445
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
nkeynes@103
   446
	render_to_tex = FALSE;
nkeynes@103
   447
    }
nkeynes@128
   448
    
nkeynes@128
   449
    float bgplanez = MMIO_READF( PVR2, BGPLANEZ );
nkeynes@103
   450
    uint32_t render_mode = MMIO_READ( PVR2, RENDMODE );
nkeynes@103
   451
    int width = 640; /* FIXME - get this from the tile buffer */
nkeynes@103
   452
    int height = 480;
nkeynes@103
   453
    int colour_format = pvr2_render_colour_format[render_mode&0x07];
nkeynes@103
   454
    pvr2_render_prepare_context( render_addr, width, height, colour_format, 
nkeynes@128
   455
				 bgplanez, render_to_tex );
nkeynes@103
   456
nkeynes@103
   457
    uint32_t *display_list = 
nkeynes@103
   458
	(uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, OBJBASE ));
nkeynes@128
   459
nkeynes@103
   460
    uint32_t display_length = *display_list++;
nkeynes@103
   461
nkeynes@103
   462
    int clip_x = MMIO_READ( PVR2, HCLIP ) & 0x03FF;
nkeynes@103
   463
    int clip_y = MMIO_READ( PVR2, VCLIP ) & 0x03FF;
nkeynes@103
   464
    int clip_width = ((MMIO_READ( PVR2, HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
nkeynes@103
   465
    int clip_height= ((MMIO_READ( PVR2, VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
nkeynes@103
   466
nkeynes@103
   467
    if( clip_x == 0 && clip_y == 0 && clip_width == width && clip_height == height ) {
nkeynes@103
   468
	glDisable( GL_SCISSOR_TEST );
nkeynes@103
   469
    } else {
nkeynes@103
   470
	glEnable( GL_SCISSOR_TEST );
nkeynes@103
   471
	glScissor( clip_x, clip_y, clip_width, clip_height );
nkeynes@103
   472
    }
nkeynes@103
   473
nkeynes@103
   474
    /* Fog setup goes here */
nkeynes@103
   475
nkeynes@128
   476
    /* Render the background plane */
nkeynes@128
   477
    uint32_t bgplane_mode = MMIO_READ(PVR2, BGPLANE);
nkeynes@128
   478
    uint32_t *bgplane = display_list + (((bgplane_mode & 0x00FFFFFF)) >> 3) - 1;
nkeynes@128
   479
    pvr2_render_draw_backplane( bgplane_mode, bgplane );
nkeynes@128
   480
nkeynes@103
   481
    /* Render the display list */
nkeynes@103
   482
    pvr2_render_display_list( display_list, display_length );
nkeynes@103
   483
nkeynes@103
   484
    /* Post-render cleanup and update */
nkeynes@103
   485
nkeynes@108
   486
    /* Add frame, fps, etc data */
nkeynes@108
   487
    glRasterPos2i( 4, 16 );
nkeynes@108
   488
    glPrintf( "Frame %d", pvr2_frame_counter );
nkeynes@103
   489
    
nkeynes@103
   490
    /* Generate end of render event */
nkeynes@100
   491
    asic_event( EVENT_PVR_RENDER_DONE );
nkeynes@100
   492
    DEBUG( "Rendered frame %d", pvr2_frame_counter );
nkeynes@100
   493
}
nkeynes@103
   494
nkeynes@103
   495
nkeynes@103
   496
/**
nkeynes@103
   497
 * Flush the indicated render buffer back to PVR. Caller is responsible for
nkeynes@103
   498
 * tracking whether there is actually anything in the buffer.
nkeynes@103
   499
 *
nkeynes@103
   500
 * @param buffer A render buffer indicating the address to store to, and the
nkeynes@103
   501
 * format the data needs to be in.
nkeynes@103
   502
 * @param backBuffer TRUE to flush the back buffer, FALSE for 
nkeynes@103
   503
 * the front buffer.
nkeynes@103
   504
 */
nkeynes@103
   505
void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   506
			      gboolean backBuffer )
nkeynes@103
   507
{
nkeynes@103
   508
    if( buffer->render_addr == -1 )
nkeynes@103
   509
	return;
nkeynes@103
   510
    GLenum type, format = GL_RGBA;
nkeynes@103
   511
    int size = buffer->width * buffer->height;
nkeynes@103
   512
nkeynes@103
   513
    switch( buffer->colour_format ) {
nkeynes@103
   514
    case COLFMT_RGB565: 
nkeynes@103
   515
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   516
	format = GL_RGB; 
nkeynes@103
   517
	size <<= 1;
nkeynes@103
   518
	break;
nkeynes@103
   519
    case COLFMT_RGB888: 
nkeynes@103
   520
	type = GL_UNSIGNED_INT; 
nkeynes@103
   521
	format = GL_RGB;
nkeynes@103
   522
	size = (size<<1)+size;
nkeynes@103
   523
	break;
nkeynes@103
   524
    case COLFMT_ARGB1555: 
nkeynes@103
   525
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   526
	size <<= 1;
nkeynes@103
   527
	break;
nkeynes@103
   528
    case COLFMT_ARGB4444: 
nkeynes@103
   529
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   530
	size <<= 1;
nkeynes@103
   531
	break;
nkeynes@103
   532
    case COLFMT_ARGB8888: 
nkeynes@103
   533
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   534
	size <<= 2;
nkeynes@103
   535
	break;
nkeynes@103
   536
    }
nkeynes@103
   537
    
nkeynes@103
   538
    if( backBuffer ) {
nkeynes@103
   539
	glFinish();
nkeynes@103
   540
	glReadBuffer( GL_BACK );
nkeynes@103
   541
    } else {
nkeynes@103
   542
	glReadBuffer( GL_FRONT );
nkeynes@103
   543
    }
nkeynes@103
   544
nkeynes@103
   545
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   546
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   547
	char target[size];
nkeynes@103
   548
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   549
	pvr2_vram64_write( buffer->render_addr, target, size );
nkeynes@103
   550
    } else {
nkeynes@103
   551
	/* Regular buffer - go direct */
nkeynes@103
   552
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   553
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@103
   554
    }
nkeynes@103
   555
}
nkeynes@103
   556
nkeynes@103
   557
nkeynes@103
   558
/**
nkeynes@103
   559
 * Copy data from PVR ram into the GL render buffer. 
nkeynes@103
   560
 *
nkeynes@103
   561
 * @param buffer A render buffer indicating the address to read from, and the
nkeynes@103
   562
 * format the data is in.
nkeynes@103
   563
 * @param backBuffer TRUE to write the back buffer, FALSE for 
nkeynes@103
   564
 * the front buffer.
nkeynes@103
   565
 */
nkeynes@103
   566
void pvr2_render_copy_from_sh4( pvr2_render_buffer_t buffer, 
nkeynes@103
   567
				gboolean backBuffer )
nkeynes@103
   568
{
nkeynes@103
   569
    if( buffer->render_addr == -1 )
nkeynes@103
   570
	return;
nkeynes@103
   571
    GLenum type, format = GL_RGBA;
nkeynes@103
   572
    int size = buffer->width * buffer->height;
nkeynes@103
   573
nkeynes@103
   574
    switch( buffer->colour_format ) {
nkeynes@103
   575
    case COLFMT_RGB565: 
nkeynes@103
   576
	type = GL_UNSIGNED_SHORT_5_6_5; 
nkeynes@103
   577
	format = GL_RGB; 
nkeynes@103
   578
	size <<= 1;
nkeynes@103
   579
	break;
nkeynes@103
   580
    case COLFMT_RGB888: 
nkeynes@103
   581
	type = GL_UNSIGNED_INT; 
nkeynes@103
   582
	format = GL_RGB;
nkeynes@103
   583
	size = (size<<1)+size;
nkeynes@103
   584
	break;
nkeynes@103
   585
    case COLFMT_ARGB1555: 
nkeynes@103
   586
	type = GL_UNSIGNED_SHORT_5_5_5_1; 
nkeynes@103
   587
	size <<= 1;
nkeynes@103
   588
	break;
nkeynes@103
   589
    case COLFMT_ARGB4444: 
nkeynes@103
   590
	type = GL_UNSIGNED_SHORT_4_4_4_4; 
nkeynes@103
   591
	size <<= 1;
nkeynes@103
   592
	break;
nkeynes@103
   593
    case COLFMT_ARGB8888: 
nkeynes@103
   594
	type = GL_UNSIGNED_INT_8_8_8_8; 
nkeynes@103
   595
	size <<= 2;
nkeynes@103
   596
	break;
nkeynes@103
   597
    }
nkeynes@103
   598
    
nkeynes@103
   599
    if( backBuffer ) {
nkeynes@103
   600
	glDrawBuffer( GL_BACK );
nkeynes@103
   601
    } else {
nkeynes@103
   602
	glDrawBuffer( GL_FRONT );
nkeynes@103
   603
    }
nkeynes@103
   604
nkeynes@103
   605
    glRasterPos2i( 0, 0 );
nkeynes@103
   606
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@103
   607
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@103
   608
	char target[size];
nkeynes@103
   609
	pvr2_vram64_read( target, buffer->render_addr, size );
nkeynes@103
   610
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   611
		      format, type, target );
nkeynes@103
   612
    } else {
nkeynes@103
   613
	/* Regular buffer - go direct */
nkeynes@103
   614
	char *target = mem_get_region( buffer->render_addr );
nkeynes@103
   615
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@103
   616
		      format, type, target );
nkeynes@103
   617
    }
nkeynes@103
   618
}
.