Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 215:f432833e8303
prev191:df4441cf3128
next221:cf5c6d326162
author nkeynes
date Fri Aug 18 12:43:24 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement pvr banking strategy wrt the polygon buffers
view annotate diff log raw
     1 /**
     2  * $Id: rendcore.c,v 1.3 2006-08-18 12:43:24 nkeynes Exp $
     3  *
     4  * PVR2 renderer core.
     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  */
    18 #include <sys/time.h>
    19 #include "pvr2/pvr2.h"
    20 #include "asic.h"
    22 static int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    23 				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    24 				      GL_ALWAYS };
    25 static int pvr2_poly_srcblend[8] = { 
    26     GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    27     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    28     GL_ONE_MINUS_DST_ALPHA };
    29 static int pvr2_poly_dstblend[8] = {
    30     GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    31     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    32     GL_ONE_MINUS_DST_ALPHA };
    33 static int pvr2_poly_texblend[4] = {
    34     GL_REPLACE, GL_BLEND, GL_DECAL, GL_MODULATE };
    35 static int pvr2_render_colour_format[8] = {
    36     COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
    37     COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
    38 #define POLY1_DEPTH_MODE(poly1) ( pvr2_poly_depthmode[(poly1)>>29] )
    39 #define POLY1_DEPTH_ENABLE(poly1) (((poly1)&0x04000000) == 0 )
    40 #define POLY1_CULL_MODE(poly1) (((poly1)>>27)&0x03)
    41 #define POLY1_TEXTURED(poly1) (((poly1)&0x02000000))
    42 #define POLY1_SPECULAR(poly1) (((poly1)&0x01000000))
    43 #define POLY1_SHADE_MODEL(poly1) (((poly1)&0x00800000) ? GL_SMOOTH : GL_FLAT)
    44 #define POLY1_UV16(poly1)   (((poly1)&0x00400000))
    45 #define POLY1_SINGLE_TILE(poly1) (((poly1)&0x00200000))
    47 #define POLY2_SRC_BLEND(poly2) ( pvr2_poly_srcblend[(poly2) >> 29] )
    48 #define POLY2_DEST_BLEND(poly2) ( pvr2_poly_dstblend[((poly2)>>26)&0x07] )
    49 #define POLY2_SRC_BLEND_ENABLE(poly2) ((poly2)&0x02000000)
    50 #define POLY2_DEST_BLEND_ENABLE(poly2) ((poly2)&0x01000000)
    51 #define POLY2_COLOUR_CLAMP_ENABLE(poly2) ((poly2)&0x00200000)
    52 #define POLY2_ALPHA_ENABLE(poly2) ((poly2)&0x001000000)
    53 #define POLY2_TEX_ALPHA_ENABLE(poly2) (((poly2)&0x00080000) == 0 )
    54 #define POLY2_TEX_WIDTH(poly2) ( 1<< ((((poly2) >> 3) & 0x07 ) + 3) )
    55 #define POLY2_TEX_HEIGHT(poly2) ( 1<< (((poly2) & 0x07 ) + 3) )
    56 #define POLY2_TEX_BLEND(poly2) ( pvr2_poly_texblend[((poly2) >> 6)&0x03] )
    58 #define RENDER_ZONLY  0
    59 #define RENDER_NORMAL 1     /* Render non-modified polygons */
    60 #define RENDER_CHEAPMOD 2   /* Render cheap-modified polygons */
    61 #define RENDER_FULLMOD 3    /* Render the fully-modified version of the polygons */
    63 #define CULL_NONE 0
    64 #define CULL_SMALL 1
    65 #define CULL_CCW 2
    66 #define CULL_CW 3
    68 #define SEGMENT_END         0x80000000
    69 #define SEGMENT_SORT_TRANS  0x20000000
    70 #define SEGMENT_START       0x10000000
    71 #define SEGMENT_X(c)        (((c) >> 2) & 0x3F)
    72 #define SEGMENT_Y(c)        (((c) >> 8) & 0x3F)
    73 #define NO_POINTER          0x80000000
    75 extern char *video_base;
    77 struct tile_segment {
    78     uint32_t control;
    79     pvraddr_t opaque_ptr;
    80     pvraddr_t opaquemod_ptr;
    81     pvraddr_t trans_ptr;
    82     pvraddr_t transmod_ptr;
    83     pvraddr_t punchout_ptr;
    84 };
    86 /**
    87  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    88  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    89  * TODO: Check the correctness of this.
    90  */
    91 float halftofloat( uint16_t half )
    92 {
    93     union {
    94         float f;
    95         uint32_t i;
    96     } temp;
    97     int e = ((half & 0x7C00) >> 10) - 15 + 127;
    99     temp.i = ((half & 0x8000) << 16) | (e << 23) |
   100              ((half & 0x03FF) << 13);
   101     return temp.f;
   102 }
   105 /**
   106  * Setup the GL context for the supplied polygon context.
   107  * @param context pointer to 3 or 5 words of polygon context
   108  * @param modified boolean flag indicating that the modified
   109  *  version should be used, rather than the normal version.
   110  */
   111 void render_set_context( uint32_t *context, int render_mode )
   112 {
   113     uint32_t poly1 = context[0], poly2, texture;
   114     if( render_mode == RENDER_FULLMOD ) {
   115 	poly2 = context[3];
   116 	texture = context[4];
   117     } else {
   118 	poly2 = context[1];
   119 	texture = context[2];
   120     }
   122     if( POLY1_DEPTH_ENABLE(poly1) ) {
   123 	glEnable( GL_DEPTH_TEST );
   124 	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   125     } else {
   126 	glDisable( GL_DEPTH_TEST );
   127     }
   129     switch( POLY1_CULL_MODE(poly1) ) {
   130     case CULL_NONE:
   131     case CULL_SMALL:
   132 	glDisable( GL_CULL_FACE );
   133 	break;
   134     case CULL_CCW:
   135 	glEnable( GL_CULL_FACE );
   136 	glFrontFace( GL_CW );
   137 	break;
   138     case CULL_CW:
   139 	glEnable( GL_CULL_FACE );
   140 	glFrontFace( GL_CCW );
   141 	break;
   142     }
   144     if( POLY1_TEXTURED(poly1) ) {
   145 	int width = POLY2_TEX_WIDTH(poly2);
   146 	int height = POLY2_TEX_HEIGHT(poly2);
   147 	glEnable(GL_TEXTURE_2D);
   148 	texcache_get_texture( (texture&0x001FFFFF)<<3, width, height, texture );
   149 	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
   150     } else {
   151 	glDisable( GL_TEXTURE_2D );
   152     }
   154     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   156     glBlendFunc( POLY2_SRC_BLEND(poly2), POLY2_DEST_BLEND(poly2) );
   157     if( POLY2_TEX_ALPHA_ENABLE(poly2) ) {
   158 	glEnable(GL_BLEND);
   159     } else {
   160 	glDisable(GL_BLEND);
   161     }
   162 }
   164 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
   165 		      int render_mode ) 
   166 {
   167     int i, m=0;
   169     if( render_mode == RENDER_FULLMOD ) {
   170 	m = (vertex_size - 3)/2;
   171     }
   173     glBegin( GL_TRIANGLE_STRIP );
   175     for( i=0; i<num_vertexes; i++ ) {
   176 	float *vertexf = (float *)vertexes;
   177 	uint32_t argb;
   178 	if( POLY1_TEXTURED(poly1) ) {
   179 	    if( POLY1_UV16(poly1) ) {
   180 		glTexCoord2f( halftofloat(vertexes[m+3]>>16),
   181 			      halftofloat(vertexes[m+3]) );
   182 		argb = vertexes[m+4];
   183 	    } else {
   184 		glTexCoord2f( vertexf[m+3], vertexf[m+4] );
   185 		argb = vertexes[m+5];
   186 	    }
   187 	} else {
   188 	    argb = vertexes[m+3];
   189 	}
   191 	glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
   192 		    (GLubyte)argb, (GLubyte)(argb >> 24) );
   193 	glVertex3f( vertexf[0], vertexf[1], vertexf[2] );
   194 	vertexes += vertex_size;
   195     }
   197     glEnd();
   198 }
   200 /**
   201  * Render a simple (not auto-sorted) tile
   202  */
   203 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   204     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
   205     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   206     do {
   207 	uint32_t entry = *tile_list++;
   208 	if( entry >> 28 == 0x0F ) {
   209 	    break;
   210 	} else if( entry >> 28 == 0x0E ) {
   211 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   212 	} else {
   213 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
   214 	    int is_modified = entry & 0x01000000;
   215 	    int vertex_length = (entry >> 21) & 0x07;
   216 	    int context_length = 3;
   217 	    if( is_modified && !cheap_modifier_mode ) {
   218 		context_length = 5;
   219 		vertex_length *= 2 ;
   220 	    }
   221 	    vertex_length += 3;
   223 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   224 		/* Triangle(s) */
   225 		int strip_count = ((entry >> 25) & 0x0F)+1;
   226 		int polygon_length = 3 * vertex_length + context_length;
   227 		int i;
   228 		for( i=0; i<strip_count; i++ ) {
   229 		    render_set_context( polygon, render_mode );
   230 		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
   231 				     render_mode );
   232 		    polygon += polygon_length;
   233 		}
   234 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   235 		/* Sprite(s) */
   236 		int strip_count = (entry >> 25) & 0x0F;
   237 		int polygon_length = 4 * vertex_length + context_length;
   238 		int i;
   239 		for( i=0; i<strip_count; i++ ) {
   240 		    render_set_context( polygon, render_mode );
   241 		    render_vertexes( *polygon, polygon+context_length, 4, vertex_length,
   242 				     render_mode );
   243 		    polygon += polygon_length;
   244 		}
   245 	    } else {
   246 		/* Polygon */
   247 		int i, first=-1, last = -1;
   248 		for( i=0; i<6; i++ ) {
   249 		    if( entry & (0x40000000>>i) ) {
   250 			if( first == -1 ) first = i;
   251 			last = i;
   252 		    }
   253 		}
   254 		if( first != -1 ) {
   255 		    first = 0;
   256 		    render_set_context(polygon, render_mode);
   257 		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
   258 				     (last-first+3), vertex_length, render_mode );
   259 		}
   260 	    }
   261 	}
   262     } while( 1 );
   263 }
   265 void render_autosort_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   266     //WARN( "Autosort not implemented yet" );
   267     render_tile( tile_entry, render_mode, cheap_modifier_mode );
   268 }
   270 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
   271 			int clipx2, int clipy2 ) {
   273     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   274     int tile_sort;
   275     gboolean cheap_shadow;
   277     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   278     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   279     int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
   281     if( obj_config & 0x00200000 ) {
   282 	if( isp_config & 1 ) {
   283 	    tile_sort = 0;
   284 	} else {
   285 	    tile_sort = 2;
   286 	}
   287     } else {
   288 	tile_sort = 1;
   289     }
   291     cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
   293     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   295     struct timeval tv_start, tv_end;
   296     gettimeofday(&tv_start, NULL);
   297     fprintf( stderr, "Start render at %d.%d\n", tv_start.tv_sec, tv_start.tv_usec );
   298     glEnable( GL_SCISSOR_TEST );
   299     while( (segment->control & SEGMENT_END) == 0 ) {
   300 	int tilex = SEGMENT_X(segment->control);
   301 	int tiley = SEGMENT_Y(segment->control);
   303 	int x1 = tilex << 5;
   304 	int y1 = tiley << 5;
   305 	if( x1 + 32 <= clipx1 ||
   306 	    y1 + 32 <= clipy1 ||
   307 	    x1 >= clipx2 ||
   308 	    y1 >= clipy2 ) {
   309 	    /* Tile completely clipped, skip */
   310 	    segment++;
   311 	    continue;
   312 	}
   314 	/* Set a scissor on the visible part of the tile */
   315 	int w = MIN(x1+32, clipx2) - x1;
   316 	int h = MIN(y1+32, clipy2) - y1;
   317 	x1 = MAX(x1,clipx1);
   318 	y1 = MAX(y1,clipy1);
   319 	glScissor( x1, height-y1-h, w, h );
   321 	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
   322 	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
   323 		/* TODO */
   324 	    }
   325 	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
   326 	}
   328 	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
   329 	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
   330 		/* TODO */
   331 	    } 
   332 	    if( tile_sort == 2 || (tile_sort == 1 && (segment->control & SEGMENT_SORT_TRANS)) ) {
   333 		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   334 	    } else {
   335 		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   336 	    }
   337 	}
   339 	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
   340 	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
   341 	}
   342 	segment++;
   344     }
   345     glDisable( GL_SCISSOR_TEST );
   347     gettimeofday(&tv_end, NULL);
   348     timersub(&tv_end,&tv_start, &tv_start);
   349     fprintf( stderr, "Frame took %d.%06ds\n", tv_start.tv_sec, tv_start.tv_usec );
   351 }
.