Search
lxdream.org :: lxdream/src/pvr2/rendcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendcore.c
changeset 221:cf5c6d326162
prev215:f432833e8303
next222:541d9d899aba
author nkeynes
date Tue Sep 12 08:38:38 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Bug #0010
Move polygon macros to pvr2.h
Implement background rendering more fully
view annotate diff log raw
     1 /**
     2  * $Id: rendcore.c,v 1.4 2006-09-12 08:38:38 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_BLEND, 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 RENDER_ZONLY  0
    41 #define RENDER_NORMAL 1     /* Render non-modified polygons */
    42 #define RENDER_CHEAPMOD 2   /* Render cheap-modified polygons */
    43 #define RENDER_FULLMOD 3    /* Render the fully-modified version of the polygons */
    45 #define CULL_NONE 0
    46 #define CULL_SMALL 1
    47 #define CULL_CCW 2
    48 #define CULL_CW 3
    50 #define SEGMENT_END         0x80000000
    51 #define SEGMENT_SORT_TRANS  0x20000000
    52 #define SEGMENT_START       0x10000000
    53 #define SEGMENT_X(c)        (((c) >> 2) & 0x3F)
    54 #define SEGMENT_Y(c)        (((c) >> 8) & 0x3F)
    55 #define NO_POINTER          0x80000000
    57 extern char *video_base;
    59 struct tile_segment {
    60     uint32_t control;
    61     pvraddr_t opaque_ptr;
    62     pvraddr_t opaquemod_ptr;
    63     pvraddr_t trans_ptr;
    64     pvraddr_t transmod_ptr;
    65     pvraddr_t punchout_ptr;
    66 };
    68 /**
    69  * Convert a half-float (16-bit) FP number to a regular 32-bit float.
    70  * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
    71  * TODO: Check the correctness of this.
    72  */
    73 float halftofloat( uint16_t half )
    74 {
    75     union {
    76         float f;
    77         uint32_t i;
    78     } temp;
    79     int e = ((half & 0x7C00) >> 10) - 15 + 127;
    81     temp.i = ((half & 0x8000) << 16) | (e << 23) |
    82              ((half & 0x03FF) << 13);
    83     return temp.f;
    84 }
    87 /**
    88  * Setup the GL context for the supplied polygon context.
    89  * @param context pointer to 3 or 5 words of polygon context
    90  * @param modified boolean flag indicating that the modified
    91  *  version should be used, rather than the normal version.
    92  */
    93 void render_set_context( uint32_t *context, int render_mode )
    94 {
    95     uint32_t poly1 = context[0], poly2, texture;
    96     if( render_mode == RENDER_FULLMOD ) {
    97 	poly2 = context[3];
    98 	texture = context[4];
    99     } else {
   100 	poly2 = context[1];
   101 	texture = context[2];
   102     }
   104     if( POLY1_DEPTH_ENABLE(poly1) ) {
   105 	glEnable( GL_DEPTH_TEST );
   106 	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   107     } else {
   108 	glDisable( GL_DEPTH_TEST );
   109     }
   111     switch( POLY1_CULL_MODE(poly1) ) {
   112     case CULL_NONE:
   113     case CULL_SMALL:
   114 	glDisable( GL_CULL_FACE );
   115 	break;
   116     case CULL_CCW:
   117 	glEnable( GL_CULL_FACE );
   118 	glFrontFace( GL_CW );
   119 	break;
   120     case CULL_CW:
   121 	glEnable( GL_CULL_FACE );
   122 	glFrontFace( GL_CCW );
   123 	break;
   124     }
   126     if( POLY1_TEXTURED(poly1) ) {
   127 	int width = POLY2_TEX_WIDTH(poly2);
   128 	int height = POLY2_TEX_HEIGHT(poly2);
   129 	glEnable(GL_TEXTURE_2D);
   130 	texcache_get_texture( (texture&0x001FFFFF)<<3, width, height, texture );
   131 	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
   132     } else {
   133 	glDisable( GL_TEXTURE_2D );
   134     }
   136     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   138     int srcblend = POLY2_SRC_BLEND(poly2);
   139     int destblend = POLY2_DEST_BLEND(poly2);
   140     glBlendFunc( srcblend, destblend );
   141     if( POLY2_TEX_ALPHA_ENABLE(poly2) ) {
   142 	glEnable(GL_BLEND);
   143     } else {
   144 	glDisable(GL_BLEND);
   145     }
   146 }
   148 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
   149 		      int render_mode ) 
   150 {
   151     int i, m=0;
   153     if( render_mode == RENDER_FULLMOD ) {
   154 	m = (vertex_size - 3)/2;
   155     }
   157     glBegin( GL_TRIANGLE_STRIP );
   159     for( i=0; i<num_vertexes; i++ ) {
   160 	float *vertexf = (float *)vertexes;
   161 	uint32_t argb;
   162 	if( POLY1_TEXTURED(poly1) ) {
   163 	    if( POLY1_UV16(poly1) ) {
   164 		glTexCoord2f( halftofloat(vertexes[m+3]>>16),
   165 			      halftofloat(vertexes[m+3]) );
   166 		argb = vertexes[m+4];
   167 	    } else {
   168 		glTexCoord2f( vertexf[m+3], vertexf[m+4] );
   169 		argb = vertexes[m+5];
   170 	    }
   171 	} else {
   172 	    argb = vertexes[m+3];
   173 	}
   175 	glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8), 
   176 		    (GLubyte)argb, (GLubyte)(argb >> 24) );
   177 	glVertex3f( vertexf[0], vertexf[1], vertexf[2] );
   178 	vertexes += vertex_size;
   179     }
   181     glEnd();
   182 }
   184 /**
   185  * Render a simple (not auto-sorted) tile
   186  */
   187 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   188     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
   189     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   190     do {
   191 	uint32_t entry = *tile_list++;
   192 	if( entry >> 28 == 0x0F ) {
   193 	    break;
   194 	} else if( entry >> 28 == 0x0E ) {
   195 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   196 	} else {
   197 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
   198 	    int is_modified = entry & 0x01000000;
   199 	    int vertex_length = (entry >> 21) & 0x07;
   200 	    int context_length = 3;
   201 	    if( is_modified && !cheap_modifier_mode ) {
   202 		context_length = 5;
   203 		vertex_length *= 2 ;
   204 	    }
   205 	    vertex_length += 3;
   207 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   208 		/* Triangle(s) */
   209 		int strip_count = ((entry >> 25) & 0x0F)+1;
   210 		int polygon_length = 3 * vertex_length + context_length;
   211 		int i;
   212 		for( i=0; i<strip_count; i++ ) {
   213 		    render_set_context( polygon, render_mode );
   214 		    render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
   215 				     render_mode );
   216 		    polygon += polygon_length;
   217 		}
   218 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   219 		/* Sprite(s) */
   220 		int strip_count = (entry >> 25) & 0x0F;
   221 		int polygon_length = 4 * vertex_length + context_length;
   222 		int i;
   223 		for( i=0; i<strip_count; i++ ) {
   224 		    render_set_context( polygon, render_mode );
   225 		    render_vertexes( *polygon, polygon+context_length, 4, vertex_length,
   226 				     render_mode );
   227 		    polygon += polygon_length;
   228 		}
   229 	    } else {
   230 		/* Polygon */
   231 		int i, first=-1, last = -1;
   232 		for( i=0; i<6; i++ ) {
   233 		    if( entry & (0x40000000>>i) ) {
   234 			if( first == -1 ) first = i;
   235 			last = i;
   236 		    }
   237 		}
   238 		if( first != -1 ) {
   239 		    first = 0;
   240 		    render_set_context(polygon, render_mode);
   241 		    render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
   242 				     (last-first+3), vertex_length, render_mode );
   243 		}
   244 	    }
   245 	}
   246     } while( 1 );
   247 }
   249 void render_autosort_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
   250     //WARN( "Autosort not implemented yet" );
   251     render_tile( tile_entry, render_mode, cheap_modifier_mode );
   252 }
   254 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, 
   255 			int clipx2, int clipy2 ) {
   257     pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
   258     int tile_sort;
   259     gboolean cheap_shadow;
   261     int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
   262     int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
   263     int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
   265     if( obj_config & 0x00200000 ) {
   266 	if( isp_config & 1 ) {
   267 	    tile_sort = 0;
   268 	} else {
   269 	    tile_sort = 2;
   270 	}
   271     } else {
   272 	tile_sort = 1;
   273     }
   275     cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
   277     struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
   279     struct timeval tv_start, tv_end;
   280     gettimeofday(&tv_start, NULL);
   281     fprintf( stderr, "Start render at %d.%d\n", tv_start.tv_sec, tv_start.tv_usec );
   282     glEnable( GL_SCISSOR_TEST );
   283     while( (segment->control & SEGMENT_END) == 0 ) {
   284 	// fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
   285 	int tilex = SEGMENT_X(segment->control);
   286 	int tiley = SEGMENT_Y(segment->control);
   288 	int x1 = tilex << 5;
   289 	int y1 = tiley << 5;
   290 	if( x1 + 32 <= clipx1 ||
   291 	    y1 + 32 <= clipy1 ||
   292 	    x1 >= clipx2 ||
   293 	    y1 >= clipy2 ) {
   294 	    /* Tile completely clipped, skip */
   295 	    segment++;
   296 	    continue;
   297 	}
   299 	/* Set a scissor on the visible part of the tile */
   300 	int w = MIN(x1+32, clipx2) - x1;
   301 	int h = MIN(y1+32, clipy2) - y1;
   302 	x1 = MAX(x1,clipx1);
   303 	y1 = MAX(y1,clipy1);
   304 	glScissor( x1, height-y1-h, w, h );
   306 	if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
   307 	    if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
   308 		/* TODO */
   309 	    }
   310 	    render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
   311 	}
   313 	if( (segment->trans_ptr & NO_POINTER) == 0 ) {
   314 	    if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
   315 		/* TODO */
   316 	    } 
   317 	    if( tile_sort == 2 || (tile_sort == 1 && (segment->control & SEGMENT_SORT_TRANS)) ) {
   318 		render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   319 	    } else {
   320 		render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
   321 	    }
   322 	}
   324 	if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
   325 	    render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
   326 	}
   327 	segment++;
   329     }
   330     glDisable( GL_SCISSOR_TEST );
   332     gettimeofday(&tv_end, NULL);
   333     timersub(&tv_end,&tv_start, &tv_start);
   334     fprintf( stderr, "Frame took %d.%06ds\n", tv_start.tv_sec, tv_start.tv_usec );
   336 }
.