Search
lxdream.org :: lxdream/src/pvr2/rendsort.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendsort.c
changeset 319:5392aed6a982
prev318:363935d31859
next331:a6048d3a9a79
author nkeynes
date Thu Jan 25 08:16:02 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix 16-bit palettes
Up texture count to 128
view annotate diff log raw
     1 /**
     2  * $Id: rendsort.c,v 1.4 2007-01-24 08:11:14 nkeynes Exp $
     3  *
     4  * PVR2 renderer routines for depth sorted polygons
     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 extern char *video_base;
    23 extern gboolean pvr2_force_fragment_alpha;
    25 #define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
    26 #define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
    28 struct render_triangle {
    29     uint32_t *polygon;
    30     int vertex_length; /* Number of 32-bit words in vertex, or 0 for an unpacked vertex */
    31     float minx,miny,minz;
    32     float maxx,maxy,maxz;
    33     float *vertexes[3];
    34 };
    36 #define SENTINEL 0xDEADBEEF
    38 /**
    39  * Count the number of triangles in the list starting at the given 
    40  * pvr memory address.
    41  */
    42 int render_count_triangles( pvraddr_t tile_entry ) {
    43     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
    44     int count = 0;
    45     while(1) {
    46 	uint32_t entry = *tile_list++;
    47 	if( entry >> 28 == 0x0F ) {
    48 	    break;
    49 	} else if( entry >> 28 == 0x0E ) {
    50 	    tile_list = (uint32_t *)(video_base+(entry&0x007FFFFF));
    51 	} else if( entry >> 29 == 0x04 ) { /* Triangle array */
    52 	    count += ((entry >> 25) & 0x0F)+1;
    53 	} else if( entry >> 29 == 0x05 ) { /* Quad array */
    54 	    count += ((((entry >> 25) & 0x0F)+1)<<1);
    55 	} else { /* Polygon */
    56 	    int i;
    57 	    for( i=0; i<6; i++ ) {
    58 		if( entry & (0x40000000>>i) ) {
    59 		    count++;
    60 		}
    61 	    }
    62 	}
    63     }
    64     return count;
    65 }
    67 static void compute_triangle_boxes( struct render_triangle *triangle, int num_triangles )
    68 {
    69     int i;
    70     for( i=0; i<num_triangles; i++ ) {
    71 	triangle[i].minx = MIN3(triangle[i].vertexes[0][0],triangle[i].vertexes[1][0],triangle[i].vertexes[2][0]);
    72 	triangle[i].maxx = MAX3(triangle[i].vertexes[0][0],triangle[i].vertexes[1][0],triangle[i].vertexes[2][0]);
    73 	triangle[i].miny = MIN3(triangle[i].vertexes[0][1],triangle[i].vertexes[1][1],triangle[i].vertexes[2][1]);
    74 	triangle[i].maxy = MAX3(triangle[i].vertexes[0][1],triangle[i].vertexes[1][1],triangle[i].vertexes[2][1]);
    75 	triangle[i].minz = MIN3(triangle[i].vertexes[0][2],triangle[i].vertexes[1][2],triangle[i].vertexes[2][2]);
    76 	triangle[i].maxz = MAX3(triangle[i].vertexes[0][2],triangle[i].vertexes[1][2],triangle[i].vertexes[2][2]);
    77     }
    78 }
    80 void render_extract_triangles( pvraddr_t tile_entry, gboolean cheap_modifier_mode, 
    81 			       struct render_triangle *triangles, int num_triangles,
    82 			       struct vertex_unpacked *vertex_space, int render_mode )
    83 {
    84     uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
    85     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
    86     int count = 0;
    87     while(1) {
    88 	uint32_t entry = *tile_list++;
    89 	if( entry >> 28 == 0x0F ) {
    90 	    break;
    91 	} else if( entry >> 28 == 0x0E ) {
    92 	    tile_list = (uint32_t *)(video_base+(entry&0x007FFFFF));
    93 	} else {
    94 	    uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
    95 	    int is_modified = entry & 0x01000000;
    96 	    int vertex_length = (entry >> 21) & 0x07;
    97 	    int context_length = 3;
    98 	    if( is_modified && !cheap_modifier_mode ) {
    99 		context_length = 5;
   100 		vertex_length *= 2 ;
   101 	    }
   102 	    vertex_length += 3;
   104 	    if( (entry & 0xE0000000) == 0x80000000 ) {
   105 		/* Triangle(s) */
   106 		int strip_count = ((entry >> 25) & 0x0F)+1;
   107 		int polygon_length = 3 * vertex_length + context_length;
   108 		int i;
   109 		for( i=0; i<strip_count; i++ ) {
   110 		    float *vertex = (float *)(polygon+context_length);
   111 		    triangles[count].polygon = polygon;
   112 		    triangles[count].vertex_length = vertex_length;
   113 		    triangles[count].vertexes[0] = vertex;
   114 		    vertex+=vertex_length;
   115 		    triangles[count].vertexes[1] = vertex;
   116 		    vertex+=vertex_length;
   117 		    triangles[count].vertexes[2] = vertex;
   118 		    polygon += polygon_length;
   119 		    count++;
   120 		}
   121 	    } else if( (entry & 0xE0000000) == 0xA0000000 ) {
   122 		/* Quad(s) */
   123 		int strip_count = ((entry >> 25) & 0x0F)+1;
   124 		int polygon_length = 4 * vertex_length + context_length;
   126 		int i;
   127 		for( i=0; i<strip_count; i++ ) {
   128 		    render_unpack_quad( vertex_space, *polygon, (polygon+context_length),
   129 					vertex_length, render_mode );
   130 		    triangles[count].polygon = polygon;
   131 		    triangles[count].vertex_length = 0;
   132 		    triangles[count].vertexes[0] = (float *)vertex_space;
   133 		    triangles[count].vertexes[1] = (float *)(vertex_space + 1);
   134 		    triangles[count].vertexes[2] = (float *)(vertex_space + 3);
   135 		    count++;
   136 		    /* Preserve face direction */
   137 		    triangles[count].polygon = polygon;
   138 		    triangles[count].vertex_length = 0;
   139 		    triangles[count].vertexes[0] = (float *)(vertex_space + 1);
   140 		    triangles[count].vertexes[1] = (float *)(vertex_space + 2);
   141 		    triangles[count].vertexes[2] = (float *)(vertex_space + 3);
   142 		    count++;
   143 		    vertex_space += 4;
   144 		    polygon += polygon_length;
   145 		}
   146 	    } else {
   147 		/* Polygon */
   148 		int i, first=-1, last = -1;
   149 		float *vertex = (float *)polygon+context_length;
   150 		for( i=0; i<6; i++ ) {
   151 		    if( entry & (0x40000000>>i) ) {
   152 			triangles[count].polygon = polygon;
   153 			triangles[count].vertex_length = vertex_length;
   154 			if( i&1 ) {
   155 			    triangles[count].vertexes[0] = vertex + vertex_length;
   156 			    triangles[count].vertexes[1] = vertex;
   157 			    triangles[count].vertexes[2] = vertex + (vertex_length<<1);
   158 			} else {
   159 			    triangles[count].vertexes[0] = vertex;
   160 			    triangles[count].vertexes[1] = vertex + vertex_length;
   161 			    triangles[count].vertexes[2] = vertex + (vertex_length<<1);
   162 			}
   163 			count++;
   164 		    }
   165 		    vertex += vertex_length;
   166 		}
   167 	    }
   168 	}
   169     }
   170     if( count != num_triangles ) {
   171 	ERROR( "Extracted triangles do not match expected count!" );
   172     }
   173 }
   175 void render_triangles( struct render_triangle *triangles, int num_triangles,
   176 		       int render_mode )
   177 {
   178     int i,j, k, m = 0;
   179     for( i=0; i<num_triangles; i++ ) {
   180 	render_set_context( triangles[i].polygon, render_mode );
   181 	if( triangles[i].vertex_length == 0 ) {
   182 	    render_unpacked_vertex_array( *triangles[i].polygon, (struct vertex_unpacked **)triangles[i].vertexes, 3 );
   183 	} else {
   184 	    render_vertex_array( *triangles[i].polygon, (uint32_t **)triangles[i].vertexes, 3,
   185 				 triangles[i].vertex_length, render_mode );
   186 	}
   187     }
   190 }
   192 int compare_triangles( void *a, void *b ) 
   193 {
   194     struct render_triangle *tri1 = a;
   195     struct render_triangle *tri2 = b;
   196     if( tri1->minz < tri2->minz ) {
   197 	return -1;
   198     } else if( tri1->minz > tri2->minz ) {
   199 	return 1;
   200     } else {
   201 	return 0;
   202     }
   203 }
   205 void sort_triangles( struct render_triangle *triangles, int num_triangles )
   206 {
   207     qsort( triangles, num_triangles, sizeof(struct render_triangle), compare_triangles );
   208 } 
   210 void render_autosort_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) 
   211 {
   212     int num_triangles = render_count_triangles(tile_entry);
   213     if( num_triangles == 0 ) {
   214 	return; /* nothing to do */
   215     } else if( num_triangles == 1 ) { /* Triangle can hardly overlap with itself */
   216 	render_tile( tile_entry, render_mode, cheap_modifier_mode );
   217     } else { /* Ooh boy here we go... */
   218 	struct render_triangle triangles[num_triangles+1];
   219 	struct vertex_unpacked vertex_space[num_triangles << 1]; 
   220 	// Reserve space for num_triangles / 2 * 4 vertexes (maximum possible number of
   221 	// quad vertices)
   222 	triangles[num_triangles].polygon = (void *)SENTINEL;
   223 	render_extract_triangles(tile_entry, cheap_modifier_mode, triangles, num_triangles, vertex_space, render_mode);
   224 	compute_triangle_boxes(triangles, num_triangles);
   225 	sort_triangles( triangles, num_triangles );
   226 	render_triangles(triangles, num_triangles, render_mode);
   227 	if( triangles[num_triangles].polygon != (void *)SENTINEL ) {
   228 	    fprintf( stderr, "Triangle overflow in render_autosort_tile!" );
   229 	}
   230     }
   231 }
.