Search
lxdream.org :: lxdream/src/pvr2/rendsort.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendsort.c
changeset 1133:f3da7d810d5c
prev1066:ddffe9d2b332
next1137:4799d64b3478
author nkeynes
date Wed Oct 20 17:56:59 2010 +1000 (10 years ago)
permissions -rw-r--r--
last change Perform backface culling in scene preparation rather than leaving it to the
GL - this is a huge performance win, at least on the 9400M - changing cull
state appears to be very expensive, whereas the CPU needed to do the same
job is only just barely measurable.
file annotate diff log raw
nkeynes@222
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@222
     3
 *
nkeynes@222
     4
 * PVR2 renderer routines for depth sorted polygons
nkeynes@222
     5
 *
nkeynes@222
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@222
     7
 *
nkeynes@222
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@222
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@222
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@222
    11
 * (at your option) any later version.
nkeynes@222
    12
 *
nkeynes@222
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@222
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@222
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@222
    16
 * GNU General Public License for more details.
nkeynes@222
    17
 */
nkeynes@222
    18
#include <sys/time.h>
nkeynes@645
    19
#include <string.h>
nkeynes@645
    20
#include <assert.h>
nkeynes@222
    21
#include "pvr2/pvr2.h"
nkeynes@645
    22
#include "pvr2/scene.h"
nkeynes@222
    23
#include "asic.h"
nkeynes@222
    24
nkeynes@222
    25
#define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
nkeynes@222
    26
#define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
nkeynes@876
    27
#define EPSILON 0.0001
nkeynes@222
    28
nkeynes@645
    29
struct sort_triangle {
nkeynes@645
    30
    struct polygon_struct *poly;
nkeynes@645
    31
    int triangle_num; // triangle number in the poly, from 0
nkeynes@876
    32
    /* plane equation */
nkeynes@876
    33
    float mx, my, mz, d;
nkeynes@876
    34
    float bounds[6]; /* x1,x2,y1,y2,z1,z2 */
nkeynes@222
    35
};
nkeynes@222
    36
nkeynes@318
    37
#define SENTINEL 0xDEADBEEF
nkeynes@318
    38
nkeynes@222
    39
/**
nkeynes@222
    40
 * Count the number of triangles in the list starting at the given 
nkeynes@1133
    41
 * pvr memory address. This is an upper bound as it includes
nkeynes@1133
    42
 * triangles that have been culled out.
nkeynes@222
    43
 */
nkeynes@876
    44
static int sort_count_triangles( pvraddr_t tile_entry ) {
nkeynes@934
    45
    uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
nkeynes@222
    46
    int count = 0;
nkeynes@222
    47
    while(1) {
nkeynes@736
    48
        uint32_t entry = *tile_list++;
nkeynes@736
    49
        if( entry >> 28 == 0x0F ) {
nkeynes@736
    50
            break;
nkeynes@736
    51
        } else if( entry >> 28 == 0x0E ) {
nkeynes@934
    52
            tile_list = (uint32_t *)(pvr2_main_ram+(entry&0x007FFFFF));
nkeynes@736
    53
        } else if( entry >> 29 == 0x04 ) { /* Triangle array */
nkeynes@736
    54
            count += ((entry >> 25) & 0x0F)+1;
nkeynes@736
    55
        } else if( entry >> 29 == 0x05 ) { /* Quad array */
nkeynes@736
    56
            count += ((((entry >> 25) & 0x0F)+1)<<1);
nkeynes@736
    57
        } else { /* Polygon */
nkeynes@1133
    58
            struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@1133
    59
            while( poly != NULL ) {
nkeynes@1133
    60
                if( poly->vertex_count != 0 )
nkeynes@1133
    61
                    count += poly->vertex_count-2;
nkeynes@1133
    62
                poly = poly->sub_next;
nkeynes@736
    63
            }
nkeynes@736
    64
        }
nkeynes@222
    65
    }
nkeynes@222
    66
    return count;
nkeynes@222
    67
}
nkeynes@222
    68
nkeynes@876
    69
static void sort_add_triangle( struct sort_triangle *triangle, struct polygon_struct *poly, int index )
nkeynes@876
    70
{
nkeynes@876
    71
    struct vertex_struct *vertexes = &pvr2_scene.vertex_array[poly->vertex_index+index];
nkeynes@876
    72
    triangle->poly = poly;
nkeynes@876
    73
    triangle->triangle_num = index;
nkeynes@876
    74
nkeynes@876
    75
    /* Compute triangle bounding-box */
nkeynes@876
    76
    triangle->bounds[0] = MIN3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
nkeynes@876
    77
    triangle->bounds[1] = MAX3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
nkeynes@876
    78
    triangle->bounds[2] = MIN3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
nkeynes@876
    79
    triangle->bounds[3] = MAX3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
nkeynes@876
    80
    triangle->bounds[4] = MIN3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
nkeynes@876
    81
    triangle->bounds[5] = MAX3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
nkeynes@876
    82
nkeynes@876
    83
    /* Compute plane equation */
nkeynes@876
    84
    float sx = vertexes[1].x - vertexes[0].x;
nkeynes@876
    85
    float sy = vertexes[1].y - vertexes[0].y;
nkeynes@876
    86
    float sz = vertexes[1].z - vertexes[0].z;
nkeynes@876
    87
    float tx = vertexes[2].x - vertexes[0].x;
nkeynes@876
    88
    float ty = vertexes[2].y - vertexes[0].y;
nkeynes@876
    89
    float tz = vertexes[2].z - vertexes[0].z;
nkeynes@876
    90
    triangle->mx = sy*tz - sz*ty;
nkeynes@876
    91
    triangle->my = sz*tx - sx*tz;
nkeynes@876
    92
    triangle->mz = sx*ty - sy*tx;
nkeynes@876
    93
    triangle->d = -vertexes[0].x*triangle->mx - 
nkeynes@876
    94
                  vertexes[0].y*triangle->my - 
nkeynes@876
    95
                  vertexes[0].z*triangle->mz;
nkeynes@876
    96
}
nkeynes@876
    97
nkeynes@1133
    98
nkeynes@1133
    99
nkeynes@645
   100
/**
nkeynes@645
   101
 * Extract a triangle list from the tile (basically indexes into the polygon list, plus
nkeynes@650
   102
 * computing maxz while we go through it
nkeynes@645
   103
 */
nkeynes@645
   104
int sort_extract_triangles( pvraddr_t tile_entry, struct sort_triangle *triangles )
nkeynes@222
   105
{
nkeynes@934
   106
    uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
nkeynes@862
   107
    int strip_count;
nkeynes@862
   108
    struct polygon_struct *poly;
nkeynes@862
   109
    int count = 0, i;
nkeynes@862
   110
nkeynes@222
   111
    while(1) {
nkeynes@736
   112
        uint32_t entry = *tile_list++;
nkeynes@862
   113
        switch( entry >> 28 ) {
nkeynes@862
   114
        case 0x0F:
nkeynes@862
   115
            return count; // End-of-list
nkeynes@862
   116
        case 0x0E:
nkeynes@934
   117
            tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
nkeynes@736
   118
            break;
nkeynes@862
   119
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@862
   120
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@862
   121
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@862
   122
            while( strip_count > 0 ) {
nkeynes@862
   123
                assert( poly != NULL );
nkeynes@1133
   124
                for( i=0; i+2<poly->vertex_count; i++ ) {
nkeynes@1133
   125
                    /* Note: tris + quads can't have sub-polys */
nkeynes@876
   126
                    sort_add_triangle( &triangles[count], poly, i );
nkeynes@736
   127
                    count++;
nkeynes@736
   128
                }
nkeynes@862
   129
                poly = poly->next;
nkeynes@862
   130
                strip_count--;
nkeynes@862
   131
            }
nkeynes@862
   132
            break;
nkeynes@862
   133
        default:
nkeynes@862
   134
            if( entry & 0x7E000000 ) {
nkeynes@862
   135
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@1133
   136
                /* FIXME: This could end up including a triangle that was
nkeynes@1133
   137
                 * excluded from the tile, if it is part of a strip that
nkeynes@1133
   138
                 * still has some other triangles in the tile.
nkeynes@1133
   139
                 * (This couldn't happen with TA output though).
nkeynes@1133
   140
                 */
nkeynes@1133
   141
                while( poly != NULL ) {
nkeynes@1133
   142
                    for( i=0; i+2<poly->vertex_count; i++ ) {
nkeynes@876
   143
                        sort_add_triangle( &triangles[count], poly, i );
nkeynes@736
   144
                        count++;
nkeynes@736
   145
                    }
nkeynes@1133
   146
                    poly = poly->sub_next;
nkeynes@736
   147
                }
nkeynes@736
   148
            }
nkeynes@736
   149
        }
nkeynes@862
   150
    }       
nkeynes@222
   151
nkeynes@222
   152
}
nkeynes@222
   153
nkeynes@876
   154
void sort_render_triangles( struct sort_triangle **triangles, int num_triangles )
nkeynes@222
   155
{
nkeynes@429
   156
    int i;
nkeynes@222
   157
    for( i=0; i<num_triangles; i++ ) {
nkeynes@876
   158
        struct polygon_struct *poly = triangles[i]->poly;
nkeynes@736
   159
        if( poly->tex_id != -1 ) {
nkeynes@736
   160
            glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@736
   161
        }
nkeynes@865
   162
        render_set_context( poly->context, GL_GEQUAL );
nkeynes@736
   163
        glDepthMask(GL_FALSE);
nkeynes@876
   164
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + triangles[i]->triangle_num, 3 );
nkeynes@222
   165
    }
nkeynes@222
   166
}
nkeynes@222
   167
nkeynes@876
   168
static int sort_triangle_compare( const void *a, const void *b ) 
nkeynes@222
   169
{
nkeynes@645
   170
    const struct sort_triangle *tri1 = a;
nkeynes@645
   171
    const struct sort_triangle *tri2 = b;
nkeynes@876
   172
    if( tri1->bounds[5] <= tri2->bounds[4] ) 
nkeynes@876
   173
        return 1; /* tri1 is entirely under tri2 */
nkeynes@876
   174
    else if( tri2->bounds[5] <= tri1->bounds[4] )
nkeynes@876
   175
        return -1;  /* tri2 is entirely under tri1 */
nkeynes@876
   176
    else if( tri1->bounds[1] <= tri2->bounds[0] ||
nkeynes@876
   177
             tri2->bounds[1] <= tri1->bounds[0] ||
nkeynes@876
   178
             tri1->bounds[3] <= tri2->bounds[2] ||
nkeynes@876
   179
             tri2->bounds[3] <= tri1->bounds[2] )
nkeynes@876
   180
        return 0; /* tri1 and tri2 don't actually overlap at all */
nkeynes@876
   181
    else { 
nkeynes@876
   182
        struct vertex_struct *tri1v = &pvr2_scene.vertex_array[tri1->poly->vertex_index + tri1->triangle_num];
nkeynes@876
   183
        struct vertex_struct *tri2v = &pvr2_scene.vertex_array[tri2->poly->vertex_index + tri2->triangle_num];
nkeynes@876
   184
        float v[3];
nkeynes@876
   185
        int i;
nkeynes@876
   186
        for( i=0; i<3; i++ ) {
nkeynes@876
   187
            v[i] = tri1->mx * tri2v[i].x + tri1->my * tri2v[i].y + tri1->mz * tri2v[i].z + tri1->d;
nkeynes@876
   188
            if( v[i] > -EPSILON && v[i] < EPSILON ) v[i] = 0;
nkeynes@876
   189
        }
nkeynes@876
   190
        if( v[0] == 0 && v[1] == 0 && v[2] == 0 ) {
nkeynes@876
   191
            return 0; /* coplanar */
nkeynes@876
   192
        }
nkeynes@876
   193
        if( (v[0] >=0 && v[1] >= 0 && v[2] >= 0) ||
nkeynes@876
   194
            (v[0] <= 0 && v[1] <= 0 && v[2] <= 0) ) {
nkeynes@876
   195
            /* Tri is on one side of the plane. Pick an arbitrary point to determine which side */
nkeynes@876
   196
            float t1z = -(tri1->mx * tri2v[0].x + tri1->my * tri2v[0].y + tri1->d) / tri1->mz;
nkeynes@876
   197
            return tri2v[0].z - t1z;
nkeynes@876
   198
        }
nkeynes@876
   199
        
nkeynes@876
   200
        /* If the above test failed, then tri2 intersects tri1's plane. This
nkeynes@876
   201
         * doesn't necessarily mean the triangles intersect (although they may).
nkeynes@876
   202
         * For now just return 0, and come back to this later as it's a fairly
nkeynes@876
   203
         * uncommon case in practice. 
nkeynes@876
   204
         */
nkeynes@876
   205
        return 0;
nkeynes@876
   206
    }
nkeynes@222
   207
}
nkeynes@222
   208
nkeynes@876
   209
/**
nkeynes@876
   210
 * This is pretty much a standard merge sort (Unfortunately can't depend on
nkeynes@876
   211
 * the system to provide one. Note we can't use quicksort here - the sort
nkeynes@876
   212
 * must be stable to preserve the order of coplanar triangles.
nkeynes@876
   213
 */
nkeynes@876
   214
static void sort_triangles( struct sort_triangle **triangles, int num_triangles, struct sort_triangle **out )
nkeynes@222
   215
{
nkeynes@876
   216
    if( num_triangles > 2 ) {
nkeynes@876
   217
        int l = num_triangles>>1, r=num_triangles-l, i=0,j=0;
nkeynes@876
   218
        struct sort_triangle *left[l];
nkeynes@876
   219
        struct sort_triangle *right[r];
nkeynes@876
   220
        sort_triangles( triangles, l, left );
nkeynes@876
   221
        sort_triangles( triangles+l, r, right );
nkeynes@876
   222
        
nkeynes@876
   223
        /* Merge */
nkeynes@876
   224
        while(1) {
nkeynes@876
   225
            if( sort_triangle_compare(left[i], right[j]) <= 0 ) {
nkeynes@876
   226
                *out++ = left[i++];
nkeynes@876
   227
                if( i == l ) {
nkeynes@876
   228
                    memcpy( out, &right[j], (r-j)*sizeof(struct sort_triangle *) );        
nkeynes@876
   229
                    break;
nkeynes@876
   230
                }
nkeynes@876
   231
            } else {
nkeynes@876
   232
                *out++ = right[j++];
nkeynes@876
   233
                if( j == r ) {
nkeynes@876
   234
                    memcpy( out, &left[i], (l-i)*sizeof(struct sort_triangle *) );
nkeynes@876
   235
                    break;
nkeynes@876
   236
                }
nkeynes@876
   237
            }
nkeynes@876
   238
        }
nkeynes@876
   239
    } else if( num_triangles == 2 ) {
nkeynes@876
   240
        if( sort_triangle_compare(triangles[0], triangles[1]) <= 0 ) {
nkeynes@876
   241
            out[0] = triangles[0];
nkeynes@876
   242
            out[1] = triangles[1];
nkeynes@876
   243
        } else {
nkeynes@876
   244
            struct sort_triangle *tmp = triangles[0];
nkeynes@876
   245
            out[0] = triangles[1];
nkeynes@876
   246
            out[1] = tmp;
nkeynes@876
   247
        }
nkeynes@876
   248
    } else {
nkeynes@876
   249
        out[0] = triangles[0];
nkeynes@876
   250
    }
nkeynes@222
   251
} 
nkeynes@736
   252
nkeynes@648
   253
void render_autosort_tile( pvraddr_t tile_entry, int render_mode ) 
nkeynes@222
   254
{
nkeynes@645
   255
    int num_triangles = sort_count_triangles(tile_entry);
nkeynes@222
   256
    if( num_triangles == 0 ) {
nkeynes@736
   257
        return; /* nothing to do */
nkeynes@222
   258
    } else if( num_triangles == 1 ) { /* Triangle can hardly overlap with itself */
nkeynes@893
   259
        gl_render_tilelist(tile_entry, GL_GEQUAL);
nkeynes@222
   260
    } else { /* Ooh boy here we go... */
nkeynes@876
   261
        int i;
nkeynes@736
   262
        struct sort_triangle triangles[num_triangles+1];
nkeynes@876
   263
        struct sort_triangle *triangle_order[num_triangles+1];
nkeynes@736
   264
        triangles[num_triangles].poly = (void *)SENTINEL;
nkeynes@876
   265
        for( i=0; i<num_triangles; i++ ) {
nkeynes@876
   266
            triangle_order[i] = &triangles[i];
nkeynes@876
   267
        }
nkeynes@736
   268
        int extracted_triangles = sort_extract_triangles(tile_entry, triangles);
nkeynes@1133
   269
        assert( extracted_triangles <= num_triangles );
nkeynes@1133
   270
        sort_triangles( triangle_order, extracted_triangles, triangle_order );
nkeynes@1133
   271
        sort_render_triangles(triangle_order, extracted_triangles);
nkeynes@736
   272
        glCullFace(GL_BACK);
nkeynes@736
   273
        assert( triangles[num_triangles].poly == (void *)SENTINEL );
nkeynes@222
   274
    }
nkeynes@222
   275
}
.