2 * $Id: rendsort.c,v 1.4 2007-01-24 08:11:14 nkeynes Exp $
4 * PVR2 renderer routines for depth sorted polygons
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
19 #include "pvr2/pvr2.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 {
30 int vertex_length; /* Number of 32-bit words in vertex, or 0 for an unpacked vertex */
36 #define SENTINEL 0xDEADBEEF
39 * Count the number of triangles in the list starting at the given
42 int render_count_triangles( pvraddr_t tile_entry ) {
43 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
46 uint32_t entry = *tile_list++;
47 if( entry >> 28 == 0x0F ) {
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 */
57 for( i=0; i<6; i++ ) {
58 if( entry & (0x40000000>>i) ) {
67 static void compute_triangle_boxes( struct render_triangle *triangle, int num_triangles )
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]);
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 )
84 uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
85 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
88 uint32_t entry = *tile_list++;
89 if( entry >> 28 == 0x0F ) {
91 } else if( entry >> 28 == 0x0E ) {
92 tile_list = (uint32_t *)(video_base+(entry&0x007FFFFF));
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 ) {
104 if( (entry & 0xE0000000) == 0x80000000 ) {
106 int strip_count = ((entry >> 25) & 0x0F)+1;
107 int polygon_length = 3 * vertex_length + context_length;
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;
121 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
123 int strip_count = ((entry >> 25) & 0x0F)+1;
124 int polygon_length = 4 * vertex_length + context_length;
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);
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);
144 polygon += polygon_length;
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;
155 triangles[count].vertexes[0] = vertex + vertex_length;
156 triangles[count].vertexes[1] = vertex;
157 triangles[count].vertexes[2] = vertex + (vertex_length<<1);
159 triangles[count].vertexes[0] = vertex;
160 triangles[count].vertexes[1] = vertex + vertex_length;
161 triangles[count].vertexes[2] = vertex + (vertex_length<<1);
165 vertex += vertex_length;
170 if( count != num_triangles ) {
171 ERROR( "Extracted triangles do not match expected count!" );
175 void render_triangles( struct render_triangle *triangles, int num_triangles,
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 );
184 render_vertex_array( *triangles[i].polygon, (uint32_t **)triangles[i].vertexes, 3,
185 triangles[i].vertex_length, render_mode );
192 int compare_triangles( void *a, void *b )
194 struct render_triangle *tri1 = a;
195 struct render_triangle *tri2 = b;
196 if( tri1->minz < tri2->minz ) {
198 } else if( tri1->minz > tri2->minz ) {
205 void sort_triangles( struct render_triangle *triangles, int num_triangles )
207 qsort( triangles, num_triangles, sizeof(struct render_triangle), compare_triangles );
210 void render_autosort_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode )
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
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!" );
.