Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 298:44d94dd0e8aa
prev286:0fa5aae66f31
next308:10a5b5475fb0
author nkeynes
date Wed Jan 17 09:21:09 2007 +0000 (17 years ago)
permissions -rw-r--r--
last change Change tex mode 1 to MODULATE
view annotate diff log raw
     1 /**
     2  * $Id: rendcore.c,v 1.9 2007-01-17 09:21:09 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 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    23 				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    24 				      GL_ALWAYS };
    25 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 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 int pvr2_poly_texblend[4] = {
    34     GL_REPLACE, GL_MODULATE, GL_DECAL, GL_MODULATE };
    35 int pvr2_render_colour_format[8] = {
    36     COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
    37     COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
    40 #define CULL_NONE 0
    41 #define CULL_SMALL 1
    42 #define CULL_CCW 2
    43 #define CULL_CW 3
    45 #define SEGMENT_END         0x80000000
    46 #define SEGMENT_ZCLEAR      0x40000000
    47 #define SEGMENT_SORT_TRANS  0x20000000
    48 #define SEGMENT_START       0x10000000
    49 #define SEGMENT_X(c)        (((c) >> 2) & 0x3F)
    50 #define SEGMENT_Y(c)        (((c) >> 8) & 0x3F)
    51 #define NO_POINTER          0x80000000
    53 extern char *video_base;
    55 struct tile_segment {
    56     uint32_t control;
    57     pvraddr_t opaque_ptr;
    58     pvraddr_t opaquemod_ptr;
    59     pvraddr_t trans_ptr;
    60     pvraddr_t transmod_ptr;
    61     pvraddr_t punchout_ptr;
    62 };
    64 /**
    65  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    66  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    67  * TODO: Check the correctness of this.
    68  */
    69 float halftofloat( uint16_t half )
    70 {
    71     union {
    72         float f;
    73         uint32_t i;
    74     } temp;
    75     int e = ((half & 0x7C00) >> 10) - 15 + 127;
    77     temp.i = ((half & 0x8000) << 16) | (e << 23) |
    78              ((half & 0x03FF) << 13);
    79     return temp.f;
    80 }
    83 /**
    84  * Setup the GL context for the supplied polygon context.
    85  * @param context pointer to 3 or 5 words of polygon context
    86  * @param modified boolean flag indicating that the modified
    87  *  version should be used, rather than the normal version.
    88  */
    89 void render_set_context( uint32_t *context, int render_mode )
    90 {
    91     uint32_t poly1 = context[0], poly2, texture;
    92     if( render_mode == RENDER_FULLMOD ) {
    93 	poly2 = context[3];
    94 	texture = context[4];
    95     } else {
    96 	poly2 = context[1];
    97 	texture = context[2];
    98     }
   100     if( POLY1_DEPTH_ENABLE(poly1) ) {
   101 	glEnable( GL_DEPTH_TEST );
   102 	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   103     } else {
   104 	glDisable( GL_DEPTH_TEST );
   105     }
   107     switch( POLY1_CULL_MODE(poly1) ) {
   108     case CULL_NONE:
   109     case CULL_SMALL:
   110 	glDisable( GL_CULL_FACE );
   111 	break;
   112     case CULL_CCW:
   113 	glEnable( GL_CULL_FACE );
   114 	glFrontFace( GL_CW );
   115 	break;
   116     case CULL_CW:
   117 	glEnable( GL_CULL_FACE );
   118 	glFrontFace( GL_CCW );
   119 	break;
   120     }
   122     if( POLY1_TEXTURED(poly1) ) {
   123 	int width = POLY2_TEX_WIDTH(poly2);
   124 	int height = POLY2_TEX_HEIGHT(poly2);
   125 	glEnable(GL_TEXTURE_2D);
   126 	texcache_get_texture( (texture&0x000FFFFF)<<3, width, height, texture );
   127 	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
   128     } else {
   129 	glDisable( GL_TEXTURE_2D );
   130     }
   132     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   134     int srcblend = POLY2_SRC_BLEND(poly2);
   135     int destblend = POLY2_DEST_BLEND(poly2);
   136     glBlendFunc( srcblend, destblend );
   137     if( POLY2_TEX_ALPHA_ENABLE(poly2) ) {
   138 	glEnable(GL_BLEND);
   139     } else {
   140 	glDisable(GL_BLEND);
   141     }
   142 }
   144 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
   145 		      int render_mode ) 
   146 {
   147     int i, m=0;
   149     if( render_mode == RENDER_FULLMOD ) {
   150 	m = (vertex_size - 3)/2;
   151     }
   153     glBegin( GL_TRIANGLE_STRIP );
   155     for( i=0; i<num_vertexes; i++ ) {
   156 	float *vertexf = (float *)vertexes;
   157 	uint32_t argb;
   158 	if( POLY1_TEXTURED(poly1) ) {
   159 	    if( POLY1_UV16(poly1) ) {
   160 		glTexCoord2f( halftofloat(vertexes[m+3]>>16),
   161 			      halftofloat(vertexes[m+3]) );
   162 		argb = vertexes[m+4];
   163 	    } else {
   164 		glTexCoord2f( vertexf[m+3], vertexf[m+4] );
   165 		argb = vertexes[m+5];
   166 	    }
   167 	} else {
   168 	    argb = vertexes[m+3];
   169 	}
   171 	glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
   172 		    (GLubyte)argb, (GLubyte)(argb >> 24) );
   173 	glVertex3f( vertexf[0], vertexf[1], vertexf[2] );
   174 	vertexes += vertex_size;
   175     }
   177     glEnd();
   178 }
   180 /**
   181  * Render a simple (not auto-sorted) tile
   182  */
   183 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   184     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
   185     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   186     do {
   187 	uint32_t entry = *tile_list++;
   188 	if( entry >> 28 == 0x0F ) {
   189 	    break;
   190 	} else if( entry >> 28 == 0x0E ) {
   191 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   192 	} else {
   193 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
   194 	    int is_modified = entry & 0x01000000;
   195 	    int vertex_length = (entry >> 21) & 0x07;
   196 	    int context_length = 3;
   197 	    if( is_modified && !cheap_modifier_mode ) {
   198 		context_length = 5;
   199 		vertex_length *= 2 ;
   200 	    }
   201 	    vertex_length += 3;
   203 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   204 		/* Triangle(s) */
   205 		int strip_count = ((entry >> 25) & 0x0F)+1;
   206 		int polygon_length = 3 * vertex_length + context_length;
   207 		int i;
   208 		for( i=0; i<strip_count; i++ ) {
   209 		    render_set_context( polygon, render_mode );
   210 		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
   211 				     render_mode );
   212 		    polygon += polygon_length;
   213 		}
   214 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   215 		/* Sprite(s) */
   216 		int strip_count = (entry >> 25) & 0x0F;
   217 		int polygon_length = 4 * vertex_length + context_length;
   218 		int i;
   219 		for( i=0; i<strip_count; i++ ) {
   220 		    render_set_context( polygon, render_mode );
   221 		    render_vertexes( *polygon, polygon+context_length, 4, vertex_length,
   222 				     render_mode );
   223 		    polygon += polygon_length;
   224 		}
   225 	    } else {
   226 		/* Polygon */
   227 		int i, first=-1, last = -1;
   228 		for( i=0; i<6; i++ ) {
   229 		    if( entry & (0x40000000>>i) ) {
   230 			if( first == -1 ) first = i;
   231 			last = i;
   232 		    }
   233 		}
   234 		if( first != -1 ) {
   235 		    first = 0;
   236 		    render_set_context(polygon, render_mode);
   237 		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
   238 				     (last-first+3), vertex_length, render_mode );
   239 		}
   240 	    }
   241 	}
   242     } while( 1 );
   243 }
   245 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
   246 			int clipx2, int clipy2 ) {
   248     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   249     int tile_sort;
   250     gboolean cheap_shadow;
   252     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   253     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   254     int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
   256     if( (obj_config & 0x00200000) == 0 ) {
   257 	if( isp_config & 1 ) {
   258 	    tile_sort = 0;
   259 	} else {
   260 	    tile_sort = 2;
   261 	}
   262     } else {
   263 	tile_sort = 1;
   264     }
   266     cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
   268     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   270     struct timeval tv_start, tv_end;
   271     gettimeofday(&tv_start, NULL);
   272     glEnable( GL_SCISSOR_TEST );
   273     do {
   274 	// fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
   275 	int tilex = SEGMENT_X(segment->control);
   276 	int tiley = SEGMENT_Y(segment->control);
   278 	int x1 = tilex << 5;
   279 	int y1 = tiley << 5;
   280 	if( x1 + 32 <= clipx1 ||
   281 	    y1 + 32 <= clipy1 ||
   282 	    x1 >= clipx2 ||
   283 	    y1 >= clipy2 ) {
   284 	    /* Tile completely clipped, skip */
   285 	    continue;
   286 	}
   288 	/* Set a scissor on the visible part of the tile */
   289 	int w = MIN(x1+32, clipx2) - x1;
   290 	int h = MIN(y1+32, clipy2) - y1;
   291 	x1 = MAX(x1,clipx1);
   292 	y1 = MAX(y1,clipy1);
   293 	glScissor( x1, height-y1-h, w, h );
   295 	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
   296 	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
   297 		/* TODO */
   298 	    }
   299 	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
   300 	}
   302 	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
   303 	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
   304 		/* TODO */
   305 	    } 
   306 	    if( tile_sort == 2 || 
   307 		(tile_sort == 1 && ((segment->control & SEGMENT_SORT_TRANS)==0)) ) {
   308 		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   309 	    } else {
   310 		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   311 	    }
   312 	}
   314 	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
   315 	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
   316 	}
   317     } while( ((segment++)->control & SEGMENT_END) == 0 );
   318     glDisable( GL_SCISSOR_TEST );
   320     gettimeofday(&tv_end, NULL);
   321     timersub(&tv_end,&tv_start, &tv_start);
   322 }
.