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.
21 #include "pvr2/pvr2.h"
22 #include "pvr2/scene.h"
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)) )
27 #define EPSILON 0.0001
29 struct sort_triangle {
30 struct polygon_struct *poly;
31 int triangle_num; // triangle number in the poly, from 0
34 float bounds[6]; /* x1,x2,y1,y2,z1,z2 */
37 #define SENTINEL 0xDEADBEEF
40 * Count the number of triangles in the list starting at the given
43 static int sort_count_triangles( pvraddr_t tile_entry ) {
44 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
47 uint32_t entry = *tile_list++;
48 if( entry >> 28 == 0x0F ) {
50 } else if( entry >> 28 == 0x0E ) {
51 tile_list = (uint32_t *)(video_base+(entry&0x007FFFFF));
52 } else if( entry >> 29 == 0x04 ) { /* Triangle array */
53 count += ((entry >> 25) & 0x0F)+1;
54 } else if( entry >> 29 == 0x05 ) { /* Quad array */
55 count += ((((entry >> 25) & 0x0F)+1)<<1);
56 } else { /* Polygon */
58 for( i=0; i<6; i++ ) {
59 if( entry & (0x40000000>>i) ) {
68 static void sort_add_triangle( struct sort_triangle *triangle, struct polygon_struct *poly, int index )
70 struct vertex_struct *vertexes = &pvr2_scene.vertex_array[poly->vertex_index+index];
71 triangle->poly = poly;
72 triangle->triangle_num = index;
74 /* Compute triangle bounding-box */
75 triangle->bounds[0] = MIN3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
76 triangle->bounds[1] = MAX3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
77 triangle->bounds[2] = MIN3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
78 triangle->bounds[3] = MAX3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
79 triangle->bounds[4] = MIN3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
80 triangle->bounds[5] = MAX3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
82 /* Compute plane equation */
83 float sx = vertexes[1].x - vertexes[0].x;
84 float sy = vertexes[1].y - vertexes[0].y;
85 float sz = vertexes[1].z - vertexes[0].z;
86 float tx = vertexes[2].x - vertexes[0].x;
87 float ty = vertexes[2].y - vertexes[0].y;
88 float tz = vertexes[2].z - vertexes[0].z;
89 triangle->mx = sy*tz - sz*ty;
90 triangle->my = sz*tx - sx*tz;
91 triangle->mz = sx*ty - sy*tx;
92 triangle->d = -vertexes[0].x*triangle->mx -
93 vertexes[0].y*triangle->my -
94 vertexes[0].z*triangle->mz;
98 * Extract a triangle list from the tile (basically indexes into the polygon list, plus
99 * computing maxz while we go through it
101 int sort_extract_triangles( pvraddr_t tile_entry, struct sort_triangle *triangles )
103 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
105 struct polygon_struct *poly;
109 uint32_t entry = *tile_list++;
110 switch( entry >> 28 ) {
112 return count; // End-of-list
114 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
116 case 0x08: case 0x09: case 0x0A: case 0x0B:
117 strip_count = ((entry >> 25) & 0x0F)+1;
118 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
119 while( strip_count > 0 ) {
120 assert( poly != NULL );
121 for( i=0; i<poly->vertex_count-2; i++ ) {
122 sort_add_triangle( &triangles[count], poly, i );
130 if( entry & 0x7E000000 ) {
131 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
132 for( i=0; i<6; i++ ) {
133 if( entry & (0x40000000>>i) ) {
134 sort_add_triangle( &triangles[count], poly, i );
144 void sort_render_triangles( struct sort_triangle **triangles, int num_triangles )
147 for( i=0; i<num_triangles; i++ ) {
148 struct polygon_struct *poly = triangles[i]->poly;
149 if( poly->tex_id != -1 ) {
150 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
152 render_set_context( poly->context, GL_GEQUAL );
153 glDepthMask(GL_FALSE);
154 /* Fix cull direction */
155 if( triangles[i]->triangle_num & 1 ) {
156 glCullFace(GL_FRONT);
161 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + triangles[i]->triangle_num, 3 );
165 static int sort_triangle_compare( const void *a, const void *b )
167 const struct sort_triangle *tri1 = a;
168 const struct sort_triangle *tri2 = b;
169 if( tri1->bounds[5] <= tri2->bounds[4] )
170 return 1; /* tri1 is entirely under tri2 */
171 else if( tri2->bounds[5] <= tri1->bounds[4] )
172 return -1; /* tri2 is entirely under tri1 */
173 else if( tri1->bounds[1] <= tri2->bounds[0] ||
174 tri2->bounds[1] <= tri1->bounds[0] ||
175 tri1->bounds[3] <= tri2->bounds[2] ||
176 tri2->bounds[3] <= tri1->bounds[2] )
177 return 0; /* tri1 and tri2 don't actually overlap at all */
179 struct vertex_struct *tri1v = &pvr2_scene.vertex_array[tri1->poly->vertex_index + tri1->triangle_num];
180 struct vertex_struct *tri2v = &pvr2_scene.vertex_array[tri2->poly->vertex_index + tri2->triangle_num];
183 for( i=0; i<3; i++ ) {
184 v[i] = tri1->mx * tri2v[i].x + tri1->my * tri2v[i].y + tri1->mz * tri2v[i].z + tri1->d;
185 if( v[i] > -EPSILON && v[i] < EPSILON ) v[i] = 0;
187 if( v[0] == 0 && v[1] == 0 && v[2] == 0 ) {
188 return 0; /* coplanar */
190 if( (v[0] >=0 && v[1] >= 0 && v[2] >= 0) ||
191 (v[0] <= 0 && v[1] <= 0 && v[2] <= 0) ) {
192 /* Tri is on one side of the plane. Pick an arbitrary point to determine which side */
193 float t1z = -(tri1->mx * tri2v[0].x + tri1->my * tri2v[0].y + tri1->d) / tri1->mz;
194 return tri2v[0].z - t1z;
197 /* If the above test failed, then tri2 intersects tri1's plane. This
198 * doesn't necessarily mean the triangles intersect (although they may).
199 * For now just return 0, and come back to this later as it's a fairly
200 * uncommon case in practice.
207 * This is pretty much a standard merge sort (Unfortunately can't depend on
208 * the system to provide one. Note we can't use quicksort here - the sort
209 * must be stable to preserve the order of coplanar triangles.
211 static void sort_triangles( struct sort_triangle **triangles, int num_triangles, struct sort_triangle **out )
213 if( num_triangles > 2 ) {
214 int l = num_triangles>>1, r=num_triangles-l, i=0,j=0;
215 struct sort_triangle *left[l];
216 struct sort_triangle *right[r];
217 sort_triangles( triangles, l, left );
218 sort_triangles( triangles+l, r, right );
222 if( sort_triangle_compare(left[i], right[j]) <= 0 ) {
225 memcpy( out, &right[j], (r-j)*sizeof(struct sort_triangle *) );
231 memcpy( out, &left[i], (l-i)*sizeof(struct sort_triangle *) );
236 } else if( num_triangles == 2 ) {
237 if( sort_triangle_compare(triangles[0], triangles[1]) <= 0 ) {
238 out[0] = triangles[0];
239 out[1] = triangles[1];
241 struct sort_triangle *tmp = triangles[0];
242 out[0] = triangles[1];
246 out[0] = triangles[0];
250 void render_autosort_tile( pvraddr_t tile_entry, int render_mode )
252 int num_triangles = sort_count_triangles(tile_entry);
253 if( num_triangles == 0 ) {
254 return; /* nothing to do */
255 } else if( num_triangles == 1 ) { /* Triangle can hardly overlap with itself */
256 gl_render_tilelist(tile_entry, GL_GEQUAL);
257 } else { /* Ooh boy here we go... */
259 struct sort_triangle triangles[num_triangles+1];
260 struct sort_triangle *triangle_order[num_triangles+1];
261 triangles[num_triangles].poly = (void *)SENTINEL;
262 for( i=0; i<num_triangles; i++ ) {
263 triangle_order[i] = &triangles[i];
265 int extracted_triangles = sort_extract_triangles(tile_entry, triangles);
266 assert( extracted_triangles == num_triangles );
267 sort_triangles( triangle_order, num_triangles, triangle_order );
268 sort_render_triangles(triangle_order, num_triangles);
270 assert( triangles[num_triangles].poly == (void *)SENTINEL );
.