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